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