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 "drawingml/chart/chartdrawingfragment.hxx"
22 #include <osl/diagnose.h>
24 #include "oox/core/xmlfilterbase.hxx"
25 #include "oox/drawingml/connectorshapecontext.hxx"
26 #include "oox/drawingml/graphicshapecontext.hxx"
27 #include "oox/drawingml/shapecontext.hxx"
28 #include "oox/drawingml/shapegroupcontext.hxx"
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 ShapeAnchor::ShapeAnchor( bool bRelSize
) :
44 void ShapeAnchor::importExt( const AttributeList
& rAttribs
)
46 OSL_ENSURE( !mbRelSize
, "ShapeAnchor::importExt - unexpected 'cdr:ext' element" );
47 maSize
.Width
= rAttribs
.getHyper( XML_cx
, 0 );
48 maSize
.Height
= rAttribs
.getHyper( XML_cy
, 0 );
51 void ShapeAnchor::setPos( sal_Int32 nElement
, sal_Int32 nParentContext
, const OUString
& rValue
)
53 AnchorPosModel
* pAnchorPos
= 0;
54 switch( nParentContext
)
56 case CDR_TOKEN( from
):
60 OSL_ENSURE( mbRelSize
, "ShapeAnchor::setPos - unexpected 'cdr:to' element" );
64 OSL_FAIL( "ShapeAnchor::setPos - unexpected parent element" );
66 if( pAnchorPos
) switch( nElement
)
68 case CDR_TOKEN( x
): pAnchorPos
->mfX
= rValue
.toDouble(); break;
69 case CDR_TOKEN( y
): pAnchorPos
->mfY
= rValue
.toDouble(); break;
70 default: OSL_FAIL( "ShapeAnchor::setPos - unexpected element" );
74 EmuRectangle
ShapeAnchor::calcAnchorRectEmu( const EmuRectangle
& rChartRect
) const
76 EmuRectangle
aAnchorRect( -1, -1, -1, -1 );
78 OSL_ENSURE( maFrom
.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid from position" );
79 OSL_ENSURE( mbRelSize
? maTo
.isValid() : maSize
.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid to/size" );
80 if( maFrom
.isValid() && (mbRelSize
? maTo
.isValid() : maSize
.isValid()) )
82 // calculate shape position
83 aAnchorRect
.X
= static_cast< sal_Int64
>( maFrom
.mfX
* rChartRect
.Width
+ 0.5 );
84 aAnchorRect
.Y
= static_cast< sal_Int64
>( maFrom
.mfY
* rChartRect
.Height
+ 0.5 );
86 // calculate shape size
89 aAnchorRect
.Width
= static_cast< sal_Int64
>( maTo
.mfX
* rChartRect
.Width
+ 0.5 ) - aAnchorRect
.X
;
90 if( aAnchorRect
.Width
< 0 )
92 aAnchorRect
.X
+= aAnchorRect
.Width
;
93 aAnchorRect
.Width
*= -1;
95 aAnchorRect
.Height
= static_cast< sal_Int64
>( maTo
.mfY
* rChartRect
.Height
+ 0.5 ) - aAnchorRect
.Y
;
96 if( aAnchorRect
.Height
< 0 )
98 aAnchorRect
.Y
+= aAnchorRect
.Height
;
99 aAnchorRect
.Height
*= -1;
104 aAnchorRect
.setSize( maSize
);
111 ChartDrawingFragment::ChartDrawingFragment( XmlFilterBase
& rFilter
,
112 const OUString
& rFragmentPath
, const Reference
< XShapes
>& rxDrawPage
,
113 const awt::Size
& rChartSize
, const awt::Point
& rShapesOffset
, bool bOleSupport
) :
114 FragmentHandler2( rFilter
, rFragmentPath
),
115 mxDrawPage( rxDrawPage
),
116 mbOleSupport( bOleSupport
)
118 maChartRectEmu
.X
= convertHmmToEmu( rShapesOffset
.X
);
119 maChartRectEmu
.Y
= convertHmmToEmu( rShapesOffset
.Y
);
120 maChartRectEmu
.Width
= convertHmmToEmu( rChartSize
.Width
);
121 maChartRectEmu
.Height
= convertHmmToEmu( rChartSize
.Height
);
124 ChartDrawingFragment::~ChartDrawingFragment()
128 ContextHandlerRef
ChartDrawingFragment::onCreateContext( sal_Int32 nElement
, const AttributeList
& rAttribs
)
130 switch( getCurrentElement() )
132 case XML_ROOT_CONTEXT
:
133 if( nElement
== C_TOKEN( userShapes
) ) return this;
136 case C_TOKEN( userShapes
):
139 case CDR_TOKEN( absSizeAnchor
):
140 mxAnchor
.reset( new ShapeAnchor( false ) );
142 case CDR_TOKEN( relSizeAnchor
):
143 mxAnchor
.reset( new ShapeAnchor( true ) );
148 case CDR_TOKEN( absSizeAnchor
):
149 case CDR_TOKEN( relSizeAnchor
):
152 case CDR_TOKEN( sp
):
153 mxShape
.reset( new Shape( "com.sun.star.drawing.CustomShape" ) );
154 return new ShapeContext( *this, ShapePtr(), mxShape
);
155 case CDR_TOKEN( cxnSp
):
156 mxShape
.reset( new Shape( "com.sun.star.drawing.ConnectorShape" ) );
157 return new ConnectorShapeContext( *this, ShapePtr(), mxShape
);
158 case CDR_TOKEN( pic
):
159 mxShape
.reset( new Shape( "com.sun.star.drawing.GraphicObjectShape" ) );
160 return new GraphicShapeContext( *this, ShapePtr(), mxShape
);
161 case CDR_TOKEN( graphicFrame
):
164 mxShape
.reset( new Shape( "com.sun.star.drawing.GraphicObjectShape" ) );
165 return new GraphicalObjectFrameContext( *this, ShapePtr(), mxShape
, true );
166 case CDR_TOKEN( grpSp
):
167 mxShape
.reset( new Shape( "com.sun.star.drawing.GroupShape" ) );
168 return new ShapeGroupContext( *this, ShapePtr(), mxShape
);
170 case CDR_TOKEN( from
):
171 case CDR_TOKEN( to
):
174 case CDR_TOKEN( ext
):
175 if( mxAnchor
.get() ) mxAnchor
->importExt( rAttribs
);
180 case CDR_TOKEN( from
):
181 case CDR_TOKEN( to
):
186 return this; // collect value in onEndElement()
193 void ChartDrawingFragment::onCharacters( const OUString
& rChars
)
195 if( isCurrentElement( CDR_TOKEN( x
), CDR_TOKEN( y
) ) && mxAnchor
.get() )
196 mxAnchor
->setPos( getCurrentElement(), getParentElement(), rChars
);
199 void ChartDrawingFragment::onEndElement()
201 if( isCurrentElement( CDR_TOKEN( absSizeAnchor
), CDR_TOKEN( relSizeAnchor
) ) )
203 if( mxDrawPage
.is() && mxShape
.get() && mxAnchor
.get() )
205 EmuRectangle aShapeRectEmu
= mxAnchor
->calcAnchorRectEmu( maChartRectEmu
);
206 if( (aShapeRectEmu
.X
>= 0) && (aShapeRectEmu
.Y
>= 0) && (aShapeRectEmu
.Width
>= 0) && (aShapeRectEmu
.Height
>= 0) )
208 // TODO: DrawingML implementation expects 32-bit coordinates for EMU rectangles (change that to EmuRectangle)
209 awt::Rectangle
aShapeRectEmu32(
210 getLimitedValue
< sal_Int32
, sal_Int64
>( aShapeRectEmu
.X
, 0, SAL_MAX_INT32
),
211 getLimitedValue
< sal_Int32
, sal_Int64
>( aShapeRectEmu
.Y
, 0, SAL_MAX_INT32
),
212 getLimitedValue
< sal_Int32
, sal_Int64
>( aShapeRectEmu
.Width
, 0, SAL_MAX_INT32
),
213 getLimitedValue
< sal_Int32
, sal_Int64
>( aShapeRectEmu
.Height
, 0, SAL_MAX_INT32
) );
214 basegfx::B2DHomMatrix aMatrix
;
215 mxShape
->addShape( getFilter(), getFilter().getCurrentTheme(), mxDrawPage
, aMatrix
, mxShape
->getFillProperties(), &aShapeRectEmu32
);
224 } // namespace drawingml
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */