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

[WEEX-203][Android] base64 font-face support #1006

Merged
merged 1 commit into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}