Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / oox / source / drawingml / drawingmltypes.cxx
blob725d7af84c87ce8b2a7ccc5e55140035e9377ade
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 <osl/diagnose.h>
27 #include <sax/tools/converter.hxx>
28 #include "oox/token/tokens.hxx"
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;
37 namespace oox {
38 namespace drawingml {
40 /** converts EMUs into 1/100th mmm */
41 sal_Int32 GetCoordinate( sal_Int32 nValue )
43 return (nValue + 180) / 360;
46 /** converts an emu string into 1/100th mmm */
47 sal_Int32 GetCoordinate( const OUString& sValue )
49 sal_Int32 nRet = 0;
50 if( !::sax::Converter::convertNumber( nRet, sValue ) )
51 nRet = 0;
52 return GetCoordinate( nRet );
55 /** converts a ST_Percentage % string into 1/1000th of % */
56 sal_Int32 GetPercent( const OUString& sValue )
58 sal_Int32 nRet = 0;
59 if( !::sax::Converter::convertNumber( nRet, sValue ) )
60 nRet = 0;
62 return nRet;
65 double GetPositiveFixedPercentage( const OUString& sValue )
67 double fPercent = sValue.toFloat() / 100000.;
68 return fPercent;
71 /** converts the attributes from an CT_TLPoint into an awt Point with 1/1000% */
72 awt::Point GetPointPercent( const Reference< XFastAttributeList >& xAttribs )
74 return awt::Point( GetPercent( xAttribs->getOptionalValue( XML_x ) ), GetCoordinate( xAttribs->getOptionalValue( XML_y ) ) );
77 /** converts the ST_TextFontSize to point */
78 float GetTextSize( const OUString& sValue )
80 float fRet = 0;
81 sal_Int32 nRet;
82 if( ::sax::Converter::convertNumber( nRet, sValue ) )
83 fRet = static_cast< float >( static_cast< double >( nRet ) / 100.0 );
84 return fRet;
87 /** converts the ST_TextSpacingPoint to 1/100mm */
88 sal_Int32 GetTextSpacingPoint( const OUString& sValue )
90 sal_Int32 nRet;
91 if( ::sax::Converter::convertNumber( nRet, sValue ) )
92 nRet = GetTextSpacingPoint( nRet );
93 return nRet;
96 sal_Int32 GetTextSpacingPoint( const sal_Int32 nValue )
98 return ( nValue * 254 + 360 ) / 720;
101 float GetFontHeight( sal_Int32 nHeight )
103 // convert 1/100 points to points
104 return static_cast< float >( nHeight / 100.0 );
107 sal_Int16 GetFontUnderline( sal_Int32 nToken )
109 OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
110 switch( nToken )
112 case XML_none: return awt::FontUnderline::NONE;
113 case XML_dash: return awt::FontUnderline::DASH;
114 case XML_dashHeavy: return awt::FontUnderline::BOLDDASH;
115 case XML_dashLong: return awt::FontUnderline::LONGDASH;
116 case XML_dashLongHeavy: return awt::FontUnderline::BOLDLONGDASH;
117 case XML_dbl: return awt::FontUnderline::DOUBLE;
118 case XML_dotDash: return awt::FontUnderline::DASHDOT;
119 case XML_dotDashHeavy: return awt::FontUnderline::BOLDDASHDOT;
120 case XML_dotDotDash: return awt::FontUnderline::DASHDOTDOT;
121 case XML_dotDotDashHeavy: return awt::FontUnderline::BOLDDASHDOTDOT;
122 case XML_dotted: return awt::FontUnderline::DOTTED;
123 case XML_dottedHeavy: return awt::FontUnderline::BOLDDOTTED;
124 case XML_heavy: return awt::FontUnderline::BOLD;
125 case XML_sng: return awt::FontUnderline::SINGLE;
126 case XML_wavy: return awt::FontUnderline::WAVE;
127 case XML_wavyDbl: return awt::FontUnderline::DOUBLEWAVE;
128 case XML_wavyHeavy: return awt::FontUnderline::BOLDWAVE;
129 // case XML_words: // TODO
131 return awt::FontUnderline::DONTKNOW;
134 sal_Int16 GetFontStrikeout( sal_Int32 nToken )
136 OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
137 switch( nToken )
139 case XML_dblStrike: return awt::FontStrikeout::DOUBLE;
140 case XML_noStrike: return awt::FontStrikeout::NONE;
141 case XML_sngStrike: return awt::FontStrikeout::SINGLE;
143 return awt::FontStrikeout::DONTKNOW;
146 sal_Int16 GetCaseMap( sal_Int32 nToken )
148 switch( nToken )
150 case XML_all: return CaseMap::UPPERCASE;
151 case XML_small: return CaseMap::SMALLCAPS;
153 return CaseMap::NONE;
156 /** converts a paragraph align to a ParaAdjust */
157 ParagraphAdjust GetParaAdjust( sal_Int32 nAlign )
159 OSL_ASSERT((nAlign & sal_Int32(0xFFFF0000))==0);
160 ParagraphAdjust nEnum;
161 switch( nAlign )
163 case XML_ctr:
164 nEnum = ParagraphAdjust_CENTER;
165 break;
166 case XML_just:
167 case XML_justLow:
168 nEnum = ParagraphAdjust_BLOCK;
169 break;
170 case XML_r:
171 nEnum = ParagraphAdjust_RIGHT;
172 break;
173 case XML_thaiDist:
174 case XML_dist:
175 nEnum = ParagraphAdjust_STRETCH;
176 break;
177 case XML_l:
178 default:
179 nEnum = ParagraphAdjust_LEFT;
180 break;
182 return nEnum;
185 TextVerticalAdjust GetTextVerticalAdjust( sal_Int32 nToken )
187 TextVerticalAdjust aVertAdjust;
188 switch( nToken )
190 case XML_b:
191 aVertAdjust = TextVerticalAdjust_BOTTOM;
192 break;
193 case XML_dist:
194 case XML_just:
195 case XML_ctr:
196 aVertAdjust = TextVerticalAdjust_CENTER;
197 break;
198 case XML_t:
199 default:
200 aVertAdjust = TextVerticalAdjust_TOP;
201 break;
203 return aVertAdjust;
206 const char* GetTextVerticalAdjust( TextVerticalAdjust eAdjust )
208 const char* sVerticalAdjust = nullptr;
209 switch( eAdjust )
211 case TextVerticalAdjust_BOTTOM:
212 sVerticalAdjust = "b";
213 break;
214 case TextVerticalAdjust_CENTER:
215 sVerticalAdjust = "ctr";
216 break;
217 case TextVerticalAdjust_TOP:
218 default:
219 sVerticalAdjust = "t";
220 break;
222 return sVerticalAdjust;
225 TabAlign GetTabAlign( sal_Int32 aToken )
227 OSL_ASSERT((aToken & sal_Int32(0xFFFF0000))==0);
228 TabAlign nEnum;
229 switch( aToken )
231 case XML_ctr:
232 nEnum = TabAlign_CENTER;
233 break;
234 case XML_dec:
235 nEnum = TabAlign_DECIMAL;
236 break;
237 case XML_l:
238 nEnum = TabAlign_LEFT;
239 break;
240 case XML_r:
241 nEnum = TabAlign_RIGHT;
242 break;
243 default:
244 nEnum = TabAlign_DEFAULT;
245 break;
247 return nEnum;
250 const char* GetHatchPattern( const drawing::Hatch& rHatch )
252 const char* sPattern = nullptr;
253 const sal_Int32 nAngle = rHatch.Angle > 1800 ? rHatch.Angle - 1800 : rHatch.Angle;
254 // Angle ~ 0° (horizontal)
255 if( (nAngle >= 0 && nAngle < 225) || nAngle >= 1575 )
257 switch( rHatch.Style )
259 case drawing::HatchStyle_SINGLE:
261 if( rHatch.Distance < 75 )
262 sPattern = "ltHorz";
263 else
264 sPattern = "horz";
266 break;
268 case drawing::HatchStyle_DOUBLE:
269 case drawing::HatchStyle_TRIPLE:
271 if( rHatch.Distance < 75 )
272 sPattern = "smGrid";
273 else
274 sPattern = "lgGrid";
276 break;
278 default: break;
281 // Angle ~ 45° (upward diagonal)
282 else if( nAngle < 675 )
284 switch( rHatch.Style )
286 case drawing::HatchStyle_SINGLE:
288 if( rHatch.Distance < 75 )
289 sPattern = "ltUpDiag";
290 else
291 sPattern = "wdUpDiag";
293 break;
295 case drawing::HatchStyle_DOUBLE:
296 case drawing::HatchStyle_TRIPLE:
298 if( rHatch.Distance < 75 )
299 sPattern = "smCheck";
300 else
301 sPattern = "openDmnd";
303 break;
305 default: break;
308 // Angle ~ 90° (vertical)
309 else if( nAngle < 1125 )
311 switch( rHatch.Style )
313 case drawing::HatchStyle_SINGLE:
315 // dkVert is imported as Distance = 25, ltVert as Distance = 50, export them accordingly.
316 if( rHatch.Distance < 50 )
317 sPattern = "dkVert";
318 else if( rHatch.Distance < 75 )
319 sPattern = "ltVert";
320 else
321 sPattern = "vert";
323 break;
325 case drawing::HatchStyle_DOUBLE:
326 case drawing::HatchStyle_TRIPLE:
328 if( rHatch.Distance < 75 )
329 sPattern = "smGrid";
330 else
331 sPattern = "lgGrid";
333 break;
335 default: break;
338 // Angle ~ 135° (downward diagonal)
339 else if( nAngle < 1575 )
341 switch( rHatch.Style )
343 case drawing::HatchStyle_SINGLE:
345 if( rHatch.Distance < 75 )
346 sPattern = "ltDnDiag";
347 else
348 sPattern = "wdDnDiag";
350 break;
352 case drawing::HatchStyle_DOUBLE:
353 case drawing::HatchStyle_TRIPLE:
355 if( rHatch.Distance < 75 )
356 sPattern = "smCheck";
357 else
358 sPattern = "openDmnd";
360 break;
362 default: break;
365 return sPattern;
368 /** converts the attributes from a CT_RelativeRect to an IntegerRectangle2D */
369 IntegerRectangle2D GetRelativeRect( const Reference< XFastAttributeList >& xAttribs )
371 IntegerRectangle2D r;
373 r.X1 = xAttribs->getOptionalValue( XML_l ).toInt32();
374 r.Y1 = xAttribs->getOptionalValue( XML_t ).toInt32();
375 r.X2 = xAttribs->getOptionalValue( XML_r ).toInt32();
376 r.Y2 = xAttribs->getOptionalValue( XML_b ).toInt32();
378 return r;
381 /** converts the attributes from an CT_Size2D into an awt Size with 1/100thmm */
382 awt::Size GetSize2D( const Reference< XFastAttributeList >& xAttribs )
384 return awt::Size( GetCoordinate( xAttribs->getOptionalValue( XML_cx ) ), GetCoordinate( xAttribs->getOptionalValue( XML_cy ) ) );
387 IndexRange GetIndexRange( const Reference< XFastAttributeList >& xAttributes )
389 IndexRange range;
390 range.start = xAttributes->getOptionalValue( XML_st ).toInt32();
391 range.end = xAttributes->getOptionalValue( XML_end ).toInt32();
392 return range;
395 } // namespace drawingml
396 } // namespace oox
398 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */