Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / oox / source / vml / vmlshapecontext.cxx
blob2e3b1ef9fd72e28b232ce35977a3e37bbacf1e4d
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 <string_view>
24 #include <oox/vml/vmlshapecontext.hxx>
26 #include <oox/core/xmlfilterbase.hxx>
27 #include <oox/helper/attributelist.hxx>
28 #include <oox/helper/helper.hxx>
29 #include <oox/token/namespaces.hxx>
30 #include <oox/token/tokens.hxx>
31 #include <oox/vml/vmldrawing.hxx>
32 #include <oox/vml/vmlshape.hxx>
33 #include <oox/vml/vmlshapecontainer.hxx>
34 #include <oox/vml/vmltextboxcontext.hxx>
36 #include <osl/diagnose.h>
37 #include <filter/msfilter/escherex.hxx>
38 #include <o3tl/string_view.hxx>
40 namespace oox::vml {
42 using namespace ::com::sun::star;
44 using ::oox::core::ContextHandler2;
45 using ::oox::core::ContextHandler2Helper;
46 using ::oox::core::ContextHandlerRef;
48 namespace {
50 /** Returns the boolean value from the specified VML attribute (if present).
52 std::optional< bool > lclDecodeBool( const AttributeList& rAttribs, sal_Int32 nToken )
54 std::optional< OUString > oValue = rAttribs.getString( nToken );
55 if( oValue.has_value() ) return std::optional< bool >( ConversionHelper::decodeBool( oValue.value() ) );
56 return std::optional< bool >();
59 /** Returns the percentage value from the specified VML attribute (if present).
60 The value will be normalized (1.0 is returned for 100%).
62 std::optional< double > lclDecodePercent( const AttributeList& rAttribs, sal_Int32 nToken, double fDefValue )
64 std::optional< OUString > oValue = rAttribs.getString( nToken );
65 if( oValue.has_value() ) return std::optional< double >( ConversionHelper::decodePercent( oValue.value(), fDefValue ) );
66 return std::optional< double >();
69 /** #119750# Special method for opacity; it *should* be a percentage value, but there are cases
70 where a value relative to 0xffff (65536) is used, ending with an 'f'
72 std::optional< double > lclDecodeOpacity( const AttributeList& rAttribs, sal_Int32 nToken, double fDefValue )
74 std::optional< OUString > oValue = rAttribs.getString( nToken );
75 double fRetval(fDefValue);
77 if( oValue.has_value() )
79 const OUString& aString(oValue.value());
80 const sal_Int32 nLength(aString.getLength());
82 if(nLength > 0)
84 if(aString.endsWith("f"))
86 fRetval = std::clamp(aString.toDouble() / 65536.0, 0.0, 1.0);
88 else
90 fRetval = ConversionHelper::decodePercent( aString, fDefValue );
95 return std::optional< double >(fRetval);
98 /** Returns the integer value pair from the specified VML attribute (if present).
100 std::optional< Int32Pair > lclDecodeInt32Pair( const AttributeList& rAttribs, sal_Int32 nToken )
102 std::optional< OUString > oValue = rAttribs.getString( nToken );
103 std::optional< Int32Pair > oRetValue;
104 if( oValue.has_value() )
106 std::u16string_view aValue1, aValue2;
107 ConversionHelper::separatePair( aValue1, aValue2, oValue.value(), ',' );
108 oRetValue = Int32Pair( o3tl::toInt32(aValue1), o3tl::toInt32(aValue2) );
110 return oRetValue;
113 /** Returns the percentage pair from the specified VML attribute (if present).
115 std::optional< DoublePair > lclDecodePercentPair( const AttributeList& rAttribs, sal_Int32 nToken )
117 std::optional< OUString > oValue = rAttribs.getString( nToken );
118 std::optional< DoublePair > oRetValue;
119 if( oValue.has_value() )
121 std::u16string_view aValue1, aValue2;
122 ConversionHelper::separatePair( aValue1, aValue2, oValue.value(), ',' );
123 oRetValue = DoublePair(
124 ConversionHelper::decodePercent( aValue1, 0.0 ),
125 ConversionHelper::decodePercent( aValue2, 0.0 ) );
127 return oRetValue;
130 /** Returns the boolean value from the passed string of an attribute in the x:
131 namespace (VML for spreadsheets). Supported values: f, t, False, True.
132 @param bDefaultForEmpty Default value for the empty string.
134 bool lclDecodeVmlxBool( std::u16string_view rValue, bool bDefaultForEmpty )
136 if( rValue.empty() ) return bDefaultForEmpty;
137 sal_Int32 nToken = AttributeConversion::decodeToken( rValue );
138 // anything else than 't' or 'True' is considered to be false, as specified
139 return (nToken == XML_t) || (nToken == XML_True);
142 } // namespace
144 ShapeLayoutContext::ShapeLayoutContext( ContextHandler2Helper const & rParent, Drawing& rDrawing ) :
145 ContextHandler2( rParent ),
146 mrDrawing( rDrawing )
150 ContextHandlerRef ShapeLayoutContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
152 switch( nElement )
154 case O_TOKEN( idmap ):
156 OUString aBlockIds = rAttribs.getStringDefaulted( XML_data);
157 sal_Int32 nIndex = 0;
158 while( nIndex >= 0 )
160 std::u16string_view aToken = o3tl::trim(o3tl::getToken(aBlockIds, 0, ' ', nIndex ));
161 if( !aToken.empty() )
162 mrDrawing.registerBlockId( o3tl::toInt32(aToken) );
165 break;
167 return nullptr;
170 ClientDataContext::ClientDataContext( ContextHandler2Helper const & rParent,
171 ClientData& rClientData, const AttributeList& rAttribs ) :
172 ContextHandler2( rParent ),
173 mrClientData( rClientData )
175 mrClientData.mnObjType = rAttribs.getToken( XML_ObjectType, XML_TOKEN_INVALID );
178 ContextHandlerRef ClientDataContext::onCreateContext( sal_Int32 /*nElement*/, const AttributeList& /*rAttribs*/ )
180 if( isRootElement() )
182 maElementText.clear();
183 return this;
185 return nullptr;
188 void ClientDataContext::onCharacters( const OUString& rChars )
190 /* Empty but existing elements have special meaning, e.g. 'true'. Collect
191 existing text and convert it in onEndElement(). */
192 maElementText = rChars;
195 void ClientDataContext::onEndElement()
197 switch( getCurrentElement() )
199 case VMLX_TOKEN( Anchor ): mrClientData.maAnchor = maElementText; break;
200 case VMLX_TOKEN( FmlaMacro ): mrClientData.maFmlaMacro = maElementText; break;
201 case VMLX_TOKEN( FmlaPict ): mrClientData.maFmlaPict = maElementText; break;
202 case VMLX_TOKEN( FmlaLink ): mrClientData.maFmlaLink = maElementText; break;
203 case VMLX_TOKEN( FmlaRange ): mrClientData.maFmlaRange = maElementText; break;
204 case VMLX_TOKEN( FmlaGroup ): mrClientData.maFmlaGroup = maElementText; break;
205 case VMLX_TOKEN( TextHAlign ): mrClientData.mnTextHAlign = AttributeConversion::decodeToken( maElementText ); break;
206 case VMLX_TOKEN( TextVAlign ): mrClientData.mnTextVAlign = AttributeConversion::decodeToken( maElementText ); break;
207 case VMLX_TOKEN( Column ): mrClientData.mnCol = maElementText.toInt32(); break;
208 case VMLX_TOKEN( Row ): mrClientData.mnRow = maElementText.toInt32(); break;
209 case VMLX_TOKEN( Checked ): mrClientData.mnChecked = maElementText.toInt32(); break;
210 case VMLX_TOKEN( DropStyle ): mrClientData.mnDropStyle = AttributeConversion::decodeToken( maElementText ); break;
211 case VMLX_TOKEN( DropLines ): mrClientData.mnDropLines = maElementText.toInt32(); break;
212 case VMLX_TOKEN( Val ): mrClientData.mnVal = maElementText.toInt32(); break;
213 case VMLX_TOKEN( Min ): mrClientData.mnMin = maElementText.toInt32(); break;
214 case VMLX_TOKEN( Max ): mrClientData.mnMax = maElementText.toInt32(); break;
215 case VMLX_TOKEN( Inc ): mrClientData.mnInc = maElementText.toInt32(); break;
216 case VMLX_TOKEN( Page ): mrClientData.mnPage = maElementText.toInt32(); break;
217 case VMLX_TOKEN( SelType ): mrClientData.mnSelType = AttributeConversion::decodeToken( maElementText ); break;
218 case VMLX_TOKEN( VTEdit ): mrClientData.mnVTEdit = maElementText.toInt32(); break;
219 case VMLX_TOKEN( PrintObject ): mrClientData.mbPrintObject = lclDecodeVmlxBool( maElementText, true ); break;
220 case VMLX_TOKEN( Visible ): mrClientData.mbVisible = lclDecodeVmlxBool( maElementText, true ); break;
221 case VMLX_TOKEN( DDE ): mrClientData.mbDde = lclDecodeVmlxBool( maElementText, true ); break;
222 case VMLX_TOKEN( NoThreeD ): mrClientData.mbNo3D = lclDecodeVmlxBool( maElementText, true ); break;
223 case VMLX_TOKEN( NoThreeD2 ): mrClientData.mbNo3D2 = lclDecodeVmlxBool( maElementText, true ); break;
224 case VMLX_TOKEN( MultiLine ): mrClientData.mbMultiLine = lclDecodeVmlxBool( maElementText, true ); break;
225 case VMLX_TOKEN( VScroll ): mrClientData.mbVScroll = lclDecodeVmlxBool( maElementText, true ); break;
226 case VMLX_TOKEN( SecretEdit ): mrClientData.mbSecretEdit = lclDecodeVmlxBool( maElementText, true ); break;
230 ShapeContextBase::ShapeContextBase( ContextHandler2Helper const & rParent ) :
231 ContextHandler2( rParent )
235 ContextHandlerRef ShapeContextBase::createShapeContext( ContextHandler2Helper const & rParent,
236 ShapeContainer& rShapes, sal_Int32 nElement, const AttributeList& rAttribs )
238 switch( nElement )
240 case O_TOKEN( shapelayout ):
241 return new ShapeLayoutContext( rParent, rShapes.getDrawing() );
243 case VML_TOKEN( shapetype ):
244 return new ShapeTypeContext( rParent, rShapes.createShapeType(), rAttribs );
245 case VML_TOKEN( group ):
246 return new GroupShapeContext( rParent, rShapes.createShape< GroupShape >(), rAttribs );
247 case VML_TOKEN( shape ):
248 if (rAttribs.hasAttribute(XML_path) &&
249 // tdf#122563 skip in the case of empty path
250 !rAttribs.getStringDefaulted(XML_path).isEmpty())
251 return new ShapeContext( rParent, rShapes.createShape< BezierShape >(), rAttribs );
252 else
253 return new ShapeContext( rParent, rShapes.createShape< ComplexShape >(), rAttribs );
254 case VML_TOKEN( rect ):
255 return new RectangleShapeContext( rParent, rAttribs, rShapes.createShape< RectangleShape >() );
256 case VML_TOKEN( roundrect ):
257 return new ShapeContext( rParent, rShapes.createShape< RectangleShape >(), rAttribs );
258 case VML_TOKEN( oval ):
259 return new ShapeContext( rParent, rShapes.createShape< EllipseShape >(), rAttribs );
260 case VML_TOKEN( polyline ):
261 return new ShapeContext( rParent, rShapes.createShape< PolyLineShape >(), rAttribs );
262 case VML_TOKEN( line ):
263 return new ShapeContext( rParent, rShapes.createShape< LineShape >(), rAttribs );
264 case VML_TOKEN( curve ):
265 return new ShapeContext( rParent, rShapes.createShape< BezierShape >(), rAttribs );
267 // TODO:
268 case VML_TOKEN( arc ):
269 case VML_TOKEN( diagram ):
270 case VML_TOKEN( image ):
271 return new ShapeContext( rParent, rShapes.createShape< ComplexShape >(), rAttribs );
273 case W_TOKEN(control):
274 return new ControlShapeContext( rParent, rShapes, rAttribs );
276 return nullptr;
279 ShapeTypeContext::ShapeTypeContext(ContextHandler2Helper const & rParent,
280 std::shared_ptr<ShapeType> const& pShapeType,
281 const AttributeList& rAttribs)
282 : ShapeContextBase(rParent)
283 , m_pShapeType(pShapeType) // tdf#112311 keep it alive
284 , mrTypeModel( pShapeType->getTypeModel() )
286 // shape identifier and shape name
287 bool bHasOspid = rAttribs.hasAttribute( O_TOKEN( spid ) );
288 mrTypeModel.maShapeId = rAttribs.getXString( bHasOspid ? O_TOKEN( spid ) : XML_id, OUString() );
289 mrTypeModel.maLegacyId = rAttribs.getStringDefaulted( XML_id);
290 OSL_ENSURE( !mrTypeModel.maShapeId.isEmpty(), "ShapeTypeContext::ShapeTypeContext - missing shape identifier" );
291 // builtin shape type identifier
292 mrTypeModel.moShapeType = rAttribs.getInteger( O_TOKEN( spt ) );
293 // if the o:spid attribute exists, the id attribute contains the user-defined shape name
294 if( bHasOspid )
296 mrTypeModel.maShapeName = rAttribs.getXString( XML_id, OUString() );
297 // get ShapeType and ShapeId from name for compatibility
298 static constexpr OUStringLiteral sShapeTypePrefix = u"shapetype_";
299 OUString tmp;
300 if( mrTypeModel.maShapeName.startsWith( sShapeTypePrefix ) )
302 mrTypeModel.maShapeId = mrTypeModel.maShapeName;
303 mrTypeModel.moShapeType = o3tl::toInt32(mrTypeModel.maShapeName.subView(sShapeTypePrefix.getLength()));
305 else if (mrTypeModel.maShapeName.startsWith("_x0000_t", &tmp))
307 mrTypeModel.maShapeId = mrTypeModel.maShapeName;
308 mrTypeModel.moShapeType = tmp.toInt32();
312 // coordinate system position/size, CSS style
313 mrTypeModel.moCoordPos = lclDecodeInt32Pair( rAttribs, XML_coordorigin );
314 mrTypeModel.moCoordSize = lclDecodeInt32Pair( rAttribs, XML_coordsize );
315 setStyle( rAttribs.getStringDefaulted( XML_style) );
316 if( lclDecodeBool( rAttribs, O_TOKEN( hr )).value_or( false ))
317 { // MSO's handling of o:hr width is nowhere near what the spec says:
318 // - o:hrpct is not in % but in 0.1%
319 // - if o:hrpct is not given, 100% width is assumed
320 // - given width is used only if explicit o:hrpct="0" is given
321 OUString hrpct = rAttribs.getString( O_TOKEN( hrpct ), "1000" );
322 if( hrpct != "0" )
323 mrTypeModel.maWidthPercent = OUString::number( hrpct.toInt32() );
324 mrTypeModel.maWrapDistanceLeft = "0";
325 mrTypeModel.maWrapDistanceRight = "0";
326 mrTypeModel.maPositionHorizontal = rAttribs.getString( O_TOKEN( hralign ), "left" );
327 mrTypeModel.moWrapType = "topAndBottom";
330 // stroke settings (may be overridden by v:stroke element later)
331 mrTypeModel.maStrokeModel.moStroked = lclDecodeBool( rAttribs, XML_stroked );
332 mrTypeModel.maStrokeModel.moColor = rAttribs.getString( XML_strokecolor );
333 mrTypeModel.maStrokeModel.moWeight = rAttribs.getString( XML_strokeweight );
335 // fill settings (may be overridden by v:fill element later)
336 mrTypeModel.maFillModel.moFilled = lclDecodeBool( rAttribs, XML_filled );
337 mrTypeModel.maFillModel.moColor = rAttribs.getString( XML_fillcolor );
339 // For roundrect we may have an arcsize attribute to read
340 mrTypeModel.maArcsize = rAttribs.getStringDefaulted(XML_arcsize);
341 // editas
342 mrTypeModel.maEditAs = rAttribs.getStringDefaulted(XML_editas);
344 mrTypeModel.maAdjustments = rAttribs.getStringDefaulted(XML_adj);
347 ContextHandlerRef ShapeTypeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
349 if( isRootElement() ) switch( nElement )
351 case VML_TOKEN( stroke ):
352 assignIfUsed( mrTypeModel.maStrokeModel.moStroked, lclDecodeBool( rAttribs, XML_on ) );
353 mrTypeModel.maStrokeModel.maStartArrow.moArrowType = rAttribs.getToken( XML_startarrow );
354 mrTypeModel.maStrokeModel.maStartArrow.moArrowWidth = rAttribs.getToken( XML_startarrowwidth );
355 mrTypeModel.maStrokeModel.maStartArrow.moArrowLength = rAttribs.getToken( XML_startarrowlength );
356 mrTypeModel.maStrokeModel.maEndArrow.moArrowType = rAttribs.getToken( XML_endarrow );
357 mrTypeModel.maStrokeModel.maEndArrow.moArrowWidth = rAttribs.getToken( XML_endarrowwidth );
358 mrTypeModel.maStrokeModel.maEndArrow.moArrowLength = rAttribs.getToken( XML_endarrowlength );
359 assignIfUsed( mrTypeModel.maStrokeModel.moColor, rAttribs.getString( XML_color ) );
360 mrTypeModel.maStrokeModel.moOpacity = lclDecodeOpacity( rAttribs, XML_opacity, 1.0 );
361 assignIfUsed( mrTypeModel.maStrokeModel.moWeight, rAttribs.getString( XML_weight ) );
362 mrTypeModel.maStrokeModel.moDashStyle = rAttribs.getString( XML_dashstyle );
363 mrTypeModel.maStrokeModel.moLineStyle = rAttribs.getToken( XML_linestyle );
364 mrTypeModel.maStrokeModel.moEndCap = rAttribs.getToken( XML_endcap );
365 mrTypeModel.maStrokeModel.moJoinStyle = rAttribs.getToken( XML_joinstyle );
366 break;
367 case VML_TOKEN( fill ):
369 // in DOCX shapes use r:id for the relationship id
370 // in XLSX they use o:relid
371 bool bHasORelId = rAttribs.hasAttribute( O_TOKEN(relid) );
372 assignIfUsed( mrTypeModel.maFillModel.moFilled, lclDecodeBool( rAttribs, XML_on ) );
373 assignIfUsed( mrTypeModel.maFillModel.moColor, rAttribs.getString( XML_color ) );
374 mrTypeModel.maFillModel.moOpacity = lclDecodeOpacity( rAttribs, XML_opacity, 1.0 );
375 mrTypeModel.maFillModel.moColor2 = rAttribs.getString( XML_color2 );
376 mrTypeModel.maFillModel.moOpacity2 = lclDecodeOpacity( rAttribs, XML_opacity2, 1.0 );
377 mrTypeModel.maFillModel.moType = rAttribs.getToken( XML_type );
378 mrTypeModel.maFillModel.moAngle = rAttribs.getInteger( XML_angle );
379 mrTypeModel.maFillModel.moFocus = lclDecodePercent( rAttribs, XML_focus, 0.0 );
380 mrTypeModel.maFillModel.moFocusPos = lclDecodePercentPair( rAttribs, XML_focusposition );
381 mrTypeModel.maFillModel.moFocusSize = lclDecodePercentPair( rAttribs, XML_focussize );
382 mrTypeModel.maFillModel.moBitmapPath = decodeFragmentPath( rAttribs, bHasORelId ? O_TOKEN(relid) : R_TOKEN(id) );
383 mrTypeModel.maFillModel.moRotate = lclDecodeBool( rAttribs, XML_rotate );
384 break;
386 case VML_TOKEN( imagedata ):
388 // shapes in docx use r:id for the relationship id
389 // in xlsx it they use o:relid
390 bool bHasORelId = rAttribs.hasAttribute( O_TOKEN( relid ) );
391 mrTypeModel.moGraphicPath = decodeFragmentPath( rAttribs, bHasORelId ? O_TOKEN( relid ) : R_TOKEN( id ) );
392 mrTypeModel.moGraphicTitle = rAttribs.getString( O_TOKEN( title ) );
394 // Get crop attributes.
395 mrTypeModel.moCropBottom = rAttribs.getString(XML_cropbottom);
396 mrTypeModel.moCropLeft = rAttribs.getString(XML_cropleft);
397 mrTypeModel.moCropRight = rAttribs.getString(XML_cropright);
398 mrTypeModel.moCropTop = rAttribs.getString(XML_croptop);
400 // Gain / contrast.
401 std::optional<OUString> oGain = rAttribs.getString(XML_gain);
402 sal_Int32 nGain = 0x10000;
403 if (oGain.has_value() && oGain.value().endsWith("f"))
405 nGain = oGain.value().toInt32();
407 if (nGain < 0x10000)
409 nGain *= 101; // 100 + 1 to round
410 nGain /= 0x10000;
411 nGain -= 100;
413 mrTypeModel.mnGain = nGain;
415 // Blacklevel / brightness.
416 std::optional<OUString> oBlacklevel = rAttribs.getString(XML_blacklevel);
417 sal_Int16 nBlacklevel = 0;
418 if (oBlacklevel.has_value() && oBlacklevel.value().endsWith("f"))
420 nBlacklevel = oBlacklevel.value().toInt32();
422 if (nBlacklevel != 0)
424 nBlacklevel /= 327;
426 mrTypeModel.mnBlacklevel = nBlacklevel;
428 break;
429 case NMSP_vmlWord | XML_wrap:
430 mrTypeModel.moWrapAnchorX = rAttribs.getString(XML_anchorx);
431 mrTypeModel.moWrapAnchorY = rAttribs.getString(XML_anchory);
432 mrTypeModel.moWrapType = rAttribs.getString(XML_type);
433 mrTypeModel.moWrapSide = rAttribs.getString(XML_side);
434 break;
435 case VML_TOKEN( shadow ):
437 mrTypeModel.maShadowModel.mbHasShadow = true;
438 mrTypeModel.maShadowModel.moShadowOn = lclDecodeBool(rAttribs, XML_on).value_or(false);
439 assignIfUsed(mrTypeModel.maShadowModel.moColor, rAttribs.getString(XML_color));
440 assignIfUsed(mrTypeModel.maShadowModel.moOffset, rAttribs.getString(XML_offset));
441 mrTypeModel.maShadowModel.moOpacity = lclDecodePercent(rAttribs, XML_opacity, 1.0);
443 break;
444 case VML_TOKEN( textpath ):
445 assignIfUsed(mrTypeModel.maTextpathModel.moString, rAttribs.getString(XML_string));
446 assignIfUsed(mrTypeModel.maTextpathModel.moStyle, rAttribs.getString(XML_style));
447 assignIfUsed(mrTypeModel.maTextpathModel.moTrim, lclDecodeBool(rAttribs, XML_trim));
448 break;
450 return nullptr;
453 std::optional< OUString > ShapeTypeContext::decodeFragmentPath( const AttributeList& rAttribs, sal_Int32 nToken ) const
455 std::optional< OUString > oFragmentPath;
456 std::optional< OUString > oRelId = rAttribs.getString( nToken );
457 if( oRelId.has_value() )
458 oFragmentPath = getFragmentPathFromRelId( oRelId.value() );
459 return oFragmentPath;
462 void ShapeTypeContext::setStyle( std::u16string_view rStyle )
464 sal_Int32 nIndex = 0;
465 while( nIndex >= 0 )
467 std::u16string_view aName, aValue;
468 if( ConversionHelper::separatePair( aName, aValue, o3tl::getToken(rStyle, 0, ';', nIndex ), ':' ) )
470 if( aName == u"position" ) mrTypeModel.maPosition = aValue;
471 else if( aName == u"z-index" ) mrTypeModel.maZIndex = aValue;
472 else if( aName == u"left" ) mrTypeModel.maLeft = aValue;
473 else if( aName == u"top" ) mrTypeModel.maTop = aValue;
474 else if( aName == u"width" ) mrTypeModel.maWidth = aValue;
475 else if( aName == u"height" ) mrTypeModel.maHeight = aValue;
476 else if( aName == u"margin-left" ) mrTypeModel.maMarginLeft = aValue;
477 else if( aName == u"margin-top" ) mrTypeModel.maMarginTop = aValue;
478 else if( aName == u"mso-position-vertical-relative" ) mrTypeModel.maPositionVerticalRelative = aValue;
479 else if( aName == u"mso-position-horizontal-relative" ) mrTypeModel.maPositionHorizontalRelative = aValue;
480 else if( aName == u"mso-position-horizontal" ) mrTypeModel.maPositionHorizontal = aValue;
481 else if( aName == u"mso-position-vertical" ) mrTypeModel.maPositionVertical = aValue;
482 else if( aName == u"mso-width-percent" ) mrTypeModel.maWidthPercent = aValue;
483 else if( aName == u"mso-width-relative" ) mrTypeModel.maWidthRelative = aValue;
484 else if( aName == u"mso-height-percent" ) mrTypeModel.maHeightPercent = aValue;
485 else if( aName == u"mso-height-relative" ) mrTypeModel.maHeightRelative = aValue;
486 else if( aName == u"mso-fit-shape-to-text" ) mrTypeModel.mbAutoHeight = true;
487 else if( aName == u"rotation" ) mrTypeModel.maRotation = aValue;
488 else if( aName == u"flip" ) mrTypeModel.maFlip = aValue;
489 else if( aName == u"visibility" )
490 mrTypeModel.mbVisible = aValue != u"hidden";
491 else if( aName == u"mso-wrap-style" ) mrTypeModel.maWrapStyle = aValue;
492 else if ( aName == u"v-text-anchor" ) mrTypeModel.maVTextAnchor = aValue;
493 else if ( aName == u"mso-wrap-distance-left" ) mrTypeModel.maWrapDistanceLeft = aValue;
494 else if ( aName == u"mso-wrap-distance-right" ) mrTypeModel.maWrapDistanceRight = aValue;
495 else if ( aName == u"mso-wrap-distance-top" ) mrTypeModel.maWrapDistanceTop = aValue;
496 else if ( aName == u"mso-wrap-distance-bottom" ) mrTypeModel.maWrapDistanceBottom = aValue;
501 ShapeContext::ShapeContext(ContextHandler2Helper const& rParent,
502 const std::shared_ptr<ShapeBase>& pShape, const AttributeList& rAttribs)
503 : ShapeTypeContext(rParent, pShape, rAttribs)
504 , mrShape(*pShape)
505 , mrShapeModel(pShape->getShapeModel())
507 // collect shape specific attributes
508 mrShapeModel.maType = rAttribs.getXString( XML_type, OUString() );
509 // polyline path
510 setPoints( rAttribs.getStringDefaulted( XML_points) );
511 // line start and end positions
512 setFrom(rAttribs.getStringDefaulted(XML_from));
513 setTo(rAttribs.getStringDefaulted(XML_to));
514 setControl1(rAttribs.getStringDefaulted(XML_control1));
515 setControl2(rAttribs.getStringDefaulted(XML_control2));
516 setVmlPath(rAttribs.getStringDefaulted(XML_path));
517 setHyperlink(rAttribs.getStringDefaulted(XML_href));
520 ContextHandlerRef ShapeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
522 // Excel specific shape client data
523 if( isRootElement() ) switch( nElement )
525 case VML_TOKEN( textbox ):
527 // Calculate the shape type: map both <rect> and <v:shape> with a textbox shape type to
528 // a TextShape.
529 sal_Int32 nShapeType = 0;
530 if (ShapeContainer* pShapeContainer = mrShape.getContainer())
532 OUString aType = mrShapeModel.maType;
533 if (!aType.isEmpty() && aType[0] == '#')
535 aType = aType.copy(1);
537 if (const ShapeType* pShapeType = pShapeContainer->getShapeTypeById(aType))
539 nShapeType = pShapeType->getTypeModel().moShapeType.value();
542 mrShapeModel.mbInGroup = (getParentElement() == VML_TOKEN(group));
544 // FIXME: the shape with textbox should be used for the next cases
545 if (getCurrentElement() == VML_TOKEN(rect) || nShapeType == ESCHER_ShpInst_TextBox)
547 if (mrShapeModel.mbInGroup)
548 // FIXME: without this a text will be added into the group-shape instead of its
549 // parent shape
550 dynamic_cast<SimpleShape&>(mrShape).setService("com.sun.star.drawing.TextShape");
551 else
552 // FIXME: without this we does not handle some properties like shadow
553 dynamic_cast<SimpleShape&>(mrShape).setService("com.sun.star.text.TextFrame");
555 return new TextBoxContext( *this, mrShapeModel.createTextBox(mrShape.getTypeModel()), rAttribs,
556 mrShape.getDrawing().getFilter().getGraphicHelper());
558 case VMLX_TOKEN( ClientData ):
559 // tdf#41466 ActiveX control shapes with a textbox are transformed into a frame
560 // (see unit test testActiveXOptionButtonGroup)
561 dynamic_cast<SimpleShape&>(mrShape).setService("com.sun.star.text.TextFrame");
562 return new ClientDataContext( *this, mrShapeModel.createClientData(), rAttribs );
563 case VMLPPT_TOKEN( textdata ):
564 // Force RectangleShape, this is ugly :(
565 // and is there because of the lines above which change it to TextFrame
566 dynamic_cast< SimpleShape& >( mrShape ).setService(
567 "com.sun.star.drawing.RectangleShape");
568 mrShapeModel.maLegacyDiagramPath = getFragmentPathFromRelId(rAttribs.getStringDefaulted(XML_id));
569 break;
570 case O_TOKEN( signatureline ):
571 mrShapeModel.mbIsSignatureLine = true;
572 mrShapeModel.maSignatureId = rAttribs.getStringDefaulted(XML_id);
573 mrShapeModel.maSignatureLineSuggestedSignerName
574 = rAttribs.getStringDefaulted(O_TOKEN(suggestedsigner));
575 mrShapeModel.maSignatureLineSuggestedSignerTitle
576 = rAttribs.getStringDefaulted(O_TOKEN(suggestedsigner2));
577 mrShapeModel.maSignatureLineSuggestedSignerEmail
578 = rAttribs.getStringDefaulted(O_TOKEN(suggestedsigneremail));
579 mrShapeModel.maSignatureLineSigningInstructions
580 = rAttribs.getStringDefaulted(O_TOKEN(signinginstructions));
581 mrShapeModel.mbSignatureLineShowSignDate = ConversionHelper::decodeBool(
582 rAttribs.getString(XML_showsigndate, "t")); // default is true
583 mrShapeModel.mbSignatureLineCanAddComment = ConversionHelper::decodeBool(
584 rAttribs.getString(XML_allowcomments, "f")); // default is false
585 break;
586 case O_TOKEN( lock ):
587 // TODO
588 break;
590 // handle remaining stuff in base class
591 return ShapeTypeContext::onCreateContext( nElement, rAttribs );
594 void ShapeContext::setPoints(std::u16string_view rPoints)
596 mrShapeModel.maPoints.clear();
597 sal_Int32 nIndex = 0;
599 while (nIndex >= 0)
601 sal_Int32 nX = ConversionHelper::decodeMeasureToTwip(
602 mrShape.getDrawing().getFilter().getGraphicHelper(), o3tl::getToken(rPoints, 0, ',', nIndex),
603 0, true, true);
604 sal_Int32 nY = ConversionHelper::decodeMeasureToTwip(
605 mrShape.getDrawing().getFilter().getGraphicHelper(), o3tl::getToken(rPoints, 0, ',', nIndex),
606 0, false, true);
607 mrShapeModel.maPoints.emplace_back(nX, nY);
609 // VML polyline has no size in its style attribute. Word writes the size to attribute
610 // coordsize with values in twip but without unit. For others we get size from points.
611 if (!mrShape.getTypeModel().maWidth.isEmpty() || !mrShape.getTypeModel().maHeight.isEmpty())
612 return;
614 if (mrShape.getTypeModel().moCoordSize.has_value())
616 double fWidth = mrShape.getTypeModel().moCoordSize.value().first;
617 fWidth = o3tl::convert(fWidth, o3tl::Length::twip, o3tl::Length::pt);
618 double fHeight = mrShape.getTypeModel().moCoordSize.value().second;
619 fHeight = o3tl::convert(fHeight, o3tl::Length::twip, o3tl::Length::pt);
620 mrShape.getTypeModel().maWidth = OUString::number(fWidth) + "pt";
621 mrShape.getTypeModel().maHeight = OUString::number(fHeight) + "pt";
623 else if (mrShapeModel.maPoints.size())
625 double fMinX = mrShapeModel.maPoints[0].X;
626 double fMaxX = mrShapeModel.maPoints[0].X;
627 double fMinY = mrShapeModel.maPoints[0].Y;
628 double fMaxY = mrShapeModel.maPoints[0].Y;
629 for (const auto& rPoint : mrShapeModel.maPoints)
631 if (rPoint.X < fMinX)
632 fMinX = rPoint.X;
633 else if (rPoint.X > fMaxX)
634 fMaxX = rPoint.X;
635 if (rPoint.Y < fMinY)
636 fMinY = rPoint.Y;
637 else if (rPoint.Y > fMaxY)
638 fMaxY = rPoint.Y;
640 mrShape.getTypeModel().maWidth
641 = OUString::number(
642 o3tl::convert(fMaxX - fMinX, o3tl::Length::twip, o3tl::Length::pt))
643 + "pt";
644 mrShape.getTypeModel().maHeight
645 = OUString::number(
646 o3tl::convert(fMaxY - fMinY, o3tl::Length::twip, o3tl::Length::pt))
647 + "pt";
648 // Set moCoordSize, otherwise default (1000,1000) is used.
649 mrShape.getTypeModel().moCoordSize =
650 Int32Pair(basegfx::fround(fMaxX - fMinX), basegfx::fround(fMaxY - fMinY));
654 void ShapeContext::setFrom( const OUString& rPoints )
656 if (!rPoints.isEmpty())
657 mrShapeModel.maFrom = rPoints;
660 void ShapeContext::setTo( const OUString& rPoints )
662 if (!rPoints.isEmpty())
663 mrShapeModel.maTo = rPoints;
666 void ShapeContext::setControl1( const OUString& rPoints )
668 if (!rPoints.isEmpty())
669 mrShapeModel.maControl1 = rPoints;
672 void ShapeContext::setControl2( const OUString& rPoints )
674 if (!rPoints.isEmpty())
675 mrShapeModel.maControl2 = rPoints;
677 void ShapeContext::setVmlPath( const OUString& rPath )
679 if (!rPath.isEmpty())
680 mrShapeModel.maVmlPath = rPath;
683 void ShapeContext::setHyperlink( const OUString& rHyperlink )
685 if (!rHyperlink.isEmpty())
686 mrShapeModel.maHyperlink = rHyperlink;
689 GroupShapeContext::GroupShapeContext(ContextHandler2Helper const& rParent,
690 const std::shared_ptr<GroupShape>& pShape,
691 const AttributeList& rAttribs)
692 : ShapeContext(rParent, pShape, rAttribs)
693 , mrShapes(pShape->getChildren())
697 ContextHandlerRef GroupShapeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
699 // try to create a context of an embedded shape
700 ContextHandlerRef xContext = createShapeContext( *this, mrShapes, nElement, rAttribs );
701 // handle remaining stuff of this shape in base class
702 return xContext ? xContext : ShapeContext::onCreateContext( nElement, rAttribs );
705 RectangleShapeContext::RectangleShapeContext(ContextHandler2Helper const& rParent,
706 const AttributeList& rAttribs,
707 const std::shared_ptr<RectangleShape>& pShape)
708 : ShapeContext(rParent, pShape, rAttribs)
712 ContextHandlerRef RectangleShapeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
714 // The parent class's context is fine
715 return ShapeContext::onCreateContext( nElement, rAttribs );
718 ControlShapeContext::ControlShapeContext( ::oox::core::ContextHandler2Helper const & rParent, ShapeContainer& rShapes, const AttributeList& rAttribs )
719 : ShapeContextBase (rParent)
721 ::oox::vml::ControlInfo aInfo;
722 aInfo.maShapeId = rAttribs.getXString( W_TOKEN( shapeid ), OUString() );
723 aInfo.maFragmentPath = getFragmentPathFromRelId(rAttribs.getStringDefaulted( R_TOKEN(id)));
724 aInfo.maName = rAttribs.getStringDefaulted( W_TOKEN( name ));
725 aInfo.mbTextContentShape = true;
726 rShapes.getDrawing().registerControl(aInfo);
729 } // namespace oox
731 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */