Java 中的字体规格不正确/缺失?

发布于 2024-07-22 05:26:30 字数 1383 浏览 3 评论 0原文

使用某种字体时,我使用 Java 的 FontLayout 来确定其上升、下降和行距。 (参见Java的FontLayout教程此处

在我的具体情况下,我' m 使用 Arial Unicode MS,字体大小 8。使用以下代码:

    Font font = new Font("Arial Unicode MS", 0, 8);
    TextLayout layout = new TextLayout("Pp", font,
                               new FontRenderContext(null, true, true));
    System.out.println( "Ascent: "+layout.getAscent());
    System.out.println( "Descent: "+layout.getDescent());
    System.out.println( "Leading: "+layout.getLeading());

Java 给了我以下值:

    Ascent: 8.550781
    Descent: 2.1679688
    Leading: 0.0

到目前为止一切顺利。 但是,如果我使用这些值的总和作为各行文本的行距,这与 OpenOffice、Microsoft Word 等中使用的行距有很大不同:它更小。 当使用默认的单行间距时,Word 和 OO 的行间距似乎约为 13.7pt(而不是像我使用上面的 Java 字体度量计算的那样 10.7pt)。

知道

  1. 这是为什么吗?
  2. 我是否可以以某种方式访问​​ Word 和 OpenOffice 似乎正在访问的字体信息,从而导致不同的行间距?

到目前为止我尝试过的事情:

  • 使用 font.getNumGlyphs() 等将所有字形添加到字形向量中 - 仍然
  • 使用多行获取相同的字体指标值,如所述此处 - 每个我得到的行具有与上面概述的相同的字体规格。
  • 使用 FontMetrics' 方法,例如 getLeading()

Using a certain font, I use Java's FontLayout to determine its ascent, descent, and leading. (see Java's FontLayout tutorial here)

In my specific case I'm using Arial Unicode MS, font size 8. Using the following code:

    Font font = new Font("Arial Unicode MS", 0, 8);
    TextLayout layout = new TextLayout("Pp", font,
                               new FontRenderContext(null, true, true));
    System.out.println( "Ascent: "+layout.getAscent());
    System.out.println( "Descent: "+layout.getDescent());
    System.out.println( "Leading: "+layout.getLeading());

Java gives me the following values:

    Ascent: 8.550781
    Descent: 2.1679688
    Leading: 0.0

So far so good. However if I use the sum of these values as my line spacing for various lines of text, this differs by quite a bit from the line spacing used in OpenOffice, Microsoft Word, etc.: it is smaller. When using default single line spacing Word and OO seem to have a line spacing of around 13.7pt (instead of 10.7pt like I computed using Java's font metrics above).

Any idea

  1. why this is?
  2. whether I can somehow access the font information Word and OpenOffice seem to be accessing which leads to this different line spacing?

Things I've tried so far:

  • adding all glyphs to a glyph vector with font.getNumGlyphs() etc. - still get the same font metrics values
  • using multiple lines as described here - each line I get has the same font metrics as outlined above.
  • using FontMetrics' methods such as getLeading()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

南渊 2024-07-29 05:26:30

扎科宁不值得他投反对票,因为他的立场是正确的。 许多 Java 字体似乎会返回零作为其前导,而实际上它们不应该返回零。 也许是由于这个错误:我不知道。 似乎需要您将这个空白放回去。

印刷行高通常定义为上升+下降+行距。 上升和下降是从角色所在的基线向上和向下测量的,行距是一条线的下降和下面的线的上升之间的空间。

alt text

但行距不是固定的。 您可以在大多数文字处理和印刷软件中设置行距。 Word 将其称为行间距。 最初的问题可能是问 Microsoft Word 如何计算其单行间距。 微软的对 OpenType 字体的建议似乎表明不同平台上的软件计算方式不同。 (也许这就是 Java 现在返回零的原因?)

快速谷歌搜索似乎表明领先的经验法则是 单行间距为 120% ascent+descent,或固定点间距; 假设所有行之间领先 2 分。 在我找不到任何硬性或快速规则的情况下,我想说这归结为您所呈现的文本的易读性,您应该选择您认为看起来最好的内容。

Zarkonnen doesn't deserve his downvotes as he's on the right lines. Many Java fonts appear to return zero for their leading when perhaps they shouldn't. Maybe it is down to this bug: I don't know. It would appear to be down to you to put this whitespace back in.

Typographical line height is usually defined as ascent + descent + leading. Ascent and descent are measured upwards and downwards from the baseline that characters sit on, and the leading is the space between the descent of one line and the ascent of the line underneath.

alt text

But leading is not fixed. You can set the leading in most Word-processing and typographical software. Word calls this the line-spacing. The original question is probably asking how Microsoft Word calculates its single line spacing. Microsoft's recommendations for OpenType fonts seem to suggest that software on different platforms calculate it differently. (Maybe this is why Java now returns zero?)

A quick bit of Googling around seems to indicate that a rule of thumb for leading is 120% of ascent+descent for single-line spacing, or a fixed point spacing; say 2pts leading between all lines. In the absence of any hard or fast rule I can find, I would say it boils down to the legibility of the text you're presenting, and you should just go with what you think looks best.

滴情不沾 2024-07-29 05:26:30

Word 和 OO 是否包含行间空格,而 Java 则不包含?

那么在Word/OO中,你的数字是Ascent + Descent + Whitespace,而在Java中你只有Ascent + Descent?

Are Word and OO including the white space between lines, while Java isn't?

So in Word / OO, your number is Ascent + Descent + Whitespace, while in Java you just have Ascent + Descent?

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