merge the formfield patch from ooo-build
[ooovba.git] / xmloff / source / style / weighhdl.cxx
blob39585b04a3c20eca1e5b70205eb9f714789ab11a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: weighhdl.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include <weighhdl.hxx>
34 #include <xmloff/xmltoken.hxx>
35 #include <xmloff/xmluconv.hxx>
36 #include <vcl/vclenum.hxx>
37 #include <tools/solar.h>
39 #ifndef _INC_LIMITS
40 #include <limits.h>
41 #endif
42 #include <rtl/ustrbuf.hxx>
43 #include <rtl/ustring.hxx>
45 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
46 #include <toolkit/unohlp.hxx>
47 #endif
48 #include <com/sun/star/uno/Any.hxx>
50 using ::rtl::OUString;
51 using ::rtl::OUStringBuffer;
53 using namespace ::com::sun::star::uno;
54 using namespace ::xmloff::token;
56 struct FontWeightMapper
58 FontWeight eWeight;
59 USHORT nValue;
62 FontWeightMapper const aFontWeightMap[] =
64 { WEIGHT_DONTKNOW, 0 },
65 { WEIGHT_THIN, 100 },
66 { WEIGHT_ULTRALIGHT, 150 },
67 { WEIGHT_LIGHT, 250 },
68 { WEIGHT_SEMILIGHT, 350 },
69 { WEIGHT_NORMAL, 400 },
70 { WEIGHT_MEDIUM, 450 },
71 { WEIGHT_SEMIBOLD, 600 },
72 { WEIGHT_BOLD, 700 },
73 { WEIGHT_ULTRABOLD, 800 },
74 { WEIGHT_BLACK, 900 },
75 { (FontWeight)USHRT_MAX, 1000 }
78 ///////////////////////////////////////////////////////////////////////////////
80 // class XMLFmtBreakBeforePropHdl
83 XMLFontWeightPropHdl::~XMLFontWeightPropHdl()
85 // Nothing to do
88 sal_Bool XMLFontWeightPropHdl::importXML( const OUString& rStrImpValue, Any& rValue, const SvXMLUnitConverter& ) const
90 sal_Bool bRet = sal_False;
91 sal_uInt16 nWeight = 0;
93 if( IsXMLToken( rStrImpValue, XML_WEIGHT_NORMAL ) )
95 nWeight = 400;
96 bRet = sal_True;
98 else if( IsXMLToken( rStrImpValue, XML_WEIGHT_BOLD ) )
100 nWeight = 700;
101 bRet = sal_True;
103 else
105 sal_Int32 nTemp;
106 bRet = SvXMLUnitConverter::convertNumber( nTemp, rStrImpValue, 100, 900 );
107 if( bRet )
108 nWeight = sal::static_int_cast< sal_uInt16 >(nTemp);
111 if( bRet )
113 bRet = sal_False;
115 for( int i = 0; aFontWeightMap[i].eWeight != USHRT_MAX; i++ )
117 if( (nWeight >= aFontWeightMap[i].nValue) && (nWeight <= aFontWeightMap[i+1].nValue) )
119 sal_uInt16 nDiff1 = nWeight - aFontWeightMap[i].nValue;
120 sal_uInt16 nDiff2 = aFontWeightMap[i+1].nValue - nWeight;
122 if( nDiff1 < nDiff2 )
123 rValue <<= (float)( VCLUnoHelper::ConvertFontWeight( aFontWeightMap[i].eWeight ) );
124 else
125 rValue <<= (float)( VCLUnoHelper::ConvertFontWeight( aFontWeightMap[i+1].eWeight ) );
127 bRet = sal_True;
128 break;
133 return bRet;
136 sal_Bool XMLFontWeightPropHdl::exportXML( OUString& rStrExpValue, const Any& rValue, const SvXMLUnitConverter& ) const
138 sal_Bool bRet = sal_False;
139 FontWeight eWeight;
141 float fValue = float();
142 if( !( rValue >>= fValue ) )
144 sal_Int32 nValue = 0;
145 if( rValue >>= nValue )
147 fValue = (float)nValue;
148 bRet = sal_True;
151 else
152 bRet = sal_True;
154 eWeight = VCLUnoHelper::ConvertFontWeight( fValue );
156 if( bRet )
158 sal_uInt16 nWeight = 0;
160 for( int i = 0; aFontWeightMap[i].eWeight != -1; i++ )
162 if( aFontWeightMap[i].eWeight == eWeight )
164 nWeight = aFontWeightMap[i].nValue;
165 break;
169 OUStringBuffer aOut;
171 if( 400 == nWeight )
172 aOut.append( GetXMLToken(XML_WEIGHT_NORMAL) );
173 else if( 700 == nWeight )
174 aOut.append( GetXMLToken(XML_WEIGHT_BOLD) );
175 else
176 SvXMLUnitConverter::convertNumber( aOut, (sal_Int32)nWeight );
178 rStrExpValue = aOut.makeStringAndClear();
181 return bRet;