Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / oox / source / drawingml / drawingmltypes.cxx
blobf341ff3f958701aeacb226569db5ab2cd62814a2
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>
26 #include <o3tl/safeint.hxx>
27 #include <osl/diagnose.h>
28 #include <sax/tools/converter.hxx>
29 #include <oox/token/tokens.hxx>
31 using ::com::sun::star::uno::Reference;
32 using ::com::sun::star::xml::sax::XFastAttributeList;
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::drawing;
35 using namespace ::com::sun::star::geometry;
36 using namespace ::com::sun::star::style;
38 namespace oox {
39 namespace drawingml {
41 /** converts EMUs into 1/100th mmm */
42 sal_Int32 GetCoordinate( sal_Int32 nValue )
44 nValue = o3tl::saturating_add<sal_Int32>(nValue, 180);
45 return nValue / 360;
48 /** converts an emu string into 1/100th mmm */
49 sal_Int32 GetCoordinate( const OUString& sValue )
51 sal_Int32 nRet = 0;
52 if( !::sax::Converter::convertNumber( nRet, sValue ) )
53 nRet = 0;
54 return GetCoordinate( nRet );
57 /** converts a ST_Percentage % string into 1/1000th of % */
58 sal_Int32 GetPercent( const OUString& sValue )
60 sal_Int32 nRet = 0;
61 if( !::sax::Converter::convertNumber( nRet, sValue ) )
62 nRet = 0;
64 return nRet;
67 double GetPositiveFixedPercentage( const OUString& sValue )
69 double fPercent = sValue.toFloat() / 100000.;
70 return fPercent;
73 /** converts the attributes from a CT_TLPoint into an awt Point with 1/1000% */
74 awt::Point GetPointPercent( const Reference< XFastAttributeList >& xAttribs )
76 return awt::Point(GetPercent(xAttribs->getOptionalValue(XML_x)), GetPercent(xAttribs->getOptionalValue(XML_y)));
79 /** converts the ST_TextFontSize to point */
80 float GetTextSize( const OUString& sValue )
82 float fRet = 0;
83 sal_Int32 nRet;
84 if( ::sax::Converter::convertNumber( nRet, sValue ) )
85 fRet = static_cast< float >( static_cast< double >( nRet ) / 100.0 );
86 return fRet;
89 /** converts the ST_TextSpacingPoint to 1/100mm */
90 sal_Int32 GetTextSpacingPoint( const OUString& sValue )
92 sal_Int32 nRet;
93 if( ::sax::Converter::convertNumber( nRet, sValue, (SAL_MIN_INT32 + 360) / 254, (SAL_MAX_INT32 - 360) / 254 ) )
94 nRet = GetTextSpacingPoint( nRet );
95 return nRet;
98 sal_Int32 GetTextSpacingPoint(sal_Int32 nValue)
100 if (nValue > 0)
101 nValue = (nValue * 254 + 360);
102 else if (nValue < 0)
103 nValue = (nValue * 254 - 360);
104 return nValue / 720;
107 float GetFontHeight( sal_Int32 nHeight )
109 // convert 1/100 points to points
110 return static_cast< float >( nHeight / 100.0 );
113 sal_Int16 GetFontUnderline( sal_Int32 nToken )
115 OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
116 switch( nToken )
118 case XML_none: return awt::FontUnderline::NONE;
119 case XML_dash: return awt::FontUnderline::DASH;
120 case XML_dashHeavy: return awt::FontUnderline::BOLDDASH;
121 case XML_dashLong: return awt::FontUnderline::LONGDASH;
122 case XML_dashLongHeavy: return awt::FontUnderline::BOLDLONGDASH;
123 case XML_dbl: return awt::FontUnderline::DOUBLE;
124 case XML_dotDash: return awt::FontUnderline::DASHDOT;
125 case XML_dotDashHeavy: return awt::FontUnderline::BOLDDASHDOT;
126 case XML_dotDotDash: return awt::FontUnderline::DASHDOTDOT;
127 case XML_dotDotDashHeavy: return awt::FontUnderline::BOLDDASHDOTDOT;
128 case XML_dotted: return awt::FontUnderline::DOTTED;
129 case XML_dottedHeavy: return awt::FontUnderline::BOLDDOTTED;
130 case XML_heavy: return awt::FontUnderline::BOLD;
131 case XML_sng: return awt::FontUnderline::SINGLE;
132 case XML_wavy: return awt::FontUnderline::WAVE;
133 case XML_wavyDbl: return awt::FontUnderline::DOUBLEWAVE;
134 case XML_wavyHeavy: return awt::FontUnderline::BOLDWAVE;
135 // case XML_words: // TODO
137 return awt::FontUnderline::DONTKNOW;
140 sal_Int16 GetFontStrikeout( sal_Int32 nToken )
142 OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
143 switch( nToken )
145 case XML_dblStrike: return awt::FontStrikeout::DOUBLE;
146 case XML_noStrike: return awt::FontStrikeout::NONE;
147 case XML_sngStrike: return awt::FontStrikeout::SINGLE;
149 return awt::FontStrikeout::DONTKNOW;
152 sal_Int16 GetCaseMap( sal_Int32 nToken )
154 switch( nToken )
156 case XML_all: return CaseMap::UPPERCASE;
157 case XML_small: return CaseMap::SMALLCAPS;
159 return CaseMap::NONE;
162 /** converts a paragraph align to a ParaAdjust */
163 ParagraphAdjust GetParaAdjust( sal_Int32 nAlign )
165 OSL_ASSERT((nAlign & sal_Int32(0xFFFF0000))==0);
166 ParagraphAdjust nEnum;
167 switch( nAlign )
169 case XML_ctr:
170 nEnum = ParagraphAdjust_CENTER;
171 break;
172 case XML_just:
173 case XML_justLow:
174 nEnum = ParagraphAdjust_BLOCK;
175 break;
176 case XML_r:
177 nEnum = ParagraphAdjust_RIGHT;
178 break;
179 case XML_thaiDist:
180 case XML_dist:
181 nEnum = ParagraphAdjust_STRETCH;
182 break;
183 case XML_l:
184 default:
185 nEnum = ParagraphAdjust_LEFT;
186 break;
188 return nEnum;
191 TextVerticalAdjust GetTextVerticalAdjust( sal_Int32 nToken )
193 TextVerticalAdjust aVertAdjust;
194 switch( nToken )
196 case XML_b:
197 aVertAdjust = TextVerticalAdjust_BOTTOM;
198 break;
199 case XML_dist:
200 case XML_just:
201 case XML_ctr:
202 aVertAdjust = TextVerticalAdjust_CENTER;
203 break;
204 case XML_t:
205 default:
206 aVertAdjust = TextVerticalAdjust_TOP;
207 break;
209 return aVertAdjust;
212 const char* GetTextVerticalAdjust( TextVerticalAdjust eAdjust )
214 const char* sVerticalAdjust = nullptr;
215 switch( eAdjust )
217 case TextVerticalAdjust_BOTTOM:
218 sVerticalAdjust = "b";
219 break;
220 case TextVerticalAdjust_CENTER:
221 sVerticalAdjust = "ctr";
222 break;
223 case TextVerticalAdjust_TOP:
224 default:
225 sVerticalAdjust = "t";
226 break;
228 return sVerticalAdjust;
231 TabAlign GetTabAlign( sal_Int32 aToken )
233 OSL_ASSERT((aToken & sal_Int32(0xFFFF0000))==0);
234 TabAlign nEnum;
235 switch( aToken )
237 case XML_ctr:
238 nEnum = TabAlign_CENTER;
239 break;
240 case XML_dec:
241 nEnum = TabAlign_DECIMAL;
242 break;
243 case XML_l:
244 nEnum = TabAlign_LEFT;
245 break;
246 case XML_r:
247 nEnum = TabAlign_RIGHT;
248 break;
249 default:
250 nEnum = TabAlign_DEFAULT;
251 break;
253 return nEnum;
256 const char* GetHatchPattern( const drawing::Hatch& rHatch )
258 const char* sPattern = nullptr;
259 const sal_Int32 nAngle = rHatch.Angle > 1800 ? rHatch.Angle - 1800 : rHatch.Angle;
260 // Angle ~ 0° (horizontal)
261 if( (nAngle >= 0 && nAngle < 225) || nAngle >= 1575 )
263 switch( rHatch.Style )
265 case drawing::HatchStyle_SINGLE:
267 if( rHatch.Distance < 75 )
268 sPattern = "ltHorz";
269 else
270 sPattern = "horz";
272 break;
274 case drawing::HatchStyle_DOUBLE:
275 case drawing::HatchStyle_TRIPLE:
277 if( rHatch.Distance < 75 )
278 sPattern = "smGrid";
279 else
280 sPattern = "lgGrid";
282 break;
284 default: break;
287 // Angle ~ 45° (upward diagonal)
288 else if( nAngle < 675 )
290 switch( rHatch.Style )
292 case drawing::HatchStyle_SINGLE:
294 if( rHatch.Distance < 75 )
295 sPattern = "ltUpDiag";
296 else
297 sPattern = "wdUpDiag";
299 break;
301 case drawing::HatchStyle_DOUBLE:
302 case drawing::HatchStyle_TRIPLE:
304 if( rHatch.Distance < 75 )
305 sPattern = "smCheck";
306 else
307 sPattern = "openDmnd";
309 break;
311 default: break;
314 // Angle ~ 90° (vertical)
315 else if( nAngle < 1125 )
317 switch( rHatch.Style )
319 case drawing::HatchStyle_SINGLE:
321 // dkVert is imported as Distance = 25, ltVert as Distance = 50, export them accordingly.
322 if( rHatch.Distance < 50 )
323 sPattern = "dkVert";
324 else if( rHatch.Distance < 75 )
325 sPattern = "ltVert";
326 else
327 sPattern = "vert";
329 break;
331 case drawing::HatchStyle_DOUBLE:
332 case drawing::HatchStyle_TRIPLE:
334 if( rHatch.Distance < 75 )
335 sPattern = "smGrid";
336 else
337 sPattern = "lgGrid";
339 break;
341 default: break;
344 // Angle ~ 135° (downward diagonal)
345 else if( nAngle < 1575 )
347 switch( rHatch.Style )
349 case drawing::HatchStyle_SINGLE:
351 if( rHatch.Distance < 75 )
352 sPattern = "ltDnDiag";
353 else
354 sPattern = "wdDnDiag";
356 break;
358 case drawing::HatchStyle_DOUBLE:
359 case drawing::HatchStyle_TRIPLE:
361 if( rHatch.Distance < 75 )
362 sPattern = "smCheck";
363 else
364 sPattern = "openDmnd";
366 break;
368 default: break;
371 return sPattern;
374 /** converts the attributes from a CT_RelativeRect to an IntegerRectangle2D */
375 IntegerRectangle2D GetRelativeRect( const Reference< XFastAttributeList >& xAttribs )
377 IntegerRectangle2D r;
379 r.X1 = xAttribs->getOptionalValue( XML_l ).toInt32();
380 r.Y1 = xAttribs->getOptionalValue( XML_t ).toInt32();
381 r.X2 = xAttribs->getOptionalValue( XML_r ).toInt32();
382 r.Y2 = xAttribs->getOptionalValue( XML_b ).toInt32();
384 return r;
387 /** converts the attributes from a CT_Size2D into an awt Size with 1/100thmm */
388 awt::Size GetSize2D( const Reference< XFastAttributeList >& xAttribs )
390 return awt::Size( GetCoordinate( xAttribs->getOptionalValue( XML_cx ) ), GetCoordinate( xAttribs->getOptionalValue( XML_cy ) ) );
393 IndexRange GetIndexRange( const Reference< XFastAttributeList >& xAttributes )
395 IndexRange range;
396 range.start = xAttributes->getOptionalValue( XML_st ).toInt32();
397 range.end = xAttributes->getOptionalValue( XML_end ).toInt32();
398 return range;
401 } // namespace drawingml
402 } // namespace oox
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */