CTFontManagerRegisterGraphicsFont does not register last name

For licensing reasons, I have to decrypt the font file and load it into memory, and then register it instead of reading directly from the URL. For this I have to use CTFontManagerRegisterGraphicsFont .

The problem part occurs when I try to use [NSFont fontWithName:@"Open Sans" size:21.0] , where it will only accept the name of the PostScript font (ie ("OpenSans or OpenSans-Bold for Bold Weight") and will not work with the surname "Open Sans".

If the font is registered using ATSApplicationFontsPath in the ATSApplicationFontsPath file or using CTFontManagerRegisterFontsForURLs , then I can use the font name. But I can’t do it.

("Open Sans" is used here as an example)

Here is the relevant code that I use to register the font.

 NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"OpenSans-Regular" ofType:@"ttf" inDirectory:@"Fonts"]; NSData *fontData = [NSData dataWithContentsOfFile:fontPath]; CGDataProviderRef fontProviderRef = CGDataProviderCreateWithCFData((CFDataRef)fontData); CGFontRef fontRef = CGFontCreateWithDataProvider(fontProviderRef); CFErrorRef error; if (! CTFontManagerRegisterGraphicsFont(fontRef, &error)) { CFStringRef errorDescription = CFErrorCopyDescription(error); NSLog(@"Failed to load font: %@", errorDescription); CFRelease(errorDescription); } CFRelease(fontRef); CFRelease(fontProviderRef); 

Is there a way to make the font family name available to NSFont using CTFontManagerRegisterGraphicsFont ?

(Using Xcode 5 and targeting on Mac OS X> = 10.8)

+7
objective-c cocoa macos
source share
1 answer

update:

Well, to make sure the last name comes from the font file itself: I tried this with the font that I have ... GillSansMTStd-Light. I changed the last name in the font file to "Gill Sans Std Mt Light" and subsequently I could use [ NSFont fontWithName:@"Gill Sans MT Std Light" size:10.0 ] .

fontforge font panel:

enter image description here

before:

enter image description here

after:

enter image description here


I looked through all the font and CT / NS font API combinations that I could think of. There seems to be no API for associating the last name with the font - I think this data comes from the font file itself.

My guess is that the font name table should have a font family (Mac) and a font subfamily. (see https://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html )

I used FontForge to edit font files in the past - maybe this can help you? It seems you can install it through a homegrown.

Finally, I think there is an easier way to add a font to CTFontManager . I changed the code to:

 // register the font: NSURL * url = [[ NSBundle mainBundle ] URLForResource:@"OpenSans-Regular" withExtension:@"ttf" ] ; assert( url ) ; CFErrorRef error = 0 ; bool ok = CTFontManagerRegisterFontsForURL( (__bridge CFURLRef)url, kCTFontManagerScopeProcess, &error ) ; assert( ok && !error ) ; // make sure it registered (using PS name): CTFontRef f = CTFontCreateWithName( CFSTR( "OpenSans-Regular" ), 10.0, NULL ) ; assert( f ) ; 
+5
source share

All Articles