2 * Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4 * Copyright (C) Research In Motion Limited 2010. 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.
23 #include "core/svg/SVGAngle.h"
25 #include "bindings/core/v8/ExceptionState.h"
26 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
27 #include "core/dom/ExceptionCode.h"
28 #include "core/svg/SVGAnimationElement.h"
29 #include "core/svg/SVGParserUtilities.h"
30 #include "wtf/MathExtras.h"
31 #include "wtf/text/WTFString.h"
35 template<> const SVGEnumerationStringEntries& getStaticStringEntries<SVGMarkerOrientType>()
37 DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
38 if (entries.isEmpty()) {
39 entries.append(std::make_pair(SVGMarkerOrientAuto, "auto"));
40 entries.append(std::make_pair(SVGMarkerOrientAngle, "angle"));
45 SVGMarkerOrientEnumeration::SVGMarkerOrientEnumeration(SVGAngle* angle)
46 : SVGEnumeration<SVGMarkerOrientType>(SVGMarkerOrientAngle)
51 SVGMarkerOrientEnumeration::~SVGMarkerOrientEnumeration()
55 void SVGMarkerOrientEnumeration::notifyChange()
58 m_angle->orientTypeChanged();
61 void SVGMarkerOrientEnumeration::add(PassRefPtrWillBeRawPtr<SVGPropertyBase>, SVGElement*)
63 // SVGMarkerOrientEnumeration is only animated via SVGAngle
67 void SVGMarkerOrientEnumeration::calculateAnimatedValue(SVGAnimationElement*, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> from, PassRefPtr<SVGPropertyBase> to, PassRefPtr<SVGPropertyBase> toAtEndOfDurationValue, SVGElement* contextElement)
69 // SVGMarkerOrientEnumeration is only animated via SVGAngle
73 float SVGMarkerOrientEnumeration::calculateDistance(PassRefPtr<SVGPropertyBase> to, SVGElement* contextElement)
75 // SVGMarkerOrientEnumeration is only animated via SVGAngle
81 : m_unitType(SVG_ANGLETYPE_UNSPECIFIED)
82 , m_valueInSpecifiedUnits(0)
83 , m_orientType(SVGMarkerOrientEnumeration::create(this))
87 SVGAngle::SVGAngle(SVGAngleType unitType, float valueInSpecifiedUnits, SVGMarkerOrientType orientType)
88 : m_unitType(unitType)
89 , m_valueInSpecifiedUnits(valueInSpecifiedUnits)
90 , m_orientType(SVGMarkerOrientEnumeration::create(this))
92 m_orientType->setEnumValue(orientType);
99 PassRefPtr<SVGAngle> SVGAngle::clone() const
101 return adoptRef(new SVGAngle(m_unitType, m_valueInSpecifiedUnits, m_orientType->enumValue()));
104 float SVGAngle::value() const
106 switch (m_unitType) {
107 case SVG_ANGLETYPE_GRAD:
108 return grad2deg(m_valueInSpecifiedUnits);
109 case SVG_ANGLETYPE_RAD:
110 return rad2deg(m_valueInSpecifiedUnits);
111 case SVG_ANGLETYPE_TURN:
112 return turn2deg(m_valueInSpecifiedUnits);
113 case SVG_ANGLETYPE_UNSPECIFIED:
114 case SVG_ANGLETYPE_UNKNOWN:
115 case SVG_ANGLETYPE_DEG:
116 return m_valueInSpecifiedUnits;
119 ASSERT_NOT_REACHED();
123 void SVGAngle::setValue(float value)
125 switch (m_unitType) {
126 case SVG_ANGLETYPE_GRAD:
127 m_valueInSpecifiedUnits = deg2grad(value);
129 case SVG_ANGLETYPE_RAD:
130 m_valueInSpecifiedUnits = deg2rad(value);
132 case SVG_ANGLETYPE_TURN:
133 m_valueInSpecifiedUnits = deg2turn(value);
135 case SVG_ANGLETYPE_UNSPECIFIED:
136 case SVG_ANGLETYPE_UNKNOWN:
137 case SVG_ANGLETYPE_DEG:
138 m_valueInSpecifiedUnits = value;
141 m_orientType->setEnumValue(SVGMarkerOrientAngle);
144 template<typename CharType>
145 static SVGAngle::SVGAngleType stringToAngleType(const CharType*& ptr, const CharType* end)
147 // If there's no unit given, the angle type is unspecified.
149 return SVGAngle::SVG_ANGLETYPE_UNSPECIFIED;
151 SVGAngle::SVGAngleType type = SVGAngle::SVG_ANGLETYPE_UNKNOWN;
152 const CharType firstChar = *ptr++;
154 if (isHTMLSpace<CharType>(firstChar)) {
155 type = SVGAngle::SVG_ANGLETYPE_UNSPECIFIED;
156 } else if (end - ptr >= 2) {
157 const CharType secondChar = *ptr++;
158 const CharType thirdChar = *ptr++;
159 if (firstChar == 'd' && secondChar == 'e' && thirdChar == 'g') {
160 type = SVGAngle::SVG_ANGLETYPE_DEG;
161 } else if (firstChar == 'r' && secondChar == 'a' && thirdChar == 'd') {
162 type = SVGAngle::SVG_ANGLETYPE_RAD;
163 } else if (ptr != end) {
164 const CharType fourthChar = *ptr++;
165 if (firstChar == 'g' && secondChar == 'r' && thirdChar == 'a' && fourthChar == 'd')
166 type = SVGAngle::SVG_ANGLETYPE_GRAD;
167 else if (firstChar == 't' && secondChar == 'u' && thirdChar == 'r' && fourthChar == 'n')
168 type = SVGAngle::SVG_ANGLETYPE_TURN;
172 if (!skipOptionalSVGSpaces(ptr, end))
175 return SVGAngle::SVG_ANGLETYPE_UNKNOWN;
178 String SVGAngle::valueAsString() const
180 switch (m_unitType) {
181 case SVG_ANGLETYPE_DEG: {
182 DEFINE_STATIC_LOCAL(String, degString, ("deg"));
183 return String::number(m_valueInSpecifiedUnits) + degString;
185 case SVG_ANGLETYPE_RAD: {
186 DEFINE_STATIC_LOCAL(String, radString, ("rad"));
187 return String::number(m_valueInSpecifiedUnits) + radString;
189 case SVG_ANGLETYPE_GRAD: {
190 DEFINE_STATIC_LOCAL(String, gradString, ("grad"));
191 return String::number(m_valueInSpecifiedUnits) + gradString;
193 case SVG_ANGLETYPE_TURN: {
194 DEFINE_STATIC_LOCAL(String, turnString, ("turn"));
195 return String::number(m_valueInSpecifiedUnits) + turnString;
197 case SVG_ANGLETYPE_UNSPECIFIED:
198 case SVG_ANGLETYPE_UNKNOWN:
199 return String::number(m_valueInSpecifiedUnits);
202 ASSERT_NOT_REACHED();
206 template<typename CharType>
207 static bool parseValue(const String& value, float& valueInSpecifiedUnits, SVGAngle::SVGAngleType& unitType)
209 const CharType* ptr = value.getCharacters<CharType>();
210 const CharType* end = ptr + value.length();
212 if (!parseNumber(ptr, end, valueInSpecifiedUnits, AllowLeadingWhitespace))
215 unitType = stringToAngleType(ptr, end);
216 if (unitType == SVGAngle::SVG_ANGLETYPE_UNKNOWN)
222 void SVGAngle::setValueAsString(const String& value, ExceptionState& exceptionState)
224 if (value.isEmpty()) {
225 newValueSpecifiedUnits(SVG_ANGLETYPE_UNSPECIFIED, 0);
229 if (value == "auto") {
230 newValueSpecifiedUnits(SVG_ANGLETYPE_UNSPECIFIED, 0);
231 m_orientType->setEnumValue(SVGMarkerOrientAuto);
235 float valueInSpecifiedUnits = 0;
236 SVGAngleType unitType = SVG_ANGLETYPE_UNKNOWN;
238 bool success = value.is8Bit() ? parseValue<LChar>(value, valueInSpecifiedUnits, unitType)
239 : parseValue<UChar>(value, valueInSpecifiedUnits, unitType);
241 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + value + "') is invalid.");
245 m_orientType->setEnumValue(SVGMarkerOrientAngle);
246 m_unitType = unitType;
247 m_valueInSpecifiedUnits = valueInSpecifiedUnits;
250 void SVGAngle::newValueSpecifiedUnits(SVGAngleType unitType, float valueInSpecifiedUnits)
252 m_orientType->setEnumValue(SVGMarkerOrientAngle);
253 m_unitType = unitType;
254 m_valueInSpecifiedUnits = valueInSpecifiedUnits;
257 void SVGAngle::convertToSpecifiedUnits(SVGAngleType unitType, ExceptionState& exceptionState)
259 if (m_unitType == SVG_ANGLETYPE_UNKNOWN) {
260 exceptionState.throwDOMException(NotSupportedError, "Cannot convert from unknown or invalid units.");
264 if (unitType == m_unitType)
267 switch (m_unitType) {
268 case SVG_ANGLETYPE_TURN:
270 case SVG_ANGLETYPE_GRAD:
271 m_valueInSpecifiedUnits = turn2grad(m_valueInSpecifiedUnits);
273 case SVG_ANGLETYPE_UNSPECIFIED:
274 case SVG_ANGLETYPE_DEG:
275 m_valueInSpecifiedUnits = turn2deg(m_valueInSpecifiedUnits);
277 case SVG_ANGLETYPE_RAD:
278 m_valueInSpecifiedUnits = deg2rad(turn2deg(m_valueInSpecifiedUnits));
280 case SVG_ANGLETYPE_TURN:
281 case SVG_ANGLETYPE_UNKNOWN:
282 ASSERT_NOT_REACHED();
286 case SVG_ANGLETYPE_RAD:
288 case SVG_ANGLETYPE_GRAD:
289 m_valueInSpecifiedUnits = rad2grad(m_valueInSpecifiedUnits);
291 case SVG_ANGLETYPE_UNSPECIFIED:
292 case SVG_ANGLETYPE_DEG:
293 m_valueInSpecifiedUnits = rad2deg(m_valueInSpecifiedUnits);
295 case SVG_ANGLETYPE_TURN:
296 m_valueInSpecifiedUnits = deg2turn(rad2deg(m_valueInSpecifiedUnits));
298 case SVG_ANGLETYPE_RAD:
299 case SVG_ANGLETYPE_UNKNOWN:
300 ASSERT_NOT_REACHED();
304 case SVG_ANGLETYPE_GRAD:
306 case SVG_ANGLETYPE_RAD:
307 m_valueInSpecifiedUnits = grad2rad(m_valueInSpecifiedUnits);
309 case SVG_ANGLETYPE_UNSPECIFIED:
310 case SVG_ANGLETYPE_DEG:
311 m_valueInSpecifiedUnits = grad2deg(m_valueInSpecifiedUnits);
313 case SVG_ANGLETYPE_TURN:
314 m_valueInSpecifiedUnits = grad2turn(m_valueInSpecifiedUnits);
316 case SVG_ANGLETYPE_GRAD:
317 case SVG_ANGLETYPE_UNKNOWN:
318 ASSERT_NOT_REACHED();
322 case SVG_ANGLETYPE_UNSPECIFIED:
323 // Spec: For angles, a unitless value is treated the same as if degrees were specified.
324 case SVG_ANGLETYPE_DEG:
326 case SVG_ANGLETYPE_RAD:
327 m_valueInSpecifiedUnits = deg2rad(m_valueInSpecifiedUnits);
329 case SVG_ANGLETYPE_GRAD:
330 m_valueInSpecifiedUnits = deg2grad(m_valueInSpecifiedUnits);
332 case SVG_ANGLETYPE_TURN:
333 m_valueInSpecifiedUnits = deg2turn(m_valueInSpecifiedUnits);
335 case SVG_ANGLETYPE_UNSPECIFIED:
336 case SVG_ANGLETYPE_DEG:
338 case SVG_ANGLETYPE_UNKNOWN:
339 ASSERT_NOT_REACHED();
343 case SVG_ANGLETYPE_UNKNOWN:
344 ASSERT_NOT_REACHED();
348 m_unitType = unitType;
349 m_orientType->setEnumValue(SVGMarkerOrientAngle);
352 void SVGAngle::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement*)
354 RefPtr<SVGAngle> otherAngle = toSVGAngle(other);
356 // Only respect by animations, if from and by are both specified in angles (and not eg. 'auto').
357 if (orientType()->enumValue() != SVGMarkerOrientAngle || otherAngle->orientType()->enumValue() != SVGMarkerOrientAngle)
360 setValue(value() + otherAngle->value());
363 void SVGAngle::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> from, PassRefPtr<SVGPropertyBase> to, PassRefPtr<SVGPropertyBase> toAtEndOfDuration, SVGElement*)
365 ASSERT(animationElement);
366 bool isToAnimation = animationElement->animationMode() == ToAnimation;
368 RefPtr<SVGAngle> fromAngle = isToAnimation ? this : toSVGAngle(from);
369 RefPtr<SVGAngle> toAngle = toSVGAngle(to);
370 RefPtr<SVGAngle> toAtEndOfDurationAngle = toSVGAngle(toAtEndOfDuration);
372 SVGMarkerOrientType fromOrientType = fromAngle->orientType()->enumValue();
373 SVGMarkerOrientType toOrientType = toAngle->orientType()->enumValue();
375 if (fromOrientType != toOrientType) {
376 // Animating from eg. auto to 90deg, or auto to 90deg.
377 if (fromOrientType == SVGMarkerOrientAngle) {
378 // Animating from an angle value to eg. 'auto' - this disabled additive as 'auto' is a keyword..
379 if (toOrientType == SVGMarkerOrientAuto) {
380 if (percentage < 0.5f) {
381 newValueSpecifiedUnits(fromAngle->unitType(), fromAngle->valueInSpecifiedUnits());
384 orientType()->setEnumValue(SVGMarkerOrientAuto);
387 m_valueInSpecifiedUnits = 0;
388 orientType()->setEnumValue(SVGMarkerOrientUnknown);
393 // From 'auto' to 'auto'.
394 if (fromOrientType == SVGMarkerOrientAuto) {
395 m_valueInSpecifiedUnits = 0;
396 orientType()->setEnumValue(SVGMarkerOrientAuto);
400 // If the enumeration value is not angle or auto, its unknown.
401 if (fromOrientType != SVGMarkerOrientAngle) {
402 m_valueInSpecifiedUnits = 0;
403 orientType()->setEnumValue(SVGMarkerOrientUnknown);
407 // Regular from angle to angle animation, with all features like additive etc.
408 float animatedValue = value();
409 animationElement->animateAdditiveNumber(percentage, repeatCount, fromAngle->value(), toAngle->value(), toAtEndOfDurationAngle->value(), animatedValue);
410 orientType()->setEnumValue(SVGMarkerOrientAngle);
411 setValue(animatedValue);
414 float SVGAngle::calculateDistance(PassRefPtr<SVGPropertyBase> other, SVGElement*)
416 return fabsf(value() - toSVGAngle(other)->value());
419 void SVGAngle::orientTypeChanged()
421 if (orientType()->enumValue() == SVGMarkerOrientAuto) {
422 m_unitType = SVG_ANGLETYPE_UNSPECIFIED;
423 m_valueInSpecifiedUnits = 0;