在核心文本中查找文本的边界矩形
我试图在核心文本中找到一行文本的边界。为简单起见,假设它只有一个字符。 目前我正在使用以下方法:
line = CTLineCreateWithAttributedString(attrString);
rect = CTLineGetImageBounds(line, context);
它在大多数情况下都有效,但对于某些字符,例如数学斜体 d (Unicode:0x1D451)或数学斜体 q (Unicode:0x1D45E),宽度有点短。
我尝试使用 CTLineGetTypgraphicBounds()
或 CTFramesetterSuggestFrameSizeWithConstraints
,但它们也没有帮助(我认为它们使用字形的提前来查找宽度,而不是其图形宽度。)字体本身不是斜体,我也无法使用倾斜角度来纠正这个问题。
我尝试直接访问字形并使用 CTFontCreatePathForGlyph()
,但失败了,因为 CGGlyph 和 UniChar 都是 16 位,而我需要 32 位字符。
有谁知道我做错了什么吗?如果是这样,正确的方法是什么?
更新:
为了确保这不是与字体相关的问题,我使用了不同的字体。即使对于 Arial Italic 中的 ASCII 字符也存在该问题。
更新:
忽略我之前的更新!事实证明,这个问题(Arial Italic 的问题)是我自己的错误。但第一个问题仍然存在。
I'm trying to find the boundaries of a line of text in Core Text. For simplicity, assume it has a single character.
At the moment I'm using the following method:
line = CTLineCreateWithAttributedString(attrString);
rect = CTLineGetImageBounds(line, context);
It works most of the times, but for some characters, like math italic d (Unicode: 0x1D451) or math italic q (Unicode: 0x1D45E), the width is a bit short.
I tried using CTLineGetTypographicBounds()
or CTFramesetterSuggestFrameSizeWithConstraints
, but they didn't help either (I think they use glyph's advance to find the width, not its graphical width.) As the font itself isn't italic, I also can't use slant angle to correct this.
I tried accessing the glyphs directly and using CTFontCreatePathForGlyph()
, but failed as CGGlyph and UniChar are both 16-bits and I need 32-bit characters.
Does anyone know if I'm doing anything wrong? If so, what's the right way?
Update:
To make sure it's not a problem related to the font, I used different fonts. The problem exists even for ASCII characters in Arial Italic.
Update:
Ignore my previous update! It turns out this one (problem with Arial Italic) was my own bug. The first problem still stands though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我所做的错误报告,显然这是一个 CoreText 问题,只在极少数情况下出现。一旦找到修复方法,我会立即通知您。
更新:2012 年 3 月 15 日
我忘记提前更新此问题,但已修复。该修复已在 OS X Lion / iOS 5 中进行。
Following the bug report I made, apparently this is a CoreText problem, appearing in rare cases. Will let you know as soon as I find a fix.
Update: March 15 '12
I forgot to update this sooner, but this is fixed. The fix is in OS X Lion / iOS 5.
据我所知, CTLineGetImageBounds 是正确使用的函数。我建议在 RadarWeb 中提交错误报告。
As far as I can tell,
CTLineGetImageBounds
is the correct function to use. I suggest filing a bug report in RadarWeb.