Skip to content

Instantly share code, notes, and snippets.

@jjgod
Created April 19, 2012 08:32
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jjgod/2419683 to your computer and use it in GitHub Desktop.
Save jjgod/2419683 to your computer and use it in GitHub Desktop.
Use cascade list attribute to customize font fallback in Core Text
#import <ApplicationServices/ApplicationServices.h>
#import <Foundation/Foundation.h>
int main(int argc, char *argv[])
{
if (argc != 2)
return 0;
NSAutoreleasePool *pool = [NSAutoreleasePool new];
CFStringRef name = (CFStringRef) [NSString stringWithUTF8String: argv[1]];
CFMutableDictionaryRef attributes = CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, name);
const void *descriptors[] = { CTFontDescriptorCreateWithNameAndSize(CFSTR("STHeitiSC-Light"), 0) };
CFArrayRef list = CFArrayCreate(kCFAllocatorDefault, descriptors, 1, &kCFTypeArrayCallBacks);
CFDictionaryAddValue(attributes, kCTFontCascadeListAttribute, list);
CFRelease(list);
CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes(attributes);
CFRelease(attributes);
CTFontRef font = CTFontCreateWithFontDescriptor(descriptor, 0.0, 0);
CFRelease(descriptor);
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject: (id) font
forKey: (NSString *) kCTFontAttributeName];
CFRelease(font);
CFAttributedStringRef attrStr = CFAttributedStringCreate(kCFAllocatorDefault, CFSTR("test中文"), (CFDictionaryRef) stringAttributes);
CFRelease(stringAttributes);
CTLineRef line = CTLineCreateWithAttributedString(attrStr);
CFRelease(attrStr);
CFShow(line);
CFRelease(line);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment