Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <weighhdl.hxx>
31 : :
32 : : #include <sax/tools/converter.hxx>
33 : :
34 : : #include <xmloff/xmltoken.hxx>
35 : : #include <tools/fontenum.hxx>
36 : : #include <tools/solar.h>
37 : :
38 : : #ifndef _INC_LIMITS
39 : : #include <limits.h>
40 : : #endif
41 : : #include <rtl/ustrbuf.hxx>
42 : : #include <rtl/ustring.hxx>
43 : :
44 : : #include <com/sun/star/uno/Any.hxx>
45 : : #include <com/sun/star/awt/FontWeight.hpp>
46 : :
47 : : using ::rtl::OUString;
48 : : using ::rtl::OUStringBuffer;
49 : :
50 : : using namespace ::com::sun::star::uno;
51 : : using namespace ::xmloff::token;
52 : :
53 : : struct FontWeightMapper
54 : : {
55 : : float fWeight;
56 : : sal_uInt16 nValue;
57 : : };
58 : :
59 : : FontWeightMapper const aFontWeightMap[] =
60 : : {
61 : : { ::com::sun::star::awt::FontWeight::DONTKNOW, 0 },
62 : : { ::com::sun::star::awt::FontWeight::THIN, 100 },
63 : : { ::com::sun::star::awt::FontWeight::ULTRALIGHT, 150 },
64 : : { ::com::sun::star::awt::FontWeight::LIGHT, 250 },
65 : : { ::com::sun::star::awt::FontWeight::SEMILIGHT, 350 },
66 : : { ::com::sun::star::awt::FontWeight::NORMAL, 400 },
67 : : { ::com::sun::star::awt::FontWeight::NORMAL, 450 },
68 : : { ::com::sun::star::awt::FontWeight::SEMIBOLD, 600 },
69 : : { ::com::sun::star::awt::FontWeight::BOLD, 700 },
70 : : { ::com::sun::star::awt::FontWeight::ULTRABOLD, 800 },
71 : : { ::com::sun::star::awt::FontWeight::BLACK, 900 },
72 : : { ::com::sun::star::awt::FontWeight::DONTKNOW, 1000 }
73 : : };
74 : :
75 : : ///////////////////////////////////////////////////////////////////////////////
76 : : //
77 : : // class XMLFmtBreakBeforePropHdl
78 : : //
79 : :
80 : 3376 : XMLFontWeightPropHdl::~XMLFontWeightPropHdl()
81 : : {
82 : : // Nothing to do
83 [ - + ]: 6752 : }
84 : :
85 : 986 : sal_Bool XMLFontWeightPropHdl::importXML( const OUString& rStrImpValue, Any& rValue, const SvXMLUnitConverter& ) const
86 : : {
87 : 986 : sal_Bool bRet = sal_False;
88 : 986 : sal_uInt16 nWeight = 0;
89 : :
90 [ + + ]: 986 : if( IsXMLToken( rStrImpValue, XML_WEIGHT_NORMAL ) )
91 : : {
92 : 181 : nWeight = 400;
93 : 181 : bRet = sal_True;
94 : : }
95 [ + - ]: 805 : else if( IsXMLToken( rStrImpValue, XML_WEIGHT_BOLD ) )
96 : : {
97 : 805 : nWeight = 700;
98 : 805 : bRet = sal_True;
99 : : }
100 : : else
101 : : {
102 : : sal_Int32 nTemp;
103 [ # # ]: 0 : bRet = ::sax::Converter::convertNumber(nTemp, rStrImpValue, 100, 900);
104 [ # # ]: 0 : if( bRet )
105 : 0 : nWeight = sal::static_int_cast< sal_uInt16 >(nTemp);
106 : : }
107 : :
108 [ + - ]: 986 : if( bRet )
109 : : {
110 : 986 : bRet = sal_False;
111 : : static int nCount = sizeof(aFontWeightMap)/sizeof(FontWeightMapper);
112 [ + - ]: 8331 : for (int i = 0; i < (nCount-1); ++i)
113 : : {
114 [ + - ][ + + ]: 7345 : if( (nWeight >= aFontWeightMap[i].nValue) && (nWeight <= aFontWeightMap[i+1].nValue) )
115 : : {
116 : 986 : sal_uInt16 nDiff1 = nWeight - aFontWeightMap[i].nValue;
117 : 986 : sal_uInt16 nDiff2 = aFontWeightMap[i+1].nValue - nWeight;
118 : :
119 [ - + ]: 986 : if( nDiff1 < nDiff2 )
120 : 0 : rValue <<= aFontWeightMap[i].fWeight;
121 : : else
122 : 986 : rValue <<= aFontWeightMap[i+1].fWeight;
123 : :
124 : 986 : bRet = sal_True;
125 : 986 : break;
126 : : }
127 : : }
128 : : }
129 : :
130 : 986 : return bRet;
131 : : }
132 : :
133 : 188 : sal_Bool XMLFontWeightPropHdl::exportXML( OUString& rStrExpValue, const Any& rValue, const SvXMLUnitConverter& ) const
134 : : {
135 : 188 : sal_Bool bRet = sal_False;
136 : :
137 : 188 : float fValue = float();
138 [ - + ]: 188 : if( !( rValue >>= fValue ) )
139 : : {
140 : 0 : sal_Int32 nValue = 0;
141 [ # # ]: 0 : if( rValue >>= nValue )
142 : : {
143 : 0 : fValue = (float)nValue;
144 : 0 : bRet = sal_True;
145 : : }
146 : : }
147 : : else
148 : 188 : bRet = sal_True;
149 : :
150 [ + - ]: 188 : if( bRet )
151 : : {
152 : 188 : sal_uInt16 nWeight = 0;
153 : : static int nCount = sizeof(aFontWeightMap)/sizeof(FontWeightMapper);
154 [ + - ]: 1440 : for( int i=0; i<nCount; i++ )
155 : : {
156 [ + + ]: 1440 : if( fValue <= aFontWeightMap[i].fWeight )
157 : : {
158 : 188 : nWeight = aFontWeightMap[i].nValue;
159 : 188 : break;
160 : : }
161 : : }
162 : :
163 : 188 : OUStringBuffer aOut;
164 : :
165 [ + + ]: 188 : if( 400 == nWeight )
166 [ + - ][ + - ]: 84 : aOut.append( GetXMLToken(XML_WEIGHT_NORMAL) );
167 [ + - ]: 104 : else if( 700 == nWeight )
168 [ + - ][ + - ]: 104 : aOut.append( GetXMLToken(XML_WEIGHT_BOLD) );
169 : : else
170 [ # # ]: 0 : ::sax::Converter::convertNumber( aOut, (sal_Int32)nWeight );
171 : :
172 [ + - ]: 188 : rStrExpValue = aOut.makeStringAndClear();
173 : : }
174 : :
175 : 188 : return bRet;
176 : : }
177 : :
178 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|