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 <osl/diagnose.h>
30#include <oox/token/tokens.hxx>
31
32using ::com::sun::star::uno::Reference;
33using ::com::sun::star::xml::sax::XFastAttributeList;
34using namespace ::com::sun::star;
35using namespace ::com::sun::star::drawing;
36using namespace ::com::sun::star::geometry;
37using namespace ::com::sun::star::style;
38
39namespace oox::drawingml {
40
42sal_Int32 GetCoordinate( sal_Int32 nValue )
43{
45}
46
48sal_Int32 GetCoordinate( std::u16string_view sValue )
49{
50 sal_Int32 nRet = 0;
51 if( !::sax::Converter::convertNumber( nRet, sValue ) )
52 nRet = 0;
53 return GetCoordinate( nRet );
54}
55
57sal_Int32 GetPointFromCoordinate( sal_Int32 nValue )
58{
60}
61
63sal_Int32 GetPercent( std::u16string_view sValue )
64{
65 sal_Int32 nRet = 0;
66 if( !::sax::Converter::convertNumber( nRet, sValue ) )
67 nRet = 0;
68
69 return nRet;
70}
71
72double GetPositiveFixedPercentage( const OUString& sValue )
73{
74 double fPercent = sValue.toFloat() / 100000.;
75 return fPercent;
76}
77
80{
81 return awt::Point(GetPercent(xAttribs->getOptionalValue(XML_x)), GetPercent(xAttribs->getOptionalValue(XML_y)));
82}
83
85float GetTextSize( std::u16string_view sValue )
86{
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;
92}
93
95sal_Int32 GetTextSpacingPoint( std::u16string_view sValue )
96{
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;
101}
102
103sal_Int32 GetTextSpacingPoint(sal_Int32 nValue)
104{
105 if (nValue > 0)
106 nValue = (nValue * 254 + 360);
107 else if (nValue < 0)
108 nValue = (nValue * 254 - 360);
109 return nValue / 720;
110}
111
112float GetFontHeight( sal_Int32 nHeight )
113{
114 // convert 1/100 points to points
115 return static_cast< float >( nHeight / 100.0 );
116}
117
118sal_Int16 GetFontUnderline( sal_Int32 nToken )
119{
120 OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
121 switch( nToken )
122 {
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
141 }
142 return awt::FontUnderline::DONTKNOW;
143}
144
145sal_Int16 GetFontStrikeout( sal_Int32 nToken )
146{
147 OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
148 switch( nToken )
149 {
150 case XML_dblStrike: return awt::FontStrikeout::DOUBLE;
151 case XML_noStrike: return awt::FontStrikeout::NONE;
152 case XML_sngStrike: return awt::FontStrikeout::SINGLE;
153 }
154 return awt::FontStrikeout::DONTKNOW;
155}
156
157sal_Int16 GetCaseMap( sal_Int32 nToken )
158{
159 switch( nToken )
160 {
161 case XML_all: return CaseMap::UPPERCASE;
162 case XML_small: return CaseMap::SMALLCAPS;
163 }
164 return CaseMap::NONE;
165}
166
168ParagraphAdjust GetParaAdjust( sal_Int32 nAlign )
169{
170 OSL_ASSERT((nAlign & sal_Int32(0xFFFF0000))==0);
171 ParagraphAdjust nEnum;
172 switch( nAlign )
173 {
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;
192 }
193 return nEnum;
194}
195
196TextVerticalAdjust GetTextVerticalAdjust( sal_Int32 nToken )
197{
198 TextVerticalAdjust aVertAdjust;
199 switch( nToken )
200 {
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;
213 }
214 return aVertAdjust;
215}
216
217const char* GetTextVerticalAdjust( TextVerticalAdjust eAdjust )
218{
219 const char* sVerticalAdjust = nullptr;
220 switch( eAdjust )
221 {
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;
232 }
233 return sVerticalAdjust;
234}
235
236TabAlign GetTabAlign( sal_Int32 aToken )
237{
238 OSL_ASSERT((aToken & sal_Int32(0xFFFF0000))==0);
239 TabAlign nEnum;
240 switch( aToken )
241 {
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;
257 }
258 return nEnum;
259}
260
261const char* GetHatchPattern( const drawing::Hatch& rHatch )
262{
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 )
267 {
268 switch( rHatch.Style )
269 {
270 case drawing::HatchStyle_SINGLE:
271 {
272 if( rHatch.Distance < 75 )
273 sPattern = "ltHorz";
274 else
275 sPattern = "horz";
276
277 break;
278 }
279 case drawing::HatchStyle_DOUBLE:
280 case drawing::HatchStyle_TRIPLE:
281 {
282 if( rHatch.Distance < 75 )
283 sPattern = "smGrid";
284 else
285 sPattern = "lgGrid";
286
287 break;
288 }
289 default: break;
290 }
291 }
292 // Angle ~ 45° (upward diagonal)
293 else if( nAngle < 675 )
294 {
295 switch( rHatch.Style )
296 {
297 case drawing::HatchStyle_SINGLE:
298 {
299 if( rHatch.Distance < 75 )
300 sPattern = "ltUpDiag";
301 else
302 sPattern = "wdUpDiag";
303
304 break;
305 }
306 case drawing::HatchStyle_DOUBLE:
307 case drawing::HatchStyle_TRIPLE:
308 {
309 if( rHatch.Distance < 75 )
310 sPattern = "smCheck";
311 else
312 sPattern = "openDmnd";
313
314 break;
315 }
316 default: break;
317 }
318 }
319 // Angle ~ 90° (vertical)
320 else if( nAngle < 1125 )
321 {
322 switch( rHatch.Style )
323 {
324 case drawing::HatchStyle_SINGLE:
325 {
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";
333
334 break;
335 }
336 case drawing::HatchStyle_DOUBLE:
337 case drawing::HatchStyle_TRIPLE:
338 {
339 if( rHatch.Distance < 75 )
340 sPattern = "smGrid";
341 else
342 sPattern = "lgGrid";
343
344 break;
345 }
346 default: break;
347 }
348 }
349 // Angle ~ 135° (downward diagonal)
350 else if( nAngle < 1575 )
351 {
352 switch( rHatch.Style )
353 {
354 case drawing::HatchStyle_SINGLE:
355 {
356 if( rHatch.Distance < 75 )
357 sPattern = "ltDnDiag";
358 else
359 sPattern = "wdDnDiag";
360
361 break;
362 }
363 case drawing::HatchStyle_DOUBLE:
364 case drawing::HatchStyle_TRIPLE:
365 {
366 if( rHatch.Distance < 75 )
367 sPattern = "smCheck";
368 else
369 sPattern = "openDmnd";
370
371 break;
372 }
373 default: break;
374 }
375 }
376 return sPattern;
377}
378
379namespace
380{
381// ISO/IEC-29500 Part 1 ST_Percentage, and [MS-OI29500] 2.1.1324
382sal_Int32 GetST_Percentage(std::u16string_view s)
383{
384 if (o3tl::ends_with(s, u"%"))
385 return std::round(o3tl::toDouble(s) * 1000);
386 return o3tl::toInt32(s);
387}
388}
389
391IntegerRectangle2D GetRelativeRect( const Reference< XFastAttributeList >& xAttribs )
392{
393 IntegerRectangle2D r;
394
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 ));
399
400 return r;
401}
402
404awt::Size GetSize2D( const Reference< XFastAttributeList >& xAttribs )
405{
406 return awt::Size( GetCoordinate( xAttribs->getOptionalValue( XML_cx ) ), GetCoordinate( xAttribs->getOptionalValue( XML_cy ) ) );
407}
408
410{
411 IndexRange range;
412 range.start = xAttributes->getOptionalValue( XML_st ).toInt32();
413 range.end = xAttributes->getOptionalValue( XML_end ).toInt32();
414 return range;
415}
416
417} // namespace oox::drawingml
418
419/* 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
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
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.
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