xref: /trunk/main/xmloff/source/style/fonthdl.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*63bba73cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*63bba73cSAndrew Rist  * distributed with this work for additional information
6*63bba73cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*63bba73cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist  * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*63bba73cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*63bba73cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist  * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*63bba73cSAndrew Rist  * specific language governing permissions and limitations
18*63bba73cSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*63bba73cSAndrew Rist  *************************************************************/
21*63bba73cSAndrew Rist 
22*63bba73cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <fonthdl.hxx>
28cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
29cdf0e10cSrcweir #include <xmloff/xmluconv.hxx>
30cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
31cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
32cdf0e10cSrcweir #include <tools/fontenum.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <tools/string.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using ::rtl::OUString;
37cdf0e10cSrcweir using ::rtl::OUStringBuffer;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using namespace ::com::sun::star;
40cdf0e10cSrcweir using namespace ::xmloff::token;
41cdf0e10cSrcweir 
lcl_getFontFamilyGenericMapping()42cdf0e10cSrcweir const SvXMLEnumMapEntry* lcl_getFontFamilyGenericMapping()
43cdf0e10cSrcweir {
44cdf0e10cSrcweir     static SvXMLEnumMapEntry __READONLY_DATA aFontFamilyGenericMapping[] =
45cdf0e10cSrcweir     {
46cdf0e10cSrcweir         { XML_DECORATIVE,       FAMILY_DECORATIVE },
47cdf0e10cSrcweir 
48cdf0e10cSrcweir         { XML_MODERN,           FAMILY_MODERN   },
49cdf0e10cSrcweir         { XML_ROMAN,            FAMILY_ROMAN    },
50cdf0e10cSrcweir         { XML_SCRIPT,           FAMILY_SCRIPT   },
51cdf0e10cSrcweir         { XML_SWISS,            FAMILY_SWISS    },
52cdf0e10cSrcweir         { XML_SYSTEM,           FAMILY_SYSTEM   },
53cdf0e10cSrcweir         { XML_TOKEN_INVALID,    0               }
54cdf0e10cSrcweir     };
55cdf0e10cSrcweir     return aFontFamilyGenericMapping;
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir static SvXMLEnumMapEntry __READONLY_DATA aFontPitchMapping[] =
59cdf0e10cSrcweir {
60cdf0e10cSrcweir     { XML_FIXED,            PITCH_FIXED     },
61cdf0e10cSrcweir     { XML_VARIABLE,         PITCH_VARIABLE  },
62cdf0e10cSrcweir     { XML_TOKEN_INVALID,    0               }
63cdf0e10cSrcweir };
64cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////////
65cdf0e10cSrcweir //
66cdf0e10cSrcweir // class XMLFontFamilyNamePropHdl
67cdf0e10cSrcweir //
68cdf0e10cSrcweir 
~XMLFontFamilyNamePropHdl()69cdf0e10cSrcweir XMLFontFamilyNamePropHdl::~XMLFontFamilyNamePropHdl()
70cdf0e10cSrcweir {
71cdf0e10cSrcweir     // Nothing to do
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
importXML(const OUString & rStrImpValue,uno::Any & rValue,const SvXMLUnitConverter &) const74cdf0e10cSrcweir sal_Bool XMLFontFamilyNamePropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
75cdf0e10cSrcweir {
76cdf0e10cSrcweir     sal_Bool bRet = sal_False;
77cdf0e10cSrcweir     String sValue;
78cdf0e10cSrcweir     sal_Int32 nPos = 0;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     do
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         sal_Int32 nFirst = nPos;
83cdf0e10cSrcweir         nPos = SvXMLUnitConverter::indexOfComma( rStrImpValue, nPos );
84cdf0e10cSrcweir         sal_Int32 nLast = (-1 == nPos ? rStrImpValue.getLength() : nPos);
85cdf0e10cSrcweir         if( nLast > 0 )
86cdf0e10cSrcweir             nLast--;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         // skip trailing blanks
89cdf0e10cSrcweir         while( sal_Unicode(' ') == rStrImpValue[nLast] && nLast > nFirst )
90cdf0e10cSrcweir             nLast--;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         // skip leading blanks
93cdf0e10cSrcweir         while( sal_Unicode(' ') == rStrImpValue[nFirst] && nFirst <= nLast )
94cdf0e10cSrcweir             nFirst++;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         // remove quotes
97cdf0e10cSrcweir         sal_Unicode c = rStrImpValue[nFirst];
98cdf0e10cSrcweir         if( nFirst < nLast && (sal_Unicode('\'') == c || sal_Unicode('\"') == c) && rStrImpValue[nLast] == c )
99cdf0e10cSrcweir         {
100cdf0e10cSrcweir             nFirst++;
101cdf0e10cSrcweir             nLast--;
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         if( nFirst <= nLast )
105cdf0e10cSrcweir         {
106cdf0e10cSrcweir             if( sValue.Len() != 0 )
107cdf0e10cSrcweir                 sValue += sal_Unicode(';');
108cdf0e10cSrcweir 
109cdf0e10cSrcweir             OUString sTemp = rStrImpValue.copy( nFirst, nLast-nFirst+1 );
110cdf0e10cSrcweir             sValue += sTemp.getStr();
111cdf0e10cSrcweir         }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir         if( -1 != nPos )
114cdf0e10cSrcweir             nPos++;
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir     while( -1 != nPos );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     if( sValue.Len() )
119cdf0e10cSrcweir     {
120cdf0e10cSrcweir         rValue <<= OUString(sValue.GetBuffer());
121cdf0e10cSrcweir         bRet = sal_True;
122cdf0e10cSrcweir     }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     return bRet;
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
exportXML(OUString & rStrExpValue,const uno::Any & rValue,const SvXMLUnitConverter &) const127cdf0e10cSrcweir sal_Bool XMLFontFamilyNamePropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     sal_Bool bRet = sal_False;
130cdf0e10cSrcweir     OUString aStrFamilyName;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     if( rValue >>= aStrFamilyName )
133cdf0e10cSrcweir     {
134cdf0e10cSrcweir         OUStringBuffer sValue( aStrFamilyName.getLength() + 2L );
135cdf0e10cSrcweir         sal_Int32 nPos = 0;
136cdf0e10cSrcweir         do
137cdf0e10cSrcweir         {
138cdf0e10cSrcweir             sal_Int32 nFirst = nPos;
139cdf0e10cSrcweir             nPos = aStrFamilyName.indexOf( sal_Unicode(';'), nPos );
140cdf0e10cSrcweir             sal_Int32 nLast = (-1 == nPos ? aStrFamilyName.getLength() : nPos);
141cdf0e10cSrcweir 
142cdf0e10cSrcweir             // Set position to the character behind the ';', so we won't
143cdf0e10cSrcweir             // forget this.
144cdf0e10cSrcweir             if( -1L != nPos )
145cdf0e10cSrcweir                 nPos++;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir             // If the property value was empty, we stop now.
148cdf0e10cSrcweir             // If there is a ';' at the first position, the empty name
149cdf0e10cSrcweir             // at the start will be removed.
150cdf0e10cSrcweir             if( 0L == nLast )
151cdf0e10cSrcweir                 continue;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir             // nFirst and nLast now denote the first and last character of
154cdf0e10cSrcweir             // one font name.
155cdf0e10cSrcweir             nLast--;
156cdf0e10cSrcweir 
157cdf0e10cSrcweir             // skip trailing blanks
158cdf0e10cSrcweir             while( sal_Unicode(' ') == aStrFamilyName[nLast] && nLast > nFirst )
159cdf0e10cSrcweir                 nLast--;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir             // skip leading blanks
162cdf0e10cSrcweir             while( sal_Unicode(' ') == aStrFamilyName[nFirst] && nFirst <= nLast )
163cdf0e10cSrcweir                 nFirst++;
164cdf0e10cSrcweir 
165cdf0e10cSrcweir             if( nFirst <= nLast )
166cdf0e10cSrcweir             {
167cdf0e10cSrcweir                 if( sValue.getLength() != 0L )
168cdf0e10cSrcweir                 {
169cdf0e10cSrcweir                     sValue.append( sal_Unicode( ',' ) );
170cdf0e10cSrcweir                     sValue.append( sal_Unicode( ' ' ));
171cdf0e10cSrcweir                 }
172cdf0e10cSrcweir                 sal_Int32 nLen = nLast-nFirst+1;
173cdf0e10cSrcweir                 OUString sFamily( aStrFamilyName.copy( nFirst, nLen ) );
174cdf0e10cSrcweir                 sal_Bool bQuote = sal_False;
175cdf0e10cSrcweir                 for( sal_Int32 i=0; i < nLen; i++ )
176cdf0e10cSrcweir                 {
177cdf0e10cSrcweir                     sal_Unicode c = sFamily[i];
178cdf0e10cSrcweir                     if( sal_Unicode(' ') == c || sal_Unicode(',') == c )
179cdf0e10cSrcweir                     {
180cdf0e10cSrcweir                         bQuote = sal_True;
181cdf0e10cSrcweir                         break;
182cdf0e10cSrcweir                     }
183cdf0e10cSrcweir                 }
184cdf0e10cSrcweir                 if( bQuote )
185cdf0e10cSrcweir                     sValue.append( sal_Unicode('\'') );
186cdf0e10cSrcweir                 sValue.append( sFamily );
187cdf0e10cSrcweir                 if( bQuote )
188cdf0e10cSrcweir                     sValue.append( sal_Unicode('\'') );
189cdf0e10cSrcweir             }
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir         while( -1L != nPos );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir         rStrExpValue = sValue.makeStringAndClear();
194cdf0e10cSrcweir 
195cdf0e10cSrcweir         bRet = sal_True;
196cdf0e10cSrcweir     }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir     return bRet;
199cdf0e10cSrcweir }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////////
202cdf0e10cSrcweir //
203cdf0e10cSrcweir // class XMLFontFamilyPropHdl
204cdf0e10cSrcweir //
205cdf0e10cSrcweir 
~XMLFontFamilyPropHdl()206cdf0e10cSrcweir XMLFontFamilyPropHdl::~XMLFontFamilyPropHdl()
207cdf0e10cSrcweir {
208cdf0e10cSrcweir     // Nothing to do
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
importXML(const OUString & rStrImpValue,uno::Any & rValue,const SvXMLUnitConverter &) const211cdf0e10cSrcweir sal_Bool XMLFontFamilyPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
212cdf0e10cSrcweir {
213cdf0e10cSrcweir     sal_uInt16 eNewFamily;
214cdf0e10cSrcweir     sal_Bool bRet = SvXMLUnitConverter::convertEnum( eNewFamily, rStrImpValue, lcl_getFontFamilyGenericMapping() );
215cdf0e10cSrcweir     if( bRet )
216cdf0e10cSrcweir         rValue <<= (sal_Int16)eNewFamily;
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     return bRet;
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
exportXML(OUString & rStrExpValue,const uno::Any & rValue,const SvXMLUnitConverter &) const221cdf0e10cSrcweir sal_Bool XMLFontFamilyPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
222cdf0e10cSrcweir {
223cdf0e10cSrcweir     sal_Bool bRet = sal_False;
224cdf0e10cSrcweir     OUStringBuffer aOut;
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     sal_Int16 nFamily = sal_Int16();
227cdf0e10cSrcweir     if( rValue >>= nFamily )
228cdf0e10cSrcweir     {
229cdf0e10cSrcweir         FontFamily eFamily = (FontFamily)nFamily;
230cdf0e10cSrcweir         if( eFamily != FAMILY_DONTKNOW )
231cdf0e10cSrcweir             bRet = SvXMLUnitConverter::convertEnum( aOut, eFamily, lcl_getFontFamilyGenericMapping() );
232cdf0e10cSrcweir     }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir     rStrExpValue = aOut.makeStringAndClear();
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     return bRet;
237cdf0e10cSrcweir }
238cdf0e10cSrcweir 
239cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////////
240cdf0e10cSrcweir //
241cdf0e10cSrcweir // class XMLFontEncodingPropHdl
242cdf0e10cSrcweir //
243cdf0e10cSrcweir 
~XMLFontEncodingPropHdl()244cdf0e10cSrcweir XMLFontEncodingPropHdl::~XMLFontEncodingPropHdl()
245cdf0e10cSrcweir {
246cdf0e10cSrcweir     // Nothing to do
247cdf0e10cSrcweir }
248cdf0e10cSrcweir 
importXML(const OUString & rStrImpValue,uno::Any & rValue,const SvXMLUnitConverter &) const249cdf0e10cSrcweir sal_Bool XMLFontEncodingPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
250cdf0e10cSrcweir {
251cdf0e10cSrcweir     sal_Bool bRet = sal_True;
252cdf0e10cSrcweir 
253cdf0e10cSrcweir     if( IsXMLToken( rStrImpValue, XML_X_SYMBOL ) )
254cdf0e10cSrcweir         rValue <<= (sal_Int16) RTL_TEXTENCODING_SYMBOL;
255cdf0e10cSrcweir 
256cdf0e10cSrcweir     return bRet;
257cdf0e10cSrcweir }
258cdf0e10cSrcweir 
exportXML(OUString & rStrExpValue,const uno::Any & rValue,const SvXMLUnitConverter &) const259cdf0e10cSrcweir sal_Bool XMLFontEncodingPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
260cdf0e10cSrcweir {
261cdf0e10cSrcweir     sal_Bool bRet = sal_False;
262cdf0e10cSrcweir     OUStringBuffer aOut;
263cdf0e10cSrcweir     sal_Int16 nSet = sal_Int16();
264cdf0e10cSrcweir 
265cdf0e10cSrcweir     if( rValue >>= nSet )
266cdf0e10cSrcweir     {
267cdf0e10cSrcweir         if( (rtl_TextEncoding)nSet == RTL_TEXTENCODING_SYMBOL )
268cdf0e10cSrcweir         {
269cdf0e10cSrcweir             aOut.append( GetXMLToken(XML_X_SYMBOL) );
270cdf0e10cSrcweir             rStrExpValue = aOut.makeStringAndClear();
271cdf0e10cSrcweir             bRet = sal_True;
272cdf0e10cSrcweir         }
273cdf0e10cSrcweir     }
274cdf0e10cSrcweir 
275cdf0e10cSrcweir     return bRet;
276cdf0e10cSrcweir }
277cdf0e10cSrcweir 
278cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////////
279cdf0e10cSrcweir //
280cdf0e10cSrcweir // class XMLFontPitchPropHdl
281cdf0e10cSrcweir //
282cdf0e10cSrcweir 
~XMLFontPitchPropHdl()283cdf0e10cSrcweir XMLFontPitchPropHdl::~XMLFontPitchPropHdl()
284cdf0e10cSrcweir {
285cdf0e10cSrcweir     // Nothing to do
286cdf0e10cSrcweir }
287cdf0e10cSrcweir 
importXML(const OUString & rStrImpValue,uno::Any & rValue,const SvXMLUnitConverter &) const288cdf0e10cSrcweir sal_Bool XMLFontPitchPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
289cdf0e10cSrcweir {
290cdf0e10cSrcweir     sal_uInt16 eNewPitch;
291cdf0e10cSrcweir     sal_Bool bRet = SvXMLUnitConverter::convertEnum( eNewPitch, rStrImpValue, aFontPitchMapping );
292cdf0e10cSrcweir     if( bRet )
293cdf0e10cSrcweir         rValue <<= (sal_Int16)eNewPitch;
294cdf0e10cSrcweir 
295cdf0e10cSrcweir     return bRet;
296cdf0e10cSrcweir }
297cdf0e10cSrcweir 
exportXML(OUString & rStrExpValue,const uno::Any & rValue,const SvXMLUnitConverter &) const298cdf0e10cSrcweir sal_Bool XMLFontPitchPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
299cdf0e10cSrcweir {
300cdf0e10cSrcweir     sal_Bool bRet = sal_False;
301cdf0e10cSrcweir     sal_Int16 nPitch = sal_Int16();
302cdf0e10cSrcweir     OUStringBuffer aOut;
303cdf0e10cSrcweir 
304cdf0e10cSrcweir     FontPitch ePitch = PITCH_DONTKNOW;
305cdf0e10cSrcweir     if( rValue >>= nPitch )
306cdf0e10cSrcweir         ePitch =  (FontPitch)nPitch;
307cdf0e10cSrcweir 
308cdf0e10cSrcweir     if( PITCH_DONTKNOW != ePitch )
309cdf0e10cSrcweir     {
310cdf0e10cSrcweir         bRet = SvXMLUnitConverter::convertEnum( aOut, ePitch, aFontPitchMapping, XML_FIXED );
311cdf0e10cSrcweir         rStrExpValue = aOut.makeStringAndClear();
312cdf0e10cSrcweir     }
313cdf0e10cSrcweir 
314cdf0e10cSrcweir     return bRet;
315cdf0e10cSrcweir }
316