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

Commit

Permalink
[iOS] Fix bug that iconfonts with the same family name but different …
Browse files Browse the repository at this point in the history
…files cannot upgrade. (#2433)
  • Loading branch information
wqyfavor authored and jianhan-he committed May 14, 2019
1 parent 8772d40 commit 21e7ba4
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions ios/sdk/WeexSDK/Sources/Utility/WXUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,12 @@ + (UIFont *)fontWithSize:(CGFloat)size textWeight:(CGFloat)textWeight textStyle:

+ (UIFont *)fontWithSize:(CGFloat)size textWeight:(CGFloat)textWeight textStyle:(WXTextStyle)textStyle fontFamily:(NSString *)fontFamily scaleFactor:(CGFloat)scaleFactor useCoreText:(BOOL)useCoreText
{
static NSMutableDictionary* RegisteredFontFileNames;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
RegisteredFontFileNames = [[NSMutableDictionary alloc] init];
});

CGFloat fontSize = (isnan(size) || size == 0) ? 32 * scaleFactor : size;
UIFont *font = nil;

Expand All @@ -507,11 +513,53 @@ + (UIFont *)fontWithSize:(CGFloat)size textWeight:(CGFloat)textWeight textStyle:
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL(fontURL);
if (fontDataProvider) {
CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
CTFontManagerRegisterGraphicsFont(newFont, NULL);
fontFamily = (__bridge_transfer NSString*)CGFontCopyPostScriptName(newFont);
CGFontRelease(newFont);
CFRelease(fontURL);
CFRelease(fontDataProvider);
if (newFont) {
fontFamily = (__bridge_transfer NSString*)CGFontCopyPostScriptName(newFont);
CFErrorRef error = NULL;
CTFontManagerRegisterGraphicsFont(newFont, &error);

if (error == NULL) {
// record which file is registered to this family name
@synchronized (RegisteredFontFileNames) {
RegisteredFontFileNames[fontFamily] = fpath;
}
}

if ([fontFamily isEqualToString:@"iconfont"]) {
WXLogError(@"Using iconfont with family name 'iconfont' is prohibited.");
if (error) {
WXLogError(@"Unable to register font, %@.", fontFamilyDic);
CFRelease(error);
}
}
else {
// Check if we have already registered another font file with the same family-name
if (error) {
@synchronized (RegisteredFontFileNames) {
NSString* previousFilePath = RegisteredFontFileNames[fontFamily];
if (![previousFilePath isEqualToString:fpath]) {
// file path changed means a new iconfont file is registered with the same name, we use it
WXLogError(@"Unable to register font, but will unregister previous one and retry with new font file, %@.", fontFamilyDic);
CTFontManagerUnregisterGraphicsFont(newFont, NULL);
CFErrorRef error2 = NULL;
CTFontManagerRegisterGraphicsFont(newFont, &error2);
if (error2) {
WXLogError(@"We cannot register the font finally. %@", error2);
CFRelease(error2);
}
else {
RegisteredFontFileNames[fontFamily] = fpath;
}
}
}
CFRelease(error);
}
}

CGFontRelease(newFont);
CFRelease(fontURL);
CFRelease(fontDataProvider);
}
}
} else {
CTFontManagerRegisterFontsForURL(fontURL, kCTFontManagerScopeProcess, NULL);
Expand Down

0 comments on commit 21e7ba4

Please sign in to comment.