Page 1 of 1

How to work nicely with fonts

Posted: Sat Sep 02, 2017 9:19 am
by matjojo
I'm trying to find a nice way to work with custom font's and sizes but the best I've been able to come up with is this:

Code: Select all

	MainFonts = {}
	for i=1, 100, 1 do
		MainFonts[i] = love.graphics.newFont("res/fonts/earthorbiter.ttf", i)
	end
I feel like there should be a better way to do this, but a search on this forum and google later I've not found any better options yet, I tried a reddit post but the only idea there was to make a really big font and scale it down every time I needed a smaller one, but that too does not really sound like a solution as meant by the devs.

Does anyone know a better solution?

Re: How to work nicely with fonts

Posted: Sat Sep 02, 2017 12:22 pm
by grump
It would be helpful if you described what problem you're trying to solve. What exactly are you doing that requires all font sizes from 1 to 100? A font size of 1 is illegible, and 100 is unusually large.

Re: How to work nicely with fonts

Posted: Sat Sep 02, 2017 2:24 pm
by matjojo
I don't need all fontsizes from one to 100, I just thought it was kinda weird to have to need to create a font for every size I might need, and thought hoped that there was a more *polished?* way of doing that. But from my searches here, your answer, and my internet searches it seems that there is not.


I guess I'll just add fonts to that array as needed then.

Re: How to work nicely with fonts

Posted: Sat Sep 02, 2017 2:39 pm
by grump
Yeah, that's the way to go. Love converts fonts to textures and draws textured quads instead of vectors. So you have to explicitly load each size. I recommend to implement a simple cache if you potentially load lots of different fonts/sizes in different places.

Code: Select all

-- untested impromptu code
local cache = {}

function loadFont(font, size)
	local key = font .. tostring(size)
	if not cache[key] then
		cache[key] = love.graphics.newFont(font, size)
	end

	font = cache[key]
	return font
end
That being said, I'm working on a library that makes it possible to load fonts as meshes. I will post it here in the forum once it's in a usable state.

Re: How to work nicely with fonts

Posted: Sat Sep 02, 2017 2:46 pm
by matjojo
That would indeed work, I'll probably rework it to not be 1-100 but just a table with all the fontsizes I need, thanks

Re: How to work nicely with fonts

Posted: Mon Sep 04, 2017 3:49 pm
by CaptainMaelstrom
Additionally, you could create a Text object (http://www.love2d.org/wiki/Text) using the biggest size font, and then scale it at draw time.

Example

Code: Select all

love.graphics.draw(mytext, x, y, rotation, scale_x, scale_y)
Or, you could set font, print to a canvas, and draw the canvas to scale.