Update ooo320-m1
[ooovba.git] / xmloff / source / style / fonthdl.cxx
blob5ad8d761ae262e789d0f86c36c4f9c50b96fa213
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fonthdl.cxx,v $
10 * $Revision: 1.15 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
34 #ifndef _XMLOFF_PROPERTYHANDLER_FONTTYPES_HXX
35 #include <fonthdl.hxx>
36 #endif
37 #include <xmloff/xmltoken.hxx>
38 #include <xmloff/xmluconv.hxx>
39 #include <rtl/ustrbuf.hxx>
40 #include <com/sun/star/uno/Any.hxx>
41 #include <vcl/vclenum.hxx>
43 #ifndef _XMLOFF_XMLEMENT_HXX
44 #include <xmloff/xmlelement.hxx>
45 #endif
46 #include <tools/string.hxx>
48 using ::rtl::OUString;
49 using ::rtl::OUStringBuffer;
51 using namespace ::com::sun::star;
52 using namespace ::xmloff::token;
54 const SvXMLEnumMapEntry* lcl_getFontFamilyGenericMapping()
56 static SvXMLEnumMapEntry __READONLY_DATA aFontFamilyGenericMapping[] =
58 { XML_DECORATIVE, FAMILY_DECORATIVE },
60 { XML_MODERN, FAMILY_MODERN },
61 { XML_ROMAN, FAMILY_ROMAN },
62 { XML_SCRIPT, FAMILY_SCRIPT },
63 { XML_SWISS, FAMILY_SWISS },
64 { XML_SYSTEM, FAMILY_SYSTEM },
65 { XML_TOKEN_INVALID, 0 }
67 return aFontFamilyGenericMapping;
70 static SvXMLEnumMapEntry __READONLY_DATA aFontPitchMapping[] =
72 { XML_FIXED, PITCH_FIXED },
73 { XML_VARIABLE, PITCH_VARIABLE },
74 { XML_TOKEN_INVALID, 0 }
76 ///////////////////////////////////////////////////////////////////////////////
78 // class XMLFontFamilyNamePropHdl
81 XMLFontFamilyNamePropHdl::~XMLFontFamilyNamePropHdl()
83 // Nothing to do
86 sal_Bool XMLFontFamilyNamePropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
88 sal_Bool bRet = sal_False;
89 String sValue;
90 sal_Int32 nPos = 0;
94 sal_Int32 nFirst = nPos;
95 nPos = SvXMLUnitConverter::indexOfComma( rStrImpValue, nPos );
96 sal_Int32 nLast = (-1 == nPos ? rStrImpValue.getLength() : nPos);
97 if( nLast > 0 )
98 nLast--;
100 // skip trailing blanks
101 while( sal_Unicode(' ') == rStrImpValue[nLast] && nLast > nFirst )
102 nLast--;
104 // skip leading blanks
105 while( sal_Unicode(' ') == rStrImpValue[nFirst] && nFirst <= nLast )
106 nFirst++;
108 // remove quotes
109 sal_Unicode c = rStrImpValue[nFirst];
110 if( nFirst < nLast && (sal_Unicode('\'') == c || sal_Unicode('\"') == c) && rStrImpValue[nLast] == c )
112 nFirst++;
113 nLast--;
116 if( nFirst <= nLast )
118 if( sValue.Len() != 0 )
119 sValue += sal_Unicode(';');
121 OUString sTemp = rStrImpValue.copy( nFirst, nLast-nFirst+1 );
122 sValue += sTemp.getStr();
125 if( -1 != nPos )
126 nPos++;
128 while( -1 != nPos );
130 if( sValue.Len() )
132 rValue <<= OUString(sValue.GetBuffer());
133 bRet = sal_True;
136 return bRet;
139 sal_Bool XMLFontFamilyNamePropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
141 sal_Bool bRet = sal_False;
142 OUString aStrFamilyName;
144 if( rValue >>= aStrFamilyName )
146 OUStringBuffer sValue( aStrFamilyName.getLength() + 2L );
147 sal_Int32 nPos = 0L;
150 sal_Int32 nFirst = nPos;
151 nPos = aStrFamilyName.indexOf( sal_Unicode(';'), nPos );
152 sal_Int32 nLast = (-1L == nPos ? aStrFamilyName.getLength() : nPos);
154 // Set position to the character behind the ';', so we won't
155 // forget this.
156 if( -1L != nPos )
157 nPos++;
159 // If the property value was empty, we stop now.
160 // If there is a ';' at the first position, the empty name
161 // at the start will be removed.
162 if( 0L == nLast )
163 continue;
165 // nFirst and nLast now denote the first and last character of
166 // one font name.
167 nLast--;
169 // skip trailing blanks
170 while( sal_Unicode(' ') == aStrFamilyName[nLast] && nLast > nFirst )
171 nLast--;
173 // skip leading blanks
174 while( sal_Unicode(' ') == aStrFamilyName[nFirst] && nFirst <= nLast )
175 nFirst++;
177 if( nFirst <= nLast )
179 if( sValue.getLength() != 0L )
181 sValue.append( sal_Unicode( ',' ) );
182 sValue.append( sal_Unicode( ' ' ));
184 sal_Int32 nLen = nLast-nFirst+1L;
185 OUString sFamily( aStrFamilyName.copy( nFirst, nLen ) );
186 sal_Bool bQuote = sal_False;
187 for( sal_Int32 i=0; i < nLen; i++ )
189 sal_Unicode c = sFamily[i];
190 if( sal_Unicode(' ') == c || sal_Unicode(',') == c )
192 bQuote = sal_True;
193 break;
196 if( bQuote )
197 sValue.append( sal_Unicode('\'') );
198 sValue.append( sFamily );
199 if( bQuote )
200 sValue.append( sal_Unicode('\'') );
203 while( -1L != nPos );
205 rStrExpValue = sValue.makeStringAndClear();
207 bRet = sal_True;
210 return bRet;
213 ///////////////////////////////////////////////////////////////////////////////
215 // class XMLFontFamilyPropHdl
218 XMLFontFamilyPropHdl::~XMLFontFamilyPropHdl()
220 // Nothing to do
223 sal_Bool XMLFontFamilyPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
225 sal_uInt16 eNewFamily;
226 sal_Bool bRet = SvXMLUnitConverter::convertEnum( eNewFamily, rStrImpValue, lcl_getFontFamilyGenericMapping() );
227 if( bRet )
228 rValue <<= (sal_Int16)eNewFamily;
230 return bRet;
233 sal_Bool XMLFontFamilyPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
235 sal_Bool bRet = sal_False;
236 OUStringBuffer aOut;
238 sal_Int16 nFamily = sal_Int16();
239 if( rValue >>= nFamily )
241 FontFamily eFamily = (FontFamily)nFamily;
242 if( eFamily != FAMILY_DONTKNOW )
243 bRet = SvXMLUnitConverter::convertEnum( aOut, eFamily, lcl_getFontFamilyGenericMapping() );
246 rStrExpValue = aOut.makeStringAndClear();
248 return bRet;
251 ///////////////////////////////////////////////////////////////////////////////
253 // class XMLFontEncodingPropHdl
256 XMLFontEncodingPropHdl::~XMLFontEncodingPropHdl()
258 // Nothing to do
261 sal_Bool XMLFontEncodingPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
263 sal_Bool bRet = sal_True;
265 if( IsXMLToken( rStrImpValue, XML_X_SYMBOL ) )
266 rValue <<= (sal_Int16) RTL_TEXTENCODING_SYMBOL;
268 return bRet;
271 sal_Bool XMLFontEncodingPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
273 sal_Bool bRet = sal_False;
274 OUStringBuffer aOut;
275 sal_Int16 nSet = sal_Int16();
277 if( rValue >>= nSet )
279 if( (rtl_TextEncoding)nSet == RTL_TEXTENCODING_SYMBOL )
281 aOut.append( GetXMLToken(XML_X_SYMBOL) );
282 rStrExpValue = aOut.makeStringAndClear();
283 bRet = sal_True;
287 return bRet;
290 ///////////////////////////////////////////////////////////////////////////////
292 // class XMLFontPitchPropHdl
295 XMLFontPitchPropHdl::~XMLFontPitchPropHdl()
297 // Nothing to do
300 sal_Bool XMLFontPitchPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
302 USHORT eNewPitch;
303 sal_Bool bRet = SvXMLUnitConverter::convertEnum( eNewPitch, rStrImpValue, aFontPitchMapping );
304 if( bRet )
305 rValue <<= (sal_Int16)eNewPitch;
307 return bRet;
310 sal_Bool XMLFontPitchPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
312 sal_Bool bRet = sal_False;
313 sal_Int16 nPitch = sal_Int16();
314 OUStringBuffer aOut;
316 FontPitch ePitch = PITCH_DONTKNOW;
317 if( rValue >>= nPitch )
318 ePitch = (FontPitch)nPitch;
320 if( PITCH_DONTKNOW != ePitch )
322 bRet = SvXMLUnitConverter::convertEnum( aOut, ePitch, aFontPitchMapping, XML_FIXED );
323 rStrExpValue = aOut.makeStringAndClear();
326 return bRet;