1/*
2 * Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 * Copyright (C) 2007 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB.  If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#include "config.h"
23
24#include "core/svg/SVGRect.h"
25
26#include "bindings/v8/ExceptionState.h"
27#include "core/dom/ExceptionCode.h"
28#include "core/svg/SVGAnimationElement.h"
29#include "core/svg/SVGParserUtilities.h"
30#include "wtf/text/StringBuilder.h"
31#include "wtf/text/WTFString.h"
32
33namespace WebCore {
34
35SVGRect::SVGRect()
36    : SVGPropertyBase(classType())
37    , m_isValid(true)
38{
39}
40
41SVGRect::SVGRect(InvalidSVGRectTag)
42    : SVGPropertyBase(classType())
43{
44    setInvalid();
45}
46
47SVGRect::SVGRect(const FloatRect& rect)
48    : SVGPropertyBase(classType())
49    , m_isValid(true)
50    , m_value(rect)
51{
52}
53
54PassRefPtr<SVGRect> SVGRect::clone() const
55{
56    return SVGRect::create(m_value);
57}
58
59PassRefPtr<SVGPropertyBase> SVGRect::cloneForAnimation(const String& value) const
60{
61    RefPtr<SVGRect> rect = SVGRect::create();
62    rect->setValueAsString(value, IGNORE_EXCEPTION);
63    return rect.release();
64}
65
66template<typename CharType>
67void SVGRect::parse(const CharType*& ptr, const CharType* end, ExceptionState& exceptionState)
68{
69    const CharType* start = ptr;
70
71    skipOptionalSVGSpaces(ptr, end);
72
73    float x = 0.0f;
74    float y = 0.0f;
75    float width = 0.0f;
76    float height = 0.0f;
77    bool valid = parseNumber(ptr, end, x) && parseNumber(ptr, end, y) && parseNumber(ptr, end, width) && parseNumber(ptr, end, height, DisallowWhitespace);
78
79    if (!valid) {
80        exceptionState.throwDOMException(SyntaxError, "Problem parsing rect \"" + String(start, end - start) + "\"");
81        setInvalid();
82        return;
83    }
84
85    skipOptionalSVGSpaces(ptr, end);
86    if (ptr < end) { // nothing should come after the last, fourth number
87        exceptionState.throwDOMException(SyntaxError, "Problem parsing rect \"" + String(start, end - start) + "\"");
88        setInvalid();
89        return;
90    }
91
92    m_value = FloatRect(x, y, width, height);
93    m_isValid = true;
94}
95
96void SVGRect::setValueAsString(const String& string, ExceptionState& exceptionState)
97{
98    if (string.isNull()) {
99        setInvalid();
100        return;
101    }
102    if (string.isEmpty()) {
103        m_value = FloatRect(0.0f, 0.0f, 0.0f, 0.0f);
104        m_isValid = true;
105        return;
106    }
107
108    if (string.is8Bit()) {
109        const LChar* ptr = string.characters8();
110        const LChar* end = ptr + string.length();
111        parse(ptr, end, exceptionState);
112        return;
113    }
114
115    const UChar* ptr = string.characters16();
116    const UChar* end = ptr + string.length();
117    parse(ptr, end, exceptionState);
118}
119
120String SVGRect::valueAsString() const
121{
122    StringBuilder builder;
123    builder.append(String::number(x()));
124    builder.append(' ');
125    builder.append(String::number(y()));
126    builder.append(' ');
127    builder.append(String::number(width()));
128    builder.append(' ');
129    builder.append(String::number(height()));
130    return builder.toString();
131}
132
133void SVGRect::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement*)
134{
135    m_value += toSVGRect(other)->value();
136}
137
138void SVGRect::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> fromValue, PassRefPtr<SVGPropertyBase> toValue, PassRefPtr<SVGPropertyBase> toAtEndOfDurationValue, SVGElement*)
139{
140    ASSERT(animationElement);
141    RefPtr<SVGRect> fromRect = animationElement->animationMode() == ToAnimation ? this : toSVGRect(fromValue);
142    RefPtr<SVGRect> toRect = toSVGRect(toValue);
143    RefPtr<SVGRect> toAtEndOfDurationRect = toSVGRect(toAtEndOfDurationValue);
144
145    float animatedX = x();
146    float animatedY = y();
147    float animatedWidth = width();
148    float animatedHeight = height();
149    animationElement->animateAdditiveNumber(percentage, repeatCount, fromRect->x(), toRect->x(), toAtEndOfDurationRect->x(), animatedX);
150    animationElement->animateAdditiveNumber(percentage, repeatCount, fromRect->y(), toRect->y(), toAtEndOfDurationRect->y(), animatedY);
151    animationElement->animateAdditiveNumber(percentage, repeatCount, fromRect->width(), toRect->width(), toAtEndOfDurationRect->width(), animatedWidth);
152    animationElement->animateAdditiveNumber(percentage, repeatCount, fromRect->height(), toRect->height(), toAtEndOfDurationRect->height(), animatedHeight);
153
154    m_value = FloatRect(animatedX, animatedY, animatedWidth, animatedHeight);
155}
156
157float SVGRect::calculateDistance(PassRefPtr<SVGPropertyBase> to, SVGElement* contextElement)
158{
159    // FIXME: Distance calculation is not possible for SVGRect right now. We need the distance for every single value.
160    return -1;
161}
162
163void SVGRect::setInvalid()
164{
165    m_value = FloatRect(0.0f, 0.0f, 0.0f, 0.0f);
166    m_isValid = false;
167}
168
169}
170