Looking for some logic to use with love.graphics.print and font settings.
I assumed (stupidly) that when you set the location of printing that the upper left hand corner of the text would at the coordinates provided. It seems that it is actually printed farther down the screen (by ~ the descent value) and the height of the text is less than the font size by some (unknown) value.
Also, getLineHeight returns only what you set using setLineHeight - nothing related to any other settings???
The attached test script allows changing the font size ('s' & 'a' keys).
The print coordinates are indicated by the red 'x'.
Know there is something simple that I'm overlooking.
love.graphics.pring & font settings
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
love.graphics.pring & font settings
- Attachments
-
- print_font.love
- Test of text printing
- (958 Bytes) Downloaded 80 times
Re: love.graphics.pring & font settings
My guess is that the font is to blame for this problem.
The first thing that popped into my mind was that you should try a monospaced font, which worked to align the font horizontally with exactly where you want to print it. If you add half of the font's descent you can negate the pesky vertical alignment issues, too, but you seem to know that already.
As for the literal height of the line, you'd find that using font:getHeight(), not font:getLineHeight().
Sorry I can't tell you how to do this with fonts that aren't monospaced, but it's a start!
The first thing that popped into my mind was that you should try a monospaced font, which worked to align the font horizontally with exactly where you want to print it. If you add half of the font's descent you can negate the pesky vertical alignment issues, too, but you seem to know that already.
As for the literal height of the line, you'd find that using font:getHeight(), not font:getLineHeight().
Sorry I can't tell you how to do this with fonts that aren't monospaced, but it's a start!
- Attachments
-
- font.love
- (4.68 KiB) Downloaded 61 times
"Bump." -CMFIend420
Re: love.graphics.pring & font settings
Hi Helvecta!
Surprised that the print location is related to font selection (Y setting) but guess that may be related to how the font was created.
Just seems strange that the settings for Love printing are not appropriate for the default font.
The only font function which seems to work as expected is Font:getWidth(string) whether mono spaced or not.
Realized that Font:getHeight is different form Font:getLineHeight. Just didn't see the utility of Font:getLineHeight since the only thing it returns is what you set using Font:setLineHeight.
Surprised that the print location is related to font selection (Y setting) but guess that may be related to how the font was created.
Just seems strange that the settings for Love printing are not appropriate for the default font.
The only font function which seems to work as expected is Font:getWidth(string) whether mono spaced or not.
Realized that Font:getHeight is different form Font:getLineHeight. Just didn't see the utility of Font:getLineHeight since the only thing it returns is what you set using Font:setLineHeight.
Re: love.graphics.pring & font settings
EDIT: DaedalusYoung has the nitty gritty explanation down
Yeah TrueType fonts are a lot like glyph fonts from images (at least, I'm pretty sure); someone creates the font character by character and is responsible for aligning the left of the letter where the designated left side is. As far as the vertical alignment goes, as long as the creator put everything along the baseline, it should be aligned right.. which means default fonts should definitely be aligning correctly. Hmm..
Oh! Maybe I did this wrong. Changing the y position to print it to "fy + asc - largefont:getHeight()" seems to make all these fonts align correctly:
(if you can't tell, this is my first time looking at this kind of problem before)
And Oh! x2!
Font:setLineHeight works like padding, sorry! It's basically a multiplier - if y = the position where you print a string, when a printed string has multiple lines, it prints the new line at y + (Font:getLineHeight() * Font:getHeight()).. so setting the line height to 100 like in your example means that it gets printed at y + 100 * Font:getHeight() higher than the previous line.
Of course, take my advice with a grain of salt, being right about things isn't my forte
Oh! Maybe I did this wrong. Changing the y position to print it to "fy + asc - largefont:getHeight()" seems to make all these fonts align correctly:
Code: Select all
gr.print( msg, fx, fy + asc - largefont:getHeight())
And Oh! x2!
Font:setLineHeight works like padding, sorry! It's basically a multiplier - if y = the position where you print a string, when a printed string has multiple lines, it prints the new line at y + (Font:getLineHeight() * Font:getHeight()).. so setting the line height to 100 like in your example means that it gets printed at y + 100 * Font:getHeight() higher than the previous line.
Of course, take my advice with a grain of salt, being right about things isn't my forte
- Attachments
-
- Font Part Version 2.0!.love
- Controls!
Q and W - Decrease/Increase line height multiplier by 1
A and S - Decrease/Increase font size by 1
Space - Load a different font! - (430.95 KiB) Downloaded 62 times
Last edited by Helvecta on Fri Mar 14, 2014 3:40 am, edited 1 time in total.
"Bump." -CMFIend420
- DaedalusYoung
- Party member
- Posts: 407
- Joined: Sun Jul 14, 2013 8:04 pm
Re: love.graphics.pring & font settings
It does have to do with the way font files work. Every character has an imaginary square, the em square, based on how texts used to be created in printing in the old days. This square determines the font size. I'm fairly sure LÖVE uses the top of the em square as y origin point. Characters typically fit within the em square, leaving blank space all around itself. Extra left and right side bearings determine each character's width, but I do believe the height is the same for every character.
Note that it's also possible for characters to extend outside the em square, for example, see figure 12 on [this page]. So it is also possible (and I have seen it happen) that when you want to print a text at y 0, the characters are actually cut off at the top, because they're larger than their em squares.
If LÖVE would accurately calculate the text height, depending on what characters you're printing, you would get strange results, where some lines are larger than others, simply based on what characters are in the string to be printed. For example a line with the string "mass" will be shorter in height than a line with a string "mast" because the 't' is taller than the other letters. This would be very unwanted behaviour.
So this is a side effect of the font file format, but it is necessary in order to print all characters at the correct height, relative to all other characters in the font. You will find some fonts have more blank space than others, and as I said earlier, some fonts may extend above the specified y position.
Note that it's also possible for characters to extend outside the em square, for example, see figure 12 on [this page]. So it is also possible (and I have seen it happen) that when you want to print a text at y 0, the characters are actually cut off at the top, because they're larger than their em squares.
If LÖVE would accurately calculate the text height, depending on what characters you're printing, you would get strange results, where some lines are larger than others, simply based on what characters are in the string to be printed. For example a line with the string "mass" will be shorter in height than a line with a string "mast" because the 't' is taller than the other letters. This would be very unwanted behaviour.
So this is a side effect of the font file format, but it is necessary in order to print all characters at the correct height, relative to all other characters in the font. You will find some fonts have more blank space than others, and as I said earlier, some fonts may extend above the specified y position.
Re: love.graphics.pring & font settings
I guess (for my use) the most robust way to put text exactly where you want is:
At least you know where the text will be regardless of the font size with descent below and the rest of the text above.
Code: Select all
base_line = font:getBaseline( )
love.graphics.print( msg, x, y - base_line )
Who is online
Users browsing this forum: Bing [Bot] and 33 guests