Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
* [android] base64 font-face support for android
Browse files Browse the repository at this point in the history
  • Loading branch information
misakuo committed Jan 29, 2018
1 parent 27b1714 commit 3976636
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ public interface Scheme {
String HTTPS = "https";
String HTTP = "http";
String LOCAL = "local";
String DATA = "data";
}

public interface CodeCache {
Expand Down
23 changes: 22 additions & 1 deletion android/sdk/src/main/java/com/taobao/weex/utils/FontDO.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import android.graphics.Typeface;
import android.net.Uri;
import android.text.TextUtils;

import com.alibaba.fastjson.util.Base64;
import com.taobao.weex.WXEnvironment;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.adapter.URIAdapter;
Expand All @@ -44,6 +46,7 @@ public class FontDO {
public final static int TYPE_FILE = 2;
public final static int TYPE_LOCAL = 3;
public final static int TYPE_NATIVE = 4;
public final static int TYPE_BASE64 = 5;


public FontDO (String fontFamilyName, String src, WXSDKInstance instance) {
Expand Down Expand Up @@ -78,7 +81,6 @@ private void parseSrc(String src, WXSDKInstance instance) {
}
mUrl = uri.toString();
try {

String scheme = uri.getScheme();
if (Constants.Scheme.HTTP.equals(scheme) ||
Constants.Scheme.HTTPS.equals(scheme)) {
Expand All @@ -88,7 +90,26 @@ private void parseSrc(String src, WXSDKInstance instance) {
mUrl = uri.getPath();
} else if (Constants.Scheme.LOCAL.equals(scheme)){
mType = TYPE_LOCAL;
} else if (Constants.Scheme.DATA.equals(scheme)) {
long start = System.currentTimeMillis();
String[] data = mUrl.split(",");
if (data != null && data.length == 2) {
String identify = data[0];
if (!TextUtils.isEmpty(identify) && identify.endsWith("base64")) {
//Do not check mime type and charset for now
String base64Data = data[1];
if (!TextUtils.isEmpty(base64Data)) {
String md5 = WXFileUtils.md5(base64Data);
String filePath = WXEnvironment.getApplication().getCacheDir() + "/font-family/" + md5;
WXFileUtils.saveFile(filePath, Base64.decodeFast(base64Data), WXEnvironment.getApplication());
mUrl = filePath;
mType = TYPE_BASE64;
WXLogUtils.d("TypefaceUtil", "Parse base64 font cost " + (System.currentTimeMillis() - start) + " ms");
}
}
}
} else {
WXLogUtils.e("TypefaceUtil", "Unknown scheme for font url: " + mUrl);
mType = TYPE_UNKNOWN;
}
mState = STATE_INIT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ public static void loadTypeface(final FontDO fontDo) {
} else if (fontDo.getType() == FontDO.TYPE_NETWORK) {
final String url = fontDo.getUrl();
final String fontFamily = fontDo.getFontFamilyName();
final String fileName = url.replace('/', '_').replace(':', '_');
final String fileName = WXFileUtils.md5(url);
//url.replace('/', '_').replace(':', '_');
File dir = new File(getFontCacheDir());
if(!dir.exists()){
dir.mkdirs();
Expand All @@ -148,7 +149,7 @@ public static void loadTypeface(final FontDO fontDo) {
if (!loadLocalFontFile(fullPath, fontFamily, false)) {
downloadFontByNetwork(url, fullPath, fontFamily);
}
} else if (fontDo.getType() == FontDO.TYPE_FILE) {
} else if (fontDo.getType() == FontDO.TYPE_FILE || fontDo.getType() == FontDO.TYPE_BASE64) {
boolean result = loadLocalFontFile(fontDo.getUrl(), fontDo.getFontFamilyName(), false);
if (!result) {
fontDo.setState(FontDO.STATE_FAILED);
Expand Down Expand Up @@ -274,6 +275,6 @@ public void run() {
}

private static String getFontCacheDir() {
return WXEnvironment.getDiskCacheDir(WXEnvironment.getApplication()) + "/" + FONT_CACHE_DIR_NAME;
return WXEnvironment.getApplication().getCacheDir() + "/" + FONT_CACHE_DIR_NAME;
}
}

0 comments on commit 3976636

Please sign in to comment.