Skip to content

Commit c1401a1

Browse files
mosab-nasrallahmeta-codesync[bot]
authored andcommitted
Log unrecognized font family on Android (#58804)
Summary: On Android, a `fontFamily` that can't be resolved silently falls back to the default system font. iOS logs `Unrecognized font family '<name>'` in the same case (`RCTFont.mm`). This adds the same log on Android, only when `Typeface.create` falls back to the default typeface, so valid system families like `monospace` don't log. Like `RCTLogInfo` on iOS, it only runs in debug builds. Fixes #58750 Replaces #58751, which was closed by accident when its fork got detached. ## Changelog: [ANDROID] [ADDED] - Log unrecognized font families, matching iOS Pull Request resolved: #58804 Test Plan: Ran this branch's `ReactFontManager.kt` in a test app on an API 35 emulator and checked logcat. Debug build: - `'FontFamilyThatDoesNotExist'` logs `Unrecognized font family 'FontFamilyThatDoesNotExist'` (once per style) - `'monospace'`, `'serif'`, `'sans-serif'`, `'sans-serif-medium'`, `'cursive'`, fonts in `assets/fonts/` and fonts added with `addCustomFont` log nothing - Every font family name declared on the device (34 names, all 4 styles) logs nothing Release build: nothing is logged, including for `'FontFamilyThatDoesNotExist'`. Repro: [RNTesterPlayground.js](https://github.com/mosab-nasrallah/react-native/blob/repro-android-unknown-font-family/packages/rn-tester/js/examples/Playground/RNTesterPlayground.js) Reviewed By: javache Differential Revision: D122984557 Pulled By: fabriziocucci fbshipit-source-id: 692295588db632d80673430b27e5a8c50cd247c0
1 parent 148a6e0 commit c1401a1

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

  • packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/assets

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/assets/ReactFontManager.kt‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import android.content.Context
1111
import android.content.res.AssetManager
1212
import android.graphics.Typeface
1313
import android.os.Build
14+
import android.util.Log
1415
import android.util.SparseArray
1516
import androidx.core.content.res.ResourcesCompat
1617
import com.facebook.react.common.ReactConstants
18+
import com.facebook.react.common.build.ReactBuildConfig
1719

1820
/**
1921
* Responsible for loading and caching Typeface objects.
@@ -162,6 +164,8 @@ public class ReactFontManager {
162164
private val EXTENSIONS = arrayOf("", "_bold", "_italic", "_bold_italic")
163165
private val FILE_EXTENSIONS = arrayOf(".ttf", ".otf")
164166
private const val FONTS_ASSET_PATH = "fonts/"
167+
// Android's default font family.
168+
private const val DEFAULT_FAMILY = "sans-serif"
165169

166170
private val _instance = ReactFontManager()
167171

@@ -184,7 +188,16 @@ public class ReactFontManager {
184188
}
185189
}
186190
}
187-
return Typeface.create(fontFamilyName, style)
191+
192+
val typeface = Typeface.create(fontFamilyName, style)
193+
// Like RCTLogInfo on iOS, only log in debug builds.
194+
if (ReactBuildConfig.DEBUG) {
195+
// Typeface.create returns the default typeface for unknown family names.
196+
if (fontFamilyName != DEFAULT_FAMILY && typeface == Typeface.defaultFromStyle(style)) {
197+
Log.i(ReactConstants.TAG, "Unrecognized font family '$fontFamilyName'")
198+
}
199+
}
200+
return typeface
188201
}
189202
}
190203

0 commit comments

Comments
 (0)