Skip to content

Commit

Permalink
Move font logic out of react native fork
Browse files Browse the repository at this point in the history
  • Loading branch information
GregKWhite committed Jul 2, 2024
1 parent 5105552 commit 91c4307
Showing 1 changed file with 3 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
public class ReactFontManager {
public static Callback createAssetTypefaceOverride = null;

// NOTE: Indices in `EXTENSIONS` correspond to the `TypeFace` style constants.
private static final String[] EXTENSIONS = {"", "_bold", "_italic", "_bold_italic"};
Expand Down Expand Up @@ -150,28 +151,10 @@ public void setTypeface(String fontFamilyName, int style, Typeface typeface) {

private static Typeface createAssetTypeface(
String fontFamilyName, int style, AssetManager assetManager) {
// This logic attempts to safely check if the frontend code is attempting to use
// fallback fonts, and if it is, to use the fallback typeface creation logic.
String[] fontFamilyNames = fontFamilyName != null ? fontFamilyName.split(",") : null;
if (fontFamilyNames != null) {
for (int i = 0; i < fontFamilyNames.length; i++) {
fontFamilyNames[i] = fontFamilyNames[i].trim();
}
}

// If there are multiple font family names:
// For newer versions of Android, construct a Typeface with fallbacks
// For older versions of Android, ignore all the fallbacks and just use the first font family
if (fontFamilyNames != null && fontFamilyNames.length > 1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
return createAssetTypefaceWithFallbacks(fontFamilyNames, style, assetManager);
} else {
fontFamilyName = fontFamilyNames[0];
}
if (createAssetTypefaceOverride != null) {
return createAssetTypefaceOverride.invoke(fontFamilyName, style, assetManager);
}

// Lastly, after all those checks above, this is the original RN logic for
// getting the typeface.
String extension = EXTENSIONS[style];
for (String fileExtension : FILE_EXTENSIONS) {
String fileName =
Expand Down

0 comments on commit 91c4307

Please sign in to comment.