bump product version to 4.1.6.2
[LibreOffice.git] / xmloff / source / style / weighhdl.cxx
blob0ef9c84ff701a3be256673f90972276740334bb0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <weighhdl.hxx>
23 #include <sax/tools/converter.hxx>
25 #include <xmloff/xmltoken.hxx>
26 #include <tools/fontenum.hxx>
27 #include <tools/solar.h>
29 #ifndef _INC_LIMITS
30 #include <limits.h>
31 #endif
32 #include <rtl/ustrbuf.hxx>
33 #include <rtl/ustring.hxx>
35 #include <com/sun/star/uno/Any.hxx>
36 #include <com/sun/star/awt/FontWeight.hpp>
39 using namespace ::com::sun::star::uno;
40 using namespace ::xmloff::token;
42 struct FontWeightMapper
44 float fWeight;
45 sal_uInt16 nValue;
48 FontWeightMapper const aFontWeightMap[] =
50 { ::com::sun::star::awt::FontWeight::DONTKNOW, 0 },
51 { ::com::sun::star::awt::FontWeight::THIN, 100 },
52 { ::com::sun::star::awt::FontWeight::ULTRALIGHT, 150 },
53 { ::com::sun::star::awt::FontWeight::LIGHT, 250 },
54 { ::com::sun::star::awt::FontWeight::SEMILIGHT, 350 },
55 { ::com::sun::star::awt::FontWeight::NORMAL, 400 },
56 { ::com::sun::star::awt::FontWeight::NORMAL, 450 },
57 { ::com::sun::star::awt::FontWeight::SEMIBOLD, 600 },
58 { ::com::sun::star::awt::FontWeight::BOLD, 700 },
59 { ::com::sun::star::awt::FontWeight::ULTRABOLD, 800 },
60 { ::com::sun::star::awt::FontWeight::BLACK, 900 },
61 { ::com::sun::star::awt::FontWeight::DONTKNOW, 1000 }
64 ///////////////////////////////////////////////////////////////////////////////
66 // class XMLFmtBreakBeforePropHdl
69 XMLFontWeightPropHdl::~XMLFontWeightPropHdl()
71 // Nothing to do
74 sal_Bool XMLFontWeightPropHdl::importXML( const OUString& rStrImpValue, Any& rValue, const SvXMLUnitConverter& ) const
76 sal_Bool bRet = sal_False;
77 sal_uInt16 nWeight = 0;
79 if( IsXMLToken( rStrImpValue, XML_WEIGHT_NORMAL ) )
81 nWeight = 400;
82 bRet = sal_True;
84 else if( IsXMLToken( rStrImpValue, XML_WEIGHT_BOLD ) )
86 nWeight = 700;
87 bRet = sal_True;
89 else
91 sal_Int32 nTemp;
92 bRet = ::sax::Converter::convertNumber(nTemp, rStrImpValue, 100, 900);
93 if( bRet )
94 nWeight = sal::static_int_cast< sal_uInt16 >(nTemp);
97 if( bRet )
99 bRet = sal_False;
100 static int nCount = sizeof(aFontWeightMap)/sizeof(FontWeightMapper);
101 for (int i = 0; i < (nCount-1); ++i)
103 if( (nWeight >= aFontWeightMap[i].nValue) && (nWeight <= aFontWeightMap[i+1].nValue) )
105 sal_uInt16 nDiff1 = nWeight - aFontWeightMap[i].nValue;
106 sal_uInt16 nDiff2 = aFontWeightMap[i+1].nValue - nWeight;
108 if( nDiff1 < nDiff2 )
109 rValue <<= aFontWeightMap[i].fWeight;
110 else
111 rValue <<= aFontWeightMap[i+1].fWeight;
113 bRet = sal_True;
114 break;
119 return bRet;
122 sal_Bool XMLFontWeightPropHdl::exportXML( OUString& rStrExpValue, const Any& rValue, const SvXMLUnitConverter& ) const
124 sal_Bool bRet = sal_False;
126 float fValue = float();
127 if( !( rValue >>= fValue ) )
129 sal_Int32 nValue = 0;
130 if( rValue >>= nValue )
132 fValue = (float)nValue;
133 bRet = sal_True;
136 else
137 bRet = sal_True;
139 if( bRet )
141 sal_uInt16 nWeight = 0;
142 static int nCount = sizeof(aFontWeightMap)/sizeof(FontWeightMapper);
143 for( int i=0; i<nCount; i++ )
145 if( fValue <= aFontWeightMap[i].fWeight )
147 nWeight = aFontWeightMap[i].nValue;
148 break;
152 OUStringBuffer aOut;
154 if( 400 == nWeight )
155 aOut.append( GetXMLToken(XML_WEIGHT_NORMAL) );
156 else if( 700 == nWeight )
157 aOut.append( GetXMLToken(XML_WEIGHT_BOLD) );
158 else
159 ::sax::Converter::convertNumber( aOut, (sal_Int32)nWeight );
161 rStrExpValue = aOut.makeStringAndClear();
164 return bRet;
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */