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 <osl/diagnose.h>
33 #include <cppcanvas/color.hxx>
35 #include "shapeattributelayer.hxx"
37 #include "rgbcolor.hxx"
38 #include "hslcolor.hxx"
45 namespace com
{ namespace sun
{ namespace star
{ namespace beans
{
55 namespace cppcanvas
{ class Canvas
; }
59 /* Definition of some animation tools */
66 class ShapeAttributeLayer
;
68 typedef ::std::shared_ptr
< GDIMetaFile
> GDIMetaFileSharedPtr
;
71 inline ::std::size_t hash_value( T
const * p
)
73 ::std::size_t d
= static_cast< ::std::size_t >(
74 reinterpret_cast< ::std::ptrdiff_t >(p
) );
78 // xxx todo: remove with boost::hash when 1.33 is available
82 ::std::size_t operator()( T
const& val
) const {
83 return hash_value(val
);
89 namespace com
{ namespace sun
{ namespace star
{ namespace uno
{
92 inline ::std::size_t hash_value(
93 css::uno::Reference
<T
> const& x
)
95 // normalize to object root, because _only_ XInterface is defined
96 // to be stable during object lifetime:
97 css::uno::Reference
< css::uno::XInterface
> const xRoot( x
, css::uno::UNO_QUERY
);
98 return slideshow::internal::hash
<void *>()(xRoot
.get());
107 // Value extraction from Any
108 // =========================
110 /// extract unary double value from Any
111 bool extractValue( double& o_rValue
,
112 const css::uno::Any
& rSourceAny
,
113 const std::shared_ptr
<Shape
>& rShape
,
114 const basegfx::B2DVector
& rSlideBounds
);
116 /// extract int from Any
117 bool extractValue( sal_Int32
& o_rValue
,
118 const css::uno::Any
& rSourceAny
,
119 const std::shared_ptr
<Shape
>& rShape
,
120 const basegfx::B2DVector
& rSlideBounds
);
122 /// extract enum/constant group value from Any
123 bool extractValue( sal_Int16
& o_rValue
,
124 const css::uno::Any
& rSourceAny
,
125 const std::shared_ptr
<Shape
>& rShape
,
126 const basegfx::B2DVector
& rSlideBounds
);
128 /// extract color value from Any
129 bool extractValue( RGBColor
& o_rValue
,
130 const css::uno::Any
& rSourceAny
,
131 const std::shared_ptr
<Shape
>& rShape
,
132 const basegfx::B2DVector
& rSlideBounds
);
134 /// extract color value from Any
135 bool extractValue( HSLColor
& o_rValue
,
136 const css::uno::Any
& rSourceAny
,
137 const std::shared_ptr
<Shape
>& rShape
,
138 const basegfx::B2DVector
& rSlideBounds
);
140 /// extract plain string from Any
141 bool extractValue( OUString
& o_rValue
,
142 const css::uno::Any
& rSourceAny
,
143 const std::shared_ptr
<Shape
>& rShape
,
144 const basegfx::B2DVector
& rSlideBounds
);
146 /// extract bool value from Any
147 bool extractValue( bool& o_rValue
,
148 const css::uno::Any
& rSourceAny
,
149 const std::shared_ptr
<Shape
>& rShape
,
150 const basegfx::B2DVector
& rSlideBounds
);
152 /// extract double 2-tuple from Any
153 bool extractValue( basegfx::B2DTuple
& o_rPair
,
154 const css::uno::Any
& rSourceAny
,
155 const std::shared_ptr
<Shape
>& rShape
,
156 const basegfx::B2DVector
& rSlideBounds
);
158 /** Search a sequence of NamedValues for a given element.
160 @return true, if the sequence contains the specified
163 bool findNamedValue( css::uno::Sequence
< css::beans::NamedValue
> const& rSequence
,
164 const css::beans::NamedValue
& rSearchKey
);
166 basegfx::B2DRange
calcRelativeShapeBounds( const basegfx::B2DVector
& rPageSize
,
167 const basegfx::B2DRange
& rShapeBounds
);
169 /** Get the shape transformation from the attribute set
172 Original shape bound rect (to substitute default attribute
176 Attribute set. Might be NULL (then, rBounds is used to set
177 a simple scale and translate of the unit rect to rBounds).
179 basegfx::B2DHomMatrix
getShapeTransformation(
180 const basegfx::B2DRange
& rBounds
,
181 const std::shared_ptr
<ShapeAttributeLayer
>& pAttr
);
183 /** Get a shape's sprite transformation from the attribute set
186 Pixel size of the sprite
189 Original shape size (i.e. the size of the actual sprite
190 content, in the user coordinate system)
193 Attribute set. Might be NULL (then, rBounds is used to set
194 a simple scale and translate of the unit rect to rBounds).
196 @return the transformation to be applied to the sprite.
198 basegfx::B2DHomMatrix
getSpriteTransformation(
199 const basegfx::B2DVector
& rPixelSize
,
200 const basegfx::B2DVector
& rOrigSize
,
201 const std::shared_ptr
<ShapeAttributeLayer
>& pAttr
);
203 /** Calc update area for a shape.
205 This method calculates the 'covered' area for the shape,
206 i.e. the rectangle that is affected when rendering the
207 shape. Apart from applying the given transformation to the
208 shape rectangle, this method also takes attributes into
209 account, which further scale the output (e.g. character
213 Shape bounds, in the unit rect coordinate space
215 @param rShapeTransform
216 Transformation matrix the shape should undergo.
219 Current shape attributes
221 basegfx::B2DRange
getShapeUpdateArea(
222 const basegfx::B2DRange
& rUnitBounds
,
223 const basegfx::B2DHomMatrix
& rShapeTransform
,
224 const std::shared_ptr
<ShapeAttributeLayer
>& pAttr
);
226 /** Calc update area for a shape.
228 This method calculates the 'covered' area for the shape,
229 i.e. the rectangle that is affected when rendering the
230 shape. The difference from the other getShapeUpdateArea()
231 method is the fact that this one works without
232 ShapeAttributeLayer, and only scales up the given shape
233 user coordinate bound rect. The method is typically used
234 to retrieve user coordinate system bound rects for shapes
235 which are smaller than the default unit bound rect
236 (because e.g. of subsetting)
239 Shape bounds, in the unit rect coordinate space
242 Current shape bounding box in user coordinate space.
244 basegfx::B2DRange
getShapeUpdateArea( const basegfx::B2DRange
& rUnitBounds
,
245 const basegfx::B2DRange
& rShapeBounds
);
247 /** Calc output position and size of shape, according to given
250 Rotations, shears etc. and not taken into account,
251 i.e. the returned rectangle is NOT the bounding box. Use
252 it as if aBounds.getMinimum() is the output position and
253 aBounds.getRange() the scaling of the shape.
255 basegfx::B2DRange
getShapePosSize(
256 const basegfx::B2DRange
& rOrigBounds
,
257 const std::shared_ptr
<ShapeAttributeLayer
>& pAttr
);
259 /** Convert a plain UNO API 32 bit int to RGBColor
261 RGBColor
unoColor2RGBColor( sal_Int32
);
262 /** Convert an IntSRGBA to plain UNO API 32 bit int
264 sal_Int32
RGBAColor2UnoColor( cppcanvas::IntSRGBA
);
266 /** Fill a plain rectangle on the given canvas with the given color
268 void fillRect( const std::shared_ptr
< cppcanvas::Canvas
>& rCanvas
,
269 const basegfx::B2DRange
& rRect
,
270 cppcanvas::IntSRGBA aFillColor
);
272 /** Init canvas with default background (white)
274 void initSlideBackground( const std::shared_ptr
< cppcanvas::Canvas
>& rCanvas
,
275 const basegfx::B2IVector
& rSize
);
277 /// Gets a random ordinal [0,n)
278 inline ::std::size_t getRandomOrdinal( const ::std::size_t n
)
280 return comphelper::rng::uniform_size_distribution(0, n
-1);
283 template <typename ValueType
>
284 inline bool getPropertyValue(
287 css::beans::XPropertySet
> const & xPropSet
,
288 OUString
const & propName
)
291 const css::uno::Any
& a(
292 xPropSet
->getPropertyValue( propName
) );
293 bool const bRet
= css::uno::fromAny(a
, &rValue
);
294 #if OSL_DEBUG_LEVEL > 0
296 SAL_INFO("slideshow", OSL_THIS_FUNC
<< ": while retrieving property " << propName
<< ", cannot extract Any of type "
297 << a
.getValueTypeRef()->pTypeName
);
301 catch (css::uno::RuntimeException
&)
305 catch (css::uno::Exception
&)
311 template <typename ValueType
>
312 inline bool getPropertyValue(
313 css::uno::Reference
< ValueType
>& rIfc
,
314 css::uno::Reference
< css::beans::XPropertySet
> const & xPropSet
,
315 OUString
const & propName
)
319 const css::uno::Any
& a(xPropSet
->getPropertyValue( propName
));
320 rIfc
.set( a
, css::uno::UNO_QUERY
);
322 bool const bRet
= rIfc
.is();
323 #if OSL_DEBUG_LEVEL > 0
325 SAL_INFO("slideshow", OSL_THIS_FUNC
<< ": while retrieving property " << propName
<< ", cannot extract Any of type "
326 << a
.getValueTypeRef()->pTypeName
<< " to interface");
330 catch (css::uno::RuntimeException
&)
334 catch (css::uno::Exception
&)
340 /// Get the content of the BoundRect shape property
341 basegfx::B2DRange
getAPIShapeBounds( const css::uno::Reference
< css::drawing::XShape
>& xShape
);
344 TODO(F1): When ZOrder someday becomes usable enable this
346 /// Get the content of the ZOrder shape property
347 double getAPIShapePrio( const css::uno::Reference< css::drawing::XShape >& xShape );
350 basegfx::B2IVector
getSlideSizePixel( const basegfx::B2DVector
& rSize
,
351 const std::shared_ptr
<UnoView
>& pView
);
354 // TODO(Q1): this could possibly be implemented with a somewhat
355 // more lightweight template, by having the actual worker receive
356 // only a function pointer, and a thin templated wrapper around
357 // that which converts member functions into that.
359 /** Apply given functor to every animation node child.
365 Functor to apply. The functor must have an appropriate
366 operator()( const css::uno::Reference< css::animations::XAnimationNode >& ) member.
368 @return true, if the functor was successfully applied to
369 all children, false otherwise.
371 template< typename Functor
> inline bool for_each_childNode( const css::uno::Reference
< css::animations::XAnimationNode
>& xNode
,
376 // get an XEnumerationAccess to the children
377 css::uno::Reference
< css::container::XEnumerationAccess
>
378 xEnumerationAccess( xNode
,
379 css::uno::UNO_QUERY_THROW
);
380 css::uno::Reference
< css::container::XEnumeration
>
381 xEnumeration( xEnumerationAccess
->createEnumeration(),
382 css::uno::UNO_QUERY_THROW
);
384 while( xEnumeration
->hasMoreElements() )
386 css::uno::Reference
< css::animations::XAnimationNode
>
387 xChildNode( xEnumeration
->nextElement(),
388 css::uno::UNO_QUERY_THROW
);
389 rFunctor( xChildNode
);
393 catch( css::uno::Exception
& )
400 #endif // INCLUDED_SLIDESHOW_SOURCE_INC_TOOLS_HXX
402 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */