xref: /aoo41x/main/oox/source/vml/vmlshapecontext.cxx (revision 7074a11e)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "oox/vml/vmlshapecontext.hxx"
25 
26 #include "oox/vml/vmldrawing.hxx"
27 #include "oox/vml/vmlshape.hxx"
28 #include "oox/vml/vmlshapecontainer.hxx"
29 #include "oox/vml/vmltextboxcontext.hxx"
30 
31 namespace oox {
32 namespace vml {
33 
34 // ============================================================================
35 
36 using namespace ::com::sun::star::awt;
37 
38 using ::oox::core::ContextHandler2;
39 using ::oox::core::ContextHandler2Helper;
40 using ::oox::core::ContextHandlerRef;
41 using ::rtl::OUString;
42 
43 // ============================================================================
44 
45 namespace {
46 
47 /** Returns the boolean value from the specified VML attribute (if present).
48  */
lclDecodeBool(const AttributeList & rAttribs,sal_Int32 nToken)49 OptValue< bool > lclDecodeBool( const AttributeList& rAttribs, sal_Int32 nToken )
50 {
51     OptValue< OUString > oValue = rAttribs.getString( nToken );
52     if( oValue.has() ) return OptValue< bool >( ConversionHelper::decodeBool( oValue.get() ) );
53     return OptValue< bool >();
54 }
55 
56 /** Returns the percentage value from the specified VML attribute (if present).
57     The value will be normalized (1.0 is returned for 100%).
58  */
lclDecodePercent(const AttributeList & rAttribs,sal_Int32 nToken,double fDefValue)59 OptValue< double > lclDecodePercent( const AttributeList& rAttribs, sal_Int32 nToken, double fDefValue )
60 {
61     OptValue< OUString > oValue = rAttribs.getString( nToken );
62     if( oValue.has() ) return OptValue< double >( ConversionHelper::decodePercent( oValue.get(), fDefValue ) );
63     return OptValue< double >();
64 }
65 
66 /** #119750# Special method for opacity; it *should* be a percentage value, but there are cases
67     where a value relative to 0xffff (65536) is used, ending with an 'f'
68  */
lclDecodeOpacity(const AttributeList & rAttribs,sal_Int32 nToken,double fDefValue)69 OptValue< double > lclDecodeOpacity( const AttributeList& rAttribs, sal_Int32 nToken, double fDefValue )
70 {
71     OptValue< OUString > oValue = rAttribs.getString( nToken );
72     double fRetval(fDefValue);
73 
74     if( oValue.has() )
75     {
76         const OUString aString(oValue.get());
77         const sal_Int32 nLength(aString.getLength());
78 
79         if(nLength > 0)
80         {
81             if(aString.endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM("f")))
82             {
83                 fRetval = std::max(0.0, std::min(1.0, aString.toDouble() / 65536.0));
84             }
85             else
86             {
87                 fRetval = ConversionHelper::decodePercent( aString, fDefValue );
88             }
89         }
90     }
91 
92     return OptValue< double >(fRetval);
93 }
94 
95 /** Returns the integer value pair from the specified VML attribute (if present).
96  */
lclDecodeInt32Pair(const AttributeList & rAttribs,sal_Int32 nToken)97 OptValue< Int32Pair > lclDecodeInt32Pair( const AttributeList& rAttribs, sal_Int32 nToken )
98 {
99     OptValue< OUString > oValue = rAttribs.getString( nToken );
100     OptValue< Int32Pair > oRetValue;
101     if( oValue.has() )
102     {
103         OUString aValue1, aValue2;
104         ConversionHelper::separatePair( aValue1, aValue2, oValue.get(), ',' );
105         oRetValue = Int32Pair( aValue1.toInt32(), aValue2.toInt32() );
106     }
107     return oRetValue;
108 }
109 
110 /** Returns the percentage pair from the specified VML attribute (if present).
111  */
lclDecodePercentPair(const AttributeList & rAttribs,sal_Int32 nToken)112 OptValue< DoublePair > lclDecodePercentPair( const AttributeList& rAttribs, sal_Int32 nToken )
113 {
114     OptValue< OUString > oValue = rAttribs.getString( nToken );
115     OptValue< DoublePair > oRetValue;
116     if( oValue.has() )
117     {
118         OUString aValue1, aValue2;
119         ConversionHelper::separatePair( aValue1, aValue2, oValue.get(), ',' );
120         oRetValue = DoublePair(
121             ConversionHelper::decodePercent( aValue1, 0.0 ),
122             ConversionHelper::decodePercent( aValue2, 0.0 ) );
123     }
124     return oRetValue;
125 }
126 
127 /** Returns the boolean value from the passed string of an attribute in the x:
128     namespace (VML for spreadsheets). Supported values: f, t, False, True.
129     @param bDefaultForEmpty  Default value for the empty string.
130  */
lclDecodeVmlxBool(const OUString & rValue,bool bDefaultForEmpty)131 bool lclDecodeVmlxBool( const OUString& rValue, bool bDefaultForEmpty )
132 {
133     if( rValue.getLength() == 0 ) return bDefaultForEmpty;
134     sal_Int32 nToken = AttributeConversion::decodeToken( rValue );
135     // anything else than 't' or 'True' is considered to be false, as specified
136     return (nToken == XML_t) || (nToken == XML_True);
137 }
138 
139 } // namespace
140 
141 // ============================================================================
142 
ShapeLayoutContext(ContextHandler2Helper & rParent,Drawing & rDrawing)143 ShapeLayoutContext::ShapeLayoutContext( ContextHandler2Helper& rParent, Drawing& rDrawing ) :
144     ContextHandler2( rParent ),
145     mrDrawing( rDrawing )
146 {
147 }
148 
149 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)150 ContextHandlerRef ShapeLayoutContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
151 {
152     switch( nElement )
153     {
154         case O_TOKEN( idmap ):
155         {
156             OUString aBlockIds = rAttribs.getString( XML_data, OUString() );
157             sal_Int32 nIndex = 0;
158             while( nIndex >= 0 )
159             {
160                 OUString aToken = aBlockIds.getToken( 0, ' ', nIndex ).trim();
161                 if( aToken.getLength() > 0 )
162                     mrDrawing.registerBlockId( aToken.toInt32() );
163             }
164         }
165         break;
166     }
167     return 0;
168 }
169 
170 // ============================================================================
171 
ClientDataContext(ContextHandler2Helper & rParent,ClientData & rClientData,const AttributeList & rAttribs)172 ClientDataContext::ClientDataContext( ContextHandler2Helper& rParent,
173         ClientData& rClientData, const AttributeList& rAttribs ) :
174     ContextHandler2( rParent ),
175     mrClientData( rClientData )
176 {
177     mrClientData.mnObjType = rAttribs.getToken( XML_ObjectType, XML_TOKEN_INVALID );
178 }
179 
onCreateContext(sal_Int32,const AttributeList &)180 ContextHandlerRef ClientDataContext::onCreateContext( sal_Int32 /*nElement*/, const AttributeList& /*rAttribs*/ )
181 {
182     if( isRootElement() )
183     {
184         maElementText = OUString();
185         return this;
186     }
187     return 0;
188 }
189 
onCharacters(const OUString & rChars)190 void ClientDataContext::onCharacters( const OUString& rChars )
191 {
192     /*  Empty but existing elements have special meaning, e.g. 'true'. Collect
193         existing text and convert it in onEndElement(). */
194     maElementText = rChars;
195 }
196 
onEndElement()197 void ClientDataContext::onEndElement()
198 {
199     switch( getCurrentElement() )
200     {
201         case VMLX_TOKEN( Anchor ):      mrClientData.maAnchor = maElementText;                                          break;
202         case VMLX_TOKEN( FmlaMacro ):   mrClientData.maFmlaMacro = maElementText;                                       break;
203         case VMLX_TOKEN( FmlaPict ):    mrClientData.maFmlaPict = maElementText;                                        break;
204         case VMLX_TOKEN( FmlaLink ):    mrClientData.maFmlaLink = maElementText;                                        break;
205         case VMLX_TOKEN( FmlaRange ):   mrClientData.maFmlaRange = maElementText;                                       break;
206         case VMLX_TOKEN( FmlaGroup ):   mrClientData.maFmlaGroup = maElementText;                                       break;
207         case VMLX_TOKEN( TextHAlign ):  mrClientData.mnTextHAlign = AttributeConversion::decodeToken( maElementText );  break;
208         case VMLX_TOKEN( TextVAlign ):  mrClientData.mnTextVAlign = AttributeConversion::decodeToken( maElementText );  break;
209         case VMLX_TOKEN( Column ):      mrClientData.mnCol = maElementText.toInt32();                                   break;
210         case VMLX_TOKEN( Row ):         mrClientData.mnRow = maElementText.toInt32();                                   break;
211         case VMLX_TOKEN( Checked ):     mrClientData.mnChecked = maElementText.toInt32();                               break;
212         case VMLX_TOKEN( DropStyle ):   mrClientData.mnDropStyle = AttributeConversion::decodeToken( maElementText );   break;
213         case VMLX_TOKEN( DropLines ):   mrClientData.mnDropLines = maElementText.toInt32();                             break;
214         case VMLX_TOKEN( Val ):         mrClientData.mnVal = maElementText.toInt32();                                   break;
215         case VMLX_TOKEN( Min ):         mrClientData.mnMin = maElementText.toInt32();                                   break;
216         case VMLX_TOKEN( Max ):         mrClientData.mnMax = maElementText.toInt32();                                   break;
217         case VMLX_TOKEN( Inc ):         mrClientData.mnInc = maElementText.toInt32();                                   break;
218         case VMLX_TOKEN( Page ):        mrClientData.mnPage = maElementText.toInt32();                                  break;
219         case VMLX_TOKEN( SelType ):     mrClientData.mnSelType = AttributeConversion::decodeToken( maElementText );     break;
220         case VMLX_TOKEN( VTEdit ):      mrClientData.mnVTEdit = maElementText.toInt32();                                break;
221         case VMLX_TOKEN( PrintObject ): mrClientData.mbPrintObject = lclDecodeVmlxBool( maElementText, true );          break;
222         case VMLX_TOKEN( Visible ):     mrClientData.mbVisible = lclDecodeVmlxBool( maElementText, true );              break;
223         case VMLX_TOKEN( DDE ):         mrClientData.mbDde = lclDecodeVmlxBool( maElementText, true );                  break;
224         case VMLX_TOKEN( NoThreeD ):    mrClientData.mbNo3D = lclDecodeVmlxBool( maElementText, true );                 break;
225         case VMLX_TOKEN( NoThreeD2 ):   mrClientData.mbNo3D2 = lclDecodeVmlxBool( maElementText, true );                break;
226         case VMLX_TOKEN( MultiLine ):   mrClientData.mbMultiLine = lclDecodeVmlxBool( maElementText, true );            break;
227         case VMLX_TOKEN( VScroll ):     mrClientData.mbVScroll = lclDecodeVmlxBool( maElementText, true );              break;
228         case VMLX_TOKEN( SecretEdit ):  mrClientData.mbSecretEdit = lclDecodeVmlxBool( maElementText, true );           break;
229     }
230 }
231 
232 // ============================================================================
233 
ShapeContextBase(ContextHandler2Helper & rParent)234 ShapeContextBase::ShapeContextBase( ContextHandler2Helper& rParent ) :
235     ContextHandler2( rParent )
236 {
237 }
238 
createShapeContext(ContextHandler2Helper & rParent,ShapeContainer & rShapes,sal_Int32 nElement,const AttributeList & rAttribs)239 /*static*/ ContextHandlerRef ShapeContextBase::createShapeContext( ContextHandler2Helper& rParent,
240         ShapeContainer& rShapes, sal_Int32 nElement, const AttributeList& rAttribs )
241 {
242     switch( nElement )
243     {
244         case O_TOKEN( shapelayout ):
245             return new ShapeLayoutContext( rParent, rShapes.getDrawing() );
246 
247         case VML_TOKEN( shapetype ):
248             return new ShapeTypeContext( rParent, rShapes.createShapeType(), rAttribs );
249         case VML_TOKEN( group ):
250             return new GroupShapeContext( rParent, rShapes.createShape< GroupShape >(), rAttribs );
251         case VML_TOKEN( shape ):
252             return new ShapeContext( rParent, rShapes.createShape< ComplexShape >(), rAttribs );
253         case VML_TOKEN( rect ):
254         case VML_TOKEN( roundrect ):
255             return new ShapeContext( rParent, rShapes.createShape< RectangleShape >(), rAttribs );
256         case VML_TOKEN( oval ):
257             return new ShapeContext( rParent, rShapes.createShape< EllipseShape >(), rAttribs );
258         case VML_TOKEN( polyline ):
259             return new ShapeContext( rParent, rShapes.createShape< PolyLineShape >(), rAttribs );
260 
261         // TODO:
262         case VML_TOKEN( arc ):
263         case VML_TOKEN( curve ):
264         case VML_TOKEN( line ):
265         case VML_TOKEN( diagram ):
266         case VML_TOKEN( image ):
267             return new ShapeContext( rParent, rShapes.createShape< ComplexShape >(), rAttribs );
268     }
269     return 0;
270 }
271 
272 // ============================================================================
273 
ShapeTypeContext(ContextHandler2Helper & rParent,ShapeType & rShapeType,const AttributeList & rAttribs)274 ShapeTypeContext::ShapeTypeContext( ContextHandler2Helper& rParent, ShapeType& rShapeType, const AttributeList& rAttribs ) :
275     ShapeContextBase( rParent ),
276     mrTypeModel( rShapeType.getTypeModel() )
277 {
278     // shape identifier and shape name
279     bool bHasOspid = rAttribs.hasAttribute( O_TOKEN( spid ) );
280     mrTypeModel.maShapeId = rAttribs.getXString( bHasOspid ? O_TOKEN( spid ) : XML_id, OUString() );
281     OSL_ENSURE( mrTypeModel.maShapeId.getLength() > 0, "ShapeTypeContext::ShapeTypeContext - missing shape identifier" );
282     // if the o:spid attribute exists, the id attribute contains the user-defined shape name
283     if( bHasOspid )
284         mrTypeModel.maShapeName = rAttribs.getXString( XML_id, OUString() );
285     // builtin shape type identifier
286     mrTypeModel.moShapeType = rAttribs.getInteger( O_TOKEN( spt ) );
287 
288     // coordinate system position/size, CSS style
289     mrTypeModel.moCoordPos = lclDecodeInt32Pair( rAttribs, XML_coordorigin );
290     mrTypeModel.moCoordSize = lclDecodeInt32Pair( rAttribs, XML_coordsize );
291     setStyle( rAttribs.getString( XML_style, OUString() ) );
292 
293     // stroke settings (may be overridden by v:stroke element later)
294     mrTypeModel.maStrokeModel.moStroked = lclDecodeBool( rAttribs, XML_stroked );
295     mrTypeModel.maStrokeModel.moColor = rAttribs.getString( XML_strokecolor );
296     mrTypeModel.maStrokeModel.moWeight = rAttribs.getString( XML_strokeweight );
297 
298     // fill settings (may be overridden by v:fill element later)
299     mrTypeModel.maFillModel.moFilled = lclDecodeBool( rAttribs, XML_filled );
300     mrTypeModel.maFillModel.moColor = rAttribs.getString( XML_fillcolor );
301 }
302 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)303 ContextHandlerRef ShapeTypeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
304 {
305     if( isRootElement() ) switch( nElement )
306     {
307         case VML_TOKEN( stroke ):
308             mrTypeModel.maStrokeModel.moStroked.assignIfUsed( lclDecodeBool( rAttribs, XML_on ) );
309             mrTypeModel.maStrokeModel.maStartArrow.moArrowType = rAttribs.getToken( XML_startarrow );
310             mrTypeModel.maStrokeModel.maStartArrow.moArrowWidth = rAttribs.getToken( XML_startarrowwidth );
311             mrTypeModel.maStrokeModel.maStartArrow.moArrowLength = rAttribs.getToken( XML_startarrowlength );
312             mrTypeModel.maStrokeModel.maEndArrow.moArrowType = rAttribs.getToken( XML_endarrow );
313             mrTypeModel.maStrokeModel.maEndArrow.moArrowWidth = rAttribs.getToken( XML_endarrowwidth );
314             mrTypeModel.maStrokeModel.maEndArrow.moArrowLength = rAttribs.getToken( XML_endarrowlength );
315             mrTypeModel.maStrokeModel.moColor.assignIfUsed( rAttribs.getString( XML_color ) );
316             mrTypeModel.maStrokeModel.moOpacity = lclDecodeOpacity( rAttribs, XML_opacity, 1.0 );
317             mrTypeModel.maStrokeModel.moWeight.assignIfUsed( rAttribs.getString( XML_weight ) );
318             mrTypeModel.maStrokeModel.moDashStyle = rAttribs.getString( XML_dashstyle );
319             mrTypeModel.maStrokeModel.moLineStyle = rAttribs.getToken( XML_linestyle );
320             mrTypeModel.maStrokeModel.moEndCap = rAttribs.getToken( XML_endcap );
321             mrTypeModel.maStrokeModel.moJoinStyle = rAttribs.getToken( XML_joinstyle );
322         break;
323         case VML_TOKEN( fill ):
324             mrTypeModel.maFillModel.moFilled.assignIfUsed( lclDecodeBool( rAttribs, XML_on ) );
325             mrTypeModel.maFillModel.moColor.assignIfUsed( rAttribs.getString( XML_color ) );
326             mrTypeModel.maFillModel.moOpacity = lclDecodeOpacity( rAttribs, XML_opacity, 1.0 );
327             mrTypeModel.maFillModel.moColor2 = rAttribs.getString( XML_color2 );
328             mrTypeModel.maFillModel.moOpacity2 = lclDecodeOpacity( rAttribs, XML_opacity2, 1.0 );
329             mrTypeModel.maFillModel.moType = rAttribs.getToken( XML_type );
330             mrTypeModel.maFillModel.moAngle = rAttribs.getInteger( XML_angle );
331             mrTypeModel.maFillModel.moFocus = lclDecodePercent( rAttribs, XML_focus, 0.0 );
332             mrTypeModel.maFillModel.moFocusPos = lclDecodePercentPair( rAttribs, XML_focusposition );
333             mrTypeModel.maFillModel.moFocusSize = lclDecodePercentPair( rAttribs, XML_focussize );
334             mrTypeModel.maFillModel.moBitmapPath = decodeFragmentPath( rAttribs, O_TOKEN( relid ) );
335             mrTypeModel.maFillModel.moRotate = lclDecodeBool( rAttribs, XML_rotate );
336         break;
337         case VML_TOKEN( imagedata ):
338             mrTypeModel.moGraphicPath = decodeFragmentPath( rAttribs, O_TOKEN( relid ) );
339             mrTypeModel.moGraphicTitle = rAttribs.getString( O_TOKEN( title ) );
340         break;
341     }
342     return 0;
343 }
344 
decodeFragmentPath(const AttributeList & rAttribs,sal_Int32 nToken) const345 OptValue< OUString > ShapeTypeContext::decodeFragmentPath( const AttributeList& rAttribs, sal_Int32 nToken ) const
346 {
347     OptValue< OUString > oFragmentPath;
348     OptValue< OUString > oRelId = rAttribs.getString( nToken );
349     if( oRelId.has() )
350         oFragmentPath = getFragmentPathFromRelId( oRelId.get() );
351     return oFragmentPath;
352 }
353 
setStyle(const OUString & rStyle)354 void ShapeTypeContext::setStyle( const OUString& rStyle )
355 {
356     sal_Int32 nIndex = 0;
357     while( nIndex >= 0 )
358     {
359         OUString aName, aValue;
360         if( ConversionHelper::separatePair( aName, aValue, rStyle.getToken( 0, ';', nIndex ), ':' ) )
361         {
362                  if( aName.equalsAscii( "position" ) )      mrTypeModel.maPosition = aValue;
363             else if( aName.equalsAscii( "left" ) )          mrTypeModel.maLeft = aValue;
364             else if( aName.equalsAscii( "top" ) )           mrTypeModel.maTop = aValue;
365             else if( aName.equalsAscii( "width" ) )         mrTypeModel.maWidth = aValue;
366             else if( aName.equalsAscii( "height" ) )        mrTypeModel.maHeight = aValue;
367             else if( aName.equalsAscii( "margin-left" ) )   mrTypeModel.maMarginLeft = aValue;
368             else if( aName.equalsAscii( "margin-top" ) )    mrTypeModel.maMarginTop = aValue;
369         }
370     }
371 }
372 
373 // ============================================================================
374 
ShapeContext(ContextHandler2Helper & rParent,ShapeBase & rShape,const AttributeList & rAttribs)375 ShapeContext::ShapeContext( ContextHandler2Helper& rParent, ShapeBase& rShape, const AttributeList& rAttribs ) :
376     ShapeTypeContext( rParent, rShape, rAttribs ),
377     mrShapeModel( rShape.getShapeModel() )
378 {
379     // collect shape specific attributes
380     mrShapeModel.maType = rAttribs.getXString( XML_type, OUString() );
381     // polyline path
382     setPoints( rAttribs.getString( XML_points, OUString() ) );
383 }
384 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)385 ContextHandlerRef ShapeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
386 {
387     // Excel specific shape client data
388     if( isRootElement() ) switch( nElement )
389     {
390         case VML_TOKEN( textbox ):
391             return new TextBoxContext( *this, mrShapeModel.createTextBox(), rAttribs );
392         case VMLX_TOKEN( ClientData ):
393             return new ClientDataContext( *this, mrShapeModel.createClientData(), rAttribs );
394     }
395     // handle remaining stuff in base class
396     return ShapeTypeContext::onCreateContext( nElement, rAttribs );
397 }
398 
setPoints(const OUString & rPoints)399 void ShapeContext::setPoints( const OUString& rPoints )
400 {
401     mrShapeModel.maPoints.clear();
402     sal_Int32 nIndex = 0;
403     while( nIndex >= 0 )
404     {
405         sal_Int32 nX = rPoints.getToken( 0, ',', nIndex ).toInt32();
406         sal_Int32 nY = rPoints.getToken( 0, ',', nIndex ).toInt32();
407         mrShapeModel.maPoints.push_back( Point( nX, nY ) );
408     }
409 }
410 
411 // ============================================================================
412 
GroupShapeContext(ContextHandler2Helper & rParent,GroupShape & rShape,const AttributeList & rAttribs)413 GroupShapeContext::GroupShapeContext( ContextHandler2Helper& rParent, GroupShape& rShape, const AttributeList& rAttribs ) :
414     ShapeContext( rParent, rShape, rAttribs ),
415     mrShapes( rShape.getChildren() )
416 {
417 }
418 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)419 ContextHandlerRef GroupShapeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
420 {
421     // try to create a context of an embedded shape
422     ContextHandlerRef xContext = createShapeContext( *this, mrShapes, nElement, rAttribs );
423     // handle remaining stuff of this shape in base class
424     return xContext.get() ? xContext : ShapeContext::onCreateContext( nElement, rAttribs );
425 }
426 
427 // ============================================================================
428 
429 } // namespace vml
430 } // namespace oox
431 
432