Skip to content

Commit e885dde

Browse files
mdvaccafacebook-github-bot
authored andcommitted
Fix crash when using TextInput.FontVariant prop in Android API level < 26
Summary: This diff fixes a crash when using TextInput.FontVariant prop in Android API level < 26 Changelog: Fix TextInput.FontVariant prop in Android API level < 26 (related to PR #27006) Reviewed By: JoshuaGross Differential Revision: D18331807 fbshipit-source-id: 5eac4d9e38eb099fae1287d128f3f8c249b0b8bc
1 parent a7f56e7 commit e885dde

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTypefaceUtils.java

+30-26
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88
package com.facebook.react.views.text;
99

1010
import android.content.res.AssetManager;
11-
import android.graphics.Paint;
1211
import android.graphics.Typeface;
13-
12+
import android.text.TextUtils;
1413
import androidx.annotation.Nullable;
15-
1614
import com.facebook.react.bridge.ReadableArray;
17-
1815
import java.util.ArrayList;
1916
import java.util.List;
2017

@@ -51,30 +48,37 @@ public static int parseFontStyle(@Nullable String fontStyleString) {
5148
List<String> features = new ArrayList<>();
5249
for (int i = 0; i < fontVariantArray.size(); i++) {
5350
// see https://docs.microsoft.com/en-us/typography/opentype/spec/featurelist
54-
switch (fontVariantArray.getString(i)) {
55-
case "small-caps":
56-
features.add("'smcp'");
57-
break;
58-
case "oldstyle-nums":
59-
features.add("'onum'");
60-
break;
61-
case "lining-nums":
62-
features.add("'lnum'");
63-
break;
64-
case "tabular-nums":
65-
features.add("'tnum'");
66-
break;
67-
case "proportional-nums":
68-
features.add("'pnum'");
69-
break;
51+
String fontVariant = fontVariantArray.getString(i);
52+
if (fontVariant != null) {
53+
switch (fontVariant) {
54+
case "small-caps":
55+
features.add("'smcp'");
56+
break;
57+
case "oldstyle-nums":
58+
features.add("'onum'");
59+
break;
60+
case "lining-nums":
61+
features.add("'lnum'");
62+
break;
63+
case "tabular-nums":
64+
features.add("'tnum'");
65+
break;
66+
case "proportional-nums":
67+
features.add("'pnum'");
68+
break;
69+
}
7070
}
7171
}
7272

73-
return String.join(", ", features);
73+
return TextUtils.join(", ", features);
7474
}
7575

76-
public static Typeface applyStyles(@Nullable Typeface typeface,
77-
int style, int weight, @Nullable String family, AssetManager assetManager) {
76+
public static Typeface applyStyles(
77+
@Nullable Typeface typeface,
78+
int style,
79+
int weight,
80+
@Nullable String family,
81+
AssetManager assetManager) {
7882
int oldStyle;
7983
if (typeface == null) {
8084
oldStyle = 0;
@@ -114,9 +118,9 @@ public static Typeface applyStyles(@Nullable Typeface typeface,
114118
private static int parseNumericFontWeight(String fontWeightString) {
115119
// This should be much faster than using regex to verify input and Integer.parseInt
116120
return fontWeightString.length() == 3
117-
&& fontWeightString.endsWith("00")
118-
&& fontWeightString.charAt(0) <= '9'
119-
&& fontWeightString.charAt(0) >= '1'
121+
&& fontWeightString.endsWith("00")
122+
&& fontWeightString.charAt(0) <= '9'
123+
&& fontWeightString.charAt(0) >= '1'
120124
? 100 * (fontWeightString.charAt(0) - '0')
121125
: UNSET;
122126
}

0 commit comments

Comments
 (0)