CGFont:计算字形边界

发布于 2024-11-01 17:27:13 字数 1053 浏览 0 评论 0原文

如何获取字符串每个字形的大小(以像素为单位)? 我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸 2024-11-08 17:27:13

对我有用的是将每个字形的高级值相加。我认为边界框不会包含字形之间的适当间距。

您可以通过调用以下命令来获取将提前量转换为 em 的值:

CGFontGetUnitsPerEm(cgfont);

从中您可以通过乘以字体大小来获取像素。但请对此持保留态度,因为我不完全确定这是否适用于所有可能的情况。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文