2015-05-03 19:32:37 +00: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 11:12:37 +00: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/. */
|
2006-04-14 15:09:39 +00:00
|
|
|
|
|
|
|
#include "nsSVGLength2.h"
|
2018-12-28 02:47:10 +00:00
|
|
|
|
|
|
|
#include "mozilla/ArrayUtils.h"
|
2019-01-23 13:48:08 +00:00
|
|
|
#include "mozilla/SMILValue.h"
|
2013-09-17 12:52:39 +00:00
|
|
|
#include "mozilla/dom/SVGAnimatedLength.h"
|
2017-08-21 08:19:31 +00:00
|
|
|
#include "mozilla/dom/SVGViewportElement.h"
|
2019-01-23 13:48:08 +00:00
|
|
|
#include "nsContentUtils.h"
|
2019-01-23 21:48:00 +00:00
|
|
|
#include "DOMSVGLength.h"
|
2013-09-17 12:52:39 +00:00
|
|
|
#include "nsIFrame.h"
|
2019-01-23 21:48:00 +00:00
|
|
|
#include "LayoutLogging.h"
|
2018-12-24 19:24:45 +00:00
|
|
|
#include "SMILFloatType.h"
|
2019-01-23 21:48:00 +00:00
|
|
|
#include "SVGAttrTearoffTable.h"
|
2013-09-17 12:52:39 +00:00
|
|
|
#include "nsSVGIntegrationUtils.h"
|
2019-01-23 13:48:08 +00:00
|
|
|
#include "nsTextFormatter.h"
|
2006-04-14 15:09:39 +00:00
|
|
|
|
2011-10-11 05:50:08 +00:00
|
|
|
using namespace mozilla;
|
2013-01-09 23:02:45 +00:00
|
|
|
using namespace mozilla::dom;
|
2011-10-11 05:50:08 +00:00
|
|
|
|
2018-04-03 07:29:37 +00:00
|
|
|
static const nsStaticAtom* const unitMap[] = {
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr, /* SVG_LENGTHTYPE_UNKNOWN */
|
|
|
|
nullptr, /* SVG_LENGTHTYPE_NUMBER */
|
2018-04-03 07:29:37 +00:00
|
|
|
nsGkAtoms::percentage,
|
|
|
|
nsGkAtoms::em,
|
|
|
|
nsGkAtoms::ex,
|
|
|
|
nsGkAtoms::px,
|
|
|
|
nsGkAtoms::cm,
|
|
|
|
nsGkAtoms::mm,
|
|
|
|
nsGkAtoms::in,
|
|
|
|
nsGkAtoms::pt,
|
|
|
|
nsGkAtoms::pc};
|
2007-10-03 09:02:19 +00:00
|
|
|
|
2019-01-23 21:48:00 +00:00
|
|
|
static SVGAttrTearoffTable<nsSVGLength2, SVGAnimatedLength>
|
2009-08-27 04:30:51 +00:00
|
|
|
sSVGAnimatedLengthTearoffTable;
|
|
|
|
|
2006-04-14 15:09:39 +00:00
|
|
|
/* Helper functions */
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static bool IsValidUnitType(uint16_t unit) {
|
2018-06-25 21:20:54 +00:00
|
|
|
if (unit > SVGLength_Binding::SVG_LENGTHTYPE_UNKNOWN &&
|
|
|
|
unit <= SVGLength_Binding::SVG_LENGTHTYPE_PC)
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2006-04-14 15:09:39 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2006-04-14 15:09:39 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static void GetUnitString(nsAString& unit, uint16_t unitType) {
|
2007-10-03 09:02:19 +00:00
|
|
|
if (IsValidUnitType(unitType)) {
|
|
|
|
if (unitMap[unitType]) {
|
2018-04-03 07:29:37 +00:00
|
|
|
unitMap[unitType]->ToString(unit);
|
2007-10-03 09:02:19 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-18 05:43:11 +00:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown unit type");
|
2007-10-03 09:02:19 +00:00
|
|
|
}
|
|
|
|
|
2012-08-24 07:29:09 +00:00
|
|
|
static uint16_t GetUnitTypeForString(const nsAString& unitStr) {
|
2018-06-25 21:20:54 +00:00
|
|
|
if (unitStr.IsEmpty()) return SVGLength_Binding::SVG_LENGTHTYPE_NUMBER;
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2017-10-02 22:05:19 +00:00
|
|
|
nsAtom* unitAtom = NS_GetStaticAtom(unitStr);
|
2012-08-24 07:29:09 +00:00
|
|
|
if (unitAtom) {
|
|
|
|
for (uint32_t i = 0; i < ArrayLength(unitMap); i++) {
|
2018-04-03 07:29:37 +00:00
|
|
|
if (unitMap[i] == unitAtom) {
|
2012-08-24 07:29:09 +00:00
|
|
|
return i;
|
|
|
|
}
|
2007-10-03 09:02:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 21:20:54 +00:00
|
|
|
return SVGLength_Binding::SVG_LENGTHTYPE_UNKNOWN;
|
2007-10-03 09:02:19 +00:00
|
|
|
}
|
2006-04-14 15:09:39 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static void GetValueString(nsAString& aValueAsString, float aValue,
|
|
|
|
uint16_t aUnitType) {
|
2017-09-06 14:19:05 +00:00
|
|
|
nsTextFormatter::ssprintf(aValueAsString, u"%g", (double)aValue);
|
2006-04-14 15:09:39 +00:00
|
|
|
|
|
|
|
nsAutoString unitString;
|
2007-10-03 09:02:19 +00:00
|
|
|
GetUnitString(unitString, aUnitType);
|
2006-04-14 15:09:39 +00:00
|
|
|
aValueAsString.Append(unitString);
|
|
|
|
}
|
|
|
|
|
2013-10-29 17:15:39 +00:00
|
|
|
static bool GetValueFromString(const nsAString& aString, float& aValue,
|
2013-10-01 07:50:40 +00:00
|
|
|
uint16_t* aUnitType) {
|
2013-10-29 17:15:39 +00:00
|
|
|
RangedPtr<const char16_t> iter = SVGContentUtils::GetStartRangedPtr(aString);
|
2014-01-04 15:02:17 +00:00
|
|
|
const RangedPtr<const char16_t> end =
|
2013-10-29 17:15:39 +00:00
|
|
|
SVGContentUtils::GetEndRangedPtr(aString);
|
|
|
|
|
|
|
|
if (!SVGContentUtils::ParseNumber(iter, end, aValue)) {
|
2013-10-01 07:50:40 +00:00
|
|
|
return false;
|
2007-10-03 09:02:19 +00:00
|
|
|
}
|
2013-10-29 17:15:39 +00:00
|
|
|
const nsAString& units = Substring(iter.get(), end.get());
|
2013-10-01 07:50:40 +00:00
|
|
|
*aUnitType = GetUnitTypeForString(units);
|
|
|
|
return IsValidUnitType(*aUnitType);
|
2007-10-03 09:02:19 +00:00
|
|
|
}
|
|
|
|
|
2008-09-11 00:24:16 +00:00
|
|
|
static float FixAxisLength(float aLength) {
|
|
|
|
if (aLength == 0.0f) {
|
2015-07-29 23:06:14 +00:00
|
|
|
LAYOUT_WARNING("zero axis length");
|
2008-09-11 00:24:16 +00:00
|
|
|
return 1e-20f;
|
|
|
|
}
|
|
|
|
return aLength;
|
|
|
|
}
|
|
|
|
|
2018-12-21 08:58:14 +00:00
|
|
|
SVGElementMetrics::SVGElementMetrics(SVGElement* aSVGElement,
|
2017-08-21 08:19:31 +00:00
|
|
|
SVGViewportElement* aCtx)
|
2014-09-15 10:12:50 +00:00
|
|
|
: mSVGElement(aSVGElement), mCtx(aCtx) {}
|
|
|
|
|
|
|
|
float SVGElementMetrics::GetEmLength() const {
|
|
|
|
return SVGContentUtils::GetFontSize(mSVGElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
float SVGElementMetrics::GetExLength() const {
|
|
|
|
return SVGContentUtils::GetFontXHeight(mSVGElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
float SVGElementMetrics::GetAxisLength(uint8_t aCtxType) const {
|
|
|
|
if (!EnsureCtx()) {
|
2006-04-14 15:09:39 +00:00
|
|
|
return 1;
|
2014-09-15 10:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return FixAxisLength(mCtx->GetLength(aCtxType));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SVGElementMetrics::EnsureCtx() const {
|
|
|
|
if (!mCtx && mSVGElement) {
|
|
|
|
mCtx = mSVGElement->GetCtx();
|
2016-07-22 17:17:05 +00:00
|
|
|
if (!mCtx && mSVGElement->IsSVGElement(nsGkAtoms::svg)) {
|
|
|
|
// mSVGElement must be the outer svg element
|
2017-08-21 08:19:31 +00:00
|
|
|
mCtx = static_cast<SVGViewportElement*>(mSVGElement);
|
2016-07-22 17:17:05 +00:00
|
|
|
}
|
2014-09-15 10:12:50 +00:00
|
|
|
}
|
|
|
|
return mCtx != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
NonSVGFrameUserSpaceMetrics::NonSVGFrameUserSpaceMetrics(nsIFrame* aFrame)
|
|
|
|
: mFrame(aFrame) {}
|
|
|
|
|
|
|
|
float NonSVGFrameUserSpaceMetrics::GetEmLength() const {
|
|
|
|
return SVGContentUtils::GetFontSize(mFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
float NonSVGFrameUserSpaceMetrics::GetExLength() const {
|
|
|
|
return SVGContentUtils::GetFontXHeight(mFrame);
|
|
|
|
}
|
2006-04-14 15:09:39 +00:00
|
|
|
|
2014-09-15 10:12:50 +00:00
|
|
|
gfx::Size NonSVGFrameUserSpaceMetrics::GetSize() const {
|
|
|
|
return nsSVGIntegrationUtils::GetSVGCoordContextForNonSVGFrame(mFrame);
|
2008-09-11 00:24:16 +00:00
|
|
|
}
|
2006-04-14 15:09:39 +00:00
|
|
|
|
2014-09-15 10:12:50 +00:00
|
|
|
float UserSpaceMetricsWithSize::GetAxisLength(uint8_t aCtxType) const {
|
|
|
|
gfx::Size size = GetSize();
|
2008-09-11 00:24:16 +00:00
|
|
|
float length;
|
2014-09-15 10:12:50 +00:00
|
|
|
switch (aCtxType) {
|
2012-09-22 19:26:05 +00:00
|
|
|
case SVGContentUtils::X:
|
|
|
|
length = size.width;
|
|
|
|
break;
|
|
|
|
case SVGContentUtils::Y:
|
|
|
|
length = size.height;
|
|
|
|
break;
|
|
|
|
case SVGContentUtils::XY:
|
|
|
|
length =
|
|
|
|
SVGContentUtils::ComputeNormalizedHypotenuse(size.width, size.height);
|
2008-09-11 00:24:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-06-18 05:43:11 +00:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown axis type");
|
2008-09-11 00:24:16 +00:00
|
|
|
length = 1;
|
|
|
|
break;
|
2006-04-14 15:09:39 +00:00
|
|
|
}
|
2008-09-11 00:24:16 +00:00
|
|
|
return FixAxisLength(length);
|
2006-04-14 15:09:39 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 08:58:14 +00:00
|
|
|
float nsSVGLength2::GetPixelsPerUnit(SVGElement* aSVGElement,
|
2017-12-30 17:07:27 +00:00
|
|
|
uint8_t aUnitType) const {
|
|
|
|
return GetPixelsPerUnit(SVGElementMetrics(aSVGElement), aUnitType);
|
2006-04-14 15:09:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 17:07:27 +00:00
|
|
|
float nsSVGLength2::GetPixelsPerUnit(SVGViewportElement* aCtx,
|
|
|
|
uint8_t aUnitType) const {
|
|
|
|
return GetPixelsPerUnit(SVGElementMetrics(aCtx, aCtx), aUnitType);
|
2006-04-14 15:09:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 17:07:27 +00:00
|
|
|
float nsSVGLength2::GetPixelsPerUnit(nsIFrame* aFrame,
|
|
|
|
uint8_t aUnitType) const {
|
2008-09-11 00:24:16 +00:00
|
|
|
nsIContent* content = aFrame->GetContent();
|
2015-03-03 11:08:59 +00:00
|
|
|
if (content->IsSVGElement()) {
|
2017-12-30 17:07:27 +00:00
|
|
|
return GetPixelsPerUnit(
|
2018-12-21 08:58:14 +00:00
|
|
|
SVGElementMetrics(static_cast<SVGElement*>(content)), aUnitType);
|
2014-09-15 10:12:50 +00:00
|
|
|
}
|
2017-12-30 17:07:27 +00:00
|
|
|
return GetPixelsPerUnit(NonSVGFrameUserSpaceMetrics(aFrame), aUnitType);
|
2014-09-15 10:12:50 +00:00
|
|
|
}
|
2008-09-11 00:24:16 +00:00
|
|
|
|
2017-12-30 17:07:27 +00:00
|
|
|
// See https://www.w3.org/TR/css-values-3/#absolute-lengths
|
|
|
|
static const float DPI = 96.0f;
|
|
|
|
|
|
|
|
float nsSVGLength2::GetPixelsPerUnit(const UserSpaceMetrics& aMetrics,
|
|
|
|
uint8_t aUnitType) const {
|
2009-01-22 01:00:27 +00:00
|
|
|
switch (aUnitType) {
|
2018-06-25 21:20:54 +00:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_NUMBER:
|
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_PX:
|
2008-09-11 00:24:16 +00:00
|
|
|
return 1;
|
2018-06-25 21:20:54 +00:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_MM:
|
2017-12-30 17:07:27 +00:00
|
|
|
return DPI / MM_PER_INCH_FLOAT;
|
2018-06-25 21:20:54 +00:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_CM:
|
2017-12-30 17:07:27 +00:00
|
|
|
return 10.0f * DPI / MM_PER_INCH_FLOAT;
|
2018-06-25 21:20:54 +00:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_IN:
|
2017-12-30 17:07:27 +00:00
|
|
|
return DPI;
|
2018-06-25 21:20:54 +00:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_PT:
|
2017-12-30 17:07:27 +00:00
|
|
|
return DPI / POINTS_PER_INCH_FLOAT;
|
2018-06-25 21:20:54 +00:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_PC:
|
2017-12-30 17:07:27 +00:00
|
|
|
return 12.0f * DPI / POINTS_PER_INCH_FLOAT;
|
2018-06-25 21:20:54 +00:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_PERCENTAGE:
|
2017-12-30 17:07:27 +00:00
|
|
|
return aMetrics.GetAxisLength(mCtxType) / 100.0f;
|
2018-06-25 21:20:54 +00:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_EMS:
|
2017-12-30 17:07:27 +00:00
|
|
|
return aMetrics.GetEmLength();
|
2018-06-25 21:20:54 +00:00
|
|
|
case SVGLength_Binding::SVG_LENGTHTYPE_EXS:
|
2017-12-30 17:07:27 +00:00
|
|
|
return aMetrics.GetExLength();
|
2008-09-11 00:24:16 +00:00
|
|
|
default:
|
2018-06-18 05:43:11 +00:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown unit type");
|
2008-09-11 00:24:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
|
|
|
|
2006-04-14 15:09:39 +00:00
|
|
|
void nsSVGLength2::SetBaseValueInSpecifiedUnits(float aValue,
|
2018-12-21 08:58:14 +00:00
|
|
|
SVGElement* aSVGElement,
|
2012-02-15 23:40:45 +00:00
|
|
|
bool aDoSetAttr) {
|
|
|
|
if (mIsBaseSet && mBaseVal == aValue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAttrValue emptyOrOldValue;
|
|
|
|
if (aDoSetAttr) {
|
|
|
|
emptyOrOldValue = aSVGElement->WillChangeLength(mAttrEnum);
|
|
|
|
}
|
2010-02-11 19:41:48 +00:00
|
|
|
mBaseVal = aValue;
|
2011-10-17 14:59:28 +00:00
|
|
|
mIsBaseSet = true;
|
2010-02-11 19:41:48 +00:00
|
|
|
if (!mIsAnimated) {
|
|
|
|
mAnimVal = mBaseVal;
|
|
|
|
} else {
|
2009-01-22 01:00:27 +00:00
|
|
|
aSVGElement->AnimationNeedsResample();
|
|
|
|
}
|
2012-02-15 23:40:45 +00:00
|
|
|
if (aDoSetAttr) {
|
|
|
|
aSVGElement->DidChangeLength(mAttrEnum, emptyOrOldValue);
|
|
|
|
}
|
2006-04-14 15:09:39 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult nsSVGLength2::ConvertToSpecifiedUnits(uint16_t unitType,
|
2018-12-21 08:58:14 +00:00
|
|
|
SVGElement* aSVGElement) {
|
2010-01-20 19:58:58 +00:00
|
|
|
if (!IsValidUnitType(unitType)) return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
|
2006-04-14 15:09:39 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
if (mIsBaseSet && mSpecifiedUnitType == uint8_t(unitType)) return NS_OK;
|
2012-02-15 23:40:45 +00:00
|
|
|
|
2017-12-30 17:07:27 +00:00
|
|
|
float pixelsPerUnit = GetPixelsPerUnit(aSVGElement, unitType);
|
|
|
|
if (pixelsPerUnit == 0.0f) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
float valueInUserUnits =
|
|
|
|
mBaseVal * GetPixelsPerUnit(aSVGElement, mSpecifiedUnitType);
|
|
|
|
float valueInSpecifiedUnits = valueInUserUnits / pixelsPerUnit;
|
|
|
|
|
|
|
|
if (!IsFinite(valueInSpecifiedUnits)) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
2012-02-15 23:40:45 +00:00
|
|
|
// Even though we're not changing the visual effect this length will have
|
|
|
|
// on the document, we still need to send out notifications in case we have
|
|
|
|
// mutation listeners, since the actual string value of the attribute will
|
|
|
|
// change.
|
|
|
|
nsAttrValue emptyOrOldValue = aSVGElement->WillChangeLength(mAttrEnum);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
mSpecifiedUnitType = uint8_t(unitType);
|
2012-02-15 23:40:45 +00:00
|
|
|
// Setting aDoSetAttr to false here will ensure we don't call
|
|
|
|
// Will/DidChangeAngle a second time (and dispatch duplicate notifications).
|
2017-12-30 17:07:27 +00:00
|
|
|
SetBaseValueInSpecifiedUnits(valueInSpecifiedUnits, aSVGElement, false);
|
2012-02-15 23:40:45 +00:00
|
|
|
|
|
|
|
aSVGElement->DidChangeLength(mAttrEnum, emptyOrOldValue);
|
2010-01-20 19:58:58 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2006-04-14 15:09:39 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult nsSVGLength2::NewValueSpecifiedUnits(uint16_t unitType,
|
2006-04-14 15:09:39 +00:00
|
|
|
float valueInSpecifiedUnits,
|
2018-12-21 08:58:14 +00:00
|
|
|
SVGElement* aSVGElement) {
|
2010-01-20 19:58:58 +00:00
|
|
|
NS_ENSURE_FINITE(valueInSpecifiedUnits, NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
|
|
|
|
if (!IsValidUnitType(unitType)) return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
|
2006-04-14 15:09:39 +00:00
|
|
|
|
2012-02-15 23:40:45 +00:00
|
|
|
if (mIsBaseSet && mBaseVal == valueInSpecifiedUnits &&
|
2012-08-22 15:56:38 +00:00
|
|
|
mSpecifiedUnitType == uint8_t(unitType)) {
|
2012-02-15 23:40:45 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAttrValue emptyOrOldValue = aSVGElement->WillChangeLength(mAttrEnum);
|
2010-02-11 19:41:48 +00:00
|
|
|
mBaseVal = valueInSpecifiedUnits;
|
2011-10-17 14:59:28 +00:00
|
|
|
mIsBaseSet = true;
|
2012-08-22 15:56:38 +00:00
|
|
|
mSpecifiedUnitType = uint8_t(unitType);
|
2010-02-11 19:41:48 +00:00
|
|
|
if (!mIsAnimated) {
|
|
|
|
mAnimVal = mBaseVal;
|
|
|
|
} else {
|
2009-01-22 01:00:27 +00:00
|
|
|
aSVGElement->AnimationNeedsResample();
|
|
|
|
}
|
2012-02-15 23:40:45 +00:00
|
|
|
aSVGElement->DidChangeLength(mAttrEnum, emptyOrOldValue);
|
2010-01-20 19:58:58 +00:00
|
|
|
return NS_OK;
|
2006-04-14 15:09:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 20:40:21 +00:00
|
|
|
already_AddRefed<DOMSVGLength> nsSVGLength2::ToDOMBaseVal(
|
|
|
|
SVGElement* aSVGElement) {
|
|
|
|
return DOMSVGLength::GetTearOff(this, aSVGElement, false);
|
2006-04-14 15:09:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 20:40:21 +00:00
|
|
|
already_AddRefed<DOMSVGLength> nsSVGLength2::ToDOMAnimVal(
|
|
|
|
SVGElement* aSVGElement) {
|
|
|
|
return DOMSVGLength::GetTearOff(this, aSVGElement, true);
|
2006-04-14 15:09:39 +00:00
|
|
|
}
|
|
|
|
|
2007-10-03 09:02:19 +00:00
|
|
|
/* Implementation */
|
|
|
|
|
|
|
|
nsresult nsSVGLength2::SetBaseValueString(const nsAString& aValueAsString,
|
2018-12-21 08:58:14 +00:00
|
|
|
SVGElement* aSVGElement,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aDoSetAttr) {
|
2007-10-03 09:02:19 +00:00
|
|
|
float value;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t unitType;
|
2012-02-15 23:40:45 +00:00
|
|
|
|
2013-10-01 07:50:40 +00:00
|
|
|
if (!GetValueFromString(aValueAsString, value, &unitType)) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
2007-10-15 12:06:42 +00:00
|
|
|
}
|
2012-02-15 23:40:45 +00:00
|
|
|
|
2013-10-01 07:50:40 +00:00
|
|
|
if (mIsBaseSet && mBaseVal == float(value) &&
|
2012-08-22 15:56:38 +00:00
|
|
|
mSpecifiedUnitType == uint8_t(unitType)) {
|
2012-02-15 23:40:45 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAttrValue emptyOrOldValue;
|
|
|
|
if (aDoSetAttr) {
|
|
|
|
emptyOrOldValue = aSVGElement->WillChangeLength(mAttrEnum);
|
|
|
|
}
|
2010-02-11 19:41:48 +00:00
|
|
|
mBaseVal = value;
|
2011-10-17 14:59:28 +00:00
|
|
|
mIsBaseSet = true;
|
2012-08-22 15:56:38 +00:00
|
|
|
mSpecifiedUnitType = uint8_t(unitType);
|
2010-02-11 19:41:48 +00:00
|
|
|
if (!mIsAnimated) {
|
|
|
|
mAnimVal = mBaseVal;
|
|
|
|
} else {
|
2009-01-22 01:00:27 +00:00
|
|
|
aSVGElement->AnimationNeedsResample();
|
|
|
|
}
|
|
|
|
|
2012-02-15 23:40:45 +00:00
|
|
|
if (aDoSetAttr) {
|
|
|
|
aSVGElement->DidChangeLength(mAttrEnum, emptyOrOldValue);
|
|
|
|
}
|
2007-10-03 09:02:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-15 23:40:45 +00:00
|
|
|
void nsSVGLength2::GetBaseValueString(nsAString& aValueAsString) const {
|
2007-10-03 09:02:19 +00:00
|
|
|
GetValueString(aValueAsString, mBaseVal, mSpecifiedUnitType);
|
|
|
|
}
|
|
|
|
|
2012-02-15 23:40:45 +00:00
|
|
|
void nsSVGLength2::GetAnimValueString(nsAString& aValueAsString) const {
|
2007-10-03 09:02:19 +00:00
|
|
|
GetValueString(aValueAsString, mAnimVal, mSpecifiedUnitType);
|
|
|
|
}
|
|
|
|
|
2018-12-21 08:58:14 +00:00
|
|
|
nsresult nsSVGLength2::SetBaseValue(float aValue, SVGElement* aSVGElement,
|
2012-02-15 23:40:45 +00:00
|
|
|
bool aDoSetAttr) {
|
2017-12-30 17:07:27 +00:00
|
|
|
float pixelsPerUnit = GetPixelsPerUnit(aSVGElement, mSpecifiedUnitType);
|
|
|
|
if (pixelsPerUnit == 0.0f) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
float valueInSpecifiedUnits = aValue / pixelsPerUnit;
|
|
|
|
if (!IsFinite(valueInSpecifiedUnits)) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetBaseValueInSpecifiedUnits(valueInSpecifiedUnits, aSVGElement, aDoSetAttr);
|
|
|
|
return NS_OK;
|
2007-10-03 09:02:19 +00:00
|
|
|
}
|
|
|
|
|
2011-03-31 19:29:44 +00:00
|
|
|
void nsSVGLength2::SetAnimValueInSpecifiedUnits(float aValue,
|
2018-12-21 08:58:14 +00:00
|
|
|
SVGElement* aSVGElement) {
|
2012-06-01 23:52:15 +00:00
|
|
|
if (mAnimVal == aValue && mIsAnimated) {
|
|
|
|
return;
|
|
|
|
}
|
2011-03-31 19:29:44 +00:00
|
|
|
mAnimVal = aValue;
|
2011-10-17 14:59:28 +00:00
|
|
|
mIsAnimated = true;
|
2009-01-15 04:38:07 +00:00
|
|
|
aSVGElement->DidAnimateLength(mAttrEnum);
|
|
|
|
}
|
|
|
|
|
2018-12-21 08:58:14 +00:00
|
|
|
nsresult nsSVGLength2::SetAnimValue(float aValue, SVGElement* aSVGElement) {
|
2017-12-30 17:07:27 +00:00
|
|
|
float valueInSpecifiedUnits =
|
|
|
|
aValue / GetPixelsPerUnit(aSVGElement, mSpecifiedUnitType);
|
|
|
|
|
|
|
|
if (IsFinite(valueInSpecifiedUnits)) {
|
|
|
|
SetAnimValueInSpecifiedUnits(valueInSpecifiedUnits, aSVGElement);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2011-03-31 19:29:44 +00:00
|
|
|
}
|
|
|
|
|
2013-01-19 20:56:00 +00:00
|
|
|
already_AddRefed<SVGAnimatedLength> nsSVGLength2::ToDOMAnimatedLength(
|
2018-12-21 08:58:14 +00:00
|
|
|
SVGElement* aSVGElement) {
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<SVGAnimatedLength> svgAnimatedLength =
|
2012-08-23 06:24:43 +00:00
|
|
|
sSVGAnimatedLengthTearoffTable.GetTearoff(this);
|
2013-01-19 20:56:00 +00:00
|
|
|
if (!svgAnimatedLength) {
|
|
|
|
svgAnimatedLength = new SVGAnimatedLength(this, aSVGElement);
|
|
|
|
sSVGAnimatedLengthTearoffTable.AddTearoff(this, svgAnimatedLength);
|
2009-08-27 04:30:51 +00:00
|
|
|
}
|
2006-04-14 15:09:39 +00:00
|
|
|
|
2013-01-19 20:56:00 +00:00
|
|
|
return svgAnimatedLength.forget();
|
2006-04-14 15:09:39 +00:00
|
|
|
}
|
2009-01-15 04:38:07 +00:00
|
|
|
|
2013-01-19 20:56:00 +00:00
|
|
|
SVGAnimatedLength::~SVGAnimatedLength() {
|
2009-08-27 04:30:51 +00:00
|
|
|
sSVGAnimatedLengthTearoffTable.RemoveTearoff(mVal);
|
|
|
|
}
|
|
|
|
|
2019-01-25 03:24:01 +00:00
|
|
|
UniquePtr<SMILAttr> nsSVGLength2::ToSMILAttr(SVGElement* aSVGElement) {
|
2017-03-30 04:10:07 +00:00
|
|
|
return MakeUnique<SMILLength>(this, aSVGElement);
|
2009-01-15 04:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsSVGLength2::SMILLength::ValueFromString(
|
2013-03-19 03:18:45 +00:00
|
|
|
const nsAString& aStr, const SVGAnimationElement* /*aSrcElement*/,
|
2019-01-23 13:48:08 +00:00
|
|
|
SMILValue& aValue, bool& aPreventCachingOfSandwich) const {
|
2009-01-22 01:00:27 +00:00
|
|
|
float value;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t unitType;
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2013-10-01 07:50:40 +00:00
|
|
|
if (!GetValueFromString(aStr, value, &unitType)) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
2009-01-22 01:00:27 +00:00
|
|
|
}
|
2009-01-15 04:38:07 +00:00
|
|
|
|
2019-01-23 13:48:08 +00:00
|
|
|
SMILValue val(SMILFloatType::Singleton());
|
2017-12-30 17:07:27 +00:00
|
|
|
val.mU.mDouble = value * mVal->GetPixelsPerUnit(mSVGElement, unitType);
|
2009-01-15 04:38:07 +00:00
|
|
|
aValue = val;
|
2010-06-24 00:30:55 +00:00
|
|
|
aPreventCachingOfSandwich =
|
2018-06-25 21:20:54 +00:00
|
|
|
(unitType == SVGLength_Binding::SVG_LENGTHTYPE_PERCENTAGE ||
|
|
|
|
unitType == SVGLength_Binding::SVG_LENGTHTYPE_EMS ||
|
|
|
|
unitType == SVGLength_Binding::SVG_LENGTHTYPE_EXS);
|
2010-02-20 21:13:11 +00:00
|
|
|
|
2009-01-15 04:38:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-01-23 13:48:08 +00:00
|
|
|
SMILValue nsSVGLength2::SMILLength::GetBaseValue() const {
|
|
|
|
SMILValue val(SMILFloatType::Singleton());
|
2009-01-15 04:38:07 +00:00
|
|
|
val.mU.mDouble = mVal->GetBaseValue(mSVGElement);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2009-07-14 19:33:29 +00:00
|
|
|
void nsSVGLength2::SMILLength::ClearAnimValue() {
|
|
|
|
if (mVal->mIsAnimated) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mVal->mIsAnimated = false;
|
2012-03-03 09:21:09 +00:00
|
|
|
mVal->mAnimVal = mVal->mBaseVal;
|
|
|
|
mSVGElement->DidAnimateLength(mVal->mAttrEnum);
|
2017-07-06 12:00:35 +00:00
|
|
|
}
|
2009-07-14 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 13:48:08 +00:00
|
|
|
nsresult nsSVGLength2::SMILLength::SetAnimValue(const SMILValue& aValue) {
|
2018-12-24 19:24:45 +00:00
|
|
|
NS_ASSERTION(aValue.mType == SMILFloatType::Singleton(),
|
2009-01-15 04:38:07 +00:00
|
|
|
"Unexpected type to assign animated value");
|
2018-12-24 19:24:45 +00:00
|
|
|
if (aValue.mType == SMILFloatType::Singleton()) {
|
2017-12-30 17:07:27 +00:00
|
|
|
return mVal->SetAnimValue(float(aValue.mU.mDouble), mSVGElement);
|
2009-01-15 04:38:07 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|