From e9af0247a62a831366e0c8a042fa7d6fab3f3757 Mon Sep 17 00:00:00 2001 From: Steven Petryk Date: Mon, 14 Nov 2022 11:11:33 -0800 Subject: [PATCH] Try setting an obvious fallback on Android --- .../react/views/text/ReactFontManager.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactFontManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactFontManager.java index d81490fd82cb25..52b50e56a72320 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactFontManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactFontManager.java @@ -10,10 +10,14 @@ import android.content.Context; import android.content.res.AssetManager; import android.graphics.Typeface; +import android.graphics.fonts.Font; +import android.graphics.fonts.FontFamily; import android.util.SparseArray; import androidx.annotation.Nullable; import androidx.core.content.res.ResourcesCompat; import com.facebook.infer.annotation.Nullsafe; + +import java.io.IOException; import java.util.HashMap; import java.util.Map; @@ -142,10 +146,25 @@ private static Typeface createAssetTypeface( .append(fileExtension) .toString(); try { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) { + Font font = new Font.Builder(assetManager, fileName).build(); + FontFamily family = new FontFamily.Builder(font).build(); + + Typeface.CustomFallbackBuilder fallbackBuilder = new Typeface.CustomFallbackBuilder(family); + // TODO - just for testing + fallbackBuilder.setSystemFallback("serif"); + + return fallbackBuilder.build(); + } + + // Earlier versions of Android are unable to have fallbacks specified return Typeface.createFromAsset(assetManager, fileName); } catch (RuntimeException e) { // If the typeface asset does not exist, try another extension. continue; + } catch (IOException e) { + // If the font asset does not exist, try another extension. + continue; } } return Typeface.create(fontFamilyName, style);