Back out "[react-native][PR] add support for native/downloadable fonts"

Summary: Original commit changeset: 67ba3148fb4b

Reviewed By: cpojer

Differential Revision: D15071309

fbshipit-source-id: 8ea6b40ae7cedd8aec1463373ccd219212fce0f5
This commit is contained in:
Kevin Gozali
2019-04-25 11:11:40 -07:00
committed by Facebook Github Bot
parent 5592744825
commit eb40b09bfd
9 changed files with 15 additions and 60 deletions

View File

@@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font app:fontStyle="normal" app:fontWeight="400" app:font="@font/srisakdi_regular"/>
<font app:fontStyle="normal" app:fontWeight="700" app:font="@font/srisakdi_bold" />
</font-family>

View File

@@ -182,14 +182,6 @@ class TextExample extends React.Component<{}> {
<Text style={{fontFamily: 'notoserif', fontStyle: 'italic'}}> <Text style={{fontFamily: 'notoserif', fontStyle: 'italic'}}>
NotoSerif Italic (Missing Font file) NotoSerif Italic (Missing Font file)
</Text> </Text>
<Text style={{fontFamily: 'srisakdi'}}>Srisakdi Regular</Text>
<Text
style={{
fontFamily: 'srisakdi',
fontWeight: 'bold',
}}>
Srisakdi Bold
</Text>
</View> </View>
</View> </View>
</RNTesterBlock> </RNTesterBlock>

View File

@@ -7,10 +7,8 @@
package com.facebook.react.views.text; package com.facebook.react.views.text;
import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import android.content.Context;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Typeface; import android.graphics.Typeface;
@@ -31,30 +29,31 @@ public class CustomStyleSpan extends MetricAffectingSpan implements ReactSpan {
* Fonts are retrieved and cached using the {@link ReactFontManager} * Fonts are retrieved and cached using the {@link ReactFontManager}
*/ */
private final AssetManager mAssetManager;
private final int mStyle; private final int mStyle;
private final int mWeight; private final int mWeight;
private final @Nullable String mFontFamily; private final @Nullable String mFontFamily;
private final Context mContext;
public CustomStyleSpan( public CustomStyleSpan(
int fontStyle, int fontStyle,
int fontWeight, int fontWeight,
@Nullable String fontFamily, @Nullable String fontFamily,
@Nonnull Context context) { AssetManager assetManager) {
mStyle = fontStyle; mStyle = fontStyle;
mWeight = fontWeight; mWeight = fontWeight;
mFontFamily = fontFamily; mFontFamily = fontFamily;
mContext = context; mAssetManager = assetManager;
} }
@Override @Override
public void updateDrawState(TextPaint ds) { public void updateDrawState(TextPaint ds) {
apply(ds, mStyle, mWeight, mFontFamily, mContext); apply(ds, mStyle, mWeight, mFontFamily, mAssetManager);
} }
@Override @Override
public void updateMeasureState(@Nonnull TextPaint paint) { public void updateMeasureState(TextPaint paint) {
apply(paint, mStyle, mWeight, mFontFamily, mContext); apply(paint, mStyle, mWeight, mFontFamily, mAssetManager);
} }
/** /**
@@ -83,7 +82,7 @@ public class CustomStyleSpan extends MetricAffectingSpan implements ReactSpan {
int style, int style,
int weight, int weight,
@Nullable String family, @Nullable String family,
Context context) { AssetManager assetManager) {
int oldStyle; int oldStyle;
Typeface typeface = paint.getTypeface(); Typeface typeface = paint.getTypeface();
if (typeface == null) { if (typeface == null) {
@@ -104,7 +103,7 @@ public class CustomStyleSpan extends MetricAffectingSpan implements ReactSpan {
} }
if (family != null) { if (family != null) {
typeface = ReactFontManager.getInstance().getTypeface(family, want, context); typeface = ReactFontManager.getInstance().getTypeface(family, want, assetManager);
} else if (typeface != null) { } else if (typeface != null) {
// TODO(t9055065): Fix custom fonts getting applied to text children with different style // TODO(t9055065): Fix custom fonts getting applied to text children with different style
typeface = Typeface.create(typeface, want); typeface = Typeface.create(typeface, want);
@@ -117,4 +116,5 @@ public class CustomStyleSpan extends MetricAffectingSpan implements ReactSpan {
} }
paint.setSubpixelText(true); paint.setSubpixelText(true);
} }
} }

View File

@@ -188,7 +188,7 @@ public abstract class ReactBaseTextShadowNode extends LayoutShadowNode {
textShadowNode.mFontStyle, textShadowNode.mFontStyle,
textShadowNode.mFontWeight, textShadowNode.mFontWeight,
textShadowNode.mFontFamily, textShadowNode.mFontFamily,
textShadowNode.getThemedContext()))); textShadowNode.getThemedContext().getAssets())));
} }
if (textShadowNode.mIsUnderlineTextDecorationSet) { if (textShadowNode.mIsUnderlineTextDecorationSet) {
ops.add(new SetSpanOperation(start, end, new ReactUnderlineSpan())); ops.add(new SetSpanOperation(start, end, new ReactUnderlineSpan()));

View File

@@ -12,14 +12,10 @@ import javax.annotation.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import android.content.Context;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.util.SparseArray; import android.util.SparseArray;
import androidx.core.content.res.ResourcesCompat;
/** /**
* Class responsible to load and cache Typeface objects. It will first try to load typefaces inside * Class responsible to load and cache Typeface objects. It will first try to load typefaces inside
* the assets/fonts folder and if it doesn't find the right Typeface in that folder will fall back * the assets/fonts folder and if it doesn't find the right Typeface in that folder will fall back
@@ -41,11 +37,9 @@ public class ReactFontManager {
private static ReactFontManager sReactFontManagerInstance; private static ReactFontManager sReactFontManagerInstance;
private Map<String, FontFamily> mFontCache; private Map<String, FontFamily> mFontCache;
private Map<String, Typeface> mTypeCache;
private ReactFontManager() { private ReactFontManager() {
mFontCache = new HashMap<>(); mFontCache = new HashMap<>();
mTypeCache = new HashMap<>();
} }
public static ReactFontManager getInstance() { public static ReactFontManager getInstance() {
@@ -55,7 +49,8 @@ public class ReactFontManager {
return sReactFontManagerInstance; return sReactFontManagerInstance;
} }
private @Nullable Typeface getTypeface( public
@Nullable Typeface getTypeface(
String fontFamilyName, String fontFamilyName,
int style, int style,
AssetManager assetManager) { AssetManager assetManager) {
@@ -76,33 +71,6 @@ public class ReactFontManager {
return typeface; return typeface;
} }
public @Nullable Typeface getTypeface(
String fontFamilyName,
int style,
Context context) {
Typeface font = mTypeCache.get(fontFamilyName);
if (font != null) {
return Typeface.create(
font,
style
);
}
int fontId = context.getResources().getIdentifier(fontFamilyName, "font", context.getPackageName());
if (fontId != 0) {
font = ResourcesCompat.getFont(context, fontId);
if (font != null) {
mTypeCache.put(fontFamilyName, font);
return Typeface.create(
font,
style
);
}
}
return getTypeface(fontFamilyName, style, context.getAssets());
}
/** /**
* Add additional font family, or replace the exist one in the font memory cache. * Add additional font family, or replace the exist one in the font memory cache.
* @param style * @param style

View File

@@ -96,7 +96,7 @@ public class TextLayoutManager {
textAttributes.mFontStyle, textAttributes.mFontStyle,
textAttributes.mFontWeight, textAttributes.mFontWeight,
textAttributes.mFontFamily, textAttributes.mFontFamily,
context))); context.getAssets())));
} }
if (textAttributes.mIsUnderlineTextDecorationSet) { if (textAttributes.mIsUnderlineTextDecorationSet) {
ops.add(new SetSpanOperation(start, end, new ReactUnderlineSpan())); ops.add(new SetSpanOperation(start, end, new ReactUnderlineSpan()));

View File

@@ -220,7 +220,7 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
Typeface newTypeface = ReactFontManager.getInstance().getTypeface( Typeface newTypeface = ReactFontManager.getInstance().getTypeface(
fontFamily, fontFamily,
style, style,
view.getContext()); view.getContext().getAssets());
view.setTypeface(newTypeface); view.setTypeface(newTypeface);
} }