Custom web font injector plugin for Corona HTML5 projects.
Custom web font injector plugin for Corona HTML5 projects.
This plugin is not needed since Corona daily build 2018.3248, but could be some nice code to study if you’re building one.
Download the Font Loader Plugin.
Move the plugin/fontloader_js.js and plugin/fontloader.lua files to your root project directory.
Require the plugin early in your project:
-- main.lua
local fontloader = require("fontloader")
Load in the specified custom fonts for use.
fontloader.load(fonts)
The fonts
argument is a table with the font family as the key, and the font source as the value.
Make sure to include the font source(s) with your HTML5 build.
Example
local fonts = {
Roboto = "Roboto-Regular.ttf",
IBMPlexMono = "IBMPlexMono-Regular.ttf"
}
fontloader.load(fonts)
You need to wait until your fonts are all loaded before you can use them.
You should load your fonts at the start of your project.
To check that your fonts are all ready, set up an event listener:
local function displayText()
local txtOne = display.newText( "Hello World", 150, 80, "IBMPlexMono", 48 )
local txtTwo = display.newText( "Hello World", 150, 140, "Roboto", 48 )
end
local function onLoadFonts(evt)
if evt.name == 'ready' then
displayText()
end
end
local fonts = {
Roboto = "Roboto-Regular.ttf",
IBMPlexMono = "IBMPlexMono-Regular.ttf"
}
fontloader.addEventListener(onLoadFonts)
fontloader.load(fonts)
Some other events on the name
key you can query are:
loading
: Called when the fonts start loading.
failed
: Could not load any fonts. Big problem somewhere!
loaded
: A font has loaded, the name will be in the data.family
key.
error
: A font could not load, the name will be in the data.family
key.
Example
local function displayText()
local txtOne = display.newText( "Hello World", 150, 80, "IBMPlexMono", 48 )
local txtTwo = display.newText( "Hello World", 150, 140, "Roboto", 48 )
end
local function onLoadFonts(evt)
if evt.name == 'loading' then
print("Loading fonts...")
elseif evt.name == 'failed' then
print("Something bad happened")
elseif evt.name == 'ready' then
displayText()
elseif evt.name == 'loaded' then
print("Font "..evt.data.family.." loaded")
elseif evt.name == 'error' then
print("Font "..evt.data.family.." could not load")
end
end
local fonts = {
Roboto = "Roboto-Regular.ttf",
IBMPlexMono = "IBMPlexMono-Regular.ttf"
}
fontloader.addEventListener(onLoadFonts)
fontloader.load(fonts)
©2018 C. Byerley (develephant)