Java:如何找出字体的大写高度和x高度?
FontMetrics 没有 大写高度 和 字体的 x 高度。
如何获得这些值?
就大写字母高度而言,无法保证特定大写字母的字母高度与大写字母高度相同。 (例如,大写的 H 不能保证顶部平坦)
就 x 高度而言,我认为它可能与“x”的高度相同,但同样,没有保证。
编辑:哎呀!我刚刚尝试过 FontMetrics.getBounds()
和 FontMetrics.getLineMetrics()
对于特定的字符序列,我总是得到相同的高度答案(显然,getBounds() 对于宽度确实有所不同)。在
hasUniformLineMetrics()
方法中有一条注释,有关 fontmetrics 具有多种字体来覆盖字符集,但覆盖字符组,而不是单个字符。
FontMetrics doesn't have getters for cap height and x-height of a font.
How can I obtain these values?
As far as cap height goes, there's no guarantee for a particular capital letter that the letter's ascent is the same as the cap height. (e.g. a capital H isn't guaranteed to be flat on the top)
As far as x height goes, I assume it's probably the same as the height of an "x", but again, there's no guarantee.
edit: Grr! I just tried FontMetrics.getBounds()
and FontMetrics.getLineMetrics()
for specific character sequences, and I always get the same answer for heights (getBounds() does differ for widths, obviously). There's a note in the hasUniformLineMetrics()
method about a fontmetrics having several fonts to cover the character set, but that covers character groups, not individual characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在寻找的是屏幕渲染框,它告诉您文本的确切大小。
这意味着您需要在某个时候提供有关您正在绘制的表面和您正在绘制的字符串的信息。原因是系统直到渲染后期才知道视觉结果。我使用过:
你也可以尝试:
我也很难保持所有这些字体方法的正确性。
What you are looking for is the screen render box that tells you the exact size of text.
This means that you will need to supply information at some point about the surface you are drawing on and the string you are drawing. The reason is that the system simply does not know the visual result until late in rendering. I used:
You might also try:
I too have trouble keeping all those font methods straight.
我没有使用过它,但是 GlyphView.GlyphPainter 类具有
getAscent
、getDescent
和getHeight
方法。这可能是值得检查的事情。I've not worked with it, but the GlyphView.GlyphPainter class has
getAscent
,getDescent
andgetHeight
methods. That might be something to check out.好吧,如果你想制作一个带有适合文本的文本的框,我认为你可以将高度设置为字体大小本身
我不确定,但我认为这就是我过去所做的
Well, if you're trying to make a box with text that fits the text, i think you can just make the height the font size itself
Im not sure, but i think that is what i've done in the past
至于 x 高度,以下代码对我来说很好:
As for the x-height, the following code woks fine for me: