Skip to content

Commit bb0dcf9

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Fix BoringLayout crash when isBoring() returns negative width
Summary: 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 Differential Revision: D95777908
1 parent 41a1941 commit bb0dcf9

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,14 +1376,24 @@ internal object TextLayoutManager {
13761376
return FontMetricsUtil.getFontMetrics(layout.text, layout, context)
13771377
}
13781378

1379-
private fun isBoring(text: Spannable, paint: TextPaint): BoringLayout.Metrics? =
1380-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
1381-
BoringLayout.isBoring(text, paint)
1382-
} else {
1383-
// Default to include fallback line spacing on Android 13+, like TextView
1384-
// https://cs.android.com/android/_/android/platform/frameworks/base/+/78c774defb238c05c42b34a12b6b3b0c64844ed7
1385-
BoringLayout.isBoring(text, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR, true, null)
1386-
}
1379+
private fun isBoring(text: Spannable, paint: TextPaint): BoringLayout.Metrics? {
1380+
val metrics =
1381+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
1382+
BoringLayout.isBoring(text, paint)
1383+
} else {
1384+
// Default to include fallback line spacing on Android 13+, like TextView
1385+
// https://cs.android.com/android/_/android/platform/frameworks/base/+/78c774defb238c05c42b34a12b6b3b0c64844ed7
1386+
BoringLayout.isBoring(text, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR, true, null)
1387+
}
1388+
1389+
// BoringLayout.isBoring() sometimes thinks text width is negative for some strings, even on
1390+
// Android 15+. Fallback to StaticLayout.
1391+
if (metrics == null || metrics.width < 0) {
1392+
return null
1393+
}
1394+
1395+
return metrics
1396+
}
13871397

13881398
private class CreateLayoutResult(
13891399
val layout: Layout,

0 commit comments

Comments
 (0)