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 .
20 #include <oox/drawingml/drawingmltypes.hxx>
21 #include <com/sun/star/awt/FontUnderline.hpp>
22 #include <com/sun/star/awt/FontStrikeout.hpp>
23 #include <com/sun/star/drawing/Hatch.hpp>
24 #include <com/sun/star/style/CaseMap.hpp>
25 #include <com/sun/star/xml/sax/XFastAttributeList.hpp>
26 #include <o3tl/safeint.hxx>
27 #include <osl/diagnose.h>
28 #include <sax/tools/converter.hxx>
29 #include <oox/token/tokens.hxx>
31 using ::com::sun::star::uno::Reference
;
32 using ::com::sun::star::xml::sax::XFastAttributeList
;
33 using namespace ::com::sun::star
;
34 using namespace ::com::sun::star::drawing
;
35 using namespace ::com::sun::star::geometry
;
36 using namespace ::com::sun::star::style
;
41 /** converts EMUs into 1/100th mmm */
42 sal_Int32
GetCoordinate( sal_Int32 nValue
)
44 nValue
= o3tl::saturating_add
<sal_Int32
>(nValue
, 180);
48 /** converts an emu string into 1/100th mmm */
49 sal_Int32
GetCoordinate( const OUString
& sValue
)
52 if( !::sax::Converter::convertNumber( nRet
, sValue
) )
54 return GetCoordinate( nRet
);
57 /** converts a ST_Percentage % string into 1/1000th of % */
58 sal_Int32
GetPercent( const OUString
& sValue
)
61 if( !::sax::Converter::convertNumber( nRet
, sValue
) )
67 double GetPositiveFixedPercentage( const OUString
& sValue
)
69 double fPercent
= sValue
.toFloat() / 100000.;
73 /** converts the attributes from a CT_TLPoint into an awt Point with 1/1000% */
74 awt::Point
GetPointPercent( const Reference
< XFastAttributeList
>& xAttribs
)
76 return awt::Point(GetPercent(xAttribs
->getOptionalValue(XML_x
)), GetPercent(xAttribs
->getOptionalValue(XML_y
)));
79 /** converts the ST_TextFontSize to point */
80 float GetTextSize( const OUString
& sValue
)
84 if( ::sax::Converter::convertNumber( nRet
, sValue
) )
85 fRet
= static_cast< float >( static_cast< double >( nRet
) / 100.0 );
89 /** converts the ST_TextSpacingPoint to 1/100mm */
90 sal_Int32
GetTextSpacingPoint( const OUString
& sValue
)
93 if( ::sax::Converter::convertNumber( nRet
, sValue
, (SAL_MIN_INT32
+ 360) / 254, (SAL_MAX_INT32
- 360) / 254 ) )
94 nRet
= GetTextSpacingPoint( nRet
);
98 sal_Int32
GetTextSpacingPoint(sal_Int32 nValue
)
101 nValue
= (nValue
* 254 + 360);
103 nValue
= (nValue
* 254 - 360);
107 float GetFontHeight( sal_Int32 nHeight
)
109 // convert 1/100 points to points
110 return static_cast< float >( nHeight
/ 100.0 );
113 sal_Int16
GetFontUnderline( sal_Int32 nToken
)
115 OSL_ASSERT((nToken
& sal_Int32(0xFFFF0000))==0);
118 case XML_none
: return awt::FontUnderline::NONE
;
119 case XML_dash
: return awt::FontUnderline::DASH
;
120 case XML_dashHeavy
: return awt::FontUnderline::BOLDDASH
;
121 case XML_dashLong
: return awt::FontUnderline::LONGDASH
;
122 case XML_dashLongHeavy
: return awt::FontUnderline::BOLDLONGDASH
;
123 case XML_dbl
: return awt::FontUnderline::DOUBLE
;
124 case XML_dotDash
: return awt::FontUnderline::DASHDOT
;
125 case XML_dotDashHeavy
: return awt::FontUnderline::BOLDDASHDOT
;
126 case XML_dotDotDash
: return awt::FontUnderline::DASHDOTDOT
;
127 case XML_dotDotDashHeavy
: return awt::FontUnderline::BOLDDASHDOTDOT
;
128 case XML_dotted
: return awt::FontUnderline::DOTTED
;
129 case XML_dottedHeavy
: return awt::FontUnderline::BOLDDOTTED
;
130 case XML_heavy
: return awt::FontUnderline::BOLD
;
131 case XML_sng
: return awt::FontUnderline::SINGLE
;
132 case XML_wavy
: return awt::FontUnderline::WAVE
;
133 case XML_wavyDbl
: return awt::FontUnderline::DOUBLEWAVE
;
134 case XML_wavyHeavy
: return awt::FontUnderline::BOLDWAVE
;
135 // case XML_words: // TODO
137 return awt::FontUnderline::DONTKNOW
;
140 sal_Int16
GetFontStrikeout( sal_Int32 nToken
)
142 OSL_ASSERT((nToken
& sal_Int32(0xFFFF0000))==0);
145 case XML_dblStrike
: return awt::FontStrikeout::DOUBLE
;
146 case XML_noStrike
: return awt::FontStrikeout::NONE
;
147 case XML_sngStrike
: return awt::FontStrikeout::SINGLE
;
149 return awt::FontStrikeout::DONTKNOW
;
152 sal_Int16
GetCaseMap( sal_Int32 nToken
)
156 case XML_all
: return CaseMap::UPPERCASE
;
157 case XML_small
: return CaseMap::SMALLCAPS
;
159 return CaseMap::NONE
;
162 /** converts a paragraph align to a ParaAdjust */
163 ParagraphAdjust
GetParaAdjust( sal_Int32 nAlign
)
165 OSL_ASSERT((nAlign
& sal_Int32(0xFFFF0000))==0);
166 ParagraphAdjust nEnum
;
170 nEnum
= ParagraphAdjust_CENTER
;
174 nEnum
= ParagraphAdjust_BLOCK
;
177 nEnum
= ParagraphAdjust_RIGHT
;
181 nEnum
= ParagraphAdjust_STRETCH
;
185 nEnum
= ParagraphAdjust_LEFT
;
191 TextVerticalAdjust
GetTextVerticalAdjust( sal_Int32 nToken
)
193 TextVerticalAdjust aVertAdjust
;
197 aVertAdjust
= TextVerticalAdjust_BOTTOM
;
202 aVertAdjust
= TextVerticalAdjust_CENTER
;
206 aVertAdjust
= TextVerticalAdjust_TOP
;
212 const char* GetTextVerticalAdjust( TextVerticalAdjust eAdjust
)
214 const char* sVerticalAdjust
= nullptr;
217 case TextVerticalAdjust_BOTTOM
:
218 sVerticalAdjust
= "b";
220 case TextVerticalAdjust_CENTER
:
221 sVerticalAdjust
= "ctr";
223 case TextVerticalAdjust_TOP
:
225 sVerticalAdjust
= "t";
228 return sVerticalAdjust
;
231 TabAlign
GetTabAlign( sal_Int32 aToken
)
233 OSL_ASSERT((aToken
& sal_Int32(0xFFFF0000))==0);
238 nEnum
= TabAlign_CENTER
;
241 nEnum
= TabAlign_DECIMAL
;
244 nEnum
= TabAlign_LEFT
;
247 nEnum
= TabAlign_RIGHT
;
250 nEnum
= TabAlign_DEFAULT
;
256 const char* GetHatchPattern( const drawing::Hatch
& rHatch
)
258 const char* sPattern
= nullptr;
259 const sal_Int32 nAngle
= rHatch
.Angle
> 1800 ? rHatch
.Angle
- 1800 : rHatch
.Angle
;
260 // Angle ~ 0° (horizontal)
261 if( (nAngle
>= 0 && nAngle
< 225) || nAngle
>= 1575 )
263 switch( rHatch
.Style
)
265 case drawing::HatchStyle_SINGLE
:
267 if( rHatch
.Distance
< 75 )
274 case drawing::HatchStyle_DOUBLE
:
275 case drawing::HatchStyle_TRIPLE
:
277 if( rHatch
.Distance
< 75 )
287 // Angle ~ 45° (upward diagonal)
288 else if( nAngle
< 675 )
290 switch( rHatch
.Style
)
292 case drawing::HatchStyle_SINGLE
:
294 if( rHatch
.Distance
< 75 )
295 sPattern
= "ltUpDiag";
297 sPattern
= "wdUpDiag";
301 case drawing::HatchStyle_DOUBLE
:
302 case drawing::HatchStyle_TRIPLE
:
304 if( rHatch
.Distance
< 75 )
305 sPattern
= "smCheck";
307 sPattern
= "openDmnd";
314 // Angle ~ 90° (vertical)
315 else if( nAngle
< 1125 )
317 switch( rHatch
.Style
)
319 case drawing::HatchStyle_SINGLE
:
321 // dkVert is imported as Distance = 25, ltVert as Distance = 50, export them accordingly.
322 if( rHatch
.Distance
< 50 )
324 else if( rHatch
.Distance
< 75 )
331 case drawing::HatchStyle_DOUBLE
:
332 case drawing::HatchStyle_TRIPLE
:
334 if( rHatch
.Distance
< 75 )
344 // Angle ~ 135° (downward diagonal)
345 else if( nAngle
< 1575 )
347 switch( rHatch
.Style
)
349 case drawing::HatchStyle_SINGLE
:
351 if( rHatch
.Distance
< 75 )
352 sPattern
= "ltDnDiag";
354 sPattern
= "wdDnDiag";
358 case drawing::HatchStyle_DOUBLE
:
359 case drawing::HatchStyle_TRIPLE
:
361 if( rHatch
.Distance
< 75 )
362 sPattern
= "smCheck";
364 sPattern
= "openDmnd";
374 /** converts the attributes from a CT_RelativeRect to an IntegerRectangle2D */
375 IntegerRectangle2D
GetRelativeRect( const Reference
< XFastAttributeList
>& xAttribs
)
377 IntegerRectangle2D r
;
379 r
.X1
= xAttribs
->getOptionalValue( XML_l
).toInt32();
380 r
.Y1
= xAttribs
->getOptionalValue( XML_t
).toInt32();
381 r
.X2
= xAttribs
->getOptionalValue( XML_r
).toInt32();
382 r
.Y2
= xAttribs
->getOptionalValue( XML_b
).toInt32();
387 /** converts the attributes from a CT_Size2D into an awt Size with 1/100thmm */
388 awt::Size
GetSize2D( const Reference
< XFastAttributeList
>& xAttribs
)
390 return awt::Size( GetCoordinate( xAttribs
->getOptionalValue( XML_cx
) ), GetCoordinate( xAttribs
->getOptionalValue( XML_cy
) ) );
393 IndexRange
GetIndexRange( const Reference
< XFastAttributeList
>& xAttributes
)
396 range
.start
= xAttributes
->getOptionalValue( XML_st
).toInt32();
397 range
.end
= xAttributes
->getOptionalValue( XML_end
).toInt32();
401 } // namespace drawingml
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */