Skip to content

Commit 4d8860d

Browse files
committed
fix(android): measure visual width for boring text
1 parent c6bfe78 commit 4d8860d

2 files changed

Lines changed: 49 additions & 30 deletions

File tree

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

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -864,38 +864,17 @@ internal object TextLayoutManager {
864864
maxNumberOfLines: Int,
865865
paint: TextPaint,
866866
): Layout {
867-
// If our text is boring, and fully fits in the available space, we can represent the text
868-
// layout as a BoringLayout
869-
if (
870-
boring != null &&
871-
(widthYogaMeasureMode == YogaMeasureMode.UNDEFINED || boring.width <= floor(width))
872-
) {
873-
// Guard uses floor() but layout width below uses ceil() for EXACTLY mode intentionally:
874-
// text that barely fails the floor-based guard falls through to StaticLayout, which also
875-
// ceils for EXACTLY — no wrapping results, just a slightly less optimal layout class in a
876-
// rare subpixel edge case.
877-
val layoutWidth =
878-
if (widthYogaMeasureMode == YogaMeasureMode.EXACTLY) ceil(width).toInt() else boring.width
879-
return BoringLayout.make(
880-
text,
881-
paint,
882-
layoutWidth,
883-
alignment,
884-
1f,
885-
0f,
886-
boring,
887-
includeFontPadding,
888-
)
889-
}
890-
891867
val layoutWidth =
892868
when {
893869
widthYogaMeasureMode == YogaMeasureMode.EXACTLY -> ceil(width).toInt()
894-
widthYogaMeasureMode == YogaMeasureMode.AT_MOST && boring != null -> floor(width).toInt()
870+
widthYogaMeasureMode == YogaMeasureMode.AT_MOST &&
871+
boring != null &&
872+
boring.width > floor(width) -> floor(width).toInt()
895873
else -> {
896874
val desiredWidth =
897875
getDesiredWidth(
898876
text,
877+
boring,
899878
includeFontPadding,
900879
textBreakStrategy,
901880
hyphenationFrequency,
@@ -912,6 +891,22 @@ internal object TextLayoutManager {
912891
}
913892
}
914893
}
894+
if (
895+
boring != null &&
896+
(widthYogaMeasureMode == YogaMeasureMode.UNDEFINED || boring.width <= floor(width))
897+
) {
898+
return BoringLayout.make(
899+
text,
900+
paint,
901+
layoutWidth,
902+
alignment,
903+
1f,
904+
0f,
905+
boring,
906+
includeFontPadding,
907+
)
908+
}
909+
915910
// Bounds-based width is limited to text with RTL characters. An EXACTLY width usually comes
916911
// from an earlier advance-based AT_MOST measurement, and LTR glyph bounds that are wider than
917912
// their advances (some OEM fonts) would make a single line overflow that width and ellipsize.
@@ -1022,6 +1017,7 @@ internal object TextLayoutManager {
10221017

10231018
private fun getDesiredWidth(
10241019
text: Spannable,
1020+
boring: BoringLayout.Metrics?,
10251021
includeFontPadding: Boolean,
10261022
textBreakStrategy: Int,
10271023
hyphenationFrequency: Int,
@@ -1031,11 +1027,10 @@ internal object TextLayoutManager {
10311027
justificationMode: Int,
10321028
paint: TextPaint,
10331029
): Int {
1034-
val advanceWidth = ceil(Layout.getDesiredWidth(text, paint)).toInt()
1030+
val advanceWidth = boring?.width ?: ceil(Layout.getDesiredWidth(text, paint)).toInt()
10351031

10361032
if (
1037-
Build.VERSION.SDK_INT < VERSION_CODE_VANILLA_ICE_CREAM ||
1038-
setUseBoundsForWidthMethod == null
1033+
Build.VERSION.SDK_INT < VERSION_CODE_VANILLA_ICE_CREAM || setUseBoundsForWidthMethod == null
10391034
) {
10401035
return advanceWidth
10411036
}

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerAbsoluteLayoutWithFractionalPixelTest.kt‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import kotlin.math.floor
1919
import org.assertj.core.api.Assertions.assertThat
2020
import org.junit.Test
2121
import org.junit.runner.RunWith
22-
import org.robolectric.annotation.Config
2322
import org.robolectric.RobolectricTestRunner
23+
import org.robolectric.annotation.Config
2424

2525
/**
2626
* Regression test for the "Keep watching" 1px text-wrap bug.
@@ -115,6 +115,29 @@ class TextLayoutManagerAbsoluteLayoutWithFractionalPixelTest {
115115
assertThat(layout.width).isGreaterThanOrEqualTo(advanceWidth)
116116
}
117117

118+
@Test
119+
@Config(sdk = [35])
120+
fun `Android 15 UNDEFINED boring layout uses the visual probe width`() {
121+
val text = SpannableString("Prison Break")
122+
val paint = TextPaint(TextPaint.ANTI_ALIAS_FLAG).apply { textSize = 16f }
123+
val boring = requireNotNull(BoringLayout.isBoring(text, paint)).apply { width = 1 }
124+
val visualProbeWidth = ceil(Layout.getDesiredWidth(text, paint)).toInt()
125+
126+
assertThat(visualProbeWidth).isGreaterThan(boring.width)
127+
128+
val layout =
129+
invokeCreateLayout(
130+
text,
131+
0f,
132+
paint,
133+
YogaMeasureMode.UNDEFINED,
134+
boring,
135+
)
136+
137+
assertThat(layout).isInstanceOf(BoringLayout::class.java)
138+
assertThat(layout.width).isGreaterThanOrEqualTo(visualProbeWidth)
139+
}
140+
118141
/**
119142
* Default values mirror what measureText() passes in the production path for a plain
120143
* single-paragraph label.
@@ -128,10 +151,11 @@ class TextLayoutManagerAbsoluteLayoutWithFractionalPixelTest {
128151
width: Float,
129152
paint: TextPaint,
130153
widthYogaMeasureMode: YogaMeasureMode = YogaMeasureMode.EXACTLY,
154+
boring: BoringLayout.Metrics? = BoringLayout.isBoring(text, paint),
131155
): Layout =
132156
TextLayoutManager.createLayout(
133157
text,
134-
BoringLayout.isBoring(text, paint),
158+
boring,
135159
width,
136160
widthYogaMeasureMode,
137161
/* includeFontPadding = */ false,

0 commit comments

Comments
 (0)