From a41a8147dfbd562f757e60e261e99dedb67a64c0 Mon Sep 17 00:00:00 2001 From: kayahickindev <192762269+kayahickindev@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:30:19 -0500 Subject: [PATCH] [Android] Fix adjustsFontSizeToFit shrinking text that fits and resizing nested font sizes adjustSpannableFontToFit rescaled every font size span by the ratio between the current and the previous probe, truncating to whole pixels each time. The truncation compounds across probes, so text that already fit could come back smaller: a single 22px span ended at 17px, and 30 of the sizes from 8 to 120px shrank. Nested font sizes drifted too: a 10px run next to a 40px run that already fit came back as 8px, and with minimumFontScale={0.5} the clamp to the minimum on the way down left it at 24px. Scale every span and the paint from its original size on each probe. Text that fits ends on the original size and keeps every size exactly. Text that has to shrink scales every size by the same ratio, rounds it, and clamps it to the minimum font size, as scaleFontSizeWithRatio does on iOS and as Android did before. Changelog: [ANDROID] [FIXED] - Fix adjustsFontSizeToFit shrinking Text that already fits and changing the size of nested Text Co-Authored-By: Claude Opus 5.5 --- .../react/views/text/TextLayoutManager.kt | 32 +++-- .../TextLayoutManagerMinimumFontScaleTest.kt | 109 ++++++++++++++++++ 2 files changed, 132 insertions(+), 9 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt index ce31988c6cbe..ca1c39387958 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt @@ -69,6 +69,7 @@ import kotlin.math.ceil import kotlin.math.floor import kotlin.math.max import kotlin.math.min +import kotlin.math.round import org.json.JSONArray import org.json.JSONObject @@ -1538,9 +1539,15 @@ internal object TextLayoutManager { } currentFontSize = max(currentFontSize, minimumFontSize) + // Every font size is scaled from its original size, so that rounding does not compound across + // iterations. Text that fits keeps its original sizes. Shrunk text scales every size by one + // ratio and clamps it to the minimum, matching scaleFontSizeWithRatio on iOS. + val originalFontSize = currentFontSize + val originalSpanSizes = IntArray(spans.size) { spans[it].size } + val originalPaintTextSize = paint.textSize + var intervalStart = minimumFontSize var intervalEnd = currentFontSize - var previousFontSize = currentFontSize // `true` instead of `intervalStart != intervalEnd` so that the last iteration where both are at // the same size goes through and updates all relevant objects with the final font size @@ -1551,18 +1558,27 @@ internal object TextLayoutManager { // we always end up doing two measurements to check whether intervalEnd would fit. val currentFontSize = (intervalStart + intervalEnd + 1) / 2 - val ratio = currentFontSize.toFloat() / previousFontSize.toFloat() - paint.textSize = max((paint.textSize * ratio).toInt(), minimumFontSize).toFloat() - - val sizeSpans = text.getSpans(0, text.length, ReactAbsoluteSizeSpan::class.java) - for (span in sizeSpans) { + val isShrunk = currentFontSize < originalFontSize + val ratio = currentFontSize.toFloat() / originalFontSize.toFloat() + paint.textSize = + if (isShrunk) max(round(originalPaintTextSize * ratio), minimumFontSize.toFloat()) + else originalPaintTextSize + + for (i in spans.indices) { + val span = spans[i] + val resizedSpan = + ReactAbsoluteSizeSpan( + if (isShrunk) max(round(originalSpanSizes[i] * ratio).toInt(), minimumFontSize) + else originalSpanSizes[i] + ) text.setSpan( - ReactAbsoluteSizeSpan(max((span.size * ratio).toInt(), minimumFontSize)), + resizedSpan, text.getSpanStart(span), text.getSpanEnd(span), text.getSpanFlags(span), ) text.removeSpan(span) + spans[i] = resizedSpan } if (boring != null) { boring = isBoring(text, paint) @@ -1611,8 +1627,6 @@ internal object TextLayoutManager { // Text fits the constraints intervalStart = currentFontSize } - - previousFontSize = currentFontSize } } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerMinimumFontScaleTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerMinimumFontScaleTest.kt index 5bc7fab45527..4d9e485f9905 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerMinimumFontScaleTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerMinimumFontScaleTest.kt @@ -12,6 +12,7 @@ import android.graphics.text.LineBreakConfig import android.text.Layout import android.text.SpannableString import android.text.Spanned +import android.text.StaticLayout import android.text.TextPaint import com.facebook.react.common.ReactConstants import com.facebook.react.uimanager.DisplayMetricsHolder @@ -25,6 +26,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.GraphicsMode @RunWith(RobolectricTestRunner::class) @SuppressLint("InlinedApi") @@ -136,6 +138,60 @@ class TextLayoutManagerMinimumFontScaleTest { assertThat(largestFontSize(text)).isEqualTo(LARGE_FONT_SIZE) } + @Test + fun `text that already fits keeps its font size at every size`() { + val shrunkFontSizes = + (8..120).filter { fontSize -> + val text = spannableWithFontSize(fontSize) + adjustToFit(text, height = 10_000f, minimumFontScale = Float.NaN) + largestFontSize(text) != fontSize + } + + assertThat(shrunkFontSizes).isEmpty() + } + + @Test + fun `smaller font sizes are kept when the text already fits`() { + val text = spannableWithSmallAndLargeFontSizes() + + adjustToFit(text, height = 10_000f, minimumFontScale = Float.NaN) + + assertThat(fontSizes(text)).containsExactly(LARGE_FONT_SIZE, SMALL_FONT_SIZE) + } + + @Test + fun `minimumFontScale does not grow smaller font sizes`() { + val text = spannableWithSmallAndLargeFontSizes() + + adjustToFit(text, height = 10_000f, minimumFontScale = 0.5f) + + assertThat(fontSizes(text)).containsExactly(LARGE_FONT_SIZE, SMALL_FONT_SIZE) + } + + @Test + @GraphicsMode(GraphicsMode.Mode.NATIVE) // Real font metrics, so the height depends on font size. + fun `smaller font sizes shrink in proportion to the largest one`() { + val text = spannableWithSmallAndLargeFontSizes(smallFontSize = 20) + // The height of the text at half its font sizes, with a minimum well below that. + val height = + layoutHeight(spannableWithSmallAndLargeFontSizes(largeFontSize = 20, smallFontSize = 10)) + + adjustToFit(text, height = height, minimumFontScale = 0.1f) + + assertThat(fontSizes(text)).containsExactly(20, 10) + } + + @Test + fun `shrunk font sizes are clamped to the minimum font size`() { + val text = spannableWithSmallAndLargeFontSizes() + + adjustToUnsatisfiableHeight(text, minimumFontScale = 0.5f) + + // Like iOS, every font size is clamped to the minimum derived from the largest one. + val minimumFontSize = (LARGE_FONT_SIZE * 0.5f).toInt() + assertThat(fontSizes(text)).containsExactly(minimumFontSize, minimumFontSize) + } + // Uses a height no font size can satisfy so the text is shrunk all the way to the minimum. private fun adjustToUnsatisfiableHeight( text: SpannableString, @@ -162,6 +218,59 @@ class TextLayoutManagerMinimumFontScaleTest { ) } + private fun adjustToFit(text: SpannableString, height: Float, minimumFontScale: Float) { + TextLayoutManager.adjustSpannableFontToFit( + text, + 10_000f, + YogaMeasureMode.EXACTLY, + height, + YogaMeasureMode.EXACTLY, + Float.NaN, + minimumFontScale, + ReactConstants.UNSET, + true, + Layout.BREAK_STRATEGY_SIMPLE, + Layout.HYPHENATION_FREQUENCY_NONE, + /* lineBreakStyle = */ LineBreakConfig.LINE_BREAK_STYLE_NONE, + /* lineBreakWordStyle = */ LineBreakConfig.LINE_BREAK_WORD_STYLE_NONE, + Layout.Alignment.ALIGN_NORMAL, + 0, + newPaint(), + ) + } + + // "Total " at the large font size followed by "USD" at the small one. + private fun spannableWithSmallAndLargeFontSizes( + largeFontSize: Int = LARGE_FONT_SIZE, + smallFontSize: Int = SMALL_FONT_SIZE, + ): SpannableString { + val text = SpannableString("Total USD") + text.setSpan(ReactAbsoluteSizeSpan(largeFontSize), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + text.setSpan( + ReactAbsoluteSizeSpan(smallFontSize), + 6, + text.length, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE, + ) + return text + } + + private fun layoutHeight(text: SpannableString): Float { + val paint = newPaint().apply { textSize = largestFontSize(text).toFloat() } + return StaticLayout.Builder.obtain(text, 0, text.length, paint, 10_000) + .setIncludePad(true) + .build() + .height + .toFloat() + } + + // Font sizes in text order. + private fun fontSizes(text: Spanned): List = + text + .getSpans(0, text.length, ReactAbsoluteSizeSpan::class.java) + .sortedBy { text.getSpanStart(it) } + .map { it.size } + private fun spannableWithFontSize(fontSize: Int): SpannableString { val text = SpannableString("Hello") text.setSpan(ReactAbsoluteSizeSpan(fontSize), 0, text.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)