From 9dde11abbeb313ec945a3a3823cf5c14cff6522c Mon Sep 17 00:00:00 2001 From: Makoto Kato Date: Tue, 3 Oct 2023 10:43:17 +0000 Subject: [PATCH] Bug 1848282 - Part 1. Move IsPunctuation to nsUnicharUtils. r=TYLin Remove duplication code. Differential Revision: https://phabricator.services.mozilla.com/D188784 --- accessible/base/TextLeafRange.cpp | 27 ++---------------------- intl/unicharutil/util/nsUnicharUtils.cpp | 26 +++++++++++++++++++++++ intl/unicharutil/util/nsUnicharUtils.h | 7 ++++++ layout/generic/nsTextFrame.cpp | 26 +---------------------- 4 files changed, 36 insertions(+), 50 deletions(-) diff --git a/accessible/base/TextLeafRange.cpp b/accessible/base/TextLeafRange.cpp index 0ad6bfd138ad..b437ca088b6f 100644 --- a/accessible/base/TextLeafRange.cpp +++ b/accessible/base/TextLeafRange.cpp @@ -31,7 +31,7 @@ #include "nsStyleStructInlines.h" #include "nsTArray.h" #include "nsTextFrame.h" -#include "nsUnicodeProperties.h" +#include "nsUnicharUtils.h" #include "Pivot.h" #include "TextAttrs.h" @@ -300,30 +300,7 @@ static WordBreakClass GetWordBreakClass(char16_t aChar) { default: break; } - // Based on ClusterIterator::IsPunctuation in - // layout/generic/nsTextFrame.cpp. - uint8_t cat = unicode::GetGeneralCategory(aChar); - switch (cat) { - case HB_UNICODE_GENERAL_CATEGORY_CONNECT_PUNCTUATION: /* Pc */ - if (aChar == '_' && - !StaticPrefs::layout_word_select_stop_at_underscore()) { - return eWbcOther; - } - [[fallthrough]]; - case HB_UNICODE_GENERAL_CATEGORY_DASH_PUNCTUATION: /* Pd */ - case HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION: /* Pe */ - case HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION: /* Pf */ - case HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION: /* Pi */ - case HB_UNICODE_GENERAL_CATEGORY_OTHER_PUNCTUATION: /* Po */ - case HB_UNICODE_GENERAL_CATEGORY_OPEN_PUNCTUATION: /* Ps */ - case HB_UNICODE_GENERAL_CATEGORY_CURRENCY_SYMBOL: /* Sc */ - case HB_UNICODE_GENERAL_CATEGORY_MATH_SYMBOL: /* Sm */ - case HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL: /* So */ - return eWbcPunct; - default: - break; - } - return eWbcOther; + return mozilla::IsPunctuationForWordSelect(aChar) ? eWbcPunct : eWbcOther; } /** diff --git a/intl/unicharutil/util/nsUnicharUtils.cpp b/intl/unicharutil/util/nsUnicharUtils.cpp index db1627460c71..31ac06a52df6 100644 --- a/intl/unicharutil/util/nsUnicharUtils.cpp +++ b/intl/unicharutil/util/nsUnicharUtils.cpp @@ -9,6 +9,7 @@ #include "mozilla/Likely.h" #include "mozilla/HashFunctions.h" #include "mozilla/intl/UnicodeProperties.h" +#include "mozilla/StaticPrefs_layout.h" // We map x -> x, except for upper-case letters, // which we map to their lower-case equivalents. @@ -519,4 +520,29 @@ bool IsSegmentBreakSkipChar(uint32_t u) { intl::UnicodeProperties::GetScriptCode(u) != intl::Script::HANGUL; } +bool IsPunctuationForWordSelect(char16_t aCh) { + const uint8_t cat = unicode::GetGeneralCategory(aCh); + switch (cat) { + case HB_UNICODE_GENERAL_CATEGORY_CONNECT_PUNCTUATION: /* Pc */ + if (aCh == '_' && !StaticPrefs::layout_word_select_stop_at_underscore()) { + return false; + } + [[fallthrough]]; + case HB_UNICODE_GENERAL_CATEGORY_DASH_PUNCTUATION: /* Pd */ + case HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION: /* Pe */ + case HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION: /* Pf */ + case HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION: /* Pi */ + case HB_UNICODE_GENERAL_CATEGORY_OTHER_PUNCTUATION: /* Po */ + case HB_UNICODE_GENERAL_CATEGORY_OPEN_PUNCTUATION: /* Ps */ + case HB_UNICODE_GENERAL_CATEGORY_CURRENCY_SYMBOL: /* Sc */ + // Deliberately omitted: + // case HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL: /* Sk */ + case HB_UNICODE_GENERAL_CATEGORY_MATH_SYMBOL: /* Sm */ + case HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL: /* So */ + return true; + default: + return false; + } +} + } // namespace mozilla diff --git a/intl/unicharutil/util/nsUnicharUtils.h b/intl/unicharutil/util/nsUnicharUtils.h index 5fcf184ec212..693e192e411f 100644 --- a/intl/unicharutil/util/nsUnicharUtils.h +++ b/intl/unicharutil/util/nsUnicharUtils.h @@ -155,6 +155,13 @@ uint32_t HashUTF8AsUTF16(const char* aUTF8, size_t aLength, bool* aErr); bool IsSegmentBreakSkipChar(uint32_t u); +/** + * Return true for all Punctuation categories (Unicode general category P?), + * and also for Symbol categories (S?) except for Modifier Symbol, which is + * kept together with any adjacent letter/number. (Bug 1066756) + */ +bool IsPunctuationForWordSelect(char16_t aCh); + } // namespace mozilla #endif /* nsUnicharUtils_h__ */ diff --git a/layout/generic/nsTextFrame.cpp b/layout/generic/nsTextFrame.cpp index 49f39866f33c..3e83198c0329 100644 --- a/layout/generic/nsTextFrame.cpp +++ b/layout/generic/nsTextFrame.cpp @@ -7833,32 +7833,8 @@ bool ClusterIterator::IsNewline() const { bool ClusterIterator::IsPunctuation() const { NS_ASSERTION(mCharIndex >= 0, "No cluster selected"); - // Return true for all Punctuation categories (Unicode general category P?), - // and also for Symbol categories (S?) except for Modifier Symbol, which is - // kept together with any adjacent letter/number. (Bug 1066756) const char16_t ch = mFrag->CharAt(AssertedCast(mCharIndex)); - const uint8_t cat = unicode::GetGeneralCategory(ch); - switch (cat) { - case HB_UNICODE_GENERAL_CATEGORY_CONNECT_PUNCTUATION: /* Pc */ - if (ch == '_' && !StaticPrefs::layout_word_select_stop_at_underscore()) { - return false; - } - [[fallthrough]]; - case HB_UNICODE_GENERAL_CATEGORY_DASH_PUNCTUATION: /* Pd */ - case HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION: /* Pe */ - case HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION: /* Pf */ - case HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION: /* Pi */ - case HB_UNICODE_GENERAL_CATEGORY_OTHER_PUNCTUATION: /* Po */ - case HB_UNICODE_GENERAL_CATEGORY_OPEN_PUNCTUATION: /* Ps */ - case HB_UNICODE_GENERAL_CATEGORY_CURRENCY_SYMBOL: /* Sc */ - // Deliberately omitted: - // case HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL: /* Sk */ - case HB_UNICODE_GENERAL_CATEGORY_MATH_SYMBOL: /* Sm */ - case HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL: /* So */ - return true; - default: - return false; - } + return mozilla::IsPunctuationForWordSelect(ch); } int32_t ClusterIterator::GetAfterInternal() const {