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; }
float SVGLengthContext::convertValueToUserUnits(float value, SVGLengthMode mode, CSSPrimitiveValue::UnitType fromUnit) const { float userUnits = value; switch (fromUnit) { case CSSPrimitiveValue::UnitType::Pixels: case CSSPrimitiveValue::UnitType::Number: case CSSPrimitiveValue::UnitType::UserUnits: userUnits = value; break; case CSSPrimitiveValue::UnitType::Percentage: { FloatSize viewportSize; if (!determineViewport(viewportSize)) return 0; userUnits = value * dimensionForLengthMode(mode, viewportSize) / 100; break; } case CSSPrimitiveValue::UnitType::Ems: userUnits = convertValueFromEMSToUserUnits(computedStyleForLengthResolving(m_context), value); break; case CSSPrimitiveValue::UnitType::Exs: userUnits = convertValueFromEXSToUserUnits(value); break; case CSSPrimitiveValue::UnitType::Centimeters: userUnits = value * cssPixelsPerCentimeter; break; case CSSPrimitiveValue::UnitType::Millimeters: userUnits = value * cssPixelsPerMillimeter; break; case CSSPrimitiveValue::UnitType::Inches: userUnits = value * cssPixelsPerInch; break; case CSSPrimitiveValue::UnitType::Points: userUnits = value * cssPixelsPerPoint; break; case CSSPrimitiveValue::UnitType::Picas: userUnits = value * cssPixelsPerPica; break; case CSSPrimitiveValue::UnitType::Rems: userUnits = convertValueFromEMSToUserUnits(rootElementStyle(m_context), value); break; case CSSPrimitiveValue::UnitType::Chs: userUnits = convertValueFromCHSToUserUnits(value); break; case CSSPrimitiveValue::UnitType::ViewportWidth: case CSSPrimitiveValue::UnitType::ViewportHeight: case CSSPrimitiveValue::UnitType::ViewportMin: case CSSPrimitiveValue::UnitType::ViewportMax: userUnits = value * dimensionForViewportUnit(m_context, fromUnit); break; default: ASSERT_NOT_REACHED(); break; } // Since we mix css <length> values with svg's length values we need to // clamp values to the narrowest range, otherwise it can result in // rendering issues. return CSSPrimitiveValue::clampToCSSLengthRange(userUnits); }
float SVGLengthContext::convertValueToUserUnits(float value, SVGLengthMode mode, SVGLengthType fromUnit) const { float userUnits = value; switch (fromUnit) { case LengthTypeUnknown: return 0; case LengthTypePX: case LengthTypeNumber: userUnits = value; break; case LengthTypePercentage: { FloatSize viewportSize; if (!determineViewport(viewportSize)) return 0; userUnits = value * dimensionForLengthMode(mode, viewportSize) / 100; break; } case LengthTypeEMS: userUnits = convertValueFromEMSToUserUnits(computedStyleForLengthResolving(m_context), value); break; case LengthTypeEXS: userUnits = convertValueFromEXSToUserUnits(value); break; case LengthTypeCM: userUnits = value * cssPixelsPerCentimeter; break; case LengthTypeMM: userUnits = value * cssPixelsPerMillimeter; break; case LengthTypeIN: userUnits = value * cssPixelsPerInch; break; case LengthTypePT: userUnits = value * cssPixelsPerPoint; break; case LengthTypePC: userUnits = value * cssPixelsPerPica; break; case LengthTypeREMS: userUnits = convertValueFromEMSToUserUnits(rootElementStyle(m_context), value); break; case LengthTypeCHS: userUnits = convertValueFromCHSToUserUnits(value); break; default: ASSERT_NOT_REACHED(); break; } // Since we mix css <length> values with svg's length values we need to // clamp values to the narrowest range, otherwise it can result in // rendering issues. return CSSPrimitiveValue::clampToCSSLengthRange(userUnits); }
float SVGLengthContext::convertValueToUserUnits(float value, SVGLengthMode mode, SVGLengthType fromUnit, ExceptionCode& ec) const { // If the SVGLengthContext carries a custom viewport, force resolving against it. if (!m_overridenViewport.isEmpty()) { // 100% = 100.0 instead of 1.0 for historical reasons, this could eventually be changed if (fromUnit == LengthTypePercentage) value /= 100; return convertValueFromPercentageToUserUnits(value, mode, ec); } switch (fromUnit) { case LengthTypeUnknown: ec = NOT_SUPPORTED_ERR; return 0; case LengthTypeNumber: return value; case LengthTypePX: return value; case LengthTypePercentage: return convertValueFromPercentageToUserUnits(value / 100, mode, ec); case LengthTypeEMS: return convertValueFromEMSToUserUnits(value, ec); case LengthTypeEXS: return convertValueFromEXSToUserUnits(value, ec); case LengthTypeCM: return value * cssPixelsPerInch / 2.54f; case LengthTypeMM: return value * cssPixelsPerInch / 25.4f; case LengthTypeIN: return value * cssPixelsPerInch; case LengthTypePT: return value * cssPixelsPerInch / 72; case LengthTypePC: return value * cssPixelsPerInch / 6; } ASSERT_NOT_REACHED(); return 0; }
float SVGLengthContext::convertValueToUserUnits(float value, SVGLengthMode mode, SVGLengthType fromUnit, ExceptionState& exceptionState) const { // If the SVGLengthContext carries a custom viewport, force resolving against it. if (!m_overridenViewport.isEmpty()) { // 100% = 100.0 instead of 1.0 for historical reasons, this could eventually be changed if (fromUnit == LengthTypePercentage) value /= 100; return convertValueFromPercentageToUserUnits(value, mode, exceptionState); } switch (fromUnit) { case LengthTypeUnknown: exceptionState.throwDOMException(NotSupportedError, ExceptionMessages::argumentNullOrIncorrectType(3, "SVGLengthType")); return 0; case LengthTypeNumber: return value; case LengthTypePX: return value; case LengthTypePercentage: return convertValueFromPercentageToUserUnits(value / 100, mode, exceptionState); case LengthTypeEMS: return convertValueFromEMSToUserUnits(value, exceptionState); case LengthTypeEXS: return convertValueFromEXSToUserUnits(value, exceptionState); case LengthTypeCM: return value * cssPixelsPerCentimeter; case LengthTypeMM: return value * cssPixelsPerMillimeter; case LengthTypeIN: return value * cssPixelsPerInch; case LengthTypePT: return value * cssPixelsPerPoint; case LengthTypePC: return value * cssPixelsPerPica; } ASSERT_NOT_REACHED(); return 0; }