Repository navigation
[Android] Fix adjustsFontSizeToFit shrinking text that fits and resizing nested font sizes - #60135
Open
kayahickindev wants to merge 1 commit into
Open
kayahickindev wants to merge 1 commit into
kayahickindev wants to merge 1 commit into
Conversation
…ing 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 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
With
adjustsFontSizeToFit, Android changes font sizes it should leave alone:Textwith a smallerfontSizechanges size even when nothing needs to shrink. With a 40px "Total " and a 10px "USD" that already fit, "USD" comes back at 8px. WithminimumFontScale={0.5}it comes back at 24px.TextLayoutManager.adjustSpannableFontToFitbinary-searches the largest font size. On each probe it multiplies the paint and everyReactAbsoluteSizeSpanbycurrentFontSize / previousFontSizeand truncates withtoInt(). The sizes it multiplies were already truncated by the previous probe, so the error compounds. In 32-bit floats22 * (13f / 22f)is 12.999999, which truncates to 12, and the search never gets the lost pixel back, even when it ends at the original size. Every span is also clamped to the minimum font size on each probe, so a nested 10px run is raised to the 20px minimum on a probe below the original size, and the later probes on the way back up scale it from there to 24px.The fix keeps the original span sizes and paint text size, and on each probe computes every size from its original:
The shrink rule matches iOS. In
packages/react-native/Libraries/Text/Text/NSTextStorage+FontScaling.m,scaleFontSizeToFitSize:minimumFontSize:maximumFontSize:returns without scaling when the text already fits ("Don't scale the font if it already fits"). Otherwise it restores the original string before each try, andscaleFontSizeWithRatio:minimumFontSize:maximumFontSize:sets every run toMAX(MIN(font.pointSize * ratio, maximumFontSize), minimumFontSize). So a 40px and 10px pair shrunk to a 0.5minimumFontScaleends at 20px and 20px on both platforms.#56815 fixes the single-size case by returning early when the text fits at its original size. This change removes the truncation instead, which also covers text that has to shrink and mixed font sizes, without an extra layout. The two touch the same lines in
adjustSpannableFontToFit.This should fix #56799. I reproduced the shrinking in a unit test, but did not run that issue's reproducer on a device.
Minimal repro:
Changelog:
[ANDROID] [FIXED] - Fix adjustsFontSizeToFit shrinking Text that already fits and changing the size of nested Text
Test Plan:
Added five tests to
TextLayoutManagerMinimumFontScaleTest(Robolectric):text that already fits keeps its font size at every size: a single span at each size from 8 to 120px, in a box it fits.smaller font sizes are kept when the text already fits: 40px and 10px spans that fit.minimumFontScale does not grow smaller font sizes: the same withminimumFontScale0.5.smaller font sizes shrink in proportion to the largest one: 40px and 20px spans in the height of the same text at 20px and 10px, withminimumFontScale0.1, using native graphics so the height follows the font metrics. Expects 20px and 10px.shrunk font sizes are clamped to the minimum font size: 40px and 10px spans in a height they cannot fit, withminimumFontScale0.5. Expects 20px and 20px, as on iOS. This one passes before and after the fix, and guards the clamp.Before the fix (the
TextLayoutManager.ktchange reverted, tests kept), four tests fail and the other nine pass:After the fix, the whole package:
BUILD SUCCESSFUL: 18 suites, 129 tests, 0 failures, including the existingminimumFontScale,minimumFontSizeand 4dp floor tests andTextLayoutManagerFontSizeToFitPixelHeightTest. Onmainat f4f8b6a the same command runs 18 suites, 124 tests, 0 failures.node ./scripts/format-kotlin.js --check(whatyarn format-check-kotlinruns) passes, andcompileDebugKotlinandcompileDebugUnitTestKotlinpass with-PenableWarningsAsErrors=true.Not run: a device or emulator, iOS, and the #56799 reproducer. The tests above are Robolectric unit tests at density 1 only.
🤖 Generated with Claude Code