Crash when appending an SVG <use> element dynamically which has animated SVG <path...
[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
45     // Build initial path byte stream.
46     auto byteStream = std::make_unique<SVGPathByteStream>();
47     resetAnimValToBaseVal(animatedTypes, byteStream.get());
48     return SVGAnimatedType::createPath(WTF::move(byteStream));
49 }
50
51 void SVGAnimatedPathAnimator::stopAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
52 {
53     stopAnimValAnimationForType<SVGAnimatedPathSegListPropertyTearOff>(animatedTypes);
54 }
55
56 void SVGAnimatedPathAnimator::resetAnimValToBaseVal(const SVGElementAnimatedPropertyList& animatedTypes, SVGPathByteStream* byteStream)
57 {
58     SVGAnimatedPathSegListPropertyTearOff* property = castAnimatedPropertyToActualType<SVGAnimatedPathSegListPropertyTearOff>(animatedTypes[0].properties[0].get());
59     const SVGPathSegList& baseValue = property->currentBaseValue();
60
61     buildSVGPathByteStreamFromSVGPathSegList(baseValue, byteStream, UnalteredParsing);
62
63     Vector<RefPtr<SVGAnimatedPathSegListPropertyTearOff>> result;
64
65     for (auto& type : animatedTypes) {
66         auto* segment = castAnimatedPropertyToActualType<SVGAnimatedPathSegListPropertyTearOff>(type.properties[0].get());
67         if (segment->isAnimating())
68             continue;
69         result.append(segment);
70     }
71
72     if (!result.isEmpty()) {
73         SVGElement::InstanceUpdateBlocker blocker(*property->contextElement());
74         for (auto& segment : result)
75             segment->animationStarted(byteStream, &baseValue);
76     }
77 }
78
79 void SVGAnimatedPathAnimator::resetAnimValToBaseVal(const SVGElementAnimatedPropertyList& animatedTypes, SVGAnimatedType* type)
80 {
81     ASSERT(animatedTypes.size() >= 1);
82     ASSERT(type);
83     ASSERT(type->type() == m_type);
84     resetAnimValToBaseVal(animatedTypes, type->path());
85 }
86
87 void SVGAnimatedPathAnimator::animValWillChange(const SVGElementAnimatedPropertyList& animatedTypes)
88 {
89     animValWillChangeForType<SVGAnimatedPathSegListPropertyTearOff>(animatedTypes);
90 }
91
92 void SVGAnimatedPathAnimator::animValDidChange(const SVGElementAnimatedPropertyList& animatedTypes)
93 {
94     animValDidChangeForType<SVGAnimatedPathSegListPropertyTearOff>(animatedTypes);
95 }
96
97 void SVGAnimatedPathAnimator::addAnimatedTypes(SVGAnimatedType* from, SVGAnimatedType* to)
98 {
99     ASSERT(from->type() == AnimatedPath);
100     ASSERT(from->type() == to->type());
101
102     SVGPathByteStream* fromPath = from->path();
103     SVGPathByteStream* toPath = to->path();
104     unsigned fromPathSize = fromPath->size();
105     if (!fromPathSize || fromPathSize != toPath->size())
106         return;
107     addToSVGPathByteStream(toPath, fromPath);
108 }
109
110 void SVGAnimatedPathAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount, SVGAnimatedType* from, SVGAnimatedType* to, SVGAnimatedType* toAtEndOfDuration, SVGAnimatedType* animated)
111 {
112     ASSERT(m_animationElement);
113     ASSERT(m_contextElement);
114
115     SVGPathByteStream* fromPath = from->path();
116     SVGPathByteStream* toPath = to->path();
117     SVGPathByteStream* toAtEndOfDurationPath = toAtEndOfDuration->path();
118     SVGPathByteStream* animatedPath = animated->path();
119
120     std::unique_ptr<SVGPathByteStream> underlyingPath;
121     bool isToAnimation = m_animationElement->animationMode() == ToAnimation;
122     if (isToAnimation) {
123         underlyingPath = animatedPath->copy();
124         fromPath = underlyingPath.get();
125     }
126
127     // Cache the current animated value before the buildAnimatedSVGPathByteStream() clears animatedPath.
128     std::unique_ptr<SVGPathByteStream> lastAnimatedPath;
129     if (!fromPath->size() || (m_animationElement->isAdditive() && !isToAnimation))
130         lastAnimatedPath = animatedPath->copy();
131
132     // Pass false to 'resizeAnimatedListIfNeeded' here, as the path animation is not a regular Vector<SVGXXX> type, but a SVGPathByteStream, that works differently.
133     if (!m_animationElement->adjustFromToListValues<SVGPathByteStream>(*fromPath, *toPath, *animatedPath, percentage, false))
134         return;
135
136     buildAnimatedSVGPathByteStream(fromPath, toPath, animatedPath, percentage);
137
138     // Handle additive='sum'.
139     if (lastAnimatedPath)
140         addToSVGPathByteStream(animatedPath, lastAnimatedPath.get());
141
142     // Handle accumulate='sum'.
143     if (m_animationElement->isAccumulated() && repeatCount)
144         addToSVGPathByteStream(animatedPath, toAtEndOfDurationPath, repeatCount);
145 }
146
147 float SVGAnimatedPathAnimator::calculateDistance(const String&, const String&)
148 {
149     // FIXME: Support paced animations.
150     return -1;
151 }
152
153 }