Get maximal possible fontsize

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
User avatar
ertt
Prole
Posts: 10
Joined: Mon Mar 20, 2017 11:02 pm

Get maximal possible fontsize

Post by ertt »

hey folks,

i have a little matter with working with fonts. i want to calculate the largest possible font size to show my text in the window.
the code in the example is veeeeery slow (i think because of creating a lot of new font-objects).

Code: Select all

fontname = "fontfile.ttf"
fontsize = 60
    
function getMaxFontSize()
    local teststring = "00:00.0"
    local testFont = love.graphics.newFont(fontname, fontsize)
    local textWidth = testFont:getWidth(teststring)
    local windowsWidth = love.graphics.getWidth()
    while textWidth < windowsWidth do
        fontsize = fontsize + 5
        testFont = love.graphics.newFont(fontname, fontsize)
        textWidth = testFont:getWidth(teststring)
        print(textWidth, fontsize)
    end
end
has anyone a better solution or workarount for this "problem"?

thanks.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Get maximal possible fontsize

Post by pgimeno »

Binary search?
User avatar
ertt
Prole
Posts: 10
Joined: Mon Mar 20, 2017 11:02 pm

Re: Get maximal possible fontsize

Post by ertt »

pgimeno wrote: Sun Dec 23, 2018 6:47 pm Binary search?
I hope there is a solution only with the love-API. But if i dont found another way it should be possible to use a search algorithm. (But maybe its a little bit over-engineered?!).

Thanks anyway.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Get maximal possible fontsize

Post by ivan »

From what I can see in the code, you are trying to find the EXACT fontsize that will fit a given width.
There is a better solution to this, something like:
1. load a very large font size, larger than necessary (based on height)
2. scale it down so that it fits exactly the desired width

Code: Select all

local font = love.graphics.newFont(fontname, 64)
local textWidth = font:getWidth(text)
local scale = windowWidth/textWidth
...
love.graphics.draw(text, 0, 0, 0, scale, scale)
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Get maximal possible fontsize

Post by pgimeno »

So I've been thinking more about this...

Width should be roughly proportional to height (for a fixed text), and height should be roughly proportional to font size.

You can determine the factor of size vs. height for the font you're using. You can do that in advance. For example, create a font with size 1000 and ask for its height: print(love.graphics.newFont(fontname, 1000):getHeight())

You can then hardcode that factor into your program. For the default LÖVE font, it's about 1.164.

Now, initialize your program by creating and keeping a large-ish (but not huge) font, say font size 100.

When you need to print with a fixed width, use that font to determine the aspect ratio of your text (width/height). Once you have the aspect ratio, you can use the desired width (the width of the screen in your case) to find the desired height. Once you have the height, you can find the font size given the ratio height:size that you calculated in advance.

Once you have calculated the size, since there can be rounding problems, you may need to adjust, perhaps by 1 or 2 font sizes. But that should be faster. So, create the font and verify whether it fits.

If it fits, either you're happy already, or you can try whether you can adjust better, by increasing the font size until it doesn't, and keep the highest value that fits.

If it doesn't fit, keep shrinking the font size until it does.

The number of steps necessary to perform the final adjustment should be very small in comparison to what you're doing now, probably 1 or 2 extra.
Post Reply

Who is online

Users browsing this forum: No registered users and 81 guests