Editing a font from within Löve

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
BrotSagtMist
Party member
Posts: 607
Joined: Fri Aug 06, 2021 10:30 pm

Editing a font from within Löve

Post by BrotSagtMist »

Greetings,
I am looking for a way to swap letters within a font file.
Specifically i want that printing a space with love.graphics.print(" ") renders for example a dot like •.
I also need font:getWidth(" ") to return the size of said dot then.
From the wiki it looks like it is possible to extract glyphs from a font. But there is no obvious way to use that data let alone to arrange it back into a font.
Does someone here have experience with such a problem?
obey
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Editing a font from within Löve

Post by pgimeno »

No, but maybe you could monkey-patch the functions to substitute text before doing the action?

Like:

Code: Select all

local function replaceText(s)
  return s:gsub(" ", "·")
end

local registry = debug.getregistry()
local print = love.graphics.print
local font_getWidth = registry.Font.getWidth

function love.graphics.print(s, ...)
  return print(replaceText(s), ...)
end

function registry.Font.getWidth(s, ...)
  return font_getWidth(replaceText(s), ...)
end
and so on with all functions you need replaced.
User avatar
BrotSagtMist
Party member
Posts: 607
Joined: Fri Aug 06, 2021 10:30 pm

Re: Editing a font from within Löve

Post by BrotSagtMist »

That works fine for the exact scenario i gave as example i guess.
But there is more which makes this less ideal:
Print accepts a table with color information like {c1,"a b",c2,"c d"} which needs no stay working. The resulting replace logic needs to be a tad bigger.
It probably wont stay limited to just one char to be switched, so the replace must called several times.
font:getWidth may be needed a few dozen times per frame resulting in a lot of replaces.
Nothing impossible to do, but i am concerned about cpu usage with that method also the code would be a Pita to write.
Whereas switching the actual picture of a letter would solve that all in a fast elegant way. This is why i want to go that route.
obey
User avatar
Froyok
Prole
Posts: 27
Joined: Tue Nov 16, 2021 4:53 pm
Contact:

Re: Editing a font from within Löve

Post by Froyok »

If you use a bitmap font with fixed letter size (monospace maybe ?) it should be easier to edit the input bitmap to build the font, no ?
User avatar
Nikki
Citizen
Posts: 83
Joined: Wed Jan 25, 2017 5:42 pm

Re: Editing a font from within Löve

Post by Nikki »

Alternatively you could use something like fontforge to put that dot in place of a space
User avatar
BrotSagtMist
Party member
Posts: 607
Joined: Fri Aug 06, 2021 10:30 pm

Re: Editing a font from within Löve

Post by BrotSagtMist »

I am working on making a themable text box.
Relying on bitmap or edited fonts kinda defeats the purpose.
obey
Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests