Maybe I'm misunderstanding these methods, but I'm trying to vertically center an "x" and a "y" in a Graphics2d object. When I read getDescent() and getAscent() from a TextLayout to try and calculate a vertical offset, they both return the same thing for both letters.
Asked
Active
Viewed 59 times
0
-
2Descent and ascent are properties of the font. They tell you the "normal" values for that font and are not specific to which glyphs you use. If you want information about the size of text, you need to use `getBounds()`. – Joachim Sauer Sep 23 '21 at 21:04
-
@JoachimSauer The [documentation of TextLayout.getDescent()](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/font/TextLayout.html#getDescent()) seems to say that the value is based on individual glyphs, rather than the font’s standard descent. – VGR Sep 23 '21 at 21:15
-
@JoachimSauer - Yes! Thank you! getBounds().getHeight() returns different heights. – John Smith Sep 23 '21 at 21:23
-
@VGR: interesting, in my (admittedly very limited and also somewhat dated) experience it tended to be independent of the actual text. Maybe that's changed, I misremembered *or* the doc is actually misleading. I actually think all of these possibilities are roughly equally likely ;-) – Joachim Sauer Sep 23 '21 at 21:40
-
@JoachimSauer - Ok, That does return different heights but not exactly what I wanted. I created a new question. https://stackoverflow.com/questions/69308312/java-having-difficulty-centering-textlayouts-on-a-graphics2d-with-various-letter – John Smith Sep 24 '21 at 00:58