| 1 |
----------------------------------------------------------------------- |
| 2 |
-- FILE: luaotfload-loaders.lua |
| 3 |
-- DESCRIPTION: Luaotfload callback handling |
| 4 |
-- REQUIREMENTS: luatex v.0.80 or later; package lualibs |
| 5 |
-- AUTHOR: Philipp Gesang <[email protected]> |
| 6 |
-- AUTHOR: Hans Hagen, Khaled Hosny, Elie Roux, David Carlisle |
| 7 |
----------------------------------------------------------------------- |
| 8 |
|
| 9 |
assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") { |
| 10 |
name = "luaotfload-loaders", |
| 11 |
version = "3.29", --TAGVERSION |
| 12 |
date = "2024-12-03", --TAGDATE |
| 13 |
description = "luaotfload submodule / callback handling", |
| 14 |
license = "GPL v2.0" |
| 15 |
} |
| 16 |
----------------------------------------------------------------------- |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
if not lualibs then error "this module requires Luaotfload" end |
| 21 |
if not luaotfload then error "this module requires Luaotfload" end |
| 22 |
|
| 23 |
local logreport = luaotfload.log and luaotfload.log.report or print |
| 24 |
|
| 25 |
local function lua_reader (specification) |
| 26 |
local fullname = specification.resolved |
| 27 |
if fullname then |
| 28 |
local loader = loadfile (fullname) |
| 29 |
loader = loader and loader () |
| 30 |
return loader and loader (specification) |
| 31 |
end |
| 32 |
end |
| 33 |
|
| 34 |
local function eval_reader (specification) |
| 35 |
local eval = specification.eval |
| 36 |
if not eval or type (eval) ~= "function" then return nil end |
| 37 |
logreport ("both", 0, "loaders", |
| 38 |
"eval: found tfmdata for %q, injecting.", |
| 39 |
specification.name) |
| 40 |
return eval () |
| 41 |
end |
| 42 |
|
| 43 |
local function unsupported_reader (format) |
| 44 |
return function (specification) |
| 45 |
logreport ("both", 4, "loaders", |
| 46 |
"font format %q unsupported; cannot load %s.", |
| 47 |
format, tostring (specification.name)) |
| 48 |
end |
| 49 |
end |
| 50 |
|
| 51 |
local type1_reader = fonts.readers.afm |
| 52 |
local tfm_reader = fonts.readers.tfm |
| 53 |
|
| 54 |
local function install_formats () |
| 55 |
local fonts = fonts |
| 56 |
if not fonts then return false end |
| 57 |
|
| 58 |
local readers = fonts.readers |
| 59 |
local sequence = readers.sequence |
| 60 |
local seqset = table.tohash (sequence) |
| 61 |
local formats = fonts.formats |
| 62 |
if not readers or not formats then return false end |
| 63 |
|
| 64 |
local function aux (which, reader) |
| 65 |
if not which or type (which) ~= "string" |
| 66 |
or not reader or type (reader) ~= "function" then |
| 67 |
logreport ("both", 2, "loaders", "Error installing reader for %q.", which) |
| 68 |
return false |
| 69 |
end |
| 70 |
formats [which] = "type1" |
| 71 |
readers [which] = reader |
| 72 |
if not seqset [which] then |
| 73 |
logreport ("both", 3, "loaders", |
| 74 |
"Extending reader sequence for %q.", which) |
| 75 |
sequence [#sequence + 1] = which |
| 76 |
seqset [which] = true |
| 77 |
end |
| 78 |
return true |
| 79 |
end |
| 80 |
|
| 81 |
return aux ("evl", eval_reader) |
| 82 |
and aux ("lua", lua_reader) |
| 83 |
and aux ("pfa", unsupported_reader "pfa") |
| 84 |
and aux ("afm", type1_reader) |
| 85 |
and aux ("pfb", type1_reader) |
| 86 |
and aux ("tfm", tfm_reader) |
| 87 |
and aux ("ofm", tfm_reader) |
| 88 |
and aux ("dfont", unsupported_reader "dfont") |
| 89 |
end |
| 90 |
|
| 91 |
local function not_found_msg (specification, size, id) |
| 92 |
logreport ("both", 0, "loaders", "") |
| 93 |
logreport ("both", 0, "loaders", |
| 94 |
"--------------------------------------------------------") |
| 95 |
logreport ("both", 0, "loaders", "") |
| 96 |
logreport ("both", 0, "loaders", "Font definition failed for:") |
| 97 |
logreport ("both", 0, "loaders", "") |
| 98 |
logreport ("both", 0, "loaders", " > id : %d", id) |
| 99 |
logreport ("both", 0, "loaders", " > specification : %q", specification) |
| 100 |
if size > 0 then |
| 101 |
logreport ("both", 0, "loaders", " > size : %.2f pt", size / 2^16) |
| 102 |
end |
| 103 |
logreport ("both", 0, "loaders", "") |
| 104 |
logreport ("both", 0, "loaders", |
| 105 |
"--------------------------------------------------------") |
| 106 |
end |
| 107 |
|
| 108 |
local definers --- (string, spec -> size -> id -> tmfdata) hash_t |
| 109 |
do |
| 110 |
local hashed_cache = {} |
| 111 |
local fontdata_cache = fonts.hashes.identifiers |
| 112 |
local function register(fontdata, id) |
| 113 |
if fontdata and id then |
| 114 |
local hash = fontdata.properties.hash |
| 115 |
if not hash then |
| 116 |
logreport('both', 2, 'loaders', "Registering font %s with id %s with missing hash.", id, fontdata.properties.filename or "unknown") |
| 117 |
elseif not hashed_cache[hash] then |
| 118 |
hashed_cache[hash] = id |
| 119 |
logreport('both', 4, 'loaders', "Registering font %s with id %s and hash %s.", id, fontdata.properties.filename or "unknown", hash) |
| 120 |
end |
| 121 |
fontdata_cache[id] = fontdata |
| 122 |
end |
| 123 |
end |
| 124 |
function registered(hash) |
| 125 |
local id = hashed_cache[hash] |
| 126 |
return id, fontdata_cache[id] |
| 127 |
end |
| 128 |
fonts.definers.register = register |
| 129 |
fonts.definers.registered = registered |
| 130 |
|
| 131 |
local extra_hash_funcs = {} |
| 132 |
|
| 133 |
local function ltx_read(spec, size, id) |
| 134 |
spec = type(spec) == 'string' and fonts.definers.analyze(spec, size) or spec |
| 135 |
spec = fonts.definers.resolve(spec) |
| 136 |
|
| 137 |
local hash = fonts.constructors.hashinstance(spec) |
| 138 |
|
| 139 |
-- extra_hash_func allows the loader to determine additional hash parts |
| 140 |
-- depending on the actual font. This is used e.g. for variable fonts which |
| 141 |
-- should be hashed based on their designsize / modifiers too. |
| 142 |
local extra_hash_func = extra_hash_funcs[hash] |
| 143 |
if extra_hash_func then |
| 144 |
hash = hash .. ' @ ' .. table.join(extra_hash_func(spec), ' @ ') |
| 145 |
end |
| 146 |
|
| 147 |
local cached = registered(hash) |
| 148 |
if cached then return cached end -- already loaded |
| 149 |
|
| 150 |
local fontdata = fonts.definers.loadfont(spec) |
| 151 |
if not fontdata then |
| 152 |
logreport('both', 2, 'loaders', 'Failed to load font ' .. spec.name) |
| 153 |
return |
| 154 |
end |
| 155 |
|
| 156 |
-- If extra_hash_func was already set then the hash is already extended. |
| 157 |
-- Otherwise we have to do it now if requested. |
| 158 |
if not extra_hash_func then |
| 159 |
extra_hash_func = fontdata.properties.extra_hash_func |
| 160 |
if extra_hash_func then |
| 161 |
hash = hash .. ' @ ' .. table.join(extra_hash_func(spec), ' @ ') |
| 162 |
end |
| 163 |
end |
| 164 |
fontdata.properties.hash = hash |
| 165 |
register(fontdata, id) |
| 166 |
return fontdata |
| 167 |
end |
| 168 |
|
| 169 |
local function read(specification, size, id) |
| 170 |
local fontdata = ltx_read(specification, size, id) |
| 171 |
if not fontdata or id or tonumber(fontdata) then |
| 172 |
return fontdata |
| 173 |
end |
| 174 |
id = font.define(fontdata) |
| 175 |
register(fontdata, id) |
| 176 |
return id |
| 177 |
end |
| 178 |
|
| 179 |
local function patch (specification, size, id) |
| 180 |
local fontdata = ltx_read (specification, size, id) |
| 181 |
----if not fontdata then not_found_msg (specification, size, id) end |
| 182 |
if type (fontdata) == "table" and fontdata.encodingbytes == 2 then |
| 183 |
--- We need to test for `encodingbytes` to avoid passing |
| 184 |
--- tfm fonts to `patch_font`. These fonts are fragile |
| 185 |
--- because they use traditional TeX font handling. |
| 186 |
luatexbase.call_callback ("luaotfload.patch_font", fontdata, specification, id) |
| 187 |
else |
| 188 |
luatexbase.call_callback ("luaotfload.patch_font_unsafe", fontdata, specification, id) |
| 189 |
end |
| 190 |
if not fontdata or id or tonumber(fontdata) then |
| 191 |
return fontdata |
| 192 |
end |
| 193 |
id = font.define(fontdata) |
| 194 |
register(fontdata, id) |
| 195 |
return id |
| 196 |
end |
| 197 |
|
| 198 |
local function mk_info (name) |
| 199 |
local definer = name == "patch" and patch or read |
| 200 |
return function (specification, size, id) |
| 201 |
logreport ("both", 0, "loaders", "defining font no. %d", id) |
| 202 |
logreport ("both", 0, "loaders", " > active font definer: %q", name) |
| 203 |
logreport ("both", 0, "loaders", " > spec %q", specification) |
| 204 |
logreport ("both", 0, "loaders", " > at size %.2f pt", size / 2^16) |
| 205 |
local result = definer (specification, size, id) |
| 206 |
if not result then return not_found_msg (specification, size, id) end |
| 207 |
if type (result) == "number" then |
| 208 |
logreport ("both", 0, "loaders", " > font definition yielded id %d", result) |
| 209 |
return result |
| 210 |
end |
| 211 |
logreport ("both", 0, "loaders", " > font definition successful") |
| 212 |
logreport ("both", 0, "loaders", " > name %q", result.name or "<nil>") |
| 213 |
logreport ("both", 0, "loaders", " > fontname %q", result.fontname or "<nil>") |
| 214 |
logreport ("both", 0, "loaders", " > fullname %q", result.fullname or "<nil>") |
| 215 |
logreport ("both", 0, "loaders", " > type %s", result.type or "<nil>") |
| 216 |
local spec = result.specification |
| 217 |
if spec then |
| 218 |
logreport ("both", 0, "loaders", " > file %q", spec.filename or "<nil>") |
| 219 |
logreport ("both", 0, "loaders", " > subfont %s", spec.sub or "<nil>") |
| 220 |
end |
| 221 |
return result |
| 222 |
end |
| 223 |
end |
| 224 |
|
| 225 |
definers = { |
| 226 |
patch = patch, |
| 227 |
generic = read, |
| 228 |
info_patch = mk_info "patch", |
| 229 |
info_generic = mk_info "generic", |
| 230 |
} |
| 231 |
end |
| 232 |
|
| 233 |
--[[doc-- |
| 234 |
|
| 235 |
We create callbacks for patching fonts on the fly, to be used by |
| 236 |
other packages. In addition to the regular \identifier{patch_font} |
| 237 |
callback there is an unsafe variant \identifier{patch_font_unsafe} |
| 238 |
that will be invoked even if the target font lacks certain essential |
| 239 |
tfmdata tables. |
| 240 |
|
| 241 |
The callbacks initially contain the empty function that we are going |
| 242 |
to override below. |
| 243 |
|
| 244 |
--doc]]-- |
| 245 |
|
| 246 |
local function purge_define_font () |
| 247 |
local cdesc = luatexbase.callback_descriptions "define_font" |
| 248 |
--- define_font is an “exclusive” callback, meaning that there can |
| 249 |
--- only ever be one entry. Everything beyond that would indicate |
| 250 |
--- that something is broken. |
| 251 |
local _, d = next (cdesc) |
| 252 |
if d then |
| 253 |
local i, d2 = next (cdesc, 1) |
| 254 |
if d2 then --> issue warning |
| 255 |
logreport ("both", 0, "loaders", |
| 256 |
"Callback table for define_font contains multiple entries: \z |
| 257 |
{ [%d] = %q } -- seems fishy.", i, d2) |
| 258 |
end |
| 259 |
logreport ("log", 0, "loaders", |
| 260 |
"Entry %q present in define_font callback; overriding.", d) |
| 261 |
luatexbase.remove_from_callback ("define_font", d) |
| 262 |
end |
| 263 |
end |
| 264 |
|
| 265 |
local install_callbacks = function () |
| 266 |
local create_callback = luatexbase.create_callback |
| 267 |
local dummy_function = function () end |
| 268 |
create_callback ("luaotfload.patch_font", "simple", dummy_function) |
| 269 |
create_callback ("luaotfload.patch_font_unsafe", "simple", dummy_function) |
| 270 |
purge_define_font () |
| 271 |
local definer = config.luaotfload.run.definer or "patch" |
| 272 |
local selected_definer = definers[definer] |
| 273 |
luaotfload.define_font = selected_definer |
| 274 |
luatexbase.add_to_callback ("define_font", |
| 275 |
selected_definer, |
| 276 |
"luaotfload.define_font", |
| 277 |
1) |
| 278 |
return true |
| 279 |
end |
| 280 |
|
| 281 |
return function () |
| 282 |
if not install_formats () then |
| 283 |
logreport ("log", 0, "loaders", "Error initializing OFM/PF{A,B} loaders.") |
| 284 |
return false |
| 285 |
end |
| 286 |
if not install_callbacks () then |
| 287 |
logreport ("log", 0, "loaders", "Error installing font loader callbacks.") |
| 288 |
return false |
| 289 |
end |
| 290 |
return true |
| 291 |
end |
| 292 |
-- vim:tw=79:sw=2:ts=2:expandtab |