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