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.
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"
40 SVGRect::SVGRect(const FloatRect
& 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
);
62 bool valid
= parseNumber(ptr
, end
, x
) && parseNumber(ptr
, end
, y
) && parseNumber(ptr
, end
, width
) && parseNumber(ptr
, end
, height
, DisallowWhitespace
);
65 exceptionState
.throwDOMException(SyntaxError
, "Problem parsing rect \"" + String(start
, end
- start
) + "\"");
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
) + "\"");
77 m_value
= FloatRect(x
, y
, width
, height
);
81 void SVGRect::setValueAsString(const String
& string
, ExceptionState
& exceptionState
)
83 if (string
.isNull()) {
87 if (string
.isEmpty()) {
88 m_value
= FloatRect(0.0f
, 0.0f
, 0.0f
, 0.0f
);
93 if (string
.is8Bit()) {
94 const LChar
* ptr
= string
.characters8();
95 const LChar
* end
= ptr
+ string
.length();
96 parse(ptr
, end
, exceptionState
);
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());
110 builder
.appendNumber(y());
112 builder
.appendNumber(width());
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.
148 void SVGRect::setInvalid()
150 m_value
= FloatRect(0.0f
, 0.0f
, 0.0f
, 0.0f
);