bump product version to 6.3.0.0.beta1
[LibreOffice.git] / oox / source / vml / vmlformatting.cxx
blob143ef173b386504319f4587e91888a72cb1806cc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <cstdlib>
24 #include <oox/vml/vmlformatting.hxx>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <com/sun/star/drawing/XShape.hpp>
29 #include <com/sun/star/drawing/EnhancedCustomShapeTextPathMode.hpp>
30 #include <com/sun/star/table/ShadowFormat.hpp>
31 #include <com/sun/star/text/XTextRange.hpp>
32 #include <rtl/strbuf.hxx>
33 #include <sal/log.hxx>
34 #include <osl/diagnose.h>
35 #include <oox/drawingml/color.hxx>
36 #include <oox/drawingml/drawingmltypes.hxx>
37 #include <drawingml/fillproperties.hxx>
38 #include <drawingml/lineproperties.hxx>
39 #include <oox/drawingml/shapepropertymap.hxx>
40 #include <oox/helper/attributelist.hxx>
41 #include <oox/helper/graphichelper.hxx>
42 #include <oox/token/properties.hxx>
43 #include <oox/token/tokens.hxx>
44 #include <svx/svdtrans.hxx>
45 #include <comphelper/propertysequence.hxx>
46 #include <vcl/virdev.hxx>
48 namespace oox {
49 namespace vml {
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::geometry;
54 using ::oox::drawingml::Color;
55 using ::oox::drawingml::FillProperties;
56 using ::oox::drawingml::LineArrowProperties;
57 using ::oox::drawingml::LineProperties;
58 using ::oox::drawingml::ShapePropertyMap;
59 using ::com::sun::star::awt::Point;
60 using ::com::sun::star::drawing::PolygonFlags;
61 using ::com::sun::star::drawing::PolygonFlags_NORMAL;
62 using ::com::sun::star::drawing::PolygonFlags_CONTROL;
64 namespace {
66 bool lclExtractDouble( double& orfValue, sal_Int32& ornEndPos, const OUString& rValue )
68 // extract the double value and find start position of unit characters
69 rtl_math_ConversionStatus eConvStatus = rtl_math_ConversionStatus_Ok;
70 orfValue = ::rtl::math::stringToDouble( rValue, '.', '\0', &eConvStatus, &ornEndPos );
71 return eConvStatus == rtl_math_ConversionStatus_Ok;
74 } // namespace
76 bool ConversionHelper::separatePair( OUString& orValue1, OUString& orValue2,
77 const OUString& rValue, sal_Unicode cSep )
79 sal_Int32 nSepPos = rValue.indexOf( cSep );
80 if( nSepPos >= 0 )
82 orValue1 = rValue.copy( 0, nSepPos ).trim();
83 orValue2 = rValue.copy( nSepPos + 1 ).trim();
85 else
87 orValue1 = rValue.trim();
89 return !orValue1.isEmpty() && !orValue2.isEmpty();
92 bool ConversionHelper::decodeBool( const OUString& rValue )
94 sal_Int32 nToken = AttributeConversion::decodeToken( rValue );
95 // anything else than 't' or 'true' is considered to be false, as specified
96 return (nToken == XML_t) || (nToken == XML_true);
99 double ConversionHelper::decodePercent( const OUString& rValue, double fDefValue )
101 if( rValue.isEmpty() )
102 return fDefValue;
104 double fValue = 0.0;
105 sal_Int32 nEndPos = 0;
106 if( !lclExtractDouble( fValue, nEndPos, rValue ) )
107 return fDefValue;
109 if( nEndPos == rValue.getLength() )
110 return fValue;
112 if( (nEndPos + 1 == rValue.getLength()) && (rValue[ nEndPos ] == '%') )
113 return fValue / 100.0;
115 if( (nEndPos + 1 == rValue.getLength()) && (rValue[ nEndPos ] == 'f') )
116 return fValue / 65536.0;
118 OSL_FAIL( "ConversionHelper::decodePercent - unknown measure unit" );
119 return fDefValue;
122 sal_Int32 ConversionHelper::decodeRotation( const OUString& rValue )
124 if( rValue.isEmpty() )
125 return 0;
127 double fValue = 0.0;
128 double fRotation = 0.0;
129 sal_Int32 nEndPos = 0;
130 if( !lclExtractDouble(fValue, nEndPos, rValue) )
131 return 0;
133 if( nEndPos == rValue.getLength() )
134 fRotation = fValue;
135 else if( (nEndPos + 2 == rValue.getLength()) && (rValue[nEndPos] == 'f') && (rValue[nEndPos+1] == 'd') )
136 fRotation = fValue / 65536.0;
137 else
139 OSL_FAIL("ConversionHelper::decodeRotation - unknown measure unit");
140 return 0;
143 return NormAngle36000(fRotation * -100);
146 sal_Int64 ConversionHelper::decodeMeasureToEmu( const GraphicHelper& rGraphicHelper,
147 const OUString& rValue, sal_Int32 nRefValue, bool bPixelX, bool bDefaultAsPixel )
149 // default for missing values is 0
150 if( rValue.isEmpty() )
151 return 0;
153 // TODO: according to spec, value may contain "auto"
154 if ( rValue == "auto" )
156 OSL_FAIL( "ConversionHelper::decodeMeasureToEmu - special value 'auto' must be handled by caller" );
157 return nRefValue;
160 // extract the double value and find start position of unit characters
161 double fValue = 0.0;
162 sal_Int32 nEndPos = 0;
163 if( !lclExtractDouble( fValue, nEndPos, rValue ) || (fValue == 0.0) )
164 return 0;
166 // process trailing unit, convert to EMU
167 OUString aUnit;
168 if( (0 < nEndPos) && (nEndPos < rValue.getLength()) )
169 aUnit = rValue.copy( nEndPos );
170 else if( bDefaultAsPixel )
171 aUnit = "px";
172 // else default is EMU
174 if( aUnit.getLength() == 2 )
176 sal_Unicode cChar1 = aUnit[ 0 ];
177 sal_Unicode cChar2 = aUnit[ 1 ];
178 if( (cChar1 == 'i') && (cChar2 == 'n') ) // 1 inch = 914,400 EMU
179 fValue *= 914400.0;
180 else if( (cChar1 == 'c') && (cChar2 == 'm') ) // 1 cm = 360,000 EMU
181 fValue *= 360000.0;
182 else if( (cChar1 == 'm') && (cChar2 == 'm') ) // 1 mm = 36,000 EMU
183 fValue *= 36000.0;
184 else if( (cChar1 == 'p') && (cChar2 == 't') ) // 1 point = 1/72 inch = 12,700 EMU
185 fValue *= 12700.0;
186 else if( (cChar1 == 'p') && (cChar2 == 'c') ) // 1 pica = 1/6 inch = 152,400 EMU
187 fValue *= 152400.0;
188 else if( (cChar1 == 'p') && (cChar2 == 'x') ) // 1 pixel, dependent on output device
189 fValue = static_cast< double >( ::oox::drawingml::convertHmmToEmu(
190 bPixelX ?
191 rGraphicHelper.convertScreenPixelXToHmm( fValue ) :
192 rGraphicHelper.convertScreenPixelYToHmm( fValue ) ) );
194 else if( (aUnit.getLength() == 1) && (aUnit[ 0 ] == '%') )
196 fValue *= nRefValue / 100.0;
198 else if( bDefaultAsPixel || !aUnit.isEmpty() ) // default as EMU and no unit -> do nothing
200 OSL_FAIL( "ConversionHelper::decodeMeasureToEmu - unknown measure unit" );
201 fValue = nRefValue;
203 return static_cast< sal_Int64 >( fValue + 0.5 );
206 sal_Int32 ConversionHelper::decodeMeasureToHmm( const GraphicHelper& rGraphicHelper,
207 const OUString& rValue, sal_Int32 nRefValue, bool bPixelX, bool bDefaultAsPixel )
209 return ::oox::drawingml::convertEmuToHmm( decodeMeasureToEmu( rGraphicHelper, rValue, nRefValue, bPixelX, bDefaultAsPixel ) );
212 Color ConversionHelper::decodeColor( const GraphicHelper& rGraphicHelper,
213 const OptValue< OUString >& roVmlColor, const OptValue< double >& roVmlOpacity,
214 ::Color nDefaultRgb, ::Color nPrimaryRgb )
216 Color aDmlColor;
218 // convert opacity
219 const sal_Int32 DML_FULL_OPAQUE = ::oox::drawingml::MAX_PERCENT;
220 double fOpacity = roVmlOpacity.get( 1.0 );
221 sal_Int32 nOpacity = getLimitedValue< sal_Int32, double >( fOpacity * DML_FULL_OPAQUE, 0, DML_FULL_OPAQUE );
222 if( nOpacity < DML_FULL_OPAQUE )
223 aDmlColor.addTransformation( XML_alpha, nOpacity );
225 // color attribute not present - set passed default color
226 if( !roVmlColor.has() )
228 aDmlColor.setSrgbClr( nDefaultRgb );
229 return aDmlColor;
232 // separate leading color name or RGB value from following palette index
233 OUString aColorName, aColorIndex;
234 separatePair( aColorName, aColorIndex, roVmlColor.get(), ' ' );
236 // RGB colors in the format '#RRGGBB'
237 if( (aColorName.getLength() == 7) && (aColorName[ 0 ] == '#') )
239 aDmlColor.setSrgbClr( aColorName.copy( 1 ).toUInt32( 16 ) );
240 return aDmlColor;
243 // RGB colors in the format '#RGB'
244 if( (aColorName.getLength() == 4) && (aColorName[ 0 ] == '#') )
246 sal_Int32 nR = aColorName.copy( 1, 1 ).toUInt32( 16 ) * 0x11;
247 sal_Int32 nG = aColorName.copy( 2, 1 ).toUInt32( 16 ) * 0x11;
248 sal_Int32 nB = aColorName.copy( 3, 1 ).toUInt32( 16 ) * 0x11;
249 aDmlColor.setSrgbClr( (nR << 16) | (nG << 8) | nB );
250 return aDmlColor;
253 /* Predefined color names or system color names (resolve to RGB to detect
254 valid color name). */
255 sal_Int32 nColorToken = AttributeConversion::decodeToken( aColorName );
256 ::Color nRgbValue = Color::getVmlPresetColor( nColorToken, API_RGB_TRANSPARENT );
257 if( nRgbValue == API_RGB_TRANSPARENT )
258 nRgbValue = rGraphicHelper.getSystemColor( nColorToken );
259 if( nRgbValue != API_RGB_TRANSPARENT )
261 aDmlColor.setSrgbClr( nRgbValue );
262 return aDmlColor;
265 // try palette colors enclosed in brackets
266 if( (aColorIndex.getLength() >= 3) && (aColorIndex[ 0 ] == '[') && (aColorIndex[ aColorIndex.getLength() - 1 ] == ']') )
268 aDmlColor.setPaletteClr( aColorIndex.copy( 1, aColorIndex.getLength() - 2 ).toInt32() );
269 return aDmlColor;
272 // try fill gradient modificator 'fill <modifier>(<amount>)'
273 if( (nPrimaryRgb != API_RGB_TRANSPARENT) && (nColorToken == XML_fill) )
275 sal_Int32 nOpenParen = aColorIndex.indexOf( '(' );
276 sal_Int32 nCloseParen = aColorIndex.indexOf( ')' );
277 if( (2 <= nOpenParen) && (nOpenParen + 1 < nCloseParen) && (nCloseParen + 1 == aColorIndex.getLength()) )
279 sal_Int32 nModToken = XML_TOKEN_INVALID;
280 switch( AttributeConversion::decodeToken( aColorIndex.copy( 0, nOpenParen ) ) )
282 case XML_darken: nModToken = XML_shade;break;
283 case XML_lighten: nModToken = XML_tint;
285 sal_Int32 nValue = aColorIndex.copy( nOpenParen + 1, nCloseParen - nOpenParen - 1 ).toInt32();
286 if( (nModToken != XML_TOKEN_INVALID) && (0 <= nValue) && (nValue < 255) )
288 /* Simulate this modifier color by a color with related transformation.
289 The modifier amount has to be converted from the range [0;255] to
290 percentage [0;100000] used by DrawingML. */
291 aDmlColor.setSrgbClr( nPrimaryRgb );
292 aDmlColor.addTransformation( nModToken, static_cast< sal_Int32 >( nValue * ::oox::drawingml::MAX_PERCENT / 255 ) );
293 return aDmlColor;
298 OSL_FAIL( OStringBuffer( "lclGetColor - invalid VML color name '" ).
299 append( OUStringToOString( roVmlColor.get(), RTL_TEXTENCODING_ASCII_US ) ).append( '\'' ).getStr() );
300 aDmlColor.setSrgbClr( nDefaultRgb );
301 return aDmlColor;
304 void ConversionHelper::decodeVmlPath( ::std::vector< ::std::vector< Point > >& rPointLists, ::std::vector< ::std::vector< PolygonFlags > >& rFlagLists, const OUString& rPath )
306 ::std::vector< sal_Int32 > aCoordList;
307 Point aCurrentPoint;
308 sal_Int32 nTokenStart = 0;
309 sal_Int32 nTokenLen = 0;
310 sal_Int32 nParamCount = 0;
311 bool bCommand = false;
312 enum VML_State { START, MOVE_REL, MOVE_ABS, BEZIER_REL, BEZIER_ABS,
313 LINE_REL, LINE_ABS, CLOSE, END, UNSUPPORTED };
314 VML_State state = START;
316 rPointLists.emplace_back( );
317 rFlagLists.emplace_back( );
319 for ( sal_Int32 i = 0; i < rPath.getLength(); i++ )
321 // Keep track of current integer token
322 if ( ( rPath[ i ] >= '0' && rPath[ i ] <= '9' ) || rPath[ i ] == '-' )
323 nTokenLen++;
324 else if ( rPath[ i ] != ' ' )
326 // Store coordinate from current token
327 if ( state != START && state != UNSUPPORTED )
329 if ( nTokenLen > 0 )
330 aCoordList.push_back( rPath.copy( nTokenStart, nTokenLen ).toInt32() );
331 else
332 aCoordList.push_back( 0 );
333 nTokenLen = 0;
336 if (rPath[ i ] == ',' )
338 nParamCount--;
341 // Upon finding the next command code, deal with stored
342 // coordinates for previous command and reset parameters counter if needed.
343 // See http://www.w3.org/TR/NOTE-VML#_Toc416858382 for params count reference
344 if ( rPath[ i ] != ',' || nParamCount == 0 )
346 switch ( state )
348 case MOVE_REL:
349 aCoordList.resize(2, 0); // 2* params -> param count reset
350 if ( !rPointLists.empty() && !rPointLists.back().empty() )
352 rPointLists.emplace_back( );
353 rFlagLists.emplace_back( );
355 rPointLists.back().emplace_back( aCoordList[ 0 ], aCoordList[ 1 ] );
356 rFlagLists.back().push_back( PolygonFlags_NORMAL );
357 aCurrentPoint = rPointLists.back().back();
358 nParamCount = 2;
359 break;
361 case MOVE_ABS:
362 aCoordList.resize(2, 0); // 2 params -> no param count reset
363 if ( !rPointLists.empty() && !rPointLists.back().empty() )
365 rPointLists.emplace_back( );
366 rFlagLists.emplace_back( );
368 rPointLists.back().emplace_back( (aCoordList[ 0 ]), aCoordList[ 1 ] );
369 rFlagLists.back().push_back( PolygonFlags_NORMAL );
370 aCurrentPoint = rPointLists.back().back();
371 break;
373 case BEZIER_REL:
374 aCoordList.resize(6, 0); // 6* params -> param count reset
375 rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 0 ],
376 aCurrentPoint.Y + aCoordList[ 1 ] );
377 rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 2 ],
378 aCurrentPoint.Y + aCoordList[ 3 ] );
379 rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 4 ],
380 aCurrentPoint.Y + aCoordList[ 5 ] );
381 rFlagLists.back().push_back( PolygonFlags_CONTROL );
382 rFlagLists.back().push_back( PolygonFlags_CONTROL );
383 rFlagLists.back().push_back( PolygonFlags_NORMAL );
384 aCurrentPoint = rPointLists.back().back();
385 nParamCount = 6;
386 break;
388 case BEZIER_ABS:
389 aCoordList.resize(6, 0); // 6* params -> param count reset
390 rPointLists.back().emplace_back( aCoordList[ 0 ], aCoordList[ 1 ] );
391 rPointLists.back().emplace_back( aCoordList[ 2 ], aCoordList[ 3 ] );
392 rPointLists.back().emplace_back( aCoordList[ 4 ], aCoordList[ 5 ] );
393 rFlagLists.back().push_back( PolygonFlags_CONTROL );
394 rFlagLists.back().push_back( PolygonFlags_CONTROL );
395 rFlagLists.back().push_back( PolygonFlags_NORMAL );
396 aCurrentPoint = rPointLists.back().back();
397 nParamCount = 6;
398 break;
400 case LINE_REL:
401 aCoordList.resize(2, 0); // 2* params -> param count reset
402 rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 0 ],
403 aCurrentPoint.Y + aCoordList[ 1 ] );
404 rFlagLists.back().push_back( PolygonFlags_NORMAL );
405 aCurrentPoint = rPointLists.back().back();
406 nParamCount = 2;
407 break;
409 case LINE_ABS:
410 aCoordList.resize(2, 0); // 2* params -> param count reset
411 rPointLists.back().emplace_back( aCoordList[ 0 ], (aCoordList.size() > 1 ? aCoordList[ 1 ] : 0) );
412 rFlagLists.back().push_back( PolygonFlags_NORMAL );
413 aCurrentPoint = rPointLists.back().back();
414 nParamCount = 2;
415 break;
417 case CLOSE: // 0 param
418 SAL_WARN_IF(rPointLists.back().empty() || rFlagLists.back().empty(), "oox", "empty pointlists at close");
419 if (!rPointLists.back().empty() && !rFlagLists.back().empty())
421 rPointLists.back().push_back( rPointLists.back()[ 0 ] );
422 rFlagLists.back().push_back( rFlagLists.back()[ 0 ] );
423 aCurrentPoint = rPointLists.back().back();
425 break;
427 case END: // 0 param
428 rPointLists.emplace_back( );
429 rFlagLists.emplace_back( );
430 break;
432 case START:
433 case UNSUPPORTED:
434 break;
437 aCoordList.clear();
440 // Allow two-char commands to peek ahead to the next character
441 sal_Unicode nextChar = '\0';
442 if (i+1 < rPath.getLength())
443 nextChar = rPath[i+1];
445 // Move to relevant state upon finding a command
446 bCommand = true;
447 switch ( rPath[ i ] )
449 // Single-character commands
450 case 't': // rmoveto
451 state = MOVE_REL; nParamCount = 2; break;
452 case 'm': // moveto
453 state = MOVE_ABS; nParamCount = 2; break;
454 case 'v': // rcurveto
455 state = BEZIER_REL; nParamCount = 6; break;
456 case 'c': // curveto
457 state = BEZIER_ABS; nParamCount = 6; break;
458 case 'r': // rlineto
459 state = LINE_REL; nParamCount = 2; break;
460 case 'l': // lineto
461 state = LINE_ABS; nParamCount = 2; break;
462 case 'x': // close
463 state = CLOSE; break;
464 case 'e': // end
465 state = END; break;
467 // Two-character commands
468 case 'n':
470 switch ( nextChar )
472 case 'f': // nf - nofill
473 case 's': // ns - nostroke
474 state = UNSUPPORTED; i++; break;
476 break;
478 case 'a': // Elliptical curves
480 switch ( nextChar )
482 case 'e': // ae - angleellipseto
483 case 'l': // al - angleellipse
484 state = UNSUPPORTED; i++; break;
485 case 't': // at - arcto
486 case 'r': // ar - arc
487 state = UNSUPPORTED; i++; break;
489 break;
491 case 'w': // Clockwise elliptical arcs
493 switch ( nextChar )
495 case 'a': // wa - clockwisearcto
496 case 'r': // wr - clockwisearc
497 state = UNSUPPORTED; i++; break;
499 break;
501 case 'q':
503 switch ( nextChar )
505 case 'x': // qx - ellipticalquadrantx
506 case 'y': // qy - ellipticalquadranty
507 state = UNSUPPORTED; i++; break;
508 case 'b': // qb - quadraticbezier
509 state = UNSUPPORTED; i++; break;
511 break;
513 case 'h': // behaviour extensions
515 switch ( nextChar )
517 case 'a': // ha - AutoLine
518 case 'b': // hb - AutoCurve
519 case 'c': // hc - CornerLine
520 case 'd': // hd - CornerCurve
521 case 'e': // he - SmoothLine
522 case 'f': // hf - SmoothCurve
523 case 'g': // hg - SymmetricLine
524 case 'h': // hh - SymmetricCurve
525 case 'i': // hi - Freeform
526 state = UNSUPPORTED; i++; break;
528 break;
530 default:
531 bCommand = false;
532 break;
535 if (bCommand) nTokenLen = 0;
536 nTokenStart = i+1;
541 namespace {
543 sal_Int64 lclGetEmu( const GraphicHelper& rGraphicHelper, const OptValue< OUString >& roValue, sal_Int64 nDefValue )
545 return roValue.has() ? ConversionHelper::decodeMeasureToEmu( rGraphicHelper, roValue.get(), 0, false, false ) : nDefValue;
548 void lclGetDmlLineDash( OptValue< sal_Int32 >& oroPresetDash, LineProperties::DashStopVector& orCustomDash, const OptValue< OUString >& roDashStyle )
550 if( roDashStyle.has() )
552 const OUString& rDashStyle = roDashStyle.get();
553 switch( AttributeConversion::decodeToken( rDashStyle ) )
555 case XML_solid: oroPresetDash = XML_solid; return;
556 case XML_shortdot: oroPresetDash = XML_sysDot; return;
557 case XML_shortdash: oroPresetDash = XML_sysDash; return;
558 case XML_shortdashdot: oroPresetDash = XML_sysDashDot; return;
559 case XML_shortdashdotdot: oroPresetDash = XML_sysDashDotDot; return;
560 case XML_dot: oroPresetDash = XML_dot; return;
561 case XML_dash: oroPresetDash = XML_dash; return;
562 case XML_dashdot: oroPresetDash = XML_dashDot; return;
563 case XML_longdash: oroPresetDash = XML_lgDash; return;
564 case XML_longdashdot: oroPresetDash = XML_lgDashDot; return;
565 case XML_longdashdotdot: oroPresetDash = XML_lgDashDotDot; return;
567 // try to convert user-defined dash style
568 default:
570 ::std::vector< sal_Int32 > aValues;
571 sal_Int32 nIndex = 0;
572 while( nIndex >= 0 )
573 aValues.push_back( rDashStyle.getToken( 0, ' ', nIndex ).toInt32() );
574 size_t nPairs = aValues.size() / 2; // ignore last value if size is odd
575 for( size_t nPairIdx = 0; nPairIdx < nPairs; ++nPairIdx )
576 orCustomDash.emplace_back( aValues[ 2 * nPairIdx ], aValues[ 2 * nPairIdx + 1 ] );
582 sal_Int32 lclGetDmlArrowType( const OptValue< sal_Int32 >& roArrowType )
584 if( roArrowType.has() ) switch( roArrowType.get() )
586 case XML_none: return XML_none;
587 case XML_block: return XML_triangle;
588 case XML_classic: return XML_stealth;
589 case XML_diamond: return XML_diamond;
590 case XML_oval: return XML_oval;
591 case XML_open: return XML_arrow;
593 return XML_none;
596 sal_Int32 lclGetDmlArrowWidth( const OptValue< sal_Int32 >& roArrowWidth )
598 if( roArrowWidth.has() ) switch( roArrowWidth.get() )
600 case XML_narrow: return XML_sm;
601 case XML_medium: return XML_med;
602 case XML_wide: return XML_lg;
604 return XML_med;
607 sal_Int32 lclGetDmlArrowLength( const OptValue< sal_Int32 >& roArrowLength )
609 if( roArrowLength.has() ) switch( roArrowLength.get() )
611 case XML_short: return XML_sm;
612 case XML_medium: return XML_med;
613 case XML_long: return XML_lg;
615 return XML_med;
618 void lclConvertArrow( LineArrowProperties& orArrowProp, const StrokeArrowModel& rStrokeArrow )
620 orArrowProp.moArrowType = lclGetDmlArrowType( rStrokeArrow.moArrowType );
621 orArrowProp.moArrowWidth = lclGetDmlArrowWidth( rStrokeArrow.moArrowWidth );
622 orArrowProp.moArrowLength = lclGetDmlArrowLength( rStrokeArrow.moArrowLength );
625 sal_Int32 lclGetDmlLineCompound( const OptValue< sal_Int32 >& roLineStyle )
627 if( roLineStyle.has() ) switch( roLineStyle.get() )
629 case XML_single: return XML_sng;
630 case XML_thinThin: return XML_dbl;
631 case XML_thinThick: return XML_thinThick;
632 case XML_thickThin: return XML_thickThin;
633 case XML_thickBetweenThin: return XML_tri;
635 return XML_sng;
638 sal_Int32 lclGetDmlLineCap( const OptValue< sal_Int32 >& roEndCap )
640 if( roEndCap.has() ) switch( roEndCap.get() )
642 case XML_flat: return XML_flat;
643 case XML_square: return XML_sq;
644 case XML_round: return XML_rnd;
646 return XML_flat; // different defaults in VML (flat) and DrawingML (square)
649 sal_Int32 lclGetDmlLineJoint( const OptValue< sal_Int32 >& roJoinStyle )
651 if( roJoinStyle.has() ) switch( roJoinStyle.get() )
653 case XML_round: return XML_round;
654 case XML_bevel: return XML_bevel;
655 case XML_miter: return XML_miter;
657 return XML_round;
660 } // namespace
662 void StrokeArrowModel::assignUsed( const StrokeArrowModel& rSource )
664 moArrowType.assignIfUsed( rSource.moArrowType );
665 moArrowWidth.assignIfUsed( rSource.moArrowWidth );
666 moArrowLength.assignIfUsed( rSource.moArrowLength );
669 void StrokeModel::assignUsed( const StrokeModel& rSource )
671 moStroked.assignIfUsed( rSource.moStroked );
672 maStartArrow.assignUsed( rSource.maStartArrow );
673 maEndArrow.assignUsed( rSource.maEndArrow );
674 moColor.assignIfUsed( rSource.moColor );
675 moOpacity.assignIfUsed( rSource.moOpacity );
676 moWeight.assignIfUsed( rSource.moWeight );
677 moDashStyle.assignIfUsed( rSource.moDashStyle );
678 moLineStyle.assignIfUsed( rSource.moLineStyle );
679 moEndCap.assignIfUsed( rSource.moEndCap );
680 moJoinStyle.assignIfUsed( rSource.moJoinStyle );
683 void StrokeModel::pushToPropMap( ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper ) const
685 /* Convert VML line formatting to DrawingML line formatting and let the
686 DrawingML code do the hard work. */
687 LineProperties aLineProps;
689 if( moStroked.get( true ) )
691 aLineProps.maLineFill.moFillType = XML_solidFill;
692 lclConvertArrow( aLineProps.maStartArrow, maStartArrow );
693 lclConvertArrow( aLineProps.maEndArrow, maEndArrow );
694 aLineProps.maLineFill.maFillColor = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_BLACK );
695 aLineProps.moLineWidth = getLimitedValue< sal_Int32, sal_Int64 >( lclGetEmu( rGraphicHelper, moWeight, 1 ), 0, SAL_MAX_INT32 );
696 lclGetDmlLineDash( aLineProps.moPresetDash, aLineProps.maCustomDash, moDashStyle );
697 aLineProps.moLineCompound = lclGetDmlLineCompound( moLineStyle );
698 aLineProps.moLineCap = lclGetDmlLineCap( moEndCap );
699 aLineProps.moLineJoint = lclGetDmlLineJoint( moJoinStyle );
701 else
703 aLineProps.maLineFill.moFillType = XML_noFill;
706 aLineProps.pushToPropMap( rPropMap, rGraphicHelper );
709 void FillModel::assignUsed( const FillModel& rSource )
711 moFilled.assignIfUsed( rSource.moFilled );
712 moColor.assignIfUsed( rSource.moColor );
713 moOpacity.assignIfUsed( rSource.moOpacity );
714 moColor2.assignIfUsed( rSource.moColor2 );
715 moOpacity2.assignIfUsed( rSource.moOpacity2 );
716 moType.assignIfUsed( rSource.moType );
717 moAngle.assignIfUsed( rSource.moAngle );
718 moFocus.assignIfUsed( rSource.moFocus );
719 moFocusPos.assignIfUsed( rSource.moFocusPos );
720 moFocusSize.assignIfUsed( rSource.moFocusSize );
721 moBitmapPath.assignIfUsed( rSource.moBitmapPath );
722 moRotate.assignIfUsed( rSource.moRotate );
725 static void lcl_setGradientStop( std::multimap< double, Color >& rMap, const double fKey, const Color& rValue ) {
726 auto aElement = rMap.find( fKey );
728 if (aElement != rMap.end())
729 aElement->second = rValue;
730 else
731 rMap.emplace( fKey, rValue );
734 void FillModel::pushToPropMap( ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper ) const
736 /* Convert VML fill formatting to DrawingML fill formatting and let the
737 DrawingML code do the hard work. */
738 FillProperties aFillProps;
740 if( moFilled.get( true ) )
742 sal_Int32 nFillType = moType.get( XML_solid );
743 switch( nFillType )
745 case XML_gradient:
746 case XML_gradientRadial:
748 aFillProps.moFillType = XML_gradFill;
749 aFillProps.maGradientProps.moRotateWithShape = moRotate.get( false );
750 double fFocus = moFocus.get( 0.0 );
752 // prepare colors
753 Color aColor1 = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_WHITE );
754 Color aColor2 = ConversionHelper::decodeColor( rGraphicHelper, moColor2, moOpacity2, API_RGB_WHITE, aColor1.getColor( rGraphicHelper ) );
756 // type XML_gradient is linear or axial gradient
757 if( nFillType == XML_gradient )
759 // normalize angle to range [0;360) degrees
760 sal_Int32 nVmlAngle = getIntervalValue< sal_Int32, sal_Int32 >( moAngle.get( 0 ), 0, 360 );
762 // focus of -50% or 50% is axial gradient
763 if( ((-0.75 <= fFocus) && (fFocus <= -0.25)) || ((0.25 <= fFocus) && (fFocus <= 0.75)) )
765 /* According to spec, focus of 50% is outer-to-inner,
766 and -50% is inner-to-outer (color to color2).
767 BUT: For angles >= 180 deg., the behaviour is
768 reversed... that's not spec'ed of course. So,
769 [0;180) deg. and 50%, or [180;360) deg. and -50% is
770 outer-to-inner in fact. */
771 bool bOuterToInner = (fFocus > 0.0) == (nVmlAngle < 180);
772 // simulate axial gradient by 3-step DrawingML gradient
773 const Color& rOuterColor = bOuterToInner ? aColor1 : aColor2;
774 const Color& rInnerColor = bOuterToInner ? aColor2 : aColor1;
776 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.0, rOuterColor);
777 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 1.0, rOuterColor);
778 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.5, rInnerColor );
780 else // focus of -100%, 0%, and 100% is linear gradient
782 /* According to spec, focus of -100% or 100% swaps the
783 start and stop colors, effectively reversing the
784 gradient. BUT: For angles >= 180 deg., the
785 behaviour is reversed. This means that in this case
786 a focus of 0% swaps the gradient. */
787 if( fFocus < -0.5 || fFocus > 0.5 )
788 nVmlAngle = (nVmlAngle + 180) % 360;
789 // set the start and stop colors
790 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.0, aColor1 );
791 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 1.0, aColor2 );
794 // VML counts counterclockwise from bottom, DrawingML clockwise from left
795 sal_Int32 nDmlAngle = (630 - nVmlAngle) % 360;
796 aFillProps.maGradientProps.moShadeAngle = nDmlAngle * ::oox::drawingml::PER_DEGREE;
798 else // XML_gradientRadial is rectangular gradient
800 aFillProps.maGradientProps.moGradientPath = XML_rect;
801 // convert VML focus position and size to DrawingML fill-to-rect
802 DoublePair aFocusPos = moFocusPos.get( DoublePair( 0.0, 0.0 ) );
803 DoublePair aFocusSize = moFocusSize.get( DoublePair( 0.0, 0.0 ) );
804 double fLeft = getLimitedValue< double, double >( aFocusPos.first, 0.0, 1.0 );
805 double fTop = getLimitedValue< double, double >( aFocusPos.second, 0.0, 1.0 );
806 double fRight = getLimitedValue< double, double >( fLeft + aFocusSize.first, fLeft, 1.0 );
807 double fBottom = getLimitedValue< double, double >( fTop + aFocusSize.second, fTop, 1.0 );
808 aFillProps.maGradientProps.moFillToRect = IntegerRectangle2D(
809 static_cast< sal_Int32 >( fLeft * ::oox::drawingml::MAX_PERCENT ),
810 static_cast< sal_Int32 >( fTop * ::oox::drawingml::MAX_PERCENT ),
811 static_cast< sal_Int32 >( (1.0 - fRight) * ::oox::drawingml::MAX_PERCENT ),
812 static_cast< sal_Int32 >( (1.0 - fBottom) * ::oox::drawingml::MAX_PERCENT ) );
814 // set the start and stop colors (focus of 0% means outer-to-inner)
815 bool bOuterToInner = (-0.5 <= fFocus) && (fFocus <= 0.5);
816 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.0, bOuterToInner ? aColor2 : aColor1 );
817 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 1.0, bOuterToInner ? aColor1 : aColor2 );
820 break;
822 case XML_pattern:
823 case XML_tile:
824 case XML_frame:
826 if( moBitmapPath.has() && !moBitmapPath.get().isEmpty() )
828 aFillProps.maBlipProps.mxFillGraphic = rGraphicHelper.importEmbeddedGraphic(moBitmapPath.get());
829 if (aFillProps.maBlipProps.mxFillGraphic.is())
831 aFillProps.moFillType = XML_blipFill;
832 aFillProps.maBlipProps.moBitmapMode = (nFillType == XML_frame) ? XML_stretch : XML_tile;
833 break; // do not break if bitmap is missing, but run to XML_solid instead
837 [[fallthrough]]; // to XML_solid in case of missing bitmap path intended!
839 case XML_solid:
840 default:
842 aFillProps.moFillType = XML_solidFill;
843 // fill color (default is white)
844 aFillProps.maFillColor = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_WHITE );
848 else
850 aFillProps.moFillType = XML_noFill;
853 aFillProps.pushToPropMap( rPropMap, rGraphicHelper );
856 ShadowModel::ShadowModel()
857 : mbHasShadow(false)
861 void ShadowModel::pushToPropMap(ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper) const
863 if (!mbHasShadow || (moShadowOn.has() && !moShadowOn.get()))
864 return;
866 drawingml::Color aColor = ConversionHelper::decodeColor(rGraphicHelper, moColor, moOpacity, API_RGB_GRAY);
867 // nOffset* is in mm100, default value is 35 twips, see DffPropertyReader::ApplyAttributes() in msfilter.
868 sal_Int32 nOffsetX = 62, nOffsetY = 62;
869 if (moOffset.has())
871 OUString aOffsetX, aOffsetY;
872 ConversionHelper::separatePair(aOffsetX, aOffsetY, moOffset.get(), ',');
873 if (!aOffsetX.isEmpty())
874 nOffsetX = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, aOffsetX, 0, false, false );
875 if (!aOffsetY.isEmpty())
876 nOffsetY = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, aOffsetY, 0, false, false );
879 table::ShadowFormat aFormat;
880 aFormat.Color = sal_Int32(aColor.getColor(rGraphicHelper));
881 aFormat.Location = nOffsetX < 0
882 ? nOffsetY < 0 ? table::ShadowLocation_TOP_LEFT : table::ShadowLocation_BOTTOM_LEFT
883 : nOffsetY < 0 ? table::ShadowLocation_TOP_RIGHT : table::ShadowLocation_BOTTOM_RIGHT;
884 // The width of the shadow is the average of the x and y values, see SwWW8ImplReader::MatchSdrItemsIntoFlySet().
885 aFormat.ShadowWidth = ((std::abs(nOffsetX) + std::abs(nOffsetY)) / 2);
886 rPropMap.setProperty(PROP_ShadowFormat, aFormat);
889 TextpathModel::TextpathModel()
893 static beans::PropertyValue lcl_createTextpathProps()
895 uno::Sequence<beans::PropertyValue> aTextpathPropSeq( comphelper::InitPropertySequence({
896 { "TextPath", uno::Any(true) },
897 { "TextPathMode", uno::Any(drawing::EnhancedCustomShapeTextPathMode_SHAPE) },
898 { "ScaleX", uno::Any(false) },
899 { "SameLetterHeights", uno::Any(false) }
900 }));
902 beans::PropertyValue aRet;
903 aRet.Name = "TextPath";
904 aRet.Value <<= aTextpathPropSeq;
905 return aRet;
908 void TextpathModel::pushToPropMap(ShapePropertyMap& rPropMap, const uno::Reference<drawing::XShape>& xShape, const GraphicHelper& rGraphicHelper) const
910 OUString sFont = "";
912 if (moString.has())
914 uno::Reference<text::XTextRange> xTextRange(xShape, uno::UNO_QUERY);
915 xTextRange->setString(moString.get());
917 uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY);
918 uno::Sequence<beans::PropertyValue> aGeomPropSeq = xPropertySet->getPropertyValue("CustomShapeGeometry").get< uno::Sequence<beans::PropertyValue> >();
919 bool bFound = false;
920 for (int i = 0; i < aGeomPropSeq.getLength(); ++i)
922 beans::PropertyValue& rProp = aGeomPropSeq[i];
923 if (rProp.Name == "TextPath")
925 bFound = true;
926 rProp = lcl_createTextpathProps();
929 if (!bFound)
931 sal_Int32 nSize = aGeomPropSeq.getLength();
932 aGeomPropSeq.realloc(nSize+1);
933 aGeomPropSeq[nSize] = lcl_createTextpathProps();
935 rPropMap.setAnyProperty(PROP_CustomShapeGeometry, uno::makeAny(aGeomPropSeq));
937 if (moStyle.has())
939 OUString aStyle = moStyle.get(OUString());
941 sal_Int32 nIndex = 0;
942 while( nIndex >= 0 )
944 OUString aName, aValue;
945 if (ConversionHelper::separatePair(aName, aValue, aStyle.getToken(0, ';', nIndex), ':'))
947 if (aName == "font-family")
949 // remove " (first, and last character)
950 if (aValue.getLength() > 2)
951 aValue = aValue.copy(1, aValue.getLength() - 2);
953 uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY);
954 xPropertySet->setPropertyValue("CharFontName", uno::makeAny(aValue));
955 sFont = aValue;
957 else if (aName == "font-size")
959 oox::OptValue<OUString> aOptString(aValue);
960 float nSize = drawingml::convertEmuToPoints(lclGetEmu(rGraphicHelper, aOptString, 1));
962 uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY);
963 xPropertySet->setPropertyValue("CharHeight", uno::makeAny(nSize));
968 if (!moTrim.has() || !moTrim.get())
970 OUString sText = moString.get();
971 ScopedVclPtrInstance<VirtualDevice> pDevice;
972 vcl::Font aFont = pDevice->GetFont();
973 aFont.SetFamilyName(sFont);
974 aFont.SetFontSize(Size(0, 96));
975 pDevice->SetFont(aFont);
977 auto nTextWidth = pDevice->GetTextWidth(sText);
978 if (nTextWidth)
980 sal_Int32 nNewHeight = (static_cast<double>(pDevice->GetTextHeight()) / nTextWidth) * xShape->getSize().Width;
981 xShape->setSize(awt::Size(xShape->getSize().Width, nNewHeight));
986 } // namespace vml
987 } // namespace oox
989 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */