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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
DrNefario
Prole
Posts: 6
Joined: Thu Apr 08, 2021 7:44 pm

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

Post 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.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
User avatar
tomxp411
Prole
Posts: 29
Joined: Thu Apr 08, 2021 5:41 pm

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

Post 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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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).
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests