Skip to content

Commit 13f4fa0

Browse files
dulmandakhkelset
authored andcommittedJun 28, 2019
custom fontWeight numeric values for Text on Android (#25341)
Summary: I found that on Android we only support 2 fontWeight options, either **normal** or **bold**, even developer can set any numeric value. But iOS supports all possible numeric values. This PR tries to add support for all possible numeric values on Android, even if it's supported only on Android P(28) and above. This change might break texts where fontWeight use improperly, because this PR removes conversion of values above 500 to BOLD and below 500 to normal. FYI, also moved **mCustomTypefaceCache** usage up because it was working after unnecessary mFontCache usage. ## Changelog [Android] [Changed] - add custom font weight support to Text component on Android, only on P(API 28) and above versions. Pull Request resolved: #25341 Test Plan: RNTester app's Text examples will show Rubik Regular, Rubik Light, Rubik Bold, Rubik Medium and Rubik Medium Italic texts in corresponding font family, style and weights. Differential Revision: D15956350 Pulled By: mdvacca fbshipit-source-id: 61079d953c65fb34ab4497d44c22317912a5a616
1 parent 9792f2c commit 13f4fa0

14 files changed

+70
-31
lines changed
 

‎RNTester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public List<ReactPackage> getPackages() {
4747

4848
@Override
4949
public void onCreate() {
50-
ReactFontManager.getInstance().addCustomFont(this, "Srisakdi", R.font.srisakdi);
50+
ReactFontManager.getInstance().addCustomFont(this, "Rubik", R.font.rubik);
5151
super.onCreate();
5252
}
5353

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
3+
<font app:fontStyle="normal" app:fontWeight="300" app:font="@font/rubik_light"/>
4+
<font app:fontStyle="normal" app:fontWeight="400" app:font="@font/rubik_regular"/>
5+
<font app:fontStyle="normal" app:fontWeight="500" app:font="@font/rubik_medium" />
6+
<font app:fontStyle="normal" app:fontWeight="700" app:font="@font/rubik_bold" />
7+
<font app:fontStyle="italic" app:fontWeight="500" app:font="@font/rubik_medium_italic" />
8+
</font-family>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

‎RNTester/android/app/src/main/res/font/srisakdi.xml

-5
This file was deleted.
Binary file not shown.
Binary file not shown.

‎RNTester/js/TextExample.android.js

+36-8
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,41 @@ class TextExample extends React.Component<{}> {
183183
<Text style={{fontFamily: 'notoserif', fontStyle: 'italic'}}>
184184
NotoSerif Italic (Missing Font file)
185185
</Text>
186-
<Text style={{fontFamily: 'Srisakdi'}}>Srisakdi Regular</Text>
187186
<Text
188187
style={{
189-
fontFamily: 'Srisakdi',
190-
fontWeight: 'bold',
188+
fontFamily: 'Rubik',
189+
fontWeight: 'normal',
190+
}}>
191+
Rubik Regular
192+
</Text>
193+
<Text
194+
style={{
195+
fontFamily: 'Rubik',
196+
fontWeight: '300',
197+
}}>
198+
Rubik Light
199+
</Text>
200+
<Text
201+
style={{
202+
fontFamily: 'Rubik',
203+
fontWeight: '700',
204+
}}>
205+
Rubik Bold
206+
</Text>
207+
<Text
208+
style={{
209+
fontFamily: 'Rubik',
210+
fontWeight: '500',
211+
}}>
212+
Rubik Medium
213+
</Text>
214+
<Text
215+
style={{
216+
fontFamily: 'Rubik',
217+
fontStyle: 'italic',
218+
fontWeight: '500',
191219
}}>
192-
Srisakdi Bold
220+
Rubik Medium Italic
193221
</Text>
194222
</View>
195223
</View>
@@ -205,15 +233,15 @@ class TextExample extends React.Component<{}> {
205233
</RNTesterBlock>
206234
<RNTesterBlock title="Font Weight">
207235
<Text style={{fontWeight: 'bold'}}>Move fast and be bold</Text>
208-
<Text style={{fontWeight: 'normal'}}>Move fast and be bold</Text>
236+
<Text style={{fontWeight: 'normal'}}>Move fast and be normal</Text>
209237
</RNTesterBlock>
210238
<RNTesterBlock title="Font Style">
211-
<Text style={{fontStyle: 'italic'}}>Move fast and be bold</Text>
212-
<Text style={{fontStyle: 'normal'}}>Move fast and be bold</Text>
239+
<Text style={{fontStyle: 'italic'}}>Move fast and be italic</Text>
240+
<Text style={{fontStyle: 'normal'}}>Move fast and be normal</Text>
213241
</RNTesterBlock>
214242
<RNTesterBlock title="Font Style and Weight">
215243
<Text style={{fontStyle: 'italic', fontWeight: 'bold'}}>
216-
Move fast and be bold
244+
Move fast and be both bold and italic
217245
</Text>
218246
</RNTesterBlock>
219247
<RNTesterBlock title="Text Decoration">

‎ReactAndroid/src/main/java/com/facebook/react/views/text/CustomStyleSpan.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private static void apply(
104104
}
105105

106106
if (family != null) {
107-
typeface = ReactFontManager.getInstance().getTypeface(family, want, assetManager);
107+
typeface = ReactFontManager.getInstance().getTypeface(family, want, weight, assetManager);
108108
} else if (typeface != null) {
109109
// TODO(t9055065): Fix custom fonts getting applied to text children with different style
110110
typeface = Typeface.create(typeface, want);

‎ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ private static int parseNumericFontWeight(String fontWeightString) {
309309
&& fontWeightString.charAt(0) <= '9'
310310
&& fontWeightString.charAt(0) >= '1'
311311
? 100 * (fontWeightString.charAt(0) - '0')
312-
: -1;
312+
: UNSET;
313313
}
314314

315315
protected TextAttributes mTextAttributes;
@@ -487,14 +487,12 @@ public void setFontFamily(@Nullable String fontFamily) {
487487
@ReactProp(name = ViewProps.FONT_WEIGHT)
488488
public void setFontWeight(@Nullable String fontWeightString) {
489489
int fontWeightNumeric =
490-
fontWeightString != null ? parseNumericFontWeight(fontWeightString) : -1;
491-
int fontWeight = UNSET;
492-
if (fontWeightNumeric >= 500 || "bold".equals(fontWeightString)) {
493-
fontWeight = Typeface.BOLD;
494-
} else if ("normal".equals(fontWeightString)
495-
|| (fontWeightNumeric != -1 && fontWeightNumeric < 500)) {
496-
fontWeight = Typeface.NORMAL;
497-
}
490+
fontWeightString != null ? parseNumericFontWeight(fontWeightString) : UNSET;
491+
int fontWeight = fontWeightNumeric != UNSET ? fontWeightNumeric : Typeface.NORMAL;
492+
493+
if (fontWeight == 700 || "bold".equals(fontWeightString)) fontWeight = Typeface.BOLD;
494+
else if (fontWeight == 400 || "normal".equals(fontWeightString)) fontWeight = Typeface.NORMAL;
495+
498496
if (fontWeight != mFontWeight) {
499497
mFontWeight = fontWeight;
500498
markUpdated();

‎ReactAndroid/src/main/java/com/facebook/react/views/text/ReactFontManager.java

+17-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.content.Context;
1414
import android.content.res.AssetManager;
1515
import android.graphics.Typeface;
16+
import android.os.Build;
1617
import android.util.SparseArray;
1718

1819
import androidx.annotation.NonNull;
@@ -54,23 +55,32 @@ public static ReactFontManager getInstance() {
5455
return sReactFontManagerInstance;
5556
}
5657

58+
public @Nullable Typeface getTypeface(
59+
String fontFamilyName,
60+
int style,
61+
AssetManager assetManager) {
62+
return getTypeface(fontFamilyName, style, 0, assetManager);
63+
}
64+
5765
public @Nullable Typeface getTypeface(
5866
String fontFamilyName,
5967
int style,
68+
int weight,
6069
AssetManager assetManager) {
70+
if(mCustomTypefaceCache.containsKey(fontFamilyName)) {
71+
Typeface typeface = mCustomTypefaceCache.get(fontFamilyName);
72+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && weight >= 100 && weight <= 1000) {
73+
return Typeface.create(typeface, weight, (style & Typeface.ITALIC) != 0);
74+
}
75+
return Typeface.create(typeface, style);
76+
}
77+
6178
FontFamily fontFamily = mFontCache.get(fontFamilyName);
6279
if (fontFamily == null) {
6380
fontFamily = new FontFamily();
6481
mFontCache.put(fontFamilyName, fontFamily);
6582
}
6683

67-
if(mCustomTypefaceCache.containsKey(fontFamilyName)) {
68-
return Typeface.create(
69-
mCustomTypefaceCache.get(fontFamilyName),
70-
style
71-
);
72-
}
73-
7484
Typeface typeface = fontFamily.getTypeface(style);
7585
if (typeface == null) {
7686
typeface = createTypeface(fontFamilyName, style, assetManager);

0 commit comments

Comments
 (0)
Please sign in to comment.