bump product version to 7.6.3.2-android
[LibreOffice.git] / xmloff / source / style / fonthdl.cxx
blobd85f425ae662e82fee224e649f40c4f7dc35fdc9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <string_view>
24 #include "fonthdl.hxx"
26 #include <sax/tools/converter.hxx>
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>
35 using namespace ::com::sun::star;
36 using namespace ::xmloff::token;
38 static const SvXMLEnumMapEntry<FontFamily>* lcl_getFontFamilyGenericMapping()
40 static SvXMLEnumMapEntry<FontFamily> const aFontFamilyGenericMapping[] =
42 { XML_DECORATIVE, FAMILY_DECORATIVE },
44 { XML_MODERN, FAMILY_MODERN },
45 { XML_ROMAN, FAMILY_ROMAN },
46 { XML_SCRIPT, FAMILY_SCRIPT },
47 { XML_SWISS, FAMILY_SWISS },
48 { XML_SYSTEM, FAMILY_SYSTEM },
49 { XML_TOKEN_INVALID, FontFamily(0) }
51 return aFontFamilyGenericMapping;
54 SvXMLEnumMapEntry<FontPitch> const aFontPitchMapping[] =
56 { XML_FIXED, PITCH_FIXED },
57 { XML_VARIABLE, PITCH_VARIABLE },
58 { XML_TOKEN_INVALID, FontPitch(0) }
62 XMLFontFamilyNamePropHdl::~XMLFontFamilyNamePropHdl()
64 // Nothing to do
67 bool XMLFontFamilyNamePropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
69 bool bRet = false;
70 OUStringBuffer sValue;
71 sal_Int32 nPos = 0;
75 sal_Int32 nFirst = nPos;
76 nPos = ::sax::Converter::indexOfComma( rStrImpValue, nPos );
77 sal_Int32 nLast = (-1 == nPos ? rStrImpValue.getLength() - 1 : nPos - 1);
79 // skip trailing blanks
80 while( nLast > nFirst && ' ' == rStrImpValue[nLast] )
81 nLast--;
83 // skip leading blanks
84 while(nFirst <= nLast && ' ' == rStrImpValue[nFirst])
85 nFirst++;
87 // remove quotes
88 sal_Unicode c = nFirst > nLast ? 0 : rStrImpValue[nFirst];
89 if( nFirst < nLast && ('\'' == c || '\"' == c) && rStrImpValue[nLast] == c )
91 nFirst++;
92 nLast--;
95 if( nFirst <= nLast )
97 if( !sValue.isEmpty() )
98 sValue.append(';');
100 sValue.append(rStrImpValue.subView(nFirst, nLast-nFirst+1));
103 if( -1 != nPos )
104 nPos++;
106 while( -1 != nPos );
108 if (!sValue.isEmpty())
110 rValue <<= sValue.makeStringAndClear();
111 bRet = true;
114 return bRet;
117 bool XMLFontFamilyNamePropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
119 bool bRet = false;
120 OUString aStrFamilyName;
122 if( rValue >>= aStrFamilyName )
124 OUStringBuffer sValue( aStrFamilyName.getLength() + 2 );
125 sal_Int32 nPos = 0;
128 sal_Int32 nFirst = nPos;
129 nPos = aStrFamilyName.indexOf( ';', nPos );
130 sal_Int32 nLast = (-1 == nPos ? aStrFamilyName.getLength() : nPos);
132 // Set position to the character behind the ';', so we won't
133 // forget this.
134 if( -1 != nPos )
135 nPos++;
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 if( 0 == nLast )
141 continue;
143 // nFirst and nLast now denote the first and last character of
144 // one font name.
145 nLast--;
147 // skip trailing blanks
148 while( nLast > nFirst && ' ' == aStrFamilyName[nLast] )
149 nLast--;
151 // skip leading blanks
152 while( nFirst <= nLast && ' ' == aStrFamilyName[nFirst] )
153 nFirst++;
155 if( nFirst <= nLast )
157 if( !sValue.isEmpty() )
158 sValue.append( ", " );
159 sal_Int32 nLen = nLast-nFirst+1;
160 std::u16string_view sFamily( aStrFamilyName.subView( nFirst, nLen ) );
161 bool bQuote = false;
162 for( sal_Int32 i=0; i < nLen; i++ )
164 sal_Unicode c = sFamily[i];
165 if( ' ' == c || ',' == c )
167 bQuote = true;
168 break;
171 if( bQuote )
172 sValue.append( '\'' );
173 sValue.append( sFamily );
174 if( bQuote )
175 sValue.append( '\'' );
178 while( -1 != nPos );
180 rStrExpValue = sValue.makeStringAndClear();
182 bRet = true;
185 return bRet;
189 XMLFontFamilyPropHdl::~XMLFontFamilyPropHdl()
191 // Nothing to do
194 bool XMLFontFamilyPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
196 FontFamily eNewFamily;
197 bool bRet = SvXMLUnitConverter::convertEnum( eNewFamily, rStrImpValue, lcl_getFontFamilyGenericMapping() );
198 if( bRet )
199 rValue <<= static_cast<sal_Int16>(eNewFamily);
201 return bRet;
204 bool XMLFontFamilyPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
206 bool bRet = false;
207 OUStringBuffer aOut;
209 sal_Int16 nFamily = sal_Int16();
210 if( rValue >>= nFamily )
212 FontFamily eFamily = static_cast<FontFamily>(nFamily);
213 if( eFamily != FAMILY_DONTKNOW )
214 bRet = SvXMLUnitConverter::convertEnum( aOut, eFamily, lcl_getFontFamilyGenericMapping() );
217 rStrExpValue = aOut.makeStringAndClear();
219 return bRet;
223 XMLFontEncodingPropHdl::~XMLFontEncodingPropHdl()
225 // Nothing to do
228 bool XMLFontEncodingPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
230 if( IsXMLToken( rStrImpValue, XML_X_SYMBOL ) )
231 rValue <<= sal_Int16(RTL_TEXTENCODING_SYMBOL);
233 return true;
236 bool XMLFontEncodingPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
238 bool bRet = false;
239 sal_Int16 nSet = sal_Int16();
241 if( rValue >>= nSet )
243 if( static_cast<rtl_TextEncoding>(nSet) == RTL_TEXTENCODING_SYMBOL )
245 rStrExpValue = GetXMLToken(XML_X_SYMBOL);
246 bRet = true;
250 return bRet;
254 XMLFontPitchPropHdl::~XMLFontPitchPropHdl()
256 // Nothing to do
259 bool XMLFontPitchPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
261 FontPitch eNewPitch;
262 bool bRet = SvXMLUnitConverter::convertEnum( eNewPitch, rStrImpValue, aFontPitchMapping );
263 if( bRet )
264 rValue <<= static_cast<sal_Int16>(eNewPitch);
266 return bRet;
269 bool XMLFontPitchPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
271 bool bRet = false;
272 sal_Int16 nPitch = sal_Int16();
274 FontPitch ePitch = PITCH_DONTKNOW;
275 if( rValue >>= nPitch )
276 ePitch = static_cast<FontPitch>(nPitch);
278 if( PITCH_DONTKNOW != ePitch )
280 OUStringBuffer aOut;
281 bRet = SvXMLUnitConverter::convertEnum( aOut, ePitch, aFontPitchMapping, XML_FIXED );
282 rStrExpValue = aOut.makeStringAndClear();
285 return bRet;
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */