1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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()
67 bool XMLFontFamilyNamePropHdl::importXML( const OUString
& rStrImpValue
, uno::Any
& rValue
, const SvXMLUnitConverter
& ) const
70 OUStringBuffer sValue
;
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
] )
83 // skip leading blanks
84 while(nFirst
<= nLast
&& ' ' == rStrImpValue
[nFirst
])
88 sal_Unicode c
= nFirst
> nLast
? 0 : rStrImpValue
[nFirst
];
89 if( nFirst
< nLast
&& ('\'' == c
|| '\"' == c
) && rStrImpValue
[nLast
] == c
)
97 if( !sValue
.isEmpty() )
100 sValue
.append(rStrImpValue
.subView(nFirst
, nLast
-nFirst
+1));
108 if (!sValue
.isEmpty())
110 rValue
<<= sValue
.makeStringAndClear();
117 bool XMLFontFamilyNamePropHdl::exportXML( OUString
& rStrExpValue
, const uno::Any
& rValue
, const SvXMLUnitConverter
& ) const
120 OUString aStrFamilyName
;
122 if( rValue
>>= aStrFamilyName
)
124 OUStringBuffer
sValue( aStrFamilyName
.getLength() + 2 );
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
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.
143 // nFirst and nLast now denote the first and last character of
147 // skip trailing blanks
148 while( nLast
> nFirst
&& ' ' == aStrFamilyName
[nLast
] )
151 // skip leading blanks
152 while( nFirst
<= nLast
&& ' ' == aStrFamilyName
[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
) );
162 for( sal_Int32 i
=0; i
< nLen
; i
++ )
164 sal_Unicode c
= sFamily
[i
];
165 if( ' ' == c
|| ',' == c
)
172 sValue
.append( '\'' );
173 sValue
.append( sFamily
);
175 sValue
.append( '\'' );
180 rStrExpValue
= sValue
.makeStringAndClear();
189 XMLFontFamilyPropHdl::~XMLFontFamilyPropHdl()
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() );
199 rValue
<<= static_cast<sal_Int16
>(eNewFamily
);
204 bool XMLFontFamilyPropHdl::exportXML( OUString
& rStrExpValue
, const uno::Any
& rValue
, const SvXMLUnitConverter
& ) const
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();
223 XMLFontEncodingPropHdl::~XMLFontEncodingPropHdl()
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
);
236 bool XMLFontEncodingPropHdl::exportXML( OUString
& rStrExpValue
, const uno::Any
& rValue
, const SvXMLUnitConverter
& ) const
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
);
254 XMLFontPitchPropHdl::~XMLFontPitchPropHdl()
259 bool XMLFontPitchPropHdl::importXML( const OUString
& rStrImpValue
, uno::Any
& rValue
, const SvXMLUnitConverter
& ) const
262 bool bRet
= SvXMLUnitConverter::convertEnum( eNewPitch
, rStrImpValue
, aFontPitchMapping
);
264 rValue
<<= static_cast<sal_Int16
>(eNewPitch
);
269 bool XMLFontPitchPropHdl::exportXML( OUString
& rStrExpValue
, const uno::Any
& rValue
, const SvXMLUnitConverter
& ) const
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
)
281 bRet
= SvXMLUnitConverter::convertEnum( aOut
, ePitch
, aFontPitchMapping
, XML_FIXED
);
282 rStrExpValue
= aOut
.makeStringAndClear();
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */