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