From 056bb264bde4657a656cf9c347e6703bf0bb7d74 Mon Sep 17 00:00:00 2001 From: Robert Longson Date: Wed, 7 Jun 2023 14:28:18 +0000 Subject: [PATCH] Bug 1837093 - Make SVGAnimatedLength methods take const pointers to avoid casting away const later r=emilio Differential Revision: https://phabricator.services.mozilla.com/D180175 --- dom/svg/SVGAnimatedLength.cpp | 14 +++++++------- dom/svg/SVGAnimatedLength.h | 22 ++++++++++++---------- dom/svg/SVGElement.cpp | 12 ++---------- dom/svg/SVGForeignObjectElement.cpp | 4 ++-- dom/svg/SVGGeometryProperty.h | 12 +++++------- dom/svg/SVGLength.cpp | 2 +- dom/svg/SVGLength.h | 5 +---- dom/svg/SVGLengthList.h | 4 ++-- dom/svg/SVGViewportElement.cpp | 16 ++++++++-------- dom/svg/SVGViewportElement.h | 2 +- layout/svg/SVGUtils.cpp | 5 ----- layout/svg/SVGUtils.h | 2 -- 12 files changed, 41 insertions(+), 59 deletions(-) diff --git a/dom/svg/SVGAnimatedLength.cpp b/dom/svg/SVGAnimatedLength.cpp index faa52fe700d8..8254cfe8800a 100644 --- a/dom/svg/SVGAnimatedLength.cpp +++ b/dom/svg/SVGAnimatedLength.cpp @@ -104,8 +104,8 @@ static float FixAxisLength(float aLength) { return aLength; } -SVGElementMetrics::SVGElementMetrics(SVGElement* aSVGElement, - SVGViewportElement* aCtx) +SVGElementMetrics::SVGElementMetrics(const SVGElement* aSVGElement, + const SVGViewportElement* aCtx) : mSVGElement(aSVGElement), mCtx(aCtx) {} float SVGElementMetrics::GetEmLength() const { @@ -128,7 +128,7 @@ bool SVGElementMetrics::EnsureCtx() const { if (!mCtx && mSVGElement) { mCtx = mSVGElement->GetCtx(); if (!mCtx && mSVGElement->IsSVGElement(nsGkAtoms::svg)) { - auto* e = static_cast(mSVGElement); + const auto* e = static_cast(mSVGElement); if (!e->IsInner()) { // mSVGElement must be the outer svg element @@ -176,13 +176,13 @@ float UserSpaceMetricsWithSize::GetAxisLength(uint8_t aCtxType) const { return FixAxisLength(length); } -float SVGAnimatedLength::GetPixelsPerUnit(SVGElement* aSVGElement, +float SVGAnimatedLength::GetPixelsPerUnit(const SVGElement* aSVGElement, uint8_t aUnitType) const { return SVGLength::GetPixelsPerUnit(SVGElementMetrics(aSVGElement), aUnitType, mCtxType); } -float SVGAnimatedLength::GetPixelsPerUnit(SVGViewportElement* aCtx, +float SVGAnimatedLength::GetPixelsPerUnit(const SVGViewportElement* aCtx, uint8_t aUnitType) const { return SVGLength::GetPixelsPerUnit(SVGElementMetrics(aCtx, aCtx), aUnitType, mCtxType); @@ -190,11 +190,11 @@ float SVGAnimatedLength::GetPixelsPerUnit(SVGViewportElement* aCtx, float SVGAnimatedLength::GetPixelsPerUnit(nsIFrame* aFrame, uint8_t aUnitType) const { - nsIContent* content = aFrame->GetContent(); + const nsIContent* content = aFrame->GetContent(); MOZ_ASSERT(!content->IsText(), "Not expecting text content"); if (content->IsSVGElement()) { return SVGLength::GetPixelsPerUnit( - SVGElementMetrics(static_cast(content)), aUnitType, + SVGElementMetrics(static_cast(content)), aUnitType, mCtxType); } return SVGLength::GetPixelsPerUnit(NonSVGFrameUserSpaceMetrics(aFrame), diff --git a/dom/svg/SVGAnimatedLength.h b/dom/svg/SVGAnimatedLength.h index 805c7a7d13fb..c242385d2348 100644 --- a/dom/svg/SVGAnimatedLength.h +++ b/dom/svg/SVGAnimatedLength.h @@ -50,8 +50,8 @@ class UserSpaceMetricsWithSize : public UserSpaceMetrics { class SVGElementMetrics : public UserSpaceMetrics { public: - explicit SVGElementMetrics(SVGElement* aSVGElement, - SVGViewportElement* aCtx = nullptr); + explicit SVGElementMetrics(const SVGElement* aSVGElement, + const SVGViewportElement* aCtx = nullptr); float GetEmLength() const override; float GetExLength() const override; @@ -60,8 +60,8 @@ class SVGElementMetrics : public UserSpaceMetrics { private: bool EnsureCtx() const; - SVGElement* mSVGElement; - mutable SVGViewportElement* mCtx; + const SVGElement* mSVGElement; + mutable const SVGViewportElement* mCtx; }; class NonSVGFrameUserSpaceMetrics : public UserSpaceMetricsWithSize { @@ -113,17 +113,17 @@ class SVGAnimatedLength { void GetBaseValueString(nsAString& aValue) const; void GetAnimValueString(nsAString& aValue) const; - float GetBaseValue(SVGElement* aSVGElement) const { + float GetBaseValue(const SVGElement* aSVGElement) const { return mBaseVal * GetPixelsPerUnit(aSVGElement, mSpecifiedUnitType); } - float GetAnimValue(SVGElement* aSVGElement) const { + float GetAnimValue(const SVGElement* aSVGElement) const { return mAnimVal * GetPixelsPerUnit(aSVGElement, mSpecifiedUnitType); } float GetAnimValue(nsIFrame* aFrame) const { return mAnimVal * GetPixelsPerUnit(aFrame, mSpecifiedUnitType); } - float GetAnimValue(SVGViewportElement* aCtx) const { + float GetAnimValue(const SVGViewportElement* aCtx) const { return mAnimVal * GetPixelsPerUnit(aCtx, mSpecifiedUnitType); } float GetAnimValue(const UserSpaceMetrics& aMetrics) const { @@ -139,7 +139,7 @@ class SVGAnimatedLength { float GetAnimValInSpecifiedUnits() const { return mAnimVal; } float GetBaseValInSpecifiedUnits() const { return mBaseVal; } - float GetBaseValue(SVGViewportElement* aCtx) const { + float GetBaseValue(const SVGViewportElement* aCtx) const { return mBaseVal * GetPixelsPerUnit(aCtx, mSpecifiedUnitType); } @@ -172,8 +172,10 @@ class SVGAnimatedLength { float GetPixelsPerUnit(nsIFrame* aFrame, uint8_t aUnitType) const; float GetPixelsPerUnit(const UserSpaceMetrics& aMetrics, uint8_t aUnitType) const; - float GetPixelsPerUnit(SVGElement* aSVGElement, uint8_t aUnitType) const; - float GetPixelsPerUnit(SVGViewportElement* aCtx, uint8_t aUnitType) const; + float GetPixelsPerUnit(const SVGElement* aSVGElement, + uint8_t aUnitType) const; + float GetPixelsPerUnit(const SVGViewportElement* aCtx, + uint8_t aUnitType) const; // SetBaseValue and SetAnimValue set the value in user units. This may fail // if unit conversion fails e.g. conversion to ex or em units where the diff --git a/dom/svg/SVGElement.cpp b/dom/svg/SVGElement.cpp index d0f65fc2127a..db059f8ae948 100644 --- a/dom/svg/SVGElement.cpp +++ b/dom/svg/SVGElement.cpp @@ -1532,7 +1532,7 @@ void SVGElement::GetAnimatedLengthValues(float* aFirst, ...) { NS_ASSERTION(info.mCount > 0, "GetAnimatedLengthValues on element with no length attribs"); - SVGViewportElement* ctx = nullptr; + SVGElementMetrics metrics(this); float* f = aFirst; uint32_t i = 0; @@ -1541,15 +1541,7 @@ void SVGElement::GetAnimatedLengthValues(float* aFirst, ...) { va_start(args, aFirst); while (f && i < info.mCount) { - uint8_t type = info.mValues[i].GetSpecifiedUnitType(); - if (!ctx && !SVGLength::IsAbsoluteUnit(type)) { - ctx = GetCtx(); - } - if (type == SVGLength_Binding::SVG_LENGTHTYPE_EMS || - type == SVGLength_Binding::SVG_LENGTHTYPE_EXS) - *f = info.mValues[i++].GetAnimValue(this); - else - *f = info.mValues[i++].GetAnimValue(ctx); + *f = info.mValues[i++].GetAnimValue(metrics); f = va_arg(args, float*); } diff --git a/dom/svg/SVGForeignObjectElement.cpp b/dom/svg/SVGForeignObjectElement.cpp index d1d7c46f43cc..e6fb81f2c3e3 100644 --- a/dom/svg/SVGForeignObjectElement.cpp +++ b/dom/svg/SVGForeignObjectElement.cpp @@ -100,8 +100,8 @@ bool SVGForeignObjectElement::HasValidDimensions() const { float width, height; DebugOnly ok = - SVGGeometryProperty::ResolveAll( - const_cast(this), &width, &height); + SVGGeometryProperty::ResolveAll(this, &width, + &height); MOZ_ASSERT(ok, "SVGGeometryProperty::ResolveAll failed"); return width > 0 && height > 0; } diff --git a/dom/svg/SVGGeometryProperty.h b/dom/svg/SVGGeometryProperty.h index d5eb0e9d116c..349668916222 100644 --- a/dom/svg/SVGGeometryProperty.h +++ b/dom/svg/SVGGeometryProperty.h @@ -89,21 +89,21 @@ using dummy = int[]; using CtxDirectionType = decltype(SVGContentUtils::X); template -float ResolvePureLengthPercentage(SVGElement* aElement, +float ResolvePureLengthPercentage(const SVGElement* aElement, const LengthPercentage& aLP) { return aLP.ResolveToCSSPixelsWith( [&] { return CSSCoord{SVGElementMetrics(aElement).GetAxisLength(CTD)}; }); } template -float ResolveImpl(ComputedStyle const& aStyle, SVGElement* aElement, +float ResolveImpl(ComputedStyle const& aStyle, const SVGElement* aElement, ResolverTypes::LengthPercentNoAuto) { auto const& value = aStyle.StyleSVGReset()->*Tag::Getter; return ResolvePureLengthPercentage(aElement, value); } template -float ResolveImpl(ComputedStyle const& aStyle, SVGElement* aElement, +float ResolveImpl(ComputedStyle const& aStyle, const SVGElement* aElement, ResolverTypes::LengthPercentWidthHeight) { static_assert( std::is_same{} || std::is_same{}, @@ -193,7 +193,7 @@ float ResolveImpl(ComputedStyle const& aStyle, SVGElement* aElement, } template -float ResolveImpl(ComputedStyle const& aStyle, SVGElement* aElement, +float ResolveImpl(ComputedStyle const& aStyle, const SVGElement* aElement, ResolverTypes::LengthPercentRXY) { static_assert(std::is_same{} || std::is_same{}, "Wrong tag"); @@ -222,9 +222,7 @@ float ResolveImpl(ComputedStyle const& aStyle, SVGElement* aElement, template float ResolveWith(const ComputedStyle& aStyle, const SVGElement* aElement) { - // TODO: There are a lot of utilities lacking const-ness in dom/svg. - // We should fix that problem and remove this `const_cast`. - return details::ResolveImpl(aStyle, const_cast(aElement), + return details::ResolveImpl(aStyle, aElement, typename Tag::ResolverType{}); } diff --git a/dom/svg/SVGLength.cpp b/dom/svg/SVGLength.cpp index a722c9ada82f..f5fbd8b7b74d 100644 --- a/dom/svg/SVGLength.cpp +++ b/dom/svg/SVGLength.cpp @@ -119,7 +119,7 @@ float SVGLength::GetValueInSpecifiedUnit(uint8_t aUnit, // succeed if aElement is non-null (although that's not sufficient to // guarantee success). - auto userSpaceMetrics = SVGElementMetrics(const_cast(aElement)); + SVGElementMetrics userSpaceMetrics(aElement); float userUnitsPerCurrentUnit = GetPixelsPerUnit(userSpaceMetrics, aAxis); float userUnitsPerNewUnit = diff --git a/dom/svg/SVGLength.h b/dom/svg/SVGLength.h index 0ce2a2de38a5..ea6f417f33f0 100644 --- a/dom/svg/SVGLength.h +++ b/dom/svg/SVGLength.h @@ -90,10 +90,7 @@ class SVGLength { */ float GetValueInPixels(const dom::SVGElement* aElement, uint8_t aAxis) const { - return mValue * - GetPixelsPerUnit( - dom::SVGElementMetrics(const_cast(aElement)), - aAxis); + return mValue * GetPixelsPerUnit(dom::SVGElementMetrics(aElement), aAxis); } /** diff --git a/dom/svg/SVGLengthList.h b/dom/svg/SVGLengthList.h index 3c2ae74b3abd..0c3f7a0c2baa 100644 --- a/dom/svg/SVGLengthList.h +++ b/dom/svg/SVGLengthList.h @@ -304,7 +304,7 @@ class MOZ_STACK_CLASS SVGUserUnitList { public: SVGUserUnitList() : mList(nullptr), mElement(nullptr), mAxis(0) {} - void Init(const SVGLengthList* aList, dom::SVGElement* aElement, + void Init(const SVGLengthList* aList, const dom::SVGElement* aElement, uint8_t aAxis) { mList = aList; mElement = aElement; @@ -330,7 +330,7 @@ class MOZ_STACK_CLASS SVGUserUnitList { private: const SVGLengthList* mList; - dom::SVGElement* mElement; + const dom::SVGElement* mElement; uint8_t mAxis; }; diff --git a/dom/svg/SVGViewportElement.cpp b/dom/svg/SVGViewportElement.cpp index e9afb5efe375..c9fef21c87aa 100644 --- a/dom/svg/SVGViewportElement.cpp +++ b/dom/svg/SVGViewportElement.cpp @@ -107,7 +107,7 @@ inline float ComputeSynthesizedViewBoxDimension( return aViewportLength * aLength.GetAnimValInSpecifiedUnits() / 100.0f; } - return aLength.GetAnimValue(const_cast(aSelf)); + return aLength.GetAnimValue(aSelf); } //---------------------------------------------------------------------- @@ -156,9 +156,9 @@ void SVGViewportElement::ChildrenOnlyTransformChanged(uint32_t aFlags) { gfx::Matrix SVGViewportElement::GetViewBoxTransform() const { float viewportWidth, viewportHeight; if (IsInner()) { - SVGElement* self = const_cast(this); - viewportWidth = mLengthAttributes[ATTR_WIDTH].GetAnimValue(self); - viewportHeight = mLengthAttributes[ATTR_HEIGHT].GetAnimValue(self); + SVGElementMetrics metrics(this); + viewportWidth = mLengthAttributes[ATTR_WIDTH].GetAnimValue(metrics); + viewportHeight = mLengthAttributes[ATTR_HEIGHT].GetAnimValue(metrics); } else { viewportWidth = mViewportWidth; viewportHeight = mViewportHeight; @@ -183,7 +183,7 @@ gfx::Matrix SVGViewportElement::GetViewBoxTransform() const { //---------------------------------------------------------------------- // SVGViewportElement -float SVGViewportElement::GetLength(uint8_t aCtxType) { +float SVGViewportElement::GetLength(uint8_t aCtxType) const { const SVGViewBox* viewbox = GetViewBoxInternal().HasRect() ? &GetViewBoxInternal().GetAnimValue() : nullptr; @@ -201,12 +201,12 @@ float SVGViewportElement::GetLength(uint8_t aCtxType) { // Resolving length for inner is exactly the same as other // ordinary element. We shouldn't use the SVGViewportElement overload // of GetAnimValue(). - SVGElement* self = this; + SVGElementMetrics metrics(this); if (shouldComputeWidth) { - w = mLengthAttributes[ATTR_WIDTH].GetAnimValue(self); + w = mLengthAttributes[ATTR_WIDTH].GetAnimValue(metrics); } if (shouldComputeHeight) { - h = mLengthAttributes[ATTR_HEIGHT].GetAnimValue(self); + h = mLengthAttributes[ATTR_HEIGHT].GetAnimValue(metrics); } } else if (ShouldSynthesizeViewBox()) { if (shouldComputeWidth) { diff --git a/dom/svg/SVGViewportElement.h b/dom/svg/SVGViewportElement.h index 14def3125cd4..32fe08f5e48a 100644 --- a/dom/svg/SVGViewportElement.h +++ b/dom/svg/SVGViewportElement.h @@ -63,7 +63,7 @@ class SVGViewportElement : public SVGGraphicsElement { // SVGViewportElement methods: - float GetLength(uint8_t aCtxType); + float GetLength(uint8_t aCtxType) const; // public helpers: diff --git a/layout/svg/SVGUtils.cpp b/layout/svg/SVGUtils.cpp index 206f56ceddfe..517093e61647 100644 --- a/layout/svg/SVGUtils.cpp +++ b/layout/svg/SVGUtils.cpp @@ -261,11 +261,6 @@ float SVGUtils::ObjectSpace(const gfxRect& aRect, axis; } -float SVGUtils::UserSpace(SVGElement* aSVGElement, - const SVGAnimatedLength* aLength) { - return aLength->GetAnimValue(aSVGElement); -} - float SVGUtils::UserSpace(nsIFrame* aNonSVGContext, const SVGAnimatedLength* aLength) { MOZ_ASSERT(!aNonSVGContext->IsTextFrame(), "Not expecting text content"); diff --git a/layout/svg/SVGUtils.h b/layout/svg/SVGUtils.h index 72a900c3e369..fbb826f4fc6f 100644 --- a/layout/svg/SVGUtils.h +++ b/layout/svg/SVGUtils.h @@ -222,8 +222,6 @@ class SVGUtils final { Input: content - object to be used for determining user space Input: length - length to be converted */ - static float UserSpace(SVGElement* aSVGElement, - const SVGAnimatedLength* aLength); static float UserSpace(nsIFrame* aNonSVGContext, const SVGAnimatedLength* aLength); static float UserSpace(const dom::UserSpaceMetrics& aMetrics,