suggestion next love release: getPixelHeight getPixelWidth for text

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
gcmartijn
Party member
Posts: 146
Joined: Sat Dec 28, 2019 6:35 pm

suggestion next love release: getPixelHeight getPixelWidth for text

Post by gcmartijn »

Everything is great with love, but then I need to work with text height and width, and don't understand why all the difficult functions are needed.

I think that the main stream users don't want to learn/understand all those functions and only want to have this.

- Use a text string for example using the newText and set a font.
- And then the magic happens, now we can draw it on the screen and get the actual pixel drawing width and height.

In other words:

Code: Select all

    textHeight =  font:getGetPixelsThatAreDrawOnScreenHeight(text)
    textWidth = font:getGetPixelsThatAreDrawOnScreenWidth(text)
    
    textHeight = text:getGetPixelsThatAreDrawOnScreenHeight(text)
    textWidth = text:getGetPixelsThatAreDrawOnScreenWidth(text)

Every time it cost me too much time to get the correct text width and height.
A snippet that I found in my old code.

Code: Select all

                self.y + ((self.textObj.font.resource:getAscent() / 2) * self.scale) - (self.textObj:getHeight() / 2)
I have to relearn now what ascent was.

Today I create this simple test, without scaling and other things (that makes it more complex), that give me not what I expected.

Code: Select all

function love.load()
    love.graphics.setDefaultFilter("nearest", "nearest")
    love.graphics.setLineStyle("rough")

    font = love.graphics.newFont("press-start-2p.ttf", 8)
    font:setFilter("nearest", "nearest")

    text = "Hello"
    text2 = love.graphics.newText(font, text)
end

function love.update(dt)
end

function love.draw()
    love.graphics.setFont(font)

    font:getHeight(text) -- 8, but in photoshop it is 7px
    font:getWidth(text) -- 40, but in photoshop it is 39px
    font:getLineHeight() -- 1
    font:getAscent() -- 8
    font:getBaseline() -- 8
    font:getDescent() -- 0

    text2:getHeight() -- 8, but in photoshop it is 7px
    text2:getWidth() -- 40, but in photoshop it is 39px

    love.graphics.print(text, 60, 60)
    love.graphics.draw(text2, 80, 20)
end

Can someone for all the other users and me give me a example how to get the pixel width and height for the given text string?
I did try the https://love2d.org/wiki/Rasterizer things, but that is overkill and more complex.

For the people like me, that just don't understand the complex font things, and only want 1 line to get the getPixelsOnScreenWidth() and getPixelsOnScreenHeight() would be cool.

Thanks :)
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: suggestion next love release: getPixelHeight getPixelWidth for text

Post by zorg »

Code: Select all

local defaultFont = love.graphics.getFont()
local height = defaultFont:getHeight() -- uniform regardless of text, this is the height of one line
local width = defaultFont:getWidth(text) -- gets the max. width of a given text (works properly with newlines)
local textWidth, wrappedText = defaultFont:getWrap(text, wraplimit) -- given a max. width a line can be, gives back the max. line width and the text in a table, correctly chopped into multiple pieces so it doesn't go over the given limit.
height, width and textWidth are all in pixels (unless dpi scaling is in effect, but then everything is in those units so it doesn't matter), so those are what you're looking for; you can feel free to ignore literally any other Font related functions like ascent, baseline, etc.

Also, fonts are kinda complex, so don't expect the same values to hold in different applications, e.g. photoshop and löve; one of these might apply some extra scaling or whatever that the other doesn't.
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.
gcmartijn
Party member
Posts: 146
Joined: Sat Dec 28, 2019 6:35 pm

Re: suggestion next love release: getPixelHeight getPixelWidth for text

Post by gcmartijn »

Thanks for the info.

With my example I did mean, when I make a screenshot and open that screenshot in Photoshop, then I see that the width and height are -1px
Screenshot 2022-03-19 at 11.17.10.png
Screenshot 2022-03-19 at 11.17.10.png (96.75 KiB) Viewed 3035 times
I'm glad that you see that I can ignore all the advanced things. Now I going to make a better example when it goes wrong, like the 1px above.

Inside the game i'm trying to make, I scale thing up, so that's why there is maybe more px wrong.
That's why I made this simple example to show the 1px 'problem'. Going to make a more advanced example, maybe I can see solve the actual problem then.
gcmartijn
Party member
Posts: 146
Joined: Sat Dec 28, 2019 6:35 pm

Re: suggestion next love release: getPixelHeight getPixelWidth for text

Post by gcmartijn »

I found the problem in the game, I did put a " " (space) character, din't know I did. And that made the text getWidth() larger...

So its more accurate then I describe above.
Post Reply

Who is online

Users browsing this forum: Google [Bot], slime and 5 guests