1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "oox/drawingml/chart/chartdrawingfragment.hxx"
22 #include "oox/core/xmlfilterbase.hxx"
23 #include "oox/drawingml/connectorshapecontext.hxx"
24 #include "oox/drawingml/graphicshapecontext.hxx"
25 #include "oox/drawingml/shapecontext.hxx"
26 #include "oox/drawingml/shapegroupcontext.hxx"
32 // ============================================================================
34 using namespace ::com::sun::star
;
35 using namespace ::com::sun::star::drawing
;
36 using namespace ::com::sun::star::uno
;
37 using namespace ::oox::core
;
39 // ============================================================================
41 ShapeAnchor::ShapeAnchor( bool bRelSize
) :
46 void ShapeAnchor::importExt( const AttributeList
& rAttribs
)
48 OSL_ENSURE( !mbRelSize
, "ShapeAnchor::importExt - unexpected 'cdr:ext' element" );
49 maSize
.Width
= rAttribs
.getHyper( XML_cx
, 0 );
50 maSize
.Height
= rAttribs
.getHyper( XML_cy
, 0 );
53 void ShapeAnchor::setPos( sal_Int32 nElement
, sal_Int32 nParentContext
, const OUString
& rValue
)
55 AnchorPosModel
* pAnchorPos
= 0;
56 switch( nParentContext
)
58 case CDR_TOKEN( from
):
62 OSL_ENSURE( mbRelSize
, "ShapeAnchor::setPos - unexpected 'cdr:to' element" );
66 OSL_FAIL( "ShapeAnchor::setPos - unexpected parent element" );
68 if( pAnchorPos
) switch( nElement
)
70 case CDR_TOKEN( x
): pAnchorPos
->mfX
= rValue
.toDouble(); break;
71 case CDR_TOKEN( y
): pAnchorPos
->mfY
= rValue
.toDouble(); break;
72 default: OSL_FAIL( "ShapeAnchor::setPos - unexpected element" );
76 EmuRectangle
ShapeAnchor::calcAnchorRectEmu( const EmuRectangle
& rChartRect
) const
78 EmuRectangle
aAnchorRect( -1, -1, -1, -1 );
80 OSL_ENSURE( maFrom
.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid from position" );
81 OSL_ENSURE( mbRelSize
? maTo
.isValid() : maSize
.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid to/size" );
82 if( maFrom
.isValid() && (mbRelSize
? maTo
.isValid() : maSize
.isValid()) )
84 // calculate shape position
85 aAnchorRect
.X
= static_cast< sal_Int64
>( maFrom
.mfX
* rChartRect
.Width
+ 0.5 );
86 aAnchorRect
.Y
= static_cast< sal_Int64
>( maFrom
.mfY
* rChartRect
.Height
+ 0.5 );
88 // calculate shape size
91 aAnchorRect
.Width
= static_cast< sal_Int64
>( maTo
.mfX
* rChartRect
.Width
+ 0.5 ) - aAnchorRect
.X
;
92 if( aAnchorRect
.Width
< 0 )
94 aAnchorRect
.X
+= aAnchorRect
.Width
;
95 aAnchorRect
.Width
*= -1;
97 aAnchorRect
.Height
= static_cast< sal_Int64
>( maTo
.mfY
* rChartRect
.Height
+ 0.5 ) - aAnchorRect
.Y
;
98 if( aAnchorRect
.Height
< 0 )
100 aAnchorRect
.Y
+= aAnchorRect
.Height
;
101 aAnchorRect
.Height
*= -1;
106 aAnchorRect
.setSize( maSize
);
112 // ============================================================================
114 ChartDrawingFragment::ChartDrawingFragment( XmlFilterBase
& rFilter
,
115 const OUString
& rFragmentPath
, const Reference
< XShapes
>& rxDrawPage
,
116 const awt::Size
& rChartSize
, const awt::Point
& rShapesOffset
, bool bOleSupport
) :
117 FragmentHandler2( rFilter
, rFragmentPath
),
118 mxDrawPage( rxDrawPage
),
119 mbOleSupport( bOleSupport
)
121 maChartRectEmu
.X
= convertHmmToEmu( rShapesOffset
.X
);
122 maChartRectEmu
.Y
= convertHmmToEmu( rShapesOffset
.Y
);
123 maChartRectEmu
.Width
= convertHmmToEmu( rChartSize
.Width
);
124 maChartRectEmu
.Height
= convertHmmToEmu( rChartSize
.Height
);
127 ChartDrawingFragment::~ChartDrawingFragment()
131 ContextHandlerRef
ChartDrawingFragment::onCreateContext( sal_Int32 nElement
, const AttributeList
& rAttribs
)
133 switch( getCurrentElement() )
135 case XML_ROOT_CONTEXT
:
136 if( nElement
== C_TOKEN( userShapes
) ) return this;
139 case C_TOKEN( userShapes
):
142 case CDR_TOKEN( absSizeAnchor
):
143 mxAnchor
.reset( new ShapeAnchor( false ) );
145 case CDR_TOKEN( relSizeAnchor
):
146 mxAnchor
.reset( new ShapeAnchor( true ) );
151 case CDR_TOKEN( absSizeAnchor
):
152 case CDR_TOKEN( relSizeAnchor
):
155 case CDR_TOKEN( sp
):
156 mxShape
.reset( new Shape( "com.sun.star.drawing.CustomShape" ) );
157 return new ShapeContext( *this, ShapePtr(), mxShape
);
158 case CDR_TOKEN( cxnSp
):
159 mxShape
.reset( new Shape( "com.sun.star.drawing.ConnectorShape" ) );
160 return new ConnectorShapeContext( *this, ShapePtr(), mxShape
);
161 case CDR_TOKEN( pic
):
162 mxShape
.reset( new Shape( "com.sun.star.drawing.GraphicObjectShape" ) );
163 return new GraphicShapeContext( *this, ShapePtr(), mxShape
);
164 case CDR_TOKEN( graphicFrame
):
167 mxShape
.reset( new Shape( "com.sun.star.drawing.GraphicObjectShape" ) );
168 return new GraphicalObjectFrameContext( *this, ShapePtr(), mxShape
, true );
169 case CDR_TOKEN( grpSp
):
170 mxShape
.reset( new Shape( "com.sun.star.drawing.GroupShape" ) );
171 return new ShapeGroupContext( *this, ShapePtr(), mxShape
);
173 case CDR_TOKEN( from
):
174 case CDR_TOKEN( to
):
177 case CDR_TOKEN( ext
):
178 if( mxAnchor
.get() ) mxAnchor
->importExt( rAttribs
);
183 case CDR_TOKEN( from
):
184 case CDR_TOKEN( to
):
189 return this; // collect value in onEndElement()
196 void ChartDrawingFragment::onCharacters( const OUString
& rChars
)
198 if( isCurrentElement( CDR_TOKEN( x
), CDR_TOKEN( y
) ) && mxAnchor
.get() )
199 mxAnchor
->setPos( getCurrentElement(), getParentElement(), rChars
);
202 void ChartDrawingFragment::onEndElement()
204 if( isCurrentElement( CDR_TOKEN( absSizeAnchor
), CDR_TOKEN( relSizeAnchor
) ) )
206 if( mxDrawPage
.is() && mxShape
.get() && mxAnchor
.get() )
208 EmuRectangle aShapeRectEmu
= mxAnchor
->calcAnchorRectEmu( maChartRectEmu
);
209 if( (aShapeRectEmu
.X
>= 0) && (aShapeRectEmu
.Y
>= 0) && (aShapeRectEmu
.Width
>= 0) && (aShapeRectEmu
.Height
>= 0) )
211 // TODO: DrawingML implementation expects 32-bit coordinates for EMU rectangles (change that to EmuRectangle)
212 awt::Rectangle
aShapeRectEmu32(
213 getLimitedValue
< sal_Int32
, sal_Int64
>( aShapeRectEmu
.X
, 0, SAL_MAX_INT32
),
214 getLimitedValue
< sal_Int32
, sal_Int64
>( aShapeRectEmu
.Y
, 0, SAL_MAX_INT32
),
215 getLimitedValue
< sal_Int32
, sal_Int64
>( aShapeRectEmu
.Width
, 0, SAL_MAX_INT32
),
216 getLimitedValue
< sal_Int32
, sal_Int64
>( aShapeRectEmu
.Height
, 0, SAL_MAX_INT32
) );
217 basegfx::B2DHomMatrix aMatrix
;
218 mxShape
->addShape( getFilter(), getFilter().getCurrentTheme(), mxDrawPage
, aMatrix
, mxShape
->getFillProperties(), &aShapeRectEmu32
);
226 // ============================================================================
229 } // namespace drawingml
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */