开发者

CGFont: calculation glyphs bounds

How can I get sizes of each glyph of the string in pixels? I use CGFontGetGlyphBBoxes to get a bounding box of each glyph in string and get following values:

2011-04-18 15:34:56.809 TextSize[3604:207] bbox['d'] = {{56, -38}, {949, 1512}}
2011-04-18 15:34:56.811 TextSize[3604:207] bbox['e'] = {{72, -38}, {978, 1135}}
2011-04-18 15:34:56.811 TextSize[3604:207] bbox['m'] = {{132, 0}, {1441, 1095}}
2011-04-18 15:34:56.812 TextSize[3604:207] bbox['o'] = {{59, -39}, {998, 1141}}

If I have correct understanding these values presented in font units. What exactly these values means and how can I translate it to pixels? What is the difference between values returned by CGFontGetGlyphAdvances and CGFontGetGlyphBBoxes? Using CGFontGetGlyphAdvances I get the following:

2011-04-18 15:34:56.809 TextSize[3604:207] advance['d'] = 1139, bbox = {{56, -38}, {949, 1512}}
2011-04-18 15:34:56.811 TextSize[3604:207] advance['e'] = 1139, bbox = {{72, -38}, {978, 1135}}
2011-04-18 15:34:56.811开发者_如何学Go TextSize[3604:207] advance['m'] = 1706, bbox = {{132, 0}, {1441, 1095}}
2011-04-18 15:34:56.812 TextSize[3604:207] advance['o'] = 1139, bbox = {{59, -39}, {998, 1141}}

If for example I want to calculate the whole width of the string (in my case 'demo'), what values (bbox.size.width or advance) should I use?


What worked for me is to add up the advance values for each glyph. I don't think the bounding box will include the proper spacing between the glyphs.

You can obtain the value to convert the advance to em by calling:

CGFontGetUnitsPerEm(cgfont);

From that you could get the pixels by multiplying with the font size. But take that with a grain of salt, as I am not entirely sure if that will work in all possible cases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜