iPhone SDK 石英字形绘图
我试图在特定点以一定的精度渲染字体字形,但遇到了很多麻烦。
首先,文本位置 Y 值永远不会改变,因此我根本无法获取字形的高度。例如,在下面的代码中,断言总是触发:
CGContextSelectFont(g, "Arial", 32.0f, kCGEncodingFontSpecific);
char c = 'A';
CGPoint pt = CGPointMake(400, 400);
CGContextSetTextDrawingMode(g, kCGTextInvisible);
CGContextSetTextPosition(g, pt.x, pt.y);
CGContextShowText(g, &c, 1);
CGPoint pt2 = CGContextGetTextPosition(g);
assert(pt2.y != pt.y);
我尝试使用一些硬编码的宽高比来解决这个问题,假设字形线性缩放。请注意,文本位置的 X 值已正确更新。
然而,更重要的是字形如何相对于文本位置呈现;有些字形以 Y 值作为字形的中心进行渲染,有些使用 Y 值作为字形的顶部,而另一些则使用 Y 值作为字形的底部。我无法对所有这些数据进行硬编码,并希望今年的任何时候都能完成我的应用程序。
上面的绘图代码中我做错了什么吗?我在 Quartz 文档中找不到任何与垂直对齐相关的参考,并且有点迷失。
I am trying to render font glyphs at a specific point with some precision, and am having quite a bit of trouble with it.
To begin with, the Text Position Y value never changes, so I cannot get the glyph's height at all. For example, in the following code, the assertion always fires:
CGContextSelectFont(g, "Arial", 32.0f, kCGEncodingFontSpecific);
char c = 'A';
CGPoint pt = CGPointMake(400, 400);
CGContextSetTextDrawingMode(g, kCGTextInvisible);
CGContextSetTextPosition(g, pt.x, pt.y);
CGContextShowText(g, &c, 1);
CGPoint pt2 = CGContextGetTextPosition(g);
assert(pt2.y != pt.y);
I have tried to work around this using some hard-coded width-to-height ratios, assuming the glyphs scale linearly. Note that the X value of the text position is updated correctly.
More importantly, however, is how the glyph renders in relation to the Text Position; some glyphs render with the Y value as the center of the glyph, some use the Y value as the top of the glyph, and others use the Y value as the bottom of the glyph. I can't hard-code all this data and hope to get my app done any time this year.
Is there anything I am doing incorrectly in the drawing code above? I can't find any reference in the Quartz docs related to vertical alignment, and am a little lost.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,这是一项相当复杂的任务。我会查看 zynga 的 LabelFont 库,然后从他们的代码中学习或简单地使用该库。特别是看看 LabelFont 类的
drawTextInRect:
方法。From what I've gleamed this is a fairly complex task. I'd check out zynga's LabelFont library and either learn from their code or simply use the library. Especially have a look at the LabelFont class'
drawTextInRect:
method.