diff --git a/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java b/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java index 16543d64..b0d1990f 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java @@ -22,13 +22,10 @@ import android.graphics.Rect; import android.graphics.Shader; import android.graphics.Typeface; import android.graphics.drawable.Drawable; -import android.os.Build; -import android.os.Environment; import android.text.Html; import android.text.Layout; import android.text.Spanned; import android.text.TextUtils; -import android.util.Log; import android.util.TypedValue; import android.view.Gravity; import android.view.ViewTreeObserver; @@ -44,7 +41,7 @@ import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.security.MessageDigest; +import java.net.URLEncoder; import java.util.concurrent.Callable; import pub.doric.DoricContext; @@ -260,6 +257,7 @@ public class TextNode extends ViewNode { } else if (prop.isObject()) { final JSObject resource = prop.asObject(); final String identifier = resource.getProperty("identifier").asString().value(); + final String type = resource.getProperty("type").asString().value(); final DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager() .load(getDoricContext(), resource); if (doricResource != null) { @@ -268,20 +266,20 @@ public class TextNode extends ViewNode { public void onResult(byte[] fontData) { try { String filePath; - if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { - filePath = Environment.getExternalStorageDirectory().getPath() + "/customFonts"; - } else { - filePath = getContext().getFilesDir().getPath() + "/customFonts"; - } - String fileName = getMD5String(fontData); + filePath = getContext().getCacheDir().getPath() + File.separator + "DoricTextFonts"; + String fileName = URLEncoder.encode(type + identifier, "UTF-8"); if (TextUtils.isEmpty(fileName)) { fileName = (identifier == null) ? "tempFont.ttf" : identifier; } else { fileName = fileName + ".ttf"; } File file = createFile(fontData, filePath, fileName); - Typeface customFont = Typeface.createFromFile(file); - view.setTypeface(customFont); + if (file == null) { + DoricLog.e("Error Font file load resource %s", resource.toString()); + } else { + Typeface customFont = Typeface.createFromFile(file); + view.setTypeface(customFont); + } } catch (Exception e) { DoricLog.e("Error Font asset load resource %s", resource.toString()); } @@ -460,37 +458,16 @@ public class TextNode extends ViewNode { textView.invalidate(); } - private static String getMD5String(byte[] input) { - try { - MessageDigest md = MessageDigest.getInstance("MD5"); - byte[] bytes = md.digest(input); - return hex(bytes); - } catch (Exception e) { - return ""; - } - } - - private static String hex(byte[] bytes) { - char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - char[] resultCharArray = new char[bytes.length * 2]; - int index = 0; - for (byte b : bytes) { - resultCharArray[index++] = hexDigits[b >>> 4 & 0xf]; - resultCharArray[index++] = hexDigits[b & 0xf]; - } - return new String(resultCharArray); - } - - private static File createFile(byte[] bfile, String filePath,String fileName) { + private static File createFile(byte[] bfile, String filePath,String fileName) throws IOException { BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null; try { File dir = new File(filePath); - if(!dir.exists() && dir.isDirectory()){ + if(!dir.exists()){ dir.mkdirs(); } - String pathName = filePath + "\\" + fileName; + String pathName = filePath + File.separator + fileName; file = new File(pathName); if (file.exists()) { return file; @@ -500,6 +477,7 @@ public class TextNode extends ViewNode { bos.write(bfile); } catch (Exception e) { e.printStackTrace(); + throw e; } finally { if (bos != null) { try { diff --git a/doric-demo/assets/Alibaba-PuHuiTi-Bold.ttf b/doric-demo/assets/Alibaba-PuHuiTi-Bold.ttf new file mode 100644 index 00000000..af57de0e Binary files /dev/null and b/doric-demo/assets/Alibaba-PuHuiTi-Bold.ttf differ diff --git a/doric-demo/bundle/assets/Alibaba-PuHuiTi-Bold.ttf b/doric-demo/bundle/assets/Alibaba-PuHuiTi-Bold.ttf new file mode 100644 index 00000000..af57de0e Binary files /dev/null and b/doric-demo/bundle/assets/Alibaba-PuHuiTi-Bold.ttf differ diff --git a/doric-demo/src/TextDemo.ts b/doric-demo/src/TextDemo.ts index b3c1df4e..ea404194 100644 --- a/doric-demo/src/TextDemo.ts +++ b/doric-demo/src/TextDemo.ts @@ -212,7 +212,7 @@ class TextDemo extends Panel { textSize: 10, }), text({ - text: "Font from custom loader.", + text: "Hanabi.ttf", textSize: 30, textColor: Color.BLUE, font: new AssetsResource('Hanabi.ttf') @@ -222,10 +222,10 @@ class TextDemo extends Panel { textSize: 10, }), text({ - text: "Font from custom loader.", + text: "Alibaba-PuHuiTi-Bold.ttf", textSize: 30, textColor: Color.BLUE, - font: new AssetsResource('DINPro.ttf') + font: new AssetsResource('Alibaba-PuHuiTi-Bold.ttf') }), text({ text: "This is line Spaceing 0,\nSecond line", diff --git a/doric-iOS/Pod/Classes/Shader/DoricTextNode.m b/doric-iOS/Pod/Classes/Shader/DoricTextNode.m index 5477def9..31ce3513 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricTextNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricTextNode.m @@ -61,7 +61,6 @@ @interface DoricTextNode () @property(nonatomic, copy) NSNumber *strikethrough; @property(nonatomic, strong) NSDictionary *textGradientProps; @property(nonatomic, assign) CGSize textGradientSize; -@property(nonatomic, assign) CGFloat textSize; @end @implementation DoricTextNode @@ -84,7 +83,6 @@ - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)pro } else { view.font = [UIFont systemFontOfSize:[(NSNumber *) prop floatValue]]; } - self.textSize = [(NSNumber *) prop floatValue]; } else if ([name isEqualToString:@"textColor"]) { if ([prop isKindOfClass:[NSNumber class]]) { view.textColor = DoricColor(prop); @@ -148,12 +146,14 @@ - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)pro withContext:self.doricContext] fetch]; [asyncResult setResultCallback:^(NSData *fontData) { [self.doricContext dispatchToMainQueue:^{ - view.font = [self registerFontWithFontData:fontData fontSize:self.textSize > 0 ? self.textSize : 12]; + view.font = [self registerFontWithFontData:fontData fontSize:view.font.pointSize]; }]; }]; [asyncResult setExceptionCallback:^(NSException *e) { DoricLog(@"Cannot load resource %@, %@", prop, e.reason); }]; + } else { + DoricLog(@"load resource error for View Type :%@, prop is %@", self.class, name); } } else if ([name isEqualToString:@"lineSpacing"]) { [[self ensureParagraphStyle] also:^(NSMutableParagraphStyle *it) { @@ -326,6 +326,8 @@ - (void)requestLayout { - (UIFont *)registerFontWithFontData:(NSData *)fontData fontSize:(CGFloat)fontSize{ CGDataProviderRef fontDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData); CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider); + // THE NEXT LINE IS RELEVANT PART + // https://stackoverflow.com/questions/24900979/cgfontcreatewithdataprovider-hangs-in-airplane-mode [UIFont familyNames]; CGDataProviderRelease(fontDataProvider); CTFontManagerRegisterGraphicsFont(fontRef, NULL);