From c096b46278449f2d0126a5c9327a8b0218dd5beb Mon Sep 17 00:00:00 2001 From: Moonchild Date: Fri, 2 Aug 2024 11:15:28 +0200 Subject: [PATCH] Issue #1925 - Convert NS_SIDE_TO_HALF_CORNER to a constexpr function. --- gfx/2d/Types.h | 20 ++++++++++++++++++++ layout/generic/nsFrame.cpp | 12 ++++++------ layout/style/nsStyleConsts.h | 7 ------- layout/style/nsStyleCoord.cpp | 5 +++-- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/gfx/2d/Types.h b/gfx/2d/Types.h index ad83a79889..43e8c14963 100644 --- a/gfx/2d/Types.h +++ b/gfx/2d/Types.h @@ -459,6 +459,9 @@ static inline mozilla::HalfCorner operator++(mozilla::HalfCorner& aHalfCorner) { return aHalfCorner; } +// The result of the conversion functions below are exhaustively checked in +// nsStyleCoord.cpp, which also serves as usage examples. + constexpr bool HalfCornerIsX(HalfCorner aHalfCorner) { return !(aHalfCorner % 2); @@ -489,6 +492,23 @@ constexpr Corner SideToFullCorner(Side aSide, bool aIsSecond) return Corner((aSide + aIsSecond) % 4); } +/* @param aIsSecond: when true, return the clockwise second of the two + * corners associated with aSide. For example, with aSide = eSideBottom the + * result is eCornerBottomRight when aIsSecond is false, and + * eCornerBottomLeft when aIsSecond is true. + * @param aIsParallel: return the half-corner that is parallel with aSide + * when aIsParallel is true. For example with aSide=eSideTop, aIsSecond=true + * the result is eCornerTopRightX when aIsParallel is true, and + * eCornerTopRightY when aIsParallel is false (because "X" is parallel with + * eSideTop/eSideBottom, similarly "Y" is parallel with eSideLeft/eSideRight) + */ +constexpr HalfCorner SideToHalfCorner(Side aSide, + bool aIsSecond, + bool aIsParallel) +{ + return HalfCorner(((aSide + aIsSecond) * 2 + (aSide + !aIsParallel) % 2) % 8); +} + } // namespace mozilla #endif /* MOZILLA_GFX_TYPES_H_ */ diff --git a/layout/generic/nsFrame.cpp b/layout/generic/nsFrame.cpp index 451a1ff7cd..c7f2471bca 100644 --- a/layout/generic/nsFrame.cpp +++ b/layout/generic/nsFrame.cpp @@ -1284,8 +1284,8 @@ nsIFrame::ComputeBorderRadii(const nsStyleCorners& aBorderRadius, bool haveRadius = false; double ratio = 1.0f; NS_FOR_CSS_SIDES(side) { - uint32_t hc1 = NS_SIDE_TO_HALF_CORNER(side, false, true); - uint32_t hc2 = NS_SIDE_TO_HALF_CORNER(side, true, true); + uint32_t hc1 = SideToHalfCorner(side, false, true); + uint32_t hc2 = SideToHalfCorner(side, true, true); nscoord length = SideIsVertical(side) ? aBorderArea.height : aBorderArea.width; nscoord sum = aRadii[hc1] + aRadii[hc2]; @@ -1310,8 +1310,8 @@ nsIFrame::InsetBorderRadii(nscoord aRadii[8], const nsMargin &aOffsets) { NS_FOR_CSS_SIDES(side) { nscoord offset = aOffsets.Side(side); - uint32_t hc1 = NS_SIDE_TO_HALF_CORNER(side, false, false); - uint32_t hc2 = NS_SIDE_TO_HALF_CORNER(side, true, false); + uint32_t hc1 = SideToHalfCorner(side, false, false); + uint32_t hc2 = SideToHalfCorner(side, true, false); aRadii[hc1] = std::max(0, aRadii[hc1] - offset); aRadii[hc2] = std::max(0, aRadii[hc2] - offset); } @@ -1322,8 +1322,8 @@ nsIFrame::OutsetBorderRadii(nscoord aRadii[8], const nsMargin &aOffsets) { NS_FOR_CSS_SIDES(side) { nscoord offset = aOffsets.Side(side); - uint32_t hc1 = NS_SIDE_TO_HALF_CORNER(side, false, false); - uint32_t hc2 = NS_SIDE_TO_HALF_CORNER(side, true, false); + uint32_t hc1 = SideToHalfCorner(side, false, false); + uint32_t hc2 = SideToHalfCorner(side, true, false); if (aRadii[hc1] > 0) aRadii[hc1] += offset; if (aRadii[hc2] > 0) diff --git a/layout/style/nsStyleConsts.h b/layout/style/nsStyleConsts.h index 96690ae2d8..9507ea6fab 100644 --- a/layout/style/nsStyleConsts.h +++ b/layout/style/nsStyleConsts.h @@ -16,13 +16,6 @@ namespace mozilla { -// The results of these conversion macros are exhaustively checked in -// nsStyleCoord.cpp. -// Arguments must not have side effects. - -#define NS_SIDE_TO_HALF_CORNER(side_, second_, parallel_) \ - ((((side_) + !!(second_))*2 + ((side_) + !(parallel_))%2) % 8) - // Basic shapes enum class StyleBasicShapeType : uint8_t { Polygon, diff --git a/layout/style/nsStyleCoord.cpp b/layout/style/nsStyleCoord.cpp index 263b0844b2..f93bb53f63 100644 --- a/layout/style/nsStyleCoord.cpp +++ b/layout/style/nsStyleCoord.cpp @@ -397,9 +397,10 @@ CASE(eSideLeft, false, eCornerBottomLeft); CASE(eSideLeft, true, eCornerTopLeft); #undef CASE +//Validation of SideToHalfCorner. #define CASE(side, second, parallel, result) \ - static_assert(NS_SIDE_TO_HALF_CORNER(side, second, parallel) == result, \ - "NS_SIDE_TO_HALF_CORNER is wrong") + static_assert(SideToHalfCorner(side, second, parallel) == result, \ + "SideToHalfCorner is wrong") CASE(eSideTop, false, true, eCornerTopLeftX); CASE(eSideTop, false, false, eCornerTopLeftY); CASE(eSideTop, true, true, eCornerTopRightX);