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