| 1 |
----------------------------------------------------------------------- |
| 2 |
-- FILE: luaotfload-resolvers.lua |
| 3 |
-- USAGE: ./luaotfload-resolvers.lua |
| 4 |
-- DESCRIPTION: Resolvers for hooking into the fontloader |
| 5 |
-- REQUIREMENTS: Luaotfload and a decent bit of courage |
| 6 |
-- AUTHOR: Philipp Gesang (Phg), <[email protected]> |
| 7 |
----------------------------------------------------------------------- |
| 8 |
-- |
| 9 |
--- The bare fontloader uses a set of simplistic file name resolvers |
| 10 |
--- that must be overloaded by the user (i. e. us). |
| 11 |
|
| 12 |
assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") { |
| 13 |
name = "luaotfload-resolvers", |
| 14 |
version = "3.29", --TAGVERSION |
| 15 |
date = "2024-12-03", --TAGDATE |
| 16 |
description = "luaotfload submodule / resolvers", |
| 17 |
license = "GPL v2.0" |
| 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 |
--[[doc-- |
| 24 |
|
| 25 |
Relying on the \verb|name:| resolver for everything has been the |
| 26 |
source of permanent trouble with the database. |
| 27 |
With the introduction of the new syntax parser we now have enough |
| 28 |
granularity to distinguish between the \XETEX emulation layer and |
| 29 |
the genuine \verb|name:| and \verb|file:| lookups of \LUATEX-Fonts. |
| 30 |
Another benefit is that we can now easily plug in or replace new |
| 31 |
lookup behaviors if necessary. |
| 32 |
The name resolver remains untouched, but it calls |
| 33 |
\luafunction{fonts.names.resolve()} internally anyways (see |
| 34 |
\fileent{luaotfload-database.lua}). |
| 35 |
|
| 36 |
--doc]]-- |
| 37 |
|
| 38 |
local next = next |
| 39 |
local tableconcat = table.concat |
| 40 |
local kpsefind_file = kpse.find_file |
| 41 |
local lfsisfile = lfs.isfile |
| 42 |
local stringlower = string.lower |
| 43 |
local stringformat = string.format |
| 44 |
local filesuffix = file.suffix |
| 45 |
local fileremovesuffix = file.removesuffix |
| 46 |
local luatexbase = luatexbase |
| 47 |
local logreport = luaotfload.log.report |
| 48 |
|
| 49 |
--[[doc-- |
| 50 |
|
| 51 |
\identifier{luaotfload} promises easy access to system fonts. |
| 52 |
Without additional precautions, this cannot be achieved by |
| 53 |
\identifier{kpathsea} alone, because it searches only the |
| 54 |
\fileent{texmf} directories by default. |
| 55 |
Although it is possible for \identifier{kpathsea} to include extra |
| 56 |
paths by adding them to the \verb|OSFONTDIR| environment variable, |
| 57 |
this is still short of the goal »\emphasis{it just works!}«. |
| 58 |
When building the font database \identifier{luaotfload} scans |
| 59 |
system font directories anyways, so we already have all the |
| 60 |
information for looking system fonts. |
| 61 |
With the release version 2.2 the file names are indexed in the |
| 62 |
database as well and we are ready to resolve \verb|file:| lookups |
| 63 |
this way. |
| 64 |
Thus we no longer need to call the \identifier{kpathsea} library in |
| 65 |
most cases when looking up font files, only when generating the |
| 66 |
database, and when verifying the existence of a file in the |
| 67 |
\fileent{texmf} tree. |
| 68 |
|
| 69 |
--doc]]-- |
| 70 |
|
| 71 |
local function resolve_file (specification) |
| 72 |
local name, _format, success = fonts.names.lookup_font_file (specification.name) |
| 73 |
if success then return name end |
| 74 |
logreport ("log", 1, "resolve", "file lookup of %q unsuccessful", name) |
| 75 |
end |
| 76 |
|
| 77 |
--[[doc-- |
| 78 |
|
| 79 |
Prior to version 2.2, \identifier{luaotfload} did not distinguish |
| 80 |
\verb|file:| and \verb|path:| lookups, causing complications with |
| 81 |
the resolver. |
| 82 |
Now we test if the requested name is an absolute path in the file |
| 83 |
system, otherwise we fall back to the \verb|file:| lookup. |
| 84 |
|
| 85 |
--doc]]-- |
| 86 |
|
| 87 |
local function resolve_path (specification) |
| 88 |
local name = specification.name |
| 89 |
if lfsisfile (name) then return name end |
| 90 |
logreport ("log", 1, "resolve", |
| 91 |
"path lookup of %q unsuccessful, falling back to file:", |
| 92 |
name) |
| 93 |
return resolve_file (specification) |
| 94 |
end |
| 95 |
|
| 96 |
--[[doc-- |
| 97 |
|
| 98 |
The \verb|name:| resolver. |
| 99 |
|
| 100 |
--doc]]-- |
| 101 |
|
| 102 |
--- fonts.names.resolvers.name -- Customized version of the |
| 103 |
--- generic name resolver. |
| 104 |
|
| 105 |
local function resolve_name (specification) |
| 106 |
local resolver = fonts.names.lookup_font_name_cached |
| 107 |
if config.luaotfload.run.resolver == "normal" then |
| 108 |
resolver = fonts.names.lookup_font_name |
| 109 |
end |
| 110 |
local resolved, subfont = resolver (specification) |
| 111 |
if resolved then |
| 112 |
logreport ("log", 1, "resolve", "name lookup %q -> %q", |
| 113 |
specification.name, resolved .. |
| 114 |
(subfont and stringformat ("(%d)", subfont) or "")) |
| 115 |
return resolved, tonumber(subfont) |
| 116 |
end |
| 117 |
return resolve_file (specification) |
| 118 |
end |
| 119 |
|
| 120 |
--[[doc-- |
| 121 |
|
| 122 |
We classify as \verb|anon:| those requests that have neither a |
| 123 |
prefix nor brackets. According to Khaled\footnote{% |
| 124 |
% XXX dead link‽ |
| 125 |
\url{https://github.com/phi-gamma/luaotfload/issues/4#issuecomment-17090553}. |
| 126 |
} |
| 127 |
they are the \XETEX equivalent of a \verb|name:| request, so we |
| 128 |
will be treating them as such or, at least, in a similar fashion. |
| 129 |
|
| 130 |
Not distinguishing between “anon” and “name” requests has a serious |
| 131 |
drawback: The syntax is overloaded for requesting fonts in |
| 132 |
\identifier{Type1} (\abbrev{tfm}, \abbrev{ofm}) format. |
| 133 |
These are essentially \verb|file:| lookups and must be caught |
| 134 |
before the \verb|name:| resolver kicks in, lest they cause the |
| 135 |
database to update. |
| 136 |
Even if we were to require the \verb|file:| prefix for all |
| 137 |
\identifier{Type1} requests, tests have shown that certain fonts |
| 138 |
still include further fonts (e.~g. \fileent{omlgcb.ofm} will ask |
| 139 |
for \fileent{omsecob.tfm}) \emphasis{using the old syntax}. |
| 140 |
For this reason, we introduce an extra check with an early return. |
| 141 |
|
| 142 |
--doc]]-- |
| 143 |
|
| 144 |
local tex_formats = { "tfm", "ofm" } |
| 145 |
|
| 146 |
local resolvers_findfile = luaotfload.fontloader.resolvers.findfile |
| 147 |
local function resolve_tex_format (specification) |
| 148 |
local name = specification.name |
| 149 |
for i=1, #tex_formats do |
| 150 |
local format = tex_formats [i] |
| 151 |
local name = name |
| 152 |
if name:sub(-#format-1) ~= '.' .. format then -- Add an explicit extension to avoid finding local fonts in other formats |
| 153 |
name = name .. '.' .. format |
| 154 |
end |
| 155 |
local resolved = resolvers_findfile(name, format) |
| 156 |
if resolved then |
| 157 |
return resolved, format |
| 158 |
end |
| 159 |
end |
| 160 |
end |
| 161 |
|
| 162 |
local function resolve_path_if_exists (specification) |
| 163 |
local spec = specification.specification |
| 164 |
local exists = lfsisfile (spec) |
| 165 |
if exists then |
| 166 |
--- If this path is taken a file matching the specification |
| 167 |
--- literally was found. In this situation, Luaotfload is |
| 168 |
--- expected to load that file directly, even though we provide |
| 169 |
--- explicit “file” and “path” lookups to address exactly this |
| 170 |
--- situation. |
| 171 |
logreport ("log", 1, "resolve", |
| 172 |
"file %q exists, performing path lookup", spec) |
| 173 |
return spec |
| 174 |
end |
| 175 |
end |
| 176 |
|
| 177 |
--[[doc-- |
| 178 |
Custom file resolvers via callback. |
| 179 |
--doc]]-- |
| 180 |
|
| 181 |
local function resolve_my (specification) |
| 182 |
return luatexbase.call_callback ("luaotfload.resolve_font", specification) |
| 183 |
end |
| 184 |
|
| 185 |
local resolve_methods = { |
| 186 |
tex = resolve_tex_format, |
| 187 |
path = resolve_path_if_exists, |
| 188 |
name = resolve_name, |
| 189 |
file = resolve_file, |
| 190 |
my = resolve_my, |
| 191 |
} |
| 192 |
|
| 193 |
local function resolve_sequence (seq, specification) |
| 194 |
for i = 1, #seq do |
| 195 |
local id = seq [i] |
| 196 |
local mth = resolve_methods [id] |
| 197 |
if not mth then |
| 198 |
logreport ("both", 0, "resolve", |
| 199 |
"step %d: invalid lookup method %q", i, id) |
| 200 |
else |
| 201 |
logreport ("both", 3, "resolve", "step %d: apply method %q (%s)", i, id, mth) |
| 202 |
local result, t = mth (specification) |
| 203 |
if result then |
| 204 |
logreport ("both", 3, "resolve", |
| 205 |
"%d: method %q resolved %q -> %s.", |
| 206 |
i, id, specification.specification, result) |
| 207 |
return result, t |
| 208 |
end |
| 209 |
end |
| 210 |
end |
| 211 |
logreport ("both", 0, "resolve", |
| 212 |
"sequence of %d lookups yielded nothing appropriate.", #seq) |
| 213 |
end |
| 214 |
|
| 215 |
local default_anon_sequence = { |
| 216 |
"tex", "path", "name", |
| 217 |
} |
| 218 |
|
| 219 |
local function resolve_anon (specification) |
| 220 |
return resolve_sequence (config.luaotfload.run.anon_sequence, specification) |
| 221 |
end |
| 222 |
|
| 223 |
--[[doc-- |
| 224 |
|
| 225 |
{\bfseries EXPERIMENTAL}: |
| 226 |
\identifier{kpse}-only resolver, for those who can do without |
| 227 |
system fonts. |
| 228 |
|
| 229 |
--doc]]-- |
| 230 |
|
| 231 |
local function resolve_kpse (specification) |
| 232 |
local name = specification.name |
| 233 |
local suffix = stringlower (filesuffix (name)) |
| 234 |
if suffix and fonts.formats[suffix] then |
| 235 |
local resolved = resolvers_findfile(name, suffix) |
| 236 |
if resolved then return resolved end |
| 237 |
end |
| 238 |
for _, t in ipairs{'otf', 'ttf', 'pfb', 'lua', 'afm'} do --- brute force |
| 239 |
local resolved = resolvers_findfile (name, t) |
| 240 |
if resolved then return resolved, t end |
| 241 |
end |
| 242 |
end |
| 243 |
|
| 244 |
local lookup_subfont_index = fonts.names.lookup_subfont_index |
| 245 |
local function wrap_resolver(resolver) |
| 246 |
return function (specification) |
| 247 |
local filename, sub, forced = resolver(specification) |
| 248 |
if type(sub) ~= "number" then |
| 249 |
forced = sub |
| 250 |
sub = nil |
| 251 |
end |
| 252 |
if filename then |
| 253 |
specification.resolved = filename |
| 254 |
specification.filename = filename |
| 255 |
specification.name = filename |
| 256 |
specification.sub = sub or specification.sub |
| 257 |
if type(specification.sub) == "string" then |
| 258 |
specification.sub = |
| 259 |
lookup_subfont_index(filename, specification.sub) |
| 260 |
end |
| 261 |
specification.forced = specification.forced or forced |
| 262 |
if not specification.forced then |
| 263 |
local suffix = stringlower (filesuffix (filename)) |
| 264 |
if suffix and fonts.formats[suffix] then |
| 265 |
specification.forced = suffix |
| 266 |
end |
| 267 |
end |
| 268 |
if specification.forced then |
| 269 |
specification.forcedname = filename |
| 270 |
end |
| 271 |
return true |
| 272 |
end |
| 273 |
return false |
| 274 |
end |
| 275 |
end |
| 276 |
|
| 277 |
local resolvers = table.merge(luaotfload.resolvers, { |
| 278 |
path = resolve_path, |
| 279 |
name = resolve_name, |
| 280 |
anon = resolve_anon, |
| 281 |
file = resolve_file, |
| 282 |
kpse = resolve_kpse, |
| 283 |
my = resolve_my, |
| 284 |
}) |
| 285 |
luaotfload.resolvers = resolvers |
| 286 |
|
| 287 |
return function() |
| 288 |
if luatexbase and luatexbase.create_callback then |
| 289 |
luatexbase.create_callback ("luaotfload.resolve_font", |
| 290 |
"exclusive", function () end) |
| 291 |
end |
| 292 |
logreport ("log", 5, "resolvers", "installing font resolvers", name) |
| 293 |
local request_resolvers = fonts.definers.resolvers |
| 294 |
for k, _ in pairs(request_resolvers) do |
| 295 |
request_resolvers[k] = nil |
| 296 |
end |
| 297 |
setmetatable(request_resolvers, {__index = function(t, n) |
| 298 |
if not resolvers[n] then return end |
| 299 |
local wrapped = wrap_resolver(resolvers[n]) |
| 300 |
t[n] = wrapped |
| 301 |
return wrapped |
| 302 |
end}) |
| 303 |
fonts.formats.ofm = "type1" |
| 304 |
fonts.encodings = fonts.encodings or { } |
| 305 |
fonts.encodings.known = fonts.encodings.known or { } |
| 306 |
return true |
| 307 |
end |
| 308 |
--- vim:ft=lua:ts=8:sw=4:et:tw=79 |