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>
29 #include <osl/diagnose.h>
30 #include <sax/tools/converter.hxx>
31 #include <oox/token/tokens.hxx>
33 using ::com::sun::star::uno::Reference
;
34 using ::com::sun::star::xml::sax::XFastAttributeList
;
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::drawing
;
37 using namespace ::com::sun::star::geometry
;
38 using namespace ::com::sun::star::style
;
40 namespace oox::drawingml
{
42 /** converts EMUs into 1/100th mmm */
43 sal_Int32
GetCoordinate( sal_Int32 nValue
)
45 return o3tl::convert(nValue
, o3tl::Length::emu
, o3tl::Length::mm100
);
48 /** converts an emu string into 1/100th mmm */
49 sal_Int32
GetCoordinate( std::u16string_view sValue
)
52 if( !::sax::Converter::convertNumber( nRet
, sValue
) )
54 return GetCoordinate( nRet
);
57 /** converts 1/100mm to EMU */
58 sal_Int32
GetPointFromCoordinate( sal_Int32 nValue
)
60 return o3tl::convert(nValue
, o3tl::Length::mm100
, o3tl::Length::emu
);
63 /** converts a ST_Percentage % string into 1/1000th of % */
64 sal_Int32
GetPercent( std::u16string_view sValue
)
67 if( !::sax::Converter::convertNumber( nRet
, sValue
) )
73 double GetPositiveFixedPercentage( const OUString
& sValue
)
75 double fPercent
= sValue
.toFloat() / 100000.;
79 /** converts the attributes from a CT_TLPoint into an awt Point with 1/1000% */
80 awt::Point
GetPointPercent( const Reference
< XFastAttributeList
>& xAttribs
)
82 return awt::Point(GetPercent(xAttribs
->getOptionalValue(XML_x
)), GetPercent(xAttribs
->getOptionalValue(XML_y
)));
85 /** converts the ST_TextFontSize to point */
86 float GetTextSize( std::u16string_view sValue
)
90 if( ::sax::Converter::convertNumber( nRet
, sValue
) )
91 fRet
= static_cast< float >( static_cast< double >( nRet
) / 100.0 );
95 /** converts the ST_TextSpacingPoint to 1/100mm */
96 sal_Int32
GetTextSpacingPoint( std::u16string_view sValue
)
99 if( ::sax::Converter::convertNumber( nRet
, sValue
, (SAL_MIN_INT32
+ 360) / 254, (SAL_MAX_INT32
- 360) / 254 ) )
100 nRet
= GetTextSpacingPoint( nRet
);
104 sal_Int32
GetTextSpacingPoint(sal_Int32 nValue
)
107 nValue
= (nValue
* 254 + 360);
109 nValue
= (nValue
* 254 - 360);
113 float GetFontHeight( sal_Int32 nHeight
)
115 // convert 1/100 points to points
116 return static_cast< float >( nHeight
/ 100.0 );
119 sal_Int16
GetFontUnderline( sal_Int32 nToken
)
121 OSL_ASSERT((nToken
& sal_Int32(0xFFFF0000))==0);
124 case XML_none
: return awt::FontUnderline::NONE
;
125 case XML_dash
: return awt::FontUnderline::DASH
;
126 case XML_dashHeavy
: return awt::FontUnderline::BOLDDASH
;
127 case XML_dashLong
: return awt::FontUnderline::LONGDASH
;
128 case XML_dashLongHeavy
: return awt::FontUnderline::BOLDLONGDASH
;
129 case XML_dbl
: return awt::FontUnderline::DOUBLE
;
130 case XML_dotDash
: return awt::FontUnderline::DASHDOT
;
131 case XML_dotDashHeavy
: return awt::FontUnderline::BOLDDASHDOT
;
132 case XML_dotDotDash
: return awt::FontUnderline::DASHDOTDOT
;
133 case XML_dotDotDashHeavy
: return awt::FontUnderline::BOLDDASHDOTDOT
;
134 case XML_dotted
: return awt::FontUnderline::DOTTED
;
135 case XML_dottedHeavy
: return awt::FontUnderline::BOLDDOTTED
;
136 case XML_heavy
: return awt::FontUnderline::BOLD
;
137 case XML_sng
: return awt::FontUnderline::SINGLE
;
138 case XML_wavy
: return awt::FontUnderline::WAVE
;
139 case XML_wavyDbl
: return awt::FontUnderline::DOUBLEWAVE
;
140 case XML_wavyHeavy
: return awt::FontUnderline::BOLDWAVE
;
141 // case XML_words: // TODO
143 return awt::FontUnderline::DONTKNOW
;
146 sal_Int16
GetFontStrikeout( sal_Int32 nToken
)
148 OSL_ASSERT((nToken
& sal_Int32(0xFFFF0000))==0);
151 case XML_dblStrike
: return awt::FontStrikeout::DOUBLE
;
152 case XML_noStrike
: return awt::FontStrikeout::NONE
;
153 case XML_sngStrike
: return awt::FontStrikeout::SINGLE
;
155 return awt::FontStrikeout::DONTKNOW
;
158 sal_Int16
GetCaseMap( sal_Int32 nToken
)
162 case XML_all
: return CaseMap::UPPERCASE
;
163 case XML_small
: return CaseMap::SMALLCAPS
;
165 return CaseMap::NONE
;
168 /** converts a paragraph align to a ParaAdjust */
169 ParagraphAdjust
GetParaAdjust( sal_Int32 nAlign
)
171 OSL_ASSERT((nAlign
& sal_Int32(0xFFFF0000))==0);
172 ParagraphAdjust nEnum
;
176 nEnum
= ParagraphAdjust_CENTER
;
180 nEnum
= ParagraphAdjust_BLOCK
;
183 nEnum
= ParagraphAdjust_RIGHT
;
187 nEnum
= ParagraphAdjust_STRETCH
;
191 nEnum
= ParagraphAdjust_LEFT
;
197 TextVerticalAdjust
GetTextVerticalAdjust( sal_Int32 nToken
)
199 TextVerticalAdjust aVertAdjust
;
203 aVertAdjust
= TextVerticalAdjust_BOTTOM
;
208 aVertAdjust
= TextVerticalAdjust_CENTER
;
212 aVertAdjust
= TextVerticalAdjust_TOP
;
218 const char* GetTextVerticalAdjust( TextVerticalAdjust eAdjust
)
220 const char* sVerticalAdjust
= nullptr;
223 case TextVerticalAdjust_BOTTOM
:
224 sVerticalAdjust
= "b";
226 case TextVerticalAdjust_CENTER
:
227 sVerticalAdjust
= "ctr";
229 case TextVerticalAdjust_TOP
:
231 sVerticalAdjust
= "t";
234 return sVerticalAdjust
;
237 TabAlign
GetTabAlign( sal_Int32 aToken
)
239 OSL_ASSERT((aToken
& sal_Int32(0xFFFF0000))==0);
244 nEnum
= TabAlign_CENTER
;
247 nEnum
= TabAlign_DECIMAL
;
250 nEnum
= TabAlign_LEFT
;
253 nEnum
= TabAlign_RIGHT
;
256 nEnum
= TabAlign_DEFAULT
;
262 const char* GetHatchPattern( const drawing::Hatch
& rHatch
)
264 const char* sPattern
= nullptr;
265 const sal_Int32 nAngle
= rHatch
.Angle
> 1800 ? rHatch
.Angle
- 1800 : rHatch
.Angle
;
266 // Angle ~ 0° (horizontal)
267 if( (nAngle
>= 0 && nAngle
< 225) || nAngle
>= 1575 )
269 switch( rHatch
.Style
)
271 case drawing::HatchStyle_SINGLE
:
273 if( rHatch
.Distance
< 75 )
280 case drawing::HatchStyle_DOUBLE
:
281 case drawing::HatchStyle_TRIPLE
:
283 if( rHatch
.Distance
< 75 )
293 // Angle ~ 45° (upward diagonal)
294 else if( nAngle
< 675 )
296 switch( rHatch
.Style
)
298 case drawing::HatchStyle_SINGLE
:
300 if( rHatch
.Distance
< 75 )
301 sPattern
= "ltUpDiag";
303 sPattern
= "wdUpDiag";
307 case drawing::HatchStyle_DOUBLE
:
308 case drawing::HatchStyle_TRIPLE
:
310 if( rHatch
.Distance
< 75 )
311 sPattern
= "smCheck";
313 sPattern
= "openDmnd";
320 // Angle ~ 90° (vertical)
321 else if( nAngle
< 1125 )
323 switch( rHatch
.Style
)
325 case drawing::HatchStyle_SINGLE
:
327 // dkVert is imported as Distance = 25, ltVert as Distance = 50, export them accordingly.
328 if( rHatch
.Distance
< 50 )
330 else if( rHatch
.Distance
< 75 )
337 case drawing::HatchStyle_DOUBLE
:
338 case drawing::HatchStyle_TRIPLE
:
340 if( rHatch
.Distance
< 75 )
350 // Angle ~ 135° (downward diagonal)
351 else if( nAngle
< 1575 )
353 switch( rHatch
.Style
)
355 case drawing::HatchStyle_SINGLE
:
357 if( rHatch
.Distance
< 75 )
358 sPattern
= "ltDnDiag";
360 sPattern
= "wdDnDiag";
364 case drawing::HatchStyle_DOUBLE
:
365 case drawing::HatchStyle_TRIPLE
:
367 if( rHatch
.Distance
< 75 )
368 sPattern
= "smCheck";
370 sPattern
= "openDmnd";
380 std::optional
<OString
> GetTextVerticalType(sal_Int32 nRotateAngle
)
382 switch (nRotateAngle
)
395 // ISO/IEC-29500 Part 1 ST_Percentage, and [MS-OI29500] 2.1.1324
396 sal_Int32
GetST_Percentage(std::u16string_view s
)
398 if (o3tl::ends_with(s
, u
"%"))
399 return std::round(o3tl::toDouble(s
) * 1000);
400 return o3tl::toInt32(s
);
404 /** converts the attributes from a CT_RelativeRect to an IntegerRectangle2D */
405 IntegerRectangle2D
GetRelativeRect( const Reference
< XFastAttributeList
>& xAttribs
)
407 IntegerRectangle2D r
;
409 r
.X1
= GetST_Percentage(xAttribs
->getOptionalValue( XML_l
));
410 r
.Y1
= GetST_Percentage(xAttribs
->getOptionalValue( XML_t
));
411 r
.X2
= GetST_Percentage(xAttribs
->getOptionalValue( XML_r
));
412 r
.Y2
= GetST_Percentage(xAttribs
->getOptionalValue( XML_b
));
417 void fillRelativeRectangle(model::RelativeRectangle
& rRelativeRectangle
, const Reference
<XFastAttributeList
>& xAttribs
)
419 rRelativeRectangle
.mnLeft
= GetST_Percentage(xAttribs
->getOptionalValue(XML_l
));
420 rRelativeRectangle
.mnTop
= GetST_Percentage(xAttribs
->getOptionalValue(XML_t
));
421 rRelativeRectangle
.mnRight
= GetST_Percentage(xAttribs
->getOptionalValue(XML_r
));
422 rRelativeRectangle
.mnBottom
= GetST_Percentage(xAttribs
->getOptionalValue(XML_b
));
425 /** converts the attributes from a CT_Size2D into an awt Size with 1/100thmm */
426 awt::Size
GetSize2D( const Reference
< XFastAttributeList
>& xAttribs
)
428 return awt::Size( GetCoordinate( xAttribs
->getOptionalValue( XML_cx
) ), GetCoordinate( xAttribs
->getOptionalValue( XML_cy
) ) );
431 IndexRange
GetIndexRange( const Reference
< XFastAttributeList
>& xAttributes
)
434 range
.start
= xAttributes
->getOptionalValue( XML_st
).toInt32();
435 range
.end
= xAttributes
->getOptionalValue( XML_end
).toInt32();
440 model::RectangleAlignment
convertToRectangleAlignment(sal_Int32 nToken
)
444 case XML_tl
: return model::RectangleAlignment::TopLeft
;
445 case XML_t
: return model::RectangleAlignment::Top
;
446 case XML_tr
: return model::RectangleAlignment::TopRight
;
447 case XML_l
: return model::RectangleAlignment::Left
;
448 case XML_ctr
: return model::RectangleAlignment::Center
;
449 case XML_r
: return model::RectangleAlignment::Right
;
450 case XML_bl
: return model::RectangleAlignment::BottomLeft
;
451 case XML_b
: return model::RectangleAlignment::Bottom
;
452 case XML_br
: return model::RectangleAlignment::BottomRight
;
456 return model::RectangleAlignment::Unset
;
459 } // namespace oox::drawingml
461 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */