CGFont:计算字形边界
如何获取字符串每个字形的大小(以像素为单位)? 我使用 CGFontGetGlyphBBoxes 获取字符串中每个字形的边界框并获取以下值:
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}}
如果我正确理解了以字体单位表示的这些值。这些值到底意味着什么以及如何将其转换为像素? CGFontGetGlyphAdvances 和 CGFontGetGlyphBBoxes 返回的值有什么区别?使用 CGFontGetGlyphAdvances 我得到以下信息:
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 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}}
例如,如果我想计算字符串的整个宽度(在我的例子中为“demo”),我应该使用什么值(bbox.size.width 或 advance)?
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我有用的是将每个字形的高级值相加。我认为边界框不会包含字形之间的适当间距。
您可以通过调用以下命令来获取将提前量转换为 em 的值:
从中您可以通过乘以字体大小来获取像素。但请对此持保留态度,因为我不完全确定这是否适用于所有可能的情况。
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:
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.