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 "FloatConversion.h"
29 #include "FrameView.h"
30 #include "RenderObject.h"
31 #include "RenderView.h"
33 #include "SVGParserUtilities.h"
34 #include "SVGSVGElement.h"
36 #include <wtf/MathExtras.h>
37 #include <wtf/text/StringConcatenate.h>
42 static inline unsigned int storeUnit(SVGLengthMode mode, SVGLengthType type)
44 return (mode << 4) | type;
47 static inline SVGLengthMode extractMode(unsigned int unit)
49 unsigned int mode = unit >> 4;
50 return static_cast<SVGLengthMode>(mode);
53 static inline SVGLengthType extractType(unsigned int unit)
55 unsigned int mode = unit >> 4;
56 unsigned int type = unit ^ (mode << 4);
57 return static_cast<SVGLengthType>(type);
60 static inline String lengthTypeToString(SVGLengthType type)
63 case LengthTypeUnknown:
64 case LengthTypeNumber:
66 case LengthTypePercentage:
90 inline SVGLengthType stringToLengthType(const UChar*& ptr, const UChar* end)
93 return LengthTypeNumber;
95 const UChar firstChar = *ptr;
98 return firstChar == '%' ? LengthTypePercentage : LengthTypeUnknown;
100 const UChar secondChar = *ptr;
103 return LengthTypeUnknown;
105 if (firstChar == 'e' && secondChar == 'm')
106 return LengthTypeEMS;
107 if (firstChar == 'e' && secondChar == 'x')
108 return LengthTypeEXS;
109 if (firstChar == 'p' && secondChar == 'x')
111 if (firstChar == 'c' && secondChar == 'm')
113 if (firstChar == 'm' && secondChar == 'm')
115 if (firstChar == 'i' && secondChar == 'n')
117 if (firstChar == 'p' && secondChar == 't')
119 if (firstChar == 'p' && secondChar == 'c')
122 return LengthTypeUnknown;
125 SVGLength::SVGLength(SVGLengthMode mode, const String& valueAsString)
126 : m_valueInSpecifiedUnits(0)
127 , m_unit(storeUnit(mode, LengthTypeNumber))
129 ExceptionCode ec = 0;
130 setValueAsString(valueAsString, ec);
133 SVGLength::SVGLength(const SVGLength& other)
134 : m_valueInSpecifiedUnits(other.m_valueInSpecifiedUnits)
135 , m_unit(other.m_unit)
139 bool SVGLength::operator==(const SVGLength& other) const
141 return m_unit == other.m_unit
142 && m_valueInSpecifiedUnits == other.m_valueInSpecifiedUnits;
145 bool SVGLength::operator!=(const SVGLength& other) const
147 return !operator==(other);
150 SVGLengthType SVGLength::unitType() const
152 return extractType(m_unit);
155 float SVGLength::value(const SVGElement* context) const
157 ExceptionCode ec = 0;
158 return value(context, ec);
161 float SVGLength::value(const SVGElement* context, ExceptionCode& ec) const
163 switch (extractType(m_unit)) {
164 case LengthTypeUnknown:
165 ec = NOT_SUPPORTED_ERR;
167 case LengthTypeNumber:
168 return m_valueInSpecifiedUnits;
169 case LengthTypePercentage:
170 return convertValueFromPercentageToUserUnits(m_valueInSpecifiedUnits / 100, context, ec);
172 return convertValueFromEMSToUserUnits(m_valueInSpecifiedUnits, context, ec);
174 return convertValueFromEXSToUserUnits(m_valueInSpecifiedUnits, context, ec);
176 return m_valueInSpecifiedUnits;
178 return m_valueInSpecifiedUnits / 2.54f * cssPixelsPerInch;
180 return m_valueInSpecifiedUnits / 25.4f * cssPixelsPerInch;
182 return m_valueInSpecifiedUnits * cssPixelsPerInch;
184 return m_valueInSpecifiedUnits / 72 * cssPixelsPerInch;
186 return m_valueInSpecifiedUnits / 6 * cssPixelsPerInch;
189 ASSERT_NOT_REACHED();
193 void SVGLength::setValue(float value, const SVGElement* context, ExceptionCode& ec)
195 switch (extractType(m_unit)) {
196 case LengthTypeUnknown:
197 ec = NOT_SUPPORTED_ERR;
199 case LengthTypeNumber:
200 m_valueInSpecifiedUnits = value;
202 case LengthTypePercentage: {
203 float result = convertValueFromUserUnitsToPercentage(value, context, ec);
205 m_valueInSpecifiedUnits = result;
208 case LengthTypeEMS: {
209 float result = convertValueFromUserUnitsToEMS(value, context, ec);
211 m_valueInSpecifiedUnits = result;
214 case LengthTypeEXS: {
215 float result = convertValueFromUserUnitsToEXS(value, context, ec);
217 m_valueInSpecifiedUnits = result;
221 m_valueInSpecifiedUnits = value;
224 m_valueInSpecifiedUnits = value * 2.54f / cssPixelsPerInch;
227 m_valueInSpecifiedUnits = value * 25.4f / cssPixelsPerInch;
230 m_valueInSpecifiedUnits = value / cssPixelsPerInch;
233 m_valueInSpecifiedUnits = value * 72 / cssPixelsPerInch;
236 m_valueInSpecifiedUnits = value * 6 / cssPixelsPerInch;
241 float SVGLength::valueAsPercentage() const
243 // 100% = 100.0 instead of 1.0 for historical reasons, this could eventually be changed
244 if (extractType(m_unit) == LengthTypePercentage)
245 return m_valueInSpecifiedUnits / 100;
247 return m_valueInSpecifiedUnits;
250 void SVGLength::setValueAsString(const String& string, ExceptionCode& ec)
252 if (string.isEmpty())
255 float convertedNumber = 0;
256 const UChar* ptr = string.characters();
257 const UChar* end = ptr + string.length();
259 if (!parseNumber(ptr, end, convertedNumber, false)) {
264 SVGLengthType type = stringToLengthType(ptr, end);
266 if (type == LengthTypeUnknown) {
271 m_unit = storeUnit(extractMode(m_unit), type);
272 m_valueInSpecifiedUnits = convertedNumber;
275 String SVGLength::valueAsString() const
277 return makeString(String::number(m_valueInSpecifiedUnits), lengthTypeToString(extractType(m_unit)));
280 void SVGLength::newValueSpecifiedUnits(unsigned short type, float value, ExceptionCode& ec)
282 if (type == LengthTypeUnknown || type > LengthTypePC) {
283 ec = NOT_SUPPORTED_ERR;
287 m_unit = storeUnit(extractMode(m_unit), static_cast<SVGLengthType>(type));
288 m_valueInSpecifiedUnits = value;
291 void SVGLength::convertToSpecifiedUnits(unsigned short type, const SVGElement* context, ExceptionCode& ec)
293 if (type == LengthTypeUnknown || type > LengthTypePC) {
294 ec = NOT_SUPPORTED_ERR;
298 float valueInUserUnits = value(context, ec);
302 unsigned int originalUnitAndType = m_unit;
303 m_unit = storeUnit(extractMode(m_unit), static_cast<SVGLengthType>(type));
304 setValue(valueInUserUnits, context, ec);
308 // Eventually restore old unit and type
309 m_unit = originalUnitAndType;
312 bool SVGLength::determineViewport(const SVGElement* context, float& width, float& height) const
317 // Take size from outermost <svg> element.
318 Document* document = context->document();
319 if (document->documentElement() == context) {
320 if (RenderView* view = toRenderView(document->renderer())) {
321 width = view->viewWidth();
322 height = view->viewHeight();
329 // Resolve value against nearest viewport element (common case: inner <svg> elements)
330 SVGElement* viewportElement = context->viewportElement();
331 if (viewportElement && viewportElement->isSVG()) {
332 const SVGSVGElement* svg = static_cast<const SVGSVGElement*>(viewportElement);
333 if (svg->hasAttribute(SVGNames::viewBoxAttr)) {
334 width = svg->viewBox().width();
335 height = svg->viewBox().height();
337 width = svg->width().value(svg);
338 height = svg->height().value(svg);
344 // Resolve value against enclosing non-SVG RenderBox
345 if (!context->parentNode() || context->parentNode()->isSVGElement())
348 RenderObject* renderer = context->renderer();
349 if (!renderer || !renderer->isBox())
352 RenderBox* box = toRenderBox(renderer);
353 width = box->width();
354 height = box->height();
358 float SVGLength::convertValueFromUserUnitsToPercentage(float value, const SVGElement* context, ExceptionCode& ec) const
362 if (!determineViewport(context, width, height)) {
363 ec = NOT_SUPPORTED_ERR;
367 switch (extractMode(m_unit)) {
368 case LengthModeWidth:
369 return value / width * 100;
370 case LengthModeHeight:
371 return value / height * 100;
372 case LengthModeOther:
373 return value / (sqrtf((width * width + height * height) / 2)) * 100;
376 ASSERT_NOT_REACHED();
380 float SVGLength::convertValueFromPercentageToUserUnits(float value, const SVGElement* context, ExceptionCode& ec) const
384 if (!determineViewport(context, width, height)) {
385 ec = NOT_SUPPORTED_ERR;
389 switch (extractMode(m_unit)) {
390 case LengthModeWidth:
391 return value * width;
392 case LengthModeHeight:
393 return value * height;
394 case LengthModeOther:
395 return value * sqrtf((width * width + height * height) / 2);
398 ASSERT_NOT_REACHED();
402 float SVGLength::convertValueFromUserUnitsToEMS(float value, const SVGElement* context, ExceptionCode& ec) const
404 if (!context || !context->renderer() || !context->renderer()->style()) {
405 ec = NOT_SUPPORTED_ERR;
409 RenderStyle* style = context->renderer()->style();
410 float fontSize = style->fontSize();
412 ec = NOT_SUPPORTED_ERR;
416 return value / fontSize;
419 float SVGLength::convertValueFromEMSToUserUnits(float value, const SVGElement* context, ExceptionCode& ec) const
421 if (!context || !context->renderer() || !context->renderer()->style()) {
422 ec = NOT_SUPPORTED_ERR;
426 RenderStyle* style = context->renderer()->style();
427 return value * style->fontSize();
430 float SVGLength::convertValueFromUserUnitsToEXS(float value, const SVGElement* context, ExceptionCode& ec) const
432 if (!context || !context->renderer() || !context->renderer()->style()) {
433 ec = NOT_SUPPORTED_ERR;
437 RenderStyle* style = context->renderer()->style();
439 // Use of ceil allows a pixel match to the W3Cs expected output of coords-units-03-b.svg
440 // if this causes problems in real world cases maybe it would be best to remove this
441 float xHeight = ceilf(style->fontMetrics().xHeight());
443 ec = NOT_SUPPORTED_ERR;
447 return value / xHeight;
450 float SVGLength::convertValueFromEXSToUserUnits(float value, const SVGElement* context, ExceptionCode& ec) const
452 if (!context || !context->renderer() || !context->renderer()->style()) {
453 ec = NOT_SUPPORTED_ERR;
457 RenderStyle* style = context->renderer()->style();
458 // Use of ceil allows a pixel match to the W3Cs expected output of coords-units-03-b.svg
459 // if this causes problems in real world cases maybe it would be best to remove this
460 return value * ceilf(style->fontMetrics().xHeight());
463 SVGLength SVGLength::fromCSSPrimitiveValue(CSSPrimitiveValue* value)
467 SVGLengthType svgType;
468 switch (value->primitiveType()) {
469 case CSSPrimitiveValue::CSS_NUMBER:
470 svgType = LengthTypeNumber;
472 case CSSPrimitiveValue::CSS_PERCENTAGE:
473 svgType = LengthTypePercentage;
475 case CSSPrimitiveValue::CSS_EMS:
476 svgType = LengthTypeEMS;
478 case CSSPrimitiveValue::CSS_EXS:
479 svgType = LengthTypeEXS;
481 case CSSPrimitiveValue::CSS_PX:
482 svgType = LengthTypePX;
484 case CSSPrimitiveValue::CSS_CM:
485 svgType = LengthTypeCM;
487 case CSSPrimitiveValue::CSS_MM:
488 svgType = LengthTypeMM;
490 case CSSPrimitiveValue::CSS_IN:
491 svgType = LengthTypeIN;
493 case CSSPrimitiveValue::CSS_PT:
494 svgType = LengthTypePT;
496 case CSSPrimitiveValue::CSS_PC:
497 svgType = LengthTypePC;
499 case CSSPrimitiveValue::CSS_UNKNOWN:
501 svgType = LengthTypeUnknown;
505 if (svgType == LengthTypeUnknown)
508 ExceptionCode ec = 0;
510 length.newValueSpecifiedUnits(svgType, value->getFloatValue(), ec);
517 PassRefPtr<CSSPrimitiveValue> SVGLength::toCSSPrimitiveValue(const SVGLength& length)
519 CSSPrimitiveValue::UnitTypes cssType = CSSPrimitiveValue::CSS_UNKNOWN;
520 switch (length.unitType()) {
521 case LengthTypeUnknown:
523 case LengthTypeNumber:
524 cssType = CSSPrimitiveValue::CSS_NUMBER;
526 case LengthTypePercentage:
527 cssType = CSSPrimitiveValue::CSS_PERCENTAGE;
530 cssType = CSSPrimitiveValue::CSS_EMS;
533 cssType = CSSPrimitiveValue::CSS_EXS;
536 cssType = CSSPrimitiveValue::CSS_PX;
539 cssType = CSSPrimitiveValue::CSS_CM;
542 cssType = CSSPrimitiveValue::CSS_MM;
545 cssType = CSSPrimitiveValue::CSS_IN;
548 cssType = CSSPrimitiveValue::CSS_PT;
551 cssType = CSSPrimitiveValue::CSS_PC;
555 return CSSPrimitiveValue::create(length.valueInSpecifiedUnits(), cssType);