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