OSX: CGGlyph to UniChar

妖精的绣舞 提交于 2019-12-01 22:44:34

问题


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!


回答1:


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.


来源:https://stackoverflow.com/questions/18238804/osx-cgglyph-to-unichar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!