From 59203c63bb777a3bacec32fb8830fba33540e809 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 12 Jun 2024 07:35:29 +0200 Subject: Adding upstream version 127.0. Signed-off-by: Daniel Baumann --- layout/generic/WritingModes.h | 227 +++++++++++++++++++----------------------- 1 file changed, 101 insertions(+), 126 deletions(-) (limited to 'layout/generic/WritingModes.h') diff --git a/layout/generic/WritingModes.h b/layout/generic/WritingModes.h index 0fcf47f1d0..4f6e9eaec1 100644 --- a/layout/generic/WritingModes.h +++ b/layout/generic/WritingModes.h @@ -11,10 +11,8 @@ #include "mozilla/intl/BidiEmbeddingLevel.h" #include "mozilla/ComputedStyle.h" -#include "mozilla/EnumeratedRange.h" - +#include "mozilla/EnumSet.h" #include "nsRect.h" -#include "nsBidiUtils.h" #include "nsStyleStruct.h" // It is the caller's responsibility to operate on logical-coordinate objects @@ -50,7 +48,7 @@ enum class LogicalAxis : uint8_t { Block, Inline, }; -enum LogicalEdge { eLogicalEdgeStart = 0x0, eLogicalEdgeEnd = 0x1 }; +enum class LogicalEdge : uint8_t { Start, End }; enum class LogicalSide : uint8_t { BStart, @@ -59,11 +57,6 @@ enum class LogicalSide : uint8_t { IEnd, }; -constexpr auto AllLogicalSides() { - return mozilla::MakeInclusiveEnumeratedRange(LogicalSide::BStart, - LogicalSide::IEnd); -} - enum class LogicalCorner : uint8_t { BStartIStart, BStartIEnd, @@ -72,7 +65,7 @@ enum class LogicalCorner : uint8_t { }; // Physical axis constants. -enum PhysicalAxis { eAxisVertical = 0x0, eAxisHorizontal = 0x1 }; +enum class PhysicalAxis : uint8_t { Vertical, Horizontal }; // Represents zero or more physical axes. enum class PhysicalAxes : uint8_t { @@ -104,38 +97,30 @@ inline LogicalAxis GetAxis(LogicalSide aSide) { } inline LogicalEdge GetEdge(LogicalSide aSide) { - return IsEnd(aSide) ? eLogicalEdgeEnd : eLogicalEdgeStart; + return IsEnd(aSide) ? LogicalEdge::End : LogicalEdge::Start; } inline LogicalEdge GetOppositeEdge(LogicalEdge aEdge) { - // This relies on the only two LogicalEdge enum values being 0 and 1. - return LogicalEdge(1 - aEdge); + return aEdge == LogicalEdge::Start ? LogicalEdge::End : LogicalEdge::Start; } inline LogicalSide MakeLogicalSide(LogicalAxis aAxis, LogicalEdge aEdge) { - return LogicalSide((uint8_t(aAxis) << 1) | aEdge); + if (aAxis == LogicalAxis::Inline) { + return aEdge == LogicalEdge::Start ? LogicalSide::IStart + : LogicalSide::IEnd; + } + return aEdge == LogicalEdge::Start ? LogicalSide::BStart : LogicalSide::BEnd; } inline LogicalSide GetOppositeSide(LogicalSide aSide) { return MakeLogicalSide(GetAxis(aSide), GetOppositeEdge(GetEdge(aSide))); } -enum LogicalSideBits { - eLogicalSideBitsNone = 0, - eLogicalSideBitsBStart = 1 << static_cast(LogicalSide::BStart), - eLogicalSideBitsBEnd = 1 << static_cast(LogicalSide::BEnd), - eLogicalSideBitsIEnd = 1 << static_cast(LogicalSide::IEnd), - eLogicalSideBitsIStart = 1 << static_cast(LogicalSide::IStart), - eLogicalSideBitsBBoth = eLogicalSideBitsBStart | eLogicalSideBitsBEnd, - eLogicalSideBitsIBoth = eLogicalSideBitsIStart | eLogicalSideBitsIEnd, - eLogicalSideBitsAll = eLogicalSideBitsBBoth | eLogicalSideBitsIBoth -}; - -enum LineRelativeDir { - eLineRelativeDirOver = static_cast(LogicalSide::BStart), - eLineRelativeDirUnder = static_cast(LogicalSide::BEnd), - eLineRelativeDirLeft = static_cast(LogicalSide::IStart), - eLineRelativeDirRight = static_cast(LogicalSide::IEnd) +enum class LineRelativeDir : uint8_t { + Over = static_cast(LogicalSide::BStart), + Under = static_cast(LogicalSide::BEnd), + Left = static_cast(LogicalSide::IStart), + Right = static_cast(LogicalSide::IEnd) }; /** @@ -155,54 +140,41 @@ class WritingMode { /** * Absolute inline flow direction */ - enum InlineDir { - eInlineLTR = 0x00, // text flows horizontally left to right - eInlineRTL = 0x02, // text flows horizontally right to left - eInlineTTB = 0x01, // text flows vertically top to bottom - eInlineBTT = 0x03, // text flows vertically bottom to top + enum class InlineDir : uint8_t { + LTR, // text flows horizontally left to right + RTL, // text flows horizontally right to left + TTB, // text flows vertically top to bottom + BTT, // text flows vertically bottom to top }; /** * Absolute block flow direction */ - enum BlockDir { - eBlockTB = 0x00, // horizontal lines stack top to bottom - eBlockRL = 0x01, // vertical lines stack right to left - eBlockLR = 0x05, // vertical lines stack left to right - }; - - /** - * Line-relative (bidi-relative) inline flow direction - */ - enum BidiDir { - eBidiLTR = 0x00, // inline flow matches bidi LTR text - eBidiRTL = 0x10, // inline flow matches bidi RTL text + enum class BlockDir : uint8_t { + TB, // horizontal lines stack top to bottom + RL, // vertical lines stack right to left + LR, // vertical lines stack left to right }; - /** - * Unknown writing mode (should never actually be stored or used anywhere). - */ - enum { eUnknownWritingMode = 0xff }; - /** * Return the absolute inline flow direction as an InlineDir */ InlineDir GetInlineDir() const { - return InlineDir(mWritingMode._0 & eInlineMask); + if (IsVertical()) { + return IsInlineReversed() ? InlineDir::BTT : InlineDir::TTB; + } + return IsInlineReversed() ? InlineDir::RTL : InlineDir::LTR; } /** * Return the absolute block flow direction as a BlockDir */ BlockDir GetBlockDir() const { - return BlockDir(mWritingMode._0 & eBlockMask); - } - - /** - * Return the line-relative inline flow direction as a BidiDir - */ - BidiDir GetBidiDir() const { - return BidiDir((mWritingMode & StyleWritingMode::RTL)._0); + if (IsVertical()) { + return mWritingMode & StyleWritingMode::VERTICAL_LR ? BlockDir::LR + : BlockDir::RL; + } + return BlockDir::TB; } /** @@ -216,14 +188,14 @@ class WritingMode { } /** - * Return true if bidi direction is LTR. (Convenience method) + * Return true if bidi direction is LTR. */ - bool IsBidiLTR() const { return eBidiLTR == GetBidiDir(); } + bool IsBidiLTR() const { return !IsBidiRTL(); } /** - * Return true if bidi direction is RTL. (Convenience method) + * Return true if bidi direction is RTL. */ - bool IsBidiRTL() const { return eBidiRTL == GetBidiDir(); } + bool IsBidiRTL() const { return !!(mWritingMode & StyleWritingMode::RTL); } /** * True if it is vertical and vertical-lr, or is horizontal and bidi LTR. @@ -242,12 +214,12 @@ class WritingMode { /** * True if vertical-mode block direction is LR (convenience method). */ - bool IsVerticalLR() const { return eBlockLR == GetBlockDir(); } + bool IsVerticalLR() const { return GetBlockDir() == BlockDir::LR; } /** * True if vertical-mode block direction is RL (convenience method). */ - bool IsVerticalRL() const { return eBlockRL == GetBlockDir(); } + bool IsVerticalRL() const { return GetBlockDir() == BlockDir::RL; } /** * True if vertical writing mode, i.e. when @@ -339,8 +311,9 @@ class WritingMode { uint8_t(StyleWritingModeProperty::VerticalRl) == 1 && uint8_t(StyleWritingModeProperty::VerticalLr) == 3 && uint8_t(LogicalAxis::Block) == 0 && - uint8_t(LogicalAxis::Inline) == 1 && eAxisVertical == 0 && - eAxisHorizontal == 1, + uint8_t(LogicalAxis::Inline) == 1 && + uint8_t(PhysicalAxis::Vertical) == 0 && + uint8_t(PhysicalAxis::Horizontal) == 1, "unexpected writing-mode, logical axis or physical axis " "constant values"); return mozilla::PhysicalAxis((aWritingModeValue ^ uint8_t(aAxis)) & 0x1); @@ -376,7 +349,7 @@ class WritingMode { // What's left of the writing-mode should be in the range 0-3: NS_ASSERTION(aWritingModeValue < 4, "invalid aWritingModeValue value"); - return kLogicalBlockSides[aWritingModeValue][aEdge]; + return kLogicalBlockSides[aWritingModeValue][static_cast(aEdge)]; } mozilla::Side PhysicalSideForInlineAxis(LogicalEdge aEdge) const { @@ -413,13 +386,13 @@ class WritingMode { // StyleWritingMode::INLINE_REVERSED, StyleWritingMode::VERTICAL_LR and // StyleWritingMode::LINE_INVERTED bits. Use these four bits to index into // kLogicalInlineSides. - MOZ_ASSERT(StyleWritingMode::VERTICAL._0 == 0x01 && - StyleWritingMode::INLINE_REVERSED._0 == 0x02 && - StyleWritingMode::VERTICAL_LR._0 == 0x04 && - StyleWritingMode::LINE_INVERTED._0 == 0x08, - "unexpected mask values"); - int index = mWritingMode._0 & 0x0F; - return kLogicalInlineSides[index][aEdge]; + static_assert(StyleWritingMode::VERTICAL._0 == 0x01 && + StyleWritingMode::INLINE_REVERSED._0 == 0x02 && + StyleWritingMode::VERTICAL_LR._0 == 0x04 && + StyleWritingMode::LINE_INVERTED._0 == 0x08, + "Unexpected values for StyleWritingMode constants!"); + uint8_t index = mWritingMode._0 & 0x0F; + return kLogicalInlineSides[index][static_cast(aEdge)]; } /** @@ -428,9 +401,9 @@ class WritingMode { */ mozilla::Side PhysicalSide(LogicalSide aSide) const { if (IsBlock(aSide)) { - MOZ_ASSERT(StyleWritingMode::VERTICAL._0 == 0x01 && - StyleWritingMode::VERTICAL_LR._0 == 0x04, - "unexpected mask values"); + static_assert(StyleWritingMode::VERTICAL._0 == 0x01 && + StyleWritingMode::VERTICAL_LR._0 == 0x04, + "Unexpected values for StyleWritingMode constants!"); const uint8_t wm = ((mWritingMode & StyleWritingMode::VERTICAL_LR)._0 >> 1) | (mWritingMode & StyleWritingMode::VERTICAL)._0; @@ -490,12 +463,12 @@ class WritingMode { }; // clang-format on - MOZ_ASSERT(StyleWritingMode::VERTICAL._0 == 0x01 && - StyleWritingMode::INLINE_REVERSED._0 == 0x02 && - StyleWritingMode::VERTICAL_LR._0 == 0x04 && - StyleWritingMode::LINE_INVERTED._0 == 0x08, - "unexpected mask values"); - int index = mWritingMode._0 & 0x0F; + static_assert(StyleWritingMode::VERTICAL._0 == 0x01 && + StyleWritingMode::INLINE_REVERSED._0 == 0x02 && + StyleWritingMode::VERTICAL_LR._0 == 0x04 && + StyleWritingMode::LINE_INVERTED._0 == 0x08, + "Unexpected values for StyleWritingMode constants!"); + uint8_t index = mWritingMode._0 & 0x0F; return kPhysicalToLogicalSides[index][aSide]; } @@ -580,7 +553,7 @@ class WritingMode { bool ParallelAxisStartsOnSameSide(LogicalAxis aLogicalAxis, const WritingMode& aOther) const { mozilla::Side myStartSide = - this->PhysicalSide(MakeLogicalSide(aLogicalAxis, eLogicalEdgeStart)); + this->PhysicalSide(MakeLogicalSide(aLogicalAxis, LogicalEdge::Start)); // Figure out which of aOther's axes is parallel to |this| WritingMode's // aLogicalAxis, and get its physical start side as well. @@ -588,7 +561,7 @@ class WritingMode { ? GetOrthogonalAxis(aLogicalAxis) : aLogicalAxis; mozilla::Side otherWMStartSide = - aOther.PhysicalSide(MakeLogicalSide(otherWMAxis, eLogicalEdgeStart)); + aOther.PhysicalSide(MakeLogicalSide(otherWMAxis, LogicalEdge::Start)); NS_ASSERTION(myStartSide % 2 == otherWMStartSide % 2, "Should end up with sides in the same physical axis"); @@ -610,11 +583,16 @@ class WritingMode { // instance from it. friend struct widget::IMENotification; + /** + * Unknown writing mode (should never actually be stored or used anywhere). + */ + static constexpr uint8_t kUnknownWritingMode = 0xff; + /** * Return a WritingMode representing an unknown value. */ static inline WritingMode Unknown() { - return WritingMode(eUnknownWritingMode); + return WritingMode(kUnknownWritingMode); } /** @@ -624,12 +602,6 @@ class WritingMode { explicit WritingMode(uint8_t aValue) : mWritingMode{aValue} {} StyleWritingMode mWritingMode; - - enum Masks { - // Masks for output enums - eInlineMask = 0x03, // VERTICAL | INLINE_REVERSED - eBlockMask = 0x05, // VERTICAL | VERTICAL_LR - }; }; inline std::ostream& operator<<(std::ostream& aStream, const WritingMode& aWM) { @@ -818,7 +790,7 @@ class LogicalPoint { LogicalPoint operator+(const LogicalPoint& aOther) const { CHECK_WRITING_MODE(aOther.GetWritingMode()); // In non-debug builds, LogicalPoint does not store the WritingMode, - // so the first parameter here (which will always be eUnknownWritingMode) + // so the first parameter here (which will always be WritingMode::Unknown()) // is ignored. return LogicalPoint(GetWritingMode(), mPoint.x + aOther.mPoint.x, mPoint.y + aOther.mPoint.y); @@ -834,7 +806,7 @@ class LogicalPoint { LogicalPoint operator-(const LogicalPoint& aOther) const { CHECK_WRITING_MODE(aOther.GetWritingMode()); // In non-debug builds, LogicalPoint does not store the WritingMode, - // so the first parameter here (which will always be eUnknownWritingMode) + // so the first parameter here (which will always be WritingMode::Unknown()) // is ignored. return LogicalPoint(GetWritingMode(), mPoint.x - aOther.mPoint.x, mPoint.y - aOther.mPoint.y); @@ -857,7 +829,7 @@ class LogicalPoint { /** * NOTE that in non-DEBUG builds, GetWritingMode() always returns - * eUnknownWritingMode, as the current mode is not stored in the logical- + * WritingMode::Unknown(), as the current mode is not stored in the logical- * geometry classes. Therefore, this method is private; it is used ONLY * by the DEBUG-mode checking macros in this class and its friends; * other code is not allowed to ask a logical point for its writing mode, @@ -1107,48 +1079,51 @@ class LogicalSize { * LogicalSides represents a set of logical sides. */ struct LogicalSides final { + static constexpr EnumSet BBoth{LogicalSide::BStart, + LogicalSide::BEnd}; + static constexpr EnumSet IBoth{LogicalSide::IStart, + LogicalSide::IEnd}; + static constexpr EnumSet All{ + LogicalSide::BStart, LogicalSide::BEnd, LogicalSide::IStart, + LogicalSide::IEnd}; + explicit LogicalSides(WritingMode aWritingMode) +#ifdef DEBUG + : mWritingMode(aWritingMode) +#endif + { + } + LogicalSides(WritingMode aWritingMode, LogicalSides aSides) : #ifdef DEBUG mWritingMode(aWritingMode), #endif - mBits(0) { + mSides(aSides.mSides) { } - LogicalSides(WritingMode aWritingMode, LogicalSideBits aSideBits) + LogicalSides(WritingMode aWritingMode, EnumSet aSides) : #ifdef DEBUG mWritingMode(aWritingMode), #endif - mBits(aSideBits) { - MOZ_ASSERT((aSideBits & ~eLogicalSideBitsAll) == 0, "illegal side bits"); - } - bool IsEmpty() const { return mBits == 0; } - bool BStart() const { return mBits & eLogicalSideBitsBStart; } - bool BEnd() const { return mBits & eLogicalSideBitsBEnd; } - bool IStart() const { return mBits & eLogicalSideBitsIStart; } - bool IEnd() const { return mBits & eLogicalSideBitsIEnd; } - bool Contains(LogicalSideBits aSideBits) const { - MOZ_ASSERT((aSideBits & ~eLogicalSideBitsAll) == 0, "illegal side bits"); - return (mBits & aSideBits) == aSideBits; - } - LogicalSides operator|(LogicalSides aOther) const { - CHECK_WRITING_MODE(aOther.GetWritingMode()); - return *this | LogicalSideBits(aOther.mBits); - } - LogicalSides operator|(LogicalSideBits aSideBits) const { - return LogicalSides(GetWritingMode(), LogicalSideBits(mBits | aSideBits)); - } - LogicalSides& operator|=(LogicalSides aOther) { - CHECK_WRITING_MODE(aOther.GetWritingMode()); - return *this |= LogicalSideBits(aOther.mBits); + mSides(aSides) { + } + bool IsEmpty() const { return mSides.isEmpty(); } + bool BStart() const { return mSides.contains(LogicalSide::BStart); } + bool BEnd() const { return mSides.contains(LogicalSide::BEnd); } + bool IStart() const { return mSides.contains(LogicalSide::IStart); } + bool IEnd() const { return mSides.contains(LogicalSide::IEnd); } + bool Contains(LogicalSide aSide) const { return mSides.contains(aSide); } + LogicalSides& operator+=(LogicalSides aOther) { + mSides += aOther.mSides; + return *this; } - LogicalSides& operator|=(LogicalSideBits aSideBits) { - mBits |= aSideBits; + LogicalSides& operator+=(LogicalSide aOther) { + mSides += aOther; return *this; } bool operator==(LogicalSides aOther) const { CHECK_WRITING_MODE(aOther.GetWritingMode()); - return mBits == aOther.mBits; + return mSides == aOther.mSides; } bool operator!=(LogicalSides aOther) const { CHECK_WRITING_MODE(aOther.GetWritingMode()); @@ -1165,7 +1140,7 @@ struct LogicalSides final { #ifdef DEBUG WritingMode mWritingMode; #endif - uint8_t mBits; + EnumSet mSides; }; /** -- cgit v1.2.3