bump product version to 4.1.6.2
[LibreOffice.git] / xmloff / source / style / fonthdl.cxx
blobf65efea29b7c67241f923de828c667bf224c1106
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <fonthdl.hxx>
23 #include <sax/tools/converter.hxx>
25 #include <xmloff/xmltoken.hxx>
26 #include <xmloff/xmluconv.hxx>
27 #include <rtl/ustrbuf.hxx>
28 #include <com/sun/star/uno/Any.hxx>
29 #include <tools/fontenum.hxx>
32 using namespace ::com::sun::star;
33 using namespace ::xmloff::token;
35 static const SvXMLEnumMapEntry* lcl_getFontFamilyGenericMapping()
37 static SvXMLEnumMapEntry const aFontFamilyGenericMapping[] =
39 { XML_DECORATIVE, FAMILY_DECORATIVE },
41 { XML_MODERN, FAMILY_MODERN },
42 { XML_ROMAN, FAMILY_ROMAN },
43 { XML_SCRIPT, FAMILY_SCRIPT },
44 { XML_SWISS, FAMILY_SWISS },
45 { XML_SYSTEM, FAMILY_SYSTEM },
46 { XML_TOKEN_INVALID, 0 }
48 return aFontFamilyGenericMapping;
51 static SvXMLEnumMapEntry const aFontPitchMapping[] =
53 { XML_FIXED, PITCH_FIXED },
54 { XML_VARIABLE, PITCH_VARIABLE },
55 { XML_TOKEN_INVALID, 0 }
57 ///////////////////////////////////////////////////////////////////////////////
59 // class XMLFontFamilyNamePropHdl
62 XMLFontFamilyNamePropHdl::~XMLFontFamilyNamePropHdl()
64 // Nothing to do
67 sal_Bool XMLFontFamilyNamePropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
69 sal_Bool bRet = sal_False;
70 OUStringBuffer sValue;
71 sal_Int32 nPos = 0;
75 sal_Int32 nFirst = nPos;
76 nPos = ::sax::Converter::indexOfComma( rStrImpValue, nPos );
77 sal_Int32 nLast = (-1 == nPos ? rStrImpValue.getLength() - 1 : nPos - 1);
79 // skip trailing blanks
80 while( nLast > nFirst && sal_Unicode(' ') == rStrImpValue[nLast] )
81 nLast--;
83 // skip leading blanks
84 while(nFirst <= nLast && sal_Unicode(' ') == rStrImpValue[nFirst])
85 nFirst++;
87 // remove quotes
88 sal_Unicode c = rStrImpValue[nFirst];
89 if( nFirst < nLast && (sal_Unicode('\'') == c || sal_Unicode('\"') == c) && rStrImpValue[nLast] == c )
91 nFirst++;
92 nLast--;
95 if( nFirst <= nLast )
97 if( !sValue.isEmpty() )
98 sValue.append(';');
100 sValue.append(rStrImpValue.copy( nFirst, nLast-nFirst+1));
103 if( -1 != nPos )
104 nPos++;
106 while( -1 != nPos );
108 if (!sValue.isEmpty())
110 rValue <<= sValue.makeStringAndClear();
111 bRet = sal_True;
114 return bRet;
117 sal_Bool XMLFontFamilyNamePropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
119 sal_Bool bRet = sal_False;
120 OUString aStrFamilyName;
122 if( rValue >>= aStrFamilyName )
124 OUStringBuffer sValue( aStrFamilyName.getLength() + 2L );
125 sal_Int32 nPos = 0;
128 sal_Int32 nFirst = nPos;
129 nPos = aStrFamilyName.indexOf( sal_Unicode(';'), nPos );
130 sal_Int32 nLast = (-1 == nPos ? aStrFamilyName.getLength() : nPos);
132 // Set position to the character behind the ';', so we won't
133 // forget this.
134 if( -1L != nPos )
135 nPos++;
137 // If the property value was empty, we stop now.
138 // If there is a ';' at the first position, the empty name
139 // at the start will be removed.
140 if( 0L == nLast )
141 continue;
143 // nFirst and nLast now denote the first and last character of
144 // one font name.
145 nLast--;
147 // skip trailing blanks
148 while( nLast > nFirst && sal_Unicode(' ') == aStrFamilyName[nLast] )
149 nLast--;
151 // skip leading blanks
152 while( nFirst <= nLast && sal_Unicode(' ') == aStrFamilyName[nFirst] )
153 nFirst++;
155 if( nFirst <= nLast )
157 if( sValue.getLength() != 0L )
159 sValue.append( sal_Unicode( ',' ) );
160 sValue.append( sal_Unicode( ' ' ));
162 sal_Int32 nLen = nLast-nFirst+1;
163 OUString sFamily( aStrFamilyName.copy( nFirst, nLen ) );
164 sal_Bool bQuote = sal_False;
165 for( sal_Int32 i=0; i < nLen; i++ )
167 sal_Unicode c = sFamily[i];
168 if( sal_Unicode(' ') == c || sal_Unicode(',') == c )
170 bQuote = sal_True;
171 break;
174 if( bQuote )
175 sValue.append( sal_Unicode('\'') );
176 sValue.append( sFamily );
177 if( bQuote )
178 sValue.append( sal_Unicode('\'') );
181 while( -1L != nPos );
183 rStrExpValue = sValue.makeStringAndClear();
185 bRet = sal_True;
188 return bRet;
191 ///////////////////////////////////////////////////////////////////////////////
193 // class XMLFontFamilyPropHdl
196 XMLFontFamilyPropHdl::~XMLFontFamilyPropHdl()
198 // Nothing to do
201 sal_Bool XMLFontFamilyPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
203 sal_uInt16 eNewFamily;
204 sal_Bool bRet = SvXMLUnitConverter::convertEnum( eNewFamily, rStrImpValue, lcl_getFontFamilyGenericMapping() );
205 if( bRet )
206 rValue <<= (sal_Int16)eNewFamily;
208 return bRet;
211 sal_Bool XMLFontFamilyPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
213 sal_Bool bRet = sal_False;
214 OUStringBuffer aOut;
216 sal_Int16 nFamily = sal_Int16();
217 if( rValue >>= nFamily )
219 FontFamily eFamily = (FontFamily)nFamily;
220 if( eFamily != FAMILY_DONTKNOW )
221 bRet = SvXMLUnitConverter::convertEnum( aOut, eFamily, lcl_getFontFamilyGenericMapping() );
224 rStrExpValue = aOut.makeStringAndClear();
226 return bRet;
229 ///////////////////////////////////////////////////////////////////////////////
231 // class XMLFontEncodingPropHdl
234 XMLFontEncodingPropHdl::~XMLFontEncodingPropHdl()
236 // Nothing to do
239 sal_Bool XMLFontEncodingPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
241 sal_Bool bRet = sal_True;
243 if( IsXMLToken( rStrImpValue, XML_X_SYMBOL ) )
244 rValue <<= (sal_Int16) RTL_TEXTENCODING_SYMBOL;
246 return bRet;
249 sal_Bool XMLFontEncodingPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
251 sal_Bool bRet = sal_False;
252 OUStringBuffer aOut;
253 sal_Int16 nSet = sal_Int16();
255 if( rValue >>= nSet )
257 if( (rtl_TextEncoding)nSet == RTL_TEXTENCODING_SYMBOL )
259 aOut.append( GetXMLToken(XML_X_SYMBOL) );
260 rStrExpValue = aOut.makeStringAndClear();
261 bRet = sal_True;
265 return bRet;
268 ///////////////////////////////////////////////////////////////////////////////
270 // class XMLFontPitchPropHdl
273 XMLFontPitchPropHdl::~XMLFontPitchPropHdl()
275 // Nothing to do
278 sal_Bool XMLFontPitchPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
280 sal_uInt16 eNewPitch;
281 sal_Bool bRet = SvXMLUnitConverter::convertEnum( eNewPitch, rStrImpValue, aFontPitchMapping );
282 if( bRet )
283 rValue <<= (sal_Int16)eNewPitch;
285 return bRet;
288 sal_Bool XMLFontPitchPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
290 sal_Bool bRet = sal_False;
291 sal_Int16 nPitch = sal_Int16();
292 OUStringBuffer aOut;
294 FontPitch ePitch = PITCH_DONTKNOW;
295 if( rValue >>= nPitch )
296 ePitch = (FontPitch)nPitch;
298 if( PITCH_DONTKNOW != ePitch )
300 bRet = SvXMLUnitConverter::convertEnum( aOut, ePitch, aFontPitchMapping, XML_FIXED );
301 rStrExpValue = aOut.makeStringAndClear();
304 return bRet;
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */