2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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-17 01:42:12 +04:00
|
|
|
|
|
|
|
#include "SVGLength.h"
|
2018-12-22 20:04:05 +03:00
|
|
|
|
|
|
|
#include "mozilla/ArrayUtils.h"
|
|
|
|
#include "mozilla/dom/SVGElement.h"
|
2013-01-10 03:02:45 +04:00
|
|
|
#include "mozilla/dom/SVGSVGElement.h"
|
2010-07-17 01:42:12 +04:00
|
|
|
#include "nsTextFormatter.h"
|
2012-09-22 23:26:05 +04:00
|
|
|
#include "SVGContentUtils.h"
|
2010-07-17 01:42:12 +04:00
|
|
|
#include <limits>
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2010-07-17 01:42:12 +04:00
|
|
|
|
2018-12-22 20:04:05 +03:00
|
|
|
using namespace mozilla::dom;
|
2018-12-22 02:29:46 +03:00
|
|
|
using namespace mozilla::dom::SVGLength_Binding;
|
2010-07-17 01:42:12 +04:00
|
|
|
|
2018-12-22 20:04:05 +03:00
|
|
|
namespace mozilla {
|
2018-02-02 16:21:33 +03:00
|
|
|
|
2010-07-17 01:42:12 +04:00
|
|
|
// Declare some helpers defined below:
|
2012-08-22 19:56:38 +04:00
|
|
|
static void GetUnitString(nsAString& unit, uint16_t unitType);
|
2012-08-24 11:29:09 +04:00
|
|
|
static uint16_t GetUnitTypeForString(const nsAString& unitStr);
|
2010-07-17 01:42:12 +04:00
|
|
|
|
|
|
|
void SVGLength::GetValueAsString(nsAString& aValue) const {
|
2017-09-06 17:19:05 +03:00
|
|
|
nsTextFormatter::ssprintf(aValue, u"%g", (double)mValue);
|
2010-07-17 01:42:12 +04:00
|
|
|
|
|
|
|
nsAutoString unitString;
|
|
|
|
GetUnitString(unitString, mUnit);
|
|
|
|
aValue.Append(unitString);
|
|
|
|
}
|
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
bool SVGLength::SetValueFromString(const nsAString& aString) {
|
2019-03-27 11:52:33 +03:00
|
|
|
bool success;
|
|
|
|
auto token = SVGContentUtils::GetAndEnsureOneToken(aString, success);
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RangedPtr<const char16_t> iter = SVGContentUtils::GetStartRangedPtr(token);
|
|
|
|
const RangedPtr<const char16_t> end = SVGContentUtils::GetEndRangedPtr(token);
|
2013-10-29 21:15:39 +04:00
|
|
|
|
2013-10-01 11:50:40 +04:00
|
|
|
float value;
|
2010-07-17 01:42:12 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
if (!SVGContentUtils::ParseNumber(iter, end, value)) {
|
2013-10-01 11:50:40 +04:00
|
|
|
return false;
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
2013-10-01 11:50:40 +04:00
|
|
|
|
2013-10-29 21:15:39 +04:00
|
|
|
const nsAString& units = Substring(iter.get(), end.get());
|
2013-10-01 11:50:40 +04:00
|
|
|
uint16_t unitType = GetUnitTypeForString(units);
|
|
|
|
if (!IsValidUnitType(unitType)) {
|
|
|
|
return false;
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
2013-10-01 11:50:40 +04:00
|
|
|
mValue = value;
|
|
|
|
mUnit = uint8_t(unitType);
|
|
|
|
return true;
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
inline static bool IsAbsoluteUnit(uint8_t aUnit) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return aUnit >= SVGLength_Binding::SVG_LENGTHTYPE_CM &&
|
|
|
|
aUnit <= SVGLength_Binding::SVG_LENGTHTYPE_PC;
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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:
|
|
|
|
*
|
2018-06-26 00:20:54 +03:00
|
|
|
* GetAbsUnitsPerAbsUnit(SVGLength_Binding::SVG_LENGTHTYPE_CM,
|
|
|
|
* SVGLength_Binding::SVG_LENGTHTYPE_IN)
|
2010-07-17 01:42:12 +04:00
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
inline static float GetAbsUnitsPerAbsUnit(uint8_t aUnits, uint8_t aPerUnit) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(IsAbsoluteUnit(aUnits), "Not a CSS absolute unit");
|
|
|
|
MOZ_ASSERT(IsAbsoluteUnit(aPerUnit), "Not a CSS absolute unit");
|
2010-07-17 01:42:12 +04:00
|
|
|
|
|
|
|
float CSSAbsoluteUnitConversionFactors[5][5] = {
|
|
|
|
// columns: cm, mm, in, pt, pc
|
|
|
|
// cm per...:
|
2013-04-07 12:46:48 +04:00
|
|
|
{1.0f, 0.1f, 2.54f, 0.035277777777777778f, 0.42333333333333333f},
|
2010-07-17 01:42:12 +04:00
|
|
|
// mm per...:
|
2013-04-07 12:46:48 +04:00
|
|
|
{10.0f, 1.0f, 25.4f, 0.35277777777777778f, 4.2333333333333333f},
|
2010-07-17 01:42:12 +04:00
|
|
|
// in per...:
|
2013-04-07 12:46:48 +04:00
|
|
|
{0.39370078740157481f, 0.039370078740157481f, 1.0f, 0.013888888888888889f,
|
|
|
|
0.16666666666666667f},
|
2010-07-17 01:42:12 +04:00
|
|
|
// pt per...:
|
2013-04-07 12:46:48 +04:00
|
|
|
{28.346456692913386f, 2.8346456692913386f, 72.0f, 1.0f, 12.0f},
|
2010-07-17 01:42:12 +04:00
|
|
|
// pc per...:
|
2013-04-07 12:46:48 +04:00
|
|
|
{2.3622047244094489f, 0.23622047244094489f, 6.0f, 0.083333333333333333f,
|
|
|
|
1.0f}};
|
2010-07-17 01:42:12 +04:00
|
|
|
|
|
|
|
// First absolute unit is SVG_LENGTHTYPE_CM = 6
|
|
|
|
return CSSAbsoluteUnitConversionFactors[aUnits - 6][aPerUnit - 6];
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
float SVGLength::GetValueInSpecifiedUnit(uint8_t aUnit,
|
2018-12-21 11:58:14 +03:00
|
|
|
const SVGElement* aElement,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t aAxis) const {
|
2010-07-17 01:42:12 +04:00
|
|
|
if (aUnit == mUnit) {
|
|
|
|
return mValue;
|
|
|
|
}
|
2018-06-26 00:20:54 +03:00
|
|
|
if ((aUnit == SVGLength_Binding::SVG_LENGTHTYPE_NUMBER &&
|
|
|
|
mUnit == SVGLength_Binding::SVG_LENGTHTYPE_PX) ||
|
|
|
|
(aUnit == SVGLength_Binding::SVG_LENGTHTYPE_PX &&
|
|
|
|
mUnit == SVGLength_Binding::SVG_LENGTHTYPE_NUMBER)) {
|
2010-07-17 01:42:12 +04:00
|
|
|
return mValue;
|
|
|
|
}
|
|
|
|
if (IsAbsoluteUnit(aUnit) && IsAbsoluteUnit(mUnit)) {
|
|
|
|
return mValue * GetAbsUnitsPerAbsUnit(aUnit, mUnit);
|
|
|
|
}
|
|
|
|
|
2018-03-18 21:09:51 +03:00
|
|
|
// Otherwise we do a two step conversion via user units. This can only
|
|
|
|
// succeed if aElement is non-null (although that's not sufficient to
|
2010-07-17 01:42:12 +04:00
|
|
|
// guarantee success).
|
|
|
|
|
|
|
|
float userUnitsPerCurrentUnit = GetUserUnitsPerUnit(aElement, aAxis);
|
|
|
|
float userUnitsPerNewUnit =
|
|
|
|
SVGLength(0.0f, aUnit).GetUserUnitsPerUnit(aElement, aAxis);
|
|
|
|
|
|
|
|
NS_ASSERTION(
|
2014-10-01 13:26:00 +04:00
|
|
|
userUnitsPerCurrentUnit >= 0 || !IsFinite(userUnitsPerCurrentUnit),
|
2010-07-17 01:42:12 +04:00
|
|
|
"bad userUnitsPerCurrentUnit");
|
|
|
|
NS_ASSERTION(userUnitsPerNewUnit >= 0 || !IsFinite(userUnitsPerNewUnit),
|
|
|
|
"bad userUnitsPerNewUnit");
|
|
|
|
|
|
|
|
float value = mValue * userUnitsPerCurrentUnit / userUnitsPerNewUnit;
|
|
|
|
|
|
|
|
// userUnitsPerCurrentUnit could be infinity, or userUnitsPerNewUnit could
|
|
|
|
// be zero.
|
2014-10-01 13:26:00 +04:00
|
|
|
if (IsFinite(value)) {
|
2010-07-17 01:42:12 +04: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)
|
|
|
|
|
2018-12-21 11:58:14 +03:00
|
|
|
float SVGLength::GetUserUnitsPerUnit(const SVGElement* aElement,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t aAxis) const {
|
2010-07-17 01:42:12 +04:00
|
|
|
switch (mUnit) {
|
2018-06-26 00:20:54 +03:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_NUMBER:
|
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_PX:
|
2010-07-17 01:42:12 +04:00
|
|
|
return 1.0f;
|
2018-06-26 00:20:54 +03:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_MM:
|
2010-08-13 13:58:04 +04:00
|
|
|
return INCHES_PER_MM_FLOAT * GetUserUnitsPerInch();
|
2018-06-26 00:20:54 +03:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_CM:
|
2010-08-13 13:58:04 +04:00
|
|
|
return INCHES_PER_CM_FLOAT * GetUserUnitsPerInch();
|
2018-06-26 00:20:54 +03:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_IN:
|
2010-08-13 13:58:04 +04:00
|
|
|
return GetUserUnitsPerInch();
|
2018-06-26 00:20:54 +03:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_PT:
|
2010-08-13 13:58:04 +04:00
|
|
|
return (1.0f / POINTS_PER_INCH_FLOAT) * GetUserUnitsPerInch();
|
2018-06-26 00:20:54 +03:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_PC:
|
2010-08-13 13:58:04 +04:00
|
|
|
return (12.0f / POINTS_PER_INCH_FLOAT) * GetUserUnitsPerInch();
|
2018-06-26 00:20:54 +03:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_PERCENTAGE:
|
2010-07-17 01:42:12 +04:00
|
|
|
return GetUserUnitsPerPercent(aElement, aAxis);
|
2018-06-26 00:20:54 +03:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_EMS:
|
2018-12-21 11:58:14 +03:00
|
|
|
return SVGContentUtils::GetFontSize(const_cast<SVGElement*>(aElement));
|
2018-06-26 00:20:54 +03:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_EXS:
|
2018-12-21 11:58:14 +03:00
|
|
|
return SVGContentUtils::GetFontXHeight(const_cast<SVGElement*>(aElement));
|
2010-07-17 01:42:12 +04:00
|
|
|
default:
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown unit type");
|
2010-07-17 01:42:12 +04:00
|
|
|
return std::numeric_limits<float>::quiet_NaN();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
float SVGLength::GetUserUnitsPerPercent(const SVGElement* aElement,
|
|
|
|
uint8_t aAxis) {
|
2010-07-17 01:42:12 +04:00
|
|
|
if (aElement) {
|
2017-10-10 09:58:34 +03:00
|
|
|
dom::SVGViewportElement* viewportElement = aElement->GetCtx();
|
2010-07-17 01:42:12 +04:00
|
|
|
if (viewportElement) {
|
2013-01-15 16:22:03 +04:00
|
|
|
return std::max(viewportElement->GetLength(aAxis) / 100.0f, 0.0f);
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::numeric_limits<float>::quiet_NaN();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helpers:
|
|
|
|
|
2018-02-02 16:21:33 +03:00
|
|
|
// These items must be at the same index as the SVGLength constants!
|
2018-04-03 10:29:37 +03:00
|
|
|
static const nsStaticAtom* const unitMap[] = {
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr, /* SVG_LENGTHTYPE_UNKNOWN */
|
|
|
|
nullptr, /* SVG_LENGTHTYPE_NUMBER */
|
2018-04-03 10:29:37 +03:00
|
|
|
nsGkAtoms::percentage,
|
|
|
|
nsGkAtoms::em,
|
|
|
|
nsGkAtoms::ex,
|
|
|
|
nsGkAtoms::px,
|
|
|
|
nsGkAtoms::cm,
|
|
|
|
nsGkAtoms::mm,
|
|
|
|
nsGkAtoms::in,
|
|
|
|
nsGkAtoms::pt,
|
|
|
|
nsGkAtoms::pc};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static void GetUnitString(nsAString& unit, uint16_t unitType) {
|
2010-07-17 01:42:12 +04:00
|
|
|
if (SVGLength::IsValidUnitType(unitType)) {
|
|
|
|
if (unitMap[unitType]) {
|
2018-04-03 10:29:37 +03:00
|
|
|
unitMap[unitType]->ToString(unit);
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE(
|
|
|
|
"Unknown unit type! Someone's using an SVGLength "
|
|
|
|
"with an invalid unit?");
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
|
|
|
|
2012-08-24 11:29:09 +04:00
|
|
|
static uint16_t GetUnitTypeForString(const nsAString& unitStr) {
|
2018-06-26 00:20:54 +03:00
|
|
|
if (unitStr.IsEmpty()) return SVGLength_Binding::SVG_LENGTHTYPE_NUMBER;
|
2010-07-17 01:42:12 +04:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* unitAtom = NS_GetStaticAtom(unitStr);
|
2010-07-17 01:42:12 +04:00
|
|
|
|
2012-08-24 11:29:09 +04:00
|
|
|
if (unitAtom) {
|
|
|
|
for (uint32_t i = 1; i < ArrayLength(unitMap); i++) {
|
2018-04-03 10:29:37 +03:00
|
|
|
if (unitMap[i] == unitAtom) {
|
2012-08-24 11:29:09 +04:00
|
|
|
return i;
|
|
|
|
}
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
|
|
|
}
|
2018-06-26 00:20:54 +03:00
|
|
|
return SVGLength_Binding::SVG_LENGTHTYPE_UNKNOWN;
|
2010-07-17 01:42:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|