Page 1 of 1

Why doesn't Font.getHeight() work the same as Font.getWidth()?

Posted: Fri Apr 16, 2021 2:40 pm
by DrNefario
Hello,

I just posted my first ever project in the Games and Creations forum, and I have many novice questions, but this was perhaps the major annoyance of my first project.

Font.getWidth(string) gives you the width of the string, taking embedded newlines into account.

Font.getHeight() doesn't take a string, and will only ever give you the height of one line. (Or, as I have seen in several examples, people think it takes a string and expect it to work like getWidth, but it doesn't.)

Why? Surely the logic is pretty much the same as it would be for getWidth()? In fact, it seems to be abnormally difficult to work out the print height - there's no way of getting it out of printf either, which makes printf much less useful than it could be.

It just seems like a trivial but incredibly useful change.

I guess I'm supposed to use Text now, but that seems kind of heavyweight for the context, and seems to involve rewriting all the code that nearly works, and would work with this small change. I admit I haven't really looked at Text yet. My project was a bit rushed.

Re: Why doesn't Font.getHeight() work the same as Font.getWidth()?

Posted: Fri Apr 16, 2021 4:38 pm
by pgimeno
If you use love.graphics.print, the total height is the number of lines (number of \n + 1) multiplied by Font:getHeight().

If you use love.graphics.printf, you can get the number of final lines by taking the number of elements present in the second return value of Font:getWrap. Multiplying that by Font:getHeight() will give you the total height.

Re: Why doesn't Font.getHeight() work the same as Font.getWidth()?

Posted: Fri Apr 16, 2021 5:52 pm
by tomxp411
The difference is a basic property of typography. The font size actually is the height of a capital letter in points. If you know the point size and the leading (space between each baseline), along with the DPI of the rendering surface, then you know the size of any line of text. The actual text doesn't matter, since the height is fixed.

On the other hand, the width of a line of text is based on the characters typed, so to compute the width of a block of text, you need to actually render the text, then go back and examine the bounds of the rendered area.

So for a given font and size, the height of a line of text never changes. However, word wrapping and newlines can certainly stretch a string out to more than one line, so that still requires the block of text to be rendered and evaluated. I'm afraid I don't know enough about the graphics API yet to give a reasonable way to do that yet.

Re: Why doesn't Font.getHeight() work the same as Font.getWidth()?

Posted: Sat Apr 17, 2021 8:07 am
by zorg
tomxp411 wrote: Fri Apr 16, 2021 5:52 pm The difference is a basic property of typography. The font size actually is the height of a capital letter in points (...) the height is fixed.

On the other hand, the width of a line of text is based on the characters typed (...)
Just as an aside, this is only true for LtR and RtL languages (and i guess horizontal boustrophedon); TtB and BtT would work a little bit differently (depending on a lot of things actually).