Skip to content

Commit c3acbc4

Browse files
vonovakmeta-codesync[bot]
authored andcommitted
feat: enable reading fonts registered via addCustomFont (#55711)
Summary: `ReactFontManager` has a write-only API for custom fonts — you can register fonts but there's no way to query what's been registered. This asymmetry means a library that needs to know about font availability needs to use reflection on the private `customTypefaceCache` map, or maintain a custom registry that can easily go out of sync. This becomes a problem when multiple libraries need to cooperate around fonts. Today when a library registers a font, there's no clean way for another library to discover what the first one registered. This adds a `customFontFamilies` property that returns the set of font family names currently in the custom typeface cache. Only `customTypefaceCache` is exposed in this PR because it contains fonts explicitly registered by library code via `addCustomFont()`, whereas `fontCache` is lazily populated as a side effect of `getTypeface()` calls and its contents depend on what has been requested so far rather than what is available. Asset-based fonts (in `assets/fonts/`) already have a discoverable source of truth - the filesystem. ## Changelog: [ANDROID] [ADDED] - `ReactFontManager.customFontFamilies` property to query registered custom font family names Pull Request resolved: #55711 Test Plan: - tested ok by adding ```kt val customFonts = ReactFontManager.getInstance().customFontFamilies Log.d("RNTesterApplication", "customFontFamilies: $customFonts") ``` prints: `customFontFamilies: [Rubik, FiraCode]` [here](https://github.com/facebook/react-native/blob/9353eb55b877e6e51c35c6041eebd72971c6b9a5/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt#L123) Reviewed By: cortinico Differential Revision: D94665574 Pulled By: javache fbshipit-source-id: 07d802ac98f4a738858038a2af6ce8d85c28b5ce
1 parent f88946d commit c3acbc4

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,7 @@ public final class com/facebook/react/common/assets/ReactFontManager {
16671667
public fun <init> ()V
16681668
public final fun addCustomFont (Landroid/content/Context;Ljava/lang/String;I)V
16691669
public final fun addCustomFont (Ljava/lang/String;Landroid/graphics/Typeface;)V
1670+
public final fun getCustomFontFamilies ()Ljava/util/Set;
16701671
public static final fun getInstance ()Lcom/facebook/react/common/assets/ReactFontManager;
16711672
public final fun getTypeface (Ljava/lang/String;IILandroid/content/res/AssetManager;)Landroid/graphics/Typeface;
16721673
public final fun getTypeface (Ljava/lang/String;ILandroid/content/res/AssetManager;)Landroid/graphics/Typeface;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public class ReactFontManager {
3333
private val fontCache: MutableMap<String, AssetFontFamily> = mutableMapOf()
3434
private val customTypefaceCache: MutableMap<String, Typeface> = mutableMapOf()
3535

36+
/** The set of font family names that have been registered via [addCustomFont]. */
37+
public val customFontFamilies: Set<String>
38+
get() = customTypefaceCache.keys.toSet()
39+
3640
public fun getTypeface(
3741
fontFamilyName: String,
3842
style: Int,

0 commit comments

Comments
 (0)