Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / xmloff / source / style / fonthdl.cxx
blob3d736a1f1a75b9a8761675c525a3135f91460c9b
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() )
159 sValue.append( ',' );
160 sValue.append( ' ' );
162 sal_Int32 nLen = nLast-nFirst+1;
163 std::u16string_view sFamily( aStrFamilyName.subView( nFirst, nLen ) );
164 bool bQuote = false;
165 for( sal_Int32 i=0; i < nLen; i++ )
167 sal_Unicode c = sFamily[i];
168 if( ' ' == c || ',' == c )
170 bQuote = true;
171 break;
174 if( bQuote )
175 sValue.append( '\'' );
176 sValue.append( sFamily );
177 if( bQuote )
178 sValue.append( '\'' );
181 while( -1 != nPos );
183 rStrExpValue = sValue.makeStringAndClear();
185 bRet = true;
188 return bRet;
192 XMLFontFamilyPropHdl::~XMLFontFamilyPropHdl()
194 // Nothing to do
197 bool XMLFontFamilyPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
199 FontFamily eNewFamily;
200 bool bRet = SvXMLUnitConverter::convertEnum( eNewFamily, rStrImpValue, lcl_getFontFamilyGenericMapping() );
201 if( bRet )
202 rValue <<= static_cast<sal_Int16>(eNewFamily);
204 return bRet;
207 bool XMLFontFamilyPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
209 bool bRet = false;
210 OUStringBuffer aOut;
212 sal_Int16 nFamily = sal_Int16();
213 if( rValue >>= nFamily )
215 FontFamily eFamily = static_cast<FontFamily>(nFamily);
216 if( eFamily != FAMILY_DONTKNOW )
217 bRet = SvXMLUnitConverter::convertEnum( aOut, eFamily, lcl_getFontFamilyGenericMapping() );
220 rStrExpValue = aOut.makeStringAndClear();
222 return bRet;
226 XMLFontEncodingPropHdl::~XMLFontEncodingPropHdl()
228 // Nothing to do
231 bool XMLFontEncodingPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
233 if( IsXMLToken( rStrImpValue, XML_X_SYMBOL ) )
234 rValue <<= sal_Int16(RTL_TEXTENCODING_SYMBOL);
236 return true;
239 bool XMLFontEncodingPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
241 bool bRet = false;
242 sal_Int16 nSet = sal_Int16();
244 if( rValue >>= nSet )
246 if( static_cast<rtl_TextEncoding>(nSet) == RTL_TEXTENCODING_SYMBOL )
248 rStrExpValue = GetXMLToken(XML_X_SYMBOL);
249 bRet = true;
253 return bRet;
257 XMLFontPitchPropHdl::~XMLFontPitchPropHdl()
259 // Nothing to do
262 bool XMLFontPitchPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
264 FontPitch eNewPitch;
265 bool bRet = SvXMLUnitConverter::convertEnum( eNewPitch, rStrImpValue, aFontPitchMapping );
266 if( bRet )
267 rValue <<= static_cast<sal_Int16>(eNewPitch);
269 return bRet;
272 bool XMLFontPitchPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
274 bool bRet = false;
275 sal_Int16 nPitch = sal_Int16();
277 FontPitch ePitch = PITCH_DONTKNOW;
278 if( rValue >>= nPitch )
279 ePitch = static_cast<FontPitch>(nPitch);
281 if( PITCH_DONTKNOW != ePitch )
283 OUStringBuffer aOut;
284 bRet = SvXMLUnitConverter::convertEnum( aOut, ePitch, aFontPitchMapping, XML_FIXED );
285 rStrExpValue = aOut.makeStringAndClear();
288 return bRet;
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */