Text Scaling Help [SOLVED!]

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
Cardibro123
Prole
Posts: 7
Joined: Sun Jan 21, 2024 3:21 pm

Text Scaling Help [SOLVED!]

Post by Cardibro123 »

I need help! :death:

How would I scale text so it fits in a box? I have been trying to do this for a while, but I can't find something that's efficient and not laggy!

Here's an example of what I have so far.


Code: Select all

function love.load()
    text = "Button"
end

function love.draw()
    love.graphics.setColor(1,1,1)

    love.graphics.print(love.timer.getFPS(),0,0) -- To check for lag

    local bx = love.graphics.getWidth()/5 -- Set box width
    local by = love.graphics.getHeight()/5 -- Set box height
    love.graphics.rectangle("fill", love.graphics.getWidth()/2-bx/2, love.graphics.getHeight()/2-by/2, bx, by) -- Draw box centered

    love.graphics.setColor(0,0,0)

    love.graphics.setFont(love.graphics.newFont(math.min(bx/4,by/3))) -- Set the font to a size that works with the current text

    local tw = love.graphics.getFont():getWidth(text) -- Get text width
    local th = love.graphics.getFont():getHeight(text) -- Get text height

    love.graphics.print(text, love.graphics.getWidth()/2-tw/2, love.graphics.getHeight()/2-th/2) -- Draw text centered
end


Everything works about this, except if I change the text to something short, the text is too small, and if I change the text to something longer, the text is too big.

How would I make this, but it works on any text?

Any help will be appreciated! :awesome: :ultraglee:
Last edited by Cardibro123 on Fri Jan 24, 2025 4:40 pm, edited 1 time in total.
User avatar
BrotSagtMist
Party member
Posts: 680
Joined: Fri Aug 06, 2021 10:30 pm

Re: Text Scaling Help

Post by BrotSagtMist »

Do you realise that love.draw is excuted 60 times a second?
You are rebuilding and reloading all you infos/data for every frame, if you continue that way your project will crush every computer with 100% cpu usage just from showing a few buttons.
Use the love.resize callback instead so stuff is only called when needed.

Next, scaling letters in a way that they retroactively fit in a textbox is a next to impossible task (for easy code), thats why no one does it.
You would have to create a font, check if the text fits with that font and then repeat the process till you find a match, for every string anew.
Or you take the size of that check and do procentual math and render with a scale, that would however end in blurry text.
So basically if the textbox is 100 pixel width and getwidth(text) reports to take 75 pixel, you got 100/75= 1.333 and then you set the overall scale to 1.333.
obey
Cardibro123
Prole
Posts: 7
Joined: Sun Jan 21, 2024 3:21 pm

Re: Text Scaling Help

Post by Cardibro123 »

First of all, thank you for taking the time to respond.

I see what you are saying about love.draw being rerun every frame. I will make sure not to make that mistake in the future! And, I get what you are saying on how to scale the text and it makes more sense now! I will have to try that out when I get the chance.

Once again, thank you for your help! :crazy:
User avatar
dusoft
Party member
Posts: 765
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Text Scaling Help

Post by dusoft »

Or don't scale and just use self-contained word-wrap with printf:
https://love2d.org/wiki/love.graphics.printf
Cardibro123
Prole
Posts: 7
Joined: Sun Jan 21, 2024 3:21 pm

Re: Text Scaling Help

Post by Cardibro123 »

I know how word-wrap works using printF, but I have not heard of self-contained word-wrap before. Is there another document I can read to understand it? Any help would be appreciated, and thank you for responding. :awesome:
User avatar
dusoft
Party member
Posts: 765
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Text Scaling Help

Post by dusoft »

The word-wrap takes care of your width (that's why it is self-contained, you don't have to contain it in any other way...).

If you need to fit text with the largest-possible size into your desired container then scaling directly using sx, sy in printf is the better way. The ratio should be easy to calculate from the text dimensions and your container width/height.

Follow what BrotSagtMist says and never put any resource initialization in update/draw etc. - that's what love.load is for.
Cardibro123
Prole
Posts: 7
Joined: Sun Jan 21, 2024 3:21 pm

Re: Text Scaling Help

Post by Cardibro123 »

Got it! I can see the differences and when to use them. Thank you for helping me! :crazy:
Post Reply

Who is online

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