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>
27 #include <o3tl/string_view.hxx>
28 #include <osl/diagnose.h>
29 #include <sax/tools/converter.hxx>
30 #include <oox/token/tokens.hxx>
32 using ::com::sun::star::uno::Reference
;
33 using ::com::sun::star::xml::sax::XFastAttributeList
;
34 using namespace ::com::sun::star
;
35 using namespace ::com::sun::star::drawing
;
36 using namespace ::com::sun::star::geometry
;
37 using namespace ::com::sun::star::style
;
39 namespace oox::drawingml
{
41 /** converts EMUs into 1/100th mmm */
42 sal_Int32
GetCoordinate( sal_Int32 nValue
)
44 return o3tl::convert(nValue
, o3tl::Length::emu
, o3tl::Length::mm100
);
47 /** converts an emu string into 1/100th mmm */
48 sal_Int32
GetCoordinate( std::u16string_view sValue
)
51 if( !::sax::Converter::convertNumber( nRet
, sValue
) )
53 return GetCoordinate( nRet
);
56 /** converts 1/100mm to EMU */
57 sal_Int32
GetPointFromCoordinate( sal_Int32 nValue
)
59 return o3tl::convert(nValue
, o3tl::Length::mm100
, o3tl::Length::emu
);
62 /** converts a ST_Percentage % string into 1/1000th of % */
63 sal_Int32
GetPercent( std::u16string_view sValue
)
66 if( !::sax::Converter::convertNumber( nRet
, sValue
) )
72 double GetPositiveFixedPercentage( const OUString
& sValue
)
74 double fPercent
= sValue
.toFloat() / 100000.;
78 /** converts the attributes from a CT_TLPoint into an awt Point with 1/1000% */
79 awt::Point
GetPointPercent( const Reference
< XFastAttributeList
>& xAttribs
)
81 return awt::Point(GetPercent(xAttribs
->getOptionalValue(XML_x
)), GetPercent(xAttribs
->getOptionalValue(XML_y
)));
84 /** converts the ST_TextFontSize to point */
85 float GetTextSize( std::u16string_view sValue
)
89 if( ::sax::Converter::convertNumber( nRet
, sValue
) )
90 fRet
= static_cast< float >( static_cast< double >( nRet
) / 100.0 );
94 /** converts the ST_TextSpacingPoint to 1/100mm */
95 sal_Int32
GetTextSpacingPoint( std::u16string_view sValue
)
98 if( ::sax::Converter::convertNumber( nRet
, sValue
, (SAL_MIN_INT32
+ 360) / 254, (SAL_MAX_INT32
- 360) / 254 ) )
99 nRet
= GetTextSpacingPoint( nRet
);
103 sal_Int32
GetTextSpacingPoint(sal_Int32 nValue
)
106 nValue
= (nValue
* 254 + 360);
108 nValue
= (nValue
* 254 - 360);
112 float GetFontHeight( sal_Int32 nHeight
)
114 // convert 1/100 points to points
115 return static_cast< float >( nHeight
/ 100.0 );
118 sal_Int16
GetFontUnderline( sal_Int32 nToken
)
120 OSL_ASSERT((nToken
& sal_Int32(0xFFFF0000))==0);
123 case XML_none
: return awt::FontUnderline::NONE
;
124 case XML_dash
: return awt::FontUnderline::DASH
;
125 case XML_dashHeavy
: return awt::FontUnderline::BOLDDASH
;
126 case XML_dashLong
: return awt::FontUnderline::LONGDASH
;
127 case XML_dashLongHeavy
: return awt::FontUnderline::BOLDLONGDASH
;
128 case XML_dbl
: return awt::FontUnderline::DOUBLE
;
129 case XML_dotDash
: return awt::FontUnderline::DASHDOT
;
130 case XML_dotDashHeavy
: return awt::FontUnderline::BOLDDASHDOT
;
131 case XML_dotDotDash
: return awt::FontUnderline::DASHDOTDOT
;
132 case XML_dotDotDashHeavy
: return awt::FontUnderline::BOLDDASHDOTDOT
;
133 case XML_dotted
: return awt::FontUnderline::DOTTED
;
134 case XML_dottedHeavy
: return awt::FontUnderline::BOLDDOTTED
;
135 case XML_heavy
: return awt::FontUnderline::BOLD
;
136 case XML_sng
: return awt::FontUnderline::SINGLE
;
137 case XML_wavy
: return awt::FontUnderline::WAVE
;
138 case XML_wavyDbl
: return awt::FontUnderline::DOUBLEWAVE
;
139 case XML_wavyHeavy
: return awt::FontUnderline::BOLDWAVE
;
140 // case XML_words: // TODO
142 return awt::FontUnderline::DONTKNOW
;
145 sal_Int16
GetFontStrikeout( sal_Int32 nToken
)
147 OSL_ASSERT((nToken
& sal_Int32(0xFFFF0000))==0);
150 case XML_dblStrike
: return awt::FontStrikeout::DOUBLE
;
151 case XML_noStrike
: return awt::FontStrikeout::NONE
;
152 case XML_sngStrike
: return awt::FontStrikeout::SINGLE
;
154 return awt::FontStrikeout::DONTKNOW
;
157 sal_Int16
GetCaseMap( sal_Int32 nToken
)
161 case XML_all
: return CaseMap::UPPERCASE
;
162 case XML_small
: return CaseMap::SMALLCAPS
;
164 return CaseMap::NONE
;
167 /** converts a paragraph align to a ParaAdjust */
168 ParagraphAdjust
GetParaAdjust( sal_Int32 nAlign
)
170 OSL_ASSERT((nAlign
& sal_Int32(0xFFFF0000))==0);
171 ParagraphAdjust nEnum
;
175 nEnum
= ParagraphAdjust_CENTER
;
179 nEnum
= ParagraphAdjust_BLOCK
;
182 nEnum
= ParagraphAdjust_RIGHT
;
186 nEnum
= ParagraphAdjust_STRETCH
;
190 nEnum
= ParagraphAdjust_LEFT
;
196 TextVerticalAdjust
GetTextVerticalAdjust( sal_Int32 nToken
)
198 TextVerticalAdjust aVertAdjust
;
202 aVertAdjust
= TextVerticalAdjust_BOTTOM
;
207 aVertAdjust
= TextVerticalAdjust_CENTER
;
211 aVertAdjust
= TextVerticalAdjust_TOP
;
217 const char* GetTextVerticalAdjust( TextVerticalAdjust eAdjust
)
219 const char* sVerticalAdjust
= nullptr;
222 case TextVerticalAdjust_BOTTOM
:
223 sVerticalAdjust
= "b";
225 case TextVerticalAdjust_CENTER
:
226 sVerticalAdjust
= "ctr";
228 case TextVerticalAdjust_TOP
:
230 sVerticalAdjust
= "t";
233 return sVerticalAdjust
;
236 TabAlign
GetTabAlign( sal_Int32 aToken
)
238 OSL_ASSERT((aToken
& sal_Int32(0xFFFF0000))==0);
243 nEnum
= TabAlign_CENTER
;
246 nEnum
= TabAlign_DECIMAL
;
249 nEnum
= TabAlign_LEFT
;
252 nEnum
= TabAlign_RIGHT
;
255 nEnum
= TabAlign_DEFAULT
;
261 const char* GetHatchPattern( const drawing::Hatch
& rHatch
)
263 const char* sPattern
= nullptr;
264 const sal_Int32 nAngle
= rHatch
.Angle
> 1800 ? rHatch
.Angle
- 1800 : rHatch
.Angle
;
265 // Angle ~ 0° (horizontal)
266 if( (nAngle
>= 0 && nAngle
< 225) || nAngle
>= 1575 )
268 switch( rHatch
.Style
)
270 case drawing::HatchStyle_SINGLE
:
272 if( rHatch
.Distance
< 75 )
279 case drawing::HatchStyle_DOUBLE
:
280 case drawing::HatchStyle_TRIPLE
:
282 if( rHatch
.Distance
< 75 )
292 // Angle ~ 45° (upward diagonal)
293 else if( nAngle
< 675 )
295 switch( rHatch
.Style
)
297 case drawing::HatchStyle_SINGLE
:
299 if( rHatch
.Distance
< 75 )
300 sPattern
= "ltUpDiag";
302 sPattern
= "wdUpDiag";
306 case drawing::HatchStyle_DOUBLE
:
307 case drawing::HatchStyle_TRIPLE
:
309 if( rHatch
.Distance
< 75 )
310 sPattern
= "smCheck";
312 sPattern
= "openDmnd";
319 // Angle ~ 90° (vertical)
320 else if( nAngle
< 1125 )
322 switch( rHatch
.Style
)
324 case drawing::HatchStyle_SINGLE
:
326 // dkVert is imported as Distance = 25, ltVert as Distance = 50, export them accordingly.
327 if( rHatch
.Distance
< 50 )
329 else if( rHatch
.Distance
< 75 )
336 case drawing::HatchStyle_DOUBLE
:
337 case drawing::HatchStyle_TRIPLE
:
339 if( rHatch
.Distance
< 75 )
349 // Angle ~ 135° (downward diagonal)
350 else if( nAngle
< 1575 )
352 switch( rHatch
.Style
)
354 case drawing::HatchStyle_SINGLE
:
356 if( rHatch
.Distance
< 75 )
357 sPattern
= "ltDnDiag";
359 sPattern
= "wdDnDiag";
363 case drawing::HatchStyle_DOUBLE
:
364 case drawing::HatchStyle_TRIPLE
:
366 if( rHatch
.Distance
< 75 )
367 sPattern
= "smCheck";
369 sPattern
= "openDmnd";
381 // ISO/IEC-29500 Part 1 ST_Percentage, and [MS-OI29500] 2.1.1324
382 sal_Int32
GetST_Percentage(std::u16string_view s
)
384 if (o3tl::ends_with(s
, u
"%"))
385 return std::round(o3tl::toDouble(s
) * 1000);
386 return o3tl::toInt32(s
);
390 /** converts the attributes from a CT_RelativeRect to an IntegerRectangle2D */
391 IntegerRectangle2D
GetRelativeRect( const Reference
< XFastAttributeList
>& xAttribs
)
393 IntegerRectangle2D r
;
395 r
.X1
= GetST_Percentage(xAttribs
->getOptionalValue( XML_l
));
396 r
.Y1
= GetST_Percentage(xAttribs
->getOptionalValue( XML_t
));
397 r
.X2
= GetST_Percentage(xAttribs
->getOptionalValue( XML_r
));
398 r
.Y2
= GetST_Percentage(xAttribs
->getOptionalValue( XML_b
));
403 /** converts the attributes from a CT_Size2D into an awt Size with 1/100thmm */
404 awt::Size
GetSize2D( const Reference
< XFastAttributeList
>& xAttribs
)
406 return awt::Size( GetCoordinate( xAttribs
->getOptionalValue( XML_cx
) ), GetCoordinate( xAttribs
->getOptionalValue( XML_cy
) ) );
409 IndexRange
GetIndexRange( const Reference
< XFastAttributeList
>& xAttributes
)
412 range
.start
= xAttributes
->getOptionalValue( XML_st
).toInt32();
413 range
.end
= xAttributes
->getOptionalValue( XML_end
).toInt32();
417 } // namespace oox::drawingml
419 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */