Update ooo320-m1
[ooovba.git] / oox / source / vml / vmlshapecontext.cxx
blob725af97777c399eeef5f1afdb884c165368352cd
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vmldrawingfragmenthandler.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "oox/vml/vmlshapecontext.hxx"
32 #include "oox/vml/vmlshape.hxx"
33 #include "oox/vml/vmlshapecontainer.hxx"
35 using ::rtl::OUString;
36 using ::com::sun::star::awt::Point;
37 using ::oox::core::ContextHandler2;
38 using ::oox::core::ContextHandler2Helper;
39 using ::oox::core::ContextHandlerRef;
41 namespace oox {
42 namespace vml {
44 // ============================================================================
46 namespace {
48 /** Returns the boolean value from the specified VML attribute (if present).
50 OptValue< bool > lclDecodeBool( const AttributeList& rAttribs, sal_Int32 nElement )
52 OptValue< OUString > oValue = rAttribs.getString( nElement );
53 if( oValue.has() ) return OptValue< bool >( ConversionHelper::decodeBool( oValue.get() ) );
54 return OptValue< bool >();
57 /** Returns the percentage value from the specified VML attribute (if present).
58 The value will be normalized (1.0 is returned for 100%).
60 OptValue< double > lclDecodePercent( const AttributeList& rAttribs, sal_Int32 nElement, double fDefValue )
62 OptValue< OUString > oValue = rAttribs.getString( nElement );
63 if( oValue.has() ) return OptValue< double >( ConversionHelper::decodePercent( oValue.get(), fDefValue ) );
64 return OptValue< double >();
67 /** Returns the integer value pair from the specified VML attribute (if present).
69 OptValue< Int32Pair > lclDecodeInt32Pair( const AttributeList& rAttribs, sal_Int32 nElement )
71 OptValue< OUString > oValue = rAttribs.getString( nElement );
72 OptValue< Int32Pair > oRetValue;
73 if( oValue.has() )
75 OUString aValue1, aValue2;
76 ConversionHelper::separatePair( aValue1, aValue2, oValue.get(), ',' );
77 oRetValue = Int32Pair( aValue1.toInt32(), aValue2.toInt32() );
79 return oRetValue;
82 /** Returns the percentage pair from the specified VML attribute (if present).
84 OptValue< DoublePair > lclDecodePercentPair( const AttributeList& rAttribs, sal_Int32 nElement )
86 OptValue< OUString > oValue = rAttribs.getString( nElement );
87 OptValue< DoublePair > oRetValue;
88 if( oValue.has() )
90 OUString aValue1, aValue2;
91 ConversionHelper::separatePair( aValue1, aValue2, oValue.get(), ',' );
92 oRetValue = DoublePair(
93 ConversionHelper::decodePercent( aValue1, 0.0 ),
94 ConversionHelper::decodePercent( aValue2, 0.0 ) );
96 return oRetValue;
99 /** Returns the boolean value from the passed string of an attribute in the x:
100 namespace (VML for spreadsheets). Supported values: f, t, False, True.
101 @param bDefaultForEmpty Default value for the empty string.
103 bool lclDecodeVmlxBool( const OUString& rValue, bool bDefaultForEmpty )
105 if( rValue.getLength() == 0 ) return bDefaultForEmpty;
106 // anything else than 't' or 'True' is considered to be false, as specified
107 return ((rValue.getLength() == 1) && (rValue[ 0 ] == 't')) || (rValue == CREATE_OUSTRING( "True" ));
110 } // namespace
112 // ============================================================================
114 ShapeClientDataContext::ShapeClientDataContext( ContextHandler2Helper& rParent,
115 const AttributeList& rAttribs, ShapeClientData& rClientData ) :
116 ContextHandler2( rParent ),
117 mrClientData( rClientData )
119 mrClientData.mnObjType = rAttribs.getToken( XML_ObjectType, XML_TOKEN_INVALID );
122 ContextHandlerRef ShapeClientDataContext::onCreateContext( sal_Int32 /*nElement*/, const AttributeList& /*rAttribs*/ )
124 return isRootElement() ? this : 0;
127 void ShapeClientDataContext::onEndElement( const OUString& rChars )
129 switch( getCurrentElement() )
131 case VMLX_TOKEN( Anchor ): mrClientData.maAnchor = rChars; break;
132 case VMLX_TOKEN( FmlaPict ): mrClientData.maPictureLink = rChars; break;
133 case VMLX_TOKEN( FmlaLink ): mrClientData.maLinkedCell = rChars; break;
134 case VMLX_TOKEN( FmlaRange ): mrClientData.maSourceRange = rChars; break;
135 case VMLX_TOKEN( Column ): mrClientData.mnCol = rChars.toInt32(); break;
136 case VMLX_TOKEN( Row ): mrClientData.mnRow = rChars.toInt32(); break;
137 case VMLX_TOKEN( PrintObject ): mrClientData.mbPrintObject = lclDecodeVmlxBool( rChars, true ); break;
138 case VMLX_TOKEN( Visible ): mrClientData.mbVisible = true; break;
142 // ============================================================================
144 ShapeContextBase::ShapeContextBase( ContextHandler2Helper& rParent ) :
145 ContextHandler2( rParent )
149 /*static*/ ContextHandlerRef ShapeContextBase::createShapeContext( ContextHandler2Helper& rParent,
150 sal_Int32 nElement, const AttributeList& rAttribs, ShapeContainer& rShapes )
152 switch( nElement )
154 case VML_TOKEN( shapetype ):
155 return new ShapeTypeContext( rParent, rAttribs, rShapes.createShapeType() );
156 case VML_TOKEN( group ):
157 return new GroupShapeContext( rParent, rAttribs, rShapes.createShape< GroupShape >() );
158 case VML_TOKEN( shape ):
159 return new ShapeContext( rParent, rAttribs, rShapes.createShape< ComplexShape >() );
160 case VML_TOKEN( rect ):
161 case VML_TOKEN( roundrect ):
162 return new ShapeContext( rParent, rAttribs, rShapes.createShape< RectangleShape >() );
163 case VML_TOKEN( oval ):
164 return new ShapeContext( rParent, rAttribs, rShapes.createShape< EllipseShape >() );
165 case VML_TOKEN( polyline ):
166 return new ShapeContext( rParent, rAttribs, rShapes.createShape< PolyLineShape >() );
168 // TODO:
169 case VML_TOKEN( arc ):
170 case VML_TOKEN( curve ):
171 case VML_TOKEN( line ):
172 case VML_TOKEN( diagram ):
173 case VML_TOKEN( image ):
174 return new ShapeContext( rParent, rAttribs, rShapes.createShape< ComplexShape >() );
176 return false;
179 // ============================================================================
181 ShapeTypeContext::ShapeTypeContext( ContextHandler2Helper& rParent, const AttributeList& rAttribs, ShapeType& rShapeType ) :
182 ShapeContextBase( rParent ),
183 mrTypeModel( rShapeType.getTypeModel() )
185 // shape identifier and shape name
186 bool bHasOspid = rAttribs.hasAttribute( O_TOKEN( spid ) );
187 mrTypeModel.maShapeId = rAttribs.getXString( bHasOspid ? O_TOKEN( spid ) : XML_id, OUString() );
188 OSL_ENSURE( mrTypeModel.maShapeId.getLength() > 0, "ShapeTypeContext::ShapeTypeContext - missing shape identifier" );
189 // if the o:spid attribute exists, the id attribute contains the user-defined shape name
190 if( bHasOspid )
191 mrTypeModel.maName = rAttribs.getXString( XML_id, OUString() );
192 // builtin shape type identifier
193 mrTypeModel.moShapeType = rAttribs.getInteger( O_TOKEN( spt ) );
195 // coordinate system position/size, CSS style
196 mrTypeModel.moCoordPos = lclDecodeInt32Pair( rAttribs, XML_coordorigin );
197 mrTypeModel.moCoordSize = lclDecodeInt32Pair( rAttribs, XML_coordsize );
198 setStyle( rAttribs.getString( XML_style, OUString() ) );
200 // stroke settings (may be overridden by v:stroke element later)
201 mrTypeModel.maStrokeModel.moStroked = lclDecodeBool( rAttribs, XML_stroked );
202 mrTypeModel.maStrokeModel.moColor = rAttribs.getString( XML_strokecolor );
203 mrTypeModel.maStrokeModel.moWeight = rAttribs.getString( XML_strokeweight );
205 // fill settings (may be overridden by v:fill element later)
206 mrTypeModel.maFillModel.moFilled = lclDecodeBool( rAttribs, XML_filled );
207 mrTypeModel.maFillModel.moColor = rAttribs.getString( XML_fillcolor );
210 ContextHandlerRef ShapeTypeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
212 if( isRootElement() ) switch( nElement )
214 case VML_TOKEN( stroke ):
215 mrTypeModel.maStrokeModel.moStroked.assignIfUsed( lclDecodeBool( rAttribs, XML_on ) );
216 mrTypeModel.maStrokeModel.maStartArrow.moArrowType = rAttribs.getToken( XML_startarrow );
217 mrTypeModel.maStrokeModel.maStartArrow.moArrowWidth = rAttribs.getToken( XML_startarrowwidth );
218 mrTypeModel.maStrokeModel.maStartArrow.moArrowLength = rAttribs.getToken( XML_startarrowlength );
219 mrTypeModel.maStrokeModel.maEndArrow.moArrowType = rAttribs.getToken( XML_endarrow );
220 mrTypeModel.maStrokeModel.maEndArrow.moArrowWidth = rAttribs.getToken( XML_endarrowwidth );
221 mrTypeModel.maStrokeModel.maEndArrow.moArrowLength = rAttribs.getToken( XML_endarrowlength );
222 mrTypeModel.maStrokeModel.moColor.assignIfUsed( rAttribs.getString( XML_color ) );
223 mrTypeModel.maStrokeModel.moOpacity = lclDecodePercent( rAttribs, XML_opacity, 1.0 );
224 mrTypeModel.maStrokeModel.moWeight.assignIfUsed( rAttribs.getString( XML_weight ) );
225 mrTypeModel.maStrokeModel.moDashStyle = rAttribs.getString( XML_dashstyle );
226 mrTypeModel.maStrokeModel.moLineStyle = rAttribs.getToken( XML_linestyle );
227 mrTypeModel.maStrokeModel.moEndCap = rAttribs.getToken( XML_endcap );
228 mrTypeModel.maStrokeModel.moJoinStyle = rAttribs.getToken( XML_joinstyle );
229 break;
230 case VML_TOKEN( fill ):
231 mrTypeModel.maFillModel.moFilled.assignIfUsed( lclDecodeBool( rAttribs, XML_on ) );
232 mrTypeModel.maFillModel.moColor.assignIfUsed( rAttribs.getString( XML_color ) );
233 mrTypeModel.maFillModel.moOpacity = lclDecodePercent( rAttribs, XML_opacity, 1.0 );
234 mrTypeModel.maFillModel.moColor2 = rAttribs.getString( XML_color2 );
235 mrTypeModel.maFillModel.moOpacity2 = lclDecodePercent( rAttribs, XML_opacity2, 1.0 );
236 mrTypeModel.maFillModel.moType = rAttribs.getToken( XML_type );
237 mrTypeModel.maFillModel.moAngle = rAttribs.getInteger( XML_angle );
238 mrTypeModel.maFillModel.moFocus = lclDecodePercent( rAttribs, XML_focus, 0.0 );
239 mrTypeModel.maFillModel.moFocusPos = lclDecodePercentPair( rAttribs, XML_focusposition );
240 mrTypeModel.maFillModel.moFocusSize = lclDecodePercentPair( rAttribs, XML_focussize );
241 mrTypeModel.maFillModel.moRotate = lclDecodeBool( rAttribs, XML_rotate );
242 break;
243 case VML_TOKEN( imagedata ):
244 OptValue< OUString > oGraphicRelId = rAttribs.getString( O_TOKEN( relid ) );
245 if( oGraphicRelId.has() )
246 mrTypeModel.moGraphicPath = getFragmentPathFromRelId( oGraphicRelId.get() );
247 mrTypeModel.moGraphicTitle = rAttribs.getString( O_TOKEN( title ) );
248 break;
250 return 0;
253 void ShapeTypeContext::setStyle( const OUString& rStyle )
255 sal_Int32 nIndex = 0;
256 while( nIndex >= 0 )
258 OUString aName, aValue;
259 if( ConversionHelper::separatePair( aName, aValue, rStyle.getToken( 0, ';', nIndex ), ':' ) )
261 if( aName.equalsAscii( "position" ) ) mrTypeModel.maPosition = aValue;
262 else if( aName.equalsAscii( "left" ) ) mrTypeModel.maLeft = aValue;
263 else if( aName.equalsAscii( "top" ) ) mrTypeModel.maTop = aValue;
264 else if( aName.equalsAscii( "width" ) ) mrTypeModel.maWidth = aValue;
265 else if( aName.equalsAscii( "height" ) ) mrTypeModel.maHeight = aValue;
266 else if( aName.equalsAscii( "margin-left" ) ) mrTypeModel.maMarginLeft = aValue;
267 else if( aName.equalsAscii( "margin-top" ) ) mrTypeModel.maMarginTop = aValue;
272 // ============================================================================
274 ShapeContext::ShapeContext( ContextHandler2Helper& rParent, const AttributeList& rAttribs, ShapeBase& rShape ) :
275 ShapeTypeContext( rParent, rAttribs, rShape ),
276 mrShapeModel( rShape.getShapeModel() )
278 // collect shape specific attributes
279 mrShapeModel.maType = rAttribs.getXString( XML_type, OUString() );
280 // polyline path
281 setPoints( rAttribs.getString( XML_points, OUString() ) );
284 ContextHandlerRef ShapeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
286 // Excel specific shape client data
287 if( isRootElement() && (nElement == VMLX_TOKEN( ClientData )) )
288 return new ShapeClientDataContext( *this, rAttribs, mrShapeModel.createClientData() );
289 // handle remaining stuff in base class
290 return ShapeTypeContext::onCreateContext( nElement, rAttribs );
293 void ShapeContext::setPoints( const OUString& rPoints )
295 mrShapeModel.maPoints.clear();
296 sal_Int32 nIndex = 0;
297 while( nIndex >= 0 )
299 sal_Int32 nX = rPoints.getToken( 0, ',', nIndex ).toInt32();
300 sal_Int32 nY = rPoints.getToken( 0, ',', nIndex ).toInt32();
301 mrShapeModel.maPoints.push_back( Point( nX, nY ) );
305 // ============================================================================
307 GroupShapeContext::GroupShapeContext( ContextHandler2Helper& rParent, const AttributeList& rAttribs, GroupShape& rShape ) :
308 ShapeContext( rParent, rAttribs, rShape ),
309 mrShapes( rShape.getChildren() )
313 ContextHandlerRef GroupShapeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
315 // try to create a context of an embedded shape
316 ContextHandlerRef xContext = createShapeContext( *this, nElement, rAttribs, mrShapes );
317 // handle remaining stuff of this shape in base class
318 return xContext.get() ? xContext : ShapeContext::onCreateContext( nElement, rAttribs );
321 // ============================================================================
323 } // namespace vml
324 } // namespace oox