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.
25 #include "SVGLength.h"
27 #include "CSSHelper.h"
28 #include "CSSPrimitiveValue.h"
29 #include "ExceptionCode.h"
30 #include "FloatConversion.h"
32 #include "SVGParserUtilities.h"
34 #include <wtf/MathExtras.h>
35 #include <wtf/text/WTFString.h>
40 static inline unsigned int storeUnit(SVGLengthMode mode, SVGLengthType type)
42 return (mode << 4) | type;
45 static inline SVGLengthMode extractMode(unsigned int unit)
47 unsigned int mode = unit >> 4;
48 return static_cast<SVGLengthMode>(mode);
51 static inline SVGLengthType extractType(unsigned int unit)
53 unsigned int mode = unit >> 4;
54 unsigned int type = unit ^ (mode << 4);
55 return static_cast<SVGLengthType>(type);
58 static inline String lengthTypeToString(SVGLengthType type)
61 case LengthTypeUnknown:
62 case LengthTypeNumber:
64 case LengthTypePercentage:
88 inline SVGLengthType stringToLengthType(const UChar*& ptr, const UChar* end)
91 return LengthTypeNumber;
93 const UChar firstChar = *ptr;
96 return firstChar == '%' ? LengthTypePercentage : LengthTypeUnknown;
98 const UChar secondChar = *ptr;
101 return LengthTypeUnknown;
103 if (firstChar == 'e' && secondChar == 'm')
104 return LengthTypeEMS;
105 if (firstChar == 'e' && secondChar == 'x')
106 return LengthTypeEXS;
107 if (firstChar == 'p' && secondChar == 'x')
109 if (firstChar == 'c' && secondChar == 'm')
111 if (firstChar == 'm' && secondChar == 'm')
113 if (firstChar == 'i' && secondChar == 'n')
115 if (firstChar == 'p' && secondChar == 't')
117 if (firstChar == 'p' && secondChar == 'c')
120 return LengthTypeUnknown;
123 SVGLength::SVGLength(SVGLengthMode mode, const String& valueAsString)
124 : m_valueInSpecifiedUnits(0)
125 , m_unit(storeUnit(mode, LengthTypeNumber))
127 ExceptionCode ec = 0;
128 setValueAsString(valueAsString, ec);
131 SVGLength::SVGLength(const SVGLengthContext& context, float value, SVGLengthMode mode, SVGLengthType unitType)
132 : m_valueInSpecifiedUnits(0)
133 , m_unit(storeUnit(mode, unitType))
135 ExceptionCode ec = 0;
136 setValue(value, context, ec);
140 SVGLength::SVGLength(const SVGLength& other)
141 : m_valueInSpecifiedUnits(other.m_valueInSpecifiedUnits)
142 , m_unit(other.m_unit)
146 void SVGLength::setValueAsString(const String& valueAsString, SVGLengthMode mode, ExceptionCode& ec)
148 m_valueInSpecifiedUnits = 0;
149 m_unit = storeUnit(mode, LengthTypeNumber);
150 setValueAsString(valueAsString, ec);
153 bool SVGLength::operator==(const SVGLength& other) const
155 return m_unit == other.m_unit
156 && m_valueInSpecifiedUnits == other.m_valueInSpecifiedUnits;
159 bool SVGLength::operator!=(const SVGLength& other) const
161 return !operator==(other);
164 SVGLength SVGLength::construct(SVGLengthMode mode, const String& valueAsString, SVGParsingError& parseError, SVGLengthNegativeValuesMode negativeValuesMode)
166 ExceptionCode ec = 0;
167 SVGLength length(mode);
169 length.setValueAsString(valueAsString, ec);
172 parseError = ParsingAttributeFailedError;
173 else if (negativeValuesMode == ForbidNegativeLengths && length.valueInSpecifiedUnits() < 0)
174 parseError = NegativeValueForbiddenError;
179 SVGLengthType SVGLength::unitType() const
181 return extractType(m_unit);
184 SVGLengthMode SVGLength::unitMode() const
186 return extractMode(m_unit);
189 float SVGLength::value(const SVGLengthContext& context) const
191 ExceptionCode ec = 0;
192 return value(context, ec);
195 float SVGLength::value(const SVGLengthContext& context, ExceptionCode& ec) const
197 return context.convertValueToUserUnits(m_valueInSpecifiedUnits, extractMode(m_unit), extractType(m_unit), ec);
200 void SVGLength::setValue(const SVGLengthContext& context, float value, SVGLengthMode mode, SVGLengthType unitType, ExceptionCode& ec)
202 m_unit = storeUnit(mode, unitType);
203 setValue(value, context, ec);
206 void SVGLength::setValue(float value, const SVGLengthContext& context, ExceptionCode& ec)
208 // 100% = 100.0 instead of 1.0 for historical reasons, this could eventually be changed
209 if (extractType(m_unit) == LengthTypePercentage)
212 float convertedValue = context.convertValueFromUserUnits(value, extractMode(m_unit), extractType(m_unit), ec);
214 m_valueInSpecifiedUnits = convertedValue;
216 float SVGLength::valueAsPercentage() const
218 // 100% = 100.0 instead of 1.0 for historical reasons, this could eventually be changed
219 if (extractType(m_unit) == LengthTypePercentage)
220 return m_valueInSpecifiedUnits / 100;
222 return m_valueInSpecifiedUnits;
225 void SVGLength::setValueAsString(const String& string, ExceptionCode& ec)
227 if (string.isEmpty())
230 float convertedNumber = 0;
231 const UChar* ptr = string.characters();
232 const UChar* end = ptr + string.length();
234 if (!parseNumber(ptr, end, convertedNumber, false)) {
239 SVGLengthType type = stringToLengthType(ptr, end);
241 if (type == LengthTypeUnknown) {
246 m_unit = storeUnit(extractMode(m_unit), type);
247 m_valueInSpecifiedUnits = convertedNumber;
250 String SVGLength::valueAsString() const
252 return String::number(m_valueInSpecifiedUnits) + lengthTypeToString(extractType(m_unit));
255 void SVGLength::newValueSpecifiedUnits(unsigned short type, float value, ExceptionCode& ec)
257 if (type == LengthTypeUnknown || type > LengthTypePC) {
258 ec = NOT_SUPPORTED_ERR;
262 m_unit = storeUnit(extractMode(m_unit), static_cast<SVGLengthType>(type));
263 m_valueInSpecifiedUnits = value;
266 void SVGLength::convertToSpecifiedUnits(unsigned short type, const SVGLengthContext& context, ExceptionCode& ec)
268 if (type == LengthTypeUnknown || type > LengthTypePC) {
269 ec = NOT_SUPPORTED_ERR;
273 float valueInUserUnits = value(context, ec);
277 unsigned int originalUnitAndType = m_unit;
278 m_unit = storeUnit(extractMode(m_unit), static_cast<SVGLengthType>(type));
279 setValue(valueInUserUnits, context, ec);
283 // Eventually restore old unit and type
284 m_unit = originalUnitAndType;
287 SVGLength SVGLength::fromCSSPrimitiveValue(CSSPrimitiveValue* value)
291 SVGLengthType svgType;
292 switch (value->primitiveType()) {
293 case CSSPrimitiveValue::CSS_NUMBER:
294 svgType = LengthTypeNumber;
296 case CSSPrimitiveValue::CSS_PERCENTAGE:
297 svgType = LengthTypePercentage;
299 case CSSPrimitiveValue::CSS_EMS:
300 svgType = LengthTypeEMS;
302 case CSSPrimitiveValue::CSS_EXS:
303 svgType = LengthTypeEXS;
305 case CSSPrimitiveValue::CSS_PX:
306 svgType = LengthTypePX;
308 case CSSPrimitiveValue::CSS_CM:
309 svgType = LengthTypeCM;
311 case CSSPrimitiveValue::CSS_MM:
312 svgType = LengthTypeMM;
314 case CSSPrimitiveValue::CSS_IN:
315 svgType = LengthTypeIN;
317 case CSSPrimitiveValue::CSS_PT:
318 svgType = LengthTypePT;
320 case CSSPrimitiveValue::CSS_PC:
321 svgType = LengthTypePC;
323 case CSSPrimitiveValue::CSS_UNKNOWN:
325 svgType = LengthTypeUnknown;
329 if (svgType == LengthTypeUnknown)
332 ExceptionCode ec = 0;
334 length.newValueSpecifiedUnits(svgType, value->getFloatValue(), ec);
341 PassRefPtr<CSSPrimitiveValue> SVGLength::toCSSPrimitiveValue(const SVGLength& length)
343 CSSPrimitiveValue::UnitTypes cssType = CSSPrimitiveValue::CSS_UNKNOWN;
344 switch (length.unitType()) {
345 case LengthTypeUnknown:
347 case LengthTypeNumber:
348 cssType = CSSPrimitiveValue::CSS_NUMBER;
350 case LengthTypePercentage:
351 cssType = CSSPrimitiveValue::CSS_PERCENTAGE;
354 cssType = CSSPrimitiveValue::CSS_EMS;
357 cssType = CSSPrimitiveValue::CSS_EXS;
360 cssType = CSSPrimitiveValue::CSS_PX;
363 cssType = CSSPrimitiveValue::CSS_CM;
366 cssType = CSSPrimitiveValue::CSS_MM;
369 cssType = CSSPrimitiveValue::CSS_IN;
372 cssType = CSSPrimitiveValue::CSS_PT;
375 cssType = CSSPrimitiveValue::CSS_PC;
379 return CSSPrimitiveValue::create(length.valueInSpecifiedUnits(), cssType);
382 SVGLengthMode SVGLength::lengthModeForAnimatedLengthAttribute(const QualifiedName& attrName)
384 typedef HashMap<QualifiedName, SVGLengthMode> LengthModeForLengthAttributeMap;
385 DEFINE_STATIC_LOCAL(LengthModeForLengthAttributeMap, s_lengthModeMap, ());
387 if (s_lengthModeMap.isEmpty()) {
388 s_lengthModeMap.set(SVGNames::xAttr, LengthModeWidth);
389 s_lengthModeMap.set(SVGNames::yAttr, LengthModeHeight);
390 s_lengthModeMap.set(SVGNames::cxAttr, LengthModeWidth);
391 s_lengthModeMap.set(SVGNames::cyAttr, LengthModeHeight);
392 s_lengthModeMap.set(SVGNames::dxAttr, LengthModeWidth);
393 s_lengthModeMap.set(SVGNames::dyAttr, LengthModeHeight);
394 s_lengthModeMap.set(SVGNames::fxAttr, LengthModeWidth);
395 s_lengthModeMap.set(SVGNames::fyAttr, LengthModeHeight);
396 s_lengthModeMap.set(SVGNames::rAttr, LengthModeOther);
397 s_lengthModeMap.set(SVGNames::widthAttr, LengthModeWidth);
398 s_lengthModeMap.set(SVGNames::heightAttr, LengthModeHeight);
399 s_lengthModeMap.set(SVGNames::x1Attr, LengthModeWidth);
400 s_lengthModeMap.set(SVGNames::x2Attr, LengthModeWidth);
401 s_lengthModeMap.set(SVGNames::y1Attr, LengthModeHeight);
402 s_lengthModeMap.set(SVGNames::y2Attr, LengthModeHeight);
403 s_lengthModeMap.set(SVGNames::refXAttr, LengthModeWidth);
404 s_lengthModeMap.set(SVGNames::refYAttr, LengthModeHeight);
405 s_lengthModeMap.set(SVGNames::markerWidthAttr, LengthModeWidth);
406 s_lengthModeMap.set(SVGNames::markerHeightAttr, LengthModeHeight);
407 s_lengthModeMap.set(SVGNames::textLengthAttr, LengthModeWidth);
408 s_lengthModeMap.set(SVGNames::startOffsetAttr, LengthModeWidth);
411 if (s_lengthModeMap.contains(attrName))
412 return s_lengthModeMap.get(attrName);
414 return LengthModeOther;