Manage SVGPathByteStream through std::unique_ptr
[WebKit-https.git] / Source / WebCore / svg / SVGAnimatedType.cpp
1 /*
2  * Copyright (C) Research In Motion Limited 2011. 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 "SVGAnimatedType.h"
24
25 #include "SVGParserUtilities.h"
26 #include "SVGPathByteStream.h"
27
28 namespace WebCore {
29
30 SVGAnimatedType::SVGAnimatedType(AnimatedPropertyType type)
31     : m_type(type)
32 {
33 }
34
35 SVGAnimatedType::~SVGAnimatedType()
36 {
37     switch (m_type) {
38     case AnimatedAngle:
39         delete m_data.angleAndEnumeration;
40         break;
41     case AnimatedBoolean:
42         delete m_data.boolean;
43         break;
44     case AnimatedColor:
45         delete m_data.color;
46         break;
47     case AnimatedEnumeration:
48         delete m_data.enumeration;
49         break;
50     case AnimatedInteger:
51         delete m_data.integer;
52         break;
53     case AnimatedIntegerOptionalInteger:
54         delete m_data.integerOptionalInteger;
55         break;
56     case AnimatedLength:
57         delete m_data.length;
58         break;
59     case AnimatedLengthList:
60         delete m_data.lengthList;
61         break;
62     case AnimatedNumber:
63         delete m_data.number;
64         break;
65     case AnimatedNumberList:
66         delete m_data.numberList;
67         break;
68     case AnimatedNumberOptionalNumber:
69         delete m_data.numberOptionalNumber;
70         break;
71     case AnimatedPath:
72         delete m_data.path;
73         break;
74     case AnimatedPoints:
75         delete m_data.pointList;
76         break;
77     case AnimatedPreserveAspectRatio:
78         delete m_data.preserveAspectRatio;
79         break;
80     case AnimatedRect:
81         delete m_data.rect;
82         break;
83     case AnimatedString:
84         delete m_data.string;
85         break;
86     case AnimatedTransformList:
87         delete m_data.transformList;
88         break;
89     case AnimatedUnknown:
90         ASSERT_NOT_REACHED();
91         break;
92     }
93 }
94
95 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createAngleAndEnumeration(std::pair<SVGAngle, unsigned>* angleAndEnumeration)
96 {
97     ASSERT(angleAndEnumeration);
98     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedAngle));
99     animatedType->m_data.angleAndEnumeration = angleAndEnumeration;
100     return animatedType.release();
101 }
102
103 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createBoolean(bool* boolean)
104 {
105     ASSERT(boolean);
106     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedBoolean));
107     animatedType->m_data.boolean = boolean;
108     return animatedType.release();
109 }
110
111 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createColor(Color* color)
112 {
113     ASSERT(color);
114     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedColor));
115     animatedType->m_data.color = color;
116     return animatedType.release();
117 }
118
119 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createEnumeration(unsigned* enumeration)
120 {
121     ASSERT(enumeration);
122     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedEnumeration));
123     animatedType->m_data.enumeration = enumeration;
124     return animatedType.release();
125 }
126
127 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createInteger(int* integer)
128 {
129     ASSERT(integer);
130     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedInteger));
131     animatedType->m_data.integer = integer;
132     return animatedType.release();
133 }
134
135 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createIntegerOptionalInteger(pair<int, int>* integerOptionalInteger)
136 {
137     ASSERT(integerOptionalInteger);
138     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedIntegerOptionalInteger));
139     animatedType->m_data.integerOptionalInteger = integerOptionalInteger;
140     return animatedType.release();
141 }
142
143 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createLength(SVGLength* length)
144 {
145     ASSERT(length);
146     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedLength));
147     animatedType->m_data.length = length;
148     return animatedType.release();
149 }
150
151 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createLengthList(SVGLengthList* lengthList)
152 {
153     ASSERT(lengthList);
154     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedLengthList));
155     animatedType->m_data.lengthList = lengthList;
156     return animatedType.release();
157 }
158
159 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createNumber(float* number)
160 {
161     ASSERT(number);
162     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedNumber));
163     animatedType->m_data.number = number;
164     return animatedType.release();
165 }
166
167 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createNumberList(SVGNumberList* numberList)
168 {
169     ASSERT(numberList);
170     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedNumberList));
171     animatedType->m_data.numberList = numberList;
172     return animatedType.release();
173 }
174
175 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createNumberOptionalNumber(pair<float, float>* numberOptionalNumber)
176 {
177     ASSERT(numberOptionalNumber);
178     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedNumberOptionalNumber));
179     animatedType->m_data.numberOptionalNumber = numberOptionalNumber;
180     return animatedType.release();
181 }
182
183 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createPath(std::unique_ptr<SVGPathByteStream> path)
184 {
185     ASSERT(path);
186     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedPath));
187     animatedType->m_data.path = path.release();
188     return animatedType.release();
189 }
190
191 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createPointList(SVGPointList* pointList)
192 {
193     ASSERT(pointList);
194     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedPoints));
195     animatedType->m_data.pointList = pointList;
196     return animatedType.release();
197 }
198
199 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createPreserveAspectRatio(SVGPreserveAspectRatio* preserveAspectRatio)
200 {
201     ASSERT(preserveAspectRatio);
202     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedPreserveAspectRatio));
203     animatedType->m_data.preserveAspectRatio = preserveAspectRatio;
204     return animatedType.release();
205 }
206
207 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createRect(FloatRect* rect)
208 {
209     ASSERT(rect);
210     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedRect));
211     animatedType->m_data.rect = rect;
212     return animatedType.release();
213 }
214
215 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createString(String* string)
216 {
217     ASSERT(string);
218     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedString));
219     animatedType->m_data.string = string;
220     return animatedType.release();
221 }
222
223 PassOwnPtr<SVGAnimatedType> SVGAnimatedType::createTransformList(SVGTransformList* transformList)
224 {
225     ASSERT(transformList);
226     OwnPtr<SVGAnimatedType> animatedType = adoptPtr(new SVGAnimatedType(AnimatedTransformList));
227     animatedType->m_data.transformList = transformList;
228     return animatedType.release();
229 }
230
231 String SVGAnimatedType::valueAsString()
232 {
233     switch (m_type) {
234     case AnimatedColor:
235         ASSERT(m_data.color);
236         return m_data.color->serialized();
237     case AnimatedLength:
238         ASSERT(m_data.length);
239         return m_data.length->valueAsString();
240     case AnimatedLengthList:
241         ASSERT(m_data.lengthList);
242         return m_data.lengthList->valueAsString();
243     case AnimatedNumber:
244         ASSERT(m_data.number);
245         return String::number(*m_data.number);
246     case AnimatedRect:
247         ASSERT(m_data.rect);
248         return String::number(m_data.rect->x()) + ' ' + String::number(m_data.rect->y()) + ' '
249              + String::number(m_data.rect->width()) + ' ' + String::number(m_data.rect->height());
250     case AnimatedString:
251         ASSERT(m_data.string);
252         return *m_data.string;
253
254     // These types don't appear in the table in SVGElement::cssPropertyToTypeMap() and thus don't need valueAsString() support.
255     case AnimatedAngle:
256     case AnimatedBoolean:
257     case AnimatedEnumeration:
258     case AnimatedInteger:
259     case AnimatedIntegerOptionalInteger:
260     case AnimatedNumberList:
261     case AnimatedNumberOptionalNumber:
262     case AnimatedPath:
263     case AnimatedPoints:
264     case AnimatedPreserveAspectRatio:
265     case AnimatedTransformList:
266     case AnimatedUnknown:
267         // Only SVG DOM animations use these property types - that means valueAsString() is never used for those.
268         ASSERT_NOT_REACHED();
269         break;
270     }
271     ASSERT_NOT_REACHED();
272     return String();
273 }
274
275 bool SVGAnimatedType::setValueAsString(const QualifiedName& attrName, const String& value)
276 {
277     switch (m_type) {
278     case AnimatedColor:
279         ASSERT(m_data.color);
280         *m_data.color = value.isEmpty() ? Color() : SVGColor::colorFromRGBColorString(value);
281         break;
282     case AnimatedLength: {
283         ASSERT(m_data.length);
284         ExceptionCode ec = 0;
285         m_data.length->setValueAsString(value, SVGLength::lengthModeForAnimatedLengthAttribute(attrName), ec);
286         return !ec;
287     }
288     case AnimatedLengthList:
289         ASSERT(m_data.lengthList);
290         m_data.lengthList->parse(value, SVGLength::lengthModeForAnimatedLengthAttribute(attrName));
291         break;
292     case AnimatedNumber:
293         ASSERT(m_data.number);
294         parseNumberFromString(value, *m_data.number);
295         break;
296     case AnimatedRect:
297         ASSERT(m_data.rect);
298         parseRect(value, *m_data.rect);
299         break;
300     case AnimatedString:
301         ASSERT(m_data.string);
302         *m_data.string = value;
303         break;
304
305     // These types don't appear in the table in SVGElement::cssPropertyToTypeMap() and thus don't need setValueAsString() support. 
306     case AnimatedAngle:
307     case AnimatedBoolean:
308     case AnimatedEnumeration:
309     case AnimatedInteger:
310     case AnimatedIntegerOptionalInteger:
311     case AnimatedNumberList:
312     case AnimatedNumberOptionalNumber:
313     case AnimatedPath:
314     case AnimatedPoints:
315     case AnimatedPreserveAspectRatio:
316     case AnimatedTransformList:
317     case AnimatedUnknown:
318         // Only SVG DOM animations use these property types - that means setValueAsString() is never used for those.
319         ASSERT_NOT_REACHED();
320         break;
321     }
322     return true;
323 }
324
325 bool SVGAnimatedType::supportsAnimVal(AnimatedPropertyType type)
326 {
327     // AnimatedColor is only used for CSS property animations.
328     return type != AnimatedUnknown && type != AnimatedColor;
329 }
330
331 } // namespace WebCore
332
333 #endif // ENABLE(SVG)