Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / oox / source / vml / vmlformatting.cxx
blobc015157fdd8a3c495cef65c366a5528b0991b064
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/XShape.hpp>
24 #include <com/sun/star/drawing/EnhancedCustomShapeTextPathMode.hpp>
25 #include <com/sun/star/table/ShadowFormat.hpp>
26 #include <com/sun/star/text/XTextRange.hpp>
27 #include <rtl/strbuf.hxx>
28 #include <osl/diagnose.h>
29 #include <oox/drawingml/color.hxx>
30 #include <oox/drawingml/drawingmltypes.hxx>
31 #include <drawingml/fillproperties.hxx>
32 #include <drawingml/lineproperties.hxx>
33 #include <oox/drawingml/shapepropertymap.hxx>
34 #include <oox/helper/attributelist.hxx>
35 #include <oox/helper/graphichelper.hxx>
36 #include <oox/token/properties.hxx>
37 #include <oox/token/tokens.hxx>
38 #include <svx/svdtrans.hxx>
39 #include <comphelper/propertysequence.hxx>
40 #include <vcl/virdev.hxx>
42 namespace oox {
43 namespace vml {
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::geometry;
48 using ::oox::drawingml::Color;
49 using ::oox::drawingml::FillProperties;
50 using ::oox::drawingml::LineArrowProperties;
51 using ::oox::drawingml::LineProperties;
52 using ::oox::drawingml::ShapePropertyMap;
53 using ::com::sun::star::awt::Point;
54 using ::com::sun::star::drawing::PolygonFlags;
55 using ::com::sun::star::drawing::PolygonFlags_NORMAL;
56 using ::com::sun::star::drawing::PolygonFlags_CONTROL;
58 namespace {
60 bool lclExtractDouble( double& orfValue, sal_Int32& ornEndPos, const OUString& rValue )
62 // extract the double value and find start position of unit characters
63 rtl_math_ConversionStatus eConvStatus = rtl_math_ConversionStatus_Ok;
64 orfValue = ::rtl::math::stringToDouble( rValue, '.', '\0', &eConvStatus, &ornEndPos );
65 return eConvStatus == rtl_math_ConversionStatus_Ok;
68 } // namespace
70 bool ConversionHelper::separatePair( OUString& orValue1, OUString& orValue2,
71 const OUString& rValue, sal_Unicode cSep )
73 sal_Int32 nSepPos = rValue.indexOf( cSep );
74 if( nSepPos >= 0 )
76 orValue1 = rValue.copy( 0, nSepPos ).trim();
77 orValue2 = rValue.copy( nSepPos + 1 ).trim();
79 else
81 orValue1 = rValue.trim();
83 return !orValue1.isEmpty() && !orValue2.isEmpty();
86 bool ConversionHelper::decodeBool( const OUString& rValue )
88 sal_Int32 nToken = AttributeConversion::decodeToken( rValue );
89 // anything else than 't' or 'true' is considered to be false, as specified
90 return (nToken == XML_t) || (nToken == XML_true);
93 double ConversionHelper::decodePercent( const OUString& rValue, double fDefValue )
95 if( rValue.isEmpty() )
96 return fDefValue;
98 double fValue = 0.0;
99 sal_Int32 nEndPos = 0;
100 if( !lclExtractDouble( fValue, nEndPos, rValue ) )
101 return fDefValue;
103 if( nEndPos == rValue.getLength() )
104 return fValue;
106 if( (nEndPos + 1 == rValue.getLength()) && (rValue[ nEndPos ] == '%') )
107 return fValue / 100.0;
109 if( (nEndPos + 1 == rValue.getLength()) && (rValue[ nEndPos ] == 'f') )
110 return fValue / 65536.0;
112 OSL_FAIL( "ConversionHelper::decodePercent - unknown measure unit" );
113 return fDefValue;
116 sal_Int32 ConversionHelper::decodeRotation( const OUString& rValue )
118 if( rValue.isEmpty() )
119 return 0;
121 double fValue = 0.0;
122 double fRotation = 0.0;
123 sal_Int32 nEndPos = 0;
124 if( !lclExtractDouble(fValue, nEndPos, rValue) )
125 return 0;
127 if( nEndPos == rValue.getLength() )
128 fRotation = fValue;
129 else if( (nEndPos + 2 == rValue.getLength()) && (rValue[nEndPos] == 'f') && (rValue[nEndPos+1] == 'd') )
130 fRotation = fValue / 65536.0;
131 else
133 OSL_FAIL("ConversionHelper::decodeRotation - unknown measure unit");
134 return 0;
137 return NormAngle360(fRotation * -100);
140 sal_Int64 ConversionHelper::decodeMeasureToEmu( const GraphicHelper& rGraphicHelper,
141 const OUString& rValue, sal_Int32 nRefValue, bool bPixelX, bool bDefaultAsPixel )
143 // default for missing values is 0
144 if( rValue.isEmpty() )
145 return 0;
147 // TODO: according to spec, value may contain "auto"
148 if ( rValue == "auto" )
150 OSL_FAIL( "ConversionHelper::decodeMeasureToEmu - special value 'auto' must be handled by caller" );
151 return nRefValue;
154 // extract the double value and find start position of unit characters
155 double fValue = 0.0;
156 sal_Int32 nEndPos = 0;
157 if( !lclExtractDouble( fValue, nEndPos, rValue ) || (fValue == 0.0) )
158 return 0;
160 // process trailing unit, convert to EMU
161 OUString aUnit;
162 if( (0 < nEndPos) && (nEndPos < rValue.getLength()) )
163 aUnit = rValue.copy( nEndPos );
164 else if( bDefaultAsPixel )
165 aUnit = "px";
166 // else default is EMU
168 if( aUnit.getLength() == 2 )
170 sal_Unicode cChar1 = aUnit[ 0 ];
171 sal_Unicode cChar2 = aUnit[ 1 ];
172 if( (cChar1 == 'i') && (cChar2 == 'n') ) // 1 inch = 914,400 EMU
173 fValue *= 914400.0;
174 else if( (cChar1 == 'c') && (cChar2 == 'm') ) // 1 cm = 360,000 EMU
175 fValue *= 360000.0;
176 else if( (cChar1 == 'm') && (cChar2 == 'm') ) // 1 mm = 36,000 EMU
177 fValue *= 36000.0;
178 else if( (cChar1 == 'p') && (cChar2 == 't') ) // 1 point = 1/72 inch = 12,700 EMU
179 fValue *= 12700.0;
180 else if( (cChar1 == 'p') && (cChar2 == 'c') ) // 1 pica = 1/6 inch = 152,400 EMU
181 fValue *= 152400.0;
182 else if( (cChar1 == 'p') && (cChar2 == 'x') ) // 1 pixel, dependent on output device
183 fValue = static_cast< double >( ::oox::drawingml::convertHmmToEmu(
184 bPixelX ?
185 rGraphicHelper.convertScreenPixelXToHmm( fValue ) :
186 rGraphicHelper.convertScreenPixelYToHmm( fValue ) ) );
188 else if( (aUnit.getLength() == 1) && (aUnit[ 0 ] == '%') )
190 fValue *= nRefValue / 100.0;
192 else if( bDefaultAsPixel || !aUnit.isEmpty() ) // default as EMU and no unit -> do nothing
194 OSL_FAIL( "ConversionHelper::decodeMeasureToEmu - unknown measure unit" );
195 fValue = nRefValue;
197 return static_cast< sal_Int64 >( fValue + 0.5 );
200 sal_Int32 ConversionHelper::decodeMeasureToHmm( const GraphicHelper& rGraphicHelper,
201 const OUString& rValue, sal_Int32 nRefValue, bool bPixelX, bool bDefaultAsPixel )
203 return ::oox::drawingml::convertEmuToHmm( decodeMeasureToEmu( rGraphicHelper, rValue, nRefValue, bPixelX, bDefaultAsPixel ) );
206 Color ConversionHelper::decodeColor( const GraphicHelper& rGraphicHelper,
207 const OptValue< OUString >& roVmlColor, const OptValue< double >& roVmlOpacity,
208 ::Color nDefaultRgb, ::Color nPrimaryRgb )
210 Color aDmlColor;
212 // convert opacity
213 const sal_Int32 DML_FULL_OPAQUE = ::oox::drawingml::MAX_PERCENT;
214 double fOpacity = roVmlOpacity.get( 1.0 );
215 sal_Int32 nOpacity = getLimitedValue< sal_Int32, double >( fOpacity * DML_FULL_OPAQUE, 0, DML_FULL_OPAQUE );
216 if( nOpacity < DML_FULL_OPAQUE )
217 aDmlColor.addTransformation( XML_alpha, nOpacity );
219 // color attribute not present - set passed default color
220 if( !roVmlColor.has() )
222 aDmlColor.setSrgbClr( nDefaultRgb );
223 return aDmlColor;
226 // separate leading color name or RGB value from following palette index
227 OUString aColorName, aColorIndex;
228 separatePair( aColorName, aColorIndex, roVmlColor.get(), ' ' );
230 // RGB colors in the format '#RRGGBB'
231 if( (aColorName.getLength() == 7) && (aColorName[ 0 ] == '#') )
233 aDmlColor.setSrgbClr( aColorName.copy( 1 ).toUInt32( 16 ) );
234 return aDmlColor;
237 // RGB colors in the format '#RGB'
238 if( (aColorName.getLength() == 4) && (aColorName[ 0 ] == '#') )
240 sal_Int32 nR = aColorName.copy( 1, 1 ).toUInt32( 16 ) * 0x11;
241 sal_Int32 nG = aColorName.copy( 2, 1 ).toUInt32( 16 ) * 0x11;
242 sal_Int32 nB = aColorName.copy( 3, 1 ).toUInt32( 16 ) * 0x11;
243 aDmlColor.setSrgbClr( (nR << 16) | (nG << 8) | nB );
244 return aDmlColor;
247 /* Predefined color names or system color names (resolve to RGB to detect
248 valid color name). */
249 sal_Int32 nColorToken = AttributeConversion::decodeToken( aColorName );
250 ::Color nRgbValue = Color::getVmlPresetColor( nColorToken, API_RGB_TRANSPARENT );
251 if( nRgbValue == API_RGB_TRANSPARENT )
252 nRgbValue = rGraphicHelper.getSystemColor( nColorToken );
253 if( nRgbValue != API_RGB_TRANSPARENT )
255 aDmlColor.setSrgbClr( nRgbValue );
256 return aDmlColor;
259 // try palette colors enclosed in brackets
260 if( (aColorIndex.getLength() >= 3) && (aColorIndex[ 0 ] == '[') && (aColorIndex[ aColorIndex.getLength() - 1 ] == ']') )
262 aDmlColor.setPaletteClr( aColorIndex.copy( 1, aColorIndex.getLength() - 2 ).toInt32() );
263 return aDmlColor;
266 // try fill gradient modificator 'fill <modifier>(<amount>)'
267 if( (nPrimaryRgb != API_RGB_TRANSPARENT) && (nColorToken == XML_fill) )
269 sal_Int32 nOpenParen = aColorIndex.indexOf( '(' );
270 sal_Int32 nCloseParen = aColorIndex.indexOf( ')' );
271 if( (2 <= nOpenParen) && (nOpenParen + 1 < nCloseParen) && (nCloseParen + 1 == aColorIndex.getLength()) )
273 sal_Int32 nModToken = XML_TOKEN_INVALID;
274 switch( AttributeConversion::decodeToken( aColorIndex.copy( 0, nOpenParen ) ) )
276 case XML_darken: nModToken = XML_shade;break;
277 case XML_lighten: nModToken = XML_tint;
279 sal_Int32 nValue = aColorIndex.copy( nOpenParen + 1, nCloseParen - nOpenParen - 1 ).toInt32();
280 if( (nModToken != XML_TOKEN_INVALID) && (0 <= nValue) && (nValue < 255) )
282 /* Simulate this modifier color by a color with related transformation.
283 The modifier amount has to be converted from the range [0;255] to
284 percentage [0;100000] used by DrawingML. */
285 aDmlColor.setSrgbClr( nPrimaryRgb );
286 aDmlColor.addTransformation( nModToken, static_cast< sal_Int32 >( nValue * ::oox::drawingml::MAX_PERCENT / 255 ) );
287 return aDmlColor;
292 OSL_FAIL( OStringBuffer( "lclGetColor - invalid VML color name '" ).
293 append( OUStringToOString( roVmlColor.get(), RTL_TEXTENCODING_ASCII_US ) ).append( '\'' ).getStr() );
294 aDmlColor.setSrgbClr( nDefaultRgb );
295 return aDmlColor;
298 void ConversionHelper::decodeVmlPath( ::std::vector< ::std::vector< Point > >& rPointLists, ::std::vector< ::std::vector< PolygonFlags > >& rFlagLists, const OUString& rPath )
300 ::std::vector< sal_Int32 > aCoordList;
301 Point aCurrentPoint;
302 sal_Int32 nTokenStart = 0;
303 sal_Int32 nTokenLen = 0;
304 sal_Int32 nParamCount = 0;
305 bool bCommand = false;
306 enum VML_State { START, MOVE_REL, MOVE_ABS, BEZIER_REL, BEZIER_ABS,
307 LINE_REL, LINE_ABS, CLOSE, END, UNSUPPORTED };
308 VML_State state = START;
310 rPointLists.emplace_back( );
311 rFlagLists.emplace_back( );
313 for ( sal_Int32 i = 0; i < rPath.getLength(); i++ )
315 // Keep track of current integer token
316 if ( ( rPath[ i ] >= '0' && rPath[ i ] <= '9' ) || rPath[ i ] == '-' )
317 nTokenLen++;
318 else if ( rPath[ i ] != ' ' )
320 // Store coordinate from current token
321 if ( state != START && state != UNSUPPORTED )
323 if ( nTokenLen > 0 )
324 aCoordList.push_back( rPath.copy( nTokenStart, nTokenLen ).toInt32() );
325 else
326 aCoordList.push_back( 0 );
327 nTokenLen = 0;
330 if (rPath[ i ] == ',' )
332 nParamCount--;
335 // Upon finding the next command code, deal with stored
336 // coordinates for previous command and reset parameters counter if needed.
337 // See http://www.w3.org/TR/NOTE-VML#_Toc416858382 for params count reference
338 if ( rPath[ i ] != ',' || nParamCount == 0 )
340 switch ( state )
342 case MOVE_REL:
343 aCoordList.resize(2, 0); // 2* params -> param count reset
344 if ( rPointLists.size() > 0 && rPointLists.back().size() > 0 )
346 rPointLists.emplace_back( );
347 rFlagLists.emplace_back( );
349 rPointLists.back().emplace_back( aCoordList[ 0 ], aCoordList[ 1 ] );
350 rFlagLists.back().push_back( PolygonFlags_NORMAL );
351 aCurrentPoint = rPointLists.back().back();
352 nParamCount = 2;
353 break;
355 case MOVE_ABS:
356 aCoordList.resize(2, 0); // 2 params -> no param count reset
357 if ( rPointLists.size() > 0 && rPointLists.back().size() > 0 )
359 rPointLists.emplace_back( );
360 rFlagLists.emplace_back( );
362 rPointLists.back().emplace_back( (aCoordList[ 0 ]), aCoordList[ 1 ] );
363 rFlagLists.back().push_back( PolygonFlags_NORMAL );
364 aCurrentPoint = rPointLists.back().back();
365 break;
367 case BEZIER_REL:
368 aCoordList.resize(6, 0); // 6* params -> param count reset
369 rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 0 ],
370 aCurrentPoint.Y + aCoordList[ 1 ] );
371 rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 2 ],
372 aCurrentPoint.Y + aCoordList[ 3 ] );
373 rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 4 ],
374 aCurrentPoint.Y + aCoordList[ 5 ] );
375 rFlagLists.back().push_back( PolygonFlags_CONTROL );
376 rFlagLists.back().push_back( PolygonFlags_CONTROL );
377 rFlagLists.back().push_back( PolygonFlags_NORMAL );
378 aCurrentPoint = rPointLists.back().back();
379 nParamCount = 6;
380 break;
382 case BEZIER_ABS:
383 aCoordList.resize(6, 0); // 6* params -> param count reset
384 rPointLists.back().emplace_back( aCoordList[ 0 ], aCoordList[ 1 ] );
385 rPointLists.back().emplace_back( aCoordList[ 2 ], aCoordList[ 3 ] );
386 rPointLists.back().emplace_back( aCoordList[ 4 ], aCoordList[ 5 ] );
387 rFlagLists.back().push_back( PolygonFlags_CONTROL );
388 rFlagLists.back().push_back( PolygonFlags_CONTROL );
389 rFlagLists.back().push_back( PolygonFlags_NORMAL );
390 aCurrentPoint = rPointLists.back().back();
391 nParamCount = 6;
392 break;
394 case LINE_REL:
395 aCoordList.resize(2, 0); // 2* params -> param count reset
396 rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 0 ],
397 aCurrentPoint.Y + aCoordList[ 1 ] );
398 rFlagLists.back().push_back( PolygonFlags_NORMAL );
399 aCurrentPoint = rPointLists.back().back();
400 nParamCount = 2;
401 break;
403 case LINE_ABS:
404 aCoordList.resize(2, 0); // 2* params -> param count reset
405 rPointLists.back().emplace_back( aCoordList[ 0 ], (aCoordList.size() > 1 ? aCoordList[ 1 ] : 0) );
406 rFlagLists.back().push_back( PolygonFlags_NORMAL );
407 aCurrentPoint = rPointLists.back().back();
408 nParamCount = 2;
409 break;
411 case CLOSE: // 0 param
412 SAL_WARN_IF(rPointLists.back().empty() || rFlagLists.back().empty(), "oox", "empty pointlists at close");
413 if (!rPointLists.back().empty() && !rFlagLists.back().empty())
415 rPointLists.back().push_back( rPointLists.back()[ 0 ] );
416 rFlagLists.back().push_back( rFlagLists.back()[ 0 ] );
417 aCurrentPoint = rPointLists.back().back();
419 break;
421 case END: // 0 param
422 rPointLists.emplace_back( );
423 rFlagLists.emplace_back( );
424 break;
426 case START:
427 case UNSUPPORTED:
428 break;
431 aCoordList.clear();
434 // Allow two-char commands to peek ahead to the next character
435 sal_Unicode nextChar = '\0';
436 if (i+1 < rPath.getLength())
437 nextChar = rPath[i+1];
439 // Move to relevant state upon finding a command
440 bCommand = true;
441 switch ( rPath[ i ] )
443 // Single-character commands
444 case 't': // rmoveto
445 state = MOVE_REL; nParamCount = 2; break;
446 case 'm': // moveto
447 state = MOVE_ABS; nParamCount = 2; break;
448 case 'v': // rcurveto
449 state = BEZIER_REL; nParamCount = 6; break;
450 case 'c': // curveto
451 state = BEZIER_ABS; nParamCount = 6; break;
452 case 'r': // rlineto
453 state = LINE_REL; nParamCount = 2; break;
454 case 'l': // lineto
455 state = LINE_ABS; nParamCount = 2; break;
456 case 'x': // close
457 state = CLOSE; break;
458 case 'e': // end
459 state = END; break;
461 // Two-character commands
462 case 'n':
464 switch ( nextChar )
466 case 'f': // nf - nofill
467 case 's': // ns - nostroke
468 state = UNSUPPORTED; i++; break;
470 break;
472 case 'a': // Elliptical curves
474 switch ( nextChar )
476 case 'e': // ae - angleellipseto
477 case 'l': // al - angleellipse
478 state = UNSUPPORTED; i++; break;
479 case 't': // at - arcto
480 case 'r': // ar - arc
481 state = UNSUPPORTED; i++; break;
483 break;
485 case 'w': // Clockwise elliptical arcs
487 switch ( nextChar )
489 case 'a': // wa - clockwisearcto
490 case 'r': // wr - clockwisearc
491 state = UNSUPPORTED; i++; break;
493 break;
495 case 'q':
497 switch ( nextChar )
499 case 'x': // qx - ellipticalquadrantx
500 case 'y': // qy - ellipticalquadranty
501 state = UNSUPPORTED; i++; break;
502 case 'b': // qb - quadraticbezier
503 state = UNSUPPORTED; i++; break;
505 break;
507 case 'h': // behaviour extensions
509 switch ( nextChar )
511 case 'a': // ha - AutoLine
512 case 'b': // hb - AutoCurve
513 case 'c': // hc - CornerLine
514 case 'd': // hd - CornerCurve
515 case 'e': // he - SmoothLine
516 case 'f': // hf - SmoothCurve
517 case 'g': // hg - SymmetricLine
518 case 'h': // hh - SymmetricCurve
519 case 'i': // hi - Freeform
520 state = UNSUPPORTED; i++; break;
522 break;
524 default:
525 bCommand = false;
526 break;
529 if (bCommand) nTokenLen = 0;
530 nTokenStart = i+1;
535 namespace {
537 sal_Int64 lclGetEmu( const GraphicHelper& rGraphicHelper, const OptValue< OUString >& roValue, sal_Int64 nDefValue )
539 return roValue.has() ? ConversionHelper::decodeMeasureToEmu( rGraphicHelper, roValue.get(), 0, false, false ) : nDefValue;
542 void lclGetDmlLineDash( OptValue< sal_Int32 >& oroPresetDash, LineProperties::DashStopVector& orCustomDash, const OptValue< OUString >& roDashStyle )
544 if( roDashStyle.has() )
546 const OUString& rDashStyle = roDashStyle.get();
547 switch( AttributeConversion::decodeToken( rDashStyle ) )
549 case XML_solid: oroPresetDash = XML_solid; return;
550 case XML_shortdot: oroPresetDash = XML_sysDot; return;
551 case XML_shortdash: oroPresetDash = XML_sysDash; return;
552 case XML_shortdashdot: oroPresetDash = XML_sysDashDot; return;
553 case XML_shortdashdotdot: oroPresetDash = XML_sysDashDotDot; return;
554 case XML_dot: oroPresetDash = XML_dot; return;
555 case XML_dash: oroPresetDash = XML_dash; return;
556 case XML_dashdot: oroPresetDash = XML_dashDot; return;
557 case XML_longdash: oroPresetDash = XML_lgDash; return;
558 case XML_longdashdot: oroPresetDash = XML_lgDashDot; return;
559 case XML_longdashdotdot: oroPresetDash = XML_lgDashDotDot; return;
561 // try to convert user-defined dash style
562 default:
564 ::std::vector< sal_Int32 > aValues;
565 sal_Int32 nIndex = 0;
566 while( nIndex >= 0 )
567 aValues.push_back( rDashStyle.getToken( 0, ' ', nIndex ).toInt32() );
568 size_t nPairs = aValues.size() / 2; // ignore last value if size is odd
569 for( size_t nPairIdx = 0; nPairIdx < nPairs; ++nPairIdx )
570 orCustomDash.emplace_back( aValues[ 2 * nPairIdx ], aValues[ 2 * nPairIdx + 1 ] );
576 sal_Int32 lclGetDmlArrowType( const OptValue< sal_Int32 >& roArrowType )
578 if( roArrowType.has() ) switch( roArrowType.get() )
580 case XML_none: return XML_none;
581 case XML_block: return XML_triangle;
582 case XML_classic: return XML_stealth;
583 case XML_diamond: return XML_diamond;
584 case XML_oval: return XML_oval;
585 case XML_open: return XML_arrow;
587 return XML_none;
590 sal_Int32 lclGetDmlArrowWidth( const OptValue< sal_Int32 >& roArrowWidth )
592 if( roArrowWidth.has() ) switch( roArrowWidth.get() )
594 case XML_narrow: return XML_sm;
595 case XML_medium: return XML_med;
596 case XML_wide: return XML_lg;
598 return XML_med;
601 sal_Int32 lclGetDmlArrowLength( const OptValue< sal_Int32 >& roArrowLength )
603 if( roArrowLength.has() ) switch( roArrowLength.get() )
605 case XML_short: return XML_sm;
606 case XML_medium: return XML_med;
607 case XML_long: return XML_lg;
609 return XML_med;
612 void lclConvertArrow( LineArrowProperties& orArrowProp, const StrokeArrowModel& rStrokeArrow )
614 orArrowProp.moArrowType = lclGetDmlArrowType( rStrokeArrow.moArrowType );
615 orArrowProp.moArrowWidth = lclGetDmlArrowWidth( rStrokeArrow.moArrowWidth );
616 orArrowProp.moArrowLength = lclGetDmlArrowLength( rStrokeArrow.moArrowLength );
619 sal_Int32 lclGetDmlLineCompound( const OptValue< sal_Int32 >& roLineStyle )
621 if( roLineStyle.has() ) switch( roLineStyle.get() )
623 case XML_single: return XML_sng;
624 case XML_thinThin: return XML_dbl;
625 case XML_thinThick: return XML_thinThick;
626 case XML_thickThin: return XML_thickThin;
627 case XML_thickBetweenThin: return XML_tri;
629 return XML_sng;
632 sal_Int32 lclGetDmlLineCap( const OptValue< sal_Int32 >& roEndCap )
634 if( roEndCap.has() ) switch( roEndCap.get() )
636 case XML_flat: return XML_flat;
637 case XML_square: return XML_sq;
638 case XML_round: return XML_rnd;
640 return XML_flat; // different defaults in VML (flat) and DrawingML (square)
643 sal_Int32 lclGetDmlLineJoint( const OptValue< sal_Int32 >& roJoinStyle )
645 if( roJoinStyle.has() ) switch( roJoinStyle.get() )
647 case XML_round: return XML_round;
648 case XML_bevel: return XML_bevel;
649 case XML_miter: return XML_miter;
651 return XML_round;
654 } // namespace
656 void StrokeArrowModel::assignUsed( const StrokeArrowModel& rSource )
658 moArrowType.assignIfUsed( rSource.moArrowType );
659 moArrowWidth.assignIfUsed( rSource.moArrowWidth );
660 moArrowLength.assignIfUsed( rSource.moArrowLength );
663 void StrokeModel::assignUsed( const StrokeModel& rSource )
665 moStroked.assignIfUsed( rSource.moStroked );
666 maStartArrow.assignUsed( rSource.maStartArrow );
667 maEndArrow.assignUsed( rSource.maEndArrow );
668 moColor.assignIfUsed( rSource.moColor );
669 moOpacity.assignIfUsed( rSource.moOpacity );
670 moWeight.assignIfUsed( rSource.moWeight );
671 moDashStyle.assignIfUsed( rSource.moDashStyle );
672 moLineStyle.assignIfUsed( rSource.moLineStyle );
673 moEndCap.assignIfUsed( rSource.moEndCap );
674 moJoinStyle.assignIfUsed( rSource.moJoinStyle );
677 void StrokeModel::pushToPropMap( ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper ) const
679 /* Convert VML line formatting to DrawingML line formatting and let the
680 DrawingML code do the hard work. */
681 LineProperties aLineProps;
683 if( moStroked.get( true ) )
685 aLineProps.maLineFill.moFillType = XML_solidFill;
686 lclConvertArrow( aLineProps.maStartArrow, maStartArrow );
687 lclConvertArrow( aLineProps.maEndArrow, maEndArrow );
688 aLineProps.maLineFill.maFillColor = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_BLACK );
689 aLineProps.moLineWidth = getLimitedValue< sal_Int32, sal_Int64 >( lclGetEmu( rGraphicHelper, moWeight, 1 ), 0, SAL_MAX_INT32 );
690 lclGetDmlLineDash( aLineProps.moPresetDash, aLineProps.maCustomDash, moDashStyle );
691 aLineProps.moLineCompound = lclGetDmlLineCompound( moLineStyle );
692 aLineProps.moLineCap = lclGetDmlLineCap( moEndCap );
693 aLineProps.moLineJoint = lclGetDmlLineJoint( moJoinStyle );
695 else
697 aLineProps.maLineFill.moFillType = XML_noFill;
700 aLineProps.pushToPropMap( rPropMap, rGraphicHelper );
703 void FillModel::assignUsed( const FillModel& rSource )
705 moFilled.assignIfUsed( rSource.moFilled );
706 moColor.assignIfUsed( rSource.moColor );
707 moOpacity.assignIfUsed( rSource.moOpacity );
708 moColor2.assignIfUsed( rSource.moColor2 );
709 moOpacity2.assignIfUsed( rSource.moOpacity2 );
710 moType.assignIfUsed( rSource.moType );
711 moAngle.assignIfUsed( rSource.moAngle );
712 moFocus.assignIfUsed( rSource.moFocus );
713 moFocusPos.assignIfUsed( rSource.moFocusPos );
714 moFocusSize.assignIfUsed( rSource.moFocusSize );
715 moBitmapPath.assignIfUsed( rSource.moBitmapPath );
716 moRotate.assignIfUsed( rSource.moRotate );
719 void lcl_setGradientStop( std::multimap< double, Color >& rMap, const double fKey, const Color& rValue ) {
720 auto aElement = rMap.find( fKey );
722 if (aElement != rMap.end())
723 aElement->second = rValue;
724 else
725 rMap.emplace( fKey, rValue );
728 void FillModel::pushToPropMap( ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper ) const
730 /* Convert VML fill formatting to DrawingML fill formatting and let the
731 DrawingML code do the hard work. */
732 FillProperties aFillProps;
734 if( moFilled.get( true ) )
736 sal_Int32 nFillType = moType.get( XML_solid );
737 switch( nFillType )
739 case XML_gradient:
740 case XML_gradientRadial:
742 aFillProps.moFillType = XML_gradFill;
743 aFillProps.maGradientProps.moRotateWithShape = moRotate.get( false );
744 double fFocus = moFocus.get( 0.0 );
746 // prepare colors
747 Color aColor1 = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_WHITE );
748 Color aColor2 = ConversionHelper::decodeColor( rGraphicHelper, moColor2, moOpacity2, API_RGB_WHITE, aColor1.getColor( rGraphicHelper ) );
750 // type XML_gradient is linear or axial gradient
751 if( nFillType == XML_gradient )
753 // normalize angle to range [0;360) degrees
754 sal_Int32 nVmlAngle = getIntervalValue< sal_Int32, sal_Int32 >( moAngle.get( 0 ), 0, 360 );
756 // focus of -50% or 50% is axial gradient
757 if( ((-0.75 <= fFocus) && (fFocus <= -0.25)) || ((0.25 <= fFocus) && (fFocus <= 0.75)) )
759 /* According to spec, focus of 50% is outer-to-inner,
760 and -50% is inner-to-outer (color to color2).
761 BUT: For angles >= 180 deg., the behaviour is
762 reversed... that's not spec'ed of course. So,
763 [0;180) deg. and 50%, or [180;360) deg. and -50% is
764 outer-to-inner in fact. */
765 bool bOuterToInner = (fFocus > 0.0) == (nVmlAngle < 180);
766 // simulate axial gradient by 3-step DrawingML gradient
767 const Color& rOuterColor = bOuterToInner ? aColor1 : aColor2;
768 const Color& rInnerColor = bOuterToInner ? aColor2 : aColor1;
770 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.0, rOuterColor);
771 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 1.0, rOuterColor);
772 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.5, rInnerColor );
774 else // focus of -100%, 0%, and 100% is linear gradient
776 /* According to spec, focus of -100% or 100% swaps the
777 start and stop colors, effectively reversing the
778 gradient. BUT: For angles >= 180 deg., the
779 behaviour is reversed. This means that in this case
780 a focus of 0% swaps the gradient. */
781 if( fFocus < -0.5 || fFocus > 0.5 )
782 (nVmlAngle += 180) %= 360;
783 // set the start and stop colors
784 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.0, aColor1 );
785 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 1.0, aColor2 );
788 // VML counts counterclockwise from bottom, DrawingML clockwise from left
789 sal_Int32 nDmlAngle = (630 - nVmlAngle) % 360;
790 aFillProps.maGradientProps.moShadeAngle = nDmlAngle * ::oox::drawingml::PER_DEGREE;
792 else // XML_gradientRadial is rectangular gradient
794 aFillProps.maGradientProps.moGradientPath = XML_rect;
795 // convert VML focus position and size to DrawingML fill-to-rect
796 DoublePair aFocusPos = moFocusPos.get( DoublePair( 0.0, 0.0 ) );
797 DoublePair aFocusSize = moFocusSize.get( DoublePair( 0.0, 0.0 ) );
798 double fLeft = getLimitedValue< double, double >( aFocusPos.first, 0.0, 1.0 );
799 double fTop = getLimitedValue< double, double >( aFocusPos.second, 0.0, 1.0 );
800 double fRight = getLimitedValue< double, double >( fLeft + aFocusSize.first, fLeft, 1.0 );
801 double fBottom = getLimitedValue< double, double >( fTop + aFocusSize.second, fTop, 1.0 );
802 aFillProps.maGradientProps.moFillToRect = IntegerRectangle2D(
803 static_cast< sal_Int32 >( fLeft * ::oox::drawingml::MAX_PERCENT ),
804 static_cast< sal_Int32 >( fTop * ::oox::drawingml::MAX_PERCENT ),
805 static_cast< sal_Int32 >( (1.0 - fRight) * ::oox::drawingml::MAX_PERCENT ),
806 static_cast< sal_Int32 >( (1.0 - fBottom) * ::oox::drawingml::MAX_PERCENT ) );
808 // set the start and stop colors (focus of 0% means outer-to-inner)
809 bool bOuterToInner = (-0.5 <= fFocus) && (fFocus <= 0.5);
810 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.0, bOuterToInner ? aColor2 : aColor1 );
811 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 1.0, bOuterToInner ? aColor1 : aColor2 );
814 break;
816 case XML_pattern:
817 case XML_tile:
818 case XML_frame:
820 if( moBitmapPath.has() && !moBitmapPath.get().isEmpty() )
822 aFillProps.maBlipProps.mxFillGraphic = rGraphicHelper.importEmbeddedGraphic(moBitmapPath.get());
823 if (aFillProps.maBlipProps.mxFillGraphic.is())
825 aFillProps.moFillType = XML_blipFill;
826 aFillProps.maBlipProps.moBitmapMode = (nFillType == XML_frame) ? XML_stretch : XML_tile;
827 break; // do not break if bitmap is missing, but run to XML_solid instead
831 SAL_FALLTHROUGH; // to XML_solid in case of missing bitmap path intended!
833 case XML_solid:
834 default:
836 aFillProps.moFillType = XML_solidFill;
837 // fill color (default is white)
838 aFillProps.maFillColor = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_WHITE );
842 else
844 aFillProps.moFillType = XML_noFill;
847 aFillProps.pushToPropMap( rPropMap, rGraphicHelper );
850 ShadowModel::ShadowModel()
851 : mbHasShadow(false)
855 void ShadowModel::pushToPropMap(ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper) const
857 if (!mbHasShadow || (moShadowOn.has() && !moShadowOn.get()))
858 return;
860 drawingml::Color aColor = ConversionHelper::decodeColor(rGraphicHelper, moColor, moOpacity, API_RGB_GRAY);
861 // nOffset* is in mm100, default value is 35 twips, see DffPropertyReader::ApplyAttributes() in msfilter.
862 sal_Int32 nOffsetX = 62, nOffsetY = 62;
863 if (moOffset.has())
865 OUString aOffsetX, aOffsetY;
866 ConversionHelper::separatePair(aOffsetX, aOffsetY, moOffset.get(), ',');
867 if (!aOffsetX.isEmpty())
868 nOffsetX = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, aOffsetX, 0, false, false );
869 if (!aOffsetY.isEmpty())
870 nOffsetY = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, aOffsetY, 0, false, false );
873 table::ShadowFormat aFormat;
874 aFormat.Color = sal_Int32(aColor.getColor(rGraphicHelper));
875 aFormat.Location = table::ShadowLocation_BOTTOM_RIGHT;
876 // The width of the shadow is the average of the x and y values, see SwWW8ImplReader::MatchSdrItemsIntoFlySet().
877 aFormat.ShadowWidth = ((nOffsetX + nOffsetY) / 2);
878 rPropMap.setProperty(PROP_ShadowFormat, aFormat);
881 TextpathModel::TextpathModel()
885 beans::PropertyValue lcl_createTextpathProps()
887 uno::Sequence<beans::PropertyValue> aTextpathPropSeq( comphelper::InitPropertySequence({
888 { "TextPath", uno::Any(true) },
889 { "TextPathMode", uno::Any(drawing::EnhancedCustomShapeTextPathMode_SHAPE) },
890 { "ScaleX", uno::Any(false) },
891 { "SameLetterHeights", uno::Any(false) }
892 }));
894 beans::PropertyValue aRet;
895 aRet.Name = "TextPath";
896 aRet.Value <<= aTextpathPropSeq;
897 return aRet;
900 void TextpathModel::pushToPropMap(ShapePropertyMap& rPropMap, const uno::Reference<drawing::XShape>& xShape, const GraphicHelper& rGraphicHelper) const
902 OUString sFont = "";
904 if (moString.has())
906 uno::Reference<text::XTextRange> xTextRange(xShape, uno::UNO_QUERY);
907 xTextRange->setString(moString.get());
909 uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY);
910 uno::Sequence<beans::PropertyValue> aGeomPropSeq = xPropertySet->getPropertyValue("CustomShapeGeometry").get< uno::Sequence<beans::PropertyValue> >();
911 bool bFound = false;
912 for (int i = 0; i < aGeomPropSeq.getLength(); ++i)
914 beans::PropertyValue& rProp = aGeomPropSeq[i];
915 if (rProp.Name == "TextPath")
917 bFound = true;
918 rProp = lcl_createTextpathProps();
921 if (!bFound)
923 sal_Int32 nSize = aGeomPropSeq.getLength();
924 aGeomPropSeq.realloc(nSize+1);
925 aGeomPropSeq[nSize] = lcl_createTextpathProps();
927 rPropMap.setAnyProperty(PROP_CustomShapeGeometry, uno::makeAny(aGeomPropSeq));
929 if (moStyle.has())
931 OUString aStyle = moStyle.get(OUString());
933 sal_Int32 nIndex = 0;
934 while( nIndex >= 0 )
936 OUString aName, aValue;
937 if (ConversionHelper::separatePair(aName, aValue, aStyle.getToken(0, ';', nIndex), ':'))
939 if (aName == "font-family")
941 // remove " (first, and last character)
942 if (aValue.getLength() > 2)
943 aValue = aValue.copy(1, aValue.getLength() - 2);
945 uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY);
946 xPropertySet->setPropertyValue("CharFontName", uno::makeAny(aValue));
947 sFont = aValue;
949 else if (aName == "font-size")
951 oox::OptValue<OUString> aOptString(aValue);
952 float nSize = drawingml::convertEmuToPoints(lclGetEmu(rGraphicHelper, aOptString, 1));
954 uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY);
955 xPropertySet->setPropertyValue("CharHeight", uno::makeAny(nSize));
960 if (!moTrim.has() || !moTrim.get())
962 OUString sText = moString.get();
963 ScopedVclPtrInstance<VirtualDevice> pDevice;
964 vcl::Font aFont = pDevice->GetFont();
965 aFont.SetFamilyName(sFont);
966 aFont.SetFontSize(Size(0, 96));
967 pDevice->SetFont(aFont);
969 auto nTextWidth = pDevice->GetTextWidth(sText);
970 if (nTextWidth)
972 sal_Int32 nNewHeight = (static_cast<double>(pDevice->GetTextHeight()) / nTextWidth) * xShape->getSize().Width;
973 xShape->setSize(awt::Size(xShape->getSize().Width, nNewHeight));
978 } // namespace vml
979 } // namespace oox
981 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */