-
-
Save cxa/2658318 to your computer and use it in GitHub Desktop.
Use cascade list attribute to customize font fallback in Core Text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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