How to work nicely with fonts

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
matjojo
Prole
Posts: 8
Joined: Sat Dec 24, 2016 9:43 am
Location: The Netherlands

How to work nicely with fonts

Post 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?
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: How to work nicely with fonts

Post 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.
matjojo
Prole
Posts: 8
Joined: Sat Dec 24, 2016 9:43 am
Location: The Netherlands

Re: How to work nicely with fonts

Post 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.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: How to work nicely with fonts

Post 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.
matjojo
Prole
Posts: 8
Joined: Sat Dec 24, 2016 9:43 am
Location: The Netherlands

Re: How to work nicely with fonts

Post 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
User avatar
CaptainMaelstrom
Party member
Posts: 161
Joined: Sat Jan 05, 2013 10:38 pm

Re: How to work nicely with fonts

Post 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.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Google [Bot] and 92 guests