15
15
import com .facebook .react .bridge .ReactContextBaseJavaModule ;
16
16
import com .facebook .react .bridge .ReactMethod ;
17
17
import com .facebook .react .bridge .Callback ;
18
+ import com .facebook .react .views .text .ReactFontManager ;
18
19
19
20
import java .io .File ;
20
21
import java .io .FileOutputStream ;
@@ -39,15 +40,15 @@ public VectorIconsModule(ReactApplicationContext reactContext) {
39
40
}
40
41
41
42
@ 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 ) {
43
44
Context context = getReactApplicationContext ();
44
45
File cacheFolder = context .getCacheDir ();
45
46
String cacheFolderPath = cacheFolder .getAbsolutePath () + "/" ;
46
47
47
48
float scale = context .getResources ().getDisplayMetrics ().density ;
48
49
String scaleSuffix = "@" + (scale == (int ) scale ? Integer .toString ((int ) scale ) : Float .toString (scale )) + "x" ;
49
50
int size = Math .round (fontSize *scale );
50
- String cacheKey = fontFile + ":" + glyph + ":" + color ;
51
+ String cacheKey = fontFamily + ":" + glyph + ":" + color ;
51
52
String hash = Integer .toString (cacheKey .hashCode (), 32 );
52
53
String cacheFilePath = cacheFolderPath + hash + "_" + Integer .toString (fontSize ) + scaleSuffix + ".png" ;
53
54
String cacheFileUrl = "file://" + cacheFilePath ;
@@ -57,7 +58,7 @@ public void getImageForFont(String fontFile, String glyph, Integer fontSize, Int
57
58
callback .invoke (null , cacheFileUrl );
58
59
} else {
59
60
FileOutputStream fos = null ;
60
- Typeface typeface = this . getOrCreateTypeface ( fontFile , context );
61
+ Typeface typeface = ReactFontManager . getInstance (). getTypeface ( fontFamily , 0 , context . getAssets () );
61
62
Paint paint = new Paint ();
62
63
paint .setTypeface (typeface );
63
64
paint .setColor (color );
@@ -97,19 +98,4 @@ public void getImageForFont(String fontFile, String glyph, Integer fontSize, Int
97
98
}
98
99
}
99
100
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
-
115
101
}
0 commit comments