80974776e2239f439d3dba6a937e59c05862b49a
[WebKit-https.git] / Source / WebCore / svg / SVGAnimatedPath.cpp
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 "SVGAnimatedPath.h"
22
23 #include "SVGAnimateElementBase.h"
24 #include "SVGAnimatedPathSegListPropertyTearOff.h"
25 #include "SVGPathUtilities.h"
26
27 namespace WebCore {
28
29 SVGAnimatedPathAnimator::SVGAnimatedPathAnimator(SVGAnimationElement* animationElement, SVGElement* contextElement)
30     : SVGAnimatedTypeAnimator(AnimatedPath, animationElement, contextElement)
31 {
32 }
33
34 std::unique_ptr<SVGAnimatedType> SVGAnimatedPathAnimator::constructFromString(const String& string)
35 {
36     auto byteStream = std::make_unique<SVGPathByteStream>();
37     buildSVGPathByteStreamFromString(string, byteStream.get(), UnalteredParsing);
38     return SVGAnimatedType::createPath(WTF::move(byteStream));
39 }
40
41 std::unique_ptr<SVGAnimatedType> SVGAnimatedPathAnimator::startAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
42 {
43     ASSERT(animatedTypes.size() >= 1);
44     SVGAnimatedPathSegListPropertyTearOff* property = castAnimatedPropertyToActualType<SVGAnimatedPathSegListPropertyTearOff>(animatedTypes[0].properties[0].get());
45     const SVGPathSegList& baseValue = property->currentBaseValue();
46
47     // Build initial path byte stream.
48     auto byteStream = std::make_unique<SVGPathByteStream>();
49     buildSVGPathByteStreamFromSVGPathSegList(baseValue, byteStream.get(), UnalteredParsing);
50
51     Vector<RefPtr<SVGAnimatedPathSegListPropertyTearOff>> result;
52
53     for (auto& type : animatedTypes)
54         result.append(castAnimatedPropertyToActualType<SVGAnimatedPathSegListPropertyTearOff>(type.properties[0].get()));
55
56     SVGElement::InstanceUpdateBlocker blocker(*property->contextElement());
57
58     for (auto& segment : result)
59         segment->animationStarted(byteStream.get(), &baseValue);
60
61     return SVGAnimatedType::createPath(WTF::move(byteStream));
62 }
63
64 void SVGAnimatedPathAnimator::stopAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
65 {
66     stopAnimValAnimationForType<SVGAnimatedPathSegListPropertyTearOff>(animatedTypes);
67 }
68
69 void SVGAnimatedPathAnimator::resetAnimValToBaseVal(const SVGElementAnimatedPropertyList& animatedTypes, SVGAnimatedType* type)
70 {
71     ASSERT(animatedTypes.size() >= 1);
72     ASSERT(type);
73     ASSERT(type->type() == m_type);
74     const SVGPathSegList& baseValue = castAnimatedPropertyToActualType<SVGAnimatedPathSegListPropertyTearOff>(animatedTypes[0].properties[0].get())->currentBaseValue();
75     buildSVGPathByteStreamFromSVGPathSegList(baseValue, type->path(), UnalteredParsing);
76 }
77
78 void SVGAnimatedPathAnimator::animValWillChange(const SVGElementAnimatedPropertyList& animatedTypes)
79 {
80     animValWillChangeForType<SVGAnimatedPathSegListPropertyTearOff>(animatedTypes);
81 }
82
83 void SVGAnimatedPathAnimator::animValDidChange(const SVGElementAnimatedPropertyList& animatedTypes)
84 {
85     animValDidChangeForType<SVGAnimatedPathSegListPropertyTearOff>(animatedTypes);
86 }
87
88 void SVGAnimatedPathAnimator::addAnimatedTypes(SVGAnimatedType* from, SVGAnimatedType* to)
89 {
90     ASSERT(from->type() == AnimatedPath);
91     ASSERT(from->type() == to->type());
92
93     SVGPathByteStream* fromPath = from->path();
94     SVGPathByteStream* toPath = to->path();
95     unsigned fromPathSize = fromPath->size();
96     if (!fromPathSize || fromPathSize != toPath->size())
97         return;
98     addToSVGPathByteStream(toPath, fromPath);
99 }
100
101 void SVGAnimatedPathAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount, SVGAnimatedType* from, SVGAnimatedType* to, SVGAnimatedType* toAtEndOfDuration, SVGAnimatedType* animated)
102 {
103     ASSERT(m_animationElement);
104     ASSERT(m_contextElement);
105
106     SVGPathByteStream* fromPath = from->path();
107     SVGPathByteStream* toPath = to->path();
108     SVGPathByteStream* toAtEndOfDurationPath = toAtEndOfDuration->path();
109     SVGPathByteStream* animatedPath = animated->path();
110
111     std::unique_ptr<SVGPathByteStream> underlyingPath;
112     bool isToAnimation = m_animationElement->animationMode() == ToAnimation;
113     if (isToAnimation) {
114         underlyingPath = animatedPath->copy();
115         fromPath = underlyingPath.get();
116     }
117
118     // Cache the current animated value before the buildAnimatedSVGPathByteStream() clears animatedPath.
119     std::unique_ptr<SVGPathByteStream> lastAnimatedPath;
120     if (!fromPath->size() || (m_animationElement->isAdditive() && !isToAnimation))
121         lastAnimatedPath = animatedPath->copy();
122
123     // Pass false to 'resizeAnimatedListIfNeeded' here, as the path animation is not a regular Vector<SVGXXX> type, but a SVGPathByteStream, that works differently.
124     if (!m_animationElement->adjustFromToListValues<SVGPathByteStream>(*fromPath, *toPath, *animatedPath, percentage, false))
125         return;
126
127     buildAnimatedSVGPathByteStream(fromPath, toPath, animatedPath, percentage);
128
129     // Handle additive='sum'.
130     if (lastAnimatedPath)
131         addToSVGPathByteStream(animatedPath, lastAnimatedPath.get());
132
133     // Handle accumulate='sum'.
134     if (m_animationElement->isAccumulated() && repeatCount)
135         addToSVGPathByteStream(animatedPath, toAtEndOfDurationPath, repeatCount);
136 }
137
138 float SVGAnimatedPathAnimator::calculateDistance(const String&, const String&)
139 {
140     // FIXME: Support paced animations.
141     return -1;
142 }
143
144 }