From 7341f9abdcfbf5cf4dd5f8cda49151b28ae246bf Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Tue, 18 Jul 2023 23:17:48 -0700 Subject: [PATCH] Android: expose ReactFontManager as a standalone target (#38506) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/38506 There are use cases where an app needs to install a custom font in a separate infra/tool. Instead of bloating those infra/tool with deep React Native dependencies, this allows it to depend on only the pieces that matter. To ensure backward compatibility, the existing ReactFontManager will proxy method calls to the new impl going forward. Changelog: [Changed][Android] Moved ReactFontManager to a common package Reviewed By: mdvacca Differential Revision: D47569319 fbshipit-source-id: 18ba6617d6c3f68823bdccaacfd8cc961cd6ce34 --- .../react/common/assets/ReactFontManager.java | 239 ++++++++++++++++++ .../react/views/text/CustomStyleSpan.java | 7 +- .../views/text/ReactBaseTextShadowNode.java | 3 +- .../react/views/text/ReactFontManager.java | 143 +---------- .../react/views/text/ReactTypefaceUtils.java | 8 +- .../react/views/text/TypefaceStyle.java | 72 ------ .../react/uiapp/RNTesterApplication.java | 2 +- 7 files changed, 266 insertions(+), 208 deletions(-) create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/assets/ReactFontManager.java delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TypefaceStyle.java diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/assets/ReactFontManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/assets/ReactFontManager.java new file mode 100644 index 00000000000000..7e7df01bd140ff --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/assets/ReactFontManager.java @@ -0,0 +1,239 @@ +/* + * 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. + */ + +package com.facebook.react.common.assets; + +import android.content.Context; +import android.content.res.AssetManager; +import android.graphics.Typeface; +import android.os.Build; +import android.util.SparseArray; +import androidx.annotation.Nullable; +import androidx.core.content.res.ResourcesCompat; +import com.facebook.infer.annotation.Nullsafe; +import java.util.HashMap; +import java.util.Map; + +/** + * Responsible for loading and caching Typeface objects. + * + *

This will first try to load a typeface from the assets/fonts folder. If one is not found in + * that folder, this will fallback to the best matching system typeface. + * + *

Custom fonts support the extensions `.ttf` and `.otf` and the variants `bold`, `italic`, and + * `bold_italic`. For example, given a font named "ExampleFontFamily", the following are supported: + * + *