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 #include <osl/diagnose.h>
27 #include <sax/tools/converter.hxx>
28 #include <oox/token/tokens.hxx>
29 
30 using ::com::sun::star::uno::Reference;
31 using ::com::sun::star::xml::sax::XFastAttributeList;
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::drawing;
34 using namespace ::com::sun::star::geometry;
35 using namespace ::com::sun::star::style;
36 
37 namespace oox::drawingml {
38 
40 sal_Int32 GetCoordinate( sal_Int32 nValue )
41 {
43 }
44 
46 sal_Int32 GetCoordinate( std::u16string_view sValue )
47 {
48  sal_Int32 nRet = 0;
49  if( !::sax::Converter::convertNumber( nRet, sValue ) )
50  nRet = 0;
51  return GetCoordinate( nRet );
52 }
53 
55 sal_Int32 GetPointFromCoordinate( sal_Int32 nValue )
56 {
58 }
59 
61 sal_Int32 GetPercent( std::u16string_view sValue )
62 {
63  sal_Int32 nRet = 0;
64  if( !::sax::Converter::convertNumber( nRet, sValue ) )
65  nRet = 0;
66 
67  return nRet;
68 }
69 
70 double GetPositiveFixedPercentage( const OUString& sValue )
71 {
72  double fPercent = sValue.toFloat() / 100000.;
73  return fPercent;
74 }
75 
77 awt::Point GetPointPercent( const Reference< XFastAttributeList >& xAttribs )
78 {
79  return awt::Point(GetPercent(xAttribs->getOptionalValue(XML_x)), GetPercent(xAttribs->getOptionalValue(XML_y)));
80 }
81 
83 float GetTextSize( std::u16string_view sValue )
84 {
85  float fRet = 0;
86  sal_Int32 nRet;
87  if( ::sax::Converter::convertNumber( nRet, sValue ) )
88  fRet = static_cast< float >( static_cast< double >( nRet ) / 100.0 );
89  return fRet;
90 }
91 
93 sal_Int32 GetTextSpacingPoint( std::u16string_view sValue )
94 {
95  sal_Int32 nRet;
96  if( ::sax::Converter::convertNumber( nRet, sValue, (SAL_MIN_INT32 + 360) / 254, (SAL_MAX_INT32 - 360) / 254 ) )
97  nRet = GetTextSpacingPoint( nRet );
98  return nRet;
99 }
100 
101 sal_Int32 GetTextSpacingPoint(sal_Int32 nValue)
102 {
103  if (nValue > 0)
104  nValue = (nValue * 254 + 360);
105  else if (nValue < 0)
106  nValue = (nValue * 254 - 360);
107  return nValue / 720;
108 }
109 
110 float GetFontHeight( sal_Int32 nHeight )
111 {
112  // convert 1/100 points to points
113  return static_cast< float >( nHeight / 100.0 );
114 }
115 
116 sal_Int16 GetFontUnderline( sal_Int32 nToken )
117 {
118  OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
119  switch( nToken )
120  {
121  case XML_none: return awt::FontUnderline::NONE;
122  case XML_dash: return awt::FontUnderline::DASH;
123  case XML_dashHeavy: return awt::FontUnderline::BOLDDASH;
124  case XML_dashLong: return awt::FontUnderline::LONGDASH;
125  case XML_dashLongHeavy: return awt::FontUnderline::BOLDLONGDASH;
126  case XML_dbl: return awt::FontUnderline::DOUBLE;
127  case XML_dotDash: return awt::FontUnderline::DASHDOT;
128  case XML_dotDashHeavy: return awt::FontUnderline::BOLDDASHDOT;
129  case XML_dotDotDash: return awt::FontUnderline::DASHDOTDOT;
130  case XML_dotDotDashHeavy: return awt::FontUnderline::BOLDDASHDOTDOT;
131  case XML_dotted: return awt::FontUnderline::DOTTED;
132  case XML_dottedHeavy: return awt::FontUnderline::BOLDDOTTED;
133  case XML_heavy: return awt::FontUnderline::BOLD;
134  case XML_sng: return awt::FontUnderline::SINGLE;
135  case XML_wavy: return awt::FontUnderline::WAVE;
136  case XML_wavyDbl: return awt::FontUnderline::DOUBLEWAVE;
137  case XML_wavyHeavy: return awt::FontUnderline::BOLDWAVE;
138 // case XML_words: // TODO
139  }
140  return awt::FontUnderline::DONTKNOW;
141 }
142 
143 sal_Int16 GetFontStrikeout( sal_Int32 nToken )
144 {
145  OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
146  switch( nToken )
147  {
148  case XML_dblStrike: return awt::FontStrikeout::DOUBLE;
149  case XML_noStrike: return awt::FontStrikeout::NONE;
150  case XML_sngStrike: return awt::FontStrikeout::SINGLE;
151  }
152  return awt::FontStrikeout::DONTKNOW;
153 }
154 
155 sal_Int16 GetCaseMap( sal_Int32 nToken )
156 {
157  switch( nToken )
158  {
159  case XML_all: return CaseMap::UPPERCASE;
160  case XML_small: return CaseMap::SMALLCAPS;
161  }
162  return CaseMap::NONE;
163 }
164 
166 ParagraphAdjust GetParaAdjust( sal_Int32 nAlign )
167 {
168  OSL_ASSERT((nAlign & sal_Int32(0xFFFF0000))==0);
169  ParagraphAdjust nEnum;
170  switch( nAlign )
171  {
172  case XML_ctr:
173  nEnum = ParagraphAdjust_CENTER;
174  break;
175  case XML_just:
176  case XML_justLow:
177  nEnum = ParagraphAdjust_BLOCK;
178  break;
179  case XML_r:
180  nEnum = ParagraphAdjust_RIGHT;
181  break;
182  case XML_thaiDist:
183  case XML_dist:
184  nEnum = ParagraphAdjust_STRETCH;
185  break;
186  case XML_l:
187  default:
188  nEnum = ParagraphAdjust_LEFT;
189  break;
190  }
191  return nEnum;
192 }
193 
194 TextVerticalAdjust GetTextVerticalAdjust( sal_Int32 nToken )
195 {
196  TextVerticalAdjust aVertAdjust;
197  switch( nToken )
198  {
199  case XML_b:
200  aVertAdjust = TextVerticalAdjust_BOTTOM;
201  break;
202  case XML_dist:
203  case XML_just:
204  case XML_ctr:
205  aVertAdjust = TextVerticalAdjust_CENTER;
206  break;
207  case XML_t:
208  default:
209  aVertAdjust = TextVerticalAdjust_TOP;
210  break;
211  }
212  return aVertAdjust;
213 }
214 
215 const char* GetTextVerticalAdjust( TextVerticalAdjust eAdjust )
216 {
217  const char* sVerticalAdjust = nullptr;
218  switch( eAdjust )
219  {
220  case TextVerticalAdjust_BOTTOM:
221  sVerticalAdjust = "b";
222  break;
223  case TextVerticalAdjust_CENTER:
224  sVerticalAdjust = "ctr";
225  break;
226  case TextVerticalAdjust_TOP:
227  default:
228  sVerticalAdjust = "t";
229  break;
230  }
231  return sVerticalAdjust;
232 }
233 
234 TabAlign GetTabAlign( sal_Int32 aToken )
235 {
236  OSL_ASSERT((aToken & sal_Int32(0xFFFF0000))==0);
237  TabAlign nEnum;
238  switch( aToken )
239  {
240  case XML_ctr:
241  nEnum = TabAlign_CENTER;
242  break;
243  case XML_dec:
244  nEnum = TabAlign_DECIMAL;
245  break;
246  case XML_l:
247  nEnum = TabAlign_LEFT;
248  break;
249  case XML_r:
250  nEnum = TabAlign_RIGHT;
251  break;
252  default:
253  nEnum = TabAlign_DEFAULT;
254  break;
255  }
256  return nEnum;
257 }
258 
259 const char* GetHatchPattern( const drawing::Hatch& rHatch )
260 {
261  const char* sPattern = nullptr;
262  const sal_Int32 nAngle = rHatch.Angle > 1800 ? rHatch.Angle - 1800 : rHatch.Angle;
263  // Angle ~ 0° (horizontal)
264  if( (nAngle >= 0 && nAngle < 225) || nAngle >= 1575 )
265  {
266  switch( rHatch.Style )
267  {
268  case drawing::HatchStyle_SINGLE:
269  {
270  if( rHatch.Distance < 75 )
271  sPattern = "ltHorz";
272  else
273  sPattern = "horz";
274 
275  break;
276  }
277  case drawing::HatchStyle_DOUBLE:
278  case drawing::HatchStyle_TRIPLE:
279  {
280  if( rHatch.Distance < 75 )
281  sPattern = "smGrid";
282  else
283  sPattern = "lgGrid";
284 
285  break;
286  }
287  default: break;
288  }
289  }
290  // Angle ~ 45° (upward diagonal)
291  else if( nAngle < 675 )
292  {
293  switch( rHatch.Style )
294  {
295  case drawing::HatchStyle_SINGLE:
296  {
297  if( rHatch.Distance < 75 )
298  sPattern = "ltUpDiag";
299  else
300  sPattern = "wdUpDiag";
301 
302  break;
303  }
304  case drawing::HatchStyle_DOUBLE:
305  case drawing::HatchStyle_TRIPLE:
306  {
307  if( rHatch.Distance < 75 )
308  sPattern = "smCheck";
309  else
310  sPattern = "openDmnd";
311 
312  break;
313  }
314  default: break;
315  }
316  }
317  // Angle ~ 90° (vertical)
318  else if( nAngle < 1125 )
319  {
320  switch( rHatch.Style )
321  {
322  case drawing::HatchStyle_SINGLE:
323  {
324  // dkVert is imported as Distance = 25, ltVert as Distance = 50, export them accordingly.
325  if( rHatch.Distance < 50 )
326  sPattern = "dkVert";
327  else if( rHatch.Distance < 75 )
328  sPattern = "ltVert";
329  else
330  sPattern = "vert";
331 
332  break;
333  }
334  case drawing::HatchStyle_DOUBLE:
335  case drawing::HatchStyle_TRIPLE:
336  {
337  if( rHatch.Distance < 75 )
338  sPattern = "smGrid";
339  else
340  sPattern = "lgGrid";
341 
342  break;
343  }
344  default: break;
345  }
346  }
347  // Angle ~ 135° (downward diagonal)
348  else if( nAngle < 1575 )
349  {
350  switch( rHatch.Style )
351  {
352  case drawing::HatchStyle_SINGLE:
353  {
354  if( rHatch.Distance < 75 )
355  sPattern = "ltDnDiag";
356  else
357  sPattern = "wdDnDiag";
358 
359  break;
360  }
361  case drawing::HatchStyle_DOUBLE:
362  case drawing::HatchStyle_TRIPLE:
363  {
364  if( rHatch.Distance < 75 )
365  sPattern = "smCheck";
366  else
367  sPattern = "openDmnd";
368 
369  break;
370  }
371  default: break;
372  }
373  }
374  return sPattern;
375 }
376 
378 IntegerRectangle2D GetRelativeRect( const Reference< XFastAttributeList >& xAttribs )
379 {
380  IntegerRectangle2D r;
381 
382  r.X1 = xAttribs->getOptionalValue( XML_l ).toInt32();
383  r.Y1 = xAttribs->getOptionalValue( XML_t ).toInt32();
384  r.X2 = xAttribs->getOptionalValue( XML_r ).toInt32();
385  r.Y2 = xAttribs->getOptionalValue( XML_b ).toInt32();
386 
387  return r;
388 }
389 
391 awt::Size GetSize2D( const Reference< XFastAttributeList >& xAttribs )
392 {
393  return awt::Size( GetCoordinate( xAttribs->getOptionalValue( XML_cx ) ), GetCoordinate( xAttribs->getOptionalValue( XML_cy ) ) );
394 }
395 
397 {
398  IndexRange range;
399  range.start = xAttributes->getOptionalValue( XML_st ).toInt32();
400  range.end = xAttributes->getOptionalValue( XML_end ).toInt32();
401  return range;
402 }
403 
404 } // namespace oox::drawingml
405 
406 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int16 GetFontStrikeout(sal_Int32 nToken)
sal_Int32 GetTextSpacingPoint(std::u16string_view sValue)
converts the ST_TextSpacingPoint to 1/100mm
TextVerticalAdjust GetTextVerticalAdjust(sal_Int32 nToken)
constexpr Point convert(const Point &rPoint, o3tl::Length eFrom, o3tl::Length eTo)
sal_Int32 GetPercent(std::u16string_view sValue)
converts a ST_Percentage % string into 1/1000th of %
float GetFontHeight(sal_Int32 nHeight)
double GetPositiveFixedPercentage(const OUString &sValue)
Converts a ST_PositiveFixedPercentage to a float.
#define SAL_MAX_INT32
awt::Size GetSize2D(const Reference< XFastAttributeList > &xAttribs)
converts the attributes from a CT_Size2D into an awt Size with 1/100thmm
#define SAL_MIN_INT32
IntegerRectangle2D GetRelativeRect(const Reference< XFastAttributeList > &xAttribs)
converts the attributes from a CT_RelativeRect to an IntegerRectangle2D
sal_Int32 GetPointFromCoordinate(sal_Int32 nValue)
converts 1/100mm to EMU
sal_Int32 GetCoordinate(sal_Int32 nValue)
converts EMUs into 1/100th mmm
const char * GetHatchPattern(const drawing::Hatch &rHatch)
ParagraphAdjust GetParaAdjust(sal_Int32 nAlign)
converts a paragraph align to a ParaAdjust
awt::Point GetPointPercent(const Reference< XFastAttributeList > &xAttribs)
converts the attributes from a CT_TLPoint into an awt Point with 1/1000%
sal_Int16 GetFontUnderline(sal_Int32 nToken)
XML_none
IndexRange GetIndexRange(const Reference< XFastAttributeList > &xAttributes)
TabAlign GetTabAlign(sal_Int32 aToken)
float GetTextSize(std::u16string_view sValue)
converts the ST_TextFontSize to point
sal_Int16 GetCaseMap(sal_Int32 nToken)
static bool convertNumber(sal_Int32 &rValue, std::u16string_view aString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)