Bug 1965114 part 1: Refactor out a static helper from VectorImage::GetWidth/GetHeight. r=tnikkel

This patch doesn't change behavior; it just refactors out some commont logic
into a helper (so that we can add another call to this same helper in a later
patch in this series).

Differential Revision: https://phabricator.services.mozilla.com/D248525
This commit is contained in:
Daniel Holbert
2025-05-09 03:35:11 +00:00
committed by dholbert@mozilla.com
parent 47b8f999ed
commit dda6b1197c

View File

@@ -430,6 +430,19 @@ bool VectorImage::ShouldAnimate() {
return ImageResource::ShouldAnimate() && mIsFullyLoaded && mHaveAnimations; return ImageResource::ShouldAnimate() && mIsFullyLoaded && mHaveAnimations;
} }
// Helper for GetWidth/GetHeight:
// If the given LengthPercentage is just a Length, this function converts it to
// CSS pixels and clamps it to be nonnegative, and returns the result.
// Otherwise, this function returns Nothing().
static Maybe<int32_t> ClampedPxLengthOrNothing(
const LengthPercentage& aLenPct) {
if (!aLenPct.IsLength()) {
return Nothing();
}
auto lenInPx = SVGUtils::ClampToInt(aLenPct.AsLength().ToCSSPixels());
return Some(std::max(0, lenInPx));
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// imgIContainer methods // imgIContainer methods
@@ -453,15 +466,14 @@ VectorImage::GetWidth(int32_t* aWidth) {
*aWidth = 0; *aWidth = 0;
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
LengthPercentage rootElemWidth = rootElem->GetIntrinsicWidth();
if (!rootElemWidth.IsLength()) { auto widthFromSVG = ClampedPxLengthOrNothing(rootElem->GetIntrinsicWidth());
if (!widthFromSVG) {
*aWidth = 0; *aWidth = 0;
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
*aWidth = SVGUtils::ClampToInt(rootElemWidth.AsLength().ToCSSPixels()); *aWidth = *widthFromSVG;
*aWidth = std::max(0, *aWidth);
return NS_OK; return NS_OK;
} }
@@ -485,15 +497,13 @@ VectorImage::GetHeight(int32_t* aHeight) {
*aHeight = 0; *aHeight = 0;
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
LengthPercentage rootElemHeight = rootElem->GetIntrinsicHeight(); auto heightFromSVG = ClampedPxLengthOrNothing(rootElem->GetIntrinsicHeight());
if (!heightFromSVG) {
if (!rootElemHeight.IsLength()) {
*aHeight = 0; *aHeight = 0;
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
*aHeight = SVGUtils::ClampToInt(rootElemHeight.AsLength().ToCSSPixels()); *aHeight = *heightFromSVG;
*aHeight = std::max(0, *aHeight);
return NS_OK; return NS_OK;
} }