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