简体   繁体   English

Java中的字体指标不正确/缺失?

[英]Incorrect / missing font metrics in Java?

Using a certain font, I use Java's FontLayout to determine its ascent, descent, and leading. 使用某种字体,我使用Java的FontLayout来确定它的上升,下降和前导。 (see Java's FontLayout tutorial here ) (见Java的FontLayout教程这里

In my specific case I'm using Arial Unicode MS, font size 8. Using the following code: 在我的具体情况下,我使用的是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 gives me the following values: Java给了我以下值:

    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. 但是,如果我使用这些值的总和作为各行文本的行间距,这与OpenOffice,Microsoft Word等中使用的行间距相差很大:它更小。 When using default single line spacing Word and OO seem to have a line spacing of around 13.7 pt (instead of 10.7 pt like I computed using Java's font metrics above). 当使用默认单行间距Word和OO似乎有大约13.7 pt的行间距(而不是像我使用上面的Java字体指标计算的10.7 pt)。

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? 是否可以以某种方式访问​​字体信息Word和OpenOffice似乎访问导致这种不同的行间距?

Things I've tried so far: 到目前为止我尝试过的事情:

  • adding all glyphs to a glyph vector with font.getNumGlyphs() etc. - still get the same font metrics values 使用font.getNumGlyphs()等将所有字形添加到字形向量中 - 仍然获得相同的字体度量值
  • using multiple lines as described here - each line I get has the same font metrics as outlined above. 使用此处所述的多行 - 我获得的每一行都具有与上述相同的字体指标。
  • using FontMetrics ' methods such as getLeading() 使用FontMetrics的方法,如getLeading()

Zarkonnen doesn't deserve his downvotes as he's on the right lines. Zarkonnen不值得他的支持,因为他在正确的路线上。 Many Java fonts appear to return zero for their leading when perhaps they shouldn't. 许多Java字体似乎在他们的领导时返回零,也许他们不应该。 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. 上升和下降从角色所在的基线向上和向下测量,并且前导是一条线的下降和下面的线的上升之间的空间。

替代文字

But leading is not fixed. 但领先并不固定。 You can set the leading in most Word-processing and typographical software. 您可以在大多数Word处理和印刷软件中设置领先地位。 Word calls this the line-spacing. Word称之为行间距。 The original question is probably asking how Microsoft Word calculates its single line spacing. 最初的问题可能是询问Microsoft Word如何计算其单行间距。 Microsoft's recommendations for OpenType fonts seem to suggest that software on different platforms calculate it differently. 微软对OpenType字体建议似乎表明不同平台上的软件以不同方式计算它。 (Maybe this is why Java now returns zero?) (也许这就是为什么Java现在返回零?)

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; 一点点谷歌搜索似乎表明, 对于单线间距或固定点间距,前导的经验法则是上升+下降的120% ; say 2pts leading between all lines. 说所有线之间领先2分。 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. 在没有任何严格或快速的规则我可以​​找到的情况下,我会说它归结为你所呈现的文本的易读性,你应该选择你认为最好的文本。

Are Word and OO including the white space between lines, while Java isn't? Word和OO是否包括行之间的空白,而Java不是?

So in Word / OO, your number is Ascent + Descent + Whitespace, while in Java you just have Ascent + Descent? 所以在Word / OO中,你的数字是Ascent + Descent + Whitespace,而在Java中你只有Ascent + Descent?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM