fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / oox / source / drawingml / drawingmltypes.cxx
blob31529ed68be78773f9df891bd3057d851da61c2f
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/style/CaseMap.hpp>
24 #include <com/sun/star/style/ParagraphAdjust.hpp>
25 #include <osl/diagnose.h>
26 #include <sax/tools/converter.hxx>
27 #include "oox/token/tokens.hxx"
29 using ::com::sun::star::uno::Reference;
30 using ::com::sun::star::xml::sax::XFastAttributeList;
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::drawing;
33 using namespace ::com::sun::star::geometry;
34 using namespace ::com::sun::star::style;
36 namespace oox {
37 namespace drawingml {
39 /** converts EMUs into 1/100th mmm */
40 sal_Int32 GetCoordinate( sal_Int32 nValue )
42 return (nValue + 180) / 360;
45 /** converts an emu string into 1/100th mmm */
46 sal_Int32 GetCoordinate( const OUString& sValue )
48 sal_Int32 nRet = 0;
49 if( !::sax::Converter::convertNumber( nRet, sValue ) )
50 nRet = 0;
51 return GetCoordinate( nRet );
54 /** converts a ST_Percentage % string into 1/1000th of % */
55 sal_Int32 GetPercent( const OUString& sValue )
57 sal_Int32 nRet = 0;
58 if( !::sax::Converter::convertNumber( nRet, sValue ) )
59 nRet = 0;
61 return nRet;
64 double GetPositiveFixedPercentage( const OUString& sValue )
66 double fPercent = sValue.toFloat() / 100000.;
67 return fPercent;
70 /** converts the attributes from an CT_TLPoint into an awt Point with 1/1000% */
71 awt::Point GetPointPercent( const Reference< XFastAttributeList >& xAttribs )
73 return awt::Point( GetPercent( xAttribs->getOptionalValue( XML_x ) ), GetCoordinate( xAttribs->getOptionalValue( XML_y ) ) );
76 /** converts the ST_TextFontSize to point */
77 float GetTextSize( const OUString& sValue )
79 float fRet = 0;
80 sal_Int32 nRet;
81 if( ::sax::Converter::convertNumber( nRet, sValue ) )
82 fRet = static_cast< float >( static_cast< double >( nRet ) / 100.0 );
83 return fRet;
86 /** converts the ST_TextSpacingPoint to 1/100mm */
87 sal_Int32 GetTextSpacingPoint( const OUString& sValue )
89 sal_Int32 nRet;
90 if( ::sax::Converter::convertNumber( nRet, sValue ) )
91 nRet = GetTextSpacingPoint( nRet );
92 return nRet;
95 sal_Int32 GetTextSpacingPoint( const sal_Int32 nValue )
97 return ( nValue * 254 + 360 ) / 720;
100 float GetFontHeight( sal_Int32 nHeight )
102 // convert 1/100 points to points
103 return static_cast< float >( nHeight / 100.0 );
106 sal_Int16 GetFontUnderline( sal_Int32 nToken )
108 OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
109 switch( nToken )
111 case XML_none: return awt::FontUnderline::NONE;
112 case XML_dash: return awt::FontUnderline::DASH;
113 case XML_dashHeavy: return awt::FontUnderline::BOLDDASH;
114 case XML_dashLong: return awt::FontUnderline::LONGDASH;
115 case XML_dashLongHeavy: return awt::FontUnderline::BOLDLONGDASH;
116 case XML_dbl: return awt::FontUnderline::DOUBLE;
117 case XML_dotDash: return awt::FontUnderline::DASHDOT;
118 case XML_dotDashHeavy: return awt::FontUnderline::BOLDDASHDOT;
119 case XML_dotDotDash: return awt::FontUnderline::DASHDOTDOT;
120 case XML_dotDotDashHeavy: return awt::FontUnderline::BOLDDASHDOTDOT;
121 case XML_dotted: return awt::FontUnderline::DOTTED;
122 case XML_dottedHeavy: return awt::FontUnderline::BOLDDOTTED;
123 case XML_heavy: return awt::FontUnderline::BOLD;
124 case XML_sng: return awt::FontUnderline::SINGLE;
125 case XML_wavy: return awt::FontUnderline::WAVE;
126 case XML_wavyDbl: return awt::FontUnderline::DOUBLEWAVE;
127 case XML_wavyHeavy: return awt::FontUnderline::BOLDWAVE;
128 // case XML_words: // TODO
130 return awt::FontUnderline::DONTKNOW;
133 sal_Int16 GetFontStrikeout( sal_Int32 nToken )
135 OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
136 switch( nToken )
138 case XML_dblStrike: return awt::FontStrikeout::DOUBLE;
139 case XML_noStrike: return awt::FontStrikeout::NONE;
140 case XML_sngStrike: return awt::FontStrikeout::SINGLE;
142 return awt::FontStrikeout::DONTKNOW;
145 sal_Int16 GetCaseMap( sal_Int32 nToken )
147 switch( nToken )
149 case XML_all: return CaseMap::UPPERCASE;
150 case XML_small: return CaseMap::SMALLCAPS;
152 return CaseMap::NONE;
155 /** converts a paragraph align to a ParaAdjust */
156 sal_Int16 GetParaAdjust( sal_Int32 nAlign )
158 OSL_ASSERT((nAlign & sal_Int32(0xFFFF0000))==0);
159 sal_Int16 nEnum;
160 switch( nAlign )
162 case XML_ctr:
163 nEnum = ParagraphAdjust_CENTER;
164 break;
165 case XML_just:
166 case XML_justLow:
167 nEnum = ParagraphAdjust_BLOCK;
168 break;
169 case XML_r:
170 nEnum = ParagraphAdjust_RIGHT;
171 break;
172 case XML_thaiDist:
173 case XML_dist:
174 nEnum = ParagraphAdjust_STRETCH;
175 break;
176 case XML_l:
177 default:
178 nEnum = ParagraphAdjust_LEFT;
179 break;
181 return nEnum;
184 TextVerticalAdjust GetTextVerticalAdjust( sal_Int32 nToken )
186 TextVerticalAdjust aVertAdjust;
187 switch( nToken )
189 case XML_b:
190 aVertAdjust = TextVerticalAdjust_BOTTOM;
191 break;
192 case XML_dist:
193 case XML_just:
194 case XML_ctr:
195 aVertAdjust = TextVerticalAdjust_CENTER;
196 break;
197 case XML_t:
198 default:
199 aVertAdjust = TextVerticalAdjust_TOP;
200 break;
202 return aVertAdjust;
205 const char* GetTextVerticalAdjust( TextVerticalAdjust eAdjust )
207 const char* sVerticalAdjust = 0;
208 switch( eAdjust )
210 case TextVerticalAdjust_BOTTOM:
211 sVerticalAdjust = "b";
212 break;
213 case TextVerticalAdjust_CENTER:
214 sVerticalAdjust = "ctr";
215 break;
216 case TextVerticalAdjust_TOP:
217 default:
218 sVerticalAdjust = "t";
219 break;
221 return sVerticalAdjust;
224 TabAlign GetTabAlign( sal_Int32 aToken )
226 OSL_ASSERT((aToken & sal_Int32(0xFFFF0000))==0);
227 TabAlign nEnum;
228 switch( aToken )
230 case XML_ctr:
231 nEnum = TabAlign_CENTER;
232 break;
233 case XML_dec:
234 nEnum = TabAlign_DECIMAL;
235 break;
236 case XML_l:
237 nEnum = TabAlign_LEFT;
238 break;
239 case XML_r:
240 nEnum = TabAlign_RIGHT;
241 break;
242 default:
243 nEnum = TabAlign_DEFAULT;
244 break;
246 return nEnum;
249 const char* GetHatchPattern( const drawing::Hatch& rHatch )
251 const char* sPattern = 0;
252 const sal_Int32 nAngle = rHatch.Angle > 1800 ? rHatch.Angle - 1800 : rHatch.Angle;
253 // Angle ~ 0° (horizontal)
254 if( (nAngle >= 0 && nAngle < 225) || nAngle >= 1575 )
256 switch( rHatch.Style )
258 case drawing::HatchStyle_SINGLE:
260 if( rHatch.Distance < 75 )
261 sPattern = "ltHorz";
262 else
263 sPattern = "horz";
265 break;
267 case drawing::HatchStyle_DOUBLE:
268 case drawing::HatchStyle_TRIPLE:
270 if( rHatch.Distance < 75 )
271 sPattern = "smGrid";
272 else
273 sPattern = "lgGrid";
275 break;
277 default: break;
280 // Angle ~ 45° (upward diagonal)
281 else if( nAngle < 675 )
283 switch( rHatch.Style )
285 case drawing::HatchStyle_SINGLE:
287 if( rHatch.Distance < 75 )
288 sPattern = "ltUpDiag";
289 else
290 sPattern = "wdUpDiag";
292 break;
294 case drawing::HatchStyle_DOUBLE:
295 case drawing::HatchStyle_TRIPLE:
297 if( rHatch.Distance < 75 )
298 sPattern = "smCheck";
299 else
300 sPattern = "openDmnd";
302 break;
304 default: break;
307 // Angle ~ 90° (vertical)
308 else if( nAngle < 1125 )
310 switch( rHatch.Style )
312 case drawing::HatchStyle_SINGLE:
314 // dkVert is imported as Distance = 25, ltVert as Distance = 50, export them accordingly.
315 if( rHatch.Distance < 50 )
316 sPattern = "dkVert";
317 else if( rHatch.Distance < 75 )
318 sPattern = "ltVert";
319 else
320 sPattern = "vert";
322 break;
324 case drawing::HatchStyle_DOUBLE:
325 case drawing::HatchStyle_TRIPLE:
327 if( rHatch.Distance < 75 )
328 sPattern = "smGrid";
329 else
330 sPattern = "lgGrid";
332 break;
334 default: break;
337 // Angle ~ 135° (downward diagonal)
338 else if( nAngle < 1575 )
340 switch( rHatch.Style )
342 case drawing::HatchStyle_SINGLE:
344 if( rHatch.Distance < 75 )
345 sPattern = "ltDnDiag";
346 else
347 sPattern = "wdDnDiag";
349 break;
351 case drawing::HatchStyle_DOUBLE:
352 case drawing::HatchStyle_TRIPLE:
354 if( rHatch.Distance < 75 )
355 sPattern = "smCheck";
356 else
357 sPattern = "openDmnd";
359 break;
361 default: break;
364 return sPattern;
367 /** converts the attributes from a CT_RelativeRect to an IntegerRectangle2D */
368 IntegerRectangle2D GetRelativeRect( const Reference< XFastAttributeList >& xAttribs )
370 IntegerRectangle2D r;
372 r.X1 = xAttribs->getOptionalValue( XML_l ).toInt32();
373 r.Y1 = xAttribs->getOptionalValue( XML_t ).toInt32();
374 r.X2 = xAttribs->getOptionalValue( XML_r ).toInt32();
375 r.Y2 = xAttribs->getOptionalValue( XML_b ).toInt32();
377 return r;
380 /** converts the attributes from an CT_Size2D into an awt Size with 1/100thmm */
381 awt::Size GetSize2D( const Reference< XFastAttributeList >& xAttribs )
383 return awt::Size( GetCoordinate( xAttribs->getOptionalValue( XML_cx ) ), GetCoordinate( xAttribs->getOptionalValue( XML_cy ) ) );
386 IndexRange GetIndexRange( const Reference< XFastAttributeList >& xAttributes )
388 IndexRange range;
389 range.start = xAttributes->getOptionalValue( XML_st ).toInt32();
390 range.end = xAttributes->getOptionalValue( XML_end ).toInt32();
391 return range;
394 } // namespace drawingml
395 } // namespace oox
397 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */