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 <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>
34 #include <o3tl/string_view.hxx>
36 namespace oox::drawingml::chart
{
38 using namespace ::com::sun::star
;
39 using namespace ::com::sun::star::drawing
;
40 using namespace ::com::sun::star::uno
;
41 using namespace ::oox::core
;
43 ShapeAnchor::ShapeAnchor( bool bRelSize
) :
48 void ShapeAnchor::importExt( const AttributeList
& rAttribs
)
50 OSL_ENSURE( !mbRelSize
, "ShapeAnchor::importExt - unexpected 'cdr:ext' element" );
51 maSize
.Width
= rAttribs
.getHyper( XML_cx
, 0 );
52 maSize
.Height
= rAttribs
.getHyper( XML_cy
, 0 );
55 void ShapeAnchor::setPos( sal_Int32 nElement
, sal_Int32 nParentContext
, std::u16string_view rValue
)
57 AnchorPosModel
* pAnchorPos
= nullptr;
58 switch( nParentContext
)
60 case CDR_TOKEN( from
):
64 OSL_ENSURE( mbRelSize
, "ShapeAnchor::setPos - unexpected 'cdr:to' element" );
68 OSL_FAIL( "ShapeAnchor::setPos - unexpected parent element" );
70 if( pAnchorPos
) switch( nElement
)
72 case CDR_TOKEN( x
): pAnchorPos
->mfX
= o3tl::toDouble(rValue
); break;
73 case CDR_TOKEN( y
): pAnchorPos
->mfY
= o3tl::toDouble(rValue
); break;
74 default: OSL_FAIL( "ShapeAnchor::setPos - unexpected element" );
78 EmuRectangle
ShapeAnchor::calcAnchorRectEmu( const EmuRectangle
& rChartRect
) const
80 EmuRectangle
aAnchorRect( -1, -1, -1, -1 );
82 OSL_ENSURE( maFrom
.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid from position" );
83 OSL_ENSURE( mbRelSize
? maTo
.isValid() : maSize
.isValid(), "ShapeAnchor::calcAnchorRectEmu - invalid to/size" );
84 if( maFrom
.isValid() && (mbRelSize
? maTo
.isValid() : maSize
.isValid()) )
86 // calculate shape position
87 aAnchorRect
.X
= static_cast< sal_Int64
>( maFrom
.mfX
* rChartRect
.Width
+ 0.5 );
88 aAnchorRect
.Y
= static_cast< sal_Int64
>( maFrom
.mfY
* rChartRect
.Height
+ 0.5 );
90 // calculate shape size
93 aAnchorRect
.Width
= static_cast< sal_Int64
>( maTo
.mfX
* rChartRect
.Width
+ 0.5 ) - aAnchorRect
.X
;
94 if( aAnchorRect
.Width
< 0 )
96 aAnchorRect
.X
+= aAnchorRect
.Width
;
97 aAnchorRect
.Width
*= -1;
99 aAnchorRect
.Height
= static_cast< sal_Int64
>( maTo
.mfY
* rChartRect
.Height
+ 0.5 ) - aAnchorRect
.Y
;
100 if( aAnchorRect
.Height
< 0 )
102 aAnchorRect
.Y
+= aAnchorRect
.Height
;
103 aAnchorRect
.Height
*= -1;
108 aAnchorRect
.setSize( maSize
);
115 ChartDrawingFragment::ChartDrawingFragment( XmlFilterBase
& rFilter
,
116 const OUString
& rFragmentPath
, const Reference
< XShapes
>& rxDrawPage
,
117 const awt::Size
& rChartSize
, const awt::Point
& rShapesOffset
, bool bOleSupport
) :
118 FragmentHandler2( rFilter
, rFragmentPath
),
119 mxDrawPage( rxDrawPage
),
120 mbOleSupport( bOleSupport
)
122 maChartRectEmu
.X
= convertHmmToEmu( rShapesOffset
.X
);
123 maChartRectEmu
.Y
= convertHmmToEmu( rShapesOffset
.Y
);
124 maChartRectEmu
.Width
= convertHmmToEmu( rChartSize
.Width
);
125 maChartRectEmu
.Height
= convertHmmToEmu( rChartSize
.Height
);
128 ChartDrawingFragment::~ChartDrawingFragment()
132 ContextHandlerRef
ChartDrawingFragment::onCreateContext( sal_Int32 nElement
, const AttributeList
& rAttribs
)
134 switch( getCurrentElement() )
136 case XML_ROOT_CONTEXT
:
137 if( nElement
== C_TOKEN( userShapes
) ) return this;
140 case C_TOKEN( userShapes
):
143 case CDR_TOKEN( absSizeAnchor
):
144 mxAnchor
= std::make_shared
<ShapeAnchor
>( false );
146 case CDR_TOKEN( relSizeAnchor
):
147 mxAnchor
= std::make_shared
<ShapeAnchor
>( true );
152 case CDR_TOKEN( absSizeAnchor
):
153 case CDR_TOKEN( relSizeAnchor
):
156 case CDR_TOKEN( sp
):
157 mxShape
= std::make_shared
<Shape
>( "com.sun.star.drawing.CustomShape" );
158 return new ShapeContext( *this, ShapePtr(), mxShape
);
159 case CDR_TOKEN( cxnSp
):
160 mxShape
= std::make_shared
<Shape
>( "com.sun.star.drawing.ConnectorShape" );
161 return new ConnectorShapeContext(*this, ShapePtr(), mxShape
,
162 mxShape
->getConnectorShapeProperties());
163 case CDR_TOKEN( pic
):
164 mxShape
= std::make_shared
<Shape
>( "com.sun.star.drawing.GraphicObjectShape" );
165 return new GraphicShapeContext( *this, ShapePtr(), mxShape
);
166 case CDR_TOKEN( graphicFrame
):
169 mxShape
= std::make_shared
<Shape
>( "com.sun.star.drawing.GraphicObjectShape" );
170 return new GraphicalObjectFrameContext( *this, ShapePtr(), mxShape
, true );
171 case CDR_TOKEN( grpSp
):
172 mxShape
= std::make_shared
<Shape
>( "com.sun.star.drawing.GroupShape" );
173 return new ShapeGroupContext( *this, ShapePtr(), mxShape
);
175 case CDR_TOKEN( from
):
176 case CDR_TOKEN( to
):
179 case CDR_TOKEN( ext
):
180 if( mxAnchor
) mxAnchor
->importExt( rAttribs
);
185 case CDR_TOKEN( from
):
186 case CDR_TOKEN( to
):
191 return this; // collect value in onEndElement()
198 void ChartDrawingFragment::onCharacters( const OUString
& rChars
)
200 if( isCurrentElement( CDR_TOKEN( x
), CDR_TOKEN( y
) ) && mxAnchor
)
201 mxAnchor
->setPos( getCurrentElement(), getParentElement(), rChars
);
204 void ChartDrawingFragment::onEndElement()
206 if( !isCurrentElement( CDR_TOKEN( absSizeAnchor
), CDR_TOKEN( relSizeAnchor
) ) )
209 if( mxDrawPage
.is() && mxShape
&& mxAnchor
)
211 EmuRectangle aShapeRectEmu
= mxAnchor
->calcAnchorRectEmu( maChartRectEmu
);
212 if( (aShapeRectEmu
.X
>= 0) && (aShapeRectEmu
.Y
>= 0) && (aShapeRectEmu
.Width
>= 0) && (aShapeRectEmu
.Height
>= 0) )
214 const sal_Int32 aRotation
= mxShape
->getRotation();
215 if( (aRotation
>= 45 * PER_DEGREE
&& aRotation
< 135 * PER_DEGREE
) || (aRotation
>= 225 * PER_DEGREE
&& aRotation
< 315 * PER_DEGREE
) )
217 sal_Int64 nHalfWidth
= aShapeRectEmu
.Width
/ 2;
218 sal_Int64 nHalfHeight
= aShapeRectEmu
.Height
/ 2;
219 aShapeRectEmu
.X
= aShapeRectEmu
.X
+ nHalfWidth
- nHalfHeight
;
220 aShapeRectEmu
.Y
= aShapeRectEmu
.Y
+ nHalfHeight
- nHalfWidth
;
221 std::swap(aShapeRectEmu
.Width
, aShapeRectEmu
.Height
);
223 // TODO: DrawingML implementation expects 32-bit coordinates for EMU rectangles (change that to EmuRectangle)
224 awt::Rectangle
aShapeRectEmu32(
225 getLimitedValue
< sal_Int32
, sal_Int64
>( aShapeRectEmu
.X
, 0, SAL_MAX_INT32
),
226 getLimitedValue
< sal_Int32
, sal_Int64
>( aShapeRectEmu
.Y
, 0, SAL_MAX_INT32
),
227 getLimitedValue
< sal_Int32
, sal_Int64
>( aShapeRectEmu
.Width
, 0, SAL_MAX_INT32
),
228 getLimitedValue
< sal_Int32
, sal_Int64
>( aShapeRectEmu
.Height
, 0, SAL_MAX_INT32
) );
230 // Set the position and size before calling addShape().
231 mxShape
->setPosition(awt::Point(aShapeRectEmu32
.X
, aShapeRectEmu32
.Y
));
232 mxShape
->setSize(awt::Size(aShapeRectEmu32
.Width
, aShapeRectEmu32
.Height
));
234 basegfx::B2DHomMatrix aMatrix
;
235 mxShape
->addShape( getFilter(), getFilter().getCurrentTheme(), mxDrawPage
, aMatrix
, mxShape
->getFillProperties() );
242 } // namespace oox::drawingml::chart
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */