initial import
[vuplus_webkit] / Source / WebCore / svg / SVGAnimatedInteger.cpp
1 /*
2  * Copyright (C) Research In Motion Limited 2011. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21
22 #if ENABLE(SVG) && ENABLE(SVG_ANIMATION)
23 #include "SVGAnimatedInteger.h"
24
25 #include "SVGAnimateElement.h"
26 #include "SVGAnimatedNumber.h"
27 #include <wtf/MathExtras.h>
28
29 namespace WebCore {
30
31 SVGAnimatedIntegerAnimator::SVGAnimatedIntegerAnimator(SVGAnimationElement* animationElement, SVGElement* contextElement)
32     : SVGAnimatedTypeAnimator(AnimatedInteger, animationElement, contextElement)
33 {
34 }
35
36 PassOwnPtr<SVGAnimatedType> SVGAnimatedIntegerAnimator::constructFromString(const String& string)
37 {
38     OwnPtr<SVGAnimatedType> animtedType = SVGAnimatedType::createInteger(new int);
39     animtedType->integer() = string.toIntStrict();
40     return animtedType.release();
41 }
42
43 void SVGAnimatedIntegerAnimator::calculateFromAndToValues(OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, const String& fromString, const String& toString)
44 {
45     ASSERT(m_contextElement);
46     ASSERT(m_animationElement);
47     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
48     animationElement->determinePropertyValueTypes(fromString, toString);
49     
50     from = constructFromString(fromString);
51     to = constructFromString(toString);
52 }
53
54 void SVGAnimatedIntegerAnimator::calculateFromAndByValues(OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, const String& fromString, const String& byString)
55 {
56     ASSERT(m_contextElement);
57     ASSERT(m_animationElement);
58     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
59     animationElement->determinePropertyValueTypes(fromString, byString);
60     
61     from = constructFromString(fromString);
62     to = constructFromString(byString);
63     
64     to->integer() += from->integer();
65 }
66
67 void SVGAnimatedIntegerAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount,
68                                                         OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, OwnPtr<SVGAnimatedType>& animated)
69 {
70     ASSERT(m_animationElement);
71     ASSERT(m_contextElement);
72
73     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);    
74     AnimationMode animationMode = animationElement->animationMode();
75
76     // To animation uses contributions from the lower priority animations as the base value.
77     int& animatedInt = animated->integer();
78     if (animationMode == ToAnimation)
79         from->integer() = animatedInt;
80     
81     float result = animatedInt;
82     SVGAnimatedNumberAnimator::calculateAnimatedNumber(animationElement, percentage, repeatCount, result, from->integer(), to->integer());
83     animatedInt = static_cast<int>(roundf(result));
84 }
85
86 float SVGAnimatedIntegerAnimator::calculateDistance(const String& fromString, const String& toString)
87 {
88     ASSERT(m_contextElement);
89     int from = fromString.toIntStrict();
90     int to = toString.toIntStrict();
91     return abs(to - from);
92 }
93     
94 }
95
96 #endif // ENABLE(SVG) && ENABLE(SVG_ANIMATION)