Skip to content

Instantly share code, notes, and snippets.

@jjgod
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjgod/4296416b06b39ef49206 to your computer and use it in GitHub Desktop.
Save jjgod/4296416b06b39ef49206 to your computer and use it in GitHub Desktop.
Test Core Text fallback line height.
// 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