2010-07-16 14:42:12 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-07-16 14:42:12 -07:00
|
|
|
|
2013-12-08 18:52:54 -08:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-10 22:50:08 -07:00
|
|
|
|
2010-07-16 14:42:12 -07:00
|
|
|
#include "SVGLength.h"
|
|
|
|
#include "nsSVGElement.h"
|
2013-01-09 15:02:45 -08:00
|
|
|
#include "mozilla/dom/SVGSVGElement.h"
|
2010-07-16 14:42:12 -07:00
|
|
|
#include "nsTextFormatter.h"
|
2012-09-22 12:26:05 -07:00
|
|
|
#include "SVGContentUtils.h"
|
2010-07-16 14:42:12 -07:00
|
|
|
#include <limits>
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2010-07-16 14:42:12 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// Declare some helpers defined below:
|
2012-08-22 08:56:38 -07:00
|
|
|
static void GetUnitString(nsAString& unit, uint16_t unitType);
|
2012-08-24 00:29:09 -07:00
|
|
|
static uint16_t GetUnitTypeForString(const nsAString& unitStr);
|
2010-07-16 14:42:12 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
SVGLength::GetValueAsString(nsAString &aValue) const
|
|
|
|
{
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t buf[24];
|
|
|
|
nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
|
2013-12-12 17:50:01 -08:00
|
|
|
MOZ_UTF16("%g"),
|
2010-07-16 14:42:12 -07:00
|
|
|
(double)mValue);
|
|
|
|
aValue.Assign(buf);
|
|
|
|
|
|
|
|
nsAutoString unitString;
|
|
|
|
GetUnitString(unitString, mUnit);
|
|
|
|
aValue.Append(unitString);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2013-10-29 10:15:39 -07:00
|
|
|
SVGLength::SetValueFromString(const nsAString &aString)
|
2010-07-16 14:42:12 -07:00
|
|
|
{
|
2014-01-04 07:02:17 -08:00
|
|
|
RangedPtr<const char16_t> iter =
|
2013-10-29 10:15:39 -07:00
|
|
|
SVGContentUtils::GetStartRangedPtr(aString);
|
2014-01-04 07:02:17 -08:00
|
|
|
const RangedPtr<const char16_t> end =
|
2013-10-29 10:15:39 -07:00
|
|
|
SVGContentUtils::GetEndRangedPtr(aString);
|
|
|
|
|
2013-10-01 00:50:40 -07:00
|
|
|
float value;
|
2010-07-16 14:42:12 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
if (!SVGContentUtils::ParseNumber(iter, end, value)) {
|
2013-10-01 00:50:40 -07:00
|
|
|
return false;
|
2010-07-16 14:42:12 -07:00
|
|
|
}
|
2013-10-01 00:50:40 -07:00
|
|
|
|
2013-10-29 10:15:39 -07:00
|
|
|
const nsAString& units = Substring(iter.get(), end.get());
|
2013-10-01 00:50:40 -07:00
|
|
|
uint16_t unitType = GetUnitTypeForString(units);
|
|
|
|
if (!IsValidUnitType(unitType)) {
|
|
|
|
return false;
|
2010-07-16 14:42:12 -07:00
|
|
|
}
|
2013-10-01 00:50:40 -07:00
|
|
|
mValue = value;
|
|
|
|
mUnit = uint8_t(unitType);
|
|
|
|
return true;
|
2010-07-16 14:42:12 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
inline static bool
|
2012-08-22 08:56:38 -07:00
|
|
|
IsAbsoluteUnit(uint8_t aUnit)
|
2010-07-16 14:42:12 -07:00
|
|
|
{
|
|
|
|
return aUnit >= nsIDOMSVGLength::SVG_LENGTHTYPE_CM &&
|
|
|
|
aUnit <= nsIDOMSVGLength::SVG_LENGTHTYPE_PC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to convert between different CSS absolute units without the need for
|
|
|
|
* an element, which provides more flexibility at the DOM level (and without
|
|
|
|
* the need for an intermediary conversion to user units, which avoids
|
|
|
|
* unnecessary overhead and rounding error).
|
|
|
|
*
|
|
|
|
* Example usage: to find out how many centimeters there are per inch:
|
|
|
|
*
|
|
|
|
* GetAbsUnitsPerAbsUnit(nsIDOMSVGLength::SVG_LENGTHTYPE_CM,
|
|
|
|
* nsIDOMSVGLength::SVG_LENGTHTYPE_IN)
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
inline static float GetAbsUnitsPerAbsUnit(uint8_t aUnits, uint8_t aPerUnit)
|
2010-07-16 14:42:12 -07:00
|
|
|
{
|
2015-02-09 14:34:50 -08:00
|
|
|
MOZ_ASSERT(IsAbsoluteUnit(aUnits), "Not a CSS absolute unit");
|
|
|
|
MOZ_ASSERT(IsAbsoluteUnit(aPerUnit), "Not a CSS absolute unit");
|
2010-07-16 14:42:12 -07:00
|
|
|
|
|
|
|
float CSSAbsoluteUnitConversionFactors[5][5] = { // columns: cm, mm, in, pt, pc
|
|
|
|
// cm per...:
|
2013-04-07 01:46:48 -07:00
|
|
|
{ 1.0f, 0.1f, 2.54f, 0.035277777777777778f, 0.42333333333333333f },
|
2010-07-16 14:42:12 -07:00
|
|
|
// mm per...:
|
2013-04-07 01:46:48 -07:00
|
|
|
{ 10.0f, 1.0f, 25.4f, 0.35277777777777778f, 4.2333333333333333f },
|
2010-07-16 14:42:12 -07:00
|
|
|
// in per...:
|
2013-04-07 01:46:48 -07:00
|
|
|
{ 0.39370078740157481f, 0.039370078740157481f, 1.0f, 0.013888888888888889f, 0.16666666666666667f },
|
2010-07-16 14:42:12 -07:00
|
|
|
// pt per...:
|
2013-04-07 01:46:48 -07:00
|
|
|
{ 28.346456692913386f, 2.8346456692913386f, 72.0f, 1.0f, 12.0f },
|
2010-07-16 14:42:12 -07:00
|
|
|
// pc per...:
|
2013-04-07 01:46:48 -07:00
|
|
|
{ 2.3622047244094489f, 0.23622047244094489f, 6.0f, 0.083333333333333333f, 1.0f }
|
2010-07-16 14:42:12 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// First absolute unit is SVG_LENGTHTYPE_CM = 6
|
|
|
|
return CSSAbsoluteUnitConversionFactors[aUnits - 6][aPerUnit - 6];
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
2012-08-22 08:56:38 -07:00
|
|
|
SVGLength::GetValueInSpecifiedUnit(uint8_t aUnit,
|
2010-07-16 14:42:12 -07:00
|
|
|
const nsSVGElement *aElement,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t aAxis) const
|
2010-07-16 14:42:12 -07:00
|
|
|
{
|
|
|
|
if (aUnit == mUnit) {
|
|
|
|
return mValue;
|
|
|
|
}
|
|
|
|
if ((aUnit == nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER &&
|
|
|
|
mUnit == nsIDOMSVGLength::SVG_LENGTHTYPE_PX) ||
|
|
|
|
(aUnit == nsIDOMSVGLength::SVG_LENGTHTYPE_PX &&
|
|
|
|
mUnit == nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER)) {
|
|
|
|
return mValue;
|
|
|
|
}
|
|
|
|
if (IsAbsoluteUnit(aUnit) && IsAbsoluteUnit(mUnit)) {
|
|
|
|
return mValue * GetAbsUnitsPerAbsUnit(aUnit, mUnit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we do a two step convertion via user units. This can only
|
|
|
|
// succeed if aElement is non-null (although that's not sufficent to
|
|
|
|
// guarantee success).
|
|
|
|
|
|
|
|
float userUnitsPerCurrentUnit = GetUserUnitsPerUnit(aElement, aAxis);
|
|
|
|
float userUnitsPerNewUnit =
|
|
|
|
SVGLength(0.0f, aUnit).GetUserUnitsPerUnit(aElement, aAxis);
|
|
|
|
|
|
|
|
NS_ASSERTION(userUnitsPerCurrentUnit >= 0 ||
|
2014-10-01 02:26:00 -07:00
|
|
|
!IsFinite(userUnitsPerCurrentUnit),
|
2010-07-16 14:42:12 -07:00
|
|
|
"bad userUnitsPerCurrentUnit");
|
|
|
|
NS_ASSERTION(userUnitsPerNewUnit >= 0 ||
|
2014-10-01 02:26:00 -07:00
|
|
|
!IsFinite(userUnitsPerNewUnit),
|
2010-07-16 14:42:12 -07:00
|
|
|
"bad userUnitsPerNewUnit");
|
|
|
|
|
|
|
|
float value = mValue * userUnitsPerCurrentUnit / userUnitsPerNewUnit;
|
|
|
|
|
|
|
|
// userUnitsPerCurrentUnit could be infinity, or userUnitsPerNewUnit could
|
|
|
|
// be zero.
|
2014-10-01 02:26:00 -07:00
|
|
|
if (IsFinite(value)) {
|
2010-07-16 14:42:12 -07:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return std::numeric_limits<float>::quiet_NaN();
|
|
|
|
}
|
|
|
|
|
|
|
|
#define INCHES_PER_MM_FLOAT float(0.0393700787)
|
|
|
|
#define INCHES_PER_CM_FLOAT float(0.393700787)
|
|
|
|
|
|
|
|
float
|
2012-08-22 08:56:38 -07:00
|
|
|
SVGLength::GetUserUnitsPerUnit(const nsSVGElement *aElement, uint8_t aAxis) const
|
2010-07-16 14:42:12 -07:00
|
|
|
{
|
|
|
|
switch (mUnit) {
|
|
|
|
case nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER:
|
|
|
|
case nsIDOMSVGLength::SVG_LENGTHTYPE_PX:
|
|
|
|
return 1.0f;
|
|
|
|
case nsIDOMSVGLength::SVG_LENGTHTYPE_MM:
|
2010-08-13 02:58:04 -07:00
|
|
|
return INCHES_PER_MM_FLOAT * GetUserUnitsPerInch();
|
2010-07-16 14:42:12 -07:00
|
|
|
case nsIDOMSVGLength::SVG_LENGTHTYPE_CM:
|
2010-08-13 02:58:04 -07:00
|
|
|
return INCHES_PER_CM_FLOAT * GetUserUnitsPerInch();
|
2010-07-16 14:42:12 -07:00
|
|
|
case nsIDOMSVGLength::SVG_LENGTHTYPE_IN:
|
2010-08-13 02:58:04 -07:00
|
|
|
return GetUserUnitsPerInch();
|
2010-07-16 14:42:12 -07:00
|
|
|
case nsIDOMSVGLength::SVG_LENGTHTYPE_PT:
|
2010-08-13 02:58:04 -07:00
|
|
|
return (1.0f/POINTS_PER_INCH_FLOAT) * GetUserUnitsPerInch();
|
2010-07-16 14:42:12 -07:00
|
|
|
case nsIDOMSVGLength::SVG_LENGTHTYPE_PC:
|
2010-08-13 02:58:04 -07:00
|
|
|
return (12.0f/POINTS_PER_INCH_FLOAT) * GetUserUnitsPerInch();
|
2010-07-16 14:42:12 -07:00
|
|
|
case nsIDOMSVGLength::SVG_LENGTHTYPE_PERCENTAGE:
|
|
|
|
return GetUserUnitsPerPercent(aElement, aAxis);
|
|
|
|
case nsIDOMSVGLength::SVG_LENGTHTYPE_EMS:
|
2012-09-22 12:26:05 -07:00
|
|
|
return SVGContentUtils::GetFontSize(const_cast<nsSVGElement*>(aElement));
|
2010-07-16 14:42:12 -07:00
|
|
|
case nsIDOMSVGLength::SVG_LENGTHTYPE_EXS:
|
2012-09-22 12:26:05 -07:00
|
|
|
return SVGContentUtils::GetFontXHeight(const_cast<nsSVGElement*>(aElement));
|
2010-07-16 14:42:12 -07:00
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown unit type");
|
|
|
|
return std::numeric_limits<float>::quiet_NaN();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ float
|
2012-08-22 08:56:38 -07:00
|
|
|
SVGLength::GetUserUnitsPerPercent(const nsSVGElement *aElement, uint8_t aAxis)
|
2010-07-16 14:42:12 -07:00
|
|
|
{
|
|
|
|
if (aElement) {
|
2013-01-11 00:43:01 -08:00
|
|
|
dom::SVGSVGElement *viewportElement = aElement->GetCtx();
|
2010-07-16 14:42:12 -07:00
|
|
|
if (viewportElement) {
|
2013-01-15 04:22:03 -08:00
|
|
|
return std::max(viewportElement->GetLength(aAxis) / 100.0f, 0.0f);
|
2010-07-16 14:42:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::numeric_limits<float>::quiet_NaN();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helpers:
|
|
|
|
|
|
|
|
// These items must be at the same index as the nsIDOMSVGLength constants!
|
|
|
|
static nsIAtom** const unitMap[] =
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, /* SVG_LENGTHTYPE_UNKNOWN */
|
|
|
|
nullptr, /* SVG_LENGTHTYPE_NUMBER */
|
2010-07-16 14:42:12 -07:00
|
|
|
&nsGkAtoms::percentage,
|
|
|
|
&nsGkAtoms::em,
|
|
|
|
&nsGkAtoms::ex,
|
|
|
|
&nsGkAtoms::px,
|
|
|
|
&nsGkAtoms::cm,
|
|
|
|
&nsGkAtoms::mm,
|
|
|
|
&nsGkAtoms::in,
|
|
|
|
&nsGkAtoms::pt,
|
|
|
|
&nsGkAtoms::pc
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2012-08-22 08:56:38 -07:00
|
|
|
GetUnitString(nsAString& unit, uint16_t unitType)
|
2010-07-16 14:42:12 -07:00
|
|
|
{
|
|
|
|
if (SVGLength::IsValidUnitType(unitType)) {
|
|
|
|
if (unitMap[unitType]) {
|
|
|
|
(*unitMap[unitType])->ToString(unit);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NS_NOTREACHED("Unknown unit type"); // Someone's using an SVGLength with an invalid unit?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint16_t
|
2012-08-24 00:29:09 -07:00
|
|
|
GetUnitTypeForString(const nsAString& unitStr)
|
2010-07-16 14:42:12 -07:00
|
|
|
{
|
2012-08-24 00:29:09 -07:00
|
|
|
if (unitStr.IsEmpty())
|
2010-07-16 14:42:12 -07:00
|
|
|
return nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER;
|
|
|
|
|
2012-08-24 00:29:09 -07:00
|
|
|
nsIAtom* unitAtom = NS_GetStaticAtom(unitStr);
|
2010-07-16 14:42:12 -07:00
|
|
|
|
2012-08-24 00:29:09 -07:00
|
|
|
if (unitAtom) {
|
|
|
|
for (uint32_t i = 1 ; i < ArrayLength(unitMap) ; i++) {
|
|
|
|
if (unitMap[i] && *unitMap[i] == unitAtom) {
|
|
|
|
return i;
|
|
|
|
}
|
2010-07-16 14:42:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nsIDOMSVGLength::SVG_LENGTHTYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|