Skip to content

Commit

Permalink
Try setting an obvious fallback on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenpetryk committed Nov 14, 2022
1 parent 767848d commit e9af024
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e9af024

Please sign in to comment.