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/drawingml/chart/chartdrawingfragment.hxx" 25 26 #include "oox/core/xmlfilterbase.hxx" 27 #include "oox/drawingml/connectorshapecontext.hxx" 28 #include "oox/drawingml/graphicshapecontext.hxx" 29 #include "oox/drawingml/shapecontext.hxx" 30 #include "oox/drawingml/shapegroupcontext.hxx" 31 32 namespace oox { 33 namespace drawingml { 34 namespace chart { 35 36 // ============================================================================ 37 38 using namespace ::com::sun::star::awt; 39 using namespace ::com::sun::star::drawing; 40 using namespace ::com::sun::star::uno; 41 using namespace ::oox::core; 42 43 using ::rtl::OUString; 44 45 // ============================================================================ 46 47 ShapeAnchor::ShapeAnchor( bool bRelSize ) : 48 mbRelSize( bRelSize ) 49 { 50 } 51 52 void ShapeAnchor::importExt( const AttributeList& rAttribs ) 53 { 54 OSL_ENSURE( !mbRelSize, "ShapeAnchor::importExt - unexpected 'cdr:ext' element" ); 55 maSize.Width = rAttribs.getHyper( XML_cx, 0 ); 56 maSize.Height = rAttribs.getHyper( XML_cy, 0 ); 57 } 58 59 void ShapeAnchor::setPos( sal_Int32 nElement, sal_Int32 nParentContext, const OUString& rValue ) 60 { 61 AnchorPosModel* pAnchorPos = 0; 62 switch( nParentContext ) 63 { 64 case CDR_TOKEN( from ): 65 pAnchorPos = &maFrom; 66 break; 67 case CDR_TOKEN( to ): 68 OSL_ENSURE( mbRelSize, "ShapeAnchor::setPos - unexpected 'cdr:to' element" ); 69 pAnchorPos = &maTo; 70 break; 71 default: 72 OSL_ENSURE( false, "ShapeAnchor::setPos - unexpected parent element" ); 73 } 74 if( pAnchorPos ) switch( nElement ) 75 { 76 case CDR_TOKEN( x ): pAnchorPos->mfX = rValue.toDouble(); break; 77 case CDR_TOKEN( y ): pAnchorPos->mfY = rValue.toDouble(); break; 78 default: OSL_ENSURE( false, "ShapeAnchor::setPos - unexpected element" ); 79 } 80 } 81 82 EmuRectangle ShapeAnchor::calcAnchorRectEmu( const EmuRectangle& rChartRect ) const 83 { 84 EmuRectangle aAnchorRect( -1, -1, -1, -1 ); 85 86 OSL_ENSURE( maFrom.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid from position" ); 87 OSL_ENSURE( mbRelSize ? maTo.isValid() : maSize.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid to/size" ); 88 if( maFrom.isValid() && (mbRelSize ? maTo.isValid() : maSize.isValid()) ) 89 { 90 // calculate shape position 91 aAnchorRect.X = static_cast< sal_Int64 >( maFrom.mfX * rChartRect.Width + 0.5 ); 92 aAnchorRect.Y = static_cast< sal_Int64 >( maFrom.mfY * rChartRect.Height + 0.5 ); 93 94 // calculate shape size 95 if( mbRelSize ) 96 { 97 aAnchorRect.Width = static_cast< sal_Int64 >( maTo.mfX * rChartRect.Width + 0.5 ) - aAnchorRect.X; 98 if( aAnchorRect.Width < 0 ) 99 { 100 aAnchorRect.X += aAnchorRect.Width; 101 aAnchorRect.Width *= -1; 102 } 103 aAnchorRect.Height = static_cast< sal_Int64 >( maTo.mfY * rChartRect.Height + 0.5 ) - aAnchorRect.Y; 104 if( aAnchorRect.Height < 0 ) 105 { 106 aAnchorRect.Y += aAnchorRect.Height; 107 aAnchorRect.Height *= -1; 108 } 109 } 110 else 111 { 112 aAnchorRect.setSize( maSize ); 113 } 114 } 115 116 return aAnchorRect; 117 } 118 // ============================================================================ 119 120 ChartDrawingFragment::ChartDrawingFragment( XmlFilterBase& rFilter, 121 const OUString& rFragmentPath, const Reference< XShapes >& rxDrawPage, 122 const Size& rChartSize, const Point& rShapesOffset, bool bOleSupport ) : 123 FragmentHandler2( rFilter, rFragmentPath ), 124 mxDrawPage( rxDrawPage ), 125 mbOleSupport( bOleSupport ) 126 { 127 maChartRectEmu.X = convertHmmToEmu( rShapesOffset.X ); 128 maChartRectEmu.Y = convertHmmToEmu( rShapesOffset.Y ); 129 maChartRectEmu.Width = convertHmmToEmu( rChartSize.Width ); 130 maChartRectEmu.Height = convertHmmToEmu( rChartSize.Height ); 131 } 132 133 ChartDrawingFragment::~ChartDrawingFragment() 134 { 135 } 136 137 ContextHandlerRef ChartDrawingFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) 138 { 139 switch( getCurrentElement() ) 140 { 141 case XML_ROOT_CONTEXT: 142 if( nElement == C_TOKEN( userShapes ) ) return this; 143 break; 144 145 case C_TOKEN( userShapes ): 146 switch( nElement ) 147 { 148 case CDR_TOKEN( absSizeAnchor ): 149 mxAnchor.reset( new ShapeAnchor( false ) ); 150 return this; 151 case CDR_TOKEN( relSizeAnchor ): 152 mxAnchor.reset( new ShapeAnchor( true ) ); 153 return this; 154 } 155 break; 156 157 case CDR_TOKEN( absSizeAnchor ): 158 case CDR_TOKEN( relSizeAnchor ): 159 switch( nElement ) 160 { 161 case CDR_TOKEN( sp ): 162 mxShape.reset( new Shape( "com.sun.star.drawing.CustomShape" ) ); 163 return new ShapeContext( *this, ShapePtr(), mxShape ); 164 case CDR_TOKEN( cxnSp ): 165 mxShape.reset( new Shape( "com.sun.star.drawing.ConnectorShape" ) ); 166 return new ConnectorShapeContext( *this, ShapePtr(), mxShape ); 167 case CDR_TOKEN( pic ): 168 mxShape.reset( new Shape( "com.sun.star.drawing.GraphicObjectShape" ) ); 169 return new GraphicShapeContext( *this, ShapePtr(), mxShape ); 170 case CDR_TOKEN( graphicFrame ): 171 if( !mbOleSupport ) 172 return 0; 173 mxShape.reset( new Shape( "com.sun.star.drawing.GraphicObjectShape" ) ); 174 return new GraphicalObjectFrameContext( *this, ShapePtr(), mxShape, true ); 175 case CDR_TOKEN( grpSp ): 176 mxShape.reset( new Shape( "com.sun.star.drawing.GroupShape" ) ); 177 return new ShapeGroupContext( *this, ShapePtr(), mxShape ); 178 179 case CDR_TOKEN( from ): 180 case CDR_TOKEN( to ): 181 return this; 182 183 case CDR_TOKEN( ext ): 184 if( mxAnchor.get() ) mxAnchor->importExt( rAttribs ); 185 return 0; 186 } 187 break; 188 189 case CDR_TOKEN( from ): 190 case CDR_TOKEN( to ): 191 switch( nElement ) 192 { 193 case CDR_TOKEN( x ): 194 case CDR_TOKEN( y ): 195 return this; // collect value in onEndElement() 196 } 197 break; 198 } 199 return 0; 200 } 201 202 void ChartDrawingFragment::onCharacters( const OUString& rChars ) 203 { 204 if( isCurrentElement( CDR_TOKEN( x ), CDR_TOKEN( y ) ) && mxAnchor.get() ) 205 mxAnchor->setPos( getCurrentElement(), getParentElement(), rChars ); 206 } 207 208 void ChartDrawingFragment::onEndElement() 209 { 210 if( isCurrentElement( CDR_TOKEN( absSizeAnchor ), CDR_TOKEN( relSizeAnchor ) ) ) 211 { 212 if( mxDrawPage.is() && mxShape.get() && mxAnchor.get() ) 213 { 214 EmuRectangle aShapeRectEmu = mxAnchor->calcAnchorRectEmu( maChartRectEmu ); 215 if( (aShapeRectEmu.X >= 0) && (aShapeRectEmu.Y >= 0) && (aShapeRectEmu.Width >= 0) && (aShapeRectEmu.Height >= 0) ) 216 { 217 // TODO: DrawingML implementation expects 32-bit coordinates for EMU rectangles (change that to EmuRectangle) 218 Rectangle aShapeRectEmu32( 219 getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.X, 0, SAL_MAX_INT32 ), 220 getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.Y, 0, SAL_MAX_INT32 ), 221 getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.Width, 0, SAL_MAX_INT32 ), 222 getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.Height, 0, SAL_MAX_INT32 ) ); 223 mxShape->addShape( getFilter(), getFilter().getCurrentTheme(), mxDrawPage, &aShapeRectEmu32 ); 224 } 225 } 226 mxShape.reset(); 227 mxAnchor.reset(); 228 } 229 } 230 231 // ============================================================================ 232 233 } // namespace chart 234 } // namespace drawingml 235 } // namespace oox 236