diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.h b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.h index 69cc621aaf75..c1eb566133d2 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.h +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.h @@ -130,6 +130,11 @@ inline bool areTextAttributesEquivalentLayoutWise(const TextAttributes &lhs, con lhs.fontVariationSettings, lhs.allowFontScaling, lhs.dynamicTypeRamp, + lhs.textTransform, + lhs.baseWritingDirection, + lhs.lineBreakStrategy, + lhs.lineBreakMode, + lhs.layoutDirection, lhs.alignment) == std::tie( rhs.fontFamily, @@ -139,16 +144,20 @@ inline bool areTextAttributesEquivalentLayoutWise(const TextAttributes &lhs, con rhs.fontVariationSettings, rhs.allowFontScaling, rhs.dynamicTypeRamp, + rhs.textTransform, + rhs.baseWritingDirection, + rhs.lineBreakStrategy, + rhs.lineBreakMode, + rhs.layoutDirection, rhs.alignment) && floatEquality(lhs.fontSize, rhs.fontSize) && floatEquality(lhs.fontSizeMultiplier, rhs.fontSizeMultiplier) && floatEquality(lhs.letterSpacing, rhs.letterSpacing) && floatEquality(lhs.lineHeight, rhs.lineHeight) && - floatEquality(lhs.maxFontSizeMultiplier, rhs.maxFontSizeMultiplier); + floatEquality(lhs.maxFontSizeMultiplier, rhs.maxFontSizeMultiplier) && lhs.textEffects == rhs.textEffects; } inline size_t textAttributesHashLayoutWise(const TextAttributes &textAttributes) { - // Taking into account the same props as - // `areTextAttributesEquivalentLayoutWise` mentions. + // Hash a subset of the attributes compared by `areTextAttributesEquivalentLayoutWise`. // The optionals are folded into one presence mask rather than being chained individually, so // that the properties a given text run does not set stay off the hash's dependency chain. This // is the hot path: it runs on every measurement-cache probe. diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/tests/TextLayoutManagerTest.cpp b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/tests/TextLayoutManagerTest.cpp index ac0593cd44c5..512a91e9ff69 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/tests/TextLayoutManagerTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/tests/TextLayoutManagerTest.cpp @@ -82,7 +82,7 @@ TEST(TextLayoutManagerTest, emptyFontVariationSettingsClearInheritedSettings) { EXPECT_TRUE(parent.fontVariationSettings->empty()); } -TEST(TextLayoutManagerTest, everyLayoutAttributeAffectsEqualityAndHash) { +TEST(TextLayoutManagerTest, hashedLayoutAttributesAffectCacheKeys) { TextAttributes baseline; baseline.fontFamily = "Inter"; baseline.fontSize = 16; @@ -175,6 +175,63 @@ TEST(TextLayoutManagerTest, everyLayoutAttributeAffectsEqualityAndHash) { } } +TEST(TextLayoutManagerTest, additionalLayoutAttributesAffectCacheKeyEquality) { + AttributedString baseline; + baseline.appendFragment({.string = "Text"}); + + struct TestCase { + const char* name; + void (*mutate)(TextAttributes&); + }; + const std::array testCases{{ + {.name = "textTransform", + .mutate = + [](TextAttributes& attributes) { + attributes.textTransform = TextTransform::Uppercase; + }}, + {.name = "baseWritingDirection", + .mutate = + [](TextAttributes& attributes) { + attributes.baseWritingDirection = WritingDirection::RightToLeft; + }}, + {.name = "lineBreakStrategy", + .mutate = + [](TextAttributes& attributes) { + attributes.lineBreakStrategy = + LineBreakStrategy::HangulWordPriority; + }}, + {.name = "lineBreakMode", + .mutate = + [](TextAttributes& attributes) { + attributes.lineBreakMode = LineBreakMode::Char; + }}, + {.name = "layoutDirection", + .mutate = + [](TextAttributes& attributes) { + attributes.layoutDirection = LayoutDirection::RightToLeft; + }}, + {.name = "textEffects", + .mutate = + [](TextAttributes& attributes) { + attributes.textEffects.push_back( + {.name = "scale", .props = nullptr}); + }}, + }}; + + for (const auto& testCase : testCases) { + SCOPED_TRACE(testCase.name); + auto changed = baseline; + testCase.mutate(changed.getFragments().front().textAttributes); + + EXPECT_FALSE( + TextMeasureCacheKey{.attributedString = baseline} == + TextMeasureCacheKey{.attributedString = changed}); + EXPECT_FALSE( + LineMeasureCacheKey{.attributedString = baseline} == + LineMeasureCacheKey{.attributedString = changed}); + } +} + TEST(TextLayoutManagerTest, equivalentPopulatedLayoutAttributesHashEqually) { TextAttributes lhs; lhs.fontFamily = "Inter";