Re:Loading Fonts from the App Bundle
Re:Loading Fonts from the App Bundle
- Subject: Re:Loading Fonts from the App Bundle
- From: Bela Bernat Hackman <email@hidden>
- Date: Sat, 7 Dec 2002 11:27:46 -0600
Try this (minus error checking of course):
// Get the main bundle object
NSBundle * appBundle = [NSBundle mainBundle];
// Ask for the path to the resources
NSString * fontsPath = [appBundle resourcePath];
// Using the Carbon APIs: get a file reference and spec for the path
FSRef fsRef;
FSSpec fsSpec;
int osstatus = FSPathMakeRef( [fontsPath UTF8String], &fsRef, NULL);
if ( osstatus == noErr)
{
osstatus = FSGetCatalogInfo( &fsRef, kFSCatInfoNone, NULL, NULL,
&fsSpec, NULL);
}
//activate the font file using the file spec
osstatus = FMActivateFonts( &fsSpec, NULL, NULL,
kFMGlobalActivationContext);
Comments:
1) Make sure the font files are actually copied to the Resource
directory of the bundle not just referenced. Copying the font files to
the bundle in the Finder will do this. Project Builder appears to place
file references in the bundle which aren't recognised as font files by
the Carbon Font Manager and hence activation doesn't occur. I haven't
figured out how to change this behaviour of PB yet.
2) The Carbon Font Manager activates all fonts in a container (ie font
file) as a group. There is no way to activate a single font within a
font file (eg activating only Cochin-Bold from the Cochin file also
containing Cochin-Roman and Cochin-Italic).
3) The code above activates all the font containers in the bundle. To
activate individual containers use [appBundle
pathForResource:@"FontFileName" ofType:/*@"otf" or @"ttf" or @"suit"
etc*/]. You may pass ofType:nil may be used if the font file name is
unique ie you aren't including different formats with the same name.
4) Local activation is accomplished by passing
kFMLocalActivationContext to FMActivateFonts.
5) To my knowlege there is no way to activate fonts directly with the
Cocoa API.
6) A method using the Core Foundation API is outlined in "Managing
Fonts With the Font Manager" available as html and pdf at:
http://developer.apple.com/techpubs/macosx/Carbon/text/FontManager/
fontmanager.html. This works just as well but is not as Cocoa-ish.
Regards,
Bela
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.