Skip to content

develephant/corona-html5-fontloader-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logo Corona HTML5 Font Loader Plugin

Custom web font injector plugin for Corona HTML5 projects.

Depreciation Notice

This plugin is not needed since Corona daily build 2018.3248, but could be some nice code to study if you're building one.

Setup

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")

API

load

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)

Events

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)

Other Events

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)

Releases

No releases published

Packages

No packages published