1/*
2 * Copyright (C) Research In Motion Limited 2011, 2012. 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)
23#include "SVGAnimatedAngle.h"
24
25#include "SVGAnimateElement.h"
26#include "SVGMarkerElement.h"
27
28namespace WebCore {
29
30SVGAnimatedAngleAnimator::SVGAnimatedAngleAnimator(SVGAnimationElement* animationElement, SVGElement* contextElement)
31    : SVGAnimatedTypeAnimator(AnimatedAngle, animationElement, contextElement)
32{
33}
34
35static inline SVGAngle& sharedSVGAngle(const String& valueAsString)
36{
37    DEFINE_STATIC_LOCAL(SVGAngle, sharedAngle, ());
38    sharedAngle.setValueAsString(valueAsString, ASSERT_NO_EXCEPTION);
39    return sharedAngle;
40}
41
42PassOwnPtr<SVGAnimatedType> SVGAnimatedAngleAnimator::constructFromString(const String& string)
43{
44    OwnPtr<SVGAnimatedType> animatedType = SVGAnimatedType::createAngleAndEnumeration(new pair<SVGAngle, unsigned>);
45    pair<SVGAngle, unsigned>& animatedPair = animatedType->angleAndEnumeration();
46
47    SVGAngle angle;
48    SVGMarkerOrientType orientType = SVGPropertyTraits<SVGMarkerOrientType>::fromString(string,  angle);
49    if (orientType > 0)
50        animatedPair.second = orientType;
51    if (orientType == SVGMarkerOrientAngle)
52        animatedPair.first = angle;
53
54    return animatedType.release();
55}
56
57PassOwnPtr<SVGAnimatedType> SVGAnimatedAngleAnimator::startAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
58{
59    return SVGAnimatedType::createAngleAndEnumeration(constructFromBaseValues<SVGAnimatedAngle, SVGAnimatedEnumeration>(animatedTypes));
60}
61
62void SVGAnimatedAngleAnimator::stopAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
63{
64    stopAnimValAnimationForTypes<SVGAnimatedAngle, SVGAnimatedEnumeration>(animatedTypes);
65}
66
67void SVGAnimatedAngleAnimator::resetAnimValToBaseVal(const SVGElementAnimatedPropertyList& animatedTypes, SVGAnimatedType* type)
68{
69    resetFromBaseValues<SVGAnimatedAngle, SVGAnimatedEnumeration>(animatedTypes, type, &SVGAnimatedType::angleAndEnumeration);
70}
71
72void SVGAnimatedAngleAnimator::animValWillChange(const SVGElementAnimatedPropertyList& animatedTypes)
73{
74    animValWillChangeForTypes<SVGAnimatedAngle, SVGAnimatedEnumeration>(animatedTypes);
75}
76
77void SVGAnimatedAngleAnimator::animValDidChange(const SVGElementAnimatedPropertyList& animatedTypes)
78{
79    animValDidChangeForTypes<SVGAnimatedAngle, SVGAnimatedEnumeration>(animatedTypes);
80}
81
82void SVGAnimatedAngleAnimator::addAnimatedTypes(SVGAnimatedType* from, SVGAnimatedType* to)
83{
84    ASSERT(from->type() == AnimatedAngle);
85    ASSERT(from->type() == to->type());
86
87    const pair<SVGAngle, unsigned>& fromAngleAndEnumeration = from->angleAndEnumeration();
88    pair<SVGAngle, unsigned>& toAngleAndEnumeration = to->angleAndEnumeration();
89    // Only respect by animations, if from and by are both specified in angles (and not eg. 'auto').
90    if (fromAngleAndEnumeration.second != toAngleAndEnumeration.second || fromAngleAndEnumeration.second != SVGMarkerOrientAngle)
91        return;
92    const SVGAngle& fromAngle = fromAngleAndEnumeration.first;
93    SVGAngle& toAngle = toAngleAndEnumeration.first;
94    toAngle.setValue(toAngle.value() + fromAngle.value());
95}
96
97void SVGAnimatedAngleAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount, SVGAnimatedType* from, SVGAnimatedType* to, SVGAnimatedType* toAtEndOfDuration, SVGAnimatedType* animated)
98{
99    ASSERT(m_animationElement);
100    ASSERT(m_contextElement);
101
102    const pair<SVGAngle, unsigned>& fromAngleAndEnumeration = m_animationElement->animationMode() == ToAnimation ? animated->angleAndEnumeration() : from->angleAndEnumeration();
103    const pair<SVGAngle, unsigned>& toAngleAndEnumeration = to->angleAndEnumeration();
104    const pair<SVGAngle, unsigned>& toAtEndOfDurationAngleAndEnumeration = toAtEndOfDuration->angleAndEnumeration();
105    pair<SVGAngle, unsigned>& animatedAngleAndEnumeration = animated->angleAndEnumeration();
106
107    if (fromAngleAndEnumeration.second != toAngleAndEnumeration.second) {
108        // Animating from eg. auto to 90deg, or auto to 90deg.
109        if (fromAngleAndEnumeration.second == SVGMarkerOrientAngle) {
110            // Animating from an angle value to eg. 'auto' - this disabled additive as 'auto' is a keyword..
111            if (toAngleAndEnumeration.second == SVGMarkerOrientAuto) {
112                if (percentage < 0.5f) {
113                    animatedAngleAndEnumeration.first = fromAngleAndEnumeration.first;
114                    animatedAngleAndEnumeration.second = SVGMarkerOrientAngle;
115                    return;
116                }
117                animatedAngleAndEnumeration.first.setValue(0);
118                animatedAngleAndEnumeration.second = SVGMarkerOrientAuto;
119                return;
120            }
121            animatedAngleAndEnumeration.first.setValue(0);
122            animatedAngleAndEnumeration.second = SVGMarkerOrientUnknown;
123            return;
124        }
125    }
126
127    // From 'auto' to 'auto'.
128    if (fromAngleAndEnumeration.second == SVGMarkerOrientAuto) {
129        animatedAngleAndEnumeration.first.setValue(0);
130        animatedAngleAndEnumeration.second = SVGMarkerOrientAuto;
131        return;
132    }
133
134    // If the enumeration value is not angle or auto, its unknown.
135    if (fromAngleAndEnumeration.second != SVGMarkerOrientAngle) {
136        animatedAngleAndEnumeration.first.setValue(0);
137        animatedAngleAndEnumeration.second = SVGMarkerOrientUnknown;
138        return;
139    }
140
141    // Regular from angle to angle animation, with all features like additive etc.
142    animatedAngleAndEnumeration.second = SVGMarkerOrientAngle;
143
144    SVGAngle& animatedSVGAngle = animatedAngleAndEnumeration.first;
145    const SVGAngle& toAtEndOfDurationSVGAngle = toAtEndOfDurationAngleAndEnumeration.first;
146    float animatedAngle = animatedSVGAngle.value();
147    m_animationElement->animateAdditiveNumber(percentage, repeatCount, fromAngleAndEnumeration.first.value(), toAngleAndEnumeration.first.value(), toAtEndOfDurationSVGAngle.value(), animatedAngle);
148    animatedSVGAngle.setValue(animatedAngle);
149}
150
151float SVGAnimatedAngleAnimator::calculateDistance(const String& fromString, const String& toString)
152{
153    SVGAngle from = SVGAngle();
154    from.setValueAsString(fromString, ASSERT_NO_EXCEPTION);
155    SVGAngle to = SVGAngle();
156    to.setValueAsString(toString, ASSERT_NO_EXCEPTION);
157    return fabsf(to.value() - from.value());
158}
159
160}
161
162#endif // ENABLE(SVG)
163