remove textSize && using urlencode

This commit is contained in:
吴尚昆 2022-03-03 16:49:06 +08:00 committed by osborn
parent 4903e1d31a
commit 37a4332a19
5 changed files with 22 additions and 42 deletions

View File

@ -22,13 +22,10 @@ import android.graphics.Rect;
import android.graphics.Shader; import android.graphics.Shader;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Environment;
import android.text.Html; import android.text.Html;
import android.text.Layout; import android.text.Layout;
import android.text.Spanned; import android.text.Spanned;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.Gravity; import android.view.Gravity;
import android.view.ViewTreeObserver; import android.view.ViewTreeObserver;
@ -44,7 +41,7 @@ import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.security.MessageDigest; import java.net.URLEncoder;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import pub.doric.DoricContext; import pub.doric.DoricContext;
@ -260,6 +257,7 @@ public class TextNode extends ViewNode<TextView> {
} else if (prop.isObject()) { } else if (prop.isObject()) {
final JSObject resource = prop.asObject(); final JSObject resource = prop.asObject();
final String identifier = resource.getProperty("identifier").asString().value(); final String identifier = resource.getProperty("identifier").asString().value();
final String type = resource.getProperty("type").asString().value();
final DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager() final DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager()
.load(getDoricContext(), resource); .load(getDoricContext(), resource);
if (doricResource != null) { if (doricResource != null) {
@ -268,20 +266,20 @@ public class TextNode extends ViewNode<TextView> {
public void onResult(byte[] fontData) { public void onResult(byte[] fontData) {
try { try {
String filePath; String filePath;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { filePath = getContext().getCacheDir().getPath() + File.separator + "DoricTextFonts";
filePath = Environment.getExternalStorageDirectory().getPath() + "/customFonts"; String fileName = URLEncoder.encode(type + identifier, "UTF-8");
} else {
filePath = getContext().getFilesDir().getPath() + "/customFonts";
}
String fileName = getMD5String(fontData);
if (TextUtils.isEmpty(fileName)) { if (TextUtils.isEmpty(fileName)) {
fileName = (identifier == null) ? "tempFont.ttf" : identifier; fileName = (identifier == null) ? "tempFont.ttf" : identifier;
} else { } else {
fileName = fileName + ".ttf"; fileName = fileName + ".ttf";
} }
File file = createFile(fontData, filePath, fileName); File file = createFile(fontData, filePath, fileName);
if (file == null) {
DoricLog.e("Error Font file load resource %s", resource.toString());
} else {
Typeface customFont = Typeface.createFromFile(file); Typeface customFont = Typeface.createFromFile(file);
view.setTypeface(customFont); view.setTypeface(customFont);
}
} catch (Exception e) { } catch (Exception e) {
DoricLog.e("Error Font asset load resource %s", resource.toString()); DoricLog.e("Error Font asset load resource %s", resource.toString());
} }
@ -460,37 +458,16 @@ public class TextNode extends ViewNode<TextView> {
textView.invalidate(); textView.invalidate();
} }
private static String getMD5String(byte[] input) { private static File createFile(byte[] bfile, String filePath,String fileName) throws IOException {
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) {
BufferedOutputStream bos = null; BufferedOutputStream bos = null;
FileOutputStream fos = null; FileOutputStream fos = null;
File file = null; File file = null;
try { try {
File dir = new File(filePath); File dir = new File(filePath);
if(!dir.exists() && dir.isDirectory()){ if(!dir.exists()){
dir.mkdirs(); dir.mkdirs();
} }
String pathName = filePath + "\\" + fileName; String pathName = filePath + File.separator + fileName;
file = new File(pathName); file = new File(pathName);
if (file.exists()) { if (file.exists()) {
return file; return file;
@ -500,6 +477,7 @@ public class TextNode extends ViewNode<TextView> {
bos.write(bfile); bos.write(bfile);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw e;
} finally { } finally {
if (bos != null) { if (bos != null) {
try { try {

Binary file not shown.

Binary file not shown.

View File

@ -212,7 +212,7 @@ class TextDemo extends Panel {
textSize: 10, textSize: 10,
}), }),
text({ text({
text: "Font from custom loader.", text: "Hanabi.ttf",
textSize: 30, textSize: 30,
textColor: Color.BLUE, textColor: Color.BLUE,
font: new AssetsResource('Hanabi.ttf') font: new AssetsResource('Hanabi.ttf')
@ -222,10 +222,10 @@ class TextDemo extends Panel {
textSize: 10, textSize: 10,
}), }),
text({ text({
text: "Font from custom loader.", text: "Alibaba-PuHuiTi-Bold.ttf",
textSize: 30, textSize: 30,
textColor: Color.BLUE, textColor: Color.BLUE,
font: new AssetsResource('DINPro.ttf') font: new AssetsResource('Alibaba-PuHuiTi-Bold.ttf')
}), }),
text({ text({
text: "This is line Spaceing 0,\nSecond line", text: "This is line Spaceing 0,\nSecond line",

View File

@ -61,7 +61,6 @@ @interface DoricTextNode ()
@property(nonatomic, copy) NSNumber *strikethrough; @property(nonatomic, copy) NSNumber *strikethrough;
@property(nonatomic, strong) NSDictionary *textGradientProps; @property(nonatomic, strong) NSDictionary *textGradientProps;
@property(nonatomic, assign) CGSize textGradientSize; @property(nonatomic, assign) CGSize textGradientSize;
@property(nonatomic, assign) CGFloat textSize;
@end @end
@implementation DoricTextNode @implementation DoricTextNode
@ -84,7 +83,6 @@ - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)pro
} else { } else {
view.font = [UIFont systemFontOfSize:[(NSNumber *) prop floatValue]]; view.font = [UIFont systemFontOfSize:[(NSNumber *) prop floatValue]];
} }
self.textSize = [(NSNumber *) prop floatValue];
} else if ([name isEqualToString:@"textColor"]) { } else if ([name isEqualToString:@"textColor"]) {
if ([prop isKindOfClass:[NSNumber class]]) { if ([prop isKindOfClass:[NSNumber class]]) {
view.textColor = DoricColor(prop); view.textColor = DoricColor(prop);
@ -148,12 +146,14 @@ - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)pro
withContext:self.doricContext] fetch]; withContext:self.doricContext] fetch];
[asyncResult setResultCallback:^(NSData *fontData) { [asyncResult setResultCallback:^(NSData *fontData) {
[self.doricContext dispatchToMainQueue:^{ [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) { [asyncResult setExceptionCallback:^(NSException *e) {
DoricLog(@"Cannot load resource %@, %@", prop, e.reason); 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"]) { } else if ([name isEqualToString:@"lineSpacing"]) {
[[self ensureParagraphStyle] also:^(NSMutableParagraphStyle *it) { [[self ensureParagraphStyle] also:^(NSMutableParagraphStyle *it) {
@ -326,6 +326,8 @@ - (void)requestLayout {
- (UIFont *)registerFontWithFontData:(NSData *)fontData fontSize:(CGFloat)fontSize{ - (UIFont *)registerFontWithFontData:(NSData *)fontData fontSize:(CGFloat)fontSize{
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData); CGDataProviderRef fontDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData);
CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider); CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider);
// THE NEXT LINE IS RELEVANT PART
// https://stackoverflow.com/questions/24900979/cgfontcreatewithdataprovider-hangs-in-airplane-mode
[UIFont familyNames]; [UIFont familyNames];
CGDataProviderRelease(fontDataProvider); CGDataProviderRelease(fontDataProvider);
CTFontManagerRegisterGraphicsFont(fontRef, NULL); CTFontManagerRegisterGraphicsFont(fontRef, NULL);