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