Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <fonthdl.hxx>
22 :
23 : #include <sax/tools/converter.hxx>
24 :
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>
30 :
31 : using ::rtl::OUString;
32 : using ::rtl::OUStringBuffer;
33 :
34 : using namespace ::com::sun::star;
35 : using namespace ::xmloff::token;
36 :
37 1086 : static const SvXMLEnumMapEntry* lcl_getFontFamilyGenericMapping()
38 : {
39 : static SvXMLEnumMapEntry const aFontFamilyGenericMapping[] =
40 : {
41 : { XML_DECORATIVE, FAMILY_DECORATIVE },
42 :
43 : { XML_MODERN, FAMILY_MODERN },
44 : { XML_ROMAN, FAMILY_ROMAN },
45 : { XML_SCRIPT, FAMILY_SCRIPT },
46 : { XML_SWISS, FAMILY_SWISS },
47 : { XML_SYSTEM, FAMILY_SYSTEM },
48 : { XML_TOKEN_INVALID, 0 }
49 : };
50 1086 : return aFontFamilyGenericMapping;
51 : }
52 :
53 : static SvXMLEnumMapEntry const aFontPitchMapping[] =
54 : {
55 : { XML_FIXED, PITCH_FIXED },
56 : { XML_VARIABLE, PITCH_VARIABLE },
57 : { XML_TOKEN_INVALID, 0 }
58 : };
59 : ///////////////////////////////////////////////////////////////////////////////
60 : //
61 : // class XMLFontFamilyNamePropHdl
62 : //
63 :
64 2996 : XMLFontFamilyNamePropHdl::~XMLFontFamilyNamePropHdl()
65 : {
66 : // Nothing to do
67 2996 : }
68 :
69 1270 : sal_Bool XMLFontFamilyNamePropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
70 : {
71 1270 : sal_Bool bRet = sal_False;
72 1270 : OUStringBuffer sValue;
73 1270 : sal_Int32 nPos = 0;
74 :
75 1342 : do
76 : {
77 1342 : sal_Int32 nFirst = nPos;
78 1342 : nPos = ::sax::Converter::indexOfComma( rStrImpValue, nPos );
79 1342 : sal_Int32 nLast = (-1 == nPos ? rStrImpValue.getLength() - 1 : nPos - 1);
80 :
81 : // skip trailing blanks
82 2684 : while( nLast > nFirst && sal_Unicode(' ') == rStrImpValue[nLast] )
83 0 : nLast--;
84 :
85 : // skip leading blanks
86 2756 : while(nFirst <= nLast && sal_Unicode(' ') == rStrImpValue[nFirst])
87 72 : nFirst++;
88 :
89 : // remove quotes
90 1342 : sal_Unicode c = rStrImpValue[nFirst];
91 1342 : if( nFirst < nLast && (sal_Unicode('\'') == c || sal_Unicode('\"') == c) && rStrImpValue[nLast] == c )
92 : {
93 770 : nFirst++;
94 770 : nLast--;
95 : }
96 :
97 1342 : if( nFirst <= nLast )
98 : {
99 1338 : if( sValue.getLength() != 0 )
100 72 : sValue.append(';');
101 :
102 1338 : sValue.append(rStrImpValue.copy( nFirst, nLast-nFirst+1));
103 : }
104 :
105 1342 : if( -1 != nPos )
106 72 : nPos++;
107 : }
108 : while( -1 != nPos );
109 :
110 1270 : if (sValue.getLength())
111 : {
112 1266 : rValue <<= sValue.makeStringAndClear();
113 1266 : bRet = sal_True;
114 : }
115 :
116 1270 : return bRet;
117 : }
118 :
119 88 : sal_Bool XMLFontFamilyNamePropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
120 : {
121 88 : sal_Bool bRet = sal_False;
122 88 : OUString aStrFamilyName;
123 :
124 88 : if( rValue >>= aStrFamilyName )
125 : {
126 88 : OUStringBuffer sValue( aStrFamilyName.getLength() + 2L );
127 88 : sal_Int32 nPos = 0;
128 152 : do
129 : {
130 152 : sal_Int32 nFirst = nPos;
131 152 : nPos = aStrFamilyName.indexOf( sal_Unicode(';'), nPos );
132 152 : sal_Int32 nLast = (-1 == nPos ? aStrFamilyName.getLength() : nPos);
133 :
134 : // Set position to the character behind the ';', so we won't
135 : // forget this.
136 152 : if( -1L != nPos )
137 64 : nPos++;
138 :
139 : // If the property value was empty, we stop now.
140 : // If there is a ';' at the first position, the empty name
141 : // at the start will be removed.
142 152 : if( 0L == nLast )
143 0 : continue;
144 :
145 : // nFirst and nLast now denote the first and last character of
146 : // one font name.
147 152 : nLast--;
148 :
149 : // skip trailing blanks
150 304 : while( nLast > nFirst && sal_Unicode(' ') == aStrFamilyName[nLast] )
151 0 : nLast--;
152 :
153 : // skip leading blanks
154 304 : while( nFirst <= nLast && sal_Unicode(' ') == aStrFamilyName[nFirst] )
155 0 : nFirst++;
156 :
157 152 : if( nFirst <= nLast )
158 : {
159 152 : if( sValue.getLength() != 0L )
160 : {
161 64 : sValue.append( sal_Unicode( ',' ) );
162 64 : sValue.append( sal_Unicode( ' ' ));
163 : }
164 152 : sal_Int32 nLen = nLast-nFirst+1;
165 152 : OUString sFamily( aStrFamilyName.copy( nFirst, nLen ) );
166 152 : sal_Bool bQuote = sal_False;
167 1160 : for( sal_Int32 i=0; i < nLen; i++ )
168 : {
169 1096 : sal_Unicode c = sFamily[i];
170 1096 : if( sal_Unicode(' ') == c || sal_Unicode(',') == c )
171 : {
172 88 : bQuote = sal_True;
173 88 : break;
174 : }
175 : }
176 152 : if( bQuote )
177 88 : sValue.append( sal_Unicode('\'') );
178 152 : sValue.append( sFamily );
179 152 : if( bQuote )
180 88 : sValue.append( sal_Unicode('\'') );
181 : }
182 : }
183 : while( -1L != nPos );
184 :
185 88 : rStrExpValue = sValue.makeStringAndClear();
186 :
187 88 : bRet = sal_True;
188 : }
189 :
190 88 : return bRet;
191 : }
192 :
193 : ///////////////////////////////////////////////////////////////////////////////
194 : //
195 : // class XMLFontFamilyPropHdl
196 : //
197 :
198 3012 : XMLFontFamilyPropHdl::~XMLFontFamilyPropHdl()
199 : {
200 : // Nothing to do
201 3012 : }
202 :
203 998 : sal_Bool XMLFontFamilyPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
204 : {
205 : sal_uInt16 eNewFamily;
206 998 : sal_Bool bRet = SvXMLUnitConverter::convertEnum( eNewFamily, rStrImpValue, lcl_getFontFamilyGenericMapping() );
207 998 : if( bRet )
208 998 : rValue <<= (sal_Int16)eNewFamily;
209 :
210 998 : return bRet;
211 : }
212 :
213 88 : sal_Bool XMLFontFamilyPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
214 : {
215 88 : sal_Bool bRet = sal_False;
216 88 : OUStringBuffer aOut;
217 :
218 88 : sal_Int16 nFamily = sal_Int16();
219 88 : if( rValue >>= nFamily )
220 : {
221 88 : FontFamily eFamily = (FontFamily)nFamily;
222 88 : if( eFamily != FAMILY_DONTKNOW )
223 88 : bRet = SvXMLUnitConverter::convertEnum( aOut, eFamily, lcl_getFontFamilyGenericMapping() );
224 : }
225 :
226 88 : rStrExpValue = aOut.makeStringAndClear();
227 :
228 88 : return bRet;
229 : }
230 :
231 : ///////////////////////////////////////////////////////////////////////////////
232 : //
233 : // class XMLFontEncodingPropHdl
234 : //
235 :
236 3012 : XMLFontEncodingPropHdl::~XMLFontEncodingPropHdl()
237 : {
238 : // Nothing to do
239 3012 : }
240 :
241 84 : sal_Bool XMLFontEncodingPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
242 : {
243 84 : sal_Bool bRet = sal_True;
244 :
245 84 : if( IsXMLToken( rStrImpValue, XML_X_SYMBOL ) )
246 84 : rValue <<= (sal_Int16) RTL_TEXTENCODING_SYMBOL;
247 :
248 84 : return bRet;
249 : }
250 :
251 88 : sal_Bool XMLFontEncodingPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
252 : {
253 88 : sal_Bool bRet = sal_False;
254 88 : OUStringBuffer aOut;
255 88 : sal_Int16 nSet = sal_Int16();
256 :
257 88 : if( rValue >>= nSet )
258 : {
259 88 : if( (rtl_TextEncoding)nSet == RTL_TEXTENCODING_SYMBOL )
260 : {
261 0 : aOut.append( GetXMLToken(XML_X_SYMBOL) );
262 0 : rStrExpValue = aOut.makeStringAndClear();
263 0 : bRet = sal_True;
264 : }
265 : }
266 :
267 88 : return bRet;
268 : }
269 :
270 : ///////////////////////////////////////////////////////////////////////////////
271 : //
272 : // class XMLFontPitchPropHdl
273 : //
274 :
275 3012 : XMLFontPitchPropHdl::~XMLFontPitchPropHdl()
276 : {
277 : // Nothing to do
278 3012 : }
279 :
280 942 : sal_Bool XMLFontPitchPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
281 : {
282 : sal_uInt16 eNewPitch;
283 942 : sal_Bool bRet = SvXMLUnitConverter::convertEnum( eNewPitch, rStrImpValue, aFontPitchMapping );
284 942 : if( bRet )
285 942 : rValue <<= (sal_Int16)eNewPitch;
286 :
287 942 : return bRet;
288 : }
289 :
290 88 : sal_Bool XMLFontPitchPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
291 : {
292 88 : sal_Bool bRet = sal_False;
293 88 : sal_Int16 nPitch = sal_Int16();
294 88 : OUStringBuffer aOut;
295 :
296 88 : FontPitch ePitch = PITCH_DONTKNOW;
297 88 : if( rValue >>= nPitch )
298 88 : ePitch = (FontPitch)nPitch;
299 :
300 88 : if( PITCH_DONTKNOW != ePitch )
301 : {
302 76 : bRet = SvXMLUnitConverter::convertEnum( aOut, ePitch, aFontPitchMapping, XML_FIXED );
303 76 : rStrExpValue = aOut.makeStringAndClear();
304 : }
305 :
306 88 : return bRet;
307 : }
308 :
309 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|