Keep mixed-direction text on screen with Android 15 start overhang (#59167)

Summary:
Pull Request resolved: https://github.com/react/react-native/pull/59167

On Android 15 and 16.0, `Layout.computeDrawingBoundingBox()` reuses a single `RectF` for every line and `TextLine.metrics()` does not reset it. A line whose paragraph direction is LTR, laid out after an RTL line, inherits the RTL line's bounds in RTL-relative (negative) coordinates. With `setShiftDrawingOffsetForStartOverhang(true)`, `Layout.draw()` then translates the whole text right by about one line width, so the text disappears or only a few glyphs stay visible at the right edge.

Common RTL content hits this: a blank line between Arabic or Hebrew paragraphs has no strong character and is laid out LTR. The platform fixed it in a later Android 16 release by resetting the rect per line.

When an exactly sized layout has mixed paragraph directions, rebuild it without the start-overhang drawing shift. Bounds-based line breaking is kept, so the measured line breaks are unchanged. Single-direction text keeps the existing start-overhang behavior.

Changelog:
[Android][Fixed] - Fix multi-paragraph RTL `Text` with blank or LTR lines being drawn off-screen on Android 15+

Reviewed By: Abbondanzo

Differential Revision: D125908523

fbshipit-source-id: 9955f447b3ab0eda603ce88971d1dab59eddb9d1
This commit is contained in:
Kevin Gozali authored and meta-codesync[bot] committed 2026-10-20 14:44:26 -07:00
1 parent ad2f451919
commit 7b6516eb11
2 files changed
+59 -1

No files matched your search

@@ -913,6 +913,24 @@ internal object TextLayoutManager {
enableStartOverhang,
)
// Android 15/16 leak RTL line bounds into later LTR lines, shifting the whole drawing right.
if (enableStartOverhang && hasMixedParagraphDirections(layout)) {
return buildLayout(
text,
layoutWidth,
includeFontPadding,
textBreakStrategy,
hyphenationFrequency,
alignment,
justificationMode,
ellipsizeMode,
maxNumberOfLines,
paint,
enableStartOverhang,
shiftDrawingOffsetForStartOverhang = false,
)
}
// Layout.draw shifts negative (left-side) overhang, but RTL line starts can overflow to the
// right. Reserve that ink inside an EXACT layout without changing the width reported to Yoga.
return if (enableStartOverhang) {
@@ -950,6 +968,15 @@ internal object TextLayoutManager {
}
}
@VisibleForTesting
internal fun hasMixedParagraphDirections(layout: Layout): Boolean {
if (Build.VERSION.SDK_INT < VERSION_CODE_VANILLA_ICE_CREAM || layout.lineCount == 0) {
return false
}
val firstDirection = layout.getParagraphDirection(0)
return (1 until layout.lineCount).any { layout.getParagraphDirection(it) != firstDirection }
}
@VisibleForTesting
internal fun getRtlRightOverhang(layout: Layout): Int {
if (
@@ -979,6 +1006,7 @@ internal object TextLayoutManager {
maxNumberOfLines: Int,
paint: TextPaint,
enableStartOverhang: Boolean,
shiftDrawingOffsetForStartOverhang: Boolean = enableStartOverhang,
): Layout {
val builder =
StaticLayout.Builder.obtain(text, 0, text.length, paint, layoutWidth)
@@ -1005,7 +1033,7 @@ internal object TextLayoutManager {
// measurement returned to the caller.
if (Build.VERSION.SDK_INT >= VERSION_CODE_VANILLA_ICE_CREAM) {
setUseBoundsForWidth(builder, enableStartOverhang)
setShiftDrawingOffsetForStartOverhang(builder, enableStartOverhang)
setShiftDrawingOffsetForStartOverhang(builder, shiftDrawingOffsetForStartOverhang)
}
return builder.build()
@@ -115,6 +115,36 @@ class TextLayoutManagerStartOverhangTest {
assertThat(layout).isSameAs(initialLayout)
}
@Test
@Config(sdk = [35])
fun `mixed paragraph directions disable the start overhang drawing shift`() {
// The blank line between the RTL paragraphs has no strong character, so it is laid out LTR.
val layout =
createLayout(
YogaMeasureMode.EXACTLY,
text = SpannableString("שלום\n\nעולם"),
maxNumberOfLines = 0,
)
assertThat(TextLayoutManager.hasMixedParagraphDirections(layout)).isTrue()
assertThat(getBooleanLayoutProperty(layout, "getUseBoundsForWidth")).isTrue()
assertThat(getBooleanLayoutProperty(layout, "getShiftDrawingOffsetForStartOverhang")).isFalse()
}
@Test
@Config(sdk = [35])
fun `single paragraph direction keeps the start overhang drawing shift`() {
val layout =
createLayout(
YogaMeasureMode.EXACTLY,
text = SpannableString("שלום\nעולם"),
maxNumberOfLines = 0,
)
assertThat(TextLayoutManager.hasMixedParagraphDirections(layout)).isFalse()
assertThat(getBooleanLayoutProperty(layout, "getShiftDrawingOffsetForStartOverhang")).isTrue()
}
@Test
@Config(sdk = [34])
fun `EXACTLY mode remains supported before Android 15`() {