Skip to content

Commit 84a3701

Browse files
Fix text wrapping in absolutely positioned elements on Android (#56651)
Summary: ## Context On Android TV, text inside absolutely-positioned Views wraps unexpectedly when the computed width from Yoga is fractional. This manifests in Instagram TV as "Keep watching" breaking to two lines in the exit dialog when the button text is focused (rendered via an absolutely-positioned overlay for color animation). The root cause is a rounding mismatch in React Native's Android `TextLayoutManager`: `desiredWidth` (the text's needed width) uses `ceil()` (rounds up), but `layoutWidth` in `EXACTLY` mode uses `floor()` (rounds down). When Yoga passes a fractional width (e.g. 258.5px), the container gets `floor(258.5) = 258px` but the text needs `ceil(258.3) = 259px`, causing a 1px shortfall that triggers wrapping. ## Fix In `TextLayoutManager.createLayout()`, change `floor(width).toInt()` to `ceil(width).toInt()` for `YogaMeasureMode.EXACTLY` in **both** layout paths so the behavior is consistent regardless of which `Layout` class is chosen: - BoringLayout (single-line text that fits) - StaticLayout (multi-line or complex text) `YogaMeasureMode.AT_MOST` is intentionally left as `floor(width).toInt()`. `AT_MOST` is a constraint contract from Yoga ("do not exceed this width"), so flooring remains the correct conservative behavior — ceiling there could violate the constraint by up to 1px. The BoringLayout entry guard (`boring.width <= floor(width)`) is also left unchanged. If a boring text fails the guard, it falls through to the StaticLayout path, which now also ceils for `EXACTLY`, so no truncation results — the only effect is a slightly less optimal layout class choice in a narrow edge case. ## Why ceiling `EXACTLY` is safe `EXACTLY` means Yoga has guaranteed this width — the container has been allocated at least the full fractional width upstream. Ceiling the local layout width by 1px cannot exceed what Yoga has reserved, while flooring it produced a 1px shortfall that mismatched `desiredWidth`'s ceiling and triggered wrapping. The compensating mechanism in `calculateWidth()` — which returns the raw fractional width to Yoga for `EXACTLY` mode rather than the floored `layout.width` — is preserved, so Yoga's upstream allocation reasoning is unchanged. This was the subpixel-safety property introduced in D74685353; only the local pixel rounding inside `createLayout` changes from "round down 1px and risk wrapping" to "round up 1px and match `desiredWidth`". ## Testing Added a new test case to cover this behavior. If this needs to be adjusted in the future, a failure will highlight this bug and ensure it's covered or mitigated with additional test cases. ## Notes This was confirmed as a problem with an absolutely positioned style with `left:0, right:0` applied. Width sizing was confirmed to be the issue when `left:-1, right:-1` resolved the issue. Further investigation found this fix in text sizing. Only `EXACTLY` is needed to fix the observed Instagram TV bug. `AT_MOST` is left untouched because the constraint semantics differ. ## Changelog: [Android] [Fixed] - Fix 1px text wrapping in absolutely positioned elements caused by fractional Yoga widths Differential Revision: D102920508
1 parent 32d3eaf commit 84a3701

2 files changed

Lines changed: 132 additions & 3 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,12 @@ internal object TextLayoutManager {
618618
boring != null &&
619619
(widthYogaMeasureMode == YogaMeasureMode.UNDEFINED || boring.width <= floor(width))
620620
) {
621+
// Guard uses floor() but layout width below uses ceil() for EXACTLY mode intentionally:
622+
// text that barely fails the floor-based guard falls through to StaticLayout, which also
623+
// ceils for EXACTLY — no wrapping results, just a slightly less optimal layout class in a
624+
// rare subpixel edge case.
621625
val layoutWidth =
622-
if (widthYogaMeasureMode == YogaMeasureMode.EXACTLY) floor(width).toInt()
623-
else boring.width
626+
if (widthYogaMeasureMode == YogaMeasureMode.EXACTLY) ceil(width).toInt() else boring.width
624627
return BoringLayout.make(
625628
text,
626629
paint,
@@ -637,7 +640,7 @@ internal object TextLayoutManager {
637640

638641
val layoutWidth =
639642
when (widthYogaMeasureMode) {
640-
YogaMeasureMode.EXACTLY -> floor(width).toInt()
643+
YogaMeasureMode.EXACTLY -> ceil(width).toInt()
641644
YogaMeasureMode.AT_MOST -> min(desiredWidth, floor(width).toInt())
642645
else -> desiredWidth
643646
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.views.text
9+
10+
import android.text.BoringLayout
11+
import android.text.Layout
12+
import android.text.SpannableString
13+
import android.text.TextPaint
14+
import android.text.TextUtils
15+
import com.facebook.yoga.YogaMeasureMode
16+
import kotlin.math.ceil
17+
import org.assertj.core.api.Assertions.assertThat
18+
import org.junit.Test
19+
import org.junit.runner.RunWith
20+
import org.robolectric.RobolectricTestRunner
21+
22+
/**
23+
* Regression test for the "Keep watching" 1px text-wrap bug.
24+
*
25+
* Scenario: A dialog renders a primary button label inside an absolutely-positioned
26+
* focused/unfocused overlay (left:0/right:0). Yoga hands the inner Text a fractional EXACTLY width
27+
* (e.g. 258.5px on a 517px parent). Pre-fix, TextLayoutManager.createLayout floored the EXACTLY
28+
* width to 258 while the text the layout actually renders needed 259px, leaving the layout 1px
29+
* narrower than its own content — on a real device that forces the text to wrap to a second line
30+
* (taller label height). The user-visible symptom is "Keep watching" rendering on two lines instead
31+
* of one inside the dialog.
32+
*
33+
* Direct assertion: the layout.width returned by createLayout (the horizontal box StaticLayout was
34+
* told it has) must be >= layout.getLineWidth(0) (the horizontal advance of the rendered line).
35+
* When that invariant is violated, the rendered text doesn't fit in the layout's allocated space
36+
* and the next layout pass wraps it. Robolectric stubs real font metrics (1px/char), but the
37+
* relationship between allocated width and rendered line width is faithful enough to expose the
38+
* floor/ceil bug.
39+
*/
40+
@RunWith(RobolectricTestRunner::class)
41+
class TextLayoutManagerKeepWatchingTest {
42+
43+
@Test
44+
fun `EXACTLY mode with fractional width allocates a layout wide enough to fit its own text`() {
45+
val text = SpannableString("Keep watching")
46+
val paint = TextPaint(TextPaint.ANTI_ALIAS_FLAG).apply { textSize = 16f }
47+
48+
val desiredWidth = Layout.getDesiredWidth(text, paint)
49+
val ceilDesired = ceil(desiredWidth).toInt()
50+
51+
// Construct the exact repro: a fractional EXACTLY width whose ceil equals
52+
// ceil(desiredWidth) but whose floor is one pixel short. Yoga has reserved the full
53+
// fractional width upstream, so flooring shaves 1px and triggers the wrap bug.
54+
val fractionalWidth = ceilDesired - 0.5f
55+
56+
val layout = invokeCreateLayout(text, fractionalWidth, paint)
57+
val renderedLineWidth = layout.getLineWidth(0)
58+
59+
assertThat(layout.width.toFloat())
60+
.withFailMessage(
61+
"Layout's allocated width (%d) is smaller than the rendered text it contains " +
62+
"(line 0 width=%.2f) for '%s' at EXACTLY width=%.2f " +
63+
"(layoutClass=%s, desiredWidth=%.2f, ceilDesired=%d). " +
64+
"On a real Android device this 1px shortfall forces the label to wrap to a " +
65+
"second line, doubling its height — the visible \"Keep watching\" bug in a " +
66+
"dialog. Root cause: TextLayoutManager.createLayout used floor(width) for " +
67+
"EXACTLY mode, producing a layout narrower than its own text.",
68+
layout.width,
69+
renderedLineWidth,
70+
text.toString(),
71+
fractionalWidth,
72+
layout::class.java.simpleName,
73+
desiredWidth,
74+
ceilDesired,
75+
)
76+
.isGreaterThanOrEqualTo(renderedLineWidth)
77+
}
78+
79+
/**
80+
* Invokes the private TextLayoutManager.createLayout via reflection. We can't call it directly
81+
* because it's `private` (friend_paths only opens up `internal`). Default values mirror what
82+
* measureText() passes in the production path for a plain single-paragraph label.
83+
*/
84+
private fun invokeCreateLayout(
85+
text: SpannableString,
86+
width: Float,
87+
paint: TextPaint,
88+
): Layout {
89+
val boring: BoringLayout.Metrics? = BoringLayout.isBoring(text, paint)
90+
val method =
91+
TextLayoutManager::class
92+
.java
93+
.getDeclaredMethod(
94+
"createLayout",
95+
android.text.Spannable::class.java,
96+
BoringLayout.Metrics::class.java,
97+
java.lang.Float.TYPE,
98+
YogaMeasureMode::class.java,
99+
java.lang.Boolean.TYPE,
100+
java.lang.Integer.TYPE,
101+
java.lang.Integer.TYPE,
102+
Layout.Alignment::class.java,
103+
java.lang.Integer.TYPE,
104+
TextUtils.TruncateAt::class.java,
105+
java.lang.Integer.TYPE,
106+
TextPaint::class.java,
107+
)
108+
.apply { isAccessible = true }
109+
110+
return method.invoke(
111+
TextLayoutManager,
112+
text,
113+
boring,
114+
width,
115+
YogaMeasureMode.EXACTLY,
116+
/* includeFontPadding = */ false,
117+
/* textBreakStrategy = */ Layout.BREAK_STRATEGY_HIGH_QUALITY,
118+
/* hyphenationFrequency = */ Layout.HYPHENATION_FREQUENCY_NONE,
119+
Layout.Alignment.ALIGN_NORMAL,
120+
/* justificationMode = */ 0,
121+
/* ellipsizeMode = */ null,
122+
/* maxNumberOfLines = */ 2,
123+
paint,
124+
) as Layout
125+
}
126+
}

0 commit comments

Comments
 (0)