From ac74bd8c64c083131dcf254c22fcdddb4238c987 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Tue, 2 Apr 2024 01:52:39 -0700 Subject: [PATCH] Fix PixelUtil OSS Build failure (#43754) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43754 Moves off of deprecated `DisplayMetrics.scaledDensity` API to builtin font scaling API, while still clamping to max multiplier using previous logic. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D55623674 fbshipit-source-id: 2668eea8dbd154cc046e2c515323ad66b289dc64 --- .../java/com/facebook/react/uimanager/PixelUtil.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.kt index 99a67dafe6a..48f1f1e2f24 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.kt @@ -8,6 +8,7 @@ package com.facebook.react.uimanager import android.util.TypedValue +import kotlin.math.min /** Android dp to pixel manipulation */ public object PixelUtil { @@ -29,12 +30,13 @@ public object PixelUtil { @JvmStatic public fun toPixelFromSP(value: Float, maxFontScale: Float = Float.NaN): Float { val displayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics() - var scaledDensity = displayMetrics.scaledDensity - val currentFontScale = scaledDensity / displayMetrics.density - if (maxFontScale >= 1 && maxFontScale < currentFontScale) { - scaledDensity = displayMetrics.density * maxFontScale + val scaledValue = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, value, displayMetrics) + + if (maxFontScale >= 1) { + return min(scaledValue, value * displayMetrics.density * maxFontScale) } - return value * scaledDensity + + return scaledValue } /** Convert from SP to PX */