Reputation: 1502
For a small side project, I need to have all glyph names available in a font. So, I thought to make this:
CTFontRef f = CTFontCreateWithName(CFSTR("Zapfino"), 40, NULL);
CFIndex count = CTFontGetGlyphCount(f);
printf("There are %ld glyphs\n", count);
for (CGGlyph i = 1; i < count + 1; i++)
{
CTGlyphInfoRef gi = CTGlyphInfoCreateWithGlyph(i, f, CFSTR("(c)"));
CFStringRef name = CTGlyphInfoGetGlyphName(gi);
CGFontIndex idx = CTGlyphInfoGetCharacterIdentifier(gi);
printf("Glyph: %4hu, idx: %4hu ", i, idx);
CFShow(name);
printf("\n");
}
Creating the font and getting the glyph count works just fine. The name of the glyph is always NULL though. The CTGlyphInfoRef variable 'gi' gets a value. Any thoughts? What am I missing here?
Upvotes: 2
Views: 1005
Reputation: 90521
You've misunderstood what CTGlyphInfo
is about. It's not a way to query for information about a glyph. It's a representation of a substitution of a glyph for a string. You can set a glyph info object as the value for the kCTGlyphInfoAttributeName
attribute in an attributed string. That glyph then replaces that range of attributed text, if the text matches the base string.
You can use CGFontCopyGlyphNameForGlyph()
to get the name for a glyph from a font. You can create the CGFontRef
directly using the CGFontCreate...()
functions. You can also create one from a CTFont
using CTFontCopyGraphicsFont()
.
Upvotes: 5