bump product version to 4.2.0.1
[LibreOffice.git] / oox / source / vml / vmlformatting.cxx
blob25caa60a9666a1ddc461021373c47d3b0f7360bd
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 .
19 #include "oox/vml/vmlformatting.hxx"
21 #include <com/sun/star/beans/PropertyValue.hpp>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/drawing/EnhancedCustomShapeTextPathMode.hpp>
24 #include <com/sun/star/table/ShadowFormat.hpp>
25 #include <com/sun/star/text/XTextRange.hpp>
26 #include <rtl/strbuf.hxx>
27 #include "oox/drawingml/color.hxx"
28 #include "oox/drawingml/drawingmltypes.hxx"
29 #include "oox/drawingml/fillproperties.hxx"
30 #include "oox/drawingml/lineproperties.hxx"
31 #include "oox/drawingml/shapepropertymap.hxx"
32 #include "oox/helper/attributelist.hxx"
33 #include "oox/helper/graphichelper.hxx"
35 namespace oox {
36 namespace vml {
38 // ============================================================================
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::geometry;
43 using ::oox::drawingml::Color;
44 using ::oox::drawingml::FillProperties;
45 using ::oox::drawingml::LineArrowProperties;
46 using ::oox::drawingml::LineProperties;
47 using ::oox::drawingml::ShapePropertyMap;
48 using ::com::sun::star::awt::Point;
49 using ::com::sun::star::drawing::PolygonFlags;
50 using ::com::sun::star::drawing::PolygonFlags_NORMAL;
51 using ::com::sun::star::drawing::PolygonFlags_CONTROL;
53 // ============================================================================
55 namespace {
57 bool lclExtractDouble( double& orfValue, sal_Int32& ornEndPos, const OUString& rValue )
59 // extract the double value and find start position of unit characters
60 rtl_math_ConversionStatus eConvStatus = rtl_math_ConversionStatus_Ok;
61 orfValue = ::rtl::math::stringToDouble( rValue, '.', '\0', &eConvStatus, &ornEndPos );
62 return eConvStatus == rtl_math_ConversionStatus_Ok;
65 } // namespace
67 // ----------------------------------------------------------------------------
69 bool ConversionHelper::separatePair( OUString& orValue1, OUString& orValue2,
70 const OUString& rValue, sal_Unicode cSep )
72 sal_Int32 nSepPos = rValue.indexOf( cSep );
73 if( nSepPos >= 0 )
75 orValue1 = rValue.copy( 0, nSepPos ).trim();
76 orValue2 = rValue.copy( nSepPos + 1 ).trim();
78 else
80 orValue1 = rValue.trim();
82 return !orValue1.isEmpty() && !orValue2.isEmpty();
85 bool ConversionHelper::decodeBool( const OUString& rValue )
87 sal_Int32 nToken = AttributeConversion::decodeToken( rValue );
88 // anything else than 't' or 'true' is considered to be false, as specified
89 return (nToken == XML_t) || (nToken == XML_true);
92 double ConversionHelper::decodePercent( const OUString& rValue, double fDefValue )
94 if( rValue.isEmpty() )
95 return fDefValue;
97 double fValue = 0.0;
98 sal_Int32 nEndPos = 0;
99 if( !lclExtractDouble( fValue, nEndPos, rValue ) )
100 return fDefValue;
102 if( nEndPos == rValue.getLength() )
103 return fValue;
105 if( (nEndPos + 1 == rValue.getLength()) && (rValue[ nEndPos ] == '%') )
106 return fValue / 100.0;
108 if( (nEndPos + 1 == rValue.getLength()) && (rValue[ nEndPos ] == 'f') )
109 return fValue / 65536.0;
111 OSL_FAIL( "ConversionHelper::decodePercent - unknown measure unit" );
112 return fDefValue;
115 sal_Int64 ConversionHelper::decodeMeasureToEmu( const GraphicHelper& rGraphicHelper,
116 const OUString& rValue, sal_Int32 nRefValue, bool bPixelX, bool bDefaultAsPixel )
118 // default for missing values is 0
119 if( rValue.isEmpty() )
120 return 0;
122 // TODO: according to spec, value may contain "auto"
123 if ( rValue == "auto" )
125 OSL_FAIL( "ConversionHelper::decodeMeasureToEmu - special value 'auto' must be handled by caller" );
126 return nRefValue;
129 // extract the double value and find start position of unit characters
130 double fValue = 0.0;
131 sal_Int32 nEndPos = 0;
132 if( !lclExtractDouble( fValue, nEndPos, rValue ) || (fValue == 0.0) )
133 return 0;
135 // process trailing unit, convert to EMU
136 OUString aUnit;
137 if( (0 < nEndPos) && (nEndPos < rValue.getLength()) )
138 aUnit = rValue.copy( nEndPos );
139 else if( bDefaultAsPixel )
140 aUnit = "px";
141 // else default is EMU
143 if( aUnit.getLength() == 2 )
145 sal_Unicode cChar1 = aUnit[ 0 ];
146 sal_Unicode cChar2 = aUnit[ 1 ];
147 if( (cChar1 == 'i') && (cChar2 == 'n') ) // 1 inch = 914,400 EMU
148 fValue *= 914400.0;
149 else if( (cChar1 == 'c') && (cChar2 == 'm') ) // 1 cm = 360,000 EMU
150 fValue *= 360000.0;
151 else if( (cChar1 == 'm') && (cChar2 == 'm') ) // 1 mm = 36,000 EMU
152 fValue *= 36000.0;
153 else if( (cChar1 == 'p') && (cChar2 == 't') ) // 1 point = 1/72 inch = 12,700 EMU
154 fValue *= 12700.0;
155 else if( (cChar1 == 'p') && (cChar2 == 'c') ) // 1 pica = 1/6 inch = 152,400 EMU
156 fValue *= 152400.0;
157 else if( (cChar1 == 'p') && (cChar2 == 'x') ) // 1 pixel, dependent on output device
158 fValue = static_cast< double >( ::oox::drawingml::convertHmmToEmu(
159 bPixelX ?
160 rGraphicHelper.convertScreenPixelXToHmm( fValue ) :
161 rGraphicHelper.convertScreenPixelYToHmm( fValue ) ) );
163 else if( (aUnit.getLength() == 1) && (aUnit[ 0 ] == '%') )
165 fValue *= nRefValue / 100.0;
167 else if( bDefaultAsPixel || !aUnit.isEmpty() ) // default as EMU and no unit -> do nothing
169 OSL_FAIL( "ConversionHelper::decodeMeasureToEmu - unknown measure unit" );
170 fValue = nRefValue;
172 return static_cast< sal_Int64 >( fValue + 0.5 );
175 sal_Int32 ConversionHelper::decodeMeasureToHmm( const GraphicHelper& rGraphicHelper,
176 const OUString& rValue, sal_Int32 nRefValue, bool bPixelX, bool bDefaultAsPixel )
178 return ::oox::drawingml::convertEmuToHmm( decodeMeasureToEmu( rGraphicHelper, rValue, nRefValue, bPixelX, bDefaultAsPixel ) );
181 Color ConversionHelper::decodeColor( const GraphicHelper& rGraphicHelper,
182 const OptValue< OUString >& roVmlColor, const OptValue< double >& roVmlOpacity,
183 sal_Int32 nDefaultRgb, sal_Int32 nPrimaryRgb )
185 Color aDmlColor;
187 // convert opacity
188 const sal_Int32 DML_FULL_OPAQUE = ::oox::drawingml::MAX_PERCENT;
189 double fOpacity = roVmlOpacity.get( 1.0 );
190 sal_Int32 nOpacity = getLimitedValue< sal_Int32, double >( fOpacity * DML_FULL_OPAQUE, 0, DML_FULL_OPAQUE );
191 if( nOpacity < DML_FULL_OPAQUE )
192 aDmlColor.addTransformation( XML_alpha, nOpacity );
194 // color attribute not present - set passed default color
195 if( !roVmlColor.has() )
197 aDmlColor.setSrgbClr( nDefaultRgb );
198 return aDmlColor;
201 // separate leading color name or RGB value from following palette index
202 OUString aColorName, aColorIndex;
203 separatePair( aColorName, aColorIndex, roVmlColor.get(), ' ' );
205 // RGB colors in the format '#RRGGBB'
206 if( (aColorName.getLength() == 7) && (aColorName[ 0 ] == '#') )
208 aDmlColor.setSrgbClr( aColorName.copy( 1 ).toUInt32( 16 ) );
209 return aDmlColor;
212 // RGB colors in the format '#RGB'
213 if( (aColorName.getLength() == 4) && (aColorName[ 0 ] == '#') )
215 sal_Int32 nR = aColorName.copy( 1, 1 ).toUInt32( 16 ) * 0x11;
216 sal_Int32 nG = aColorName.copy( 2, 1 ).toUInt32( 16 ) * 0x11;
217 sal_Int32 nB = aColorName.copy( 3, 1 ).toUInt32( 16 ) * 0x11;
218 aDmlColor.setSrgbClr( (nR << 16) | (nG << 8) | nB );
219 return aDmlColor;
222 /* Predefined color names or system color names (resolve to RGB to detect
223 valid color name). */
224 sal_Int32 nColorToken = AttributeConversion::decodeToken( aColorName );
225 sal_Int32 nRgbValue = Color::getVmlPresetColor( nColorToken, API_RGB_TRANSPARENT );
226 if( nRgbValue == API_RGB_TRANSPARENT )
227 nRgbValue = rGraphicHelper.getSystemColor( nColorToken, API_RGB_TRANSPARENT );
228 if( nRgbValue != API_RGB_TRANSPARENT )
230 aDmlColor.setSrgbClr( nRgbValue );
231 return aDmlColor;
234 // try palette colors enclosed in brackets
235 if( (aColorIndex.getLength() >= 3) && (aColorIndex[ 0 ] == '[') && (aColorIndex[ aColorIndex.getLength() - 1 ] == ']') )
237 aDmlColor.setPaletteClr( aColorIndex.copy( 1, aColorIndex.getLength() - 2 ).toInt32() );
238 return aDmlColor;
241 // try fill gradient modificator 'fill <modifier>(<amount>)'
242 if( (nPrimaryRgb != API_RGB_TRANSPARENT) && (nColorToken == XML_fill) )
244 sal_Int32 nOpenParen = aColorIndex.indexOf( '(' );
245 sal_Int32 nCloseParen = aColorIndex.indexOf( ')' );
246 if( (2 <= nOpenParen) && (nOpenParen + 1 < nCloseParen) && (nCloseParen + 1 == aColorIndex.getLength()) )
248 sal_Int32 nModToken = XML_TOKEN_INVALID;
249 switch( AttributeConversion::decodeToken( aColorIndex.copy( 0, nOpenParen ) ) )
251 case XML_darken: nModToken = XML_shade;break;
252 case XML_lighten: nModToken = XML_tint;
254 sal_Int32 nValue = aColorIndex.copy( nOpenParen + 1, nCloseParen - nOpenParen - 1 ).toInt32();
255 if( (nModToken != XML_TOKEN_INVALID) && (0 <= nValue) && (nValue < 255) )
257 /* Simulate this modifier color by a color with related transformation.
258 The modifier amount has to be converted from the range [0;255] to
259 percentage [0;100000] used by DrawingML. */
260 aDmlColor.setSrgbClr( nPrimaryRgb );
261 aDmlColor.addTransformation( nModToken, static_cast< sal_Int32 >( nValue * ::oox::drawingml::MAX_PERCENT / 255 ) );
262 return aDmlColor;
267 OSL_FAIL( OStringBuffer( "lclGetColor - invalid VML color name '" ).
268 append( OUStringToOString( roVmlColor.get(), RTL_TEXTENCODING_ASCII_US ) ).append( '\'' ).getStr() );
269 aDmlColor.setSrgbClr( nDefaultRgb );
270 return aDmlColor;
273 void ConversionHelper::decodeVmlPath( ::std::vector< ::std::vector< Point > >& rPointLists, ::std::vector< ::std::vector< PolygonFlags > >& rFlagLists, const OUString& rPath )
275 ::std::vector< sal_Int32 > aCoordList;
276 Point aCurrentPoint;
277 sal_Int32 nTokenStart = 0;
278 sal_Int32 nTokenLen = 0;
279 sal_Int32 nParamCount = 0;
280 bool bCommand = false;
281 enum VML_State { START, MOVE_REL, MOVE_ABS, BEZIER_REL, BEZIER_ABS,
282 LINE_REL, LINE_ABS, CLOSE, END, UNSUPPORTED };
283 VML_State state = START;
285 rPointLists.push_back( ::std::vector< Point>() );
286 rFlagLists.push_back( ::std::vector< PolygonFlags >() );
288 for ( sal_Int32 i = 0; i < rPath.getLength(); i++ )
290 // Keep track of current integer token
291 if ( ( rPath[ i ] >= '0' && rPath[ i ] <= '9' ) || rPath[ i ] == '-' )
292 nTokenLen++;
293 else if ( rPath[ i ] != ' ' )
295 // Store coordinate from current token
296 if ( state != START && state != UNSUPPORTED )
298 if ( nTokenLen > 0 )
299 aCoordList.push_back( rPath.copy( nTokenStart, nTokenLen ).toInt32() );
300 else
301 aCoordList.push_back( 0 );
302 nTokenLen = 0;
305 if (rPath[ i ] == ',' )
307 nParamCount--;
310 // Upon finding the next command code, deal with stored
311 // coordinates for previous command and reset parameters counter if needed.
312 // See http://www.w3.org/TR/NOTE-VML#_Toc416858382 for params count reference
313 if ( rPath[ i ] != ',' || nParamCount == 0 )
315 switch ( state )
317 case MOVE_REL: // 2* params -> param count reset
318 if ( rPointLists.size() > 0 && rPointLists.back().size() > 0 )
320 rPointLists.push_back( ::std::vector< Point >() );
321 rFlagLists.push_back( ::std::vector< PolygonFlags >() );
323 rPointLists.back().push_back( Point( aCoordList[ 0 ], aCoordList[ 1 ] ) );
324 rFlagLists.back().push_back( PolygonFlags_NORMAL );
325 aCurrentPoint = rPointLists.back().back();
326 nParamCount = 2;
327 break;
329 case MOVE_ABS: // 2 params -> no param count reset
330 if ( rPointLists.size() > 0 && rPointLists.back().size() > 0 )
332 rPointLists.push_back( ::std::vector< Point >() );
333 rFlagLists.push_back( ::std::vector< PolygonFlags >() );
335 rPointLists.back().push_back( Point( aCoordList[ 0 ], aCoordList[ 1 ] ) );
336 rFlagLists.back().push_back( PolygonFlags_NORMAL );
337 aCurrentPoint = rPointLists.back().back();
338 break;
340 case BEZIER_REL: // 6* params -> param count reset
341 rPointLists.back().push_back( Point( aCurrentPoint.X + aCoordList[ 0 ],
342 aCurrentPoint.Y + aCoordList[ 1 ] ) );
343 rPointLists.back().push_back( Point( aCurrentPoint.X + aCoordList[ 2 ],
344 aCurrentPoint.Y + aCoordList[ 3 ] ) );
345 rPointLists.back().push_back( Point( aCurrentPoint.X + aCoordList[ 4 ],
346 aCurrentPoint.Y + aCoordList[ 5 ] ) );
347 rFlagLists.back().push_back( PolygonFlags_CONTROL );
348 rFlagLists.back().push_back( PolygonFlags_CONTROL );
349 rFlagLists.back().push_back( PolygonFlags_NORMAL );
350 aCurrentPoint = rPointLists.back().back();
351 nParamCount = 6;
352 break;
354 case BEZIER_ABS: // 6* params -> param count reset
355 rPointLists.back().push_back( Point( aCoordList[ 0 ], aCoordList[ 1 ] ) );
356 rPointLists.back().push_back( Point( aCoordList[ 2 ], aCoordList[ 3 ] ) );
357 rPointLists.back().push_back( Point( aCoordList[ 4 ], aCoordList[ 5 ] ) );
358 rFlagLists.back().push_back( PolygonFlags_CONTROL );
359 rFlagLists.back().push_back( PolygonFlags_CONTROL );
360 rFlagLists.back().push_back( PolygonFlags_NORMAL );
361 aCurrentPoint = rPointLists.back().back();
362 nParamCount = 6;
363 break;
365 case LINE_REL: // 2* params -> param count reset
366 rPointLists.back().push_back( Point( aCurrentPoint.X + aCoordList[ 0 ],
367 aCurrentPoint.Y + aCoordList[ 1 ] ) );
368 rFlagLists.back().push_back( PolygonFlags_NORMAL );
369 aCurrentPoint = rPointLists.back().back();
370 nParamCount = 2;
371 break;
373 case LINE_ABS: // 2* params -> param count reset
374 rPointLists.back().push_back( Point( aCoordList[ 0 ], aCoordList[ 1 ] ) );
375 rFlagLists.back().push_back( PolygonFlags_NORMAL );
376 aCurrentPoint = rPointLists.back().back();
377 nParamCount = 2;
378 break;
380 case CLOSE: // 0 param
381 rPointLists.back().push_back( rPointLists.back()[ 0 ] );
382 rFlagLists.back().push_back( rFlagLists.back()[ 0 ] );
383 aCurrentPoint = rPointLists.back().back();
384 break;
386 case END: // 0 param
387 rPointLists.push_back( ::std::vector< Point >() );
388 rFlagLists.push_back( ::std::vector< PolygonFlags >() );
389 break;
391 case START:
392 case UNSUPPORTED:
393 break;
396 aCoordList.clear();
399 // Allow two-char commands to peek ahead to the next character
400 char nextChar = '\0';
401 if (i+1 < rPath.getLength())
402 nextChar = rPath[i+1];
404 // Move to relevant state upon finding a command
405 bCommand = true;
406 switch ( rPath[ i ] )
408 // Single-character commands
409 case 't': // rmoveto
410 state = MOVE_REL; nParamCount = 2; break;
411 case 'm': // moveto
412 state = MOVE_ABS; nParamCount = 2; break;
413 case 'v': // rcurveto
414 state = BEZIER_REL; nParamCount = 6; break;
415 case 'c': // curveto
416 state = BEZIER_ABS; nParamCount = 6; break;
417 case 'r': // rlineto
418 state = LINE_REL; nParamCount = 2; break;
419 case 'l': // lineto
420 state = LINE_ABS; nParamCount = 2; break;
421 case 'x': // close
422 state = CLOSE; break;
423 case 'e': // end
424 state = END; break;
426 // Two-character commands
427 case 'n':
429 switch ( nextChar )
431 case 'f': // nf - nofill
432 case 's': // ns - nostroke
433 state = UNSUPPORTED; i++; break;
435 break;
437 case 'a': // Elliptical curves
439 switch ( nextChar )
441 case 'e': // ae - angleellipseto
442 case 'l': // al - angleellipse
443 state = UNSUPPORTED; i++; break;
444 case 't': // at - arcto
445 case 'r': // ar - arc
446 state = UNSUPPORTED; i++; break;
448 break;
450 case 'w': // Clockwise elliptical arcs
452 switch ( nextChar )
454 case 'a': // wa - clockwisearcto
455 case 'r': // wr - clockwisearc
456 state = UNSUPPORTED; i++; break;
458 break;
460 case 'q':
462 switch ( nextChar )
464 case 'x': // qx - ellipticalquadrantx
465 case 'y': // qy - ellipticalquadranty
466 state = UNSUPPORTED; i++; break;
467 case 'b': // qb - quadraticbezier
468 state = UNSUPPORTED; i++; break;
470 break;
472 case 'h': // behaviour extensions
474 switch ( nextChar )
476 case 'a': // ha - AutoLine
477 case 'b': // hb - AutoCurve
478 case 'c': // hc - CornerLine
479 case 'd': // hd - CornerCurve
480 case 'e': // he - SmoothLine
481 case 'f': // hf - SmoothCurve
482 case 'g': // hg - SymmetricLine
483 case 'h': // hh - SymmetricCurve
484 case 'i': // hi - Freeform
485 state = UNSUPPORTED; i++; break;
487 break;
489 default:
490 bCommand = false;
491 break;
494 if (bCommand) nTokenLen = 0;
495 nTokenStart = i+1;
500 // ============================================================================
502 namespace {
504 sal_Int64 lclGetEmu( const GraphicHelper& rGraphicHelper, const OptValue< OUString >& roValue, sal_Int64 nDefValue )
506 return roValue.has() ? ConversionHelper::decodeMeasureToEmu( rGraphicHelper, roValue.get(), 0, false, false ) : nDefValue;
509 void lclGetDmlLineDash( OptValue< sal_Int32 >& oroPresetDash, LineProperties::DashStopVector& orCustomDash, const OptValue< OUString >& roDashStyle )
511 if( roDashStyle.has() )
513 const OUString& rDashStyle = roDashStyle.get();
514 switch( AttributeConversion::decodeToken( rDashStyle ) )
516 case XML_solid: oroPresetDash = XML_solid; return;
517 case XML_shortdot: oroPresetDash = XML_sysDot; return;
518 case XML_shortdash: oroPresetDash = XML_sysDash; return;
519 case XML_shortdashdot: oroPresetDash = XML_sysDashDot; return;
520 case XML_shortdashdotdot: oroPresetDash = XML_sysDashDotDot; return;
521 case XML_dot: oroPresetDash = XML_dot; return;
522 case XML_dash: oroPresetDash = XML_dash; return;
523 case XML_dashdot: oroPresetDash = XML_dashDot; return;
524 case XML_longdash: oroPresetDash = XML_lgDash; return;
525 case XML_longdashdot: oroPresetDash = XML_lgDashDot; return;
526 case XML_longdashdotdot: oroPresetDash = XML_lgDashDotDot; return;
528 // try to convert user-defined dash style
529 default:
531 ::std::vector< sal_Int32 > aValues;
532 sal_Int32 nIndex = 0;
533 while( nIndex >= 0 )
534 aValues.push_back( rDashStyle.getToken( 0, ' ', nIndex ).toInt32() );
535 size_t nPairs = aValues.size() / 2; // ignore last value if size is odd
536 for( size_t nPairIdx = 0; nPairIdx < nPairs; ++nPairIdx )
537 orCustomDash.push_back( LineProperties::DashStop( aValues[ 2 * nPairIdx ], aValues[ 2 * nPairIdx + 1 ] ) );
543 sal_Int32 lclGetDmlArrowType( const OptValue< sal_Int32 >& roArrowType )
545 if( roArrowType.has() ) switch( roArrowType.get() )
547 case XML_none: return XML_none;
548 case XML_block: return XML_triangle;
549 case XML_classic: return XML_stealth;
550 case XML_diamond: return XML_diamond;
551 case XML_oval: return XML_oval;
552 case XML_open: return XML_arrow;
554 return XML_none;
557 sal_Int32 lclGetDmlArrowWidth( const OptValue< sal_Int32 >& roArrowWidth )
559 if( roArrowWidth.has() ) switch( roArrowWidth.get() )
561 case XML_narrow: return XML_sm;
562 case XML_medium: return XML_med;
563 case XML_wide: return XML_lg;
565 return XML_med;
568 sal_Int32 lclGetDmlArrowLength( const OptValue< sal_Int32 >& roArrowLength )
570 if( roArrowLength.has() ) switch( roArrowLength.get() )
572 case XML_short: return XML_sm;
573 case XML_medium: return XML_med;
574 case XML_long: return XML_lg;
576 return XML_med;
579 void lclConvertArrow( LineArrowProperties& orArrowProp, const StrokeArrowModel& rStrokeArrow )
581 orArrowProp.moArrowType = lclGetDmlArrowType( rStrokeArrow.moArrowType );
582 orArrowProp.moArrowWidth = lclGetDmlArrowWidth( rStrokeArrow.moArrowWidth );
583 orArrowProp.moArrowLength = lclGetDmlArrowLength( rStrokeArrow.moArrowLength );
586 sal_Int32 lclGetDmlLineCompound( const OptValue< sal_Int32 >& roLineStyle )
588 if( roLineStyle.has() ) switch( roLineStyle.get() )
590 case XML_single: return XML_sng;
591 case XML_thinThin: return XML_dbl;
592 case XML_thinThick: return XML_thinThick;
593 case XML_thickThin: return XML_thickThin;
594 case XML_thickBetweenThin: return XML_tri;
596 return XML_sng;
599 sal_Int32 lclGetDmlLineCap( const OptValue< sal_Int32 >& roEndCap )
601 if( roEndCap.has() ) switch( roEndCap.get() )
603 case XML_flat: return XML_flat;
604 case XML_square: return XML_sq;
605 case XML_round: return XML_rnd;
607 return XML_flat; // different defaults in VML (flat) and DrawingML (square)
610 sal_Int32 lclGetDmlLineJoint( const OptValue< sal_Int32 >& roJoinStyle )
612 if( roJoinStyle.has() ) switch( roJoinStyle.get() )
614 case XML_round: return XML_round;
615 case XML_bevel: return XML_bevel;
616 case XML_miter: return XML_miter;
618 return XML_round;
621 } // namespace
623 // ============================================================================
625 void StrokeArrowModel::assignUsed( const StrokeArrowModel& rSource )
627 moArrowType.assignIfUsed( rSource.moArrowType );
628 moArrowWidth.assignIfUsed( rSource.moArrowWidth );
629 moArrowLength.assignIfUsed( rSource.moArrowLength );
632 // ============================================================================
634 void StrokeModel::assignUsed( const StrokeModel& rSource )
636 moStroked.assignIfUsed( rSource.moStroked );
637 maStartArrow.assignUsed( rSource.maStartArrow );
638 maEndArrow.assignUsed( rSource.maEndArrow );
639 moColor.assignIfUsed( rSource.moColor );
640 moOpacity.assignIfUsed( rSource.moOpacity );
641 moWeight.assignIfUsed( rSource.moWeight );
642 moDashStyle.assignIfUsed( rSource.moDashStyle );
643 moLineStyle.assignIfUsed( rSource.moLineStyle );
644 moEndCap.assignIfUsed( rSource.moEndCap );
645 moJoinStyle.assignIfUsed( rSource.moJoinStyle );
648 void StrokeModel::pushToPropMap( ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper ) const
650 /* Convert VML line formatting to DrawingML line formatting and let the
651 DrawingML code do the hard work. */
652 LineProperties aLineProps;
654 if( moStroked.get( true ) )
656 aLineProps.maLineFill.moFillType = XML_solidFill;
657 lclConvertArrow( aLineProps.maStartArrow, maStartArrow );
658 lclConvertArrow( aLineProps.maEndArrow, maEndArrow );
659 aLineProps.maLineFill.maFillColor = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_BLACK );
660 aLineProps.moLineWidth = getLimitedValue< sal_Int32, sal_Int64 >( lclGetEmu( rGraphicHelper, moWeight, 1 ), 0, SAL_MAX_INT32 );
661 lclGetDmlLineDash( aLineProps.moPresetDash, aLineProps.maCustomDash, moDashStyle );
662 aLineProps.moLineCompound = lclGetDmlLineCompound( moLineStyle );
663 aLineProps.moLineCap = lclGetDmlLineCap( moEndCap );
664 aLineProps.moLineJoint = lclGetDmlLineJoint( moJoinStyle );
666 else
668 aLineProps.maLineFill.moFillType = XML_noFill;
671 aLineProps.pushToPropMap( rPropMap, rGraphicHelper );
674 // ============================================================================
676 void FillModel::assignUsed( const FillModel& rSource )
678 moFilled.assignIfUsed( rSource.moFilled );
679 moColor.assignIfUsed( rSource.moColor );
680 moOpacity.assignIfUsed( rSource.moOpacity );
681 moColor2.assignIfUsed( rSource.moColor2 );
682 moOpacity2.assignIfUsed( rSource.moOpacity2 );
683 moType.assignIfUsed( rSource.moType );
684 moAngle.assignIfUsed( rSource.moAngle );
685 moFocus.assignIfUsed( rSource.moFocus );
686 moFocusPos.assignIfUsed( rSource.moFocusPos );
687 moFocusSize.assignIfUsed( rSource.moFocusSize );
688 moBitmapPath.assignIfUsed( rSource.moBitmapPath );
689 moRotate.assignIfUsed( rSource.moRotate );
692 void FillModel::pushToPropMap( ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper ) const
694 /* Convert VML fill formatting to DrawingML fill formatting and let the
695 DrawingML code do the hard work. */
696 FillProperties aFillProps;
698 if( moFilled.get( true ) )
700 sal_Int32 nFillType = moType.get( XML_solid );
701 switch( nFillType )
703 case XML_gradient:
704 case XML_gradientRadial:
706 aFillProps.moFillType = XML_gradFill;
707 aFillProps.maGradientProps.moRotateWithShape = moRotate.get( false );
708 double fFocus = moFocus.get( 0.0 );
710 // prepare colors
711 Color aColor1 = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_WHITE );
712 Color aColor2 = ConversionHelper::decodeColor( rGraphicHelper, moColor2, moOpacity2, API_RGB_WHITE, aColor1.getColor( rGraphicHelper ) );
714 // type XML_gradient is linear or axial gradient
715 if( nFillType == XML_gradient )
717 // normalize angle to range [0;360) degrees
718 sal_Int32 nVmlAngle = getIntervalValue< sal_Int32, sal_Int32 >( moAngle.get( 0 ), 0, 360 );
720 // focus of -50% or 50% is axial gradient
721 if( ((-0.75 <= fFocus) && (fFocus <= -0.25)) || ((0.25 <= fFocus) && (fFocus <= 0.75)) )
723 /* According to spec, focus of 50% is outer-to-inner,
724 and -50% is inner-to-outer (color to color2).
725 BUT: For angles >= 180 deg., the behaviour is
726 reversed... that's not spec'ed of course. So,
727 [0;180) deg. and 50%, or [180;360) deg. and -50% is
728 outer-to-inner in fact. */
729 bool bOuterToInner = (fFocus > 0.0) == (nVmlAngle < 180);
730 // simulate axial gradient by 3-step DrawingML gradient
731 const Color& rOuterColor = bOuterToInner ? aColor1 : aColor2;
732 const Color& rInnerColor = bOuterToInner ? aColor2 : aColor1;
733 aFillProps.maGradientProps.maGradientStops[ 0.0 ] = aFillProps.maGradientProps.maGradientStops[ 1.0 ] = rOuterColor;
734 aFillProps.maGradientProps.maGradientStops[ 0.5 ] = rInnerColor;
736 else // focus of -100%, 0%, and 100% is linear gradient
738 /* According to spec, focus of -100% or 100% swaps the
739 start and stop colors, effectively reversing the
740 gradient. BUT: For angles >= 180 deg., the
741 behaviour is reversed. This means that in this case
742 a focus of 0% swaps the gradient. */
743 if( fFocus < -0.5 || fFocus > 0.5 )
744 (nVmlAngle += 180) %= 360;
745 // set the start and stop colors
746 aFillProps.maGradientProps.maGradientStops[ 0.0 ] = aColor1;
747 aFillProps.maGradientProps.maGradientStops[ 1.0 ] = aColor2;
750 // VML counts counterclockwise from bottom, DrawingML clockwise from left
751 sal_Int32 nDmlAngle = (630 - nVmlAngle) % 360;
752 aFillProps.maGradientProps.moShadeAngle = nDmlAngle * ::oox::drawingml::PER_DEGREE;
754 else // XML_gradientRadial is rectangular gradient
756 aFillProps.maGradientProps.moGradientPath = XML_rect;
757 // convert VML focus position and size to DrawingML fill-to-rect
758 DoublePair aFocusPos = moFocusPos.get( DoublePair( 0.0, 0.0 ) );
759 DoublePair aFocusSize = moFocusSize.get( DoublePair( 0.0, 0.0 ) );
760 double fLeft = getLimitedValue< double, double >( aFocusPos.first, 0.0, 1.0 );
761 double fTop = getLimitedValue< double, double >( aFocusPos.second, 0.0, 1.0 );
762 double fRight = getLimitedValue< double, double >( fLeft + aFocusSize.first, fLeft, 1.0 );
763 double fBottom = getLimitedValue< double, double >( fTop + aFocusSize.second, fTop, 1.0 );
764 aFillProps.maGradientProps.moFillToRect = IntegerRectangle2D(
765 static_cast< sal_Int32 >( fLeft * ::oox::drawingml::MAX_PERCENT ),
766 static_cast< sal_Int32 >( fTop * ::oox::drawingml::MAX_PERCENT ),
767 static_cast< sal_Int32 >( (1.0 - fRight) * ::oox::drawingml::MAX_PERCENT ),
768 static_cast< sal_Int32 >( (1.0 - fBottom) * ::oox::drawingml::MAX_PERCENT ) );
770 // set the start and stop colors (focus of 0% means outer-to-inner)
771 bool bOuterToInner = (-0.5 <= fFocus) && (fFocus <= 0.5);
772 aFillProps.maGradientProps.maGradientStops[ 0.0 ] = bOuterToInner ? aColor2 : aColor1;
773 aFillProps.maGradientProps.maGradientStops[ 1.0 ] = bOuterToInner ? aColor1 : aColor2;
776 break;
778 case XML_pattern:
779 case XML_tile:
780 case XML_frame:
782 if( moBitmapPath.has() && !moBitmapPath.get().isEmpty() )
784 aFillProps.maBlipProps.mxGraphic = rGraphicHelper.importEmbeddedGraphic( moBitmapPath.get() );
785 if( aFillProps.maBlipProps.mxGraphic.is() )
787 aFillProps.moFillType = XML_blipFill;
788 aFillProps.maBlipProps.moBitmapMode = (nFillType == XML_frame) ? XML_stretch : XML_tile;
789 break; // do not break if bitmap is missing, but run to XML_solid instead
793 // run-through to XML_solid in case of missing bitmap path intended!
795 case XML_solid:
796 default:
798 aFillProps.moFillType = XML_solidFill;
799 // fill color (default is white)
800 aFillProps.maFillColor = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_WHITE );
804 else
806 aFillProps.moFillType = XML_noFill;
809 aFillProps.pushToPropMap( rPropMap, rGraphicHelper );
812 // ============================================================================
814 ShadowModel::ShadowModel()
815 : mbHasShadow(false)
819 void ShadowModel::pushToPropMap(ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper) const
821 if (!mbHasShadow || (moShadowOn.has() && !moShadowOn.get()))
822 return;
824 drawingml::Color aColor = ConversionHelper::decodeColor(rGraphicHelper, moColor, moOpacity, API_RGB_GRAY);
825 // nOffset* is in mm100, default value is 35 twips, see DffPropertyReader::ApplyAttributes() in msfilter.
826 sal_Int32 nOffsetX = 62, nOffsetY = 62;
827 if (moOffset.has())
829 OUString aOffsetX, aOffsetY;
830 ConversionHelper::separatePair(aOffsetX, aOffsetY, moOffset.get(), ',');
831 if (!aOffsetX.isEmpty())
832 nOffsetX = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, aOffsetX, 0, false, false );
833 if (!aOffsetY.isEmpty())
834 nOffsetY = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, aOffsetY, 0, false, false );
837 table::ShadowFormat aFormat;
838 aFormat.Color = aColor.getColor(rGraphicHelper);
839 aFormat.Location = table::ShadowLocation_BOTTOM_RIGHT;
840 // The width of the shadow is the average of the x and y values, see SwWW8ImplReader::MatchSdrItemsIntoFlySet().
841 aFormat.ShadowWidth = ((nOffsetX + nOffsetY) / 2);
842 rPropMap.setProperty(PROP_ShadowFormat, uno::makeAny(aFormat));
845 TextpathModel::TextpathModel()
849 beans::PropertyValue lcl_createTextpathProps()
851 uno::Sequence<beans::PropertyValue> aTextpathPropSeq(4);
852 aTextpathPropSeq[0].Name = "TextPath";
853 aTextpathPropSeq[0].Value <<= sal_True;
854 aTextpathPropSeq[1].Name = "TextPathMode";
855 aTextpathPropSeq[1].Value <<= drawing::EnhancedCustomShapeTextPathMode_SHAPE;
856 aTextpathPropSeq[2].Name = "ScaleX";
857 aTextpathPropSeq[2].Value <<= sal_False;
858 aTextpathPropSeq[3].Name = "SameLetterHeights";
859 aTextpathPropSeq[3].Value <<= sal_False;
861 beans::PropertyValue aRet;
862 aRet.Name = "TextPath";
863 aRet.Value <<= aTextpathPropSeq;
864 return aRet;
867 void TextpathModel::pushToPropMap(ShapePropertyMap& rPropMap, uno::Reference<drawing::XShape> xShape) const
869 if (moString.has())
871 uno::Reference<text::XTextRange> xTextRange(xShape, uno::UNO_QUERY);
872 xTextRange->setString(moString.get());
874 uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY);
875 uno::Sequence<beans::PropertyValue> aGeomPropSeq = xPropertySet->getPropertyValue("CustomShapeGeometry").get< uno::Sequence<beans::PropertyValue> >();
876 bool bFound = false;
877 for (int i = 0; i < aGeomPropSeq.getLength(); ++i)
879 beans::PropertyValue& rProp = aGeomPropSeq[i];
880 if (rProp.Name == "TextPath")
882 bFound = true;
883 rProp = lcl_createTextpathProps();
886 if (!bFound)
888 sal_Int32 nSize = aGeomPropSeq.getLength();
889 aGeomPropSeq.realloc(nSize+1);
890 aGeomPropSeq[nSize] = lcl_createTextpathProps();
892 rPropMap.setAnyProperty(PROP_CustomShapeGeometry, uno::makeAny(aGeomPropSeq));
896 } // namespace vml
897 } // namespace oox
899 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */