Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Custom fonts support added
Reviewed By: andreicoman11 Differential Revision: D2629077 fb-gh-sync-id: 8d647aff13f97d90c5047ad0ddbcae90215ca4ca
- Loading branch information
1 parent
1ae7a77
commit bfeaa6a
Showing
6 changed files
with
176 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file added
BIN
+326 KB
Examples/UIExplorer/android/app/src/main/assets/fonts/notoserif_bold_italic.ttf
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
ReactAndroid/src/main/java/com/facebook/react/views/text/ReactFontManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. All rights reserved. | ||
* <p/> | ||
* This source code is licensed under the BSD-style license found in the LICENSE file in the root | ||
* directory of this source tree. An additional grant of patent rights can be found in the PATENTS | ||
* file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.views.text; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import android.content.res.AssetManager; | ||
import android.graphics.Typeface; | ||
import android.util.SparseArray; | ||
|
||
/** | ||
* 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 | ||
* on the best matching system Typeface The supported custom fonts extensions are .ttf and .otf. For | ||
* each font family the bold, italic and bold_italic variants are supported. Given a "family" font | ||
* family the files in the assets/fonts folder need to be family.ttf(.otf) family_bold.ttf(.otf) | ||
* family_italic.ttf(.otf) and family_bold_italic.ttf(.otf) | ||
*/ | ||
public class ReactFontManager { | ||
|
||
private static final String[] EXTENSIONS = { | ||
"", | ||
"_bold", | ||
"_italic", | ||
"_bold_italic"}; | ||
private static final String[] FILE_EXTENSIONS = {".ttf", ".otf"}; | ||
private static final String FONTS_ASSET_PATH = "fonts/"; | ||
|
||
private static ReactFontManager sReactFontManagerInstance; | ||
|
||
private Map<String, FontFamily> mFontCache; | ||
|
||
private ReactFontManager() { | ||
mFontCache = new HashMap<>(); | ||
} | ||
|
||
public static ReactFontManager getInstance() { | ||
if (sReactFontManagerInstance == null) { | ||
sReactFontManagerInstance = new ReactFontManager(); | ||
} | ||
return sReactFontManagerInstance; | ||
} | ||
|
||
public | ||
@Nullable Typeface getTypeface( | ||
String fontFamilyName, | ||
int style, | ||
AssetManager assetManager) { | ||
FontFamily fontFamily = mFontCache.get(fontFamilyName); | ||
if (fontFamily == null) { | ||
fontFamily = new FontFamily(); | ||
mFontCache.put(fontFamilyName, fontFamily); | ||
} | ||
|
||
Typeface typeface = fontFamily.getTypeface(style); | ||
if (typeface == null) { | ||
typeface = createTypeface(fontFamilyName, style, assetManager); | ||
if (typeface != null) { | ||
fontFamily.setTypeface(style, typeface); | ||
} | ||
} | ||
|
||
return typeface; | ||
} | ||
|
||
private static | ||
@Nullable Typeface createTypeface( | ||
String fontFamilyName, | ||
int style, | ||
AssetManager assetManager) { | ||
String extension = EXTENSIONS[style]; | ||
for (String fileExtension : FILE_EXTENSIONS) { | ||
String fileName = new StringBuilder() | ||
.append(FONTS_ASSET_PATH) | ||
.append(fontFamilyName) | ||
.append(extension) | ||
.append(fileExtension) | ||
.toString(); | ||
try { | ||
return Typeface.createFromAsset(assetManager, fileName); | ||
} catch (RuntimeException e) { | ||
// unfortunately Typeface.createFromAsset throws an exception instead of returning null | ||
// if the typeface doesn't exist | ||
} | ||
} | ||
|
||
return Typeface.create(fontFamilyName, style); | ||
} | ||
|
||
private static class FontFamily { | ||
|
||
private SparseArray<Typeface> mTypefaceSparseArray; | ||
|
||
private FontFamily() { | ||
mTypefaceSparseArray = new SparseArray<>(4); | ||
} | ||
|
||
public Typeface getTypeface(int style) { | ||
return mTypefaceSparseArray.get(style); | ||
} | ||
|
||
public void setTypeface(int style, Typeface typeface) { | ||
mTypefaceSparseArray.put(style, typeface); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bfeaa6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mkonicek should I make a pull request to support load font file from file system , since the Android framework is supporting that. After that we can load extra typeface by downloading font files at runtime.
bfeaa6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sospartan +1
bfeaa6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sospartan +1
bfeaa6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@flyingmate @obipawan I've send PR #4696
bfeaa6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what happens with other font weights like light, black, etc?
bfeaa6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's say I've added MavenPro-Bold.ttf font..
iOS
{ fontFamily: 'Maven Pro', fontWeight: 'bold' }
Android
{ fontFamily: 'MavenPro-Bold' }
This seems a lot of code just to make it work in Android...
Any way to fix Android to use iOS approach?
bfeaa6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do we handle other weights like semibold, extrabold, black etc, as @PierBover said. We can't get by with just 2 fontweights in our app.
bfeaa6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
bfeaa6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andrispraulitis Have you found any way to fix Android to use iOS approach?
bfeaa6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.