/src/libreoffice/xmloff/source/style/fonthdl.cxx
Line | Count | Source |
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 | | #include <sal/config.h> |
21 | | |
22 | | #include <string_view> |
23 | | |
24 | | #include "fonthdl.hxx" |
25 | | |
26 | | #include <sax/tools/converter.hxx> |
27 | | |
28 | | #include <xmloff/xmltoken.hxx> |
29 | | #include <xmloff/xmluconv.hxx> |
30 | | #include <xmloff/xmlement.hxx> |
31 | | #include <rtl/ustrbuf.hxx> |
32 | | #include <com/sun/star/uno/Any.hxx> |
33 | | #include <tools/fontenum.hxx> |
34 | | |
35 | | using namespace ::com::sun::star; |
36 | | using namespace ::xmloff::token; |
37 | | |
38 | | static const SvXMLEnumMapEntry<FontFamily>* lcl_getFontFamilyGenericMapping() |
39 | 36.6k | { |
40 | 36.6k | static SvXMLEnumMapEntry<FontFamily> const aFontFamilyGenericMapping[] = |
41 | 36.6k | { |
42 | 36.6k | { XML_DECORATIVE, FAMILY_DECORATIVE }, |
43 | | |
44 | 36.6k | { XML_MODERN, FAMILY_MODERN }, |
45 | 36.6k | { XML_ROMAN, FAMILY_ROMAN }, |
46 | 36.6k | { XML_SCRIPT, FAMILY_SCRIPT }, |
47 | 36.6k | { XML_SWISS, FAMILY_SWISS }, |
48 | 36.6k | { XML_SYSTEM, FAMILY_SYSTEM }, |
49 | 36.6k | { XML_TOKEN_INVALID, FontFamily(0) } |
50 | 36.6k | }; |
51 | 36.6k | return aFontFamilyGenericMapping; |
52 | 36.6k | } |
53 | | |
54 | | SvXMLEnumMapEntry<FontPitch> const aFontPitchMapping[] = |
55 | | { |
56 | | { XML_FIXED, PITCH_FIXED }, |
57 | | { XML_VARIABLE, PITCH_VARIABLE }, |
58 | | { XML_TOKEN_INVALID, FontPitch(0) } |
59 | | }; |
60 | | |
61 | | |
62 | | XMLFontFamilyNamePropHdl::~XMLFontFamilyNamePropHdl() |
63 | 197k | { |
64 | | // Nothing to do |
65 | 197k | } |
66 | | |
67 | | bool XMLFontFamilyNamePropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const |
68 | 53.2k | { |
69 | 53.2k | bool bRet = false; |
70 | 53.2k | OUStringBuffer sValue; |
71 | 53.2k | sal_Int32 nPos = 0; |
72 | | |
73 | 53.2k | do |
74 | 92.3k | { |
75 | 92.3k | sal_Int32 nFirst = nPos; |
76 | 92.3k | nPos = ::sax::Converter::indexOfComma( rStrImpValue, nPos ); |
77 | 92.3k | sal_Int32 nLast = (-1 == nPos ? rStrImpValue.getLength() - 1 : nPos - 1); |
78 | | |
79 | | // skip trailing blanks |
80 | 92.4k | while( nLast > nFirst && ' ' == rStrImpValue[nLast] ) |
81 | 97 | nLast--; |
82 | | |
83 | | // skip leading blanks |
84 | 92.3k | while(nFirst <= nLast && ' ' == rStrImpValue[nFirst]) |
85 | 8 | nFirst++; |
86 | | |
87 | | // remove quotes |
88 | 92.3k | sal_Unicode c = nFirst > nLast ? 0 : rStrImpValue[nFirst]; |
89 | 92.3k | if( nFirst < nLast && ('\'' == c || '\"' == c) && rStrImpValue[nLast] == c ) |
90 | 20.1k | { |
91 | 20.1k | nFirst++; |
92 | 20.1k | nLast--; |
93 | 20.1k | } |
94 | | |
95 | 92.3k | if( nFirst <= nLast ) |
96 | 92.1k | { |
97 | 92.1k | if( !sValue.isEmpty() ) |
98 | 38.8k | sValue.append(';'); |
99 | | |
100 | 92.1k | sValue.append(rStrImpValue.subView(nFirst, nLast-nFirst+1)); |
101 | 92.1k | } |
102 | | |
103 | 92.3k | if( -1 != nPos ) |
104 | 39.0k | nPos++; |
105 | 92.3k | } |
106 | 92.3k | while( -1 != nPos ); |
107 | | |
108 | 53.2k | if (!sValue.isEmpty()) |
109 | 53.2k | { |
110 | 53.2k | rValue <<= sValue.makeStringAndClear(); |
111 | 53.2k | bRet = true; |
112 | 53.2k | } |
113 | | |
114 | 53.2k | return bRet; |
115 | 53.2k | } |
116 | | |
117 | | bool XMLFontFamilyNamePropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const |
118 | 93 | { |
119 | 93 | bool bRet = false; |
120 | 93 | OUString aStrFamilyName; |
121 | | |
122 | 93 | if( rValue >>= aStrFamilyName ) |
123 | 93 | { |
124 | 93 | OUStringBuffer sValue( aStrFamilyName.getLength() + 2 ); |
125 | 93 | sal_Int32 nPos = 0; |
126 | 93 | do |
127 | 93 | { |
128 | 93 | sal_Int32 nFirst = nPos; |
129 | 93 | nPos = aStrFamilyName.indexOf( ';', nPos ); |
130 | 93 | sal_Int32 nLast = (-1 == nPos ? aStrFamilyName.getLength() : nPos); |
131 | | |
132 | | // Set position to the character behind the ';', so we won't |
133 | | // forget this. |
134 | 93 | if( -1 != nPos ) |
135 | 0 | nPos++; |
136 | | |
137 | | // If the property value was empty, we stop now. |
138 | | // If there is a ';' at the first position, the empty name |
139 | | // at the start will be removed. |
140 | 93 | if( 0 == nLast ) |
141 | 0 | continue; |
142 | | |
143 | | // nFirst and nLast now denote the first and last character of |
144 | | // one font name. |
145 | 93 | nLast--; |
146 | | |
147 | | // skip trailing blanks |
148 | 93 | while( nLast > nFirst && ' ' == aStrFamilyName[nLast] ) |
149 | 0 | nLast--; |
150 | | |
151 | | // skip leading blanks |
152 | 93 | while( nFirst <= nLast && ' ' == aStrFamilyName[nFirst] ) |
153 | 0 | nFirst++; |
154 | | |
155 | 93 | if( nFirst <= nLast ) |
156 | 93 | { |
157 | 93 | if( !sValue.isEmpty() ) |
158 | 0 | sValue.append( ", " ); |
159 | 93 | sal_Int32 nLen = nLast-nFirst+1; |
160 | 93 | std::u16string_view sFamily( aStrFamilyName.subView( nFirst, nLen ) ); |
161 | 93 | bool bQuote = false; |
162 | 988 | for( sal_Int32 i=0; i < nLen; i++ ) |
163 | 926 | { |
164 | 926 | sal_Unicode c = sFamily[i]; |
165 | 926 | if( ' ' == c || ',' == c ) |
166 | 31 | { |
167 | 31 | bQuote = true; |
168 | 31 | break; |
169 | 31 | } |
170 | 926 | } |
171 | 93 | if( bQuote ) |
172 | 31 | sValue.append( '\'' ); |
173 | 93 | sValue.append( sFamily ); |
174 | 93 | if( bQuote ) |
175 | 31 | sValue.append( '\'' ); |
176 | 93 | } |
177 | 93 | } |
178 | 93 | while( -1 != nPos ); |
179 | | |
180 | 93 | rStrExpValue = sValue.makeStringAndClear(); |
181 | | |
182 | 93 | bRet = true; |
183 | 93 | } |
184 | | |
185 | 93 | return bRet; |
186 | 93 | } |
187 | | |
188 | | |
189 | | XMLFontFamilyPropHdl::~XMLFontFamilyPropHdl() |
190 | 197k | { |
191 | | // Nothing to do |
192 | 197k | } |
193 | | |
194 | | bool XMLFontFamilyPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const |
195 | 36.6k | { |
196 | 36.6k | FontFamily eNewFamily; |
197 | 36.6k | bool bRet = SvXMLUnitConverter::convertEnum( eNewFamily, rStrImpValue, lcl_getFontFamilyGenericMapping() ); |
198 | 36.6k | if( bRet ) |
199 | 35.1k | rValue <<= static_cast<sal_Int16>(eNewFamily); |
200 | | |
201 | 36.6k | return bRet; |
202 | 36.6k | } |
203 | | |
204 | | bool XMLFontFamilyPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const |
205 | 93 | { |
206 | 93 | bool bRet = false; |
207 | 93 | OUStringBuffer aOut; |
208 | | |
209 | 93 | sal_Int16 nFamily = sal_Int16(); |
210 | 93 | if( rValue >>= nFamily ) |
211 | 93 | { |
212 | 93 | FontFamily eFamily = static_cast<FontFamily>(nFamily); |
213 | 93 | if( eFamily != FAMILY_DONTKNOW ) |
214 | 30 | bRet = SvXMLUnitConverter::convertEnum( aOut, eFamily, lcl_getFontFamilyGenericMapping() ); |
215 | 93 | } |
216 | | |
217 | 93 | rStrExpValue = aOut.makeStringAndClear(); |
218 | | |
219 | 93 | return bRet; |
220 | 93 | } |
221 | | |
222 | | |
223 | | XMLFontEncodingPropHdl::~XMLFontEncodingPropHdl() |
224 | 197k | { |
225 | | // Nothing to do |
226 | 197k | } |
227 | | |
228 | | bool XMLFontEncodingPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const |
229 | 6.10k | { |
230 | 6.10k | if( IsXMLToken( rStrImpValue, XML_X_SYMBOL ) ) |
231 | 6.02k | rValue <<= sal_Int16(RTL_TEXTENCODING_SYMBOL); |
232 | | |
233 | 6.10k | return true; |
234 | 6.10k | } |
235 | | |
236 | | bool XMLFontEncodingPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const |
237 | 93 | { |
238 | 93 | bool bRet = false; |
239 | 93 | sal_Int16 nSet = sal_Int16(); |
240 | | |
241 | 93 | if( rValue >>= nSet ) |
242 | 93 | { |
243 | 93 | if( static_cast<rtl_TextEncoding>(nSet) == RTL_TEXTENCODING_SYMBOL ) |
244 | 0 | { |
245 | 0 | rStrExpValue = GetXMLToken(XML_X_SYMBOL); |
246 | 0 | bRet = true; |
247 | 0 | } |
248 | 93 | } |
249 | | |
250 | 93 | return bRet; |
251 | 93 | } |
252 | | |
253 | | |
254 | | XMLFontPitchPropHdl::~XMLFontPitchPropHdl() |
255 | 197k | { |
256 | | // Nothing to do |
257 | 197k | } |
258 | | |
259 | | bool XMLFontPitchPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const |
260 | 41.5k | { |
261 | 41.5k | FontPitch eNewPitch; |
262 | 41.5k | bool bRet = SvXMLUnitConverter::convertEnum( eNewPitch, rStrImpValue, aFontPitchMapping ); |
263 | 41.5k | if( bRet ) |
264 | 40.2k | rValue <<= static_cast<sal_Int16>(eNewPitch); |
265 | | |
266 | 41.5k | return bRet; |
267 | 41.5k | } |
268 | | |
269 | | bool XMLFontPitchPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const |
270 | 93 | { |
271 | 93 | bool bRet = false; |
272 | 93 | sal_Int16 nPitch = sal_Int16(); |
273 | | |
274 | 93 | FontPitch ePitch = PITCH_DONTKNOW; |
275 | 93 | if( rValue >>= nPitch ) |
276 | 93 | ePitch = static_cast<FontPitch>(nPitch); |
277 | | |
278 | 93 | if( PITCH_DONTKNOW != ePitch ) |
279 | 33 | { |
280 | 33 | OUStringBuffer aOut; |
281 | 33 | bRet = SvXMLUnitConverter::convertEnum( aOut, ePitch, aFontPitchMapping, XML_FIXED ); |
282 | 33 | rStrExpValue = aOut.makeStringAndClear(); |
283 | 33 | } |
284 | | |
285 | 93 | return bRet; |
286 | 93 | } |
287 | | |
288 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |