Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

* [ios] bugfix iconfont load, support woff #1804

Merged
merged 2 commits into from
Dec 5, 2016
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
5 changes: 1 addition & 4 deletions ios/sdk/WeexSDK/Sources/Handler/WXNavigationDefaultImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ - (void)pushViewControllerWithParam:(NSDictionary *)param completion:(WXNavigati
animated = NO;
}

NSString *url = param[@"url"] ;
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

WXBaseViewController *vc = [[WXBaseViewController alloc]initWithSourceURL:[NSURL URLWithString:url]];
WXBaseViewController *vc = [[WXBaseViewController alloc]initWithSourceURL:[NSURL URLWithString:param[@"url"]]];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

取消 对 url 的utf-8 编码, 这个控制由前端同学来做,

vc.hidesBottomBarWhenPushed = YES;
[container.navigationController pushViewController:vc animated:animated];
[self callback:block code:MSG_SUCCESS data:nil];
Expand Down
18 changes: 2 additions & 16 deletions ios/sdk/WeexSDK/Sources/Manager/WXRuleManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,34 +84,20 @@ - (void)addRule:(NSString*)type rule:(NSDictionary *)rule
return;
}
[fontFamily setObject:fontSrc forKey:@"src"];
if ([fontURL isFileURL]) {
// local font file will be added directly if existed
if ([WXUtility isFileExist:[fontURL path]]) {
[fontFamily setObject:fontURL forKey:@"localSrc"];
[_fontStorage setObject:fontFamily forKey:rule[@"fontFamily"]];
} else {
WXLogWarning(@"font file %@ is not exist", fontSrc);
}
return;
}
[_fontStorage setObject:fontFamily forKey:rule[@"fontFamily"]];
// remote font file
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对fileURL 的处理逻辑加到了下载fontURL中 getIconfont:

NSString *fontfile = [NSString stringWithFormat:@"%@/%@",WX_FONT_DOWNLOAD_DIR,[WXUtility md5:fontURL.path]];
NSString *fontfile = [NSString stringWithFormat:@"%@/%@",WX_FONT_DOWNLOAD_DIR,[WXUtility md5:[fontURL absoluteString]]];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kfeagle 更新为使用 url absoluteString

if ([WXUtility isFileExist:fontfile]) {
// if has been cached, load directly
[fontFamily setObject:[NSURL fileURLWithPath:fontfile] forKey:@"localSrc"];
[_fontStorage setObject:fontFamily forKey:rule[@"fontFamily"]];
return;
}

[_fontStorage setObject:fontFamily forKey:rule[@"fontFamily"]];

__weak typeof(self) weakSelf = self;
[WXUtility getIconfont:fontURL completion:^(NSURL * _Nonnull url, NSError * _Nullable error) {
if (!error && url) {
// load success
NSMutableDictionary * dictForFontFamily = [weakSelf.fontStorage objectForKey:rule[@"fontFamily"]];
[dictForFontFamily setObject:url forKey:@"localSrc"];
[weakSelf.fontStorage setObject:url forKey: dictForFontFamily];
} else {
//there was some errors during loading
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove unused

WXLogError(@"load font failed %@",error.description);
Expand Down
41 changes: 26 additions & 15 deletions ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,15 @@ + (UIFont *)fontWithSize:(CGFloat)size textWeight:(WXTextWeight)textWeight textS
if (fontFamilyDic[@"localSrc"]){
NSString *fpath = [((NSURL*)fontFamilyDic[@"localSrc"]) path];
if ([self isFileExist:fpath]) {
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fpath UTF8String]);
CGFontRef customfont = CGFontCreateWithDataProvider(fontDataProvider);

// if the font file is not the correct font file. it will crash by singal 9
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种加载font 的方式不支持,woff 字体格式,换成 CoreText支持

CFURLRef fontURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (__bridge CFStringRef)fpath, kCFURLPOSIXPathStyle, false);
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL(fontURL);
CFRelease(fontURL);
CGFontRef graphicFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
NSString *fontName = (__bridge NSString *)CGFontCopyFullName(customfont);
CFErrorRef error;
CTFontManagerRegisterGraphicsFont(customfont, &error);
if (error){
CTFontManagerUnregisterGraphicsFont(customfont, &error);
CTFontManagerRegisterGraphicsFont(customfont, &error);
}
CGFontRelease(customfont);
font = [UIFont fontWithName:fontName size:fontSize];
CTFontRef smallFont = CTFontCreateWithGraphicsFont(graphicFont, size, NULL, NULL);
CFRelease(graphicFont);
font = (__bridge UIFont*)smallFont;
}else {
[[WXRuleManager sharedInstance] removeRule:@"fontFace" rule:@{@"fontFamily": fontFamily}];
}
Expand Down Expand Up @@ -396,12 +392,23 @@ + (UIFont *)fontWithSize:(CGFloat)size textWeight:(WXTextWeight)textWeight textS

+ (void)getIconfont:(NSURL *)url completion:(void(^)(NSURL *url, NSError *error))completionBlock
{
if ([url isFileURL]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

增加FileURL 处理

// local file url
NSError * error = nil;
if (![WXUtility isFileExist:url.absoluteString]) {
error = [NSError errorWithDomain:WX_ERROR_DOMAIN code:-1 userInfo:@{@"errMsg":[NSString stringWithFormat:@"local font %@ is't exist", url.absoluteString]}];
}
completionBlock(url, error);
return;
}

// remote font url
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSURL * downloadPath = nil;
if (!error && location) {
NSString *file = [NSString stringWithFormat:@"%@/%@",WX_FONT_DOWNLOAD_DIR,[WXUtility md5:[url path]]];

NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse*)response;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remote url is 404 , the error is nil , so add httpResponse status process

if (200 == httpResponse.statusCode && !error && location) {
NSString *file = [NSString stringWithFormat:@"%@/%@",WX_FONT_DOWNLOAD_DIR,[WXUtility md5:[url absoluteString]]];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kfeagle 更新为 使用 url absoluteString

downloadPath = [NSURL fileURLWithPath:file];
NSFileManager *mgr = [NSFileManager defaultManager];
NSError * error ;
Expand All @@ -413,6 +420,10 @@ + (void)getIconfont:(NSURL *)url completion:(void(^)(NSURL *url, NSError *error)
if (!result) {
downloadPath = nil;
}
} else {
if (200 != httpResponse.statusCode) {
error = [NSError errorWithDomain:WX_ERROR_DOMAIN code:-1 userInfo:@{@"ErrorMsg": [NSString stringWithFormat:@"can not load the font url %@ ", url.absoluteString]}];
}
}
completionBlock(downloadPath, error);
}];
Expand Down