Java:如何找出字体的大写高度和x高度?

发布于 2024-11-11 15:47:08 字数 1074 浏览 9 评论 0原文

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 技术交流群。

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

发布评论

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

评论(4

(り薆情海 2024-11-18 15:47:08

您正在寻找的是屏幕渲染框,它告诉您文本的确切大小。

这意味着您需要在某个时候提供有关您正在绘制的表面和您正在绘制的字符串的信息。原因是系统直到渲染后期才知道视觉结果。我使用过:

Graphics2D g;
g.getFont().createGlyphVector(g.getFontRenderContext(),"abc").getVisualBounds();

你也可以尝试:

Graphics2D g;
g.getFont().getMaxCharBounds(g.getFontRenderContext());

我也很难保持所有这些字体方法的正确性。

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:

Graphics2D g;
g.getFont().createGlyphVector(g.getFontRenderContext(),"abc").getVisualBounds();

You might also try:

Graphics2D g;
g.getFont().getMaxCharBounds(g.getFontRenderContext());

I too have trouble keeping all those font methods straight.

歌入人心 2024-11-18 15:47:08

我没有使用过它,但是 GlyphView.GlyphPainter 类具有 getAscentgetDescentgetHeight 方法。这可能是值得检查的事情。

I've not worked with it, but the GlyphView.GlyphPainter class has getAscent, getDescent and getHeight methods. That might be something to check out.

挽容 2024-11-18 15:47:08

好吧,如果你想制作一个带有适合文本的文本的框,我认为你可以将高度设置为字体大小本身

我不确定,但我认为这就是我过去所做的

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

吃素的狼 2024-11-18 15:47:08

至于 x 高度,以下代码对我来说很好:

    public double getXHeight(Font font)
    {
        FontRenderContext fc = new FontRenderContext(null, false, false);
        TextLayout layout = new TextLayout("x", font, fc);
        return layout.getBounds().getHeight();
    }

As for the x-height, the following code woks fine for me:

    public double getXHeight(Font font)
    {
        FontRenderContext fc = new FontRenderContext(null, false, false);
        TextLayout layout = new TextLayout("x", font, fc);
        return layout.getBounds().getHeight();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文