Bump for 3.6-28
[LibreOffice.git] / xmloff / source / style / fonthdl.cxx
blobf4d1705fa357c2bb771d25275c1febba1eb7ae0d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <fonthdl.hxx>
32 #include <sax/tools/converter.hxx>
34 #include <xmloff/xmltoken.hxx>
35 #include <xmloff/xmluconv.hxx>
36 #include <rtl/ustrbuf.hxx>
37 #include <com/sun/star/uno/Any.hxx>
38 #include <tools/fontenum.hxx>
40 using ::rtl::OUString;
41 using ::rtl::OUStringBuffer;
43 using namespace ::com::sun::star;
44 using namespace ::xmloff::token;
46 const SvXMLEnumMapEntry* lcl_getFontFamilyGenericMapping()
48 static SvXMLEnumMapEntry const aFontFamilyGenericMapping[] =
50 { XML_DECORATIVE, FAMILY_DECORATIVE },
52 { XML_MODERN, FAMILY_MODERN },
53 { XML_ROMAN, FAMILY_ROMAN },
54 { XML_SCRIPT, FAMILY_SCRIPT },
55 { XML_SWISS, FAMILY_SWISS },
56 { XML_SYSTEM, FAMILY_SYSTEM },
57 { XML_TOKEN_INVALID, 0 }
59 return aFontFamilyGenericMapping;
62 static SvXMLEnumMapEntry const aFontPitchMapping[] =
64 { XML_FIXED, PITCH_FIXED },
65 { XML_VARIABLE, PITCH_VARIABLE },
66 { XML_TOKEN_INVALID, 0 }
68 ///////////////////////////////////////////////////////////////////////////////
70 // class XMLFontFamilyNamePropHdl
73 XMLFontFamilyNamePropHdl::~XMLFontFamilyNamePropHdl()
75 // Nothing to do
78 sal_Bool XMLFontFamilyNamePropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
80 sal_Bool bRet = sal_False;
81 OUStringBuffer sValue;
82 sal_Int32 nPos = 0;
86 sal_Int32 nFirst = nPos;
87 nPos = ::sax::Converter::indexOfComma( rStrImpValue, nPos );
88 sal_Int32 nLast = (-1 == nPos ? rStrImpValue.getLength() - 1 : nPos - 1);
90 // skip trailing blanks
91 while( nLast > nFirst && sal_Unicode(' ') == rStrImpValue[nLast] )
92 nLast--;
94 // skip leading blanks
95 while(nFirst <= nLast && sal_Unicode(' ') == rStrImpValue[nFirst])
96 nFirst++;
98 // remove quotes
99 sal_Unicode c = rStrImpValue[nFirst];
100 if( nFirst < nLast && (sal_Unicode('\'') == c || sal_Unicode('\"') == c) && rStrImpValue[nLast] == c )
102 nFirst++;
103 nLast--;
106 if( nFirst <= nLast )
108 if( sValue.getLength() != 0 )
109 sValue.append(';');
111 sValue.append(rStrImpValue.copy( nFirst, nLast-nFirst+1));
114 if( -1 != nPos )
115 nPos++;
117 while( -1 != nPos );
119 if (sValue.getLength())
121 rValue <<= sValue.makeStringAndClear();
122 bRet = sal_True;
125 return bRet;
128 sal_Bool XMLFontFamilyNamePropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
130 sal_Bool bRet = sal_False;
131 OUString aStrFamilyName;
133 if( rValue >>= aStrFamilyName )
135 OUStringBuffer sValue( aStrFamilyName.getLength() + 2L );
136 sal_Int32 nPos = 0;
139 sal_Int32 nFirst = nPos;
140 nPos = aStrFamilyName.indexOf( sal_Unicode(';'), nPos );
141 sal_Int32 nLast = (-1 == nPos ? aStrFamilyName.getLength() : nPos);
143 // Set position to the character behind the ';', so we won't
144 // forget this.
145 if( -1L != nPos )
146 nPos++;
148 // If the property value was empty, we stop now.
149 // If there is a ';' at the first position, the empty name
150 // at the start will be removed.
151 if( 0L == nLast )
152 continue;
154 // nFirst and nLast now denote the first and last character of
155 // one font name.
156 nLast--;
158 // skip trailing blanks
159 while( nLast > nFirst && sal_Unicode(' ') == aStrFamilyName[nLast] )
160 nLast--;
162 // skip leading blanks
163 while( nFirst <= nLast && sal_Unicode(' ') == aStrFamilyName[nFirst] )
164 nFirst++;
166 if( nFirst <= nLast )
168 if( sValue.getLength() != 0L )
170 sValue.append( sal_Unicode( ',' ) );
171 sValue.append( sal_Unicode( ' ' ));
173 sal_Int32 nLen = nLast-nFirst+1;
174 OUString sFamily( aStrFamilyName.copy( nFirst, nLen ) );
175 sal_Bool bQuote = sal_False;
176 for( sal_Int32 i=0; i < nLen; i++ )
178 sal_Unicode c = sFamily[i];
179 if( sal_Unicode(' ') == c || sal_Unicode(',') == c )
181 bQuote = sal_True;
182 break;
185 if( bQuote )
186 sValue.append( sal_Unicode('\'') );
187 sValue.append( sFamily );
188 if( bQuote )
189 sValue.append( sal_Unicode('\'') );
192 while( -1L != nPos );
194 rStrExpValue = sValue.makeStringAndClear();
196 bRet = sal_True;
199 return bRet;
202 ///////////////////////////////////////////////////////////////////////////////
204 // class XMLFontFamilyPropHdl
207 XMLFontFamilyPropHdl::~XMLFontFamilyPropHdl()
209 // Nothing to do
212 sal_Bool XMLFontFamilyPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
214 sal_uInt16 eNewFamily;
215 sal_Bool bRet = SvXMLUnitConverter::convertEnum( eNewFamily, rStrImpValue, lcl_getFontFamilyGenericMapping() );
216 if( bRet )
217 rValue <<= (sal_Int16)eNewFamily;
219 return bRet;
222 sal_Bool XMLFontFamilyPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
224 sal_Bool bRet = sal_False;
225 OUStringBuffer aOut;
227 sal_Int16 nFamily = sal_Int16();
228 if( rValue >>= nFamily )
230 FontFamily eFamily = (FontFamily)nFamily;
231 if( eFamily != FAMILY_DONTKNOW )
232 bRet = SvXMLUnitConverter::convertEnum( aOut, eFamily, lcl_getFontFamilyGenericMapping() );
235 rStrExpValue = aOut.makeStringAndClear();
237 return bRet;
240 ///////////////////////////////////////////////////////////////////////////////
242 // class XMLFontEncodingPropHdl
245 XMLFontEncodingPropHdl::~XMLFontEncodingPropHdl()
247 // Nothing to do
250 sal_Bool XMLFontEncodingPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
252 sal_Bool bRet = sal_True;
254 if( IsXMLToken( rStrImpValue, XML_X_SYMBOL ) )
255 rValue <<= (sal_Int16) RTL_TEXTENCODING_SYMBOL;
257 return bRet;
260 sal_Bool XMLFontEncodingPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
262 sal_Bool bRet = sal_False;
263 OUStringBuffer aOut;
264 sal_Int16 nSet = sal_Int16();
266 if( rValue >>= nSet )
268 if( (rtl_TextEncoding)nSet == RTL_TEXTENCODING_SYMBOL )
270 aOut.append( GetXMLToken(XML_X_SYMBOL) );
271 rStrExpValue = aOut.makeStringAndClear();
272 bRet = sal_True;
276 return bRet;
279 ///////////////////////////////////////////////////////////////////////////////
281 // class XMLFontPitchPropHdl
284 XMLFontPitchPropHdl::~XMLFontPitchPropHdl()
286 // Nothing to do
289 sal_Bool XMLFontPitchPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
291 sal_uInt16 eNewPitch;
292 sal_Bool bRet = SvXMLUnitConverter::convertEnum( eNewPitch, rStrImpValue, aFontPitchMapping );
293 if( bRet )
294 rValue <<= (sal_Int16)eNewPitch;
296 return bRet;
299 sal_Bool XMLFontPitchPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
301 sal_Bool bRet = sal_False;
302 sal_Int16 nPitch = sal_Int16();
303 OUStringBuffer aOut;
305 FontPitch ePitch = PITCH_DONTKNOW;
306 if( rValue >>= nPitch )
307 ePitch = (FontPitch)nPitch;
309 if( PITCH_DONTKNOW != ePitch )
311 bRet = SvXMLUnitConverter::convertEnum( aOut, ePitch, aFontPitchMapping, XML_FIXED );
312 rStrExpValue = aOut.makeStringAndClear();
315 return bRet;
318 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */