custom fonts support The Android Way (#24595)

Summary:
In https://github.com/facebook/react-native/pull/23865, RN introduced support for custom fonts the Android Way. But it introduced performance regression because it'll lookup for a font using getIdentifier() every time fontFamily changed. This PR fixes regression by requiring custom fonts to be listed in **fonts** array, and populating **mTypeCache** at first use using the list.

[Android] [Changed] - Require custom fonts to list in **fonts** array. Fixes performance regression.
Pull Request resolved: https://github.com/facebook/react-native/pull/24595

Reviewed By: mdvacca

Differential Revision: D15184590

Pulled By: fkgozali

fbshipit-source-id: e3feb2396609583ebc95101130186a1f5af931da
This commit is contained in:
Dulmandakh
2019-05-07 18:44:10 -07:00
committed by Facebook Github Bot
parent 661b3b63ec
commit fd6386a07e
8 changed files with 56 additions and 14 deletions
@@ -7,14 +7,15 @@
package com.facebook.react.views.text;
import javax.annotation.Nullable;
import android.content.res.AssetManager;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class CustomStyleSpan extends MetricAffectingSpan implements ReactSpan {
/**
@@ -39,7 +40,7 @@ public class CustomStyleSpan extends MetricAffectingSpan implements ReactSpan {
int fontStyle,
int fontWeight,
@Nullable String fontFamily,
AssetManager assetManager) {
@NonNull AssetManager assetManager) {
mStyle = fontStyle;
mWeight = fontWeight;
mFontFamily = fontFamily;
@@ -52,7 +53,7 @@ public class CustomStyleSpan extends MetricAffectingSpan implements ReactSpan {
}
@Override
public void updateMeasureState(TextPaint paint) {
public void updateMeasureState(@NonNull TextPaint paint) {
apply(paint, mStyle, mWeight, mFontFamily, mAssetManager);
}
@@ -116,5 +117,4 @@ public class CustomStyleSpan extends MetricAffectingSpan implements ReactSpan {
}
paint.setSubpixelText(true);
}
}
@@ -7,15 +7,18 @@
package com.facebook.react.views.text;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.util.SparseArray;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;
/**
* Class responsible to load and cache Typeface objects. It will first try to load typefaces inside
* the assets/fonts folder and if it doesn't find the right Typeface in that folder will fall back
@@ -36,10 +39,12 @@ public class ReactFontManager {
private static ReactFontManager sReactFontManagerInstance;
private Map<String, FontFamily> mFontCache;
final private Map<String, FontFamily> mFontCache;
final private Map<String, Typeface> mCustomTypefaceCache;
private ReactFontManager() {
mFontCache = new HashMap<>();
mCustomTypefaceCache = new HashMap<>();
}
public static ReactFontManager getInstance() {
@@ -49,8 +54,7 @@ public class ReactFontManager {
return sReactFontManagerInstance;
}
public
@Nullable Typeface getTypeface(
public @Nullable Typeface getTypeface(
String fontFamilyName,
int style,
AssetManager assetManager) {
@@ -60,6 +64,13 @@ public class ReactFontManager {
mFontCache.put(fontFamilyName, fontFamily);
}
if(mCustomTypefaceCache.containsKey(fontFamilyName)) {
return Typeface.create(
mCustomTypefaceCache.get(fontFamilyName),
style
);
}
Typeface typeface = fontFamily.getTypeface(style);
if (typeface == null) {
typeface = createTypeface(fontFamilyName, style, assetManager);
@@ -71,6 +82,20 @@ public class ReactFontManager {
return typeface;
}
/*
* This method allows you to load custom fonts from res/font folder as provided font family name.
* Fonts may be one of .ttf, .otf or XML (https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml).
* To support multiple font styles or weights, you must provide a font in XML format.
*
* ReactFontManager.getInstance().addCustomFont(this, "Srisakdi", R.font.srisakdi);
*/
public void addCustomFont(@NonNull Context context, @NonNull String fontFamily, int fontId) {
Typeface font = ResourcesCompat.getFont(context, fontId);
if (font != null) {
mCustomTypefaceCache.put(fontFamily, font);
}
}
/**
* Add additional font family, or replace the exist one in the font memory cache.
* @param style