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
; }
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
);
82 bool isShowingMoreDebugInfo();
87 namespace com::sun::star::uno
{
90 inline ::std::size_t hash_value(
91 css::uno::Reference
<T
> const& x
)
93 // normalize to object root, because _only_ XInterface is defined
94 // to be stable during object lifetime:
95 css::uno::Reference
< css::uno::XInterface
> const xRoot( x
, css::uno::UNO_QUERY
);
96 return slideshow::internal::hash
<void *>()(xRoot
.get());
105 // Value extraction from Any
106 // =========================
108 /// extract unary double value from Any
109 bool extractValue( double& o_rValue
,
110 const css::uno::Any
& rSourceAny
,
111 const ShapeSharedPtr
& rShape
,
112 const basegfx::B2DVector
& rSlideBounds
);
114 /// extract int from Any
115 bool extractValue( sal_Int32
& o_rValue
,
116 const css::uno::Any
& rSourceAny
,
117 const ShapeSharedPtr
& rShape
,
118 const basegfx::B2DVector
& rSlideBounds
);
120 /// extract enum/constant group value from Any
121 bool extractValue( sal_Int16
& o_rValue
,
122 const css::uno::Any
& rSourceAny
,
123 const ShapeSharedPtr
& rShape
,
124 const basegfx::B2DVector
& rSlideBounds
);
126 /// extract color value from Any
127 bool extractValue( RGBColor
& o_rValue
,
128 const css::uno::Any
& rSourceAny
,
129 const ShapeSharedPtr
& rShape
,
130 const basegfx::B2DVector
& rSlideBounds
);
132 /// extract color value from Any
133 bool extractValue( HSLColor
& o_rValue
,
134 const css::uno::Any
& rSourceAny
,
135 const ShapeSharedPtr
& rShape
,
136 const basegfx::B2DVector
& rSlideBounds
);
138 /// extract plain string from Any
139 bool extractValue( OUString
& o_rValue
,
140 const css::uno::Any
& rSourceAny
,
141 const ShapeSharedPtr
& rShape
,
142 const basegfx::B2DVector
& rSlideBounds
);
144 /// extract bool value from Any
145 bool extractValue( bool& o_rValue
,
146 const css::uno::Any
& rSourceAny
,
147 const ShapeSharedPtr
& rShape
,
148 const basegfx::B2DVector
& rSlideBounds
);
150 /// extract double 2-tuple from Any
151 bool extractValue( basegfx::B2DTuple
& o_rPair
,
152 const css::uno::Any
& rSourceAny
,
153 const ShapeSharedPtr
& rShape
,
154 const basegfx::B2DVector
& rSlideBounds
);
156 /** Search a sequence of NamedValues for a given element.
158 @return true, if the sequence contains the specified
161 bool findNamedValue( css::uno::Sequence
< css::beans::NamedValue
> const& rSequence
,
162 const css::beans::NamedValue
& rSearchKey
);
164 basegfx::B2DRange
calcRelativeShapeBounds( const basegfx::B2DVector
& rPageSize
,
165 const basegfx::B2DRange
& rShapeBounds
);
167 /** Get the shape transformation from the attribute set
170 Original shape bound rect (to substitute default attribute
174 Attribute set. Might be NULL (then, rBounds is used to set
175 a simple scale and translate of the unit rect to rBounds).
177 basegfx::B2DHomMatrix
getShapeTransformation(
178 const basegfx::B2DRectangle
& rBounds
,
179 const ShapeAttributeLayerSharedPtr
& pAttr
);
181 /** Get a shape's sprite transformation from the attribute set
184 Pixel size of the sprite
187 Original shape size (i.e. the size of the actual sprite
188 content, in the user coordinate system)
191 Attribute set. Might be NULL (then, rBounds is used to set
192 a simple scale and translate of the unit rect to rBounds).
194 @return the transformation to be applied to the sprite.
196 basegfx::B2DHomMatrix
getSpriteTransformation(
197 const basegfx::B2DVector
& rPixelSize
,
198 const basegfx::B2DVector
& rOrigSize
,
199 const ShapeAttributeLayerSharedPtr
& pAttr
);
201 /** Calc update area for a shape.
203 This method calculates the 'covered' area for the shape,
204 i.e. the rectangle that is affected when rendering the
205 shape. Apart from applying the given transformation to the
206 shape rectangle, this method also takes attributes into
207 account, which further scale the output (e.g. character
211 Shape bounds, in the unit rect coordinate space
213 @param rShapeTransform
214 Transformation matrix the shape should undergo.
217 Current shape attributes
219 basegfx::B2DRectangle
getShapeUpdateArea(
220 const basegfx::B2DRectangle
& rUnitBounds
,
221 const basegfx::B2DHomMatrix
& rShapeTransform
,
222 const ShapeAttributeLayerSharedPtr
& pAttr
);
224 /** Calc update area for a shape.
226 This method calculates the 'covered' area for the shape,
227 i.e. the rectangle that is affected when rendering the
228 shape. The difference from the other getShapeUpdateArea()
229 method is the fact that this one works without
230 ShapeAttributeLayer, and only scales up the given shape
231 user coordinate bound rect. The method is typically used
232 to retrieve user coordinate system bound rects for shapes
233 which are smaller than the default unit bound rect
234 (because e.g. of subsetting)
237 Shape bounds, in the unit rect coordinate space
240 Current shape bounding box in user coordinate space.
242 basegfx::B2DRange
getShapeUpdateArea( const basegfx::B2DRange
& rUnitBounds
,
243 const basegfx::B2DRange
& rShapeBounds
);
245 /** Calc output position and size of shape, according to given
248 Rotations, shears etc. and not taken into account,
249 i.e. the returned rectangle is NOT the bounding box. Use
250 it as if aBounds.getMinimum() is the output position and
251 aBounds.getRange() the scaling of the shape.
253 basegfx::B2DRectangle
getShapePosSize(
254 const basegfx::B2DRectangle
& rOrigBounds
,
255 const ShapeAttributeLayerSharedPtr
& pAttr
);
257 /** Convert a plain UNO API 32 bit int to RGBColor
259 RGBColor
unoColor2RGBColor( sal_Int32
);
260 /** Convert an IntSRGBA to plain UNO API 32 bit int
262 sal_Int32
RGBAColor2UnoColor( cppcanvas::IntSRGBA
);
264 /** Fill a plain rectangle on the given canvas with the given color
266 void fillRect( const cppcanvas::CanvasSharedPtr
& rCanvas
,
267 const basegfx::B2DRectangle
& rRect
,
268 cppcanvas::IntSRGBA aFillColor
);
270 /** Init canvas with default background (white)
272 void initSlideBackground( const cppcanvas::CanvasSharedPtr
& rCanvas
,
273 const basegfx::B2ISize
& rSize
);
275 /// Gets a random ordinal [0,n)
276 inline ::std::size_t getRandomOrdinal( const ::std::size_t n
)
278 return comphelper::rng::uniform_size_distribution(0, n
-1);
281 template <typename ValueType
>
282 inline bool getPropertyValue(
285 css::beans::XPropertySet
> const & xPropSet
,
286 OUString
const & propName
)
289 const css::uno::Any
a(
290 xPropSet
->getPropertyValue( propName
) );
291 bool const bRet
= css::uno::fromAny(a
, &rValue
);
292 #if OSL_DEBUG_LEVEL > 0
294 SAL_INFO("slideshow", __func__
<< ": while retrieving property " << propName
<< ", cannot extract Any of type "
295 << a
.getValueTypeRef()->pTypeName
);
299 catch (css::uno::RuntimeException
&)
303 catch (css::uno::Exception
&)
309 template <typename ValueType
>
310 inline bool getPropertyValue(
311 css::uno::Reference
< ValueType
>& rIfc
,
312 css::uno::Reference
< css::beans::XPropertySet
> const & xPropSet
,
313 OUString
const & propName
)
317 const css::uno::Any
a(xPropSet
->getPropertyValue( propName
));
318 rIfc
.set( a
, css::uno::UNO_QUERY
);
320 bool const bRet
= rIfc
.is();
321 #if OSL_DEBUG_LEVEL > 0
323 SAL_INFO("slideshow", __func__
<< ": while retrieving property " << propName
<< ", cannot extract Any of type "
324 << a
.getValueTypeRef()->pTypeName
<< " to interface");
328 catch (css::uno::RuntimeException
&)
332 catch (css::uno::Exception
&)
338 /// Get the content of the BoundRect shape property
339 basegfx::B2DRectangle
getAPIShapeBounds( const css::uno::Reference
< css::drawing::XShape
>& xShape
);
342 TODO(F1): When ZOrder someday becomes usable enable this
344 /// Get the content of the ZOrder shape property
345 double getAPIShapePrio( const css::uno::Reference< css::drawing::XShape >& xShape );
348 basegfx::B2IVector
getSlideSizePixel( const basegfx::B2DVector
& rSize
,
349 const UnoViewSharedPtr
& pView
);
352 // TODO(Q1): this could possibly be implemented with a somewhat
353 // more lightweight template, by having the actual worker receive
354 // only a function pointer, and a thin templated wrapper around
355 // that which converts member functions into that.
357 /** Apply given functor to every animation node child.
363 Functor to apply. The functor must have an appropriate
364 operator()( const css::uno::Reference< css::animations::XAnimationNode >& ) member.
366 @return true, if the functor was successfully applied to
367 all children, false otherwise.
369 template< typename Functor
> inline bool for_each_childNode( const css::uno::Reference
< css::animations::XAnimationNode
>& xNode
,
374 // get an XEnumerationAccess to the children
375 css::uno::Reference
< css::container::XEnumerationAccess
>
376 xEnumerationAccess( xNode
,
377 css::uno::UNO_QUERY_THROW
);
378 css::uno::Reference
< css::container::XEnumeration
>
379 xEnumeration( xEnumerationAccess
->createEnumeration(),
380 css::uno::UNO_SET_THROW
);
382 while( xEnumeration
->hasMoreElements() )
384 css::uno::Reference
< css::animations::XAnimationNode
>
385 xChildNode( xEnumeration
->nextElement(),
386 css::uno::UNO_QUERY_THROW
);
387 rFunctor( xChildNode
);
391 catch( css::uno::Exception
& )
398 #endif // INCLUDED_SLIDESHOW_SOURCE_INC_TOOLS_HXX
400 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */