#include "config.h"
#if ENABLE(SVG)
#include "SVGLength.h"
#include "CSSHelper.h"
#include "DeprecatedString.h"
#include "FloatConversion.h"
#include "FrameView.h"
#include "RenderObject.h"
#include "RenderView.h"
#include "SVGParserUtilities.h"
#include "SVGSVGElement.h"
#include "SVGStyledElement.h"
#include <math.h>
#include <wtf/Assertions.h>
namespace WebCore {
inline unsigned int storeUnit(SVGLengthMode mode, SVGLengthType type)
{
return (mode << 4) | type;
}
inline SVGLengthMode extractMode(unsigned int unit)
{
unsigned int mode = unit >> 4;
return static_cast<SVGLengthMode>(mode);
}
inline SVGLengthType extractType(unsigned int unit)
{
unsigned int mode = unit >> 4;
unsigned int type = unit ^ (mode << 4);
return static_cast<SVGLengthType>(type);
}
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";
}
return String();
}
inline SVGLengthType stringToLengthType(const String& string)
{
if (string.endsWith("%"))
return LengthTypePercentage;
else if (string.endsWith("em"))
return LengthTypeEMS;
else if (string.endsWith("ex"))
return LengthTypeEXS;
else if (string.endsWith("px"))
return LengthTypePX;
else if (string.endsWith("cm"))
return LengthTypeCM;
else if (string.endsWith("mm"))
return LengthTypeMM;
else if (string.endsWith("in"))
return LengthTypeIN;
else if (string.endsWith("pt"))
return LengthTypePT;
else if (string.endsWith("pc"))
return LengthTypePC;
else if (!string.isEmpty())
return LengthTypeNumber;
return LengthTypeUnknown;
}
SVGLength::SVGLength(const SVGStyledElement* context, SVGLengthMode mode, const String& valueAsString)
: m_valueInSpecifiedUnits(0.0f)
, m_unit(storeUnit(mode, LengthTypeNumber))
, m_context(context)
{
setValueAsString(valueAsString);
}
SVGLengthType SVGLength::unitType() const
{
return extractType(m_unit);
}
float SVGLength::value() const
{
SVGLengthType type = extractType(m_unit);
if (type == LengthTypeUnknown)
return 0.0f;
switch (type) {
case LengthTypeNumber:
return m_valueInSpecifiedUnits;
case LengthTypePercentage:
return SVGLength::PercentageOfViewport(m_valueInSpecifiedUnits / 100.0f, m_context, extractMode(m_unit));
case LengthTypeEMS:
case LengthTypeEXS:
{
RenderStyle* style = 0;
if (m_context && m_context->renderer())
style = m_context->renderer()->style();
if (style) {
float useSize = style->fontSize();
ASSERT(useSize > 0);
if (type == LengthTypeEMS)
return m_valueInSpecifiedUnits * useSize;
else {
float xHeight = style->font().xHeight();
return m_valueInSpecifiedUnits * ceilf(xHeight);
}
}
return 0.0f;
}
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.0f * cssPixelsPerInch;
case LengthTypePC:
return m_valueInSpecifiedUnits / 6.0f * cssPixelsPerInch;
default:
break;
}
ASSERT_NOT_REACHED();
return 0.0f;
}
void SVGLength::setValue(float value)
{
SVGLengthType type = extractType(m_unit);
ASSERT(type != LengthTypeUnknown);
switch (type) {
case LengthTypeNumber:
m_valueInSpecifiedUnits = value;
break;
case LengthTypePercentage:
case LengthTypeEMS:
case LengthTypeEXS:
ASSERT_NOT_REACHED();
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.0f / cssPixelsPerInch;
break;
case LengthTypePC:
m_valueInSpecifiedUnits = value / 6.0f * cssPixelsPerInch;
break;
default:
break;
}
}
void SVGLength::setValueInSpecifiedUnits(float value)
{
m_valueInSpecifiedUnits = value;
}
float SVGLength::valueInSpecifiedUnits() const
{
return m_valueInSpecifiedUnits;
}
float SVGLength::valueAsPercentage() const
{
if (extractType(m_unit) == LengthTypePercentage)
return valueInSpecifiedUnits() / 100.0f;
return valueInSpecifiedUnits();
}
void SVGLength::setValueAsString(const String& s)
{
if (s.isEmpty())
return;
double convertedNumber = 0;
const UChar* ptr = s.characters();
const UChar* end = ptr + s.length();
parseNumber(ptr, end, convertedNumber, false);
m_unit = storeUnit(extractMode(m_unit), stringToLengthType(s));
m_valueInSpecifiedUnits = narrowPrecisionToFloat(convertedNumber);
}
String SVGLength::valueAsString() const
{
return String::number(m_valueInSpecifiedUnits) + lengthTypeToString(extractType(m_unit));
}
void SVGLength::newValueSpecifiedUnits(unsigned short type, float value)
{
ASSERT(type <= LengthTypePC);
m_unit = storeUnit(extractMode(m_unit), (SVGLengthType) type);
m_valueInSpecifiedUnits = value;
}
void SVGLength::convertToSpecifiedUnits(unsigned short type)
{
ASSERT(type <= LengthTypePC);
float valueInUserUnits = value();
m_unit = storeUnit(extractMode(m_unit), (SVGLengthType) type);
setValue(valueInUserUnits);
}
float SVGLength::PercentageOfViewport(float value, const SVGStyledElement* context, SVGLengthMode mode)
{
ASSERT(context);
float width = 0, height = 0;
SVGElement* viewportElement = context->viewportElement();
Document* doc = context->document();
if (doc->documentElement() == context) {
RenderView* view = static_cast<RenderView*>(doc->renderer());
if (view && view->frameView()) {
width = view->frameView()->visibleWidth(); height = view->frameView()->visibleHeight(); }
} else 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();
height = svg->height().value();
}
} else if (context->parent() && !context->parent()->isSVGElement()) {
if (RenderObject* renderer = context->renderer()) {
width = renderer->width();
height = renderer->height();
}
}
if (mode == LengthModeWidth)
return value * width;
else if (mode == LengthModeHeight)
return value * height;
else if (mode == LengthModeOther)
return value * sqrtf(powf(width, 2) + powf(height, 2)) / sqrtf(2.0f);
return 0.0f;
}
}
#endif // ENABLE(SVG)