Question

I need to get distance from baseline of one line to baseline to next text line, the same as it is laid out in JTextArea. For that, I have only a font with specified font size.

My problem is that on font, I can only get LineMetrics or FontMetrics, and they both have different sizes of line (getAscent/getHeight/getDescent) depending on the current text - for example, if there is letter f, the ascent is bigger, and if there is g, I have descent.

Is there a way to get "global" font height property? So I will be able to draw text on Graphics2D properly.

Was it helpful?

Solution

From JavaDoc for FontMetrics.getHeight():

Gets the standard height of a line of text in this font. This is the distance between the baseline of adjacent lines of text. It is the sum of the leading + ascent + descent. Due to rounding this may not be the same as getAscent() + getDescent() + getLeading(). There is no guarantee that lines of text spaced at this distance are disjoint; such lines may overlap if some characters overshoot either the standard ascent or the standard descent metric.

So if your fonts are implemented correctly, this should be the distance you should put between two lines of text.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top