Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / oox / source / vml / vmlformatting.cxx
blob029d5429d921b2cb0e5d541f72da012985f7f07c
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 <o3tl/float_int_conversion.hxx>
33 #include <o3tl/unit_conversion.hxx>
34 #include <rtl/strbuf.hxx>
35 #include <sal/log.hxx>
36 #include <osl/diagnose.h>
37 #include <oox/drawingml/color.hxx>
38 #include <oox/drawingml/drawingmltypes.hxx>
39 #include <drawingml/fillproperties.hxx>
40 #include <drawingml/lineproperties.hxx>
41 #include <oox/drawingml/shapepropertymap.hxx>
42 #include <oox/helper/attributelist.hxx>
43 #include <oox/helper/graphichelper.hxx>
44 #include <oox/token/properties.hxx>
45 #include <oox/token/tokens.hxx>
46 #include <svx/svdtrans.hxx>
47 #include <comphelper/propertysequence.hxx>
48 #include <o3tl/string_view.hxx>
49 #include <vcl/virdev.hxx>
51 namespace oox::vml {
53 using namespace ::com::sun::star;
54 using namespace ::com::sun::star::geometry;
56 using ::oox::drawingml::Color;
57 using ::oox::drawingml::FillProperties;
58 using ::oox::drawingml::LineArrowProperties;
59 using ::oox::drawingml::LineProperties;
60 using ::oox::drawingml::ShapePropertyMap;
61 using ::com::sun::star::awt::Point;
62 using ::com::sun::star::drawing::PolygonFlags;
63 using ::com::sun::star::drawing::PolygonFlags_NORMAL;
64 using ::com::sun::star::drawing::PolygonFlags_CONTROL;
66 namespace {
68 bool lclExtractDouble( double& orfValue, size_t& ornEndPos, std::u16string_view aValue )
70 // extract the double value and find start position of unit characters
71 rtl_math_ConversionStatus eConvStatus = rtl_math_ConversionStatus_Ok;
72 sal_Int32 nEndPos;
73 orfValue = ::rtl::math::stringToDouble( aValue, '.', '\0', &eConvStatus, &nEndPos );
74 ornEndPos = nEndPos;
75 return eConvStatus == rtl_math_ConversionStatus_Ok;
78 } // namespace
80 bool ConversionHelper::separatePair( std::u16string_view& orValue1, std::u16string_view& orValue2,
81 std::u16string_view rValue, sal_Unicode cSep )
83 size_t nSepPos = rValue.find( cSep );
84 if( nSepPos != std::u16string_view::npos )
86 orValue1 = o3tl::trim(rValue.substr( 0, nSepPos ));
87 orValue2 = o3tl::trim(rValue.substr( nSepPos + 1 ));
89 else
91 orValue1 = o3tl::trim(rValue);
92 orValue2 = std::u16string_view();
94 return !orValue1.empty() && !orValue2.empty();
97 bool ConversionHelper::decodeBool( std::u16string_view rValue )
99 sal_Int32 nToken = AttributeConversion::decodeToken( rValue );
100 // anything else than 't' or 'true' is considered to be false, as specified
101 return (nToken == XML_t) || (nToken == XML_true);
104 double ConversionHelper::decodePercent( std::u16string_view rValue, double fDefValue )
106 if( rValue.empty() )
107 return fDefValue;
109 double fValue = 0.0;
110 size_t nEndPos = 0;
111 if( !lclExtractDouble( fValue, nEndPos, rValue ) )
112 return fDefValue;
114 if( nEndPos == rValue.size() )
115 return fValue;
117 if( (nEndPos + 1 == rValue.size()) && (rValue[ nEndPos ] == '%') )
118 return fValue / 100.0;
120 if( (nEndPos + 1 == rValue.size()) && (rValue[ nEndPos ] == 'f') )
121 return fValue / 65536.0;
123 OSL_FAIL( "ConversionHelper::decodePercent - unknown measure unit" );
124 return fDefValue;
127 Degree100 ConversionHelper::decodeRotation( std::u16string_view rValue )
129 if( rValue.empty() )
130 return 0_deg100;
132 double fValue = 0.0;
133 double fRotation = 0.0;
134 size_t nEndPos = 0;
135 if( !lclExtractDouble(fValue, nEndPos, rValue) )
136 return 0_deg100;
138 if( nEndPos == rValue.size() )
139 fRotation = fValue;
140 else if( (nEndPos + 2 == rValue.size()) && (rValue[nEndPos] == 'f') && (rValue[nEndPos+1] == 'd') )
141 fRotation = fValue / 65536.0;
142 else
144 OSL_FAIL("ConversionHelper::decodeRotation - unknown measure unit");
145 return 0_deg100;
148 return NormAngle36000(Degree100(static_cast<sal_Int32>(fRotation * -100)));
151 sal_Int64 ConversionHelper::decodeMeasureToEmu( const GraphicHelper& rGraphicHelper,
152 std::u16string_view rValue, sal_Int32 nRefValue, bool bPixelX, bool bDefaultAsPixel )
154 // default for missing values is 0
155 if( rValue.empty() )
156 return 0;
158 // TODO: according to spec, value may contain "auto"
159 if ( rValue == u"auto" )
161 OSL_FAIL( "ConversionHelper::decodeMeasureToEmu - special value 'auto' must be handled by caller" );
162 return nRefValue;
165 // extract the double value and find start position of unit characters
166 double fValue = 0.0;
167 size_t nEndPos = 0;
168 if( !lclExtractDouble( fValue, nEndPos, rValue ) || (fValue == 0.0) )
169 return 0;
171 // process trailing unit, convert to EMU
172 std::u16string_view aUnit;
173 if( (0 < nEndPos) && (nEndPos < rValue.size()) )
174 aUnit = rValue.substr( nEndPos );
175 else if( bDefaultAsPixel )
176 aUnit = u"px";
177 // else default is EMU
179 if( aUnit.size() == 2 )
181 sal_Unicode cChar1 = aUnit[ 0 ];
182 sal_Unicode cChar2 = aUnit[ 1 ];
183 if ((cChar1 == 'i') && (cChar2 == 'n'))
184 fValue = o3tl::convert(fValue, o3tl::Length::in, o3tl::Length::emu);
185 else if ((cChar1 == 'c') && (cChar2 == 'm'))
186 fValue = o3tl::convert(fValue, o3tl::Length::cm, o3tl::Length::emu);
187 else if ((cChar1 == 'm') && (cChar2 == 'm'))
188 fValue = o3tl::convert(fValue, o3tl::Length::mm, o3tl::Length::emu);
189 else if ((cChar1 == 'p') && (cChar2 == 't'))
190 fValue = o3tl::convert(fValue, o3tl::Length::pt, o3tl::Length::emu);
191 else if ((cChar1 == 'p') && (cChar2 == 'c'))
192 fValue = o3tl::convert(fValue, o3tl::Length::pc, o3tl::Length::emu);
193 else if( (cChar1 == 'p') && (cChar2 == 'x') ) // 1 pixel, dependent on output device
194 fValue = o3tl::convert(bPixelX ? rGraphicHelper.convertScreenPixelXToHmm(fValue)
195 : rGraphicHelper.convertScreenPixelYToHmm(fValue),
196 o3tl::Length::mm100, o3tl::Length::emu);
198 else if( (aUnit.size() == 1) && (aUnit[ 0 ] == '%') )
200 fValue *= nRefValue / 100.0;
202 else if( bDefaultAsPixel || !aUnit.empty() ) // default as EMU and no unit -> do nothing
204 OSL_FAIL( "ConversionHelper::decodeMeasureToEmu - unknown measure unit" );
205 fValue = nRefValue;
207 return o3tl::saturating_cast< sal_Int64 >( fValue + 0.5 );
210 sal_Int32 ConversionHelper::decodeMeasureToHmm( const GraphicHelper& rGraphicHelper,
211 std::u16string_view rValue, sal_Int32 nRefValue, bool bPixelX, bool bDefaultAsPixel )
213 return ::oox::drawingml::convertEmuToHmm( decodeMeasureToEmu( rGraphicHelper, rValue, nRefValue, bPixelX, bDefaultAsPixel ) );
216 sal_Int32 ConversionHelper::decodeMeasureToTwip(const GraphicHelper& rGraphicHelper,
217 std::u16string_view rValue, sal_Int32 nRefValue,
218 bool bPixelX, bool bDefaultAsPixel)
220 return ::o3tl::convert(
221 decodeMeasureToEmu(rGraphicHelper, rValue, nRefValue, bPixelX, bDefaultAsPixel),
222 o3tl::Length::emu, o3tl::Length::twip);
225 Color ConversionHelper::decodeColor( const GraphicHelper& rGraphicHelper,
226 const std::optional< OUString >& roVmlColor, const std::optional< double >& roVmlOpacity,
227 ::Color nDefaultRgb, ::Color nPrimaryRgb )
229 Color aDmlColor;
231 // convert opacity
232 const sal_Int32 DML_FULL_OPAQUE = ::oox::drawingml::MAX_PERCENT;
233 double fOpacity = roVmlOpacity.value_or( 1.0 );
234 sal_Int32 nOpacity = getLimitedValue< sal_Int32, double >( fOpacity * DML_FULL_OPAQUE, 0, DML_FULL_OPAQUE );
235 if( nOpacity < DML_FULL_OPAQUE )
236 aDmlColor.addTransformation( XML_alpha, nOpacity );
238 // color attribute not present - set passed default color
239 if( !roVmlColor.has_value() )
241 aDmlColor.setSrgbClr( nDefaultRgb );
242 return aDmlColor;
245 // separate leading color name or RGB value from following palette index
246 std::u16string_view aColorName, aColorIndex;
247 separatePair( aColorName, aColorIndex, roVmlColor.value(), ' ' );
249 // RGB colors in the format '#RRGGBB'
250 if( (aColorName.size() == 7) && (aColorName[ 0 ] == '#') )
252 aDmlColor.setSrgbClr( o3tl::toUInt32(aColorName.substr( 1 ), 16) );
253 return aDmlColor;
256 // RGB colors in the format '#RGB'
257 if( (aColorName.size() == 4) && (aColorName[ 0 ] == '#') )
259 sal_Int32 nR = o3tl::toUInt32(aColorName.substr( 1, 1 ), 16 ) * 0x11;
260 sal_Int32 nG = o3tl::toUInt32(aColorName.substr( 2, 1 ), 16 ) * 0x11;
261 sal_Int32 nB = o3tl::toUInt32(aColorName.substr( 3, 1 ), 16 ) * 0x11;
262 aDmlColor.setSrgbClr( (nR << 16) | (nG << 8) | nB );
263 return aDmlColor;
266 /* Predefined color names or system color names (resolve to RGB to detect
267 valid color name). */
268 sal_Int32 nColorToken = AttributeConversion::decodeToken( aColorName );
269 ::Color nRgbValue = Color::getVmlPresetColor( nColorToken, API_RGB_TRANSPARENT );
270 if( nRgbValue == API_RGB_TRANSPARENT )
271 nRgbValue = rGraphicHelper.getSystemColor( nColorToken );
272 if( nRgbValue != API_RGB_TRANSPARENT )
274 aDmlColor.setSrgbClr( nRgbValue );
275 return aDmlColor;
278 // try palette colors enclosed in brackets
279 if( (aColorIndex.size() >= 3) && (aColorIndex[ 0 ] == '[') && (aColorIndex[ aColorIndex.size() - 1 ] == ']') )
281 aDmlColor.setPaletteClr( o3tl::toInt32(aColorIndex.substr( 1, aColorIndex.size() - 2 )) );
282 return aDmlColor;
285 // try fill gradient modificator 'fill <modifier>(<amount>)'
286 if( (nPrimaryRgb != API_RGB_TRANSPARENT) && (nColorToken == XML_fill) )
288 size_t nOpenParen = aColorIndex.find( '(' );
289 size_t nCloseParen = aColorIndex.find( ')' );
290 if( nOpenParen != std::u16string_view::npos && nCloseParen != std::u16string_view::npos &&
291 (2 <= nOpenParen) && (nOpenParen + 1 < nCloseParen) && (nCloseParen + 1 == aColorIndex.size()) )
293 sal_Int32 nModToken = XML_TOKEN_INVALID;
294 switch( AttributeConversion::decodeToken( aColorIndex.substr( 0, nOpenParen ) ) )
296 case XML_darken: nModToken = XML_shade;break;
297 case XML_lighten: nModToken = XML_tint;
299 sal_Int32 nValue = o3tl::toInt32(aColorIndex.substr( nOpenParen + 1, nCloseParen - nOpenParen - 1 ));
300 if( (nModToken != XML_TOKEN_INVALID) && (0 <= nValue) && (nValue < 255) )
302 /* Simulate this modifier color by a color with related transformation.
303 The modifier amount has to be converted from the range [0;255] to
304 percentage [0;100000] used by DrawingML. */
305 aDmlColor.setSrgbClr( nPrimaryRgb );
306 aDmlColor.addTransformation( nModToken, static_cast< sal_Int32 >( nValue * ::oox::drawingml::MAX_PERCENT / 255 ) );
307 return aDmlColor;
312 OSL_FAIL( OStringBuffer( "lclGetColor - invalid VML color name '" +
313 OUStringToOString( roVmlColor.value(), RTL_TEXTENCODING_ASCII_US ) + "'" ).getStr() );
314 aDmlColor.setSrgbClr( nDefaultRgb );
315 return aDmlColor;
318 void ConversionHelper::decodeVmlPath( ::std::vector< ::std::vector< Point > >& rPointLists, ::std::vector< ::std::vector< PolygonFlags > >& rFlagLists, std::u16string_view rPath )
320 ::std::vector< sal_Int32 > aCoordList;
321 Point aCurrentPoint;
322 sal_Int32 nTokenStart = 0;
323 sal_Int32 nTokenLen = 0;
324 sal_Int32 nParamCount = 0;
325 bool bCommand = false;
326 enum VML_State { START, MOVE_REL, MOVE_ABS, BEZIER_REL, BEZIER_ABS,
327 LINE_REL, LINE_ABS, CLOSE, END, UNSUPPORTED };
328 VML_State state = START;
330 rPointLists.emplace_back( );
331 rFlagLists.emplace_back( );
333 for ( size_t i = 0; i < rPath.size(); i++ )
335 // Keep track of current integer token
336 if ( ( rPath[ i ] >= '0' && rPath[ i ] <= '9' ) || rPath[ i ] == '-' )
337 nTokenLen++;
338 else if ( rPath[ i ] != ' ' )
340 // Store coordinate from current token
341 if ( state != START && state != UNSUPPORTED )
343 if ( nTokenLen > 0 )
344 aCoordList.push_back( o3tl::toInt32(rPath.substr( nTokenStart, nTokenLen )) );
345 else
346 aCoordList.push_back( 0 );
347 nTokenLen = 0;
350 if (rPath[ i ] == ',' )
352 nParamCount--;
355 // Upon finding the next command code, deal with stored
356 // coordinates for previous command and reset parameters counter if needed.
357 // See http://www.w3.org/TR/NOTE-VML#_Toc416858382 for params count reference
358 if ( rPath[ i ] != ',' || nParamCount == 0 )
360 switch ( state )
362 case MOVE_REL:
363 aCoordList.resize(2, 0); // 2* params -> param count reset
364 if ( !rPointLists.empty() && !rPointLists.back().empty() )
366 rPointLists.emplace_back( );
367 rFlagLists.emplace_back( );
369 rPointLists.back().emplace_back( aCoordList[ 0 ], aCoordList[ 1 ] );
370 rFlagLists.back().push_back( PolygonFlags_NORMAL );
371 aCurrentPoint = rPointLists.back().back();
372 nParamCount = 2;
373 break;
375 case MOVE_ABS:
376 aCoordList.resize(2, 0); // 2 params -> no param count reset
377 if ( !rPointLists.empty() && !rPointLists.back().empty() )
379 rPointLists.emplace_back( );
380 rFlagLists.emplace_back( );
382 rPointLists.back().emplace_back( (aCoordList[ 0 ]), aCoordList[ 1 ] );
383 rFlagLists.back().push_back( PolygonFlags_NORMAL );
384 aCurrentPoint = rPointLists.back().back();
385 break;
387 case BEZIER_REL:
388 aCoordList.resize(6, 0); // 6* params -> param count reset
389 rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 0 ],
390 aCurrentPoint.Y + aCoordList[ 1 ] );
391 rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 2 ],
392 aCurrentPoint.Y + aCoordList[ 3 ] );
393 rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 4 ],
394 aCurrentPoint.Y + aCoordList[ 5 ] );
395 rFlagLists.back().push_back( PolygonFlags_CONTROL );
396 rFlagLists.back().push_back( PolygonFlags_CONTROL );
397 rFlagLists.back().push_back( PolygonFlags_NORMAL );
398 aCurrentPoint = rPointLists.back().back();
399 nParamCount = 6;
400 break;
402 case BEZIER_ABS:
403 aCoordList.resize(6, 0); // 6* params -> param count reset
404 rPointLists.back().emplace_back( aCoordList[ 0 ], aCoordList[ 1 ] );
405 rPointLists.back().emplace_back( aCoordList[ 2 ], aCoordList[ 3 ] );
406 rPointLists.back().emplace_back( aCoordList[ 4 ], aCoordList[ 5 ] );
407 rFlagLists.back().push_back( PolygonFlags_CONTROL );
408 rFlagLists.back().push_back( PolygonFlags_CONTROL );
409 rFlagLists.back().push_back( PolygonFlags_NORMAL );
410 aCurrentPoint = rPointLists.back().back();
411 nParamCount = 6;
412 break;
414 case LINE_REL:
415 aCoordList.resize(2, 0); // 2* params -> param count reset
416 rPointLists.back().emplace_back( aCurrentPoint.X + aCoordList[ 0 ],
417 aCurrentPoint.Y + aCoordList[ 1 ] );
418 rFlagLists.back().push_back( PolygonFlags_NORMAL );
419 aCurrentPoint = rPointLists.back().back();
420 nParamCount = 2;
421 break;
423 case LINE_ABS:
424 aCoordList.resize(2, 0); // 2* params -> param count reset
425 rPointLists.back().emplace_back( aCoordList[ 0 ], (aCoordList.size() > 1 ? aCoordList[ 1 ] : 0) );
426 rFlagLists.back().push_back( PolygonFlags_NORMAL );
427 aCurrentPoint = rPointLists.back().back();
428 nParamCount = 2;
429 break;
431 case CLOSE: // 0 param
432 SAL_WARN_IF(rPointLists.back().empty() || rFlagLists.back().empty(), "oox", "empty pointlists at close");
433 if (!rPointLists.back().empty() && !rFlagLists.back().empty())
435 rPointLists.back().push_back( rPointLists.back()[ 0 ] );
436 rFlagLists.back().push_back( rFlagLists.back()[ 0 ] );
437 aCurrentPoint = rPointLists.back().back();
439 break;
441 case END: // 0 param
442 rPointLists.emplace_back( );
443 rFlagLists.emplace_back( );
444 break;
446 case START:
447 case UNSUPPORTED:
448 break;
451 aCoordList.clear();
454 // Allow two-char commands to peek ahead to the next character
455 sal_Unicode nextChar = '\0';
456 if (i+1 < rPath.size())
457 nextChar = rPath[i+1];
459 // Move to relevant state upon finding a command
460 bCommand = true;
461 switch ( rPath[ i ] )
463 // Single-character commands
464 case 't': // rmoveto
465 state = MOVE_REL; nParamCount = 2; break;
466 case 'm': // moveto
467 state = MOVE_ABS; nParamCount = 2; break;
468 case 'v': // rcurveto
469 state = BEZIER_REL; nParamCount = 6; break;
470 case 'c': // curveto
471 state = BEZIER_ABS; nParamCount = 6; break;
472 case 'r': // rlineto
473 state = LINE_REL; nParamCount = 2; break;
474 case 'l': // lineto
475 state = LINE_ABS; nParamCount = 2; break;
476 case 'x': // close
477 state = CLOSE; break;
478 case 'e': // end
479 state = END; break;
481 // Two-character commands
482 case 'n':
484 switch ( nextChar )
486 case 'f': // nf - nofill
487 case 's': // ns - nostroke
488 state = UNSUPPORTED; i++; break;
490 break;
492 case 'a': // Elliptical curves
494 switch ( nextChar )
496 case 'e': // ae - angleellipseto
497 case 'l': // al - angleellipse
498 state = UNSUPPORTED; i++; break;
499 case 't': // at - arcto
500 case 'r': // ar - arc
501 state = UNSUPPORTED; i++; break;
503 break;
505 case 'w': // Clockwise elliptical arcs
507 switch ( nextChar )
509 case 'a': // wa - clockwisearcto
510 case 'r': // wr - clockwisearc
511 state = UNSUPPORTED; i++; break;
513 break;
515 case 'q':
517 switch ( nextChar )
519 case 'x': // qx - ellipticalquadrantx
520 case 'y': // qy - ellipticalquadranty
521 state = UNSUPPORTED; i++; break;
522 case 'b': // qb - quadraticbezier
523 state = UNSUPPORTED; i++; break;
525 break;
527 case 'h': // behaviour extensions
529 switch ( nextChar )
531 case 'a': // ha - AutoLine
532 case 'b': // hb - AutoCurve
533 case 'c': // hc - CornerLine
534 case 'd': // hd - CornerCurve
535 case 'e': // he - SmoothLine
536 case 'f': // hf - SmoothCurve
537 case 'g': // hg - SymmetricLine
538 case 'h': // hh - SymmetricCurve
539 case 'i': // hi - Freeform
540 state = UNSUPPORTED; i++; break;
542 break;
544 default:
545 bCommand = false;
546 break;
549 if (bCommand) nTokenLen = 0;
550 nTokenStart = i+1;
555 namespace {
557 sal_Int64 lclGetEmu( const GraphicHelper& rGraphicHelper, const std::optional< OUString >& roValue, sal_Int64 nDefValue )
559 return roValue.has_value() ? ConversionHelper::decodeMeasureToEmu( rGraphicHelper, roValue.value(), 0, false, false ) : nDefValue;
562 void lclGetDmlLineDash( std::optional< sal_Int32 >& oroPresetDash, LineProperties::DashStopVector& orCustomDash, const std::optional< OUString >& roDashStyle )
564 if( !roDashStyle.has_value() )
565 return;
567 const OUString& rDashStyle = roDashStyle.value();
568 switch( AttributeConversion::decodeToken( rDashStyle ) )
570 case XML_solid: oroPresetDash = XML_solid; return;
571 case XML_shortdot: oroPresetDash = XML_sysDot; return;
572 case XML_shortdash: oroPresetDash = XML_sysDash; return;
573 case XML_shortdashdot: oroPresetDash = XML_sysDashDot; return;
574 case XML_shortdashdotdot: oroPresetDash = XML_sysDashDotDot; return;
575 case XML_dot: oroPresetDash = XML_dot; return;
576 case XML_dash: oroPresetDash = XML_dash; return;
577 case XML_dashdot: oroPresetDash = XML_dashDot; return;
578 case XML_longdash: oroPresetDash = XML_lgDash; return;
579 case XML_longdashdot: oroPresetDash = XML_lgDashDot; return;
580 case XML_longdashdotdot: oroPresetDash = XML_lgDashDotDot; return;
582 // try to convert user-defined dash style
583 default:
585 ::std::vector< sal_Int32 > aValues;
586 sal_Int32 nIndex = 0;
587 while( nIndex >= 0 )
588 aValues.push_back( o3tl::toInt32(o3tl::getToken(rDashStyle, 0, ' ', nIndex )) );
589 size_t nPairs = aValues.size() / 2; // ignore last value if size is odd
590 for( size_t nPairIdx = 0; nPairIdx < nPairs; ++nPairIdx )
591 orCustomDash.emplace_back( aValues[ 2 * nPairIdx ], aValues[ 2 * nPairIdx + 1 ] );
596 sal_Int32 lclGetDmlArrowType( const std::optional< sal_Int32 >& roArrowType )
598 if( roArrowType.has_value() ) switch( roArrowType.value() )
600 case XML_none: return XML_none;
601 case XML_block: return XML_triangle;
602 case XML_classic: return XML_stealth;
603 case XML_diamond: return XML_diamond;
604 case XML_oval: return XML_oval;
605 case XML_open: return XML_arrow;
607 return XML_none;
610 sal_Int32 lclGetDmlArrowWidth( const std::optional< sal_Int32 >& roArrowWidth )
612 if( roArrowWidth.has_value() ) switch( roArrowWidth.value() )
614 case XML_narrow: return XML_sm;
615 case XML_medium: return XML_med;
616 case XML_wide: return XML_lg;
618 return XML_med;
621 sal_Int32 lclGetDmlArrowLength( const std::optional< sal_Int32 >& roArrowLength )
623 if( roArrowLength.has_value() ) switch( roArrowLength.value() )
625 case XML_short: return XML_sm;
626 case XML_medium: return XML_med;
627 case XML_long: return XML_lg;
629 return XML_med;
632 void lclConvertArrow( LineArrowProperties& orArrowProp, const StrokeArrowModel& rStrokeArrow )
634 orArrowProp.moArrowType = lclGetDmlArrowType( rStrokeArrow.moArrowType );
635 orArrowProp.moArrowWidth = lclGetDmlArrowWidth( rStrokeArrow.moArrowWidth );
636 orArrowProp.moArrowLength = lclGetDmlArrowLength( rStrokeArrow.moArrowLength );
639 sal_Int32 lclGetDmlLineCompound( const std::optional< sal_Int32 >& roLineStyle )
641 if( roLineStyle.has_value() ) switch( roLineStyle.value() )
643 case XML_single: return XML_sng;
644 case XML_thinThin: return XML_dbl;
645 case XML_thinThick: return XML_thinThick;
646 case XML_thickThin: return XML_thickThin;
647 case XML_thickBetweenThin: return XML_tri;
649 return XML_sng;
652 sal_Int32 lclGetDmlLineCap( const std::optional< sal_Int32 >& roEndCap )
654 if( roEndCap.has_value() ) switch( roEndCap.value() )
656 case XML_flat: return XML_flat;
657 case XML_square: return XML_sq;
658 case XML_round: return XML_rnd;
660 return XML_flat; // different defaults in VML (flat) and DrawingML (square)
663 sal_Int32 lclGetDmlLineJoint( const std::optional< sal_Int32 >& roJoinStyle )
665 if( roJoinStyle.has_value() ) switch( roJoinStyle.value() )
667 case XML_round: return XML_round;
668 case XML_bevel: return XML_bevel;
669 case XML_miter: return XML_miter;
671 return XML_round;
674 } // namespace
676 void StrokeArrowModel::assignUsed( const StrokeArrowModel& rSource )
678 assignIfUsed( moArrowType, rSource.moArrowType );
679 assignIfUsed( moArrowWidth, rSource.moArrowWidth );
680 assignIfUsed( moArrowLength, rSource.moArrowLength );
683 void StrokeModel::assignUsed( const StrokeModel& rSource )
685 assignIfUsed( moStroked, rSource.moStroked );
686 maStartArrow.assignUsed( rSource.maStartArrow );
687 maEndArrow.assignUsed( rSource.maEndArrow );
688 assignIfUsed( moColor, rSource.moColor );
689 assignIfUsed( moOpacity, rSource.moOpacity );
690 assignIfUsed( moWeight, rSource.moWeight );
691 assignIfUsed( moDashStyle, rSource.moDashStyle );
692 assignIfUsed( moLineStyle, rSource.moLineStyle );
693 assignIfUsed( moEndCap, rSource.moEndCap );
694 assignIfUsed( moJoinStyle, rSource.moJoinStyle );
697 void StrokeModel::pushToPropMap( ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper ) const
699 /* Convert VML line formatting to DrawingML line formatting and let the
700 DrawingML code do the hard work. */
701 LineProperties aLineProps;
703 if( moStroked.value_or( true ) )
705 aLineProps.maLineFill.moFillType = XML_solidFill;
706 lclConvertArrow( aLineProps.maStartArrow, maStartArrow );
707 lclConvertArrow( aLineProps.maEndArrow, maEndArrow );
708 aLineProps.maLineFill.maFillColor = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_BLACK );
709 aLineProps.moLineWidth = getLimitedValue< sal_Int32, sal_Int64 >( lclGetEmu( rGraphicHelper, moWeight, 1 ), 0, SAL_MAX_INT32 );
710 lclGetDmlLineDash( aLineProps.moPresetDash, aLineProps.maCustomDash, moDashStyle );
711 aLineProps.moLineCompound = lclGetDmlLineCompound( moLineStyle );
712 aLineProps.moLineCap = lclGetDmlLineCap( moEndCap );
713 aLineProps.moLineJoint = lclGetDmlLineJoint( moJoinStyle );
715 else
717 aLineProps.maLineFill.moFillType = XML_noFill;
720 aLineProps.pushToPropMap( rPropMap, rGraphicHelper );
723 void FillModel::assignUsed( const FillModel& rSource )
725 assignIfUsed( moFilled, rSource.moFilled );
726 assignIfUsed( moColor, rSource.moColor );
727 assignIfUsed( moOpacity, rSource.moOpacity );
728 assignIfUsed( moColor2, rSource.moColor2 );
729 assignIfUsed( moOpacity2, rSource.moOpacity2 );
730 assignIfUsed( moType, rSource.moType );
731 assignIfUsed( moAngle, rSource.moAngle );
732 assignIfUsed( moFocus, rSource.moFocus );
733 assignIfUsed( moFocusPos, rSource.moFocusPos );
734 assignIfUsed( moFocusSize, rSource.moFocusSize );
735 assignIfUsed( moBitmapPath, rSource.moBitmapPath );
736 assignIfUsed( moRotate, rSource.moRotate );
739 static void lcl_setGradientStop( std::multimap< double, Color >& rMap, const double fKey, const Color& rValue ) {
740 auto aElement = rMap.find( fKey );
742 if (aElement != rMap.end())
743 aElement->second = rValue;
744 else
745 rMap.emplace( fKey, rValue );
748 void FillModel::pushToPropMap( ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper ) const
750 /* Convert VML fill formatting to DrawingML fill formatting and let the
751 DrawingML code do the hard work. */
752 FillProperties aFillProps;
754 if( moFilled.value_or( true ) )
756 sal_Int32 nFillType = moType.value_or( XML_solid );
757 switch( nFillType )
759 case XML_gradient:
760 case XML_gradientRadial:
762 aFillProps.moFillType = XML_gradFill;
763 aFillProps.maGradientProps.moRotateWithShape = moRotate.value_or( false );
764 double fFocus = moFocus.value_or( 0.0 );
766 // prepare colors
767 Color aColor1 = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_WHITE );
768 Color aColor2 = ConversionHelper::decodeColor( rGraphicHelper, moColor2, moOpacity2, API_RGB_WHITE, aColor1.getColor( rGraphicHelper ) );
770 // type XML_gradient is linear or axial gradient
771 if( nFillType == XML_gradient )
773 // normalize angle to range [0;360) degrees
774 sal_Int32 nVmlAngle = getIntervalValue< sal_Int32, sal_Int32 >( moAngle.value_or( 0 ), 0, 360 );
776 // focus of -50% or 50% is axial gradient
777 if( ((-0.75 <= fFocus) && (fFocus <= -0.25)) || ((0.25 <= fFocus) && (fFocus <= 0.75)) )
779 /* According to spec, focus of 50% is outer-to-inner,
780 and -50% is inner-to-outer (color to color2).
781 BUT: For angles >= 180 deg., the behaviour is
782 reversed... that's not spec'ed of course. So,
783 [0;180) deg. and 50%, or [180;360) deg. and -50% is
784 outer-to-inner in fact. */
785 bool bOuterToInner = (fFocus > 0.0) == (nVmlAngle < 180);
786 // simulate axial gradient by 3-step DrawingML gradient
787 const Color& rOuterColor = bOuterToInner ? aColor1 : aColor2;
788 const Color& rInnerColor = bOuterToInner ? aColor2 : aColor1;
790 // add in order of offset
791 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.0, rOuterColor);
792 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.5, rInnerColor);
793 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 1.0, rOuterColor);
795 else // focus of -100%, 0%, and 100% is linear gradient
797 /* According to spec, focus of -100% or 100% swaps the
798 start and stop colors, effectively reversing the
799 gradient. BUT: For angles >= 180 deg., the
800 behaviour is reversed. This means that in this case
801 a focus of 0% swaps the gradient. */
802 if( fFocus < -0.5 || fFocus > 0.5 )
803 nVmlAngle = (nVmlAngle + 180) % 360;
804 // set the start and stop colors
805 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.0, aColor1 );
806 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 1.0, aColor2 );
809 // VML counts counterclockwise from bottom, DrawingML clockwise from left
810 sal_Int32 nDmlAngle = (630 - nVmlAngle) % 360;
811 aFillProps.maGradientProps.moShadeAngle = nDmlAngle * ::oox::drawingml::PER_DEGREE;
813 else // XML_gradientRadial is rectangular gradient
815 aFillProps.maGradientProps.moGradientPath = XML_rect;
816 // convert VML focus position and size to DrawingML fill-to-rect
817 DoublePair aFocusPos = moFocusPos.value_or( DoublePair( 0.0, 0.0 ) );
818 DoublePair aFocusSize = moFocusSize.value_or( DoublePair( 0.0, 0.0 ) );
819 double fLeft = getLimitedValue< double, double >( aFocusPos.first, 0.0, 1.0 );
820 double fTop = getLimitedValue< double, double >( aFocusPos.second, 0.0, 1.0 );
821 double fRight = getLimitedValue< double, double >( fLeft + aFocusSize.first, fLeft, 1.0 );
822 double fBottom = getLimitedValue< double, double >( fTop + aFocusSize.second, fTop, 1.0 );
823 aFillProps.maGradientProps.moFillToRect = IntegerRectangle2D(
824 static_cast< sal_Int32 >( fLeft * ::oox::drawingml::MAX_PERCENT ),
825 static_cast< sal_Int32 >( fTop * ::oox::drawingml::MAX_PERCENT ),
826 static_cast< sal_Int32 >( (1.0 - fRight) * ::oox::drawingml::MAX_PERCENT ),
827 static_cast< sal_Int32 >( (1.0 - fBottom) * ::oox::drawingml::MAX_PERCENT ) );
829 // set the start and stop colors (focus of 0% means outer-to-inner)
830 bool bOuterToInner = (-0.5 <= fFocus) && (fFocus <= 0.5);
831 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 0.0, bOuterToInner ? aColor2 : aColor1 );
832 lcl_setGradientStop( aFillProps.maGradientProps.maGradientStops, 1.0, bOuterToInner ? aColor1 : aColor2 );
835 break;
837 case XML_pattern:
838 case XML_tile:
839 case XML_frame:
841 if( moBitmapPath.has_value() && !moBitmapPath.value().isEmpty() )
843 aFillProps.maBlipProps.mxFillGraphic = rGraphicHelper.importEmbeddedGraphic(moBitmapPath.value());
844 if (aFillProps.maBlipProps.mxFillGraphic.is())
846 aFillProps.moFillType = XML_blipFill;
847 aFillProps.maBlipProps.moBitmapMode = (nFillType == XML_frame) ? XML_stretch : XML_tile;
848 break; // do not break if bitmap is missing, but run to XML_solid instead
852 [[fallthrough]]; // to XML_solid in case of missing bitmap path intended!
854 case XML_solid:
855 default:
857 aFillProps.moFillType = XML_solidFill;
858 // fill color (default is white)
859 aFillProps.maFillColor = ConversionHelper::decodeColor( rGraphicHelper, moColor, moOpacity, API_RGB_WHITE );
863 else
865 aFillProps.moFillType = XML_noFill;
868 aFillProps.pushToPropMap( rPropMap, rGraphicHelper );
871 ShadowModel::ShadowModel()
872 : mbHasShadow(false)
876 void ShadowModel::pushToPropMap(ShapePropertyMap& rPropMap, const GraphicHelper& rGraphicHelper) const
878 if (!mbHasShadow || (moShadowOn.has_value() && !moShadowOn.value()))
879 return;
881 drawingml::Color aColor = ConversionHelper::decodeColor(rGraphicHelper, moColor, moOpacity, API_RGB_GRAY);
882 // nOffset* is in mm100, default value is 35 twips, see DffPropertyReader::ApplyAttributes() in msfilter.
883 sal_Int32 nOffsetX = 62, nOffsetY = 62;
884 if (moOffset.has_value())
886 std::u16string_view aOffsetX, aOffsetY;
887 ConversionHelper::separatePair(aOffsetX, aOffsetY, moOffset.value(), ',');
888 if (!aOffsetX.empty())
889 nOffsetX = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, aOffsetX, 0, false, false );
890 if (!aOffsetY.empty())
891 nOffsetY = ConversionHelper::decodeMeasureToHmm(rGraphicHelper, aOffsetY, 0, false, false );
894 table::ShadowFormat aFormat;
895 aFormat.Color = sal_Int32(aColor.getColor(rGraphicHelper));
896 aFormat.Location = nOffsetX < 0
897 ? nOffsetY < 0 ? table::ShadowLocation_TOP_LEFT : table::ShadowLocation_BOTTOM_LEFT
898 : nOffsetY < 0 ? table::ShadowLocation_TOP_RIGHT : table::ShadowLocation_BOTTOM_RIGHT;
899 // The width of the shadow is the average of the x and y values, see SwWW8ImplReader::MatchSdrItemsIntoFlySet().
900 aFormat.ShadowWidth = ((std::abs(nOffsetX) + std::abs(nOffsetY)) / 2);
901 rPropMap.setProperty(PROP_ShadowFormat, aFormat);
904 TextpathModel::TextpathModel()
908 static beans::PropertyValue lcl_createTextpathProps()
910 uno::Sequence<beans::PropertyValue> aTextpathPropSeq( comphelper::InitPropertySequence({
911 { "TextPath", uno::Any(true) },
912 { "TextPathMode", uno::Any(drawing::EnhancedCustomShapeTextPathMode_SHAPE) },
913 { "ScaleX", uno::Any(false) },
914 { "SameLetterHeights", uno::Any(false) }
915 }));
917 beans::PropertyValue aRet;
918 aRet.Name = "TextPath";
919 aRet.Value <<= aTextpathPropSeq;
920 return aRet;
923 void TextpathModel::pushToPropMap(ShapePropertyMap& rPropMap, const uno::Reference<drawing::XShape>& xShape, const GraphicHelper& rGraphicHelper) const
925 OUString sFont = "";
927 if (moString.has_value())
929 uno::Reference<text::XTextRange> xTextRange(xShape, uno::UNO_QUERY);
930 xTextRange->setString(moString.value());
932 uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY);
933 uno::Sequence<beans::PropertyValue> aGeomPropSeq = xPropertySet->getPropertyValue("CustomShapeGeometry").get< uno::Sequence<beans::PropertyValue> >();
934 bool bFound = false;
935 for (beans::PropertyValue& rProp : asNonConstRange(aGeomPropSeq))
937 if (rProp.Name == "TextPath")
939 bFound = true;
940 rProp = lcl_createTextpathProps();
943 if (!bFound)
945 sal_Int32 nSize = aGeomPropSeq.getLength();
946 aGeomPropSeq.realloc(nSize+1);
947 aGeomPropSeq.getArray()[nSize] = lcl_createTextpathProps();
949 rPropMap.setAnyProperty(PROP_CustomShapeGeometry, uno::Any(aGeomPropSeq));
951 if (moStyle.has_value())
953 OUString aStyle = moStyle.value_or(OUString());
955 sal_Int32 nIndex = 0;
956 while( nIndex >= 0 )
958 std::u16string_view aName, aValue;
959 if (ConversionHelper::separatePair(aName, aValue, o3tl::getToken(aStyle, 0, ';', nIndex), ':'))
961 if (aName == u"font-family")
963 // remove " (first, and last character)
964 if (aValue.size() > 2)
965 aValue = aValue.substr(1, aValue.size() - 2);
967 uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY);
968 xPropertySet->setPropertyValue("CharFontName", uno::Any(OUString(aValue)));
969 sFont = aValue;
971 else if (aName == u"font-size")
973 std::optional<OUString> aOptString {OUString(aValue)};
974 float nSize = drawingml::convertEmuToPoints(lclGetEmu(rGraphicHelper, aOptString, 1));
976 uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY);
977 xPropertySet->setPropertyValue("CharHeight", uno::Any(nSize));
982 if (moTrim.has_value() && moTrim.value())
983 return;
985 OUString sText = moString.value_or("");
986 ScopedVclPtrInstance<VirtualDevice> pDevice;
987 vcl::Font aFont = pDevice->GetFont();
988 aFont.SetFamilyName(sFont);
989 aFont.SetFontSize(Size(0, 96));
990 pDevice->SetFont(aFont);
992 auto nTextWidth = pDevice->GetTextWidth(sText);
993 if (nTextWidth)
995 sal_Int32 nNewHeight = (static_cast<double>(pDevice->GetTextHeight()) / nTextWidth) * xShape->getSize().Width;
996 xShape->setSize(awt::Size(xShape->getSize().Width, nNewHeight));
1000 } // namespace oox
1002 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */