1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
/*
* Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org>
* Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
* Copyright (C) 2007 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#if ENABLE(SVG)
#include "SVGLength.h"
#include "CSSHelper.h"
#include "FloatConversion.h"
#include "FrameView.h"
#include "RenderObject.h"
#include "RenderView.h"
#include "SVGNames.h"
#include "SVGParserUtilities.h"
#include "SVGSVGElement.h"
#include <wtf/MathExtras.h>
#include <wtf/text/StringConcatenate.h>
namespace WebCore {
// Helper functions
static inline unsigned int storeUnit(SVGLengthMode mode, SVGLengthType type)
{
return (mode << 4) | type;
}
static inline SVGLengthMode extractMode(unsigned int unit)
{
unsigned int mode = unit >> 4;
return static_cast<SVGLengthMode>(mode);
}
static inline SVGLengthType extractType(unsigned int unit)
{
unsigned int mode = unit >> 4;
unsigned int type = unit ^ (mode << 4);
return static_cast<SVGLengthType>(type);
}
static inline String lengthTypeToString(SVGLengthType type)
{
switch (type) {
case LengthTypeUnknown:
case LengthTypeNumber:
return "";
case LengthTypePercentage:
return "%";
case LengthTypeEMS:
return "em";
case LengthTypeEXS:
return "ex";
case LengthTypePX:
return "px";
case LengthTypeCM:
return "cm";
case LengthTypeMM:
return "mm";
case LengthTypeIN:
return "in";
case LengthTypePT:
return "pt";
case LengthTypePC:
return "pc";
}
ASSERT_NOT_REACHED();
return String();
}
inline SVGLengthType stringToLengthType(const UChar*& ptr, const UChar* end)
{
if (ptr == end)
return LengthTypeNumber;
const UChar firstChar = *ptr;
if (++ptr == end)
return firstChar == '%' ? LengthTypePercentage : LengthTypeUnknown;
const UChar secondChar = *ptr;
if (++ptr != end)
return LengthTypeUnknown;
if (firstChar == 'e' && secondChar == 'm')
return LengthTypeEMS;
if (firstChar == 'e' && secondChar == 'x')
return LengthTypeEXS;
if (firstChar == 'p' && secondChar == 'x')
return LengthTypePX;
if (firstChar == 'c' && secondChar == 'm')
return LengthTypeCM;
if (firstChar == 'm' && secondChar == 'm')
return LengthTypeMM;
if (firstChar == 'i' && secondChar == 'n')
return LengthTypeIN;
if (firstChar == 'p' && secondChar == 't')
return LengthTypePT;
if (firstChar == 'p' && secondChar == 'c')
return LengthTypePC;
return LengthTypeUnknown;
}
SVGLength::SVGLength(SVGLengthMode mode, const String& valueAsString)
: m_valueInSpecifiedUnits(0)
, m_unit(storeUnit(mode, LengthTypeNumber))
{
ExceptionCode ec = 0;
setValueAsString(valueAsString, ec);
}
SVGLength::SVGLength(const SVGLength& other)
: m_valueInSpecifiedUnits(other.m_valueInSpecifiedUnits)
, m_unit(other.m_unit)
{
}
bool SVGLength::operator==(const SVGLength& other) const
{
return m_unit == other.m_unit
&& m_valueInSpecifiedUnits == other.m_valueInSpecifiedUnits;
}
bool SVGLength::operator!=(const SVGLength& other) const
{
return !operator==(other);
}
SVGLengthType SVGLength::unitType() const
{
return extractType(m_unit);
}
float SVGLength::value(const SVGElement* context) const
{
ExceptionCode ec = 0;
return value(context, ec);
}
float SVGLength::value(const SVGElement* context, ExceptionCode& ec) const
{
switch (extractType(m_unit)) {
case LengthTypeUnknown:
ec = NOT_SUPPORTED_ERR;
return 0;
case LengthTypeNumber:
return m_valueInSpecifiedUnits;
case LengthTypePercentage:
return convertValueFromPercentageToUserUnits(m_valueInSpecifiedUnits / 100, context, ec);
case LengthTypeEMS:
return convertValueFromEMSToUserUnits(m_valueInSpecifiedUnits, context, ec);
case LengthTypeEXS:
return convertValueFromEXSToUserUnits(m_valueInSpecifiedUnits, context, ec);
case LengthTypePX:
return m_valueInSpecifiedUnits;
case LengthTypeCM:
return m_valueInSpecifiedUnits / 2.54f * cssPixelsPerInch;
case LengthTypeMM:
return m_valueInSpecifiedUnits / 25.4f * cssPixelsPerInch;
case LengthTypeIN:
return m_valueInSpecifiedUnits * cssPixelsPerInch;
case LengthTypePT:
return m_valueInSpecifiedUnits / 72 * cssPixelsPerInch;
case LengthTypePC:
return m_valueInSpecifiedUnits / 6 * cssPixelsPerInch;
}
ASSERT_NOT_REACHED();
return 0;
}
void SVGLength::setValue(float value, const SVGElement* context, ExceptionCode& ec)
{
switch (extractType(m_unit)) {
case LengthTypeUnknown:
ec = NOT_SUPPORTED_ERR;
break;
case LengthTypeNumber:
m_valueInSpecifiedUnits = value;
break;
case LengthTypePercentage: {
float result = convertValueFromUserUnitsToPercentage(value, context, ec);
if (!ec)
m_valueInSpecifiedUnits = result;
break;
}
case LengthTypeEMS: {
float result = convertValueFromUserUnitsToEMS(value, context, ec);
if (!ec)
m_valueInSpecifiedUnits = result;
break;
}
case LengthTypeEXS: {
float result = convertValueFromUserUnitsToEXS(value, context, ec);
if (!ec)
m_valueInSpecifiedUnits = result;
break;
}
case LengthTypePX:
m_valueInSpecifiedUnits = value;
break;
case LengthTypeCM:
m_valueInSpecifiedUnits = value * 2.54f / cssPixelsPerInch;
break;
case LengthTypeMM:
m_valueInSpecifiedUnits = value * 25.4f / cssPixelsPerInch;
break;
case LengthTypeIN:
m_valueInSpecifiedUnits = value / cssPixelsPerInch;
break;
case LengthTypePT:
m_valueInSpecifiedUnits = value * 72 / cssPixelsPerInch;
break;
case LengthTypePC:
m_valueInSpecifiedUnits = value * 6 / cssPixelsPerInch;
break;
}
}
float SVGLength::valueAsPercentage() const
{
// 100% = 100.0 instead of 1.0 for historical reasons, this could eventually be changed
if (extractType(m_unit) == LengthTypePercentage)
return m_valueInSpecifiedUnits / 100;
return m_valueInSpecifiedUnits;
}
void SVGLength::setValueAsString(const String& string, ExceptionCode& ec)
{
if (string.isEmpty())
return;
float convertedNumber = 0;
const UChar* ptr = string.characters();
const UChar* end = ptr + string.length();
if (!parseNumber(ptr, end, convertedNumber, false)) {
ec = SYNTAX_ERR;
return;
}
SVGLengthType type = stringToLengthType(ptr, end);
ASSERT(ptr <= end);
if (type == LengthTypeUnknown) {
ec = SYNTAX_ERR;
return;
}
m_unit = storeUnit(extractMode(m_unit), type);
m_valueInSpecifiedUnits = convertedNumber;
}
String SVGLength::valueAsString() const
{
return makeString(String::number(m_valueInSpecifiedUnits), lengthTypeToString(extractType(m_unit)));
}
void SVGLength::newValueSpecifiedUnits(unsigned short type, float value, ExceptionCode& ec)
{
if (type == LengthTypeUnknown || type > LengthTypePC) {
ec = NOT_SUPPORTED_ERR;
return;
}
m_unit = storeUnit(extractMode(m_unit), static_cast<SVGLengthType>(type));
m_valueInSpecifiedUnits = value;
}
void SVGLength::convertToSpecifiedUnits(unsigned short type, const SVGElement* context, ExceptionCode& ec)
{
if (type == LengthTypeUnknown || type > LengthTypePC) {
ec = NOT_SUPPORTED_ERR;
return;
}
float valueInUserUnits = value(context, ec);
if (ec)
return;
unsigned int originalUnitAndType = m_unit;
m_unit = storeUnit(extractMode(m_unit), static_cast<SVGLengthType>(type));
setValue(valueInUserUnits, context, ec);
if (!ec)
return;
// Eventually restore old unit and type
m_unit = originalUnitAndType;
}
bool SVGLength::determineViewport(const SVGElement* context, float& width, float& height) const
{
if (!context)
return false;
// Take size from outermost <svg> element.
Document* document = context->document();
if (document->documentElement() == context) {
if (RenderView* view = toRenderView(document->renderer())) {
width = view->viewWidth();
height = view->viewHeight();
return true;
}
return false;
}
// Resolve value against nearest viewport element (common case: inner <svg> elements)
SVGElement* viewportElement = context->viewportElement();
if (viewportElement && viewportElement->isSVG()) {
const SVGSVGElement* svg = static_cast<const SVGSVGElement*>(viewportElement);
if (svg->hasAttribute(SVGNames::viewBoxAttr)) {
width = svg->viewBox().width();
height = svg->viewBox().height();
} else {
width = svg->width().value(svg);
height = svg->height().value(svg);
}
return true;
}
// Resolve value against enclosing non-SVG RenderBox
if (!context->parentNode() || context->parentNode()->isSVGElement())
return false;
RenderObject* renderer = context->renderer();
if (!renderer || !renderer->isBox())
return false;
RenderBox* box = toRenderBox(renderer);
width = box->width();
height = box->height();
return true;
}
float SVGLength::convertValueFromUserUnitsToPercentage(float value, const SVGElement* context, ExceptionCode& ec) const
{
float width = 0;
float height = 0;
if (!determineViewport(context, width, height)) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
switch (extractMode(m_unit)) {
case LengthModeWidth:
return value / width * 100;
case LengthModeHeight:
return value / height * 100;
case LengthModeOther:
return value / (sqrtf((width * width + height * height) / 2)) * 100;
};
ASSERT_NOT_REACHED();
return 0;
}
float SVGLength::convertValueFromPercentageToUserUnits(float value, const SVGElement* context, ExceptionCode& ec) const
{
float width = 0;
float height = 0;
if (!determineViewport(context, width, height)) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
switch (extractMode(m_unit)) {
case LengthModeWidth:
return value * width;
case LengthModeHeight:
return value * height;
case LengthModeOther:
return value * sqrtf((width * width + height * height) / 2);
};
ASSERT_NOT_REACHED();
return 0;
}
float SVGLength::convertValueFromUserUnitsToEMS(float value, const SVGElement* context, ExceptionCode& ec) const
{
if (!context || !context->renderer() || !context->renderer()->style()) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
RenderStyle* style = context->renderer()->style();
float fontSize = style->fontSize();
if (!fontSize) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
return value / fontSize;
}
float SVGLength::convertValueFromEMSToUserUnits(float value, const SVGElement* context, ExceptionCode& ec) const
{
if (!context || !context->renderer() || !context->renderer()->style()) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
RenderStyle* style = context->renderer()->style();
return value * style->fontSize();
}
float SVGLength::convertValueFromUserUnitsToEXS(float value, const SVGElement* context, ExceptionCode& ec) const
{
if (!context || !context->renderer() || !context->renderer()->style()) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
RenderStyle* style = context->renderer()->style();
// Use of ceil allows a pixel match to the W3Cs expected output of coords-units-03-b.svg
// if this causes problems in real world cases maybe it would be best to remove this
float xHeight = ceilf(style->fontMetrics().xHeight());
if (!xHeight) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
return value / xHeight;
}
float SVGLength::convertValueFromEXSToUserUnits(float value, const SVGElement* context, ExceptionCode& ec) const
{
if (!context || !context->renderer() || !context->renderer()->style()) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
RenderStyle* style = context->renderer()->style();
// Use of ceil allows a pixel match to the W3Cs expected output of coords-units-03-b.svg
// if this causes problems in real world cases maybe it would be best to remove this
return value * ceilf(style->fontMetrics().xHeight());
}
SVGLength SVGLength::fromCSSPrimitiveValue(CSSPrimitiveValue* value)
{
ASSERT(value);
SVGLengthType svgType;
switch (value->primitiveType()) {
case CSSPrimitiveValue::CSS_NUMBER:
svgType = LengthTypeNumber;
break;
case CSSPrimitiveValue::CSS_PERCENTAGE:
svgType = LengthTypePercentage;
break;
case CSSPrimitiveValue::CSS_EMS:
svgType = LengthTypeEMS;
break;
case CSSPrimitiveValue::CSS_EXS:
svgType = LengthTypeEXS;
break;
case CSSPrimitiveValue::CSS_PX:
svgType = LengthTypePX;
break;
case CSSPrimitiveValue::CSS_CM:
svgType = LengthTypeCM;
break;
case CSSPrimitiveValue::CSS_MM:
svgType = LengthTypeMM;
break;
case CSSPrimitiveValue::CSS_IN:
svgType = LengthTypeIN;
break;
case CSSPrimitiveValue::CSS_PT:
svgType = LengthTypePT;
break;
case CSSPrimitiveValue::CSS_PC:
svgType = LengthTypePC;
break;
case CSSPrimitiveValue::CSS_UNKNOWN:
default:
svgType = LengthTypeUnknown;
break;
};
if (svgType == LengthTypeUnknown)
return SVGLength();
ExceptionCode ec = 0;
SVGLength length;
length.newValueSpecifiedUnits(svgType, value->getFloatValue(), ec);
if (ec)
return SVGLength();
return length;
}
PassRefPtr<CSSPrimitiveValue> SVGLength::toCSSPrimitiveValue(const SVGLength& length)
{
CSSPrimitiveValue::UnitTypes cssType = CSSPrimitiveValue::CSS_UNKNOWN;
switch (length.unitType()) {
case LengthTypeUnknown:
break;
case LengthTypeNumber:
cssType = CSSPrimitiveValue::CSS_NUMBER;
break;
case LengthTypePercentage:
cssType = CSSPrimitiveValue::CSS_PERCENTAGE;
break;
case LengthTypeEMS:
cssType = CSSPrimitiveValue::CSS_EMS;
break;
case LengthTypeEXS:
cssType = CSSPrimitiveValue::CSS_EXS;
break;
case LengthTypePX:
cssType = CSSPrimitiveValue::CSS_PX;
break;
case LengthTypeCM:
cssType = CSSPrimitiveValue::CSS_CM;
break;
case LengthTypeMM:
cssType = CSSPrimitiveValue::CSS_MM;
break;
case LengthTypeIN:
cssType = CSSPrimitiveValue::CSS_IN;
break;
case LengthTypePT:
cssType = CSSPrimitiveValue::CSS_PT;
break;
case LengthTypePC:
cssType = CSSPrimitiveValue::CSS_PC;
break;
};
return CSSPrimitiveValue::create(length.valueInSpecifiedUnits(), cssType);
}
}
#endif
|