1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <string_view>
23 
24 #include <oox/vml/vmlshapecontext.hxx>
25 
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>
35 
36 #include <osl/diagnose.h>
37 #include <filter/msfilter/escherex.hxx>
38 
39 namespace oox::vml {
40 
41 using namespace ::com::sun::star;
42 
43 using ::oox::core::ContextHandler2;
44 using ::oox::core::ContextHandler2Helper;
45 using ::oox::core::ContextHandlerRef;
46 
47 namespace {
48 
49 /** Returns the boolean value from the specified VML attribute (if present).
50  */
lclDecodeBool(const AttributeList & rAttribs,sal_Int32 nToken)51 OptValue< bool > lclDecodeBool( const AttributeList& rAttribs, sal_Int32 nToken )
52 {
53     OptValue< OUString > oValue = rAttribs.getString( nToken );
54     if( oValue.has() ) return OptValue< bool >( ConversionHelper::decodeBool( oValue.get() ) );
55     return OptValue< bool >();
56 }
57 
58 /** Returns the percentage value from the specified VML attribute (if present).
59     The value will be normalized (1.0 is returned for 100%).
60  */
lclDecodePercent(const AttributeList & rAttribs,sal_Int32 nToken,double fDefValue)61 OptValue< double > lclDecodePercent( const AttributeList& rAttribs, sal_Int32 nToken, double fDefValue )
62 {
63     OptValue< OUString > oValue = rAttribs.getString( nToken );
64     if( oValue.has() ) return OptValue< double >( ConversionHelper::decodePercent( oValue.get(), fDefValue ) );
65     return OptValue< double >();
66 }
67 
68 /** #119750# Special method for opacity; it *should* be a percentage value, but there are cases
69     where a value relative to 0xffff (65536) is used, ending with an 'f'
70  */
lclDecodeOpacity(const AttributeList & rAttribs,sal_Int32 nToken,double fDefValue)71 OptValue< double > lclDecodeOpacity( const AttributeList& rAttribs, sal_Int32 nToken, double fDefValue )
72 {
73     OptValue< OUString > oValue = rAttribs.getString( nToken );
74     double fRetval(fDefValue);
75 
76     if( oValue.has() )
77     {
78         const OUString& aString(oValue.get());
79         const sal_Int32 nLength(aString.getLength());
80 
81         if(nLength > 0)
82         {
83             if(aString.endsWith("f"))
84             {
85                 fRetval = std::clamp(aString.toDouble() / 65536.0, 0.0, 1.0);
86             }
87             else
88             {
89                 fRetval = ConversionHelper::decodePercent( aString, fDefValue );
90             }
91         }
92     }
93 
94     return OptValue< double >(fRetval);
95 }
96 
97 /** Returns the integer value pair from the specified VML attribute (if present).
98  */
lclDecodeInt32Pair(const AttributeList & rAttribs,sal_Int32 nToken)99 OptValue< Int32Pair > lclDecodeInt32Pair( const AttributeList& rAttribs, sal_Int32 nToken )
100 {
101     OptValue< OUString > oValue = rAttribs.getString( nToken );
102     OptValue< Int32Pair > oRetValue;
103     if( oValue.has() )
104     {
105         OUString aValue1, aValue2;
106         ConversionHelper::separatePair( aValue1, aValue2, oValue.get(), ',' );
107         oRetValue = Int32Pair( aValue1.toInt32(), aValue2.toInt32() );
108     }
109     return oRetValue;
110 }
111 
112 /** Returns the percentage pair from the specified VML attribute (if present).
113  */
lclDecodePercentPair(const AttributeList & rAttribs,sal_Int32 nToken)114 OptValue< DoublePair > lclDecodePercentPair( const AttributeList& rAttribs, sal_Int32 nToken )
115 {
116     OptValue< OUString > oValue = rAttribs.getString( nToken );
117     OptValue< DoublePair > oRetValue;
118     if( oValue.has() )
119     {
120         OUString aValue1, aValue2;
121         ConversionHelper::separatePair( aValue1, aValue2, oValue.get(), ',' );
122         oRetValue = DoublePair(
123             ConversionHelper::decodePercent( aValue1, 0.0 ),
124             ConversionHelper::decodePercent( aValue2, 0.0 ) );
125     }
126     return oRetValue;
127 }
128 
129 /** Returns the boolean value from the passed string of an attribute in the x:
130     namespace (VML for spreadsheets). Supported values: f, t, False, True.
131     @param bDefaultForEmpty  Default value for the empty string.
132  */
lclDecodeVmlxBool(std::u16string_view rValue,bool bDefaultForEmpty)133 bool lclDecodeVmlxBool( std::u16string_view rValue, bool bDefaultForEmpty )
134 {
135     if( rValue.empty() ) return bDefaultForEmpty;
136     sal_Int32 nToken = AttributeConversion::decodeToken( rValue );
137     // anything else than 't' or 'True' is considered to be false, as specified
138     return (nToken == XML_t) || (nToken == XML_True);
139 }
140 
141 } // namespace
142 
ShapeLayoutContext(ContextHandler2Helper const & rParent,Drawing & rDrawing)143 ShapeLayoutContext::ShapeLayoutContext( ContextHandler2Helper const & rParent, Drawing& rDrawing ) :
144     ContextHandler2( rParent ),
145     mrDrawing( rDrawing )
146 {
147 }
148 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)149 ContextHandlerRef ShapeLayoutContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
150 {
151     switch( nElement )
152     {
153         case O_TOKEN( idmap ):
154         {
155             OUString aBlockIds = rAttribs.getString( XML_data, OUString() );
156             sal_Int32 nIndex = 0;
157             while( nIndex >= 0 )
158             {
159                 OUString aToken = aBlockIds.getToken( 0, ' ', nIndex ).trim();
160                 if( !aToken.isEmpty() )
161                     mrDrawing.registerBlockId( aToken.toInt32() );
162             }
163         }
164         break;
165     }
166     return nullptr;
167 }
168 
ClientDataContext(ContextHandler2Helper const & rParent,ClientData & rClientData,const AttributeList & rAttribs)169 ClientDataContext::ClientDataContext( ContextHandler2Helper const & rParent,
170         ClientData& rClientData, const AttributeList& rAttribs ) :
171     ContextHandler2( rParent ),
172     mrClientData( rClientData )
173 {
174     mrClientData.mnObjType = rAttribs.getToken( XML_ObjectType, XML_TOKEN_INVALID );
175 }
176 
onCreateContext(sal_Int32,const AttributeList &)177 ContextHandlerRef ClientDataContext::onCreateContext( sal_Int32 /*nElement*/, const AttributeList& /*rAttribs*/ )
178 {
179     if( isRootElement() )
180     {
181         maElementText.clear();
182         return this;
183     }
184     return nullptr;
185 }
186 
onCharacters(const OUString & rChars)187 void ClientDataContext::onCharacters( const OUString& rChars )
188 {
189     /*  Empty but existing elements have special meaning, e.g. 'true'. Collect
190         existing text and convert it in onEndElement(). */
191     maElementText = rChars;
192 }
193 
onEndElement()194 void ClientDataContext::onEndElement()
195 {
196     switch( getCurrentElement() )
197     {
198         case VMLX_TOKEN( Anchor ):      mrClientData.maAnchor = maElementText;                                          break;
199         case VMLX_TOKEN( FmlaMacro ):   mrClientData.maFmlaMacro = maElementText;                                       break;
200         case VMLX_TOKEN( FmlaPict ):    mrClientData.maFmlaPict = maElementText;                                        break;
201         case VMLX_TOKEN( FmlaLink ):    mrClientData.maFmlaLink = maElementText;                                        break;
202         case VMLX_TOKEN( FmlaRange ):   mrClientData.maFmlaRange = maElementText;                                       break;
203         case VMLX_TOKEN( FmlaGroup ):   mrClientData.maFmlaGroup = maElementText;                                       break;
204         case VMLX_TOKEN( TextHAlign ):  mrClientData.mnTextHAlign = AttributeConversion::decodeToken( maElementText );  break;
205         case VMLX_TOKEN( TextVAlign ):  mrClientData.mnTextVAlign = AttributeConversion::decodeToken( maElementText );  break;
206         case VMLX_TOKEN( Column ):      mrClientData.mnCol = maElementText.toInt32();                                   break;
207         case VMLX_TOKEN( Row ):         mrClientData.mnRow = maElementText.toInt32();                                   break;
208         case VMLX_TOKEN( Checked ):     mrClientData.mnChecked = maElementText.toInt32();                               break;
209         case VMLX_TOKEN( DropStyle ):   mrClientData.mnDropStyle = AttributeConversion::decodeToken( maElementText );   break;
210         case VMLX_TOKEN( DropLines ):   mrClientData.mnDropLines = maElementText.toInt32();                             break;
211         case VMLX_TOKEN( Val ):         mrClientData.mnVal = maElementText.toInt32();                                   break;
212         case VMLX_TOKEN( Min ):         mrClientData.mnMin = maElementText.toInt32();                                   break;
213         case VMLX_TOKEN( Max ):         mrClientData.mnMax = maElementText.toInt32();                                   break;
214         case VMLX_TOKEN( Inc ):         mrClientData.mnInc = maElementText.toInt32();                                   break;
215         case VMLX_TOKEN( Page ):        mrClientData.mnPage = maElementText.toInt32();                                  break;
216         case VMLX_TOKEN( SelType ):     mrClientData.mnSelType = AttributeConversion::decodeToken( maElementText );     break;
217         case VMLX_TOKEN( VTEdit ):      mrClientData.mnVTEdit = maElementText.toInt32();                                break;
218         case VMLX_TOKEN( PrintObject ): mrClientData.mbPrintObject = lclDecodeVmlxBool( maElementText, true );          break;
219         case VMLX_TOKEN( Visible ):     mrClientData.mbVisible = lclDecodeVmlxBool( maElementText, true );              break;
220         case VMLX_TOKEN( DDE ):         mrClientData.mbDde = lclDecodeVmlxBool( maElementText, true );                  break;
221         case VMLX_TOKEN( NoThreeD ):    mrClientData.mbNo3D = lclDecodeVmlxBool( maElementText, true );                 break;
222         case VMLX_TOKEN( NoThreeD2 ):   mrClientData.mbNo3D2 = lclDecodeVmlxBool( maElementText, true );                break;
223         case VMLX_TOKEN( MultiLine ):   mrClientData.mbMultiLine = lclDecodeVmlxBool( maElementText, true );            break;
224         case VMLX_TOKEN( VScroll ):     mrClientData.mbVScroll = lclDecodeVmlxBool( maElementText, true );              break;
225         case VMLX_TOKEN( SecretEdit ):  mrClientData.mbSecretEdit = lclDecodeVmlxBool( maElementText, true );           break;
226     }
227 }
228 
ShapeContextBase(ContextHandler2Helper const & rParent)229 ShapeContextBase::ShapeContextBase( ContextHandler2Helper const & rParent ) :
230     ContextHandler2( rParent )
231 {
232 }
233 
createShapeContext(ContextHandler2Helper const & rParent,ShapeContainer & rShapes,sal_Int32 nElement,const AttributeList & rAttribs)234 ContextHandlerRef ShapeContextBase::createShapeContext( ContextHandler2Helper const & rParent,
235         ShapeContainer& rShapes, sal_Int32 nElement, const AttributeList& rAttribs )
236 {
237     switch( nElement )
238     {
239         case O_TOKEN( shapelayout ):
240             return new ShapeLayoutContext( rParent, rShapes.getDrawing() );
241 
242         case VML_TOKEN( shapetype ):
243             return new ShapeTypeContext( rParent, rShapes.createShapeType(), rAttribs );
244         case VML_TOKEN( group ):
245             return new GroupShapeContext( rParent, rShapes.createShape< GroupShape >(), rAttribs );
246         case VML_TOKEN( shape ):
247             if (rAttribs.hasAttribute(XML_path) &&
248                     // tdf#122563 skip in the case of empty path
249                     !rAttribs.getString(XML_path, "").isEmpty())
250                 return new ShapeContext( rParent, rShapes.createShape< BezierShape >(), rAttribs );
251             else
252                 return new ShapeContext( rParent, rShapes.createShape< ComplexShape >(), rAttribs );
253         case VML_TOKEN( rect ):
254             return new RectangleShapeContext( rParent, rAttribs, rShapes.createShape< RectangleShape >() );
255         case VML_TOKEN( roundrect ):
256             return new ShapeContext( rParent, rShapes.createShape< RectangleShape >(), rAttribs );
257         case VML_TOKEN( oval ):
258             return new ShapeContext( rParent, rShapes.createShape< EllipseShape >(), rAttribs );
259         case VML_TOKEN( polyline ):
260             return new ShapeContext( rParent, rShapes.createShape< PolyLineShape >(), rAttribs );
261         case VML_TOKEN( line ):
262             return new ShapeContext( rParent, rShapes.createShape< LineShape >(), rAttribs );
263         case VML_TOKEN( curve ):
264             return new ShapeContext( rParent, rShapes.createShape< BezierShape >(), rAttribs );
265 
266         // TODO:
267         case VML_TOKEN( arc ):
268         case VML_TOKEN( diagram ):
269         case VML_TOKEN( image ):
270             return new ShapeContext( rParent, rShapes.createShape< ComplexShape >(), rAttribs );
271 
272         case W_TOKEN(control):
273             return new ControlShapeContext( rParent, rShapes, rAttribs );
274     }
275     return nullptr;
276 }
277 
ShapeTypeContext(ContextHandler2Helper const & rParent,std::shared_ptr<ShapeType> const & pShapeType,const AttributeList & rAttribs)278 ShapeTypeContext::ShapeTypeContext(ContextHandler2Helper const & rParent,
279         std::shared_ptr<ShapeType> const& pShapeType,
280         const AttributeList& rAttribs)
281     : ShapeContextBase(rParent)
282     , m_pShapeType(pShapeType) // tdf#112311 keep it alive
283     , mrTypeModel( pShapeType->getTypeModel() )
284 {
285     // shape identifier and shape name
286     bool bHasOspid = rAttribs.hasAttribute( O_TOKEN( spid ) );
287     mrTypeModel.maShapeId = rAttribs.getXString( bHasOspid ? O_TOKEN( spid ) : XML_id, OUString() );
288     mrTypeModel.maLegacyId = rAttribs.getString( XML_id, OUString() );
289     OSL_ENSURE( !mrTypeModel.maShapeId.isEmpty(), "ShapeTypeContext::ShapeTypeContext - missing shape identifier" );
290     // builtin shape type identifier
291     mrTypeModel.moShapeType = rAttribs.getInteger( O_TOKEN( spt ) );
292     // if the o:spid attribute exists, the id attribute contains the user-defined shape name
293     if( bHasOspid )
294     {
295         mrTypeModel.maShapeName = rAttribs.getXString( XML_id, OUString() );
296         // get ShapeType and ShapeId from name for compatibility
297         static constexpr OUStringLiteral sShapeTypePrefix = u"shapetype_";
298         OUString tmp;
299         if( mrTypeModel.maShapeName.startsWith( sShapeTypePrefix ) )
300         {
301             mrTypeModel.maShapeId = mrTypeModel.maShapeName;
302             mrTypeModel.moShapeType = mrTypeModel.maShapeName.copy(sShapeTypePrefix.getLength()).toInt32();
303         }
304         else if (mrTypeModel.maShapeName.startsWith("_x0000_t", &tmp))
305         {
306             mrTypeModel.maShapeId = mrTypeModel.maShapeName;
307             mrTypeModel.moShapeType = tmp.toInt32();
308         }
309     }
310 
311     // coordinate system position/size, CSS style
312     mrTypeModel.moCoordPos = lclDecodeInt32Pair( rAttribs, XML_coordorigin );
313     mrTypeModel.moCoordSize = lclDecodeInt32Pair( rAttribs, XML_coordsize );
314     setStyle( rAttribs.getString( XML_style, OUString() ) );
315     if( lclDecodeBool( rAttribs, O_TOKEN( hr )).get( false ))
316     {   // MSO's handling of o:hr width is nowhere near what the spec says:
317         // - o:hrpct is not in % but in 0.1%
318         // - if o:hrpct is not given, 100% width is assumed
319         // - given width is used only if explicit o:hrpct="0" is given
320         OUString hrpct = rAttribs.getString( O_TOKEN( hrpct ), "1000" );
321         if( hrpct != "0" )
322             mrTypeModel.maWidthPercent = OUString::number( hrpct.toInt32() );
323         mrTypeModel.maWrapDistanceLeft = "0";
324         mrTypeModel.maWrapDistanceRight = "0";
325         mrTypeModel.maPositionHorizontal = rAttribs.getString( O_TOKEN( hralign ), "left" );
326         mrTypeModel.moWrapType = "topAndBottom";
327     }
328 
329     // stroke settings (may be overridden by v:stroke element later)
330     mrTypeModel.maStrokeModel.moStroked = lclDecodeBool( rAttribs, XML_stroked );
331     mrTypeModel.maStrokeModel.moColor = rAttribs.getString( XML_strokecolor );
332     mrTypeModel.maStrokeModel.moWeight = rAttribs.getString( XML_strokeweight );
333 
334     // fill settings (may be overridden by v:fill element later)
335     mrTypeModel.maFillModel.moFilled = lclDecodeBool( rAttribs, XML_filled );
336     mrTypeModel.maFillModel.moColor = rAttribs.getString( XML_fillcolor );
337 
338     // For roundrect we may have an arcsize attribute to read
339     mrTypeModel.maArcsize = rAttribs.getString(XML_arcsize, OUString());
340     // editas
341     mrTypeModel.maEditAs = rAttribs.getString(XML_editas, OUString());
342 
343     mrTypeModel.maAdjustments = rAttribs.getString(XML_adj, OUString());
344 }
345 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)346 ContextHandlerRef ShapeTypeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
347 {
348     if( isRootElement() ) switch( nElement )
349     {
350         case VML_TOKEN( stroke ):
351             mrTypeModel.maStrokeModel.moStroked.assignIfUsed( lclDecodeBool( rAttribs, XML_on ) );
352             mrTypeModel.maStrokeModel.maStartArrow.moArrowType = rAttribs.getToken( XML_startarrow );
353             mrTypeModel.maStrokeModel.maStartArrow.moArrowWidth = rAttribs.getToken( XML_startarrowwidth );
354             mrTypeModel.maStrokeModel.maStartArrow.moArrowLength = rAttribs.getToken( XML_startarrowlength );
355             mrTypeModel.maStrokeModel.maEndArrow.moArrowType = rAttribs.getToken( XML_endarrow );
356             mrTypeModel.maStrokeModel.maEndArrow.moArrowWidth = rAttribs.getToken( XML_endarrowwidth );
357             mrTypeModel.maStrokeModel.maEndArrow.moArrowLength = rAttribs.getToken( XML_endarrowlength );
358             mrTypeModel.maStrokeModel.moColor.assignIfUsed( rAttribs.getString( XML_color ) );
359             mrTypeModel.maStrokeModel.moOpacity = lclDecodeOpacity( rAttribs, XML_opacity, 1.0 );
360             mrTypeModel.maStrokeModel.moWeight.assignIfUsed( rAttribs.getString( XML_weight ) );
361             mrTypeModel.maStrokeModel.moDashStyle = rAttribs.getString( XML_dashstyle );
362             mrTypeModel.maStrokeModel.moLineStyle = rAttribs.getToken( XML_linestyle );
363             mrTypeModel.maStrokeModel.moEndCap = rAttribs.getToken( XML_endcap );
364             mrTypeModel.maStrokeModel.moJoinStyle = rAttribs.getToken( XML_joinstyle );
365         break;
366         case VML_TOKEN( fill ):
367         {
368             // in DOCX shapes use r:id for the relationship id
369             // in XLSX they use o:relid
370             bool bHasORelId = rAttribs.hasAttribute( O_TOKEN(relid) );
371             mrTypeModel.maFillModel.moFilled.assignIfUsed( lclDecodeBool( rAttribs, XML_on ) );
372             mrTypeModel.maFillModel.moColor.assignIfUsed( rAttribs.getString( XML_color ) );
373             mrTypeModel.maFillModel.moOpacity = lclDecodeOpacity( rAttribs, XML_opacity, 1.0 );
374             mrTypeModel.maFillModel.moColor2 = rAttribs.getString( XML_color2 );
375             mrTypeModel.maFillModel.moOpacity2 = lclDecodeOpacity( rAttribs, XML_opacity2, 1.0 );
376             mrTypeModel.maFillModel.moType = rAttribs.getToken( XML_type );
377             mrTypeModel.maFillModel.moAngle = rAttribs.getInteger( XML_angle );
378             mrTypeModel.maFillModel.moFocus = lclDecodePercent( rAttribs, XML_focus, 0.0 );
379             mrTypeModel.maFillModel.moFocusPos = lclDecodePercentPair( rAttribs, XML_focusposition );
380             mrTypeModel.maFillModel.moFocusSize = lclDecodePercentPair( rAttribs, XML_focussize );
381             mrTypeModel.maFillModel.moBitmapPath = decodeFragmentPath( rAttribs, bHasORelId ? O_TOKEN(relid) : R_TOKEN(id) );
382             mrTypeModel.maFillModel.moRotate = lclDecodeBool( rAttribs, XML_rotate );
383             break;
384         }
385         case VML_TOKEN( imagedata ):
386         {
387             // shapes in docx use r:id for the relationship id
388             // in xlsx it they use o:relid
389             bool bHasORelId = rAttribs.hasAttribute( O_TOKEN( relid ) );
390             mrTypeModel.moGraphicPath = decodeFragmentPath( rAttribs, bHasORelId ? O_TOKEN( relid ) : R_TOKEN( id ) );
391             mrTypeModel.moGraphicTitle = rAttribs.getString( O_TOKEN( title ) );
392 
393             // Get crop attributes.
394             mrTypeModel.moCropBottom = rAttribs.getString(XML_cropbottom);
395             mrTypeModel.moCropLeft = rAttribs.getString(XML_cropleft);
396             mrTypeModel.moCropRight = rAttribs.getString(XML_cropright);
397             mrTypeModel.moCropTop = rAttribs.getString(XML_croptop);
398         }
399         break;
400         case NMSP_vmlWord | XML_wrap:
401             mrTypeModel.moWrapAnchorX = rAttribs.getString(XML_anchorx);
402             mrTypeModel.moWrapAnchorY = rAttribs.getString(XML_anchory);
403             mrTypeModel.moWrapType = rAttribs.getString(XML_type);
404             mrTypeModel.moWrapSide = rAttribs.getString(XML_side);
405         break;
406         case VML_TOKEN( shadow ):
407         {
408             mrTypeModel.maShadowModel.mbHasShadow = true;
409             mrTypeModel.maShadowModel.moShadowOn = lclDecodeBool(rAttribs, XML_on).get(false);
410             mrTypeModel.maShadowModel.moColor.assignIfUsed(rAttribs.getString(XML_color));
411             mrTypeModel.maShadowModel.moOffset.assignIfUsed(rAttribs.getString(XML_offset));
412             mrTypeModel.maShadowModel.moOpacity = lclDecodePercent(rAttribs, XML_opacity, 1.0);
413         }
414         break;
415         case VML_TOKEN( textpath ):
416             mrTypeModel.maTextpathModel.moString.assignIfUsed(rAttribs.getString(XML_string));
417             mrTypeModel.maTextpathModel.moStyle.assignIfUsed(rAttribs.getString(XML_style));
418             mrTypeModel.maTextpathModel.moTrim.assignIfUsed(lclDecodeBool(rAttribs, XML_trim));
419         break;
420     }
421     return nullptr;
422 }
423 
decodeFragmentPath(const AttributeList & rAttribs,sal_Int32 nToken) const424 OptValue< OUString > ShapeTypeContext::decodeFragmentPath( const AttributeList& rAttribs, sal_Int32 nToken ) const
425 {
426     OptValue< OUString > oFragmentPath;
427     OptValue< OUString > oRelId = rAttribs.getString( nToken );
428     if( oRelId.has() )
429         oFragmentPath = getFragmentPathFromRelId( oRelId.get() );
430     return oFragmentPath;
431 }
432 
setStyle(const OUString & rStyle)433 void ShapeTypeContext::setStyle( const OUString& rStyle )
434 {
435     sal_Int32 nIndex = 0;
436     while( nIndex >= 0 )
437     {
438         OUString aName, aValue;
439         if( ConversionHelper::separatePair( aName, aValue, rStyle.getToken( 0, ';', nIndex ), ':' ) )
440         {
441             if( aName == "position" )      mrTypeModel.maPosition = aValue;
442             else if( aName == "z-index" )        mrTypeModel.maZIndex = aValue;
443             else if( aName == "left" )           mrTypeModel.maLeft = aValue;
444             else if( aName == "top" )            mrTypeModel.maTop = aValue;
445             else if( aName == "width" )          mrTypeModel.maWidth = aValue;
446             else if( aName == "height" )         mrTypeModel.maHeight = aValue;
447             else if( aName == "margin-left" )    mrTypeModel.maMarginLeft = aValue;
448             else if( aName == "margin-top" )     mrTypeModel.maMarginTop = aValue;
449             else if( aName == "mso-position-vertical-relative" )  mrTypeModel.maPositionVerticalRelative = aValue;
450             else if( aName == "mso-position-horizontal-relative" )  mrTypeModel.maPositionHorizontalRelative = aValue;
451             else if( aName == "mso-position-horizontal" ) mrTypeModel.maPositionHorizontal = aValue;
452             else if( aName == "mso-position-vertical" ) mrTypeModel.maPositionVertical = aValue;
453             else if( aName == "mso-width-percent" ) mrTypeModel.maWidthPercent = aValue;
454             else if( aName == "mso-width-relative" ) mrTypeModel.maWidthRelative = aValue;
455             else if( aName == "mso-height-percent" ) mrTypeModel.maHeightPercent = aValue;
456             else if( aName == "mso-height-relative" ) mrTypeModel.maHeightRelative = aValue;
457             else if( aName == "mso-fit-shape-to-text" )           mrTypeModel.mbAutoHeight = true;
458             else if( aName == "rotation" )       mrTypeModel.maRotation = aValue;
459             else if( aName == "flip" )       mrTypeModel.maFlip = aValue;
460             else if( aName == "visibility" )
461                 mrTypeModel.mbVisible = aValue != "hidden";
462             else if( aName == "mso-wrap-style" ) mrTypeModel.maWrapStyle = aValue;
463             else if ( aName == "v-text-anchor" ) mrTypeModel.maVTextAnchor = aValue;
464             else if ( aName == "mso-wrap-distance-left" ) mrTypeModel.maWrapDistanceLeft = aValue;
465             else if ( aName == "mso-wrap-distance-right" ) mrTypeModel.maWrapDistanceRight = aValue;
466             else if ( aName == "mso-wrap-distance-top" ) mrTypeModel.maWrapDistanceTop = aValue;
467             else if ( aName == "mso-wrap-distance-bottom" ) mrTypeModel.maWrapDistanceBottom = aValue;
468         }
469     }
470 }
471 
ShapeContext(ContextHandler2Helper const & rParent,const std::shared_ptr<ShapeBase> & pShape,const AttributeList & rAttribs)472 ShapeContext::ShapeContext(ContextHandler2Helper const& rParent,
473                            const std::shared_ptr<ShapeBase>& pShape, const AttributeList& rAttribs)
474     : ShapeTypeContext(rParent, pShape, rAttribs)
475     , mrShape(*pShape)
476     , mrShapeModel(pShape->getShapeModel())
477 {
478     // collect shape specific attributes
479     mrShapeModel.maType = rAttribs.getXString( XML_type, OUString() );
480     // polyline path
481     setPoints( rAttribs.getString( XML_points, OUString() ) );
482     // line start and end positions
483     setFrom(rAttribs.getString(XML_from, OUString()));
484     setTo(rAttribs.getString(XML_to, OUString()));
485     setControl1(rAttribs.getString(XML_control1, OUString()));
486     setControl2(rAttribs.getString(XML_control2, OUString()));
487     setVmlPath(rAttribs.getString(XML_path, OUString()));
488 }
489 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)490 ContextHandlerRef ShapeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
491 {
492     // Excel specific shape client data
493     if( isRootElement() ) switch( nElement )
494     {
495         case VML_TOKEN( textbox ):
496         {
497             // Calculate the shape type: map both <rect> and <v:shape> with a textbox shape type to
498             // a TextShape.
499             sal_Int32 nShapeType = 0;
500             if (ShapeContainer* pShapeContainer = mrShape.getContainer())
501             {
502                 OUString aType = mrShapeModel.maType;
503                 if (!aType.isEmpty() && aType[0] == '#')
504                 {
505                     aType = aType.copy(1);
506                 }
507                 if (const ShapeType* pShapeType = pShapeContainer->getShapeTypeById(aType))
508                 {
509                     nShapeType = pShapeType->getTypeModel().moShapeType.get();
510                 }
511             }
512             mrShapeModel.mbInGroup = (getParentElement() == VML_TOKEN(group));
513 
514             // FIXME: the shape with textbox should be used for the next cases
515             if (getCurrentElement() == VML_TOKEN(rect) || nShapeType == ESCHER_ShpInst_TextBox)
516             {
517                 if (mrShapeModel.mbInGroup)
518                     // FIXME: without this a text will be added into the group-shape instead of its
519                     // parent shape
520                     dynamic_cast<SimpleShape&>(mrShape).setService("com.sun.star.drawing.TextShape");
521                 else
522                     // FIXME: without this we does not handle some properties like shadow
523                     dynamic_cast<SimpleShape&>(mrShape).setService("com.sun.star.text.TextFrame");
524             }
525             return new TextBoxContext( *this, mrShapeModel.createTextBox(mrShape.getTypeModel()), rAttribs,
526                 mrShape.getDrawing().getFilter().getGraphicHelper());
527         }
528         case VMLX_TOKEN( ClientData ):
529             // tdf#41466 ActiveX control shapes with a textbox are transformed into a frame
530             // (see unit test testActiveXOptionButtonGroup)
531             dynamic_cast<SimpleShape&>(mrShape).setService("com.sun.star.text.TextFrame");
532             return new ClientDataContext( *this, mrShapeModel.createClientData(), rAttribs );
533         case VMLPPT_TOKEN( textdata ):
534             // Force RectangleShape, this is ugly :(
535             // and is there because of the lines above which change it to TextFrame
536             dynamic_cast< SimpleShape& >( mrShape ).setService(
537                     "com.sun.star.drawing.RectangleShape");
538             mrShapeModel.maLegacyDiagramPath = getFragmentPathFromRelId(rAttribs.getString(XML_id, OUString()));
539             break;
540         case O_TOKEN( signatureline ):
541             mrShapeModel.mbIsSignatureLine = true;
542             mrShapeModel.maSignatureId = rAttribs.getString(XML_id, OUString());
543             mrShapeModel.maSignatureLineSuggestedSignerName
544                 = rAttribs.getString(O_TOKEN(suggestedsigner), OUString());
545             mrShapeModel.maSignatureLineSuggestedSignerTitle
546                 = rAttribs.getString(O_TOKEN(suggestedsigner2), OUString());
547             mrShapeModel.maSignatureLineSuggestedSignerEmail
548                 = rAttribs.getString(O_TOKEN(suggestedsigneremail), OUString());
549             mrShapeModel.maSignatureLineSigningInstructions
550                 = rAttribs.getString(O_TOKEN(signinginstructions), OUString());
551             mrShapeModel.mbSignatureLineShowSignDate = ConversionHelper::decodeBool(
552                 rAttribs.getString(XML_showsigndate, "t")); // default is true
553             mrShapeModel.mbSignatureLineCanAddComment = ConversionHelper::decodeBool(
554                 rAttribs.getString(XML_allowcomments, "f")); // default is false
555             break;
556         case O_TOKEN( lock ):
557             // TODO
558             break;
559     }
560     // handle remaining stuff in base class
561     return ShapeTypeContext::onCreateContext( nElement, rAttribs );
562 }
563 
setPoints(const OUString & rPoints)564 void ShapeContext::setPoints( const OUString& rPoints )
565 {
566     mrShapeModel.maPoints.clear();
567     sal_Int32 nIndex = 0;
568 
569     while( nIndex >= 0 )
570     {
571         sal_Int32 nX = rPoints.getToken( 0, ',', nIndex ).toInt32();
572         sal_Int32 nY = rPoints.getToken( 0, ',', nIndex ).toInt32();
573         mrShapeModel.maPoints.emplace_back( nX, nY );
574     }
575 }
576 
setFrom(const OUString & rPoints)577 void ShapeContext::setFrom( const OUString& rPoints )
578 {
579     if (!rPoints.isEmpty())
580         mrShapeModel.maFrom = rPoints;
581 }
582 
setTo(const OUString & rPoints)583 void ShapeContext::setTo( const OUString& rPoints )
584 {
585     if (!rPoints.isEmpty())
586         mrShapeModel.maTo = rPoints;
587 }
588 
setControl1(const OUString & rPoints)589 void ShapeContext::setControl1( const OUString& rPoints )
590 {
591     if (!rPoints.isEmpty())
592         mrShapeModel.maControl1 = rPoints;
593 }
594 
setControl2(const OUString & rPoints)595 void ShapeContext::setControl2( const OUString& rPoints )
596 {
597     if (!rPoints.isEmpty())
598         mrShapeModel.maControl2 = rPoints;
599 }
setVmlPath(const OUString & rPath)600 void ShapeContext::setVmlPath( const OUString& rPath )
601 {
602     if (!rPath.isEmpty())
603         mrShapeModel.maVmlPath = rPath;
604 }
605 
GroupShapeContext(ContextHandler2Helper const & rParent,const std::shared_ptr<GroupShape> & pShape,const AttributeList & rAttribs)606 GroupShapeContext::GroupShapeContext(ContextHandler2Helper const& rParent,
607                                      const std::shared_ptr<GroupShape>& pShape,
608                                      const AttributeList& rAttribs)
609     : ShapeContext(rParent, pShape, rAttribs)
610     , mrShapes(pShape->getChildren())
611 {
612 }
613 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)614 ContextHandlerRef GroupShapeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
615 {
616     // try to create a context of an embedded shape
617     ContextHandlerRef xContext = createShapeContext( *this, mrShapes, nElement, rAttribs );
618     // handle remaining stuff of this shape in base class
619     return xContext ? xContext : ShapeContext::onCreateContext( nElement, rAttribs );
620 }
621 
RectangleShapeContext(ContextHandler2Helper const & rParent,const AttributeList & rAttribs,const std::shared_ptr<RectangleShape> & pShape)622 RectangleShapeContext::RectangleShapeContext(ContextHandler2Helper const& rParent,
623                                              const AttributeList& rAttribs,
624                                              const std::shared_ptr<RectangleShape>& pShape)
625     : ShapeContext(rParent, pShape, rAttribs)
626 {
627 }
628 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)629 ContextHandlerRef RectangleShapeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
630 {
631     // The parent class's context is fine
632     return ShapeContext::onCreateContext( nElement, rAttribs );
633 }
634 
ControlShapeContext(::oox::core::ContextHandler2Helper const & rParent,ShapeContainer & rShapes,const AttributeList & rAttribs)635 ControlShapeContext::ControlShapeContext( ::oox::core::ContextHandler2Helper const & rParent, ShapeContainer& rShapes, const AttributeList& rAttribs )
636     : ShapeContextBase (rParent)
637 {
638     ::oox::vml::ControlInfo aInfo;
639     aInfo.maShapeId = rAttribs.getXString( W_TOKEN( shapeid ), OUString() );
640     aInfo.maFragmentPath = getFragmentPathFromRelId(rAttribs.getString( R_TOKEN(id), OUString() ));
641     aInfo.maName = rAttribs.getString( W_TOKEN( name ), OUString() );
642     aInfo.mbTextContentShape = true;
643     rShapes.getDrawing().registerControl(aInfo);
644 }
645 
646 } // namespace oox
647 
648 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
649