Skip to content

Commit

Permalink
Back out "[react-native][PR] add support for native/downloadable fonts"
Browse files Browse the repository at this point in the history
Summary: Original commit changeset: 67ba3148fb4b

Reviewed By: cpojer

Differential Revision: D15071309

fbshipit-source-id: 8ea6b40ae7cedd8aec1463373ccd219212fce0f5
  • Loading branch information
fkgozali authored and facebook-github-bot committed Apr 25, 2019
1 parent 5592744 commit eb40b09
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 60 deletions.
5 changes: 0 additions & 5 deletions RNTester/android/app/src/main/res/font/srisakdi.xml

This file was deleted.

Binary file not shown.
Binary file not shown.
8 changes: 0 additions & 8 deletions RNTester/js/TextExample.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,6 @@ class TextExample extends React.Component<{}> {
<Text style={{fontFamily: 'notoserif', fontStyle: 'italic'}}>
NotoSerif Italic (Missing Font file)
</Text>
<Text style={{fontFamily: 'srisakdi'}}>Srisakdi Regular</Text>
<Text
style={{
fontFamily: 'srisakdi',
fontWeight: 'bold',
}}>
Srisakdi Bold
</Text>
</View>
</View>
</RNTesterBlock>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

package com.facebook.react.views.text;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Paint;
import android.graphics.Typeface;
Expand All @@ -31,30 +29,31 @@ public class CustomStyleSpan extends MetricAffectingSpan implements ReactSpan {
* Fonts are retrieved and cached using the {@link ReactFontManager}
*/

private final AssetManager mAssetManager;

private final int mStyle;
private final int mWeight;
private final @Nullable String mFontFamily;
private final Context mContext;

public CustomStyleSpan(
int fontStyle,
int fontWeight,
@Nullable String fontFamily,
@Nonnull Context context) {
AssetManager assetManager) {
mStyle = fontStyle;
mWeight = fontWeight;
mFontFamily = fontFamily;
mContext = context;
mAssetManager = assetManager;
}

@Override
public void updateDrawState(TextPaint ds) {
apply(ds, mStyle, mWeight, mFontFamily, mContext);
apply(ds, mStyle, mWeight, mFontFamily, mAssetManager);
}

@Override
public void updateMeasureState(@Nonnull TextPaint paint) {
apply(paint, mStyle, mWeight, mFontFamily, mContext);
public void updateMeasureState(TextPaint paint) {
apply(paint, mStyle, mWeight, mFontFamily, mAssetManager);
}

/**
Expand Down Expand Up @@ -83,7 +82,7 @@ private static void apply(
int style,
int weight,
@Nullable String family,
Context context) {
AssetManager assetManager) {
int oldStyle;
Typeface typeface = paint.getTypeface();
if (typeface == null) {
Expand All @@ -104,7 +103,7 @@ private static void apply(
}

if (family != null) {
typeface = ReactFontManager.getInstance().getTypeface(family, want, context);
typeface = ReactFontManager.getInstance().getTypeface(family, want, assetManager);
} else if (typeface != null) {
// TODO(t9055065): Fix custom fonts getting applied to text children with different style
typeface = Typeface.create(typeface, want);
Expand All @@ -117,4 +116,5 @@ private static void apply(
}
paint.setSubpixelText(true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private static void buildSpannedFromShadowNode(
textShadowNode.mFontStyle,
textShadowNode.mFontWeight,
textShadowNode.mFontFamily,
textShadowNode.getThemedContext())));
textShadowNode.getThemedContext().getAssets())));
}
if (textShadowNode.mIsUnderlineTextDecorationSet) {
ops.add(new SetSpanOperation(start, end, new ReactUnderlineSpan()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;
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
* the assets/fonts folder and if it doesn't find the right Typeface in that folder will fall back
Expand All @@ -41,11 +37,9 @@ public class ReactFontManager {
private static ReactFontManager sReactFontManagerInstance;

private Map<String, FontFamily> mFontCache;
private Map<String, Typeface> mTypeCache;

private ReactFontManager() {
mFontCache = new HashMap<>();
mTypeCache = new HashMap<>();
}

public static ReactFontManager getInstance() {
Expand All @@ -55,7 +49,8 @@ public static ReactFontManager getInstance() {
return sReactFontManagerInstance;
}

private @Nullable Typeface getTypeface(
public
@Nullable Typeface getTypeface(
String fontFamilyName,
int style,
AssetManager assetManager) {
Expand All @@ -76,33 +71,6 @@ public static ReactFontManager getInstance() {
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.
* @param style
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static void buildSpannableFromFragment(
textAttributes.mFontStyle,
textAttributes.mFontWeight,
textAttributes.mFontFamily,
context)));
context.getAssets())));
}
if (textAttributes.mIsUnderlineTextDecorationSet) {
ops.add(new SetSpanOperation(start, end, new ReactUnderlineSpan()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public void setFontFamily(ReactEditText view, String fontFamily) {
Typeface newTypeface = ReactFontManager.getInstance().getTypeface(
fontFamily,
style,
view.getContext());
view.getContext().getAssets());
view.setTypeface(newTypeface);
}

Expand Down

0 comments on commit eb40b09

Please sign in to comment.