Skip to content

Instantly share code, notes, and snippets.

@darvin
Created September 5, 2014 22:12
Show Gist options
  • Save darvin/e6b839ad323f847dd758 to your computer and use it in GitHub Desktop.
Save darvin/e6b839ad323f847dd758 to your computer and use it in GitHub Desktop.
diff --git a/cocos2d/CCLabelTTF.h b/cocos2d/CCLabelTTF.h
index 062618c..fad3df1 100644
--- a/cocos2d/CCLabelTTF.h
+++ b/cocos2d/CCLabelTTF.h
@@ -252,7 +252,7 @@
*
* @param fontFile Font file path.
*/
-+(void) registerCustomTTF:(NSString*)fontFile;
++(NSString*) registerCustomTTF:(NSString*)fontFile;
@end
diff --git a/cocos2d/CCLabelTTF.m b/cocos2d/CCLabelTTF.m
index 9d641d2..950f3bd 100644
--- a/cocos2d/CCLabelTTF.m
+++ b/cocos2d/CCLabelTTF.m
@@ -167,10 +167,9 @@ static __strong NSMutableDictionary* ccLabelTTF_registeredFonts;
- (void)setFontName:(NSString*)fontName
{
// Handle passing of complete file paths
- if ([[[fontName pathExtension] lowercaseString] isEqualToString:@"ttf"])
+ if ([[[fontName pathExtension] lowercaseString] isEqualToString:@"ttf"] || [[[fontName pathExtension] lowercaseString] isEqualToString:@"otf"])
{
- [CCLabelTTF registerCustomTTF:fontName];
- fontName = [[fontName lastPathComponent] stringByDeletingPathExtension];
+ fontName = [CCLabelTTF registerCustomTTF:fontName];
}
if( fontName.hash != _fontName.hash ) {
@@ -1011,7 +1010,8 @@ static __strong NSMutableDictionary* ccLabelTTF_registeredFonts;
#pragma mark Class functions
-+ (void) registerCustomTTF:(NSString *)fontFile
+
++ (NSString*) registerCustomTTF:(NSString *)fontFile
{
// Do not register a font if it has already been registered
if (!ccLabelTTF_registeredFonts)
@@ -1019,17 +1019,45 @@ static __strong NSMutableDictionary* ccLabelTTF_registeredFonts;
ccLabelTTF_registeredFonts = [[NSMutableDictionary alloc] init];
}
- if ([ccLabelTTF_registeredFonts objectForKey:fontFile]) return;
- [ccLabelTTF_registeredFonts setObject:[NSNumber numberWithBool:YES] forKey:fontFile];
+ if ([ccLabelTTF_registeredFonts objectForKey:fontFile]) return [ccLabelTTF_registeredFonts objectForKey:fontFile];
+
// Register with font manager
- if ([[fontFile lowercaseString] hasSuffix:@".ttf"])
+ if ([[fontFile lowercaseString] hasSuffix:@".ttf"] || [[fontFile lowercaseString] hasSuffix:@".otf"])
{
// This is a file, register font with font manager
NSString* fontPath = [[CCFileUtils sharedFileUtils] fullPathForFilename:fontFile];
+ NSCAssert(fontPath != nil, @"FontFile can not be located");
+
NSURL* fontURL = [NSURL fileURLWithPath:fontPath];
CTFontManagerRegisterFontsForURL((__bridge CFURLRef)fontURL, kCTFontManagerScopeProcess, NULL);
+ NSString *fontName = nil;
+#if __CC_PLATFORM_IOS && ! APPORTABLE
+ BOOL needsCGFontFailback = [[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending;
+#else
+ BOOL needsCGFontFailback = NO;
+#endif
+ if (!needsCGFontFailback) {
+ CFArrayRef descriptors = CTFontManagerCreateFontDescriptorsFromURL((__bridge CFURLRef)fontURL);
+ if (!descriptors || CFArrayGetCount(descriptors)<1) {
+ return nil;
+ }
+ CTFontDescriptorRef descriptor = CFArrayGetValueAtIndex(descriptors, 0);
+ fontName = (__bridge NSString *)CTFontDescriptorCopyAttribute(descriptor, kCTFontNameAttribute);
+ CFRelease(descriptors);
+
+ } else {
+ CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fontURL);
+ CGFontRef loadedFont = CGFontCreateWithDataProvider(fontDataProvider);
+ fontName = (__bridge NSString *)CGFontCopyPostScriptName(loadedFont);
+
+ CGFontRelease(loadedFont);
+ CGDataProviderRelease(fontDataProvider);
+ }
+
+ [ccLabelTTF_registeredFonts setObject:fontName forKey:fontFile];
+ return fontName;
}
+ return nil;
}
-
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment