A Defold native extension for interacting with fonts at runtime.
This extension allows for extending an existing Defold font (.fontc) with more glyphs at runtime.
It allows for smaller package sizes, as well as less runtime memory footprint.
We need to create an empty font file, and also bundle the project with the desired .ttf fonts.
.font
file
.
The idea being that the character will look mostly the same between the offline/runtime generation. This restrictions will be removed later on..ttf
file as a custom resourceThe developer is reponsible for adding or removing glyphs from the font, as the game engine doesn’t know what text will be shown ahead of time.
You associate the .fontc
with the .ttf
font like so:
local ttf = "/assets/fonts/Roboto/Roboto-Bold.ttf" -- path to custom resource
local fontc_hash, err = fontgen.load_font("/assets/fonts/roboto.fontc", ttf)
if err ~= nil then
print("Result:", fontc_hash, err)
end
self.font = fontc_hash
Before showing any text, the developer need to make sure the glyphs are generated.
The call to fontgen.add_glyphs()
is asynchronous, and allows you to supply a callback, which will be called when the glyphs are generated, and it’s safe to display the text.
local chars = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
local request = fontgen.add_glyphs(self.font, chars, function (self, id, result, errmsg)
if not result then
print("Request " .. id .." finished with error:", errmsg)
else
print("Request " .. id .." finished successfully")
end
end)
If required, it is also possible to remove glyphs. This may beneficial if memory is needed to be kept at a minimum.
fontgen.remove_glyphs(self.font, "DEFdef")
WHen the font is not needed anymore, you can unload it. This also removes the currently generated glyphs.
fontgen.unload_font(self.font)
See ext.properties for the up-to-date list of settings.
fontgen.sdf_base_padding
- The base padding when generating sdf glyphs [0-255]fontgen.sdf_edge_value
- The on edge when generating sdf glyphs. [0-255]Did you spot an error or do you have a suggestion? Please let us know on GitHub!
GITHUB