Correct parsing of ST_Percentage
[LibreOffice.git] / oox / source / drawingml / drawingmltypes.cxx
blobfc2f28d902b77a6f25a6e5b371f824455c855903
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 .
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 )
50 sal_Int32 nRet = 0;
51 if( !::sax::Converter::convertNumber( nRet, sValue ) )
52 nRet = 0;
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 )
65 sal_Int32 nRet = 0;
66 if( !::sax::Converter::convertNumber( nRet, sValue ) )
67 nRet = 0;
69 return nRet;
72 double GetPositiveFixedPercentage( const OUString& sValue )
74 double fPercent = sValue.toFloat() / 100000.;
75 return fPercent;
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 )
87 float fRet = 0;
88 sal_Int32 nRet;
89 if( ::sax::Converter::convertNumber( nRet, sValue ) )
90 fRet = static_cast< float >( static_cast< double >( nRet ) / 100.0 );
91 return fRet;
94 /** converts the ST_TextSpacingPoint to 1/100mm */
95 sal_Int32 GetTextSpacingPoint( std::u16string_view sValue )
97 sal_Int32 nRet;
98 if( ::sax::Converter::convertNumber( nRet, sValue, (SAL_MIN_INT32 + 360) / 254, (SAL_MAX_INT32 - 360) / 254 ) )
99 nRet = GetTextSpacingPoint( nRet );
100 return nRet;
103 sal_Int32 GetTextSpacingPoint(sal_Int32 nValue)
105 if (nValue > 0)
106 nValue = (nValue * 254 + 360);
107 else if (nValue < 0)
108 nValue = (nValue * 254 - 360);
109 return nValue / 720;
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);
121 switch( nToken )
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);
148 switch( nToken )
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 )
159 switch( 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;
172 switch( nAlign )
174 case XML_ctr:
175 nEnum = ParagraphAdjust_CENTER;
176 break;
177 case XML_just:
178 case XML_justLow:
179 nEnum = ParagraphAdjust_BLOCK;
180 break;
181 case XML_r:
182 nEnum = ParagraphAdjust_RIGHT;
183 break;
184 case XML_thaiDist:
185 case XML_dist:
186 nEnum = ParagraphAdjust_STRETCH;
187 break;
188 case XML_l:
189 default:
190 nEnum = ParagraphAdjust_LEFT;
191 break;
193 return nEnum;
196 TextVerticalAdjust GetTextVerticalAdjust( sal_Int32 nToken )
198 TextVerticalAdjust aVertAdjust;
199 switch( nToken )
201 case XML_b:
202 aVertAdjust = TextVerticalAdjust_BOTTOM;
203 break;
204 case XML_dist:
205 case XML_just:
206 case XML_ctr:
207 aVertAdjust = TextVerticalAdjust_CENTER;
208 break;
209 case XML_t:
210 default:
211 aVertAdjust = TextVerticalAdjust_TOP;
212 break;
214 return aVertAdjust;
217 const char* GetTextVerticalAdjust( TextVerticalAdjust eAdjust )
219 const char* sVerticalAdjust = nullptr;
220 switch( eAdjust )
222 case TextVerticalAdjust_BOTTOM:
223 sVerticalAdjust = "b";
224 break;
225 case TextVerticalAdjust_CENTER:
226 sVerticalAdjust = "ctr";
227 break;
228 case TextVerticalAdjust_TOP:
229 default:
230 sVerticalAdjust = "t";
231 break;
233 return sVerticalAdjust;
236 TabAlign GetTabAlign( sal_Int32 aToken )
238 OSL_ASSERT((aToken & sal_Int32(0xFFFF0000))==0);
239 TabAlign nEnum;
240 switch( aToken )
242 case XML_ctr:
243 nEnum = TabAlign_CENTER;
244 break;
245 case XML_dec:
246 nEnum = TabAlign_DECIMAL;
247 break;
248 case XML_l:
249 nEnum = TabAlign_LEFT;
250 break;
251 case XML_r:
252 nEnum = TabAlign_RIGHT;
253 break;
254 default:
255 nEnum = TabAlign_DEFAULT;
256 break;
258 return nEnum;
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 )
273 sPattern = "ltHorz";
274 else
275 sPattern = "horz";
277 break;
279 case drawing::HatchStyle_DOUBLE:
280 case drawing::HatchStyle_TRIPLE:
282 if( rHatch.Distance < 75 )
283 sPattern = "smGrid";
284 else
285 sPattern = "lgGrid";
287 break;
289 default: break;
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";
301 else
302 sPattern = "wdUpDiag";
304 break;
306 case drawing::HatchStyle_DOUBLE:
307 case drawing::HatchStyle_TRIPLE:
309 if( rHatch.Distance < 75 )
310 sPattern = "smCheck";
311 else
312 sPattern = "openDmnd";
314 break;
316 default: break;
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 )
328 sPattern = "dkVert";
329 else if( rHatch.Distance < 75 )
330 sPattern = "ltVert";
331 else
332 sPattern = "vert";
334 break;
336 case drawing::HatchStyle_DOUBLE:
337 case drawing::HatchStyle_TRIPLE:
339 if( rHatch.Distance < 75 )
340 sPattern = "smGrid";
341 else
342 sPattern = "lgGrid";
344 break;
346 default: break;
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";
358 else
359 sPattern = "wdDnDiag";
361 break;
363 case drawing::HatchStyle_DOUBLE:
364 case drawing::HatchStyle_TRIPLE:
366 if( rHatch.Distance < 75 )
367 sPattern = "smCheck";
368 else
369 sPattern = "openDmnd";
371 break;
373 default: break;
376 return sPattern;
379 namespace
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 ));
400 return r;
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 )
411 IndexRange range;
412 range.start = xAttributes->getOptionalValue( XML_st ).toInt32();
413 range.end = xAttributes->getOptionalValue( XML_end ).toInt32();
414 return range;
417 } // namespace oox::drawingml
419 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */