LibreOffice Module oox (master) 1
drawingmltypes.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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
27#include <o3tl/string_view.hxx>
28#include <optional>
29#include <osl/diagnose.h>
31#include <oox/token/tokens.hxx>
32
33using ::com::sun::star::uno::Reference;
34using ::com::sun::star::xml::sax::XFastAttributeList;
35using namespace ::com::sun::star;
36using namespace ::com::sun::star::drawing;
37using namespace ::com::sun::star::geometry;
38using namespace ::com::sun::star::style;
39
40namespace oox::drawingml {
41
43sal_Int32 GetCoordinate( sal_Int32 nValue )
44{
46}
47
49sal_Int32 GetCoordinate( std::u16string_view sValue )
50{
51 sal_Int32 nRet = 0;
52 if( !::sax::Converter::convertNumber( nRet, sValue ) )
53 nRet = 0;
54 return GetCoordinate( nRet );
55}
56
58sal_Int32 GetPointFromCoordinate( sal_Int32 nValue )
59{
61}
62
64sal_Int32 GetPercent( std::u16string_view sValue )
65{
66 sal_Int32 nRet = 0;
67 if( !::sax::Converter::convertNumber( nRet, sValue ) )
68 nRet = 0;
69
70 return nRet;
71}
72
73double GetPositiveFixedPercentage( const OUString& sValue )
74{
75 double fPercent = sValue.toFloat() / 100000.;
76 return fPercent;
77}
78
81{
82 return awt::Point(GetPercent(xAttribs->getOptionalValue(XML_x)), GetPercent(xAttribs->getOptionalValue(XML_y)));
83}
84
86float GetTextSize( std::u16string_view sValue )
87{
88 float fRet = 0;
89 sal_Int32 nRet;
90 if( ::sax::Converter::convertNumber( nRet, sValue ) )
91 fRet = static_cast< float >( static_cast< double >( nRet ) / 100.0 );
92 return fRet;
93}
94
96sal_Int32 GetTextSpacingPoint( std::u16string_view sValue )
97{
98 sal_Int32 nRet;
99 if( ::sax::Converter::convertNumber( nRet, sValue, (SAL_MIN_INT32 + 360) / 254, (SAL_MAX_INT32 - 360) / 254 ) )
100 nRet = GetTextSpacingPoint( nRet );
101 return nRet;
102}
103
104sal_Int32 GetTextSpacingPoint(sal_Int32 nValue)
105{
106 if (nValue > 0)
107 nValue = (nValue * 254 + 360);
108 else if (nValue < 0)
109 nValue = (nValue * 254 - 360);
110 return nValue / 720;
111}
112
113float GetFontHeight( sal_Int32 nHeight )
114{
115 // convert 1/100 points to points
116 return static_cast< float >( nHeight / 100.0 );
117}
118
119sal_Int16 GetFontUnderline( sal_Int32 nToken )
120{
121 OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
122 switch( nToken )
123 {
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
142 }
143 return awt::FontUnderline::DONTKNOW;
144}
145
146sal_Int16 GetFontStrikeout( sal_Int32 nToken )
147{
148 OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
149 switch( nToken )
150 {
151 case XML_dblStrike: return awt::FontStrikeout::DOUBLE;
152 case XML_noStrike: return awt::FontStrikeout::NONE;
153 case XML_sngStrike: return awt::FontStrikeout::SINGLE;
154 }
155 return awt::FontStrikeout::DONTKNOW;
156}
157
158sal_Int16 GetCaseMap( sal_Int32 nToken )
159{
160 switch( nToken )
161 {
162 case XML_all: return CaseMap::UPPERCASE;
163 case XML_small: return CaseMap::SMALLCAPS;
164 }
165 return CaseMap::NONE;
166}
167
169ParagraphAdjust GetParaAdjust( sal_Int32 nAlign )
170{
171 OSL_ASSERT((nAlign & sal_Int32(0xFFFF0000))==0);
172 ParagraphAdjust nEnum;
173 switch( nAlign )
174 {
175 case XML_ctr:
176 nEnum = ParagraphAdjust_CENTER;
177 break;
178 case XML_just:
179 case XML_justLow:
180 nEnum = ParagraphAdjust_BLOCK;
181 break;
182 case XML_r:
183 nEnum = ParagraphAdjust_RIGHT;
184 break;
185 case XML_thaiDist:
186 case XML_dist:
187 nEnum = ParagraphAdjust_STRETCH;
188 break;
189 case XML_l:
190 default:
191 nEnum = ParagraphAdjust_LEFT;
192 break;
193 }
194 return nEnum;
195}
196
197TextVerticalAdjust GetTextVerticalAdjust( sal_Int32 nToken )
198{
199 TextVerticalAdjust aVertAdjust;
200 switch( nToken )
201 {
202 case XML_b:
203 aVertAdjust = TextVerticalAdjust_BOTTOM;
204 break;
205 case XML_dist:
206 case XML_just:
207 case XML_ctr:
208 aVertAdjust = TextVerticalAdjust_CENTER;
209 break;
210 case XML_t:
211 default:
212 aVertAdjust = TextVerticalAdjust_TOP;
213 break;
214 }
215 return aVertAdjust;
216}
217
218const char* GetTextVerticalAdjust( TextVerticalAdjust eAdjust )
219{
220 const char* sVerticalAdjust = nullptr;
221 switch( eAdjust )
222 {
223 case TextVerticalAdjust_BOTTOM:
224 sVerticalAdjust = "b";
225 break;
226 case TextVerticalAdjust_CENTER:
227 sVerticalAdjust = "ctr";
228 break;
229 case TextVerticalAdjust_TOP:
230 default:
231 sVerticalAdjust = "t";
232 break;
233 }
234 return sVerticalAdjust;
235}
236
237TabAlign GetTabAlign( sal_Int32 aToken )
238{
239 OSL_ASSERT((aToken & sal_Int32(0xFFFF0000))==0);
240 TabAlign nEnum;
241 switch( aToken )
242 {
243 case XML_ctr:
244 nEnum = TabAlign_CENTER;
245 break;
246 case XML_dec:
247 nEnum = TabAlign_DECIMAL;
248 break;
249 case XML_l:
250 nEnum = TabAlign_LEFT;
251 break;
252 case XML_r:
253 nEnum = TabAlign_RIGHT;
254 break;
255 default:
256 nEnum = TabAlign_DEFAULT;
257 break;
258 }
259 return nEnum;
260}
261
262const char* GetHatchPattern( const drawing::Hatch& rHatch )
263{
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 )
268 {
269 switch( rHatch.Style )
270 {
271 case drawing::HatchStyle_SINGLE:
272 {
273 if( rHatch.Distance < 75 )
274 sPattern = "ltHorz";
275 else
276 sPattern = "horz";
277
278 break;
279 }
280 case drawing::HatchStyle_DOUBLE:
281 case drawing::HatchStyle_TRIPLE:
282 {
283 if( rHatch.Distance < 75 )
284 sPattern = "smGrid";
285 else
286 sPattern = "lgGrid";
287
288 break;
289 }
290 default: break;
291 }
292 }
293 // Angle ~ 45° (upward diagonal)
294 else if( nAngle < 675 )
295 {
296 switch( rHatch.Style )
297 {
298 case drawing::HatchStyle_SINGLE:
299 {
300 if( rHatch.Distance < 75 )
301 sPattern = "ltUpDiag";
302 else
303 sPattern = "wdUpDiag";
304
305 break;
306 }
307 case drawing::HatchStyle_DOUBLE:
308 case drawing::HatchStyle_TRIPLE:
309 {
310 if( rHatch.Distance < 75 )
311 sPattern = "smCheck";
312 else
313 sPattern = "openDmnd";
314
315 break;
316 }
317 default: break;
318 }
319 }
320 // Angle ~ 90° (vertical)
321 else if( nAngle < 1125 )
322 {
323 switch( rHatch.Style )
324 {
325 case drawing::HatchStyle_SINGLE:
326 {
327 // dkVert is imported as Distance = 25, ltVert as Distance = 50, export them accordingly.
328 if( rHatch.Distance < 50 )
329 sPattern = "dkVert";
330 else if( rHatch.Distance < 75 )
331 sPattern = "ltVert";
332 else
333 sPattern = "vert";
334
335 break;
336 }
337 case drawing::HatchStyle_DOUBLE:
338 case drawing::HatchStyle_TRIPLE:
339 {
340 if( rHatch.Distance < 75 )
341 sPattern = "smGrid";
342 else
343 sPattern = "lgGrid";
344
345 break;
346 }
347 default: break;
348 }
349 }
350 // Angle ~ 135° (downward diagonal)
351 else if( nAngle < 1575 )
352 {
353 switch( rHatch.Style )
354 {
355 case drawing::HatchStyle_SINGLE:
356 {
357 if( rHatch.Distance < 75 )
358 sPattern = "ltDnDiag";
359 else
360 sPattern = "wdDnDiag";
361
362 break;
363 }
364 case drawing::HatchStyle_DOUBLE:
365 case drawing::HatchStyle_TRIPLE:
366 {
367 if( rHatch.Distance < 75 )
368 sPattern = "smCheck";
369 else
370 sPattern = "openDmnd";
371
372 break;
373 }
374 default: break;
375 }
376 }
377 return sPattern;
378}
379
380std::optional<OString> GetTextVerticalType(sal_Int32 nRotateAngle)
381{
382 switch (nRotateAngle)
383 {
384 case 9000:
385 return "vert270";
386 case 27000:
387 return "vert";
388 default:
389 return {};
390 }
391}
392
393namespace
394{
395// ISO/IEC-29500 Part 1 ST_Percentage, and [MS-OI29500] 2.1.1324
396sal_Int32 GetST_Percentage(std::u16string_view s)
397{
398 if (o3tl::ends_with(s, u"%"))
399 return std::round(o3tl::toDouble(s) * 1000);
400 return o3tl::toInt32(s);
401}
402}
403
405IntegerRectangle2D GetRelativeRect( const Reference< XFastAttributeList >& xAttribs )
406{
407 IntegerRectangle2D r;
408
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 ));
413
414 return r;
415}
416
418{
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));
423}
424
426awt::Size GetSize2D( const Reference< XFastAttributeList >& xAttribs )
427{
428 return awt::Size( GetCoordinate( xAttribs->getOptionalValue( XML_cx ) ), GetCoordinate( xAttribs->getOptionalValue( XML_cy ) ) );
429}
430
432{
433 IndexRange range;
434 range.start = xAttributes->getOptionalValue( XML_st ).toInt32();
435 range.end = xAttributes->getOptionalValue( XML_end ).toInt32();
436 return range;
437}
438
439
441{
442 switch (nToken)
443 {
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;
453 default:
454 break;
455 }
457}
458
459} // namespace oox::drawingml
460
461/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool convertNumber(sal_Int32 &rValue, std::u16string_view aString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)
sal_Int16 nValue
RectangleAlignment
constexpr bool ends_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
constexpr Point convert(const Point &rPoint, o3tl::Length eFrom, o3tl::Length eTo)
double toDouble(std::u16string_view str)
IndexRange GetIndexRange(const Reference< XFastAttributeList > &xAttributes)
TextVerticalAdjust GetTextVerticalAdjust(sal_Int32 nToken)
float GetFontHeight(sal_Int32 nHeight)
sal_Int16 GetCaseMap(sal_Int32 nToken)
awt::Point GetPointPercent(const Reference< XFastAttributeList > &xAttribs)
converts the attributes from a CT_TLPoint into an awt Point with 1/1000%
TabAlign GetTabAlign(sal_Int32 aToken)
sal_Int32 GetCoordinate(sal_Int32 nValue)
converts EMUs into 1/100th mmm
sal_Int16 GetFontUnderline(sal_Int32 nToken)
awt::Size GetSize2D(const Reference< XFastAttributeList > &xAttribs)
converts the attributes from a CT_Size2D into an awt Size with 1/100thmm
model::RectangleAlignment convertToRectangleAlignment(sal_Int32 nToken)
std::optional< OString > GetTextVerticalType(sal_Int32 nRotateAngle)
Converts nRotate angle to TextVerticalType string appearing in ooxml.
sal_Int32 GetPointFromCoordinate(sal_Int32 nValue)
converts 1/100mm to EMU
ParagraphAdjust GetParaAdjust(sal_Int32 nAlign)
converts a paragraph align to a ParaAdjust
IntegerRectangle2D GetRelativeRect(const Reference< XFastAttributeList > &xAttribs)
converts the attributes from a CT_RelativeRect to an IntegerRectangle2D
double GetPositiveFixedPercentage(const OUString &sValue)
Converts a ST_PositiveFixedPercentage to a float.
void fillRelativeRectangle(model::RelativeRectangle &rRelativeRectangle, const Reference< XFastAttributeList > &xAttribs)
sal_Int32 GetTextSpacingPoint(std::u16string_view sValue)
converts the ST_TextSpacingPoint to 1/100mm
sal_Int32 GetPercent(std::u16string_view sValue)
converts a ST_Percentage % string into 1/1000th of %
sal_Int16 GetFontStrikeout(sal_Int32 nToken)
float GetTextSize(std::u16string_view sValue)
converts the ST_TextFontSize to point
const char * GetHatchPattern(const drawing::Hatch &rHatch)
XML_none
DefTokenId nToken
#define SAL_MAX_INT32
#define SAL_MIN_INT32