Skip to content

Commit

Permalink
feat(fonts): Allow for code to be run when a font is first loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens authored and alerque committed Feb 16, 2021
1 parent bf74417 commit bdf05ab
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion core/font.lua
Expand Up @@ -42,6 +42,11 @@ SILE.registerCommand("font", function (options, content)
SILE.settings.set("font.hyphenchar", SU.utf8charfromcodepoint(options.hyphenchar))
end

-- We must *actually* load the font here, because by the time we're inside
-- SILE.shaper.shapeToken, it's too late to respond appropriately to things
-- that the post-load hook might want to do.
SILE.font.cache(SILE.font.loadDefaults({}), SILE.shaper.getFace)

if type(content) == "function" or content[1] then
SILE.process(content)
SILE.settings.popState()
Expand Down Expand Up @@ -95,10 +100,20 @@ SILE.font = {
local key = _key(options)
if not SILE.fontCache[key] then
SU.debug("fonts", "Looking for "..key)
SILE.fontCache[key] = callback(options)
local face = callback(options)
SILE.font.postLoadHook(face)
SILE.fontCache[key] = face
end
return SILE.fontCache[key]
end,

postLoadHook = function(face)
local ot = SILE.require("core/opentype-parser")
local font = ot.parseFont(face)
if font.cpal then
SILE.require("packages/color-fonts")
end
end,

_key = _key
}

0 comments on commit bdf05ab

Please sign in to comment.