Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / svg / SVGRect.cpp
blobb7139338a3bf35935da4445e5090b9f852a08d9e
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.
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.
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.
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.
22 #include "config.h"
24 #include "core/svg/SVGRect.h"
26 #include "bindings/core/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"
33 namespace blink {
35 SVGRect::SVGRect()
36 : m_isValid(true)
40 SVGRect::SVGRect(const FloatRect& rect)
41 : m_isValid(true)
42 , m_value(rect)
46 PassRefPtrWillBeRawPtr<SVGRect> SVGRect::clone() const
48 return SVGRect::create(m_value);
51 template<typename CharType>
52 void SVGRect::parse(const CharType*& ptr, const CharType* end, ExceptionState& exceptionState)
54 const CharType* start = ptr;
56 skipOptionalSVGSpaces(ptr, end);
58 float x = 0.0f;
59 float y = 0.0f;
60 float width = 0.0f;
61 float height = 0.0f;
62 bool valid = parseNumber(ptr, end, x) && parseNumber(ptr, end, y) && parseNumber(ptr, end, width) && parseNumber(ptr, end, height, DisallowWhitespace);
64 if (!valid) {
65 exceptionState.throwDOMException(SyntaxError, "Problem parsing rect \"" + String(start, end - start) + "\"");
66 setInvalid();
67 return;
70 skipOptionalSVGSpaces(ptr, end);
71 if (ptr < end) { // nothing should come after the last, fourth number
72 exceptionState.throwDOMException(SyntaxError, "Problem parsing rect \"" + String(start, end - start) + "\"");
73 setInvalid();
74 return;
77 m_value = FloatRect(x, y, width, height);
78 m_isValid = true;
81 void SVGRect::setValueAsString(const String& string, ExceptionState& exceptionState)
83 if (string.isNull()) {
84 setInvalid();
85 return;
87 if (string.isEmpty()) {
88 m_value = FloatRect(0.0f, 0.0f, 0.0f, 0.0f);
89 m_isValid = true;
90 return;
93 if (string.is8Bit()) {
94 const LChar* ptr = string.characters8();
95 const LChar* end = ptr + string.length();
96 parse(ptr, end, exceptionState);
97 return;
100 const UChar* ptr = string.characters16();
101 const UChar* end = ptr + string.length();
102 parse(ptr, end, exceptionState);
105 String SVGRect::valueAsString() const
107 StringBuilder builder;
108 builder.appendNumber(x());
109 builder.append(' ');
110 builder.appendNumber(y());
111 builder.append(' ');
112 builder.appendNumber(width());
113 builder.append(' ');
114 builder.appendNumber(height());
115 return builder.toString();
118 void SVGRect::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement*)
120 m_value += toSVGRect(other)->value();
123 void SVGRect::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtrWillBeRawPtr<SVGPropertyBase> fromValue, PassRefPtrWillBeRawPtr<SVGPropertyBase> toValue, PassRefPtrWillBeRawPtr<SVGPropertyBase> toAtEndOfDurationValue, SVGElement*)
125 ASSERT(animationElement);
126 RefPtrWillBeRawPtr<SVGRect> fromRect = animationElement->animationMode() == ToAnimation ? PassRefPtrWillBeRawPtr<SVGRect>(this) : toSVGRect(fromValue);
127 RefPtrWillBeRawPtr<SVGRect> toRect = toSVGRect(toValue);
128 RefPtrWillBeRawPtr<SVGRect> toAtEndOfDurationRect = toSVGRect(toAtEndOfDurationValue);
130 float animatedX = x();
131 float animatedY = y();
132 float animatedWidth = width();
133 float animatedHeight = height();
134 animationElement->animateAdditiveNumber(percentage, repeatCount, fromRect->x(), toRect->x(), toAtEndOfDurationRect->x(), animatedX);
135 animationElement->animateAdditiveNumber(percentage, repeatCount, fromRect->y(), toRect->y(), toAtEndOfDurationRect->y(), animatedY);
136 animationElement->animateAdditiveNumber(percentage, repeatCount, fromRect->width(), toRect->width(), toAtEndOfDurationRect->width(), animatedWidth);
137 animationElement->animateAdditiveNumber(percentage, repeatCount, fromRect->height(), toRect->height(), toAtEndOfDurationRect->height(), animatedHeight);
139 m_value = FloatRect(animatedX, animatedY, animatedWidth, animatedHeight);
142 float SVGRect::calculateDistance(PassRefPtrWillBeRawPtr<SVGPropertyBase> to, SVGElement* contextElement)
144 // FIXME: Distance calculation is not possible for SVGRect right now. We need the distance for every single value.
145 return -1;
148 void SVGRect::setInvalid()
150 m_value = FloatRect(0.0f, 0.0f, 0.0f, 0.0f);
151 m_isValid = false;