diff options
Diffstat (limited to '')
-rw-r--r-- | layout/style/ServoStyleConstsInlines.h | 68 |
1 files changed, 36 insertions, 32 deletions
diff --git a/layout/style/ServoStyleConstsInlines.h b/layout/style/ServoStyleConstsInlines.h index b6a574037a..1985e8fbeb 100644 --- a/layout/style/ServoStyleConstsInlines.h +++ b/layout/style/ServoStyleConstsInlines.h @@ -507,20 +507,15 @@ StyleCSSPixelLength StyleCSSPixelLength::ScaledBy(float aScale) const { return FromPixels(ToCSSPixels() * aScale); } -nscoord StyleCSSPixelLength::ToAppUnits() const { - // We want to resolve the length part of the calc() expression rounding 0.5 - // away from zero, instead of the default behavior of - // NSToCoordRound{,WithClamp} which do floor(x + 0.5). - // - // This is what the rust code in the app_units crate does, and not doing this - // would regress bug 1323735, for example. - // - // FIXME(emilio, bug 1528114): Probably we should do something smarter. - if (IsZero()) { - // Avoid the expensive FP math below. - return 0; - } - float length = _0 * float(mozilla::AppUnitsPerCSSPixel()); +namespace detail { +static inline nscoord DefaultPercentLengthToAppUnits(float aPixelLength) { + return NSToCoordTruncClamped(aPixelLength); +} + +static inline nscoord DefaultLengthToAppUnits(float aPixelLength) { + // We want to round lengths rounding 0.5 away from zero, instead of the + // default behavior of NSToCoordRound{,WithClamp} which do floor(x + 0.5). + float length = aPixelLength * float(mozilla::AppUnitsPerCSSPixel()); if (length >= float(nscoord_MAX)) { return nscoord_MAX; } @@ -529,6 +524,15 @@ nscoord StyleCSSPixelLength::ToAppUnits() const { } return NSToIntRound(length); } +} // namespace detail + +nscoord StyleCSSPixelLength::ToAppUnits() const { + if (IsZero()) { + // Avoid the expensive FP math below. + return 0; + } + return detail::DefaultLengthToAppUnits(_0); +} bool LengthPercentage::IsLength() const { return Tag() == TAG_LENGTH; } @@ -693,6 +697,11 @@ CSSCoord StyleCalcLengthPercentage::ResolveToCSSPixels(CSSCoord aBasis) const { return Servo_ResolveCalcLengthPercentage(this, aBasis); } +nscoord StyleCalcLengthPercentage::Resolve(nscoord aBasis) const { + return detail::DefaultLengthToAppUnits( + ResolveToCSSPixels(CSSPixel::FromAppUnits(aBasis))); +} + template <> void StyleCalcNode::ScaleLengthsBy(float); @@ -708,20 +717,18 @@ CSSCoord LengthPercentage::ResolveToCSSPixels(CSSCoord aPercentageBasis) const { template <typename T> CSSCoord LengthPercentage::ResolveToCSSPixelsWith(T aPercentageGetter) const { - static_assert(std::is_same<decltype(aPercentageGetter()), CSSCoord>::value, - "Should return CSS pixels"); + static_assert(std::is_same_v<decltype(aPercentageGetter()), CSSCoord>); if (ConvertsToLength()) { return ToLengthInCSSPixels(); } return ResolveToCSSPixels(aPercentageGetter()); } -template <typename T, typename U> -nscoord LengthPercentage::Resolve(T aPercentageGetter, U aRounder) const { - static_assert(std::is_same<decltype(aPercentageGetter()), nscoord>::value, - "Should return app units"); - static_assert(std::is_same<decltype(aRounder(1.0f)), nscoord>::value, - "Should return app units"); +template <typename T, typename PercentRounder> +nscoord LengthPercentage::Resolve(T aPercentageGetter, + PercentRounder aPercentRounder) const { + static_assert(std::is_same_v<decltype(aPercentageGetter()), nscoord>); + static_assert(std::is_same_v<decltype(aPercentRounder(1.0f)), nscoord>); if (ConvertsToLength()) { return ToLength(); } @@ -730,29 +737,26 @@ nscoord LengthPercentage::Resolve(T aPercentageGetter, U aRounder) const { } nscoord basis = aPercentageGetter(); if (IsPercentage()) { - return aRounder(basis * AsPercentage()._0); + return aPercentRounder(basis * AsPercentage()._0); } - return AsCalc().Resolve(basis, aRounder); + return AsCalc().Resolve(basis); } -// Note: the static_cast<> wrappers below are needed to disambiguate between -// the versions of NSToCoordTruncClamped that take float vs. double as the arg. nscoord LengthPercentage::Resolve(nscoord aPercentageBasis) const { return Resolve([=] { return aPercentageBasis; }, - static_cast<nscoord (*)(float)>(NSToCoordTruncClamped)); + detail::DefaultPercentLengthToAppUnits); } template <typename T> nscoord LengthPercentage::Resolve(T aPercentageGetter) const { - return Resolve(aPercentageGetter, - static_cast<nscoord (*)(float)>(NSToCoordTruncClamped)); + return Resolve(aPercentageGetter, detail::DefaultPercentLengthToAppUnits); } -template <typename T> +template <typename PercentRounder> nscoord LengthPercentage::Resolve(nscoord aPercentageBasis, - T aPercentageRounder) const { + PercentRounder aPercentRounder) const { return Resolve([aPercentageBasis] { return aPercentageBasis; }, - aPercentageRounder); + aPercentRounder); } void LengthPercentage::ScaleLengthsBy(float aScale) { |