2 * Copyright (C) Research In Motion Limited 2011, 2012. All rights reserved.
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.
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.
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.
21 #include "core/svg/SVGAnimatedAngle.h"
23 #include "bindings/v8/ExceptionStatePlaceholder.h"
24 #include "core/svg/SVGAnimateElement.h"
25 #include "core/svg/SVGMarkerElement.h"
29 SVGAnimatedAngleAnimator::SVGAnimatedAngleAnimator(SVGAnimationElement* animationElement, SVGElement* contextElement)
30 : SVGAnimatedTypeAnimator(AnimatedAngle, animationElement, contextElement)
34 static inline SVGAngle& sharedSVGAngle(const String& valueAsString)
36 DEFINE_STATIC_LOCAL(SVGAngle, sharedAngle, ());
37 sharedAngle.setValueAsString(valueAsString, ASSERT_NO_EXCEPTION);
41 PassOwnPtr<SVGAnimatedType> SVGAnimatedAngleAnimator::constructFromString(const String& string)
43 OwnPtr<SVGAnimatedType> animatedType = SVGAnimatedType::createAngleAndEnumeration(new pair<SVGAngle, unsigned>);
44 pair<SVGAngle, unsigned>& animatedPair = animatedType->angleAndEnumeration();
47 SVGMarkerOrientType orientType = SVGPropertyTraits<SVGMarkerOrientType>::fromString(string, angle);
49 animatedPair.second = orientType;
50 if (orientType == SVGMarkerOrientAngle)
51 animatedPair.first = angle;
53 return animatedType.release();
56 PassOwnPtr<SVGAnimatedType> SVGAnimatedAngleAnimator::startAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
58 return SVGAnimatedType::createAngleAndEnumeration(constructFromBaseValues<SVGAnimatedAngle, SVGAnimatedEnumeration>(animatedTypes));
61 void SVGAnimatedAngleAnimator::stopAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
63 stopAnimValAnimationForTypes<SVGAnimatedAngle, SVGAnimatedEnumeration>(animatedTypes);
66 void SVGAnimatedAngleAnimator::resetAnimValToBaseVal(const SVGElementAnimatedPropertyList& animatedTypes, SVGAnimatedType* type)
68 resetFromBaseValues<SVGAnimatedAngle, SVGAnimatedEnumeration>(animatedTypes, type, &SVGAnimatedType::angleAndEnumeration);
71 void SVGAnimatedAngleAnimator::animValWillChange(const SVGElementAnimatedPropertyList& animatedTypes)
73 animValWillChangeForTypes<SVGAnimatedAngle, SVGAnimatedEnumeration>(animatedTypes);
76 void SVGAnimatedAngleAnimator::animValDidChange(const SVGElementAnimatedPropertyList& animatedTypes)
78 animValDidChangeForTypes<SVGAnimatedAngle, SVGAnimatedEnumeration>(animatedTypes);
81 void SVGAnimatedAngleAnimator::addAnimatedTypes(SVGAnimatedType* from, SVGAnimatedType* to)
83 ASSERT(from->type() == AnimatedAngle);
84 ASSERT(from->type() == to->type());
86 const pair<SVGAngle, unsigned>& fromAngleAndEnumeration = from->angleAndEnumeration();
87 pair<SVGAngle, unsigned>& toAngleAndEnumeration = to->angleAndEnumeration();
88 // Only respect by animations, if from and by are both specified in angles (and not eg. 'auto').
89 if (fromAngleAndEnumeration.second != toAngleAndEnumeration.second || fromAngleAndEnumeration.second != SVGMarkerOrientAngle)
91 const SVGAngle& fromAngle = fromAngleAndEnumeration.first;
92 SVGAngle& toAngle = toAngleAndEnumeration.first;
93 toAngle.setValue(toAngle.value() + fromAngle.value());
96 void SVGAnimatedAngleAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount, SVGAnimatedType* from, SVGAnimatedType* to, SVGAnimatedType* toAtEndOfDuration, SVGAnimatedType* animated)
98 ASSERT(m_animationElement);
99 ASSERT(m_contextElement);
101 const pair<SVGAngle, unsigned>& fromAngleAndEnumeration = m_animationElement->animationMode() == ToAnimation ? animated->angleAndEnumeration() : from->angleAndEnumeration();
102 const pair<SVGAngle, unsigned>& toAngleAndEnumeration = to->angleAndEnumeration();
103 const pair<SVGAngle, unsigned>& toAtEndOfDurationAngleAndEnumeration = toAtEndOfDuration->angleAndEnumeration();
104 pair<SVGAngle, unsigned>& animatedAngleAndEnumeration = animated->angleAndEnumeration();
106 if (fromAngleAndEnumeration.second != toAngleAndEnumeration.second) {
107 // Animating from eg. auto to 90deg, or auto to 90deg.
108 if (fromAngleAndEnumeration.second == SVGMarkerOrientAngle) {
109 // Animating from an angle value to eg. 'auto' - this disabled additive as 'auto' is a keyword..
110 if (toAngleAndEnumeration.second == SVGMarkerOrientAuto) {
111 if (percentage < 0.5f) {
112 animatedAngleAndEnumeration.first = fromAngleAndEnumeration.first;
113 animatedAngleAndEnumeration.second = SVGMarkerOrientAngle;
116 animatedAngleAndEnumeration.first.setValue(0);
117 animatedAngleAndEnumeration.second = SVGMarkerOrientAuto;
120 animatedAngleAndEnumeration.first.setValue(0);
121 animatedAngleAndEnumeration.second = SVGMarkerOrientUnknown;
126 // From 'auto' to 'auto'.
127 if (fromAngleAndEnumeration.second == SVGMarkerOrientAuto) {
128 animatedAngleAndEnumeration.first.setValue(0);
129 animatedAngleAndEnumeration.second = SVGMarkerOrientAuto;
133 // If the enumeration value is not angle or auto, its unknown.
134 if (fromAngleAndEnumeration.second != SVGMarkerOrientAngle) {
135 animatedAngleAndEnumeration.first.setValue(0);
136 animatedAngleAndEnumeration.second = SVGMarkerOrientUnknown;
140 // Regular from angle to angle animation, with all features like additive etc.
141 animatedAngleAndEnumeration.second = SVGMarkerOrientAngle;
143 SVGAngle& animatedSVGAngle = animatedAngleAndEnumeration.first;
144 const SVGAngle& toAtEndOfDurationSVGAngle = toAtEndOfDurationAngleAndEnumeration.first;
145 float animatedAngle = animatedSVGAngle.value();
146 m_animationElement->animateAdditiveNumber(percentage, repeatCount, fromAngleAndEnumeration.first.value(), toAngleAndEnumeration.first.value(), toAtEndOfDurationSVGAngle.value(), animatedAngle);
147 animatedSVGAngle.setValue(animatedAngle);
150 float SVGAnimatedAngleAnimator::calculateDistance(const String& fromString, const String& toString)
152 SVGAngle from = SVGAngle();
153 from.setValueAsString(fromString, ASSERT_NO_EXCEPTION);
154 SVGAngle to = SVGAngle();
155 to.setValueAsString(toString, ASSERT_NO_EXCEPTION);
156 return fabsf(to.value() - from.value());