Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <com/sun/star/uno/XComponentContext.hpp>
21 : #include <com/sun/star/lang/XServiceInfo.hpp>
22 : #include <com/sun/star/lang/XTypeProvider.hpp>
23 : #include <com/sun/star/animations/XTargetPropertiesCreator.hpp>
24 : #include <com/sun/star/animations/XIterateContainer.hpp>
25 : #include <com/sun/star/animations/TargetProperties.hpp>
26 : #include <com/sun/star/presentation/ParagraphTarget.hpp>
27 : #include <com/sun/star/registry/XRegistryKey.hpp>
28 : #include <com/sun/star/lang/XInitialization.hpp>
29 : #include <com/sun/star/lang/XServiceName.hpp>
30 : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
31 : #include <com/sun/star/drawing/XShape.hpp>
32 : #include <com/sun/star/animations/AnimationNodeType.hpp>
33 : #include <com/sun/star/animations/XAnimate.hpp>
34 : #include <cppuhelper/compbase3.hxx>
35 : #include <cppuhelper/factory.hxx>
36 : #include <cppuhelper/implementationentry.hxx>
37 : #include <comphelper/broadcasthelper.hxx>
38 : #include <comphelper/sequence.hxx>
39 :
40 : #include <animations/animationnodehelper.hxx>
41 :
42 : #include <vector>
43 : #include <boost/unordered_map.hpp>
44 :
45 :
46 : using namespace ::com::sun::star;
47 :
48 : #define IMPLEMENTATION_NAME "animcore::TargetPropertiesCreator"
49 : #define SERVICE_NAME "com.sun.star.animations.TargetPropertiesCreator"
50 :
51 : namespace animcore
52 : {
53 : typedef ::cppu::WeakComponentImplHelper3< ::com::sun::star::animations::XTargetPropertiesCreator,
54 : lang::XServiceInfo,
55 : lang::XServiceName > TargetPropertiesCreator_Base;
56 :
57 : class TargetPropertiesCreator : public ::comphelper::OBaseMutex,
58 : public TargetPropertiesCreator_Base
59 : {
60 : public:
61 0 : static uno::Reference< uno::XInterface > SAL_CALL createInstance( const uno::Reference< uno::XComponentContext >& xContext ) throw ( uno::Exception )
62 : {
63 0 : return uno::Reference< uno::XInterface >( static_cast<cppu::OWeakObject*>(new TargetPropertiesCreator( xContext )) );
64 : }
65 :
66 : /// Dispose all internal references
67 : virtual void SAL_CALL disposing();
68 :
69 : // XTargetPropertiesCreator
70 : virtual uno::Sequence< animations::TargetProperties > SAL_CALL createInitialTargetProperties( const uno::Reference< animations::XAnimationNode >& rootNode ) throw (uno::RuntimeException);
71 :
72 : // XServiceInfo
73 : virtual OUString SAL_CALL getImplementationName() throw( uno::RuntimeException );
74 : virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw( uno::RuntimeException );
75 : virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() throw( uno::RuntimeException );
76 :
77 : // XServiceName
78 : virtual OUString SAL_CALL getServiceName( ) throw (uno::RuntimeException);
79 :
80 : protected:
81 : ~TargetPropertiesCreator(); // we're a ref-counted UNO class. _We_ destroy ourselves.
82 :
83 : private:
84 : // default: disabled copy/assignment
85 : TargetPropertiesCreator(const TargetPropertiesCreator&);
86 : TargetPropertiesCreator& operator=( const TargetPropertiesCreator& );
87 :
88 : TargetPropertiesCreator( const uno::Reference< uno::XComponentContext >& rxContext );
89 : };
90 :
91 : // --------------------------------------------------------------------
92 :
93 0 : uno::Reference< uno::XInterface > SAL_CALL createInstance_TargetPropertiesCreator( const uno::Reference< uno::XComponentContext > & rSMgr ) throw (uno::Exception)
94 : {
95 0 : return TargetPropertiesCreator::createInstance( rSMgr );
96 : }
97 :
98 27 : OUString getImplementationName_TargetPropertiesCreator()
99 : {
100 27 : return OUString( IMPLEMENTATION_NAME );
101 : }
102 :
103 0 : uno::Sequence< OUString > getSupportedServiceNames_TargetPropertiesCreator(void)
104 : {
105 0 : uno::Sequence< OUString > aRet(1);
106 0 : aRet.getArray()[0] = SERVICE_NAME;
107 0 : return aRet;
108 : }
109 :
110 : // --------------------------------------------------------------------
111 :
112 : namespace
113 : {
114 : // Vector containing all properties for a given shape
115 : typedef ::std::vector< beans::NamedValue > VectorOfNamedValues;
116 :
117 : /** The hash map key
118 :
119 : This key contains both XShape reference and a paragraph
120 : index, as we somehow have to handle shape and paragraph
121 : targets with the same data structure.
122 : */
123 0 : struct ShapeHashKey
124 : {
125 : /// Shape target
126 : uno::Reference< drawing::XShape > mxRef;
127 :
128 : /** Paragraph index.
129 :
130 : If this is a pure shape target, mnParagraphIndex is
131 : set to -1.
132 : */
133 : sal_Int16 mnParagraphIndex;
134 :
135 : /// Comparison needed for boost::unordered_map
136 0 : bool operator==( const ShapeHashKey& rRHS ) const
137 : {
138 0 : return mxRef == rRHS.mxRef && mnParagraphIndex == rRHS.mnParagraphIndex;
139 : }
140 : };
141 :
142 : // A hash functor for ShapeHashKey objects
143 : struct ShapeKeyHasher
144 : {
145 0 : ::std::size_t operator()( const ShapeHashKey& rKey ) const
146 : {
147 : // TODO(P2): Maybe a better hash function would be to
148 : // spread mnParagraphIndex to 32 bit: a0b0c0d0e0... Hakmem
149 : // should have a formula.
150 : //
151 : // Yes it has:
152 : // x = (x & 0x0000FF00) << 8) | (x >> 8) & 0x0000FF00 | x & 0xFF0000FF;
153 : // x = (x & 0x00F000F0) << 4) | (x >> 4) & 0x00F000F0 | x & 0xF00FF00F;
154 : // x = (x & 0x0C0C0C0C) << 2) | (x >> 2) & 0x0C0C0C0C | x & 0xC3C3C3C3;
155 : // x = (x & 0x22222222) << 1) | (x >> 1) & 0x22222222 | x & 0x99999999;
156 : //
157 : // Costs about 17 cycles on a RISC machine with infinite
158 : // instruction level parallelism (~42 basic
159 : // instructions). Thus I truly doubt this pays off...
160 0 : return reinterpret_cast< ::std::size_t >(rKey.mxRef.get()) ^ (rKey.mnParagraphIndex << 16L);
161 : }
162 : };
163 :
164 : // A hash map which maps a XShape to the corresponding vector of initial properties
165 : typedef ::boost::unordered_map< ShapeHashKey, VectorOfNamedValues, ShapeKeyHasher > XShapeHash;
166 :
167 :
168 0 : class NodeFunctor
169 : {
170 : public:
171 0 : explicit NodeFunctor( XShapeHash& rShapeHash ) :
172 : mrShapeHash( rShapeHash ),
173 : mxTargetShape(),
174 0 : mnParagraphIndex( -1 )
175 : {
176 0 : }
177 :
178 0 : NodeFunctor( XShapeHash& rShapeHash,
179 : const uno::Reference< drawing::XShape >& rTargetShape,
180 : sal_Int16 nParagraphIndex ) :
181 : mrShapeHash( rShapeHash ),
182 : mxTargetShape( rTargetShape ),
183 0 : mnParagraphIndex( nParagraphIndex )
184 : {
185 0 : }
186 :
187 0 : void operator()( const uno::Reference< animations::XAnimationNode >& xNode ) const
188 : {
189 0 : if( !xNode.is() )
190 : {
191 : OSL_FAIL( "AnimCore: NodeFunctor::operator(): invalid XAnimationNode" );
192 0 : return;
193 : }
194 :
195 0 : uno::Reference< drawing::XShape > xTargetShape( mxTargetShape );
196 0 : sal_Int16 nParagraphIndex( mnParagraphIndex );
197 :
198 0 : switch( xNode->getType() )
199 : {
200 : case animations::AnimationNodeType::ITERATE:
201 : {
202 : // extract target shape from iterate node
203 : // (will override the target for all children)
204 : // --------------------------------------------------
205 :
206 : uno::Reference< animations::XIterateContainer > xIterNode( xNode,
207 0 : uno::UNO_QUERY );
208 :
209 : // TODO(E1): I'm not too sure what to expect here...
210 0 : if( !xIterNode->getTarget().hasValue() )
211 : {
212 : OSL_FAIL( "animcore: NodeFunctor::operator(): no target on ITERATE node" );
213 0 : return;
214 : }
215 :
216 0 : xTargetShape.set( xIterNode->getTarget(),
217 0 : uno::UNO_QUERY );
218 :
219 0 : if( !xTargetShape.is() )
220 : {
221 0 : ::com::sun::star::presentation::ParagraphTarget aTarget;
222 :
223 : // no shape provided. Maybe a ParagraphTarget?
224 0 : if( !(xIterNode->getTarget() >>= aTarget) )
225 : {
226 : OSL_FAIL( "animcore: NodeFunctor::operator(): could not extract any "
227 : "target information" );
228 0 : return;
229 : }
230 :
231 0 : xTargetShape = aTarget.Shape;
232 0 : nParagraphIndex = aTarget.Paragraph;
233 :
234 0 : if( !xTargetShape.is() )
235 : {
236 : OSL_FAIL( "animcore: NodeFunctor::operator(): invalid shape in ParagraphTarget" );
237 0 : return;
238 0 : }
239 0 : }
240 : }
241 : // FALLTHROUGH intended
242 : case animations::AnimationNodeType::PAR:
243 : // FALLTHROUGH intended
244 : case animations::AnimationNodeType::SEQ:
245 : {
246 : NodeFunctor aFunctor( mrShapeHash,
247 : xTargetShape,
248 0 : nParagraphIndex );
249 0 : if( !::anim::for_each_childNode( xNode,
250 0 : aFunctor ) )
251 : {
252 : OSL_FAIL( "AnimCore: NodeFunctor::operator(): child node iteration failed, "
253 : "or extraneous container nodes encountered" );
254 0 : }
255 : }
256 0 : break;
257 :
258 : case animations::AnimationNodeType::CUSTOM:
259 : // FALLTHROUGH intended
260 : case animations::AnimationNodeType::ANIMATE:
261 : // FALLTHROUGH intended
262 : case animations::AnimationNodeType::ANIMATEMOTION:
263 : // FALLTHROUGH intended
264 : case animations::AnimationNodeType::ANIMATECOLOR:
265 : // FALLTHROUGH intended
266 : case animations::AnimationNodeType::ANIMATETRANSFORM:
267 : // FALLTHROUGH intended
268 : case animations::AnimationNodeType::TRANSITIONFILTER:
269 : // FALLTHROUGH intended
270 : case animations::AnimationNodeType::AUDIO:
271 : // FALLTHROUGH intended
272 : /*default:
273 : // ignore this node, no valuable content for now.
274 : break;*/
275 :
276 : case animations::AnimationNodeType::SET:
277 : {
278 : // evaluate set node content
279 : uno::Reference< animations::XAnimate > xAnimateNode( xNode,
280 0 : uno::UNO_QUERY );
281 :
282 0 : if( !xAnimateNode.is() )
283 0 : break; // invalid node
284 :
285 : // determine target shape (if any)
286 0 : ShapeHashKey aTarget;
287 0 : if( xTargetShape.is() )
288 : {
289 : // override target shape with parent-supplied
290 0 : aTarget.mxRef = xTargetShape;
291 0 : aTarget.mnParagraphIndex = nParagraphIndex;
292 : }
293 : else
294 : {
295 : // no parent-supplied target, retrieve
296 : // node target
297 0 : if( (xAnimateNode->getTarget() >>= aTarget.mxRef) )
298 : {
299 : // pure shape target - set paragraph
300 : // index to magic
301 0 : aTarget.mnParagraphIndex = -1;
302 : }
303 : else
304 : {
305 : // not a pure shape target - maybe a
306 : // ParagraphTarget?
307 0 : presentation::ParagraphTarget aUnoTarget;
308 :
309 0 : if( !(xAnimateNode->getTarget() >>= aUnoTarget) )
310 : {
311 : OSL_FAIL( "AnimCore: NodeFunctor::operator(): unknown target type encountered" );
312 0 : break;
313 : }
314 :
315 0 : aTarget.mxRef = aUnoTarget.Shape;
316 0 : aTarget.mnParagraphIndex = aUnoTarget.Paragraph;
317 : }
318 : }
319 :
320 0 : if( !aTarget.mxRef.is() )
321 : {
322 : OSL_FAIL( "AnimCore: NodeFunctor::operator(): Found target, but XShape is NULL" );
323 0 : break; // invalid target XShape
324 : }
325 :
326 : // check whether we already have an entry for
327 : // this target (we only take the first set
328 : // effect for every shape)
329 0 : XShapeHash::const_iterator aIter;
330 0 : if( (aIter=mrShapeHash.find( aTarget )) != mrShapeHash.end() )
331 0 : break; // already an entry in existence for given XShape
332 :
333 : // if this is an appear effect, hide shape
334 : // initially. This is currently the only place
335 : // where a shape effect influences shape
336 : // attributes outside it's effective duration.
337 0 : sal_Bool bVisible( sal_False );
338 0 : if( xAnimateNode->getAttributeName().equalsIgnoreAsciiCase("visibility") )
339 : {
340 :
341 0 : uno::Any aAny( xAnimateNode->getTo() );
342 :
343 : // try to extract bool value
344 0 : if( !(aAny >>= bVisible) )
345 : {
346 : // try to extract string
347 0 : OUString aString;
348 0 : if( (aAny >>= aString) )
349 : {
350 : // we also take the strings "true" and "false",
351 : // as well as "on" and "off" here
352 0 : if( aString.equalsIgnoreAsciiCase("true") ||
353 0 : aString.equalsIgnoreAsciiCase("on") )
354 : {
355 0 : bVisible = sal_True;
356 : }
357 0 : if( aString.equalsIgnoreAsciiCase("false") ||
358 0 : aString.equalsIgnoreAsciiCase("off") )
359 : {
360 0 : bVisible = sal_False;
361 : }
362 0 : }
363 0 : }
364 :
365 : /*if( bVisible )
366 : {
367 : // target is set to 'visible' at the
368 : // first relevant effect. Thus, target
369 : // must be initially _hidden_, for the
370 : // effect to have visible impact.
371 : */
372 : }
373 : // target is set the 'visible' value,
374 : // so we should record the opposite value
375 : mrShapeHash.insert(
376 : XShapeHash::value_type(
377 : aTarget,
378 : VectorOfNamedValues(
379 : 1,
380 : beans::NamedValue(
381 : //xAnimateNode->getAttributeName(),
382 0 : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("visibility")),
383 0 : uno::makeAny( !bVisible ) ) ) ) );
384 : //}
385 : //}
386 : }
387 0 : break;
388 0 : }
389 : }
390 :
391 : private:
392 : XShapeHash& mrShapeHash;
393 : uno::Reference< drawing::XShape > mxTargetShape;
394 : sal_Int16 mnParagraphIndex;
395 : };
396 : }
397 :
398 : // --------------------------------------------------------------------
399 :
400 0 : TargetPropertiesCreator::TargetPropertiesCreator( const uno::Reference< uno::XComponentContext >& ) :
401 0 : TargetPropertiesCreator_Base( m_aMutex )
402 : {
403 0 : }
404 :
405 0 : TargetPropertiesCreator::~TargetPropertiesCreator()
406 : {
407 0 : }
408 :
409 0 : void SAL_CALL TargetPropertiesCreator::disposing()
410 : {
411 0 : ::osl::MutexGuard aGuard( m_aMutex );
412 0 : }
413 :
414 : // XTargetPropertiesCreator
415 0 : uno::Sequence< animations::TargetProperties > SAL_CALL TargetPropertiesCreator::createInitialTargetProperties
416 : (
417 : const uno::Reference< animations::XAnimationNode >& xRootNode
418 : ) throw (uno::RuntimeException)
419 : {
420 0 : ::osl::MutexGuard aGuard( m_aMutex );
421 :
422 : // scan all nodes for visibility changes, and record first
423 : // 'visibility=true' for each shape
424 0 : XShapeHash aShapeHash( 101 );
425 :
426 0 : NodeFunctor aFunctor( aShapeHash );
427 :
428 : // TODO(F1): Maybe limit functor application to main sequence
429 : // alone (CL said something that shape visibility is only
430 : // affected by effects in the main sequence for PPT).
431 : //
432 : // OTOH, client code can pass us only the main sequence (which
433 : // it actually does right now, for the slideshow implementation).
434 0 : aFunctor( xRootNode );
435 :
436 :
437 : // output to result sequence
438 : // ----------------------------------------------------------------------
439 :
440 0 : uno::Sequence< animations::TargetProperties > aRes( aShapeHash.size() );
441 :
442 0 : ::std::size_t nCurrIndex(0);
443 0 : XShapeHash::const_iterator aCurr( aShapeHash.begin() );
444 0 : const XShapeHash::const_iterator aEnd ( aShapeHash.end() );
445 0 : while( aCurr != aEnd )
446 : {
447 0 : animations::TargetProperties& rCurrProps( aRes[ nCurrIndex++ ] );
448 :
449 0 : if( aCurr->first.mnParagraphIndex == -1 )
450 : {
451 0 : rCurrProps.Target = uno::makeAny( aCurr->first.mxRef );
452 : }
453 : else
454 : {
455 0 : rCurrProps.Target = uno::makeAny(
456 : presentation::ParagraphTarget(
457 0 : aCurr->first.mxRef,
458 0 : aCurr->first.mnParagraphIndex ) );
459 : }
460 :
461 0 : rCurrProps.Properties = ::comphelper::containerToSequence( aCurr->second );
462 :
463 0 : ++aCurr;
464 : }
465 :
466 0 : return aRes;
467 : }
468 :
469 : // XServiceInfo
470 0 : OUString SAL_CALL TargetPropertiesCreator::getImplementationName() throw( uno::RuntimeException )
471 : {
472 0 : return OUString( IMPLEMENTATION_NAME );
473 : }
474 :
475 0 : sal_Bool SAL_CALL TargetPropertiesCreator::supportsService( const OUString& ServiceName ) throw( uno::RuntimeException )
476 : {
477 0 : return ServiceName.equalsIgnoreAsciiCase(SERVICE_NAME);
478 : }
479 :
480 0 : uno::Sequence< OUString > SAL_CALL TargetPropertiesCreator::getSupportedServiceNames() throw( uno::RuntimeException )
481 : {
482 0 : uno::Sequence< OUString > aRet(1);
483 0 : aRet[0] = SERVICE_NAME;
484 :
485 0 : return aRet;
486 : }
487 :
488 : // XServiceName
489 0 : OUString SAL_CALL TargetPropertiesCreator::getServiceName( ) throw (uno::RuntimeException)
490 : {
491 0 : return OUString( SERVICE_NAME );
492 : }
493 :
494 : } // namespace animcore
495 :
496 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|