3

In core Java book it says

The width of the rectangle that the getStringBounds method returns is the horizontal extent of the string. The height of the rectangle is the sum of ascent, descent, and leading. The rectangle has its origin at the baseline of the string. The top y -coordinate of the rectangle is negative. Thus, you can obtain string width, height, and ascent as follows:

double stringWidth = bounds.getWidth();
double stringHeight = bounds.getHeight();
double ascent = -bounds.getY();

What does the author mean when saying that the rectangle has its origin at the baseline of the string, while top y-coordinate is the ascent?

Where does the bounding rectangle of the string start?

with a test string i got the following:

w: 291.0
h: 91.265625
x:0.0
y:-72.38671875
descent: 15.8203125
leading: 3.0585938

That mean the rectangle origin is at the leading not the baseline, am i correct on this?

skystar7
  • 4,419
  • 11
  • 38
  • 41
  • Why do you think the origin is at the leading? – Paul Feb 06 '12 at 23:25
  • by adding y+h=(descent+leading) which the point that leading start, i am assuming the origin here bottom lefft – skystar7 Feb 06 '12 at 23:31
  • 1
    See also this [answer](http://stackoverflow.com/a/5998117/230513) comparing `FontMetrics.stringWidth()` with `TextLayout#getBounds()`. – trashgod Feb 06 '12 at 23:47
  • 1
    @trashgod `TextLayout`! Gads.. that is the one I am always forgetting of `FontMetrics`, `GlyphVector`, `JLabel` & **`TextLayout`**. Java offers a myriad of ways to skin this [cat](http://stackoverflow.com/questions/6295084/cut-out-image-in-shape-of-text/6296381#6296381). – Andrew Thompson Feb 06 '12 at 23:58

3 Answers3

6

It means that the bounds' coordinates are in a space where zero Y coordinate is at string's baseline and positive Y coordinates go downwards. In the following image the black dot corresponds to zero Y:

font metrics

Therefore negative bounds.getY() (ascent) corresponds to the topmost coordinate. And positive bounds.getHeight() + bounds.getY() (descent + leading) will correspond to the botmommost coordinate in this coordinate space.

pingw33n
  • 12,292
  • 2
  • 37
  • 38
  • +1 for the diagram; see also [`FonMetrics`](http://docs.oracle.com/javase/7/docs/api/java/awt/FontMetrics.html). – trashgod Feb 06 '12 at 23:50
4

The math works out:

72.38671875 ascent + 15.8203125 descent + 3.0585938 leading = 91.265625 total height

This tutorial on 2D Text has an image illustrating leading, descent, and ascent.

In your specific case, 72.38671875 is the height of the ascent. That's measured from the baseline to the top of the tallest glyph. The leading is the space between the bottom of the descender to the top of the next line.

The bounding rectangle is relative to the baseline. The API for FontMetrics.getStringBounds states "The returned bounds is in baseline-relative coordinates", which explains your results. x will always be 0, and the height of the bounding box will be the ascent plus the descent plus the leading.

Paul
  • 19,704
  • 14
  • 78
  • 96
  • Thank for your answer it is now clear to me, the baseline is at (0,0) coordinate, and the bounding rectangle is relative to this coordinate. – skystar7 Feb 07 '12 at 01:01
3

The Java graphics coordinate system has its origin in the top right of the canvas, with the Y coordinate increasing from top to bottom. This means that a rectangle's top edge (the return value of getY()) will have a smaller Y coordinate than its bottom edge (the baseline of a text string).

The result value of getStringBounds() is only somewhat consistent with this. While the coordinate system is respected, the origin of the bounding rectangle is relative to the baseline, not at the top left. This means that the top left of the rectangle will have a negative Y coordinate.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
millimoose
  • 39,073
  • 9
  • 82
  • 134
  • Not the _bottom_ but the _baseline_. Edit: The API uses the term _baseline-relative_. – trashgod Feb 06 '12 at 23:49
  • 1
    @trashgod The "bottom left" was meant as an intuitive description, not as a literal one. I'll edit it to make that clearer. – millimoose Feb 06 '12 at 23:54