1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
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()
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
) )
84 else if( IsXMLToken( rStrImpValue
, XML_WEIGHT_BOLD
) )
92 bRet
= ::sax::Converter::convertNumber(nTemp
, rStrImpValue
, 100, 900);
94 nWeight
= sal::static_int_cast
< sal_uInt16
>(nTemp
);
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
;
111 rValue
<<= aFontWeightMap
[i
+1].fWeight
;
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
;
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
;
155 aOut
.append( GetXMLToken(XML_WEIGHT_NORMAL
) );
156 else if( 700 == nWeight
)
157 aOut
.append( GetXMLToken(XML_WEIGHT_BOLD
) );
159 ::sax::Converter::convertNumber( aOut
, (sal_Int32
)nWeight
);
161 rStrExpValue
= aOut
.makeStringAndClear();
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */