From 551d12a787f925aa0b9c7d7fa9a5703b28a64cbc Mon Sep 17 00:00:00 2001 From: Andrew Ghostuhin Date: Thu, 13 Aug 2026 04:51:25 -0700 Subject: [PATCH] Fix Android inline Text views with small font scale Summary: Fixes https://github.com/facebook/react-native/issues/50916. Closes torin-asakura/workspace#126. Android inline views inside `` receive their measured layout size from Fabric as layout units/DIP. TextLayoutManager was converting those attachment dimensions with `PixelUtil.toPixelFromSP(...)`, so a device font scale below 1.0 shrank the inline view placeholder width. In the https://github.com/facebook/react-native/issues/50916 RNTester reproducer, a measured `Row cutoff` attachment around 155px became about 132px at `font_scale=0.85`, clipping the rendered text to `Row`. This changes inline text attachment dimensions to use DIP conversion and rounds them up to the pixel grid in both spannable construction paths. ## Changelog: [ANDROID] [FIXED] - Keep inline views inside Text from shrinking with Android system font scale X-link: https://github.com/facebook/react-native/pull/57132 Reviewed By: christophpurrer Differential Revision: D108030457 Pulled By: cipolleschi fbshipit-source-id: af68489aed0ee9c39f3a440f07ca9286ff5ed30c --- .../react/views/text/TextLayoutManager.kt | 16 ++++-- .../TextLayoutManagerInlineViewSizeTest.kt | 51 +++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerInlineViewSizeTest.kt 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 5e0f6575c6d..66e6f24fbb0 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 @@ -266,13 +266,15 @@ internal object TextLayoutManager { val reactTag = if (fragment.contains(FR_KEY_REACT_TAG)) fragment.getInt(FR_KEY_REACT_TAG) else View.NO_ID if (fragment.contains(FR_KEY_IS_ATTACHMENT) && fragment.getBoolean(FR_KEY_IS_ATTACHMENT)) { - val width = PixelUtil.toPixelFromSP(fragment.getDouble(FR_KEY_WIDTH)) - val height = PixelUtil.toPixelFromSP(fragment.getDouble(FR_KEY_HEIGHT)) ops.add( SetSpanOperation( sb.length - 1, sb.length, - TextInlineViewPlaceholderSpan(reactTag, width.toInt(), height.toInt()), + TextInlineViewPlaceholderSpan( + reactTag, + inlineViewSizeToPixels(fragment.getDouble(FR_KEY_WIDTH)), + inlineViewSizeToPixels(fragment.getDouble(FR_KEY_HEIGHT)), + ), ), ) } else if (end >= start) { @@ -490,8 +492,8 @@ internal object TextLayoutManager { spannable.setSpan( TextInlineViewPlaceholderSpan( fragment.reactTag, - PixelUtil.toPixelFromSP(fragment.width).toInt(), - PixelUtil.toPixelFromSP(fragment.height).toInt(), + inlineViewSizeToPixels(fragment.width), + inlineViewSizeToPixels(fragment.height), ), start, end, @@ -658,6 +660,10 @@ internal object TextLayoutManager { return spannable } + @VisibleForTesting + internal fun inlineViewSizeToPixels(size: Double): Int = + ceil(PixelUtil.toPixelFromDIP(size).toDouble()).toInt() + @OptIn(UnstableReactNativeAPI::class) fun getOrCreateSpannableForText( assets: AssetManager, diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerInlineViewSizeTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerInlineViewSizeTest.kt new file mode 100644 index 00000000000..d7148d0b2f5 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerInlineViewSizeTest.kt @@ -0,0 +1,51 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +@file:Suppress("DEPRECATION") + +package com.facebook.react.views.text + +import android.util.DisplayMetrics +import com.facebook.react.uimanager.DisplayMetricsHolder +import org.assertj.core.api.Assertions.assertThat +import org.junit.After +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class TextLayoutManagerInlineViewSizeTest { + + @After + fun tearDown() { + DisplayMetricsHolder.setScreenDisplayMetrics(null) + } + + @Test + fun `inline view attachment width does not shrink with small font scale`() { + DisplayMetricsHolder.setScreenDisplayMetrics( + DisplayMetrics().apply { + density = 1f + scaledDensity = 0.85f + }, + ) + + assertThat(TextLayoutManager.inlineViewSizeToPixels(155.0)).isEqualTo(155) + } + + @Test + fun `inline view attachment width is rounded up to the pixel grid`() { + DisplayMetricsHolder.setScreenDisplayMetrics( + DisplayMetrics().apply { + density = 1f + scaledDensity = 1f + }, + ) + + assertThat(TextLayoutManager.inlineViewSizeToPixels(132.1)).isEqualTo(133) + } +}