Skip to content

Instantly share code, notes, and snippets.

@jjgod
Created January 22, 2009 06:38
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/50453 to your computer and use it in GitHub Desktop.
Save jjgod/50453 to your computer and use it in GitHub Desktop.
NSString *message = [NSString stringWithUTF8String: "Hello"];
int count = [message length];
UniChar *characters = (UniChar *) malloc(count * sizeof(UniChar));
CGGlyph *glyphs = (CGGlyph *) malloc(count * sizeof(CGGlyph));
int *advances = (int *) malloc(count * sizeof(int ));
CGSize *cgAdvances = (CGSize *) malloc(count * sizeof(CGSize));
/* Get the characters, glyphs and advances and calculate aStringWidth. */
[message getCharacters: characters];
CGFontGetGlyphsForUnichars(font, characters, glyphs, count);
CGFontGetGlyphAdvances(font, glyphs, count, advances);
int unitsPerEm = CGFontGetUnitsPerEm(font);
int i;
for (i = 0; i < count; i++)
{
NSLog(@"glyph[%d] = %u, advances[%d] = %d\n", i, glyphs[i], i, advances[i]);
cgAdvances[i].width = advances[i] * fontSize / unitsPerEm;
NSLog(@"advance: %g", cgAdvances[i].width);
cgAdvances[i].height = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment