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/SVGLength.h"
26 #include "bindings/core/v8/ExceptionState.h"
27 #include "core/SVGNames.h"
28 #include "core/css/CSSPrimitiveValue.h"
29 #include "core/dom/ExceptionCode.h"
30 #include "core/svg/SVGAnimationElement.h"
31 #include "core/svg/SVGParserUtilities.h"
32 #include "platform/animation/AnimationUtilities.h"
33 #include "wtf/MathExtras.h"
34 #include "wtf/text/WTFString.h"
40 inline const char* lengthTypeToString(SVGLengthType type)
43 case LengthTypeUnknown:
44 case LengthTypeNumber:
46 case LengthTypePercentage:
70 template<typename CharType>
71 SVGLengthType stringToLengthType(const CharType*& ptr, const CharType* end)
74 return LengthTypeNumber;
76 SVGLengthType type = LengthTypeUnknown;
77 const CharType firstChar = *ptr++;
79 if (firstChar == '%') {
80 type = LengthTypePercentage;
81 } else if (isHTMLSpace<CharType>(firstChar)) {
82 type = LengthTypeNumber;
83 } else if (ptr < end) {
84 const CharType secondChar = *ptr++;
86 if (firstChar == 'p') {
87 if (secondChar == 'x')
89 if (secondChar == 't')
91 if (secondChar == 'c')
93 } else if (firstChar == 'e') {
94 if (secondChar == 'm')
96 if (secondChar == 'x')
98 } else if (firstChar == 'c' && secondChar == 'm') {
100 } else if (firstChar == 'm' && secondChar == 'm') {
102 } else if (firstChar == 'i' && secondChar == 'n') {
104 } else if (isHTMLSpace<CharType>(firstChar) && isHTMLSpace<CharType>(secondChar)) {
105 type = LengthTypeNumber;
109 if (!skipOptionalSVGSpaces(ptr, end))
112 return LengthTypeUnknown;
117 SVGLength::SVGLength(SVGLengthMode mode)
118 : SVGPropertyBase(classType())
119 , m_valueInSpecifiedUnits(0)
121 , m_unitType(LengthTypeNumber)
125 SVGLength::SVGLength(const SVGLength& o)
126 : SVGPropertyBase(classType())
127 , m_valueInSpecifiedUnits(o.m_valueInSpecifiedUnits)
128 , m_unitMode(o.m_unitMode)
129 , m_unitType(o.m_unitType)
133 PassRefPtr<SVGLength> SVGLength::clone() const
135 return adoptRef(new SVGLength(*this));
138 PassRefPtr<SVGPropertyBase> SVGLength::cloneForAnimation(const String& value) const
140 RefPtr<SVGLength> length = create();
142 length->m_unitMode = m_unitMode;
143 length->m_unitType = m_unitType;
145 TrackExceptionState exceptionState;
146 length->setValueAsString(value, exceptionState);
147 if (exceptionState.hadException()) {
148 length->m_unitType = LengthTypeNumber;
149 length->m_valueInSpecifiedUnits = 0;
152 return length.release();
155 bool SVGLength::operator==(const SVGLength& other) const
157 return m_unitMode == other.m_unitMode
158 && m_unitType == other.m_unitType
159 && m_valueInSpecifiedUnits == other.m_valueInSpecifiedUnits;
162 float SVGLength::value(const SVGLengthContext& context, ExceptionState& es) const
164 return context.convertValueToUserUnits(m_valueInSpecifiedUnits, unitMode(), unitType(), es);
167 void SVGLength::setValue(float value, const SVGLengthContext& context, ExceptionState& es)
169 // 100% = 100.0 instead of 1.0 for historical reasons, this could eventually be changed
170 if (m_unitType == LengthTypePercentage)
173 float convertedValue = context.convertValueFromUserUnits(value, unitMode(), unitType(), es);
174 if (es.hadException())
177 m_valueInSpecifiedUnits = convertedValue;
180 void SVGLength::setUnitType(SVGLengthType type)
182 ASSERT(type != LengthTypeUnknown && type <= LengthTypePC);
186 float SVGLength::valueAsPercentage() const
188 // 100% = 100.0 instead of 1.0 for historical reasons, this could eventually be changed
189 if (m_unitType == LengthTypePercentage)
190 return m_valueInSpecifiedUnits / 100;
192 return m_valueInSpecifiedUnits;
195 template<typename CharType>
196 static bool parseValueInternal(const String& string, float& convertedNumber, SVGLengthType& type)
198 const CharType* ptr = string.getCharacters<CharType>();
199 const CharType* end = ptr + string.length();
201 if (!parseNumber(ptr, end, convertedNumber, AllowLeadingWhitespace))
204 type = stringToLengthType(ptr, end);
206 if (type == LengthTypeUnknown)
212 void SVGLength::setValueAsString(const String& string, ExceptionState& exceptionState)
214 if (string.isEmpty()) {
215 m_unitType = LengthTypeNumber;
216 m_valueInSpecifiedUnits = 0;
220 float convertedNumber = 0;
221 SVGLengthType type = LengthTypeUnknown;
223 bool success = string.is8Bit() ?
224 parseValueInternal<LChar>(string, convertedNumber, type) :
225 parseValueInternal<UChar>(string, convertedNumber, type);
228 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + string + "') is invalid.");
233 m_valueInSpecifiedUnits = convertedNumber;
236 String SVGLength::valueAsString() const
238 return String::number(m_valueInSpecifiedUnits) + lengthTypeToString(unitType());
241 void SVGLength::newValueSpecifiedUnits(SVGLengthType type, float value)
244 m_valueInSpecifiedUnits = value;
247 void SVGLength::convertToSpecifiedUnits(SVGLengthType type, const SVGLengthContext& context, ExceptionState& exceptionState)
249 ASSERT(type != LengthTypeUnknown && type <= LengthTypePC);
251 float valueInUserUnits = value(context, exceptionState);
252 if (exceptionState.hadException())
255 SVGLengthType originalType = unitType();
257 setValue(valueInUserUnits, context, exceptionState);
258 if (!exceptionState.hadException())
261 // Eventually restore old unit and type
262 m_unitType = originalType;
265 PassRefPtr<SVGLength> SVGLength::fromCSSPrimitiveValue(CSSPrimitiveValue* value)
269 SVGLengthType svgType;
270 switch (value->primitiveType()) {
271 case CSSPrimitiveValue::CSS_NUMBER:
272 svgType = LengthTypeNumber;
274 case CSSPrimitiveValue::CSS_PERCENTAGE:
275 svgType = LengthTypePercentage;
277 case CSSPrimitiveValue::CSS_EMS:
278 svgType = LengthTypeEMS;
280 case CSSPrimitiveValue::CSS_EXS:
281 svgType = LengthTypeEXS;
283 case CSSPrimitiveValue::CSS_PX:
284 svgType = LengthTypePX;
286 case CSSPrimitiveValue::CSS_CM:
287 svgType = LengthTypeCM;
289 case CSSPrimitiveValue::CSS_MM:
290 svgType = LengthTypeMM;
292 case CSSPrimitiveValue::CSS_IN:
293 svgType = LengthTypeIN;
295 case CSSPrimitiveValue::CSS_PT:
296 svgType = LengthTypePT;
298 case CSSPrimitiveValue::CSS_PC:
299 svgType = LengthTypePC;
301 case CSSPrimitiveValue::CSS_UNKNOWN:
303 svgType = LengthTypeUnknown;
307 if (svgType == LengthTypeUnknown)
308 return SVGLength::create();
310 RefPtr<SVGLength> length = SVGLength::create();
311 length->newValueSpecifiedUnits(svgType, value->getFloatValue());
312 return length.release();
315 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> SVGLength::toCSSPrimitiveValue(PassRefPtr<SVGLength> passLength)
317 RefPtr<SVGLength> length = passLength;
319 CSSPrimitiveValue::UnitType cssType = CSSPrimitiveValue::CSS_UNKNOWN;
320 switch (length->unitType()) {
321 case LengthTypeUnknown:
323 case LengthTypeNumber:
324 cssType = CSSPrimitiveValue::CSS_NUMBER;
326 case LengthTypePercentage:
327 cssType = CSSPrimitiveValue::CSS_PERCENTAGE;
330 cssType = CSSPrimitiveValue::CSS_EMS;
333 cssType = CSSPrimitiveValue::CSS_EXS;
336 cssType = CSSPrimitiveValue::CSS_PX;
339 cssType = CSSPrimitiveValue::CSS_CM;
342 cssType = CSSPrimitiveValue::CSS_MM;
345 cssType = CSSPrimitiveValue::CSS_IN;
348 cssType = CSSPrimitiveValue::CSS_PT;
351 cssType = CSSPrimitiveValue::CSS_PC;
355 return CSSPrimitiveValue::create(length->valueInSpecifiedUnits(), cssType);
358 SVGLengthMode SVGLength::lengthModeForAnimatedLengthAttribute(const QualifiedName& attrName)
360 typedef HashMap<QualifiedName, SVGLengthMode> LengthModeForLengthAttributeMap;
361 DEFINE_STATIC_LOCAL(LengthModeForLengthAttributeMap, s_lengthModeMap, ());
363 if (s_lengthModeMap.isEmpty()) {
364 s_lengthModeMap.set(SVGNames::xAttr, LengthModeWidth);
365 s_lengthModeMap.set(SVGNames::yAttr, LengthModeHeight);
366 s_lengthModeMap.set(SVGNames::cxAttr, LengthModeWidth);
367 s_lengthModeMap.set(SVGNames::cyAttr, LengthModeHeight);
368 s_lengthModeMap.set(SVGNames::dxAttr, LengthModeWidth);
369 s_lengthModeMap.set(SVGNames::dyAttr, LengthModeHeight);
370 s_lengthModeMap.set(SVGNames::fxAttr, LengthModeWidth);
371 s_lengthModeMap.set(SVGNames::fyAttr, LengthModeHeight);
372 s_lengthModeMap.set(SVGNames::rAttr, LengthModeOther);
373 s_lengthModeMap.set(SVGNames::rxAttr, LengthModeWidth);
374 s_lengthModeMap.set(SVGNames::ryAttr, LengthModeHeight);
375 s_lengthModeMap.set(SVGNames::widthAttr, LengthModeWidth);
376 s_lengthModeMap.set(SVGNames::heightAttr, LengthModeHeight);
377 s_lengthModeMap.set(SVGNames::x1Attr, LengthModeWidth);
378 s_lengthModeMap.set(SVGNames::x2Attr, LengthModeWidth);
379 s_lengthModeMap.set(SVGNames::y1Attr, LengthModeHeight);
380 s_lengthModeMap.set(SVGNames::y2Attr, LengthModeHeight);
381 s_lengthModeMap.set(SVGNames::refXAttr, LengthModeWidth);
382 s_lengthModeMap.set(SVGNames::refYAttr, LengthModeHeight);
383 s_lengthModeMap.set(SVGNames::markerWidthAttr, LengthModeWidth);
384 s_lengthModeMap.set(SVGNames::markerHeightAttr, LengthModeHeight);
385 s_lengthModeMap.set(SVGNames::textLengthAttr, LengthModeWidth);
386 s_lengthModeMap.set(SVGNames::startOffsetAttr, LengthModeWidth);
389 if (s_lengthModeMap.contains(attrName))
390 return s_lengthModeMap.get(attrName);
392 return LengthModeOther;
395 PassRefPtr<SVGLength> SVGLength::blend(PassRefPtr<SVGLength> passFrom, float progress) const
397 RefPtr<SVGLength> from = passFrom;
399 SVGLengthType toType = unitType();
400 SVGLengthType fromType = from->unitType();
401 if ((from->isZero() && isZero())
402 || fromType == LengthTypeUnknown
403 || toType == LengthTypeUnknown
404 || (!from->isZero() && fromType != LengthTypePercentage && toType == LengthTypePercentage)
405 || (!isZero() && fromType == LengthTypePercentage && toType != LengthTypePercentage)
406 || (!from->isZero() && !isZero() && (fromType == LengthTypeEMS || fromType == LengthTypeEXS) && fromType != toType))
409 RefPtr<SVGLength> length = create();
411 if (fromType == LengthTypePercentage || toType == LengthTypePercentage) {
412 float fromPercent = from->valueAsPercentage() * 100;
413 float toPercent = valueAsPercentage() * 100;
414 length->newValueSpecifiedUnits(LengthTypePercentage, blink::blend(fromPercent, toPercent, progress));
418 if (fromType == toType || from->isZero() || isZero() || fromType == LengthTypeEMS || fromType == LengthTypeEXS) {
419 float fromValue = from->valueInSpecifiedUnits();
420 float toValue = valueInSpecifiedUnits();
422 length->newValueSpecifiedUnits(fromType, blink::blend(fromValue, toValue, progress));
424 length->newValueSpecifiedUnits(toType, blink::blend(fromValue, toValue, progress));
428 ASSERT(!isRelative());
429 ASSERT(!from->isRelative());
431 TrackExceptionState es;
432 SVGLengthContext nonRelativeLengthContext(0);
433 float fromValueInUserUnits = nonRelativeLengthContext.convertValueToUserUnits(from->valueInSpecifiedUnits(), from->unitMode(), fromType, es);
434 if (es.hadException())
437 float fromValue = nonRelativeLengthContext.convertValueFromUserUnits(fromValueInUserUnits, unitMode(), toType, es);
438 if (es.hadException())
441 float toValue = valueInSpecifiedUnits();
442 length->newValueSpecifiedUnits(toType, blink::blend(fromValue, toValue, progress));
446 void SVGLength::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement* contextElement)
448 SVGLengthContext lengthContext(contextElement);
450 setValue(value(lengthContext) + toSVGLength(other)->value(lengthContext), lengthContext, ASSERT_NO_EXCEPTION);
453 void SVGLength::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> fromValue, PassRefPtr<SVGPropertyBase> toValue, PassRefPtr<SVGPropertyBase> toAtEndOfDurationValue, SVGElement* contextElement)
455 RefPtr<SVGLength> fromLength = toSVGLength(fromValue);
456 RefPtr<SVGLength> toLength = toSVGLength(toValue);
457 RefPtr<SVGLength> toAtEndOfDurationLength = toSVGLength(toAtEndOfDurationValue);
459 SVGLengthContext lengthContext(contextElement);
460 float animatedNumber = value(lengthContext, IGNORE_EXCEPTION);
461 animationElement->animateAdditiveNumber(percentage, repeatCount, fromLength->value(lengthContext, IGNORE_EXCEPTION), toLength->value(lengthContext, IGNORE_EXCEPTION), toAtEndOfDurationLength->value(lengthContext, IGNORE_EXCEPTION), animatedNumber);
463 ASSERT(unitMode() == lengthModeForAnimatedLengthAttribute(animationElement->attributeName()));
464 m_unitType = percentage < 0.5 ? fromLength->unitType() : toLength->unitType();
465 setValue(animatedNumber, lengthContext, ASSERT_NO_EXCEPTION);
468 float SVGLength::calculateDistance(PassRefPtr<SVGPropertyBase> toValue, SVGElement* contextElement)
470 SVGLengthContext lengthContext(contextElement);
471 RefPtr<SVGLength> toLength = toSVGLength(toValue);
473 return fabsf(toLength->value(lengthContext, IGNORE_EXCEPTION) - value(lengthContext, IGNORE_EXCEPTION));