Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update Java comments
  • Loading branch information
stevenpetryk committed Nov 15, 2022
commit 4a3920cebaccf9f00a0051624683f085b2f27284
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,18 @@ 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);
Expand All @@ -154,6 +160,8 @@ private static Typeface createAssetTypeface(
}
}
Comment on lines 143 to 161
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few checks happening here:

  1. First we really carefully try to see whether a comma-separated list was passed to fontFamilyName
  2. If it was, we split it and trim whitespace off each entry (to allow folks to put spaces around commas)
  3. Finally, iff that list has more than one thing and we're on a high enough version of Android, we use the fallback font creation code. If we're on an older version of Android, we just ignore the fallbacks and proceed wit the original logic.


// 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 All @@ -176,10 +184,10 @@ private static Typeface createAssetTypeface(
@RequiresApi(api = Build.VERSION_CODES.Q)
private static Typeface createAssetTypefaceWithFallbacks(
String[] fontFamilyNames, int style, AssetManager assetManager) {
// Create a new list of fontFamilies
List<FontFamily> fontFamilies = new ArrayList<>();

// For each font family name, create a new fontFamily and add it to the list
// Iterate over the list of fontFamilyNames, constructing new FontFamily objects
// for use in the CustomFallbackBuilder below.
for (String fontFamilyName : fontFamilyNames) {
String extension = EXTENSIONS[style];
for (String fileExtension : FILE_EXTENSIONS) {
Expand All @@ -204,14 +212,10 @@ private static Typeface createAssetTypefaceWithFallbacks(
}
}

// Using the first fontFamily, construct a new CustomFallbackBuilder typeface
Typeface.CustomFallbackBuilder fallbackBuilder = new Typeface.CustomFallbackBuilder(fontFamilies.get(0));
// For each fontFamily, add it as a fallback
for (int i = 1; i < fontFamilies.size(); i++) {
fallbackBuilder.addCustomFallback(fontFamilies.get(i));
}

// Return the built typeface
return fallbackBuilder.build();
}

Expand Down