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/SVGLength.h"
25
26 #include "bindings/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"
35
36 namespace WebCore {
37
38 namespace {
39
lengthTypeToString(SVGLengthType type)40 inline String lengthTypeToString(SVGLengthType type)
41 {
42 switch (type) {
43 case LengthTypeUnknown:
44 case LengthTypeNumber:
45 return "";
46 case LengthTypePercentage:
47 return "%";
48 case LengthTypeEMS:
49 return "em";
50 case LengthTypeEXS:
51 return "ex";
52 case LengthTypePX:
53 return "px";
54 case LengthTypeCM:
55 return "cm";
56 case LengthTypeMM:
57 return "mm";
58 case LengthTypeIN:
59 return "in";
60 case LengthTypePT:
61 return "pt";
62 case LengthTypePC:
63 return "pc";
64 }
65
66 ASSERT_NOT_REACHED();
67 return String();
68 }
69
70 template<typename CharType>
stringToLengthType(const CharType * & ptr,const CharType * end)71 SVGLengthType stringToLengthType(const CharType*& ptr, const CharType* end)
72 {
73 if (ptr == end)
74 return LengthTypeNumber;
75
76 SVGLengthType type = LengthTypeUnknown;
77 const CharType firstChar = *ptr++;
78
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++;
85
86 if (firstChar == 'p') {
87 if (secondChar == 'x')
88 type = LengthTypePX;
89 if (secondChar == 't')
90 type = LengthTypePT;
91 if (secondChar == 'c')
92 type = LengthTypePC;
93 } else if (firstChar == 'e') {
94 if (secondChar == 'm')
95 type = LengthTypeEMS;
96 if (secondChar == 'x')
97 type = LengthTypeEXS;
98 } else if (firstChar == 'c' && secondChar == 'm') {
99 type = LengthTypeCM;
100 } else if (firstChar == 'm' && secondChar == 'm') {
101 type = LengthTypeMM;
102 } else if (firstChar == 'i' && secondChar == 'n') {
103 type = LengthTypeIN;
104 } else if (isHTMLSpace<CharType>(firstChar) && isHTMLSpace<CharType>(secondChar)) {
105 type = LengthTypeNumber;
106 }
107 }
108
109 if (!skipOptionalSVGSpaces(ptr, end))
110 return type;
111
112 return LengthTypeUnknown;
113 }
114
115 } // namespace
116
SVGLength(SVGLengthMode mode)117 SVGLength::SVGLength(SVGLengthMode mode)
118 : SVGPropertyBase(classType())
119 , m_valueInSpecifiedUnits(0)
120 , m_unitMode(mode)
121 , m_unitType(LengthTypeNumber)
122 {
123 }
124
SVGLength(const SVGLength & o)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)
130 {
131 }
132
clone() const133 PassRefPtr<SVGLength> SVGLength::clone() const
134 {
135 return adoptRef(new SVGLength(*this));
136 }
137
cloneForAnimation(const String & value) const138 PassRefPtr<SVGPropertyBase> SVGLength::cloneForAnimation(const String& value) const
139 {
140 RefPtr<SVGLength> length = create();
141
142 length->m_unitMode = m_unitMode;
143 length->m_unitType = m_unitType;
144
145 TrackExceptionState exceptionState;
146 length->setValueAsString(value, exceptionState);
147 if (exceptionState.hadException()) {
148 length->m_unitType = LengthTypeNumber;
149 length->m_valueInSpecifiedUnits = 0;
150 }
151
152 return length.release();
153 }
154
operator ==(const SVGLength & other) const155 bool SVGLength::operator==(const SVGLength& other) const
156 {
157 return m_unitMode == other.m_unitMode
158 && m_unitType == other.m_unitType
159 && m_valueInSpecifiedUnits == other.m_valueInSpecifiedUnits;
160 }
161
value(const SVGLengthContext & context,ExceptionState & es) const162 float SVGLength::value(const SVGLengthContext& context, ExceptionState& es) const
163 {
164 return context.convertValueToUserUnits(m_valueInSpecifiedUnits, unitMode(), unitType(), es);
165 }
166
setValue(float value,const SVGLengthContext & context,ExceptionState & es)167 void SVGLength::setValue(float value, const SVGLengthContext& context, ExceptionState& es)
168 {
169 // 100% = 100.0 instead of 1.0 for historical reasons, this could eventually be changed
170 if (m_unitType == LengthTypePercentage)
171 value = value / 100;
172
173 float convertedValue = context.convertValueFromUserUnits(value, unitMode(), unitType(), es);
174 if (es.hadException())
175 return;
176
177 m_valueInSpecifiedUnits = convertedValue;
178 }
179
setUnitType(SVGLengthType type)180 void SVGLength::setUnitType(SVGLengthType type)
181 {
182 ASSERT(type != LengthTypeUnknown && type <= LengthTypePC);
183 m_unitType = type;
184 }
185
valueAsPercentage() const186 float SVGLength::valueAsPercentage() const
187 {
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;
191
192 return m_valueInSpecifiedUnits;
193 }
194
195 template<typename CharType>
parseValueInternal(const String & string,float & convertedNumber,SVGLengthType & type)196 static bool parseValueInternal(const String& string, float& convertedNumber, SVGLengthType& type)
197 {
198 const CharType* ptr = string.getCharacters<CharType>();
199 const CharType* end = ptr + string.length();
200
201 if (!parseNumber(ptr, end, convertedNumber, AllowLeadingWhitespace))
202 return false;
203
204 type = stringToLengthType(ptr, end);
205 ASSERT(ptr <= end);
206 if (type == LengthTypeUnknown)
207 return false;
208
209 return true;
210 }
211
setValueAsString(const String & string,ExceptionState & exceptionState)212 void SVGLength::setValueAsString(const String& string, ExceptionState& exceptionState)
213 {
214 if (string.isEmpty()) {
215 m_unitType = LengthTypeNumber;
216 m_valueInSpecifiedUnits = 0;
217 return;
218 }
219
220 float convertedNumber = 0;
221 SVGLengthType type = LengthTypeUnknown;
222
223 bool success = string.is8Bit() ?
224 parseValueInternal<LChar>(string, convertedNumber, type) :
225 parseValueInternal<UChar>(string, convertedNumber, type);
226
227 if (!success) {
228 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + string + "') is invalid.");
229 return;
230 }
231
232 m_unitType = type;
233 m_valueInSpecifiedUnits = convertedNumber;
234 }
235
valueAsString() const236 String SVGLength::valueAsString() const
237 {
238 return String::number(m_valueInSpecifiedUnits) + lengthTypeToString(unitType());
239 }
240
newValueSpecifiedUnits(SVGLengthType type,float value)241 void SVGLength::newValueSpecifiedUnits(SVGLengthType type, float value)
242 {
243 setUnitType(type);
244 m_valueInSpecifiedUnits = value;
245 }
246
convertToSpecifiedUnits(SVGLengthType type,const SVGLengthContext & context,ExceptionState & exceptionState)247 void SVGLength::convertToSpecifiedUnits(SVGLengthType type, const SVGLengthContext& context, ExceptionState& exceptionState)
248 {
249 ASSERT(type != LengthTypeUnknown && type <= LengthTypePC);
250
251 float valueInUserUnits = value(context, exceptionState);
252 if (exceptionState.hadException())
253 return;
254
255 SVGLengthType originalType = unitType();
256 m_unitType = type;
257 setValue(valueInUserUnits, context, exceptionState);
258 if (!exceptionState.hadException())
259 return;
260
261 // Eventually restore old unit and type
262 m_unitType = originalType;
263 }
264
fromCSSPrimitiveValue(CSSPrimitiveValue * value)265 PassRefPtr<SVGLength> SVGLength::fromCSSPrimitiveValue(CSSPrimitiveValue* value)
266 {
267 ASSERT(value);
268
269 SVGLengthType svgType;
270 switch (value->primitiveType()) {
271 case CSSPrimitiveValue::CSS_NUMBER:
272 svgType = LengthTypeNumber;
273 break;
274 case CSSPrimitiveValue::CSS_PERCENTAGE:
275 svgType = LengthTypePercentage;
276 break;
277 case CSSPrimitiveValue::CSS_EMS:
278 svgType = LengthTypeEMS;
279 break;
280 case CSSPrimitiveValue::CSS_EXS:
281 svgType = LengthTypeEXS;
282 break;
283 case CSSPrimitiveValue::CSS_PX:
284 svgType = LengthTypePX;
285 break;
286 case CSSPrimitiveValue::CSS_CM:
287 svgType = LengthTypeCM;
288 break;
289 case CSSPrimitiveValue::CSS_MM:
290 svgType = LengthTypeMM;
291 break;
292 case CSSPrimitiveValue::CSS_IN:
293 svgType = LengthTypeIN;
294 break;
295 case CSSPrimitiveValue::CSS_PT:
296 svgType = LengthTypePT;
297 break;
298 case CSSPrimitiveValue::CSS_PC:
299 svgType = LengthTypePC;
300 break;
301 case CSSPrimitiveValue::CSS_UNKNOWN:
302 default:
303 svgType = LengthTypeUnknown;
304 break;
305 };
306
307 if (svgType == LengthTypeUnknown)
308 return SVGLength::create();
309
310 RefPtr<SVGLength> length = SVGLength::create();
311 length->newValueSpecifiedUnits(svgType, value->getFloatValue());
312 return length.release();
313 }
314
toCSSPrimitiveValue(PassRefPtr<SVGLength> passLength)315 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> SVGLength::toCSSPrimitiveValue(PassRefPtr<SVGLength> passLength)
316 {
317 RefPtr<SVGLength> length = passLength;
318
319 CSSPrimitiveValue::UnitType cssType = CSSPrimitiveValue::CSS_UNKNOWN;
320 switch (length->unitType()) {
321 case LengthTypeUnknown:
322 break;
323 case LengthTypeNumber:
324 cssType = CSSPrimitiveValue::CSS_NUMBER;
325 break;
326 case LengthTypePercentage:
327 cssType = CSSPrimitiveValue::CSS_PERCENTAGE;
328 break;
329 case LengthTypeEMS:
330 cssType = CSSPrimitiveValue::CSS_EMS;
331 break;
332 case LengthTypeEXS:
333 cssType = CSSPrimitiveValue::CSS_EXS;
334 break;
335 case LengthTypePX:
336 cssType = CSSPrimitiveValue::CSS_PX;
337 break;
338 case LengthTypeCM:
339 cssType = CSSPrimitiveValue::CSS_CM;
340 break;
341 case LengthTypeMM:
342 cssType = CSSPrimitiveValue::CSS_MM;
343 break;
344 case LengthTypeIN:
345 cssType = CSSPrimitiveValue::CSS_IN;
346 break;
347 case LengthTypePT:
348 cssType = CSSPrimitiveValue::CSS_PT;
349 break;
350 case LengthTypePC:
351 cssType = CSSPrimitiveValue::CSS_PC;
352 break;
353 };
354
355 return CSSPrimitiveValue::create(length->valueInSpecifiedUnits(), cssType);
356 }
357
lengthModeForAnimatedLengthAttribute(const QualifiedName & attrName)358 SVGLengthMode SVGLength::lengthModeForAnimatedLengthAttribute(const QualifiedName& attrName)
359 {
360 typedef HashMap<QualifiedName, SVGLengthMode> LengthModeForLengthAttributeMap;
361 DEFINE_STATIC_LOCAL(LengthModeForLengthAttributeMap, s_lengthModeMap, ());
362
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);
387 }
388
389 if (s_lengthModeMap.contains(attrName))
390 return s_lengthModeMap.get(attrName);
391
392 return LengthModeOther;
393 }
394
blend(PassRefPtr<SVGLength> passFrom,float progress) const395 PassRefPtr<SVGLength> SVGLength::blend(PassRefPtr<SVGLength> passFrom, float progress) const
396 {
397 RefPtr<SVGLength> from = passFrom;
398
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))
407 return clone();
408
409 RefPtr<SVGLength> length = create();
410
411 if (fromType == LengthTypePercentage || toType == LengthTypePercentage) {
412 float fromPercent = from->valueAsPercentage() * 100;
413 float toPercent = valueAsPercentage() * 100;
414 length->newValueSpecifiedUnits(LengthTypePercentage, WebCore::blend(fromPercent, toPercent, progress));
415 return length;
416 }
417
418 if (fromType == toType || from->isZero() || isZero() || fromType == LengthTypeEMS || fromType == LengthTypeEXS) {
419 float fromValue = from->valueInSpecifiedUnits();
420 float toValue = valueInSpecifiedUnits();
421 if (isZero())
422 length->newValueSpecifiedUnits(fromType, WebCore::blend(fromValue, toValue, progress));
423 else
424 length->newValueSpecifiedUnits(toType, WebCore::blend(fromValue, toValue, progress));
425 return length;
426 }
427
428 ASSERT(!isRelative());
429 ASSERT(!from->isRelative());
430
431 TrackExceptionState es;
432 SVGLengthContext nonRelativeLengthContext(0);
433 float fromValueInUserUnits = nonRelativeLengthContext.convertValueToUserUnits(from->valueInSpecifiedUnits(), from->unitMode(), fromType, es);
434 if (es.hadException())
435 return create();
436
437 float fromValue = nonRelativeLengthContext.convertValueFromUserUnits(fromValueInUserUnits, unitMode(), toType, es);
438 if (es.hadException())
439 return create();
440
441 float toValue = valueInSpecifiedUnits();
442 length->newValueSpecifiedUnits(toType, WebCore::blend(fromValue, toValue, progress));
443 return length;
444 }
445
add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other,SVGElement * contextElement)446 void SVGLength::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement* contextElement)
447 {
448 SVGLengthContext lengthContext(contextElement);
449
450 setValue(value(lengthContext) + toSVGLength(other)->value(lengthContext), lengthContext, ASSERT_NO_EXCEPTION);
451 }
452
calculateAnimatedValue(SVGAnimationElement * animationElement,float percentage,unsigned repeatCount,PassRefPtr<SVGPropertyBase> fromValue,PassRefPtr<SVGPropertyBase> toValue,PassRefPtr<SVGPropertyBase> toAtEndOfDurationValue,SVGElement * contextElement)453 void SVGLength::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> fromValue, PassRefPtr<SVGPropertyBase> toValue, PassRefPtr<SVGPropertyBase> toAtEndOfDurationValue, SVGElement* contextElement)
454 {
455 RefPtr<SVGLength> fromLength = toSVGLength(fromValue);
456 RefPtr<SVGLength> toLength = toSVGLength(toValue);
457 RefPtr<SVGLength> toAtEndOfDurationLength = toSVGLength(toAtEndOfDurationValue);
458
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);
462
463 ASSERT(unitMode() == lengthModeForAnimatedLengthAttribute(animationElement->attributeName()));
464 m_unitType = percentage < 0.5 ? fromLength->unitType() : toLength->unitType();
465 setValue(animatedNumber, lengthContext, ASSERT_NO_EXCEPTION);
466 }
467
calculateDistance(PassRefPtr<SVGPropertyBase> toValue,SVGElement * contextElement)468 float SVGLength::calculateDistance(PassRefPtr<SVGPropertyBase> toValue, SVGElement* contextElement)
469 {
470 SVGLengthContext lengthContext(contextElement);
471 RefPtr<SVGLength> toLength = toSVGLength(toValue);
472
473 return fabsf(toLength->value(lengthContext, IGNORE_EXCEPTION) - value(lengthContext, IGNORE_EXCEPTION));
474 }
475
476 }
477