Skip to content

Commit 3920656

Browse files
committed
Refactors android getImageForFont to use RN font manager.
1 parent 7abdbb1 commit 3920656

File tree

2 files changed

+5
-19
lines changed

2 files changed

+5
-19
lines changed

android/src/main/java/com/oblador/vectoricons/VectorIconsModule.java

+4-18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1616
import com.facebook.react.bridge.ReactMethod;
1717
import com.facebook.react.bridge.Callback;
18+
import com.facebook.react.views.text.ReactFontManager;
1819

1920
import java.io.File;
2021
import java.io.FileOutputStream;
@@ -39,15 +40,15 @@ public VectorIconsModule(ReactApplicationContext reactContext) {
3940
}
4041

4142
@ReactMethod
42-
public void getImageForFont(String fontFile, String glyph, Integer fontSize, Integer color, Callback callback) {
43+
public void getImageForFont(String fontFamily, String glyph, Integer fontSize, Integer color, Callback callback) {
4344
Context context = getReactApplicationContext();
4445
File cacheFolder = context.getCacheDir();
4546
String cacheFolderPath = cacheFolder.getAbsolutePath() + "/";
4647

4748
float scale = context.getResources().getDisplayMetrics().density;
4849
String scaleSuffix = "@" + (scale == (int) scale ? Integer.toString((int) scale) : Float.toString(scale)) + "x";
4950
int size = Math.round(fontSize*scale);
50-
String cacheKey = fontFile + ":" + glyph + ":" + color;
51+
String cacheKey = fontFamily + ":" + glyph + ":" + color;
5152
String hash = Integer.toString(cacheKey.hashCode(), 32);
5253
String cacheFilePath = cacheFolderPath + hash + "_" + Integer.toString(fontSize) + scaleSuffix + ".png";
5354
String cacheFileUrl = "file://" + cacheFilePath;
@@ -57,7 +58,7 @@ public void getImageForFont(String fontFile, String glyph, Integer fontSize, Int
5758
callback.invoke(null, cacheFileUrl);
5859
} else {
5960
FileOutputStream fos = null;
60-
Typeface typeface = this.getOrCreateTypeface(fontFile, context);
61+
Typeface typeface = ReactFontManager.getInstance().getTypeface(fontFamily, 0, context.getAssets());
6162
Paint paint = new Paint();
6263
paint.setTypeface(typeface);
6364
paint.setColor(color);
@@ -97,19 +98,4 @@ public void getImageForFont(String fontFile, String glyph, Integer fontSize, Int
9798
}
9899
}
99100

100-
public static Typeface getOrCreateTypeface(String fontFile, Context context) {
101-
if (sTypefaceCache.get(fontFile) != null) {
102-
return sTypefaceCache.get(fontFile);
103-
}
104-
105-
try {
106-
Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontFile);
107-
sTypefaceCache.put(fontFile, typeface);
108-
return typeface;
109-
} catch (Exception ex) {
110-
Log.e(REACT_CLASS, "Error: " + ex.toString());
111-
}
112-
return null;
113-
}
114-
115101
}

lib/create-icon-set.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ function createIconSet(glyphMap : Object, fontFamily : string, fontFile : string
178178
if(!cached || cached instanceof Error ) { reject(cached); }
179179
return resolve({ uri: cached });
180180
}
181-
NativeIconAPI.getImageForFont(Platform.OS === 'android' ? fontFile : fontFamily, glyph, size, color, function(err, image) {
181+
NativeIconAPI.getImageForFont(fontReference, glyph, size, color, function(err, image) {
182182
if(typeof err === 'string') {
183183
err = new Error(err);
184184
}

0 commit comments

Comments
 (0)