Skip to content

Instantly share code, notes, and snippets.

@jjgod
Created November 23, 2014 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjgod/d1c0c116c359091338b7 to your computer and use it in GitHub Desktop.
Save jjgod/d1c0c116c359091338b7 to your computer and use it in GitHub Desktop.
Shape text with system font.
// systemfont.m
#import <Cocoa/Cocoa.h>
int main(int argc, char const *argv[]) {
NSFont* font = [NSFont menuFontOfSize:18.0];
CTFontRef ctFont = (CTFontRef)font;
CFStringRef str = CFSTR("Opera");
UniChar characters[5];
CFStringGetCharacters(str, CFRangeMake(0, 5), characters);
CGGlyph glyphs[5];
CTFontGetGlyphsForCharacters(ctFont, characters, glyphs, 5);
CGSize advances[5];
CTFontGetAdvancesForGlyphs(ctFont, kCTFontHorizontalOrientation, glyphs, advances, 5);
for (int i = 0; i < 5; i++)
printf("%d [%g] ", glyphs[i], advances[i].width);
printf("\n");
CFAttributedStringRef attrStr =
CFAttributedStringCreate(NULL, str,
(CFDictionaryRef)@{(NSString*)kCTFontAttributeName: font});
CTLineRef line = CTLineCreateWithAttributedString(attrStr);
CFArrayRef runs = CTLineGetGlyphRuns(line);
for (int i = 0; i < CFArrayGetCount(runs); i++) {
CTRunRef run = CFArrayGetValueAtIndex(runs, i);
CFIndex count = CTRunGetGlyphCount(run);
CGGlyph glyphs[100];
CTRunGetGlyphs(run, CFRangeMake(0, count), glyphs);
CGSize advances[100];
CTRunGetAdvances(run, CFRangeMake(0, count), advances);
for (int j = 0; j < count; j++)
printf("%d [%g] ", glyphs[j], advances[j].width);
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment