diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt index f60b5b043c3..c53e7d9e168 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt @@ -20,6 +20,7 @@ import android.text.SpannableString import android.text.SpannableStringBuilder import android.text.Spanned import android.text.StaticLayout +import android.text.TextDirectionHeuristic import android.text.TextDirectionHeuristics import android.text.TextPaint import android.text.TextUtils @@ -246,6 +247,10 @@ internal object TextLayoutManager { ) == LayoutDirection.RTL } + private fun getTextDirectionHeuristic(isParagraphRTL: Boolean): TextDirectionHeuristic = + if (isParagraphRTL) TextDirectionHeuristics.FIRSTSTRONG_RTL + else TextDirectionHeuristics.FIRSTSTRONG_LTR + private fun getTextAlignmentAttr(attributedString: MapBuffer): String? { // TODO: Don't read AS_KEY_FRAGMENTS, which may be expensive, and is not present when using // cached Spannable @@ -288,7 +293,7 @@ internal object TextLayoutManager { // I.e. paragraph is LTR but script is RTL, text needs to be aligned to the left, which means // ALIGN_OPPOSITE needs to be used to align RTL script to the left val isParagraphRTL = isRTL(attributedString) - val isScriptRTL = TextDirectionHeuristics.FIRSTSTRONG_LTR.isRtl(spanned, 0, spanned.length) + val isScriptRTL = getTextDirectionHeuristic(isParagraphRTL).isRtl(spanned, 0, spanned.length) val swapNormalAndOpposite = isParagraphRTL != isScriptRTL var alignment = @@ -317,7 +322,8 @@ internal object TextLayoutManager { // depending on whether the script is LTR or RTL, ALIGN_NORMAL and ALIGN_OPPOSITE may mean // different things - val swapLeftAndRight = TextDirectionHeuristics.FIRSTSTRONG_LTR.isRtl(spanned, 0, spanned.length) + val swapLeftAndRight = + getTextDirectionHeuristic(isRTL(attributedString)).isRtl(spanned, 0, spanned.length) return when (alignment) { Layout.Alignment.ALIGN_NORMAL -> if (swapLeftAndRight) Gravity.RIGHT else Gravity.LEFT @@ -914,6 +920,7 @@ internal object TextLayoutManager { ellipsizeMode: TextUtils.TruncateAt?, maxNumberOfLines: Int, paint: TextPaint, + textDirection: TextDirectionHeuristic = TextDirectionHeuristics.FIRSTSTRONG_LTR, ): Layout { // If our text is boring, and fully fits in the available space, we can represent the text // layout as a BoringLayout @@ -978,6 +985,7 @@ internal object TextLayoutManager { ellipsizeMode, maxNumberOfLines, paint, + textDirection, enableStartOverhang, ) @@ -996,6 +1004,7 @@ internal object TextLayoutManager { ellipsizeMode, maxNumberOfLines, paint, + textDirection, enableStartOverhang, shiftDrawingOffsetForStartOverhang = false, ) @@ -1018,6 +1027,7 @@ internal object TextLayoutManager { ellipsizeMode, maxNumberOfLines, paint, + textDirection, enableStartOverhang, ) } @@ -1087,12 +1097,14 @@ internal object TextLayoutManager { ellipsizeMode: TextUtils.TruncateAt?, maxNumberOfLines: Int, paint: TextPaint, + textDirection: TextDirectionHeuristic, enableStartOverhang: Boolean, shiftDrawingOffsetForStartOverhang: Boolean = enableStartOverhang, ): Layout { val builder = StaticLayout.Builder.obtain(text, 0, text.length, paint, layoutWidth) .setAlignment(alignment) + .setTextDirection(textDirection) .setLineSpacing(0f, 1f) .setIncludePad(includeFontPadding) .setBreakStrategy(textBreakStrategy) @@ -1308,7 +1320,12 @@ internal object TextLayoutManager { height: Float, heightYogaMeasureMode: YogaMeasureMode, ): CreateLayoutResult { - var boring = isBoring(text, paint) + // Paragraphs without a strong character follow the direction of the whole text + val textDirection = + if (getTextDirectionHeuristic(isRTL(attributedString)).isRtl(text, 0, text.length)) + TextDirectionHeuristics.FIRSTSTRONG_RTL + else TextDirectionHeuristics.FIRSTSTRONG_LTR + var boring = isBoring(text, paint, textDirection) val textBreakStrategy = TextAttributeProps.getTextBreakStrategy( @@ -1385,7 +1402,7 @@ internal object TextLayoutManager { // The spans and paint now hold the fitted font size. Re-measure so the final layout does not // keep the line metrics of the original, larger font. if (boring != null) { - boring = isBoring(text, paint) + boring = isBoring(text, paint, textDirection) } } @@ -1405,6 +1422,7 @@ internal object TextLayoutManager { ellipsizeMode, maximumNumberOfLines, paint, + textDirection, ) if ( @@ -1430,6 +1448,7 @@ internal object TextLayoutManager { ellipsizeMode, maximumNumberOfLines, paint, + textDirection, /* enableStartOverhang = */ false, ) if (calculateLineCount(tightenedLayout, maximumNumberOfLines) == lineCount) { @@ -1722,7 +1741,6 @@ internal object TextLayoutManager { textEffectRegistry: TextEffectRegistry? = null, spannableCacheOwner: ReactContext? = null, ): Long { - // TODO(5578671): Handle text direction (see View#getTextDirectionHeuristic) val layout = createLayoutForMeasurement( assets, @@ -2049,14 +2067,20 @@ internal object TextLayoutManager { ) } - private fun isBoring(text: Spannable, paint: TextPaint): BoringLayout.Metrics? { + private fun isBoring( + text: Spannable, + paint: TextPaint, + textDirection: TextDirectionHeuristic = TextDirectionHeuristics.FIRSTSTRONG_LTR, + ): BoringLayout.Metrics? { val metrics = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { - BoringLayout.isBoring(text, paint) + // BoringLayout is always laid out left to right + if (textDirection.isRtl(text, 0, text.length)) null + else BoringLayout.isBoring(text, paint) } else { // Default to include fallback line spacing on Android 13+, like TextView // https://cs.android.com/android/_/android/platform/frameworks/base/+/78c774defb238c05c42b34a12b6b3b0c64844ed7 - BoringLayout.isBoring(text, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR, true, null) + BoringLayout.isBoring(text, paint, textDirection, true, null) } // BoringLayout.isBoring() sometimes thinks text width is negative for some strings, even on diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerTextDirectionTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerTextDirectionTest.kt new file mode 100644 index 00000000000..b643f4f3496 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerTextDirectionTest.kt @@ -0,0 +1,85 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.views.text + +import android.text.Layout +import android.text.SpannableString +import android.text.TextPaint +import com.facebook.react.common.mapbuffer.WritableMapBuffer +import com.facebook.yoga.YogaMeasureMode +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import org.robolectric.annotation.GraphicsMode + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +@GraphicsMode(GraphicsMode.Mode.NATIVE) +class TextLayoutManagerTextDirectionTest { + + @Test + fun `isolated text in an rtl layout is laid out right to left`() { + val layout = createLayout("\u2068Anna\u2069 \u2013 \u2068Bob\u2069", "rtl") + + assertThat(layout.getParagraphDirection(0)).isEqualTo(Layout.DIR_RIGHT_TO_LEFT) + } + + @Test + fun `text without strong characters in an rtl layout is laid out right to left`() { + val layout = createLayout("1 \u2013 2", "rtl") + + assertThat(layout.getParagraphDirection(0)).isEqualTo(Layout.DIR_RIGHT_TO_LEFT) + } + + @Test + fun `text starting with a left to right character in an rtl layout is laid out left to right`() { + val layout = createLayout("Anna \u2013 Bob\n1 \u2013 2", "rtl") + + assertThat(layout.getParagraphDirection(0)).isEqualTo(Layout.DIR_LEFT_TO_RIGHT) + assertThat(layout.getParagraphDirection(layout.lineCount - 1)) + .isEqualTo(Layout.DIR_LEFT_TO_RIGHT) + } + + @Test + fun `isolated text in an ltr layout is laid out left to right`() { + val layout = createLayout("\u2068Anna\u2069 \u2013 \u2068Bob\u2069", "ltr") + + assertThat(layout.getParagraphDirection(0)).isEqualTo(Layout.DIR_LEFT_TO_RIGHT) + } + + private fun createLayout(text: String, layoutDirection: String): Layout { + val attributes = + WritableMapBuffer() + .put(TextAttributeProps.TA_KEY_FONT_SIZE, 16.0) + .put(TextAttributeProps.TA_KEY_LAYOUT_DIRECTION, layoutDirection) + val fragment = + WritableMapBuffer() + .put(TextLayoutManager.FR_KEY_STRING, text) + .put(TextLayoutManager.FR_KEY_TEXT_ATTRIBUTES, attributes) + val attributedString = + WritableMapBuffer() + .put(TextLayoutManager.AS_KEY_FRAGMENTS, WritableMapBuffer().put(0, fragment)) + .put(TextLayoutManager.AS_KEY_BASE_ATTRIBUTES, attributes) + + return TextLayoutManager.createLayout( + SpannableString(text), + TextPaint(TextPaint.ANTI_ALIAS_FLAG).apply { textSize = 16f }, + attributedString, + WritableMapBuffer() + .put(TextLayoutManager.PA_KEY_TEXT_BREAK_STRATEGY, "highQuality") + .put(TextLayoutManager.PA_KEY_HYPHENATION_FREQUENCY, "none"), + /* width = */ 400f, + YogaMeasureMode.EXACTLY, + /* height = */ Float.NaN, + YogaMeasureMode.UNDEFINED, + ) + .layout + } +}