LibreOffice Module xmloff (master) 1
weighhdl.cxx
Go to the documentation of this file.
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 "weighhdl.hxx"
21
23
24#include <xmloff/xmltoken.hxx>
25
26#include <rtl/ustring.hxx>
27
28#include <com/sun/star/uno/Any.hxx>
29#include <com/sun/star/awt/FontWeight.hpp>
30
31using namespace ::com::sun::star::uno;
32using namespace ::xmloff::token;
33
34namespace {
35
36struct FontWeightMapper
37{
38 float fWeight;
39 sal_uInt16 nValue;
40};
41
42}
43
44FontWeightMapper const aFontWeightMap[] =
45{
46 { css::awt::FontWeight::DONTKNOW, 0 },
47 { css::awt::FontWeight::THIN, 100 },
48 { css::awt::FontWeight::ULTRALIGHT, 150 },
49 { css::awt::FontWeight::LIGHT, 250 },
50 { css::awt::FontWeight::SEMILIGHT, 350 },
51 { css::awt::FontWeight::NORMAL, 400 },
52 { css::awt::FontWeight::NORMAL, 450 },
53 { css::awt::FontWeight::SEMIBOLD, 600 },
54 { css::awt::FontWeight::BOLD, 700 },
55 { css::awt::FontWeight::ULTRABOLD, 800 },
56 { css::awt::FontWeight::BLACK, 900 },
57 { css::awt::FontWeight::DONTKNOW, 1000 }
58};
59
60
62{
63 // Nothing to do
64}
65
66bool XMLFontWeightPropHdl::importXML( const OUString& rStrImpValue, Any& rValue, const SvXMLUnitConverter& ) const
67{
68 bool bRet = false;
69 sal_uInt16 nWeight = 0;
70
71 if( IsXMLToken( rStrImpValue, XML_NORMAL ) )
72 {
73 nWeight = 400;
74 bRet = true;
75 }
76 else if( IsXMLToken( rStrImpValue, XML_BOLD ) )
77 {
78 nWeight = 700;
79 bRet = true;
80 }
81 else
82 {
83 sal_Int32 nTemp;
84 bRet = ::sax::Converter::convertNumber(nTemp, rStrImpValue, 100, 900);
85 if( bRet )
86 nWeight = sal::static_int_cast< sal_uInt16 >(nTemp);
87 }
88
89 if( bRet )
90 {
91 bRet = false;
93 for (int i = 0; i < (nCount-1); ++i)
94 {
95 if( (nWeight >= aFontWeightMap[i].nValue) && (nWeight <= aFontWeightMap[i+1].nValue) )
96 {
97 sal_uInt16 nDiff1 = nWeight - aFontWeightMap[i].nValue;
98 sal_uInt16 nDiff2 = aFontWeightMap[i+1].nValue - nWeight;
99
100 if( nDiff1 < nDiff2 )
101 rValue <<= aFontWeightMap[i].fWeight;
102 else
103 rValue <<= aFontWeightMap[i+1].fWeight;
104
105 bRet = true;
106 break;
107 }
108 }
109 }
110
111 return bRet;
112}
113
114bool XMLFontWeightPropHdl::exportXML( OUString& rStrExpValue, const Any& rValue, const SvXMLUnitConverter& ) const
115{
116 bool bRet = false;
117
118 float fValue = float();
119 if( !( rValue >>= fValue ) )
120 {
121 sal_Int32 nValue = 0;
122 if( rValue >>= nValue )
123 {
124 fValue = static_cast<float>(nValue);
125 bRet = true;
126 }
127 }
128 else
129 bRet = true;
130
131 if( bRet )
132 {
133 sal_uInt16 nWeight = 0;
134 for( auto const & pair : aFontWeightMap )
135 {
136 if( fValue <= pair.fWeight )
137 {
138 nWeight = pair.nValue;
139 break;
140 }
141 }
142
143 if( 400 == nWeight )
144 rStrExpValue = GetXMLToken(XML_NORMAL);
145 else if( 700 == nWeight )
146 rStrExpValue = GetXMLToken(XML_BOLD);
147 else
148 rStrExpValue = OUString::number( nWeight );
149 }
150
151 return bRet;
152}
153
154/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
the SvXMLTypeConverter converts values of various types from their internal representation to the tex...
Definition: xmluconv.hxx:83
virtual bool exportXML(OUString &rStrExpValue, const css::uno::Any &rValue, const SvXMLUnitConverter &rUnitConverter) const override
Exports the given value according to the XML-data-type corresponding to the derived class.
Definition: weighhdl.cxx:114
virtual ~XMLFontWeightPropHdl() override
Definition: weighhdl.cxx:61
virtual bool importXML(const OUString &rStrImpValue, css::uno::Any &rValue, const SvXMLUnitConverter &rUnitConverter) const override
Imports the given value according to the XML-data-type corresponding to the derived class.
Definition: weighhdl.cxx:66
static bool convertNumber(sal_Int32 &rValue, std::u16string_view aString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)
int nCount
sal_Int16 nValue
#define SAL_N_ELEMENTS(arr)
int i
Handling of tokens in XML:
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3597
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3541
FontWeightMapper const aFontWeightMap[]
Definition: weighhdl.cxx:44