Branch libreoffice-5-0-4
[LibreOffice.git] / oox / source / vml / vmlformatting.cxx
blob6dcbf3e4c3782d8783416124b10bd401586aa75c
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 <osl/diagnose.h>
28 #include "oox/drawingml/color.hxx"
29 #include "oox/drawingml/drawingmltypes.hxx"
30 #include "oox/drawingml/fillproperties.hxx"
31 #include "oox/drawingml/lineproperties.hxx"
32 #include "oox/drawingml/shapepropertymap.hxx"
33 #include "oox/helper/attributelist.hxx"
34 #include "oox/helper/graphichelper.hxx"
36 namespace oox {
37 namespace vml {
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::geometry;
42 using ::oox::drawingml::Color;
43 using ::oox::drawingml::FillProperties;
44 using ::oox::drawingml::LineArrowProperties;
45 using ::oox::drawingml::LineProperties;
46 using ::oox::drawingml::ShapePropertyMap;
47 using ::com::sun::star::awt::Point;
48 using ::com::sun::star::drawing::PolygonFlags;
49 using ::com::sun::star::drawing::PolygonFlags_NORMAL;
50 using ::com::sun::star::drawing::PolygonFlags_CONTROL;
52 namespace {
54 bool lclExtractDouble( double& orfValue, sal_Int32& ornEndPos, const OUString& rValue )
56 // extract the double value and find start position of unit characters
57 rtl_math_ConversionStatus eConvStatus = rtl_math_ConversionStatus_Ok;
58 orfValue = ::rtl::math::stringToDouble( rValue, '.', '\0', &eConvStatus, &ornEndPos );
59 return eConvStatus == rtl_math_ConversionStatus_Ok;
62 } // namespace
64 bool ConversionHelper::separatePair( OUString& orValue1, OUString& orValue2,
65 const OUString& rValue, sal_Unicode cSep )
67 sal_Int32 nSepPos = rValue.indexOf( cSep );
68 if( nSepPos >= 0 )
70 orValue1 = rValue.copy( 0, nSepPos ).trim();
71 orValue2 = rValue.copy( nSepPos + 1 ).trim();
73 else
75 orValue1 = rValue.trim();
77 return !orValue1.isEmpty() && !orValue2.isEmpty();
80 bool ConversionHelper::decodeBool( const OUString& rValue )
82 sal_Int32 nToken = AttributeConversion::decodeToken( rValue );
83 // anything else than 't' or 'true' is considered to be false, as specified
84 return (nToken == XML_t) || (nToken == XML_true);
87 double ConversionHelper::decodePercent( const OUString& rValue, double fDefValue )
89 if( rValue.isEmpty() )
90 return fDefValue;
92 double fValue = 0.0;
93 sal_Int32 nEndPos = 0;
94 if( !lclExtractDouble( fValue, nEndPos, rValue ) )
95 return fDefValue;
97 if( nEndPos == rValue.getLength() )
98 return fValue;
100 if( (nEndPos + 1 == rValue.getLength()) && (rValue[ nEndPos ] == '%') )
101 return fValue / 100.0;
103 if( (nEndPos + 1 == rValue.getLength()) && (rValue[ nEndPos ] == 'f') )
104 return fValue / 65536.0;
106 OSL_FAIL( "ConversionHelper::decodePercent - unknown measure unit" );
107 return fDefValue;
110 sal_Int64 ConversionHelper::decodeMeasureToEmu( const GraphicHelper& rGraphicHelper,
111 const OUString& rValue, sal_Int32 nRefValue, bool bPixelX, bool bDefaultAsPixel )
113 // default for missing values is 0
114 if( rValue.isEmpty() )
115 return 0;
117 // TODO: according to spec, value may contain "auto"
118 if ( rValue == "auto" )
120 OSL_FAIL( "ConversionHelper::decodeMeasureToEmu - special value 'auto' must be handled by caller" );
121 return nRefValue;
124 // extract the double value and find start position of unit characters
125 double fValue = 0.0;
126 sal_Int32 nEndPos = 0;
127 if( !lclExtractDouble( fValue, nEndPos, rValue ) || (fValue == 0.0) )
128 return 0;
130 // process trailing unit, convert to EMU
131 OUString aUnit;
132 if( (0 < nEndPos) && (nEndPos < rValue.getLength()) )
133 aUnit = rValue.copy( nEndPos );
134 else if( bDefaultAsPixel )
135 aUnit = "px";
136 // else default is EMU
138 if( aUnit.getLength() == 2 )
140 sal_Unicode cChar1 = aUnit[ 0 ];
141 sal_Unicode cChar2 = aUnit[ 1 ];
142 if( (cChar1 == 'i') && (cChar2 == 'n') ) // 1 inch = 914,400 EMU
143 fValue *= 914400.0;
144 else if( (cChar1 == 'c') && (cChar2 == 'm') ) // 1 cm = 360,000 EMU
145 fValue *= 360000.0;
146 else if( (cChar1 == 'm') && (cChar2 == 'm') ) // 1 mm = 36,000 EMU
147 fValue *= 36000.0;
148 else if( (cChar1 == 'p') && (cChar2 == 't') ) // 1 point = 1/72 inch = 12,700 EMU
149 fValue *= 12700.0;
150 else if( (cChar1 == 'p') && (cChar2 == 'c') ) // 1 pica = 1/6 inch = 152,400 EMU
151 fValue *= 152400.0;
152 else if( (cChar1 == 'p') && (cChar2 == 'x') ) // 1 pixel, dependent on output device
153 fValue = static_cast< double >( ::oox::drawingml::convertHmmToEmu(
154 bPixelX ?
155 rGraphicHelper.convertScreenPixelXToHmm( fValue ) :
156 rGraphicHelper.convertScreenPixelYToHmm( fValue ) ) );
158 else if( (aUnit.getLength() == 1) && (aUnit[ 0 ] == '%') )
160 fValue *= nRefValue / 100.0;
162 else if( bDefaultAsPixel || !aUnit.isEmpty() ) // default as EMU and no unit -> do nothing
164 OSL_FAIL( "ConversionHelper::decodeMeasureToEmu - unknown measure unit" );
165 fValue = nRefValue;
167 return static_cast< sal_Int64 >( fValue + 0.5 );
170 sal_Int32 ConversionHelper::decodeMeasureToHmm( const GraphicHelper& rGraphicHelper,
171 const OUString& rValue, sal_Int32 nRefValue, bool bPixelX, bool bDefaultAsPixel )
173 return ::oox::drawingml::convertEmuToHmm( decodeMeasureToEmu( rGraphicHelper, rValue, nRefValue, bPixelX, bDefaultAsPixel ) );
176 Color ConversionHelper::decodeColor( const GraphicHelper& rGraphicHelper,
177 const OptValue< OUString >& roVmlColor, const OptValue< double >& roVmlOpacity,
178 sal_Int32 nDefaultRgb, sal_Int32 nPrimaryRgb )
180 Color aDmlColor;
182 // convert opacity
183 const sal_Int32 DML_FULL_OPAQUE = ::oox::drawingml::MAX_PERCENT;
184 double fOpacity = roVmlOpacity.get( 1.0 );
185 sal_Int32 nOpacity = getLimitedValue< sal_Int32, double >( fOpacity * DML_FULL_OPAQUE, 0, DML_FULL_OPAQUE );
186 if( nOpacity < DML_FULL_OPAQUE )
187 aDmlColor.addTransformation( XML_alpha, nOpacity );
189 // color attribute not present - set passed default color
190 if( !roVmlColor.has() )
192 aDmlColor.setSrgbClr( nDefaultRgb );
193 return aDmlColor;
196 // separate leading color name or RGB value from following palette index
197 OUString aColorName, aColorIndex;
198 separatePair( aColorName, aColorIndex, roVmlColor.get(), ' ' );
200 // RGB colors in the format '#RRGGBB'
201 if( (aColorName.getLength() == 7) && (aColorName[ 0 ] == '#') )
203 aDmlColor.setSrgbClr( aColorName.copy( 1 ).toUInt32( 16 ) );
204 return aDmlColor;
207 // RGB colors in the format '#RGB'
208 if( (aColorName.getLength() == 4) && (aColorName[ 0 ] == '#') )
210 sal_Int32 nR = aColorName.copy( 1, 1 ).toUInt32( 16 ) * 0x11;
211 sal_Int32 nG = aColorName.copy( 2, 1 ).toUInt32( 16 ) * 0x11;
212 sal_Int32 nB = aColorName.copy( 3, 1 ).toUInt32( 16 ) * 0x11;
213 aDmlColor.setSrgbClr( (nR << 16) | (nG << 8) | nB );
214 return aDmlColor;
217 /* Predefined color names or system color names (resolve to RGB to detect
218 valid color name). */
219 sal_Int32 nColorToken = AttributeConversion::decodeToken( aColorName );
220 sal_Int32 nRgbValue = Color::getVmlPresetColor( nColorToken, API_RGB_TRANSPARENT );
221 if( nRgbValue == API_RGB_TRANSPARENT )
222 nRgbValue = rGraphicHelper.getSystemColor( nColorToken, API_RGB_TRANSPARENT );
223 if( nRgbValue != API_RGB_TRANSPARENT )
225 aDmlColor.setSrgbClr( nRgbValue );
226 return aDmlColor;
229 // try palette colors enclosed in brackets
230 if( (aColorIndex.getLength() >= 3) && (aColorIndex[ 0 ] == '[') && (aColorIndex[ aColorIndex.getLength() - 1 ] == ']') )
232 aDmlColor.setPaletteClr( aColorIndex.copy( 1, aColorIndex.getLength() - 2 ).toInt32() );
233 return aDmlColor;
236 // try fill gradient modificator 'fill <modifier>(<amount>)'
237 if( (nPrimaryRgb != API_RGB_TRANSPARENT) && (nColorToken == XML_fill) )
239 sal_Int32 nOpenParen = aColorIndex.indexOf( '(' );
240 sal_Int32 nCloseParen = aColorIndex.indexOf( ')' );
241 if( (2 <= nOpenParen) && (nOpenParen + 1 < nCloseParen) && (nCloseParen + 1 == aColorIndex.getLength()) )
243 sal_Int32 nModToken = XML_TOKEN_INVALID;
244 switch( AttributeConversion::decodeToken( aColorIndex.copy( 0, nOpenParen ) ) )
246 case XML_darken: nModToken = XML_shade;break;
247 case XML_lighten: nModToken = XML_tint;
249 sal_Int32 nValue = aColorIndex.copy( nOpenParen + 1, nCloseParen - nOpenParen - 1 ).toInt32();
250 if( (nModToken != XML_TOKEN_INVALID) && (0 <= nValue) && (nValue < 255) )
252 /* Simulate this modifier color by a color with related transformation.
253 The modifier amount has to be converted from the range [0;255] to
254 percentage [0;100000] used by DrawingML. */
255 aDmlColor.setSrgbClr( nPrimaryRgb );
256 aDmlColor.addTransformation( nModToken, static_cast< sal_Int32 >( nValue * ::oox::drawingml::MAX_PERCENT / 255 ) );
257 return aDmlColor;
262 OSL_FAIL( OStringBuffer( "lclGetColor - invalid VML color name '" ).
263 append( OUStringToOString( roVmlColor.get(), RTL_TEXTENCODING_ASCII_US ) ).append( '\'' ).getStr() );
264 aDmlColor.setSrgbClr( nDefaultRgb );
265 return aDmlColor;
268 void ConversionHelper::decodeVmlPath( ::std::vector< ::std::vector< Point > >& rPointLists, ::std::vector< ::std::vector< PolygonFlags > >& rFlagLists, const OUString& rPath )
270 ::std::vector< sal_Int32 > aCoordList;
271 Point aCurrentPoint;
272 sal_Int32 nTokenStart = 0;
273 sal_Int32 nTokenLen = 0;
274 sal_Int32 nParamCount = 0;
275 bool bCommand = false;
276 enum VML_State { START, MOVE_REL, MOVE_ABS, BEZIER_REL, BEZIER_ABS,
277 LINE_REL, LINE_ABS, CLOSE, END, UNSUPPORTED };
278 VML_State state = START;
280 rPointLists.push_back( ::std::vector< Point>() );
281 rFlagLists.push_back( ::std::vector< PolygonFlags >() );
283 for ( sal_Int32 i = 0; i < rPath.getLength(); i++ )
285 // Keep track of current integer token
286 if ( ( rPath[ i ] >= '0' && rPath[ i ] <= '9' ) || rPath[ i ] == '-' )
287 nTokenLen++;
288 else if ( rPath[ i ] != ' ' )
290 // Store coordinate from current token
291 if ( state != START && state != UNSUPPORTED )
293 if ( nTokenLen > 0 )
294 aCoordList.push_back( rPath.copy( nTokenStart, nTokenLen ).toInt32() );
295 else
296 aCoordList.push_back( 0 );
297 nTokenLen = 0;
300 if (rPath[ i ] == ',' )
302 nParamCount--;
305 // Upon finding the next command code, deal with stored
306 // coordinates for previous command and reset parameters counter if needed.
307 // See http://www.w3.org/TR/NOTE-VML#_Toc416858382 for params count reference
308 if ( rPath[ i ] != ',' || nParamCount == 0 )
310 switch ( state )
312 case MOVE_REL: // 2* params -> param count reset
313 if ( rPointLists.size() > 0 && rPointLists.back().size() > 0 )
315 rPointLists.push_back( ::std::vector< Point >() );
316 rFlagLists.push_back( ::std::vector< PolygonFlags >() );
318 rPointLists.back().push_back( Point( aCoordList[ 0 ], aCoordList[ 1 ] ) );
319 rFlagLists.back().push_back( PolygonFlags_NORMAL );
320 aCurrentPoint = rPointLists.back().back();
321 nParamCount = 2;
322 break;
324 case MOVE_ABS: // 2 params -> no param count reset
325 if ( rPointLists.size() > 0 && rPointLists.back().size() > 0 )
327 rPointLists.push_back( ::std::vector< Point >() );
328 rFlagLists.push_back( ::std::vector< PolygonFlags >() );
330 rPointLists.back().push_back( Point( (aCoordList[ 0 ]), (aCoordList.size() > 1 ? aCoordList[ 1 ] : 0) ) );
331 rFlagLists.back().push_back( PolygonFlags_NORMAL );
332 aCurrentPoint = rPointLists.back().back();
333 break;
335 case BEZIER_REL: // 6* params -> param count reset
336 rPointLists.back().push_back( Point( aCurrentPoint.X + aCoordList[ 0 ],
337 aCurrentPoint.Y + aCoordList[ 1 ] ) );
338 rPointLists.back().push_back( Point( aCurrentPoint.X + aCoordList[ 2 ],
339 aCurrentPoint.Y + aCoordList[ 3 ] ) );
340 rPointLists.back().push_back( Point( aCurrentPoint.X + aCoordList[ 4 ],
341 aCurrentPoint.Y + aCoordList[ 5 ] ) );
342 rFlagLists.back().push_back( PolygonFlags_CONTROL );
343 rFlagLists.back().push_back( PolygonFlags_CONTROL );
344 rFlagLists.back().push_back( PolygonFlags_NORMAL );
345 aCurrentPoint = rPointLists.back().back();
346 nParamCount = 6;
347 break;
349 case BEZIER_ABS: // 6* params -> param count reset
350 rPointLists.back().push_back( Point( aCoordList[ 0 ], aCoordList[ 1 ] ) );
351 rPointLists.back().push_back( Point( aCoordList[ 2 ], aCoordList[ 3 ] ) );
352 rPointLists.back().push_back( Point( aCoordList[ 4 ], aCoordList[ 5 ] ) );
353 rFlagLists.back().push_back( PolygonFlags_CONTROL );
354 rFlagLists.back().push_back( PolygonFlags_CONTROL );
355 rFlagLists.back().push_back( PolygonFlags_NORMAL );
356 aCurrentPoint = rPointLists.back().back();
357 nParamCount = 6;
358 break;
360 case LINE_REL: // 2* params -> param count reset
361 rPointLists.back().push_back( Point( aCurrentPoint.X + aCoordList[ 0 ],
362 aCurrentPoint.Y + aCoordList[ 1 ] ) );
363 rFlagLists.back().push_back( PolygonFlags_NORMAL );
364 aCurrentPoint = rPointLists.back().back();
365 nParamCount = 2;
366 break;
368 case LINE_ABS: // 2* params -> param count reset
369 rPointLists.back().push_back( Point( aCoordList[ 0 ], (aCoordList.size() > 1 ? aCoordList[ 1 ] : 0) ) );
370 rFlagLists.back().push_back( PolygonFlags_NORMAL );
371 aCurrentPoint = rPointLists.back().back();
372 nParamCount = 2;
373 break;
375 case CLOSE: // 0 param
376 SAL_WARN_IF(rPointLists.back().empty() || rFlagLists.back().empty(), "oox", "empty pointlists at close");
377 if (!rPointLists.back().empty() && !rFlagLists.back().empty())
379 rPointLists.back().push_back( rPointLists.back()[ 0 ] );
380 rFlagLists.back().push_back( rFlagLists.back()[ 0 ] );
381 aCurrentPoint = rPointLists.back().back();
383 break;
385 case END: // 0 param
386 rPointLists.push_back( ::std::vector< Point >() );
387 rFlagLists.push_back( ::std::vector< PolygonFlags >() );
388 break;
390 case START:
391 case UNSUPPORTED:
392 break;
395 aCoordList.clear();
398 // Allow two-char commands to peek ahead to the next character
399 char nextChar = '\0';
400 if (i+1 < rPath.getLength())
401 nextChar = rPath[i+1];
403 // Move to relevant state upon finding a command
404 bCommand = true;
405 switch ( rPath[ i ] )
407 // Single-character commands
408 case 't': // rmoveto
409 state = MOVE_REL; nParamCount = 2; break;
410 case 'm': // moveto
411 state = MOVE_ABS; nParamCount = 2; break;
412 case 'v': // rcurveto
413 state = BEZIER_REL; nParamCount = 6; break;
414 case 'c': // curveto
415 state = BEZIER_ABS; nParamCount = 6; break;
416 case 'r': // rlineto
417 state = LINE_REL; nParamCount = 2; break;
418 case 'l': // lineto
419 state = LINE_ABS; nParamCount = 2; break;
420 case 'x': // close
421 state = CLOSE; break;
422 case 'e': // end
423 state = END; break;
425 // Two-character commands
426 case 'n':
428 switch ( nextChar )
430 case 'f': // nf - nofill
431 case 's': // ns - nostroke
432 state = UNSUPPORTED; i++; break;
434 break;
436 case 'a': // Elliptical curves
438 switch ( nextChar )
440 case 'e': // ae - angleellipseto
441 case 'l': // al - angleellipse
442 state = UNSUPPORTED; i++; break;
443 case 't': // at - arcto
444 case 'r': // ar - arc
445 state = UNSUPPORTED; i++; break;
447 break;
449 case 'w': // Clockwise elliptical arcs
451 switch ( nextChar )
453 case 'a': // wa - clockwisearcto
454 case 'r': // wr - clockwisearc
455 state = UNSUPPORTED; i++; break;
457 break;
459 case 'q':
461 switch ( nextChar )
463 case 'x': // qx - ellipticalquadrantx
464 case 'y': // qy - ellipticalquadranty
465 state = UNSUPPORTED; i++; break;
466 case 'b': // qb - quadraticbezier
467 state = UNSUPPORTED; i++; break;
469 break;
471 case 'h': // behaviour extensions
473 switch ( nextChar )
475 case 'a': // ha - AutoLine
476 case 'b': // hb - AutoCurve
477 case 'c': // hc - CornerLine
478 case 'd': // hd - CornerCurve
479 case 'e': // he - SmoothLine
480 case 'f': // hf - SmoothCurve
481 case 'g': // hg - SymmetricLine
482 case 'h': // hh - SymmetricCurve
483 case 'i': // hi - Freeform
484 state = UNSUPPORTED; i++; break;
486 break;
488 default:
489 bCommand = false;
490 break;
493 if (bCommand) nTokenLen = 0;
494 nTokenStart = i+1;
499 namespace {
501 sal_Int64 lclGetEmu( const GraphicHelper& rGraphicHelper, const OptValue< OUString >& roValue, sal_Int64 nDefValue )
503 return roValue.has() ? ConversionHelper::decodeMeasureToEmu( rGraphicHelper, roValue.get(), 0, false, false ) : nDefValue;
506 void lclGetDmlLineDash( OptValue< sal_Int32 >& oroPresetDash, LineProperties::DashStopVector& orCustomDash, const OptValue< OUString >& roDashStyle )
508 if( roDashStyle.has() )
510 const OUString& rDashStyle = roDashStyle.get();
511 switch( AttributeConversion::decodeToken( rDashStyle ) )
513 case XML_solid: oroPresetDash = XML_solid; return;
514 case XML_shortdot: oroPresetDash = XML_sysDot; return;
515 case XML_shortdash: oroPresetDash = XML_sysDash; return;
516 case XML_shortdashdot: oroPresetDash = XML_sysDashDot; return;
517 case XML_shortdashdotdot: oroPresetDash = XML_sysDashDotDot; return;
518 case XML_dot: oroPresetDash = XML_dot; return;
519 case XML_dash: oroPresetDash = XML_dash; return;
520 case XML_dashdot: oroPresetDash = XML_dashDot; return;
521 case XML_longdash: oroPresetDash = XML_lgDash; return;
522 case XML_longdashdot: oroPresetDash = XML_lgDashDot; return;
523 case XML_longdashdotdot: oroPresetDash = XML_lgDashDotDot; return;
525 // try to convert user-defined dash style
526 default:
528 ::std::vector< sal_Int32 > aValues;
529 sal_Int32 nIndex = 0;
530 while( nIndex >= 0 )
531 aValues.push_back( rDashStyle.getToken( 0, ' ', nIndex ).toInt32() );
532 size_t nPairs = aValues.size() / 2; // ignore last value if size is odd
533 for( size_t nPairIdx = 0; nPairIdx < nPairs; ++nPairIdx )
534 orCustomDash.push_back( LineProperties::DashStop( aValues[ 2 * nPairIdx ], aValues[ 2 * nPairIdx + 1 ] ) );
540 sal_Int32 lclGetDmlArrowType( const OptValue< sal_Int32 >& roArrowType )
542 if( roArrowType.has() ) switch( roArrowType.get() )
544 case XML_none: return XML_none;
545 case XML_block: return XML_triangle;
546 case XML_classic: return XML_stealth;
547 case XML_diamond: return XML_diamond;
548 case XML_oval: return XML_oval;
549 case XML_open: return XML_arrow;
551 return XML_none;
554 sal_Int32 lclGetDmlArrowWidth( const OptValue< sal_Int32 >& roArrowWidth )
556 if( roArrowWidth.has() ) switch( roArrowWidth.get() )
558 case XML_narrow: return XML_sm;
559 case XML_medium: return XML_med;
560 case XML_wide: return XML_lg;
562 return XML_med;
565 sal_Int32 lclGetDmlArrowLength( const OptValue< sal_Int32 >& roArrowLength )
567 if( roArrowLength.has() ) switch( roArrowLength.get() )
569 case XML_short: return XML_sm;
570 case XML_medium: return XML_med;
571 case XML_long: return XML_lg;
573 return XML_med;
576 void lclConvertArrow( LineArrowProperties& orArrowProp, const StrokeArrowModel& rStrokeArrow )
578 orArrowProp.moArrowType = lclGetDmlArrowType( rStrokeArrow.moArrowType );
579 orArrowProp.moArrowWidth = lclGetDmlArrowWidth( rStrokeArrow.moArrowWidth );
580 orArrowProp.moArrowLength = lclGetDmlArrowLength( rStrokeArrow.moArrowLength );
583 sal_Int32 lclGetDmlLineCompound( const OptValue< sal_Int32 >& roLineStyle )
585 if( roLineStyle.has() ) switch( roLineStyle.get() )
587 case XML_single: return XML_sng;
588 case XML_thinThin: return XML_dbl;
589 case XML_thinThick: return XML_thinThick;
590 case XML_thickThin: return XML_thickThin;
591 case XML_thickBetweenThin: return XML_tri;
593 return XML_sng;
596 sal_Int32 lclGetDmlLineCap( const OptValue< sal_Int32 >& roEndCap )
598 if( roEndCap.has() ) switch( roEndCap.get() )
600 case XML_flat: return XML_flat;
601 case XML_square: return XML_sq;
602 case XML_round: return XML_rnd;
604 return XML_flat; // different defaults in VML (flat) and DrawingML (square)
607 sal_Int32 lclGetDmlLineJoint( const OptValue< sal_Int32 >& roJoinStyle )
609 if( roJoinStyle.has() ) switch( roJoinStyle.get() )
611 case XML_round: return XML_round;
612 case XML_bevel: return XML_bevel;
613 case XML_miter: return XML_miter;
615 return XML_round;
618 } // namespace
620 void StrokeArrowModel::assignUsed( const StrokeArrowModel& rSource )
622 moArrowType.assignIfUsed( rSource.moArrowType );
623 moArrowWidth.assignIfUsed( rSource.moArrowWidth );
624 moArrowLength.assignIfUsed( rSource.moArrowLength );
627 void StrokeModel::assignUsed( const StrokeModel& rSource )
629 moStroked.assignIfUsed( rSource.moStroked );
630 maStartArrow.assignUsed( rSource.maStartArrow );
631 maEndArrow.assignUsed( rSource.maEndArrow );
632 moColor.assignIfUsed( rSource.moColor );
633 moOpacity.assignIfUsed( rSource.moOpacity );
634 moWeight.assignIfUsed( rSource.moWeight );
635 moDashStyle.assignIfUsed( rSource.moDashStyle );
636 moLineStyle.assignIfUsed( rSource.moLineStyle );
637 moEndCap.assignIfUsed( rSource.moEndCap );
638 moJoinStyle.assignIfUsed( rSource.moJoinStyle );
641 void StrokeModel::pushToPropMap( ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper ) const
643 /* Convert VML line formatting to DrawingML line formatting and let the
644 DrawingML code do the hard work. */
645 LineProperties aLineProps;
647 if( moStroked.get( true ) )
649 aLineProps.maLineFill.moFillType = XML_solidFill;
650 lclConvertArrow( aLineProps.maStartArrow, maStartArrow );
651 lclConvertArrow( aLineProps.maEndArrow, maEndArrow );
652 aLineProps.maLineFill.maFillColor = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_BLACK );
653 aLineProps.moLineWidth = getLimitedValue< sal_Int32, sal_Int64 >( lclGetEmu( rGraphicHelper, moWeight, 1 ), 0, SAL_MAX_INT32 );
654 lclGetDmlLineDash( aLineProps.moPresetDash, aLineProps.maCustomDash, moDashStyle );
655 aLineProps.moLineCompound = lclGetDmlLineCompound( moLineStyle );
656 aLineProps.moLineCap = lclGetDmlLineCap( moEndCap );
657 aLineProps.moLineJoint = lclGetDmlLineJoint( moJoinStyle );
659 else
661 aLineProps.maLineFill.moFillType = XML_noFill;
664 aLineProps.pushToPropMap( rPropMap, rGraphicHelper );
667 void FillModel::assignUsed( const FillModel& rSource )
669 moFilled.assignIfUsed( rSource.moFilled );
670 moColor.assignIfUsed( rSource.moColor );
671 moOpacity.assignIfUsed( rSource.moOpacity );
672 moColor2.assignIfUsed( rSource.moColor2 );
673 moOpacity2.assignIfUsed( rSource.moOpacity2 );
674 moType.assignIfUsed( rSource.moType );
675 moAngle.assignIfUsed( rSource.moAngle );
676 moFocus.assignIfUsed( rSource.moFocus );
677 moFocusPos.assignIfUsed( rSource.moFocusPos );
678 moFocusSize.assignIfUsed( rSource.moFocusSize );
679 moBitmapPath.assignIfUsed( rSource.moBitmapPath );
680 moRotate.assignIfUsed( rSource.moRotate );
683 void FillModel::pushToPropMap( ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper ) const
685 /* Convert VML fill formatting to DrawingML fill formatting and let the
686 DrawingML code do the hard work. */
687 FillProperties aFillProps;
689 if( moFilled.get( true ) )
691 sal_Int32 nFillType = moType.get( XML_solid );
692 switch( nFillType )
694 case XML_gradient:
695 case XML_gradientRadial:
697 aFillProps.moFillType = XML_gradFill;
698 aFillProps.maGradientProps.moRotateWithShape = moRotate.get( false );
699 double fFocus = moFocus.get( 0.0 );
701 // prepare colors
702 Color aColor1 = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_WHITE );
703 Color aColor2 = ConversionHelper::decodeColor( rGraphicHelper, moColor2, moOpacity2, API_RGB_WHITE, aColor1.getColor( rGraphicHelper ) );
705 // type XML_gradient is linear or axial gradient
706 if( nFillType == XML_gradient )
708 // normalize angle to range [0;360) degrees
709 sal_Int32 nVmlAngle = getIntervalValue< sal_Int32, sal_Int32 >( moAngle.get( 0 ), 0, 360 );
711 // focus of -50% or 50% is axial gradient
712 if( ((-0.75 <= fFocus) && (fFocus <= -0.25)) || ((0.25 <= fFocus) && (fFocus <= 0.75)) )
714 /* According to spec, focus of 50% is outer-to-inner,
715 and -50% is inner-to-outer (color to color2).
716 BUT: For angles >= 180 deg., the behaviour is
717 reversed... that's not spec'ed of course. So,
718 [0;180) deg. and 50%, or [180;360) deg. and -50% is
719 outer-to-inner in fact. */
720 bool bOuterToInner = (fFocus > 0.0) == (nVmlAngle < 180);
721 // simulate axial gradient by 3-step DrawingML gradient
722 const Color& rOuterColor = bOuterToInner ? aColor1 : aColor2;
723 const Color& rInnerColor = bOuterToInner ? aColor2 : aColor1;
724 aFillProps.maGradientProps.maGradientStops[ 0.0 ] = aFillProps.maGradientProps.maGradientStops[ 1.0 ] = rOuterColor;
725 aFillProps.maGradientProps.maGradientStops[ 0.5 ] = rInnerColor;
727 else // focus of -100%, 0%, and 100% is linear gradient
729 /* According to spec, focus of -100% or 100% swaps the
730 start and stop colors, effectively reversing the
731 gradient. BUT: For angles >= 180 deg., the
732 behaviour is reversed. This means that in this case
733 a focus of 0% swaps the gradient. */
734 if( fFocus < -0.5 || fFocus > 0.5 )
735 (nVmlAngle += 180) %= 360;
736 // set the start and stop colors
737 aFillProps.maGradientProps.maGradientStops[ 0.0 ] = aColor1;
738 aFillProps.maGradientProps.maGradientStops[ 1.0 ] = aColor2;
741 // VML counts counterclockwise from bottom, DrawingML clockwise from left
742 sal_Int32 nDmlAngle = (630 - nVmlAngle) % 360;
743 aFillProps.maGradientProps.moShadeAngle = nDmlAngle * ::oox::drawingml::PER_DEGREE;
745 else // XML_gradientRadial is rectangular gradient
747 aFillProps.maGradientProps.moGradientPath = XML_rect;
748 // convert VML focus position and size to DrawingML fill-to-rect
749 DoublePair aFocusPos = moFocusPos.get( DoublePair( 0.0, 0.0 ) );
750 DoublePair aFocusSize = moFocusSize.get( DoublePair( 0.0, 0.0 ) );
751 double fLeft = getLimitedValue< double, double >( aFocusPos.first, 0.0, 1.0 );
752 double fTop = getLimitedValue< double, double >( aFocusPos.second, 0.0, 1.0 );
753 double fRight = getLimitedValue< double, double >( fLeft + aFocusSize.first, fLeft, 1.0 );
754 double fBottom = getLimitedValue< double, double >( fTop + aFocusSize.second, fTop, 1.0 );
755 aFillProps.maGradientProps.moFillToRect = IntegerRectangle2D(
756 static_cast< sal_Int32 >( fLeft * ::oox::drawingml::MAX_PERCENT ),
757 static_cast< sal_Int32 >( fTop * ::oox::drawingml::MAX_PERCENT ),
758 static_cast< sal_Int32 >( (1.0 - fRight) * ::oox::drawingml::MAX_PERCENT ),
759 static_cast< sal_Int32 >( (1.0 - fBottom) * ::oox::drawingml::MAX_PERCENT ) );
761 // set the start and stop colors (focus of 0% means outer-to-inner)
762 bool bOuterToInner = (-0.5 <= fFocus) && (fFocus <= 0.5);
763 aFillProps.maGradientProps.maGradientStops[ 0.0 ] = bOuterToInner ? aColor2 : aColor1;
764 aFillProps.maGradientProps.maGradientStops[ 1.0 ] = bOuterToInner ? aColor1 : aColor2;
767 break;
769 case XML_pattern:
770 case XML_tile:
771 case XML_frame:
773 if( moBitmapPath.has() && !moBitmapPath.get().isEmpty() )
775 aFillProps.maBlipProps.mxGraphic = rGraphicHelper.importEmbeddedGraphic( moBitmapPath.get() );
776 if( aFillProps.maBlipProps.mxGraphic.is() )
778 aFillProps.moFillType = XML_blipFill;
779 aFillProps.maBlipProps.moBitmapMode = (nFillType == XML_frame) ? XML_stretch : XML_tile;
780 break; // do not break if bitmap is missing, but run to XML_solid instead
784 // run-through to XML_solid in case of missing bitmap path intended!
786 case XML_solid:
787 default:
789 aFillProps.moFillType = XML_solidFill;
790 // fill color (default is white)
791 aFillProps.maFillColor = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_WHITE );
795 else
797 aFillProps.moFillType = XML_noFill;
800 aFillProps.pushToPropMap( rPropMap, rGraphicHelper );
803 ShadowModel::ShadowModel()
804 : mbHasShadow(false)
808 void ShadowModel::pushToPropMap(ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper) const
810 if (!mbHasShadow || (moShadowOn.has() && !moShadowOn.get()))
811 return;
813 drawingml::Color aColor = ConversionHelper::decodeColor(rGraphicHelper, moColor, moOpacity, API_RGB_GRAY);
814 // nOffset* is in mm100, default value is 35 twips, see DffPropertyReader::ApplyAttributes() in msfilter.
815 sal_Int32 nOffsetX = 62, nOffsetY = 62;
816 if (moOffset.has())
818 OUString aOffsetX, aOffsetY;
819 ConversionHelper::separatePair(aOffsetX, aOffsetY, moOffset.get(), ',');
820 if (!aOffsetX.isEmpty())
821 nOffsetX = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, aOffsetX, 0, false, false );
822 if (!aOffsetY.isEmpty())
823 nOffsetY = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, aOffsetY, 0, false, false );
826 table::ShadowFormat aFormat;
827 aFormat.Color = aColor.getColor(rGraphicHelper);
828 aFormat.Location = table::ShadowLocation_BOTTOM_RIGHT;
829 // The width of the shadow is the average of the x and y values, see SwWW8ImplReader::MatchSdrItemsIntoFlySet().
830 aFormat.ShadowWidth = ((nOffsetX + nOffsetY) / 2);
831 rPropMap.setProperty(PROP_ShadowFormat, uno::makeAny(aFormat));
834 TextpathModel::TextpathModel()
838 beans::PropertyValue lcl_createTextpathProps()
840 uno::Sequence<beans::PropertyValue> aTextpathPropSeq(4);
841 aTextpathPropSeq[0].Name = "TextPath";
842 aTextpathPropSeq[0].Value <<= sal_True;
843 aTextpathPropSeq[1].Name = "TextPathMode";
844 aTextpathPropSeq[1].Value <<= drawing::EnhancedCustomShapeTextPathMode_SHAPE;
845 aTextpathPropSeq[2].Name = "ScaleX";
846 aTextpathPropSeq[2].Value <<= sal_False;
847 aTextpathPropSeq[3].Name = "SameLetterHeights";
848 aTextpathPropSeq[3].Value <<= sal_False;
850 beans::PropertyValue aRet;
851 aRet.Name = "TextPath";
852 aRet.Value <<= aTextpathPropSeq;
853 return aRet;
856 void TextpathModel::pushToPropMap(ShapePropertyMap& rPropMap, uno::Reference<drawing::XShape> xShape) const
858 if (moString.has())
860 uno::Reference<text::XTextRange> xTextRange(xShape, uno::UNO_QUERY);
861 xTextRange->setString(moString.get());
863 uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY);
864 uno::Sequence<beans::PropertyValue> aGeomPropSeq = xPropertySet->getPropertyValue("CustomShapeGeometry").get< uno::Sequence<beans::PropertyValue> >();
865 bool bFound = false;
866 for (int i = 0; i < aGeomPropSeq.getLength(); ++i)
868 beans::PropertyValue& rProp = aGeomPropSeq[i];
869 if (rProp.Name == "TextPath")
871 bFound = true;
872 rProp = lcl_createTextpathProps();
875 if (!bFound)
877 sal_Int32 nSize = aGeomPropSeq.getLength();
878 aGeomPropSeq.realloc(nSize+1);
879 aGeomPropSeq[nSize] = lcl_createTextpathProps();
881 rPropMap.setAnyProperty(PROP_CustomShapeGeometry, uno::makeAny(aGeomPropSeq));
885 } // namespace vml
886 } // namespace oox
888 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */