项目作者: develephant

项目描述 :
Custom web font injector plugin for Corona HTML5 projects.
高级语言: JavaScript
项目地址: git://github.com/develephant/corona-html5-fontloader-plugin.git
创建时间: 2018-03-26T13:54:31Z
项目社区:https://github.com/develephant/corona-html5-fontloader-plugin

开源协议:Apache License 2.0

下载


Corona HTML5 Font Loader Plugin" class="reference-link">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:

  1. -- main.lua
  2. local fontloader = require("fontloader")

API

load

Load in the specified custom fonts for use.

  1. 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

  1. local fonts = {
  2. Roboto = "Roboto-Regular.ttf",
  3. IBMPlexMono = "IBMPlexMono-Regular.ttf"
  4. }
  5. 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:

  1. local function displayText()
  2. local txtOne = display.newText( "Hello World", 150, 80, "IBMPlexMono", 48 )
  3. local txtTwo = display.newText( "Hello World", 150, 140, "Roboto", 48 )
  4. end
  5. local function onLoadFonts(evt)
  6. if evt.name == 'ready' then
  7. displayText()
  8. end
  9. end
  10. local fonts = {
  11. Roboto = "Roboto-Regular.ttf",
  12. IBMPlexMono = "IBMPlexMono-Regular.ttf"
  13. }
  14. fontloader.addEventListener(onLoadFonts)
  15. 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

  1. local function displayText()
  2. local txtOne = display.newText( "Hello World", 150, 80, "IBMPlexMono", 48 )
  3. local txtTwo = display.newText( "Hello World", 150, 140, "Roboto", 48 )
  4. end
  5. local function onLoadFonts(evt)
  6. if evt.name == 'loading' then
  7. print("Loading fonts...")
  8. elseif evt.name == 'failed' then
  9. print("Something bad happened")
  10. elseif evt.name == 'ready' then
  11. displayText()
  12. elseif evt.name == 'loaded' then
  13. print("Font "..evt.data.family.." loaded")
  14. elseif evt.name == 'error' then
  15. print("Font "..evt.data.family.." could not load")
  16. end
  17. end
  18. local fonts = {
  19. Roboto = "Roboto-Regular.ttf",
  20. IBMPlexMono = "IBMPlexMono-Regular.ttf"
  21. }
  22. fontloader.addEventListener(onLoadFonts)
  23. fontloader.load(fonts)

©2018 C. Byerley (develephant)