xref: /AOO41X/main/xmloff/source/style/weighhdl.cxx (revision 1776ad396d100a46b9f68f725413ce2ae561dd84)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 #include <weighhdl.hxx>
27 #include <xmloff/xmltoken.hxx>
28 #include <xmloff/xmluconv.hxx>
29 #include <tools/fontenum.hxx>
30 #include <tools/solar.h>
31 
32 #ifndef _INC_LIMITS
33 #include <limits.h>
34 #endif
35 #include <rtl/ustrbuf.hxx>
36 #include <rtl/ustring.hxx>
37 
38 #include <com/sun/star/uno/Any.hxx>
39 #include <com/sun/star/awt/FontWeight.hpp>
40 
41 using ::rtl::OUString;
42 using ::rtl::OUStringBuffer;
43 
44 using namespace ::com::sun::star::uno;
45 using namespace ::xmloff::token;
46 
47 struct FontWeightMapper
48 {
49     float fWeight;
50     sal_uInt16 nValue;
51 };
52 
53 FontWeightMapper const aFontWeightMap[] =
54 {
55     { ::com::sun::star::awt::FontWeight::DONTKNOW,              0 },
56     { ::com::sun::star::awt::FontWeight::THIN,                  100 },
57     { ::com::sun::star::awt::FontWeight::ULTRALIGHT,            150 },
58     { ::com::sun::star::awt::FontWeight::LIGHT,                 250 },
59     { ::com::sun::star::awt::FontWeight::SEMILIGHT,             350 },
60     { ::com::sun::star::awt::FontWeight::NORMAL,                400 },
61     { ::com::sun::star::awt::FontWeight::NORMAL,                450 },
62     { ::com::sun::star::awt::FontWeight::SEMIBOLD,              600 },
63     { ::com::sun::star::awt::FontWeight::BOLD,                  700 },
64     { ::com::sun::star::awt::FontWeight::ULTRABOLD,             800 },
65     { ::com::sun::star::awt::FontWeight::BLACK,                 900 },
66     { ::com::sun::star::awt::FontWeight::DONTKNOW,             1000 }
67 };
68 
69 ///////////////////////////////////////////////////////////////////////////////
70 //
71 // class XMLFmtBreakBeforePropHdl
72 //
73 
~XMLFontWeightPropHdl()74 XMLFontWeightPropHdl::~XMLFontWeightPropHdl()
75 {
76     // Nothing to do
77 }
78 
importXML(const OUString & rStrImpValue,Any & rValue,const SvXMLUnitConverter &) const79 sal_Bool XMLFontWeightPropHdl::importXML( const OUString& rStrImpValue, Any& rValue, const SvXMLUnitConverter& ) const
80 {
81     sal_Bool bRet = sal_False;
82     sal_uInt16 nWeight = 0;
83 
84     if( IsXMLToken( rStrImpValue, XML_WEIGHT_NORMAL ) )
85     {
86         nWeight = 400;
87         bRet = sal_True;
88     }
89     else if( IsXMLToken( rStrImpValue, XML_WEIGHT_BOLD ) )
90     {
91         nWeight = 700;
92         bRet = sal_True;
93     }
94     else
95     {
96         sal_Int32 nTemp;
97         bRet = SvXMLUnitConverter::convertNumber( nTemp, rStrImpValue, 100, 900 );
98         if( bRet )
99             nWeight = sal::static_int_cast< sal_uInt16 >(nTemp);
100     }
101 
102     if( bRet )
103     {
104         bRet = sal_False;
105         static int nCount = sizeof(aFontWeightMap)/sizeof(FontWeightMapper);
106         for (int i = 0; i < (nCount-1); ++i)
107         {
108             if( (nWeight >= aFontWeightMap[i].nValue) && (nWeight <= aFontWeightMap[i+1].nValue) )
109             {
110                 sal_uInt16 nDiff1 = nWeight - aFontWeightMap[i].nValue;
111                 sal_uInt16 nDiff2 = aFontWeightMap[i+1].nValue - nWeight;
112 
113                 if( nDiff1 < nDiff2 )
114                     rValue <<= aFontWeightMap[i].fWeight;
115                 else
116                     rValue <<= aFontWeightMap[i+1].fWeight;
117 
118                 bRet = sal_True;
119                 break;
120             }
121         }
122     }
123 
124     return bRet;
125 }
126 
exportXML(OUString & rStrExpValue,const Any & rValue,const SvXMLUnitConverter &) const127 sal_Bool XMLFontWeightPropHdl::exportXML( OUString& rStrExpValue, const Any& rValue, const SvXMLUnitConverter& ) const
128 {
129     sal_Bool bRet = sal_False;
130 
131     float fValue = float();
132     if( !( rValue >>= fValue ) )
133     {
134         sal_Int32 nValue = 0;
135         if( rValue >>= nValue )
136         {
137             fValue = (float)nValue;
138             bRet = sal_True;
139         }
140     }
141     else
142         bRet = sal_True;
143 
144     if( bRet )
145     {
146         sal_uInt16 nWeight = 0;
147         static int nCount = sizeof(aFontWeightMap)/sizeof(FontWeightMapper);
148         for( int i=0; i<nCount; i++ )
149         {
150             if( fValue <= aFontWeightMap[i].fWeight )
151             {
152                  nWeight = aFontWeightMap[i].nValue;
153                  break;
154             }
155         }
156 
157         OUStringBuffer aOut;
158 
159         if( 400 == nWeight )
160             aOut.append( GetXMLToken(XML_WEIGHT_NORMAL) );
161         else if( 700 == nWeight )
162             aOut.append( GetXMLToken(XML_WEIGHT_BOLD) );
163         else
164             SvXMLUnitConverter::convertNumber( aOut, (sal_Int32)nWeight );
165 
166         rStrExpValue = aOut.makeStringAndClear();
167     }
168 
169     return bRet;
170 }
171 
172