From 96518a8fb9b380422ff428e44e7602977a8ec1dd Mon Sep 17 00:00:00 2001 From: Jakub Adamczyk Date: Mon, 7 Sep 2026 08:23:13 -0700 Subject: [PATCH] Fix Android text mis-measurement when using fontVariationSettings (#58320) Summary: `fontVariationSettings` (https://github.com/react/react-native/issues/57804, https://github.com/react/react-native/issues/57805) is applied with `Paint.setFontVariationSettings` after `Paint.setTypeface`. Android's `Paint.setFontVariationSettings` returns early when the requested settings string equals the value the paint already holds, and `Paint.setTypeface` does not clear that stored string. On the legacy variation path, the variation instance lives on the typeface, so once `setTypeface` replaces the typeface the axes are gone, but the paint still reports the old settings and the second `setFontVariationSettings` call is a no-op. With a variable font whose default instance is lighter than the requested `wght`, Yoga receives regular-width metrics while semibold glyphs are drawn, so `numberOfLines={1}` text is ellipsized ("Get Start...") and wrapped text breaks in different places than it renders. This change clears the paint's settings before re-applying them in `ReactTypefaceUtils.applyFontVariationSettings` and in `ReactEditText.maybeUpdateTypeface`, so the axes are always rebuilt on the current typeface. Paints without previous settings are unaffected. ## Changelog: [ANDROID] [FIXED] - Fix text measured with the wrong width when `fontVariationSettings` are re-applied to a reused `Paint` Pull Request resolved: https://github.com/react/react-native/pull/58320 Test Plan: Ran test suite, validated the fix in my patched react-native instance that backports variable fonts support. Reviewed By: andrewdacenko Differential Revision: D118791592 Pulled By: javache fbshipit-source-id: 937e6bb2ceed937679abfb6ca192a74fd385b3eb --- .../react/views/text/ReactTypefaceUtils.kt | 4 ++ .../react/views/textinput/ReactEditText.kt | 4 ++ ...xtLayoutManagerFontWeightAdjustmentTest.kt | 50 +++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTypefaceUtils.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTypefaceUtils.kt index 154062b966d..04e3f06dd52 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTypefaceUtils.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTypefaceUtils.kt @@ -179,6 +179,10 @@ public object ReactTypefaceUtils { } try { + // Paint skips unchanged settings even after setTypeface, so clear them first. + if (paint.fontVariationSettings != null && fontVariationSettings != null) { + paint.setFontVariationSettings(null) + } paint.setFontVariationSettings(fontVariationSettings) } catch (exception: IllegalArgumentException) { // Paint instances are reused, so explicitly clear axes from a previous layout. diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt index e27330c8714..4c4bb253d86 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt @@ -689,6 +689,10 @@ public open class ReactEditText public constructor(context: Context) : AppCompat val newTypeface = applyStyles(typeface, fontStyle, fontWeight, fontFamily, context.assets) typeface = newTypeface if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // TextView skips unchanged settings even after a typeface change, so clear them first. + if (getFontVariationSettings() != null && parsedFontVariationSettings != null) { + super.setFontVariationSettings(null) + } super.setFontVariationSettings(parsedFontVariationSettings) } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerFontWeightAdjustmentTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerFontWeightAdjustmentTest.kt index 9b7454b1e0b..b2df8cfd4c0 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerFontWeightAdjustmentTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerFontWeightAdjustmentTest.kt @@ -24,6 +24,7 @@ import org.junit.runner.RunWith import org.mockito.kotlin.any import org.mockito.kotlin.inOrder import org.mockito.kotlin.mock +import org.mockito.kotlin.never import org.mockito.kotlin.verify import org.mockito.kotlin.whenever import org.robolectric.RobolectricTestRunner @@ -127,6 +128,55 @@ class TextLayoutManagerFontWeightAdjustmentTest { verify(paint).setFontVariationSettings(null) } + @Test + fun `reused paint re-applies unchanged font variation settings to a new typeface`() { + val paint = mock() + whenever(paint.fontVariationSettings).thenReturn("'wght' 600") + + ReactTypefaceUtils.applyFontVariationSettings(paint, "'wght' 600") + + inOrder(paint) { + verify(paint).setFontVariationSettings(null) + verify(paint).setFontVariationSettings("'wght' 600") + } + } + + @Test + fun `fresh paint applies font variation settings without clearing first`() { + val paint = mock() + whenever(paint.fontVariationSettings).thenReturn(null) + + ReactTypefaceUtils.applyFontVariationSettings(paint, "'wght' 600") + + verify(paint).setFontVariationSettings("'wght' 600") + verify(paint, never()).setFontVariationSettings(null) + } + + @Test + fun `scratch text paint re-applies font variation settings on repeated measurement`() { + val paint = mock() + whenever(paint.fontVariationSettings).thenReturn("'wght' 600") + val textAttributes = + TextAttributeProps.fromReadableMap( + ReactStylesDiffMap( + JavaOnlyMap.of("fontWeight", "600", "fontVariationSettings", "'wght' 600"), + ), + ) + + TextLayoutManager.updateTextPaint( + paint, + textAttributes, + RuntimeEnvironment.getApplication().assets, + 0, + ) + + inOrder(paint) { + verify(paint).setTypeface(any()) + verify(paint).setFontVariationSettings(null) + verify(paint).setFontVariationSettings("'wght' 600") + } + } + private companion object { const val FONT_WEIGHT_ADJUSTMENT_BOLD_TEXT = 300 }