Knowing the size of some text in pixels

How do you compute the scale that should be applied to a TextNode if you want the text to have a certain width and height in pixels? For example you are drawing to an 800x600 pixels screen and you want the text bounds to be 32 pixels height and 128 pixels wide.

Another question. Howcan we obtain info about the font we are using like baseline and how would we be able to determine tha baseline value for a certain height in pixels?

Use TextNode.getWidth() and/or TextNode.getHeight() to compute the size of a text segment in screen units. You can also use TextNode.getLeft(), getRight(), getBottom, getTop(). See the generated docs for TextNode for a complete list.

If you want to fill up a 128 x32 pixel space within a 800x600 screen, you have to do a bit of math. 128/800 = 0.16, and 32/600 = 0.0533. Since the dimensions of aspect2d in screen units is 2.667 x 2, it follows that you are really trying to fill up a space 0.16 x 2.667 = 0.427 screen units wide by 0.0533 * 2 = 0.107 screen units high. Scale your text accordingly.

You can query properties like the line height with font.getLineHeight(). The baseline is implicitly encoded in each character: all fonts are rendered with z = 0 at the baseline, so there’s no font-wide parameter for that. If you want to measure how far above or below the baseline a font is likely to go, you have to measure specific characters from the font one at a time (for instance, you can measure “y” and “T” to get a guess for the lowest and highest letters), or you can use the line height as an overall estimate.

David