Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / oox / source / drawingml / chart / chartdrawingfragment.cxx
blobd5140c11607164a979e68f07c5882601c320014d
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 "drawingml/chart/chartdrawingfragment.hxx"
22 #include <osl/diagnose.h>
24 #include <basegfx/matrix/b2dhommatrix.hxx>
25 #include <com/sun/star/awt/Rectangle.hpp>
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 #include <oox/helper/attributelist.hxx>
32 #include <oox/token/namespaces.hxx>
33 #include <oox/token/tokens.hxx>
35 namespace oox {
36 namespace drawingml {
37 namespace chart {
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::drawing;
41 using namespace ::com::sun::star::uno;
42 using namespace ::oox::core;
44 ShapeAnchor::ShapeAnchor( bool bRelSize ) :
45 mbRelSize( bRelSize )
49 void ShapeAnchor::importExt( const AttributeList& rAttribs )
51 OSL_ENSURE( !mbRelSize, "ShapeAnchor::importExt - unexpected 'cdr:ext' element" );
52 maSize.Width = rAttribs.getHyper( XML_cx, 0 );
53 maSize.Height = rAttribs.getHyper( XML_cy, 0 );
56 void ShapeAnchor::setPos( sal_Int32 nElement, sal_Int32 nParentContext, const OUString& rValue )
58 AnchorPosModel* pAnchorPos = nullptr;
59 switch( nParentContext )
61 case CDR_TOKEN( from ):
62 pAnchorPos = &maFrom;
63 break;
64 case CDR_TOKEN( to ):
65 OSL_ENSURE( mbRelSize, "ShapeAnchor::setPos - unexpected 'cdr:to' element" );
66 pAnchorPos = &maTo;
67 break;
68 default:
69 OSL_FAIL( "ShapeAnchor::setPos - unexpected parent element" );
71 if( pAnchorPos ) switch( nElement )
73 case CDR_TOKEN( x ): pAnchorPos->mfX = rValue.toDouble(); break;
74 case CDR_TOKEN( y ): pAnchorPos->mfY = rValue.toDouble(); break;
75 default: OSL_FAIL( "ShapeAnchor::setPos - unexpected element" );
79 EmuRectangle ShapeAnchor::calcAnchorRectEmu( const EmuRectangle& rChartRect ) const
81 EmuRectangle aAnchorRect( -1, -1, -1, -1 );
83 OSL_ENSURE( maFrom.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid from position" );
84 OSL_ENSURE( mbRelSize ? maTo.isValid() : maSize.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid to/size" );
85 if( maFrom.isValid() && (mbRelSize ? maTo.isValid() : maSize.isValid()) )
87 // calculate shape position
88 aAnchorRect.X = static_cast< sal_Int64 >( maFrom.mfX * rChartRect.Width + 0.5 );
89 aAnchorRect.Y = static_cast< sal_Int64 >( maFrom.mfY * rChartRect.Height + 0.5 );
91 // calculate shape size
92 if( mbRelSize )
94 aAnchorRect.Width = static_cast< sal_Int64 >( maTo.mfX * rChartRect.Width + 0.5 ) - aAnchorRect.X;
95 if( aAnchorRect.Width < 0 )
97 aAnchorRect.X += aAnchorRect.Width;
98 aAnchorRect.Width *= -1;
100 aAnchorRect.Height = static_cast< sal_Int64 >( maTo.mfY * rChartRect.Height + 0.5 ) - aAnchorRect.Y;
101 if( aAnchorRect.Height < 0 )
103 aAnchorRect.Y += aAnchorRect.Height;
104 aAnchorRect.Height *= -1;
107 else
109 aAnchorRect.setSize( maSize );
113 return aAnchorRect;
116 ChartDrawingFragment::ChartDrawingFragment( XmlFilterBase& rFilter,
117 const OUString& rFragmentPath, const Reference< XShapes >& rxDrawPage,
118 const awt::Size& rChartSize, const awt::Point& rShapesOffset, bool bOleSupport ) :
119 FragmentHandler2( rFilter, rFragmentPath ),
120 mxDrawPage( rxDrawPage ),
121 mbOleSupport( bOleSupport )
123 maChartRectEmu.X = convertHmmToEmu( rShapesOffset.X );
124 maChartRectEmu.Y = convertHmmToEmu( rShapesOffset.Y );
125 maChartRectEmu.Width = convertHmmToEmu( rChartSize.Width );
126 maChartRectEmu.Height = convertHmmToEmu( rChartSize.Height );
129 ChartDrawingFragment::~ChartDrawingFragment()
133 ContextHandlerRef ChartDrawingFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
135 switch( getCurrentElement() )
137 case XML_ROOT_CONTEXT:
138 if( nElement == C_TOKEN( userShapes ) ) return this;
139 break;
141 case C_TOKEN( userShapes ):
142 switch( nElement )
144 case CDR_TOKEN( absSizeAnchor ):
145 mxAnchor.reset( new ShapeAnchor( false ) );
146 return this;
147 case CDR_TOKEN( relSizeAnchor ):
148 mxAnchor.reset( new ShapeAnchor( true ) );
149 return this;
151 break;
153 case CDR_TOKEN( absSizeAnchor ):
154 case CDR_TOKEN( relSizeAnchor ):
155 switch( nElement )
157 case CDR_TOKEN( sp ):
158 mxShape.reset( new Shape( "com.sun.star.drawing.CustomShape" ) );
159 return new ShapeContext( *this, ShapePtr(), mxShape );
160 case CDR_TOKEN( cxnSp ):
161 mxShape.reset( new Shape( "com.sun.star.drawing.ConnectorShape" ) );
162 return new ConnectorShapeContext( *this, ShapePtr(), mxShape );
163 case CDR_TOKEN( pic ):
164 mxShape.reset( new Shape( "com.sun.star.drawing.GraphicObjectShape" ) );
165 return new GraphicShapeContext( *this, ShapePtr(), mxShape );
166 case CDR_TOKEN( graphicFrame ):
167 if( !mbOleSupport )
168 return nullptr;
169 mxShape.reset( new Shape( "com.sun.star.drawing.GraphicObjectShape" ) );
170 return new GraphicalObjectFrameContext( *this, ShapePtr(), mxShape, true );
171 case CDR_TOKEN( grpSp ):
172 mxShape.reset( new Shape( "com.sun.star.drawing.GroupShape" ) );
173 return new ShapeGroupContext( *this, ShapePtr(), mxShape );
175 case CDR_TOKEN( from ):
176 case CDR_TOKEN( to ):
177 return this;
179 case CDR_TOKEN( ext ):
180 if( mxAnchor.get() ) mxAnchor->importExt( rAttribs );
181 return nullptr;
183 break;
185 case CDR_TOKEN( from ):
186 case CDR_TOKEN( to ):
187 switch( nElement )
189 case CDR_TOKEN( x ):
190 case CDR_TOKEN( y ):
191 return this; // collect value in onEndElement()
193 break;
195 return nullptr;
198 void ChartDrawingFragment::onCharacters( const OUString& rChars )
200 if( isCurrentElement( CDR_TOKEN( x ), CDR_TOKEN( y ) ) && mxAnchor.get() )
201 mxAnchor->setPos( getCurrentElement(), getParentElement(), rChars );
204 void ChartDrawingFragment::onEndElement()
206 if( isCurrentElement( CDR_TOKEN( absSizeAnchor ), CDR_TOKEN( relSizeAnchor ) ) )
208 if( mxDrawPage.is() && mxShape.get() && mxAnchor.get() )
210 EmuRectangle aShapeRectEmu = mxAnchor->calcAnchorRectEmu( maChartRectEmu );
211 if( (aShapeRectEmu.X >= 0) && (aShapeRectEmu.Y >= 0) && (aShapeRectEmu.Width >= 0) && (aShapeRectEmu.Height >= 0) )
213 // TODO: DrawingML implementation expects 32-bit coordinates for EMU rectangles (change that to EmuRectangle)
214 awt::Rectangle aShapeRectEmu32(
215 getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.X, 0, SAL_MAX_INT32 ),
216 getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.Y, 0, SAL_MAX_INT32 ),
217 getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.Width, 0, SAL_MAX_INT32 ),
218 getLimitedValue< sal_Int32, sal_Int64 >( aShapeRectEmu.Height, 0, SAL_MAX_INT32 ) );
220 // Set the position and size before calling addShape().
221 mxShape->setPosition(awt::Point(aShapeRectEmu32.X, aShapeRectEmu32.Y));
222 mxShape->setSize(awt::Size(aShapeRectEmu32.Width, aShapeRectEmu32.Height));
224 basegfx::B2DHomMatrix aMatrix;
225 mxShape->addShape( getFilter(), getFilter().getCurrentTheme(), mxDrawPage, aMatrix, mxShape->getFillProperties() );
228 mxShape.reset();
229 mxAnchor.reset();
233 } // namespace chart
234 } // namespace drawingml
235 } // namespace oox
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */