Question

I am currently writing a simple bitmap font generator using CoreGraphics and CoreText. I am retrieving the kerning table of a font with:

CFDataRef kernTable = CTFontCopyTable(m_ctFontRef, kCTFontTableKern, kCTFontTableOptionNoOptions);

and then parse it which works fine. The kerning pairs give me the glyph indices (i.e. CGGlyph) for the kerning pairs, and I need to translate them to unicode (i.e. UniChar), which unfortunately does not seem super easy. The closest I got was using:

CGFontCopyGlyphNameForGlyph

to retrieve the glyph name of the CGGlyph, but I don't know how to convert the name to unicode, as they are really just strings such as quoteleft. Another thing I though about was parsing the kCTFontTableCmap myself to manually do the mapping from the glyph to the unicode id, but that seems to be a ton of extra work for the task. Is there any simple way of doing this?

Thanks!

Was it helpful?

Solution

I don't know a direct method to get the Unicode for a given glyph, but you could build a mapping in the following way:

  • Get all characters of the font with CTFontCopyCharacterSet().
  • Map all these Unicode characters to their glyph with CTFontGetGlyphsForCharacters().
  • For each Unicode character and its glyph, store the mapping glyph -> Unicode in a dictionary.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top