Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect / missing font metrics in Java?

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()
like image 611
Epaga Avatar asked May 29 '09 09:05

Epaga


2 Answers

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.

like image 181
banjollity Avatar answered Sep 19 '22 14:09

banjollity


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?

like image 40
Zarkonnen Avatar answered Sep 18 '22 14:09

Zarkonnen