xref: /trunk/main/xmloff/source/style/fonthdl.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 
31 #include <fonthdl.hxx>
32 #include <xmloff/xmltoken.hxx>
33 #include <xmloff/xmluconv.hxx>
34 #include <rtl/ustrbuf.hxx>
35 #include <com/sun/star/uno/Any.hxx>
36 #include <tools/fontenum.hxx>
37 
38 #include <tools/string.hxx>
39 
40 using ::rtl::OUString;
41 using ::rtl::OUStringBuffer;
42 
43 using namespace ::com::sun::star;
44 using namespace ::xmloff::token;
45 
46 const SvXMLEnumMapEntry* lcl_getFontFamilyGenericMapping()
47 {
48     static SvXMLEnumMapEntry __READONLY_DATA aFontFamilyGenericMapping[] =
49     {
50         { XML_DECORATIVE,       FAMILY_DECORATIVE },
51 
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               }
58     };
59     return aFontFamilyGenericMapping;
60 }
61 
62 static SvXMLEnumMapEntry __READONLY_DATA aFontPitchMapping[] =
63 {
64     { XML_FIXED,            PITCH_FIXED     },
65     { XML_VARIABLE,         PITCH_VARIABLE  },
66     { XML_TOKEN_INVALID,    0               }
67 };
68 ///////////////////////////////////////////////////////////////////////////////
69 //
70 // class XMLFontFamilyNamePropHdl
71 //
72 
73 XMLFontFamilyNamePropHdl::~XMLFontFamilyNamePropHdl()
74 {
75     // Nothing to do
76 }
77 
78 sal_Bool XMLFontFamilyNamePropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
79 {
80     sal_Bool bRet = sal_False;
81     String sValue;
82     sal_Int32 nPos = 0;
83 
84     do
85     {
86         sal_Int32 nFirst = nPos;
87         nPos = SvXMLUnitConverter::indexOfComma( rStrImpValue, nPos );
88         sal_Int32 nLast = (-1 == nPos ? rStrImpValue.getLength() : nPos);
89         if( nLast > 0 )
90             nLast--;
91 
92         // skip trailing blanks
93         while( sal_Unicode(' ') == rStrImpValue[nLast] && nLast > nFirst )
94             nLast--;
95 
96         // skip leading blanks
97         while( sal_Unicode(' ') == rStrImpValue[nFirst] && nFirst <= nLast )
98             nFirst++;
99 
100         // remove quotes
101         sal_Unicode c = rStrImpValue[nFirst];
102         if( nFirst < nLast && (sal_Unicode('\'') == c || sal_Unicode('\"') == c) && rStrImpValue[nLast] == c )
103         {
104             nFirst++;
105             nLast--;
106         }
107 
108         if( nFirst <= nLast )
109         {
110             if( sValue.Len() != 0 )
111                 sValue += sal_Unicode(';');
112 
113             OUString sTemp = rStrImpValue.copy( nFirst, nLast-nFirst+1 );
114             sValue += sTemp.getStr();
115         }
116 
117         if( -1 != nPos )
118             nPos++;
119     }
120     while( -1 != nPos );
121 
122     if( sValue.Len() )
123     {
124         rValue <<= OUString(sValue.GetBuffer());
125         bRet = sal_True;
126     }
127 
128     return bRet;
129 }
130 
131 sal_Bool XMLFontFamilyNamePropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
132 {
133     sal_Bool bRet = sal_False;
134     OUString aStrFamilyName;
135 
136     if( rValue >>= aStrFamilyName )
137     {
138         OUStringBuffer sValue( aStrFamilyName.getLength() + 2L );
139         sal_Int32 nPos = 0;
140         do
141         {
142             sal_Int32 nFirst = nPos;
143             nPos = aStrFamilyName.indexOf( sal_Unicode(';'), nPos );
144             sal_Int32 nLast = (-1 == nPos ? aStrFamilyName.getLength() : nPos);
145 
146             // Set position to the character behind the ';', so we won't
147             // forget this.
148             if( -1L != nPos )
149                 nPos++;
150 
151             // If the property value was empty, we stop now.
152             // If there is a ';' at the first position, the empty name
153             // at the start will be removed.
154             if( 0L == nLast )
155                 continue;
156 
157             // nFirst and nLast now denote the first and last character of
158             // one font name.
159             nLast--;
160 
161             // skip trailing blanks
162             while( sal_Unicode(' ') == aStrFamilyName[nLast] && nLast > nFirst )
163                 nLast--;
164 
165             // skip leading blanks
166             while( sal_Unicode(' ') == aStrFamilyName[nFirst] && nFirst <= nLast )
167                 nFirst++;
168 
169             if( nFirst <= nLast )
170             {
171                 if( sValue.getLength() != 0L )
172                 {
173                     sValue.append( sal_Unicode( ',' ) );
174                     sValue.append( sal_Unicode( ' ' ));
175                 }
176                 sal_Int32 nLen = nLast-nFirst+1;
177                 OUString sFamily( aStrFamilyName.copy( nFirst, nLen ) );
178                 sal_Bool bQuote = sal_False;
179                 for( sal_Int32 i=0; i < nLen; i++ )
180                 {
181                     sal_Unicode c = sFamily[i];
182                     if( sal_Unicode(' ') == c || sal_Unicode(',') == c )
183                     {
184                         bQuote = sal_True;
185                         break;
186                     }
187                 }
188                 if( bQuote )
189                     sValue.append( sal_Unicode('\'') );
190                 sValue.append( sFamily );
191                 if( bQuote )
192                     sValue.append( sal_Unicode('\'') );
193             }
194         }
195         while( -1L != nPos );
196 
197         rStrExpValue = sValue.makeStringAndClear();
198 
199         bRet = sal_True;
200     }
201 
202     return bRet;
203 }
204 
205 ///////////////////////////////////////////////////////////////////////////////
206 //
207 // class XMLFontFamilyPropHdl
208 //
209 
210 XMLFontFamilyPropHdl::~XMLFontFamilyPropHdl()
211 {
212     // Nothing to do
213 }
214 
215 sal_Bool XMLFontFamilyPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
216 {
217     sal_uInt16 eNewFamily;
218     sal_Bool bRet = SvXMLUnitConverter::convertEnum( eNewFamily, rStrImpValue, lcl_getFontFamilyGenericMapping() );
219     if( bRet )
220         rValue <<= (sal_Int16)eNewFamily;
221 
222     return bRet;
223 }
224 
225 sal_Bool XMLFontFamilyPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
226 {
227     sal_Bool bRet = sal_False;
228     OUStringBuffer aOut;
229 
230     sal_Int16 nFamily = sal_Int16();
231     if( rValue >>= nFamily )
232     {
233         FontFamily eFamily = (FontFamily)nFamily;
234         if( eFamily != FAMILY_DONTKNOW )
235             bRet = SvXMLUnitConverter::convertEnum( aOut, eFamily, lcl_getFontFamilyGenericMapping() );
236     }
237 
238     rStrExpValue = aOut.makeStringAndClear();
239 
240     return bRet;
241 }
242 
243 ///////////////////////////////////////////////////////////////////////////////
244 //
245 // class XMLFontEncodingPropHdl
246 //
247 
248 XMLFontEncodingPropHdl::~XMLFontEncodingPropHdl()
249 {
250     // Nothing to do
251 }
252 
253 sal_Bool XMLFontEncodingPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
254 {
255     sal_Bool bRet = sal_True;
256 
257     if( IsXMLToken( rStrImpValue, XML_X_SYMBOL ) )
258         rValue <<= (sal_Int16) RTL_TEXTENCODING_SYMBOL;
259 
260     return bRet;
261 }
262 
263 sal_Bool XMLFontEncodingPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
264 {
265     sal_Bool bRet = sal_False;
266     OUStringBuffer aOut;
267     sal_Int16 nSet = sal_Int16();
268 
269     if( rValue >>= nSet )
270     {
271         if( (rtl_TextEncoding)nSet == RTL_TEXTENCODING_SYMBOL )
272         {
273             aOut.append( GetXMLToken(XML_X_SYMBOL) );
274             rStrExpValue = aOut.makeStringAndClear();
275             bRet = sal_True;
276         }
277     }
278 
279     return bRet;
280 }
281 
282 ///////////////////////////////////////////////////////////////////////////////
283 //
284 // class XMLFontPitchPropHdl
285 //
286 
287 XMLFontPitchPropHdl::~XMLFontPitchPropHdl()
288 {
289     // Nothing to do
290 }
291 
292 sal_Bool XMLFontPitchPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
293 {
294     sal_uInt16 eNewPitch;
295     sal_Bool bRet = SvXMLUnitConverter::convertEnum( eNewPitch, rStrImpValue, aFontPitchMapping );
296     if( bRet )
297         rValue <<= (sal_Int16)eNewPitch;
298 
299     return bRet;
300 }
301 
302 sal_Bool XMLFontPitchPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
303 {
304     sal_Bool bRet = sal_False;
305     sal_Int16 nPitch = sal_Int16();
306     OUStringBuffer aOut;
307 
308     FontPitch ePitch = PITCH_DONTKNOW;
309     if( rValue >>= nPitch )
310         ePitch =  (FontPitch)nPitch;
311 
312     if( PITCH_DONTKNOW != ePitch )
313     {
314         bRet = SvXMLUnitConverter::convertEnum( aOut, ePitch, aFontPitchMapping, XML_FIXED );
315         rStrExpValue = aOut.makeStringAndClear();
316     }
317 
318     return bRet;
319 }
320