summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-resolvers.lua
blob: d932aee3e4657e5eef6d6aa1eb1f3260d8587f41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
-----------------------------------------------------------------------
--         FILE:  luaotfload-resolvers.lua
--        USAGE:  ./luaotfload-resolvers.lua 
--  DESCRIPTION:  Resolvers for hooking into the fontloader
-- REQUIREMENTS:  Luaotfload and a decent bit of courage
--       AUTHOR:  Philipp Gesang (Phg), <phg@phi-gamma.net>
-----------------------------------------------------------------------
--
--- The bare fontloader uses a set of simplistic file name resolvers
--- that must be overloaded by the user (i. e. us).

local ProvidesLuaModule = { 
    name          = "luaotfload-resolvers",
    version       = "2.94",       --TAGVERSION
    date          = "2018-12-19", --TAGDATE
    description   = "luaotfload submodule / resolvers",
    license       = "GPL v2.0"
}

if luatexbase and luatexbase.provides_module then
  luatexbase.provides_module (ProvidesLuaModule)
end  


if not lualibs    then error "this module requires Luaotfload" end
if not luaotfload then error "this module requires Luaotfload" end

--[[doc--

    Relying on the \verb|name:| resolver for everything has been the
    source of permanent trouble with the database.
    With the introduction of the new syntax parser we now have enough
    granularity to distinguish between the \XETEX emulation layer and
    the genuine \verb|name:| and \verb|file:| lookups of \LUATEX-Fonts.
    Another benefit is that we can now easily plug in or replace new
    lookup behaviors if necessary.
    The name resolver remains untouched, but it calls
    \luafunction{fonts.names.resolve()} internally anyways (see
    \fileent{luaotfload-database.lua}).

--doc]]--

local next                = next
local tableconcat         = table.concat
local kpsefind_file       = kpse.find_file
local lfsisfile           = lfs.isfile
local stringlower         = string.lower
local stringformat        = string.format
local filesuffix          = file.suffix
local fileremovesuffix    = file.removesuffix
local luatexbase          = luatexbase
local logreport           = luaotfload.log.report

--[[doc--

    \identifier{luaotfload} promises easy access to system fonts.
    Without additional precautions, this cannot be achieved by
    \identifier{kpathsea} alone, because it searches only the
    \fileent{texmf} directories by default.
    Although it is possible for \identifier{kpathsea} to include extra
    paths by adding them to the \verb|OSFONTDIR| environment variable,
    this is still short of the goal »\emphasis{it just works!}«.
    When building the font database \identifier{luaotfload} scans
    system font directories anyways, so we already have all the
    information for looking sytem fonts.
    With the release version 2.2 the file names are indexed in the
    database as well and we are ready to resolve \verb|file:| lookups
    this way.
    Thus we no longer need to call the \identifier{kpathsea} library in
    most cases when looking up font files, only when generating the
    database, and when verifying the existence of a file in the
    \fileent{texmf} tree.

--doc]]--

local resolve_file
resolve_file = function (specification)
    local name, _format, success = fonts.names.lookup_font_file (specification.name)
    local suffix = filesuffix (name)
    if not specification.forced and fonts.formats[suffix] then
        specification.forced      = stringlower (suffix)
        specification.forcedname  = fileremovesuffix (name)
    else
        specification.name = name
    end
    if success ~= true then
        logreport ("log", 1, "resolve", "file lookup of %q unsuccessful", name)
    end
    return success
end

--[[doc--

    Prior to version 2.2, \identifier{luaotfload} did not distinguish
    \verb|file:| and \verb|path:| lookups, causing complications with
    the resolver.
    Now we test if the requested name is an absolute path in the file
    system, otherwise we fall back to the \verb|file:| lookup.

--doc]]--

local resolve_path
resolve_path = function (specification)
    local name       = specification.name
    local exists, _  = lfsisfile (name)
    if not exists then -- resort to file: lookup
        logreport ("log", 1, "resolve",
                   "path lookup of %q unsuccessful, falling back to file:",
                   name)
        return resolve_file (specification)
    end
    local suffix = filesuffix (name)
    if not specification.forced and fonts.formats [suffix] then
        specification.forced      = stringlower (suffix)
        specification.name        = fileremovesuffix (name)
        specification.forcedname  = name
    else
        specification.name = name
    end
    return true
end

--[[doc--

    The \verb|name:| resolver.

--doc]]--

--- fonts.names.resolvers.name -- Customized version of the
--- generic name resolver.

local resolve_name
resolve_name = function (specification)
    local resolver = fonts.names.lookup_font_name_cached
    if config.luaotfload.run.resolver == "normal" then
        resolver = fonts.names.lookup_font_name
    end
    local resolved, subfont = resolver (specification)
    if resolved then
        logreport ("log", 1, "resolve", "name lookup %q -> \"%s%s\"",
                   specification.name, resolved,
                   subfont and stringformat ("(%d)", subfont) or "")
        specification.resolved   = resolved
        specification.sub        = subfont
        specification.forced     = specification.forced
                                or stringlower (filesuffix (resolved) or "")
        specification.forcedname = resolved
        specification.name       = fileremovesuffix (resolved)
        return true
    end
    return resolve_file (specification)
end

--[[doc--

    We classify as \verb|anon:| those requests that have neither a
    prefix nor brackets. According to Khaled\footnote{%
        % XXX dead link‽
        \url{https://github.com/phi-gamma/luaotfload/issues/4#issuecomment-17090553}.
    }
    they are the \XETEX equivalent of a \verb|name:| request, so we
    will be treating them as such or, at least, in a similar fashion.

    Not distinguishing between “anon” and “name” requests has a serious
    drawback: The syntax is overloaded for requesting fonts in
    \identifier{Type1} (\abbrev{tfm}, \abbrev{ofm}) format.
    These are essentially \verb|file:| lookups and must be caught
    before the \verb|name:| resolver kicks in, lest they cause the
    database to update.
    Even if we were to require the \verb|file:| prefix for all
    \identifier{Type1} requests, tests have shown that certain fonts
    still include further fonts (e.~g. \fileent{omlgcb.ofm} will ask
    for \fileent{omsecob.tfm}) \emphasis{using the old syntax}.
    For this reason, we introduce an extra check with an early return.

--doc]]--

local tex_formats = { "tfm", "ofm", "TFM", "OFM", }

local resolve_tex_format = function (specification)
    local name = specification.name
    for i=1, #tex_formats do
        local format = tex_formats [i]
        local suffix = filesuffix (name)
        if resolvers.findfile (name, format) then
            local usename = suffix == format and fileremovesuffix (name) or name
            specification.forcedname = file.addsuffix (usename, format)
            specification.forced     = specification.forced or format
----        specification.resolved   = name
            return true
        end
    end
    return false
end

local resolve_path_if_exists = function (specification)
    local spec = specification.specification
    local exists, _void = lfsisfile (spec)
    if exists then
        --- If this path is taken a file matching the specification
        --- literally was found. In this situation, Luaotfload is
        --- expected to load that file directly, even though we provide
        --- explicit “file” and “path” lookups to address exactly this
        --- situation.
        logreport ("log", 1, "resolve",
                   "file %q exists, performing path lookup", spec)
        specification.name = spec
        return resolve_path (specification)
    end
    return false
end

--[[doc--
    Custom file resolvers via callback.
--doc]]--

local resolve_my = function (specification)
    luatexbase.call_callback ("luaotfload.resolve_font", specification)
end

local resolve_methods = {
    tex  = resolve_tex_format,
    path = resolve_path_if_exists,
    name = resolve_name,
    file = resolve_file,
    my   = resolve_my,
}

local resolve_sequence = function (seq, specification)
    for i = 1, #seq do
        local id  = seq [i]
        local mth = resolve_methods [id]
        if not mth then
            logreport ("both", 0, "resolve",
                       "step %d: invalid lookup method %q", i, id)
        else
            logreport ("both", 3, "resolve", "step %d: apply method %q (%s)", i, id, mth)
            if mth (specification) == true then
                logreport ("both", 3, "resolve",
                           "%d: method %q resolved %q -> %s (%s).",
                           i, id, specification.specification,
                           specification.name,
                           specification.forcedname)
                return true
            end
        end
    end
    logreport ("both", 0, "resolve",
               "sequence of %d lookups yielded nothing appropriate.", #seq)
    return false
end

local default_anon_sequence = {
    "tex", "path", "name",
}

local resolve_anon
resolve_anon = function (specification)
    local seq = default_anon_sequence
    if config and config.luaotfload then
        local anonseq = config.luaotfload.run.anon_sequence
        if anonseq and next (anonseq) then
            seq = anonseq
        end
    end
    return resolve_sequence (seq, specification)
end

--[[doc--

    {\bfseries EXPERIMENTAL}:
    \identifier{kpse}-only resolver, for those who can do without
    system fonts.

--doc]]--

local resolve_kpse
resolve_kpse = function (specification)
    local name       = specification.name
    local suffix     = filesuffix (name)
    if not specification.forced and suffix and fonts.formats[suffix] then
        name = fileremovesuffix (name)
        if resolvers.findfile (name, suffix) then
            specification.forced       = stringlower (suffix)
            specification.forcedname   = name
            return true
        end
    end
    for t, format in next, fonts.formats do --- brute force
        if kpsefind_file (name, format) then
            specification.forced = t
            specification.name   = name
            return true
        end
    end
    return false
end

return {
    init = function ( )
        if luatexbase and luatexbase.create_callback then
            luatexbase.create_callback ("luaotfload.resolve_font",
                                        "simple", function () end)
        end
        logreport ("log", 5, "resolvers", "installing font resolvers", name)
        local request_resolvers = fonts.definers.resolvers
        request_resolvers.file = resolve_file
        request_resolvers.name = resolve_name
        request_resolvers.anon = resolve_anon
        request_resolvers.path = resolve_path
        request_resolvers.kpse = resolve_kpse
        request_resolvers.my   = resolve_my
        fonts.formats.ofm      = "type1"
        fonts.encodings        = fonts.encodings       or { }
        fonts.encodings.known  = fonts.encodings.known or { }
        return true
    end, --- [.init]
}

--- vim:ft=lua:ts=8:sw=4:et:tw=79