Fix BoringLayout crash when isBoring() returns negative width (#56007)

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

Fix a crash in `TextLayoutManager.isBoring()` where `BoringLayout.isBoring()` sometimes returns metrics with a negative width for certain strings, even on Android 15+. This causes issues downstream when the layout is created.

The fix checks if the returned metrics have a negative width and falls back to `StaticLayout` instead, which (theoretically) handles these edge cases correctly.

Changelog:

[Android][Fixed] - Fix BoringLayout crash when isBoring() returns negative width

Reviewed By: mdvacca

Differential Revision: D95777908

fbshipit-source-id: 82ea7d5186d8d2fe6df7e424c8ec20aaddb72a15
This commit is contained in:
Nick Gerleman authored and meta-codesync[bot] committed 2026-03-09 15:29:34 -07:00
1 parent c3e11cd05c
commit d527f2d663
1 file changed
+18 -8
@@ -1376,14 +1376,24 @@ internal object TextLayoutManager {
return FontMetricsUtil.getFontMetrics(layout.text, layout, context)
}
private fun isBoring(text: Spannable, paint: TextPaint): BoringLayout.Metrics? =
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
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)
}
private fun isBoring(text: Spannable, paint: TextPaint): BoringLayout.Metrics? {
val metrics =
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
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() sometimes thinks text width is negative for some strings, even on
// Android 15+. Fallback to StaticLayout.
if (metrics == null || metrics.width < 0) {
return null
}
return metrics
}
private class CreateLayoutResult(
val layout: Layout,