Last active
August 29, 2015 14:04
-
-
Save jjgod/4296416b06b39ef49206 to your computer and use it in GitHub Desktop.
Test Core Text fallback line height.
This file contains hidden or 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
// Compile with: clang fallback.m -framework CoreGraphics -framework CoreText -framework Foundation -o fallback | |
// Run with: ./fallback "Fallback Font Family" "Text to Typeset" | |
#import <ApplicationServices/ApplicationServices.h> | |
#import <Foundation/Foundation.h> | |
int main(int argc, char *argv[]) | |
{ | |
if (argc != 3) | |
return 0; | |
CTFontRef systemFont = CTFontCreateUIFontForLanguage(kCTFontSystemFontType, 12.0, CFSTR("en-US")); | |
CFStringRef name = CTFontCopyFamilyName(systemFont); | |
CFMutableDictionaryRef attributes = | |
CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, | |
&kCFTypeDictionaryValueCallBacks); | |
CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, name); | |
CFRelease(name); | |
const void *descriptors[] = { CTFontDescriptorCreateWithNameAndSize((CFStringRef)[NSString stringWithUTF8String:argv[1]], 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 = @{ (id)kCTFontAttributeName: (id)font }; | |
CFRelease(font); | |
CFAttributedStringRef attrStr = | |
CFAttributedStringCreate(kCFAllocatorDefault, | |
(CFStringRef)[NSString stringWithUTF8String:argv[2]], // Test two lines: CFSTR("test\n中文"), | |
(CFDictionaryRef)stringAttributes); | |
CFRelease(stringAttributes); | |
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrStr); | |
CGPathRef path = CGPathCreateWithRect(CGRectMake(0, 0, 100, 200), NULL); | |
CTFrameRef frame = | |
CTFramesetterCreateFrame(framesetter, | |
CFRangeMake(0, CFAttributedStringGetLength(attrStr)), | |
path, | |
NULL); | |
CFRelease(path); | |
CFRelease(attrStr); | |
CFArrayRef lines = CTFrameGetLines(frame); | |
for (CFIndex i = 0; i < CFArrayGetCount(lines); i++) { | |
CTLineRef line = CFArrayGetValueAtIndex(lines, i); | |
CFShow(line); | |
} | |
CFRelease(frame); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment