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 #ifndef INCLUDED_SLIDESHOW_SOURCE_INC_TOOLS_HXX
21 #define INCLUDED_SLIDESHOW_SOURCE_INC_TOOLS_HXX
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/uno/Reference.hxx>
26 #include <com/sun/star/animations/XAnimationNode.hpp>
27 #include <com/sun/star/container/XEnumerationAccess.hpp>
28 #include <com/sun/star/container/XEnumeration.hpp>
29 #include <comphelper/random.hxx>
30 #include <sal/log.hxx>
32 #include <cppcanvas/color.hxx>
34 #include "shapeattributelayer.hxx"
36 #include "rgbcolor.hxx"
37 #include "hslcolor.hxx"
41 namespace com::sun::star::beans
{ struct NamedValue
; }
48 typedef B2IVector B2ISize
;
50 namespace cppcanvas
{ class Canvas
; }
54 /* Definition of some animation tools */
61 class ShapeAttributeLayer
;
62 typedef std::shared_ptr
< UnoView
> UnoViewSharedPtr
;
63 typedef std::shared_ptr
< GDIMetaFile
> GDIMetaFileSharedPtr
;
66 inline ::std::size_t hash_value( T
const * p
)
68 ::std::size_t d
= static_cast< ::std::size_t >(
69 reinterpret_cast< ::std::ptrdiff_t >(p
) );
76 ::std::size_t operator()( T
const& val
) const {
77 return hash_value(val
);
83 namespace com::sun::star::uno
{
86 inline ::std::size_t hash_value(
87 css::uno::Reference
<T
> const& x
)
89 // normalize to object root, because _only_ XInterface is defined
90 // to be stable during object lifetime:
91 css::uno::Reference
< css::uno::XInterface
> const xRoot( x
, css::uno::UNO_QUERY
);
92 return slideshow::internal::hash
<void *>()(xRoot
.get());
101 // Value extraction from Any
102 // =========================
104 /// extract unary double value from Any
105 bool extractValue( double& o_rValue
,
106 const css::uno::Any
& rSourceAny
,
107 const ShapeSharedPtr
& rShape
,
108 const basegfx::B2DVector
& rSlideBounds
);
110 /// extract int from Any
111 bool extractValue( sal_Int32
& o_rValue
,
112 const css::uno::Any
& rSourceAny
,
113 const ShapeSharedPtr
& rShape
,
114 const basegfx::B2DVector
& rSlideBounds
);
116 /// extract enum/constant group value from Any
117 bool extractValue( sal_Int16
& o_rValue
,
118 const css::uno::Any
& rSourceAny
,
119 const ShapeSharedPtr
& rShape
,
120 const basegfx::B2DVector
& rSlideBounds
);
122 /// extract color value from Any
123 bool extractValue( RGBColor
& o_rValue
,
124 const css::uno::Any
& rSourceAny
,
125 const ShapeSharedPtr
& rShape
,
126 const basegfx::B2DVector
& rSlideBounds
);
128 /// extract color value from Any
129 bool extractValue( HSLColor
& o_rValue
,
130 const css::uno::Any
& rSourceAny
,
131 const ShapeSharedPtr
& rShape
,
132 const basegfx::B2DVector
& rSlideBounds
);
134 /// extract plain string from Any
135 bool extractValue( OUString
& o_rValue
,
136 const css::uno::Any
& rSourceAny
,
137 const ShapeSharedPtr
& rShape
,
138 const basegfx::B2DVector
& rSlideBounds
);
140 /// extract bool value from Any
141 bool extractValue( bool& o_rValue
,
142 const css::uno::Any
& rSourceAny
,
143 const ShapeSharedPtr
& rShape
,
144 const basegfx::B2DVector
& rSlideBounds
);
146 /// extract double 2-tuple from Any
147 bool extractValue( basegfx::B2DTuple
& o_rPair
,
148 const css::uno::Any
& rSourceAny
,
149 const ShapeSharedPtr
& rShape
,
150 const basegfx::B2DVector
& rSlideBounds
);
152 /** Search a sequence of NamedValues for a given element.
154 @return true, if the sequence contains the specified
157 bool findNamedValue( css::uno::Sequence
< css::beans::NamedValue
> const& rSequence
,
158 const css::beans::NamedValue
& rSearchKey
);
160 basegfx::B2DRange
calcRelativeShapeBounds( const basegfx::B2DVector
& rPageSize
,
161 const basegfx::B2DRange
& rShapeBounds
);
163 /** Get the shape transformation from the attribute set
166 Original shape bound rect (to substitute default attribute
170 Attribute set. Might be NULL (then, rBounds is used to set
171 a simple scale and translate of the unit rect to rBounds).
173 basegfx::B2DHomMatrix
getShapeTransformation(
174 const basegfx::B2DRectangle
& rBounds
,
175 const ShapeAttributeLayerSharedPtr
& pAttr
);
177 /** Get a shape's sprite transformation from the attribute set
180 Pixel size of the sprite
183 Original shape size (i.e. the size of the actual sprite
184 content, in the user coordinate system)
187 Attribute set. Might be NULL (then, rBounds is used to set
188 a simple scale and translate of the unit rect to rBounds).
190 @return the transformation to be applied to the sprite.
192 basegfx::B2DHomMatrix
getSpriteTransformation(
193 const basegfx::B2DVector
& rPixelSize
,
194 const basegfx::B2DVector
& rOrigSize
,
195 const ShapeAttributeLayerSharedPtr
& pAttr
);
197 /** Calc update area for a shape.
199 This method calculates the 'covered' area for the shape,
200 i.e. the rectangle that is affected when rendering the
201 shape. Apart from applying the given transformation to the
202 shape rectangle, this method also takes attributes into
203 account, which further scale the output (e.g. character
207 Shape bounds, in the unit rect coordinate space
209 @param rShapeTransform
210 Transformation matrix the shape should undergo.
213 Current shape attributes
215 basegfx::B2DRectangle
getShapeUpdateArea(
216 const basegfx::B2DRectangle
& rUnitBounds
,
217 const basegfx::B2DHomMatrix
& rShapeTransform
,
218 const ShapeAttributeLayerSharedPtr
& pAttr
);
220 /** Calc update area for a shape.
222 This method calculates the 'covered' area for the shape,
223 i.e. the rectangle that is affected when rendering the
224 shape. The difference from the other getShapeUpdateArea()
225 method is the fact that this one works without
226 ShapeAttributeLayer, and only scales up the given shape
227 user coordinate bound rect. The method is typically used
228 to retrieve user coordinate system bound rects for shapes
229 which are smaller than the default unit bound rect
230 (because e.g. of subsetting)
233 Shape bounds, in the unit rect coordinate space
236 Current shape bounding box in user coordinate space.
238 basegfx::B2DRange
getShapeUpdateArea( const basegfx::B2DRange
& rUnitBounds
,
239 const basegfx::B2DRange
& rShapeBounds
);
241 /** Calc output position and size of shape, according to given
244 Rotations, shears etc. and not taken into account,
245 i.e. the returned rectangle is NOT the bounding box. Use
246 it as if aBounds.getMinimum() is the output position and
247 aBounds.getRange() the scaling of the shape.
249 basegfx::B2DRectangle
getShapePosSize(
250 const basegfx::B2DRectangle
& rOrigBounds
,
251 const ShapeAttributeLayerSharedPtr
& pAttr
);
253 /** Convert a plain UNO API 32 bit int to RGBColor
255 RGBColor
unoColor2RGBColor( sal_Int32
);
256 /** Convert an IntSRGBA to plain UNO API 32 bit int
258 sal_Int32
RGBAColor2UnoColor( cppcanvas::IntSRGBA
);
260 /** Fill a plain rectangle on the given canvas with the given color
262 void fillRect( const cppcanvas::CanvasSharedPtr
& rCanvas
,
263 const basegfx::B2DRectangle
& rRect
,
264 cppcanvas::IntSRGBA aFillColor
);
266 /** Init canvas with default background (white)
268 void initSlideBackground( const cppcanvas::CanvasSharedPtr
& rCanvas
,
269 const basegfx::B2ISize
& rSize
);
271 /// Gets a random ordinal [0,n)
272 inline ::std::size_t getRandomOrdinal( const ::std::size_t n
)
274 return comphelper::rng::uniform_size_distribution(0, n
-1);
277 template <typename ValueType
>
278 inline bool getPropertyValue(
281 css::beans::XPropertySet
> const & xPropSet
,
282 OUString
const & propName
)
285 const css::uno::Any
& a(
286 xPropSet
->getPropertyValue( propName
) );
287 bool const bRet
= css::uno::fromAny(a
, &rValue
);
288 #if OSL_DEBUG_LEVEL > 0
290 SAL_INFO("slideshow", __func__
<< ": while retrieving property " << propName
<< ", cannot extract Any of type "
291 << a
.getValueTypeRef()->pTypeName
);
295 catch (css::uno::RuntimeException
&)
299 catch (css::uno::Exception
&)
305 template <typename ValueType
>
306 inline bool getPropertyValue(
307 css::uno::Reference
< ValueType
>& rIfc
,
308 css::uno::Reference
< css::beans::XPropertySet
> const & xPropSet
,
309 OUString
const & propName
)
313 const css::uno::Any
& a(xPropSet
->getPropertyValue( propName
));
314 rIfc
.set( a
, css::uno::UNO_QUERY
);
316 bool const bRet
= rIfc
.is();
317 #if OSL_DEBUG_LEVEL > 0
319 SAL_INFO("slideshow", __func__
<< ": while retrieving property " << propName
<< ", cannot extract Any of type "
320 << a
.getValueTypeRef()->pTypeName
<< " to interface");
324 catch (css::uno::RuntimeException
&)
328 catch (css::uno::Exception
&)
334 /// Get the content of the BoundRect shape property
335 basegfx::B2DRectangle
getAPIShapeBounds( const css::uno::Reference
< css::drawing::XShape
>& xShape
);
338 TODO(F1): When ZOrder someday becomes usable enable this
340 /// Get the content of the ZOrder shape property
341 double getAPIShapePrio( const css::uno::Reference< css::drawing::XShape >& xShape );
344 basegfx::B2IVector
getSlideSizePixel( const basegfx::B2DVector
& rSize
,
345 const UnoViewSharedPtr
& pView
);
348 // TODO(Q1): this could possibly be implemented with a somewhat
349 // more lightweight template, by having the actual worker receive
350 // only a function pointer, and a thin templated wrapper around
351 // that which converts member functions into that.
353 /** Apply given functor to every animation node child.
359 Functor to apply. The functor must have an appropriate
360 operator()( const css::uno::Reference< css::animations::XAnimationNode >& ) member.
362 @return true, if the functor was successfully applied to
363 all children, false otherwise.
365 template< typename Functor
> inline bool for_each_childNode( const css::uno::Reference
< css::animations::XAnimationNode
>& xNode
,
370 // get an XEnumerationAccess to the children
371 css::uno::Reference
< css::container::XEnumerationAccess
>
372 xEnumerationAccess( xNode
,
373 css::uno::UNO_QUERY_THROW
);
374 css::uno::Reference
< css::container::XEnumeration
>
375 xEnumeration( xEnumerationAccess
->createEnumeration(),
376 css::uno::UNO_SET_THROW
);
378 while( xEnumeration
->hasMoreElements() )
380 css::uno::Reference
< css::animations::XAnimationNode
>
381 xChildNode( xEnumeration
->nextElement(),
382 css::uno::UNO_QUERY_THROW
);
383 rFunctor( xChildNode
);
387 catch( css::uno::Exception
& )
394 #endif // INCLUDED_SLIDESHOW_SOURCE_INC_TOOLS_HXX
396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */