Line data Source code
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 :
22 : #include <sax/tools/converter.hxx>
23 :
24 : #include <xmloff/xmltoken.hxx>
25 : #include <tools/fontenum.hxx>
26 :
27 : #include <limits.h>
28 : #include <rtl/ustrbuf.hxx>
29 : #include <rtl/ustring.hxx>
30 :
31 : #include <com/sun/star/uno/Any.hxx>
32 : #include <com/sun/star/awt/FontWeight.hpp>
33 :
34 : using namespace ::com::sun::star::uno;
35 : using namespace ::xmloff::token;
36 :
37 : struct FontWeightMapper
38 : {
39 : float fWeight;
40 : sal_uInt16 nValue;
41 : };
42 :
43 : FontWeightMapper const aFontWeightMap[] =
44 : {
45 : { ::com::sun::star::awt::FontWeight::DONTKNOW, 0 },
46 : { ::com::sun::star::awt::FontWeight::THIN, 100 },
47 : { ::com::sun::star::awt::FontWeight::ULTRALIGHT, 150 },
48 : { ::com::sun::star::awt::FontWeight::LIGHT, 250 },
49 : { ::com::sun::star::awt::FontWeight::SEMILIGHT, 350 },
50 : { ::com::sun::star::awt::FontWeight::NORMAL, 400 },
51 : { ::com::sun::star::awt::FontWeight::NORMAL, 450 },
52 : { ::com::sun::star::awt::FontWeight::SEMIBOLD, 600 },
53 : { ::com::sun::star::awt::FontWeight::BOLD, 700 },
54 : { ::com::sun::star::awt::FontWeight::ULTRABOLD, 800 },
55 : { ::com::sun::star::awt::FontWeight::BLACK, 900 },
56 : { ::com::sun::star::awt::FontWeight::DONTKNOW, 1000 }
57 : };
58 :
59 : // class XMLFmtBreakBeforePropHdl
60 :
61 21586 : XMLFontWeightPropHdl::~XMLFontWeightPropHdl()
62 : {
63 : // Nothing to do
64 21586 : }
65 :
66 3966 : bool XMLFontWeightPropHdl::importXML( const OUString& rStrImpValue, Any& rValue, const SvXMLUnitConverter& ) const
67 : {
68 3966 : bool bRet = false;
69 3966 : sal_uInt16 nWeight = 0;
70 :
71 3966 : if( IsXMLToken( rStrImpValue, XML_WEIGHT_NORMAL ) )
72 : {
73 1544 : nWeight = 400;
74 1544 : bRet = true;
75 : }
76 2422 : else if( IsXMLToken( rStrImpValue, XML_WEIGHT_BOLD ) )
77 : {
78 2418 : nWeight = 700;
79 2418 : bRet = true;
80 : }
81 : else
82 : {
83 : sal_Int32 nTemp;
84 4 : bRet = ::sax::Converter::convertNumber(nTemp, rStrImpValue, 100, 900);
85 4 : if( bRet )
86 4 : nWeight = sal::static_int_cast< sal_uInt16 >(nTemp);
87 : }
88 :
89 3966 : if( bRet )
90 : {
91 3966 : bRet = false;
92 : static int nCount = sizeof(aFontWeightMap)/sizeof(FontWeightMapper);
93 27090 : for (int i = 0; i < (nCount-1); ++i)
94 : {
95 27090 : if( (nWeight >= aFontWeightMap[i].nValue) && (nWeight <= aFontWeightMap[i+1].nValue) )
96 : {
97 3966 : sal_uInt16 nDiff1 = nWeight - aFontWeightMap[i].nValue;
98 3966 : sal_uInt16 nDiff2 = aFontWeightMap[i+1].nValue - nWeight;
99 :
100 3966 : if( nDiff1 < nDiff2 )
101 0 : rValue <<= aFontWeightMap[i].fWeight;
102 : else
103 3966 : rValue <<= aFontWeightMap[i+1].fWeight;
104 :
105 3966 : bRet = true;
106 3966 : break;
107 : }
108 : }
109 : }
110 :
111 3966 : return bRet;
112 : }
113 :
114 1141 : bool XMLFontWeightPropHdl::exportXML( OUString& rStrExpValue, const Any& rValue, const SvXMLUnitConverter& ) const
115 : {
116 1141 : bool bRet = false;
117 :
118 1141 : float fValue = float();
119 1141 : if( !( rValue >>= fValue ) )
120 : {
121 0 : sal_Int32 nValue = 0;
122 0 : if( rValue >>= nValue )
123 : {
124 0 : fValue = (float)nValue;
125 0 : bRet = true;
126 : }
127 : }
128 : else
129 1141 : bRet = true;
130 :
131 1141 : if( bRet )
132 : {
133 1141 : sal_uInt16 nWeight = 0;
134 : static int nCount = sizeof(aFontWeightMap)/sizeof(FontWeightMapper);
135 9717 : for( int i=0; i<nCount; i++ )
136 : {
137 9717 : if( fValue <= aFontWeightMap[i].fWeight )
138 : {
139 1141 : nWeight = aFontWeightMap[i].nValue;
140 1141 : break;
141 : }
142 : }
143 :
144 1141 : OUStringBuffer aOut;
145 :
146 1141 : if( 400 == nWeight )
147 184 : aOut.append( GetXMLToken(XML_WEIGHT_NORMAL) );
148 957 : else if( 700 == nWeight )
149 957 : aOut.append( GetXMLToken(XML_WEIGHT_BOLD) );
150 : else
151 0 : ::sax::Converter::convertNumber( aOut, (sal_Int32)nWeight );
152 :
153 1141 : rStrExpValue = aOut.makeStringAndClear();
154 : }
155 :
156 1141 : return bRet;
157 : }
158 :
159 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|