mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
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
This commit is contained in:
1 parent
a385cecbea
commit
96518a8fb9
3 files changed
+58
No files matched your search
+4
@@ -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.
|
||||
|
||||
+4
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
+50
@@ -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<Paint>()
|
||||
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<Paint>()
|
||||
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<TextPaint>()
|
||||
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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user