Non Standard Font

Hi there,

does anyone know if it’s possible to include a font in the App package so the app can use a font that’s not osx default included?

Micha

Hi Micha,

it is possible - best you add the font(s) to the resources of your project …
Then you can use the code below to activate it/them from within your application:

add a new Objective-C class file named “bundleFonts.h/.m” (for example) and paste this code:

//  bundleFonts.h

#import <Cocoa/Cocoa.h>

@interface bundleFonts : NSObject {}
+(BOOL)openFontAtPath:(NSString *)fontPath;
@end
//  bundleFonts.m

#import "bundleFonts.h"

@implementation bundleFonts

+(BOOL)openFontAtPath:(NSString *)fontPath{

	FSRef fsRef;
	FSSpec fileSpec;
	
	CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, fontPath, kCFURLPOSIXPathStyle,NULL);
	if (url == NULL) { return(NO);}

	if (CFURLGetFSRef(url, &fsRef) == NULL){ 
		CFRelease(url);
		return(NO);
	}
	if (FSGetCatalogInfo (&fsRef, kFSCatInfoNone, NULL, NULL, &fileSpec,NULL) != noErr) {
		CFRelease(url);
		return(NO);
	}
	CFRelease(url);

	// ########## edit: ############

	// OSStatus status = FMActivateFonts(&fileSpec, NULL, NULL, kFMLocalActivationContext); // still working, but deprecated since 10.4

	//use this insted, if >= 10.4:
	ATSFontContainerRef oContainer;
	OSStatus status = ATSFontActivateFromFileSpecification(&fileSpec, kATSFontContextLocal,kATSFontFormatUnspecified, NULL,kATSOptionFlagsDefault, &oContainer);

	// ######## end edit: ##########

	if (status == noErr) {
		return(YES);
	} else {
		return(NO);
	}
}

@end

Now you can activate (and use) your bundeled font from applescript - theFontPath is a POSIX path to your font (String - non-quoted):


	if ((call method "openFontAtPath:" of class "bundleFonts" with parameter theFontPath) as boolean) then
		log "font successfully activated"
	else
		log "font activation failed"
	end if
	-- use the new font(s) ....

Hope that helps,

D.