Page 1 of 1

Editing a font from within Löve

Posted: Fri Jan 28, 2022 6:35 pm
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?

Re: Editing a font from within Löve

Posted: Fri Jan 28, 2022 7:50 pm
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.

Re: Editing a font from within Löve

Posted: Fri Jan 28, 2022 8:35 pm
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.

Re: Editing a font from within Löve

Posted: Sun Jan 30, 2022 5:08 pm
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 ?

Re: Editing a font from within Löve

Posted: Sun Jan 30, 2022 6:26 pm
by Nikki
Alternatively you could use something like fontforge to put that dot in place of a space

Re: Editing a font from within Löve

Posted: Sun Jan 30, 2022 7:06 pm
by BrotSagtMist
I am working on making a themable text box.
Relying on bitmap or edited fonts kinda defeats the purpose.