1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: weighhdl.cxx,v $
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>
42 #include <rtl/ustrbuf.hxx>
43 #include <rtl/ustring.hxx>
45 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
46 #include <toolkit/unohlp.hxx>
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
62 FontWeightMapper
const aFontWeightMap
[] =
64 { WEIGHT_DONTKNOW
, 0 },
66 { WEIGHT_ULTRALIGHT
, 150 },
67 { WEIGHT_LIGHT
, 250 },
68 { WEIGHT_SEMILIGHT
, 350 },
69 { WEIGHT_NORMAL
, 400 },
70 { WEIGHT_MEDIUM
, 450 },
71 { WEIGHT_SEMIBOLD
, 600 },
73 { WEIGHT_ULTRABOLD
, 800 },
74 { WEIGHT_BLACK
, 900 },
75 { (FontWeight
)USHRT_MAX
, 1000 }
78 ///////////////////////////////////////////////////////////////////////////////
80 // class XMLFmtBreakBeforePropHdl
83 XMLFontWeightPropHdl::~XMLFontWeightPropHdl()
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
) )
98 else if( IsXMLToken( rStrImpValue
, XML_WEIGHT_BOLD
) )
106 bRet
= SvXMLUnitConverter::convertNumber( nTemp
, rStrImpValue
, 100, 900 );
108 nWeight
= sal::static_int_cast
< sal_uInt16
>(nTemp
);
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
) );
125 rValue
<<= (float)( VCLUnoHelper::ConvertFontWeight( aFontWeightMap
[i
+1].eWeight
) );
136 sal_Bool
XMLFontWeightPropHdl::exportXML( OUString
& rStrExpValue
, const Any
& rValue
, const SvXMLUnitConverter
& ) const
138 sal_Bool bRet
= sal_False
;
141 float fValue
= float();
142 if( !( rValue
>>= fValue
) )
144 sal_Int32 nValue
= 0;
145 if( rValue
>>= nValue
)
147 fValue
= (float)nValue
;
154 eWeight
= VCLUnoHelper::ConvertFontWeight( fValue
);
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
;
172 aOut
.append( GetXMLToken(XML_WEIGHT_NORMAL
) );
173 else if( 700 == nWeight
)
174 aOut
.append( GetXMLToken(XML_WEIGHT_BOLD
) );
176 SvXMLUnitConverter::convertNumber( aOut
, (sal_Int32
)nWeight
);
178 rStrExpValue
= aOut
.makeStringAndClear();