summaryrefslogtreecommitdiff
path: root/luaotfload-override.lua
blob: ea6af9aa3a82b45cac862776d579c48fe5e4116c (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
if not modules then modules = { } end modules ['luat-ovr'] = {
    version   = "2.3a",
    comment   = "companion to luatex-*.tex",
    author    = "Khaled Hosny, Elie Roux, Philipp Gesang",
    copyright = "Luaotfload Development Team",
    license   = "GNU GPL v2"
}

--[[doc--
The logging system is slow in general, as we always have the function
call overhead even if we aren’t going to output anything. On the other
hand, the more efficient approach followed by Context isn’t an option
because we lack a user interface to toggle per-subsystem tracing.
--doc]]--

local module_name       = "luaotfload"

local ioopen            = io.open
local iowrite           = io.write
local lfsisdir          = lfs.isdir
local lfsisfile         = lfs.isfile
local md5sumhexa        = md5.sumhexa
local osdate            = os.date
local ostime            = os.time
local select            = select
local stringformat      = string.format
local stringsub         = string.sub
local tableconcat       = table.concat
local texio_write_nl    = texio.write_nl
local texiowrite_nl     = texio.write_nl
local texio_write       = texio.write
local texiowrite        = texio.write
local type              = type

local dummyfunction     = function () end

local texjob = false
if tex and (tex.jobname or tex.formatname) then
    --- TeX
    texjob = true
end

--[[doc--
We recreate the verbosity levels previously implemented in font-nms:

    ==========================================================
    lvl      arg  trace_loading  trace_search  suppress_output
    ----------------------------------------------------------
    (0)  ->  -q         ⊥              ⊥            ⊤
    (1)  ->  ∅          ⊥              ⊥            ⊥
    (2)  ->  -v         ⊤              ⊥            ⊥
    (>2) ->  -vv        ⊤              ⊤            ⊥
    ==========================================================

--doc]]--

local loglevel = 0 --- default
local logout   = "log"

--- int -> bool
local set_loglevel = function (n)
    if type(n) == "number" then
        loglevel = n
    end
    return true
end
logs.setloglevel    = set_loglevel
logs.set_loglevel   = set_loglevel
logs.set_log_level  = set_loglevel --- accomodating lazy typists

--- unit -> int
local get_loglevel = function ( )
    return loglevel
end
logs.getloglevel    = get_loglevel
logs.get_loglevel   = get_loglevel
logs.get_log_level  = get_loglevel

local writeln  --- pointer to terminal/log writer
local statusln --- terminal writer that reuses the current line
local first_status = true --- indicate the begin of a status region

local log_msg = [[
logging output redirected to %s
to monitor the progress run "tail -f %s" in another terminal
]]

local tmppath = os.getenv "TMPDIR" or "/tmp"

local choose_logfile = function ( )
    if lfsisdir (tmppath) then
        local fname
        repeat --- ensure that file of that name doesn’t exist
            fname = tmppath .. "/luaotfload-log-"
                            .. stringsub (md5sumhexa (ostime ()), 1, 8)
        until not lfsisfile (fname)
        iowrite (stringformat (log_msg, fname, fname))
        return ioopen (fname, "w")
    end
    --- missing /tmp
    return false
end

local set_logout = function (s, finalizers)
    if s == "stdout" then
        logout = "redirect"
    elseif s == "file" then --- inject custom logger
        logout = "redirect"
        local chan = choose_logfile ()
        chan:write (stringformat ("logging initiated at %s",
                                  osdate ("%F %T", ostime ())))
        local writefile = function (...)
            if select ("#", ...) == 2 then
                chan:write (select (2, ...))
            else
                chan:write (select (1, ...))
            end
        end
        local writefile_nl= function (...)
            chan:write "\n"
            if select ("#", ...) == 2 then
                chan:write (select (2, ...))
            else
                chan:write (select (1, ...))
            end
        end

        local writeln_orig = writeln

        texiowrite    = writefile
        texiowrite_nl = writefile_nl
        writeln       = writefile_nl
        statusln      = dummyfunction

        finalizers[#finalizers+1] = function ()
            chan:write (stringformat ("\nlogging finished at %s\n",
                                      osdate ("%F %T", ostime ())))
            chan:close ()
            texiowrite    = texio.write
            texiowrite_nl = texio.write_nl
            writeln       = writeln_orig
        end
    --else --- remains “log”
    end
    return finalizers
end

logs.set_logout = set_logout

local log = function (category, fmt, ...)
    local res = { module_name, "|", category, ":" }
    if fmt then
        res [#res + 1] = stringformat (fmt, ...)
    end
    texiowrite_nl (logout, tableconcat(res, " "))
end

--- with faux db update with maximum verbosity:
---
---     ---------   --------
---     buffering   time (s)
---     ---------   --------
---     full        4.12
---     line        4.20
---     none        4.39
---     ---------   --------
---

io.stdout:setvbuf "no"
io.stderr:setvbuf "no"

local kill_line = "\r\x1b[K"

if texjob == true then
    --- We imitate the texio.* functions so the output is consistent.
    writeln = function (str)
        iowrite "\n"
        iowrite(str)
    end
    statusln = function (str)
        if first_status == false then
            iowrite (kill_line)
        end
        iowrite (str)
    end
else
    writeln = function (str)
        iowrite(str)
        iowrite "\n"
    end
    statusln = function (str)
        if first_status == false then
            iowrite (kill_line)
        end
        iowrite (str)
    end
end

stdout = function (writer, category, ...)
    local res = { module_name, "|", category, ":" }
    local nargs = select("#", ...)
    if nargs == 0 then
        --writeln tableconcat(res, " ")
        --return
    elseif nargs == 1 then
        res[#res+1] = select(1, ...) -- around 30% faster than unpack()
    else
        res[#res+1] = stringformat(...)
    end
    writer (tableconcat(res, " "))
end

--- at default (zero), we aim to be quiet
local level_ids = { common  = 1, loading = 2, search  = 3 }

--[[doc--

    The names_report logger is used more or less all over luaotfload.
    Its requirements are twofold:

    1) Provide two logging channels, the terminal and the log file;
    2) Allow for control over verbosity levels.

    The first part is addressed by specifying the log *mode* as the
    first argument that can be either “log”, meaning the log file, or
    “both”: log file and stdout. Anything else is taken as referring to
    stdout only.

    Verbosity levels, though not as fine-grained as e.g. Context’s
    system of tracers, allow keeping the logging spam caused by
    different subsystems manageable. By default, luaotfload will not
    emit anything if things are running smoothly on level zero. Only
    warning messages are relayed, while the other messages are skipped
    over. (This is a little sub-optimal performance-wise since the
    function calls to the logger are executed regardless.) The log
    level during a Luatex run can be adjusted by setting the “loglevel”
    field in config.luaotfload, or by calling logs.set_loglevel() as
    defined above.

--doc]]--

local names_report = function (mode, lvl, ...)
    if type(lvl) == "string" then
        lvl = level_ids[lvl]
    end
    if not lvl then lvl = 0 end

    if loglevel >= lvl then
        if mode == "log" then
            log (...)
        elseif mode == "both" and logout ~= "redirect" then
            log (...)
            stdout (writeln, ...)
        else
            stdout (writeln, ...)
        end
    end
end

logs.names_report = names_report

--[[doc--

    status_logger -- Overwrites the most recently printed line of the
    terminal. Its purpose is to provide feedback without spamming
    stdout with irrelevant messages, i.e. when building the database.

    Status logging must be initialized by calling status_start() and
    properly reset via status_stop().

    The arguments low and high indicate the loglevel threshold at which
    linewise and full logging is triggered, respectively. E.g.

            names_status (1, 4, "term", "Hello, world!")

    will print nothing if the loglevel is less than one, reuse the
    current line if the loglevel ranges from one to three inclusively,
    and output the message on a separate line otherwise.

--doc]]--

local status_logger = function (mode, ...)
    if mode == "log" then
        log (...)
    else
        if mode == "both" and logout ~= "redirect" then
            log (...)
            stdout (statusln, ...)
        else
            stdout (statusln, ...)
        end
        first_status = false
    end
end

--[[doc--

    status_start -- Initialize status logging. This installs the status
        logger if the loglevel is in the specified range, and the normal
        logger otherwise.  It also resets the first line state which
        causing the next line printed using the status logger to not kill
        the current line.

--doc]]--

local status_writer
local status_low  = 99
local status_high = 99

local status_start = function (low, high)
    first_status = true
    status_low   = low
    status_high  = high

    if os.type == "windows" --- Assume broken terminal.
    or os.getenv "TERM" == "dumb"
    then
        status_writer = function (mode, ...)
            names_report (mode, high, ...)
        end
        return
    end

    if low <= loglevel and loglevel < high then
        status_writer = status_logger
    else
        status_writer = function (mode, ...)
            names_report (mode, high, ...)
        end
    end
end

--[[doc--

    status_stop -- Finalize a status region by outputting a newline and
    printing a message.

--doc]]--

local status_stop = function (...)
    if first_status == false then
        status_writer(...)
        writeln ""
    end
end

logs.names_status = function (...) status_writer (...) end
logs.names_status_start = status_start
logs.names_status_stop  = status_stop

--[[doc--

    The fontloader comes with the Context logging mechanisms
    inaccessible. Instead, it provides dumb fallbacks based
    on the functions in texio.write*() that can be overridden
    by providing a function texio.reporter().

    The fontloader output can be quite verbose, so we disable
    it entirely by default.

--doc]]--

local texioreporter = function (message)
    names_report("log", 2, message)
end

texio.reporter = texioreporter

--[[doc--

    Adobe Glyph List.
    -------------------------------------------------------------------

    Context provides a somewhat different font-age.lua from an unclear
    origin. Unfortunately, the file name it reads from is hard-coded
    in font-enc.lua, so we have to replace the entire table.

    This shouldn’t cause any complications. Due to its implementation
    the glyph list will be loaded upon loading a OTF or TTF for the
    first time during a TeX run. (If one sticks to TFM/OFM then it is
    never read at all.) For this reason we can install a metatable that
    looks up the file of our choosing and only falls back to the
    Context one in case it cannot be found.

--doc]]--

if fonts then --- need to be running TeX
    if next(fonts.encodings.agl) then
        --- unnecessary because the file shouldn’t be loaded at this time
        --- but we’re just making sure
        fonts.encodings.agl = nil
        collectgarbage"collect"
    end


    fonts.encodings.agl = { }

    setmetatable(fonts.encodings.agl, { __index = function (t, k)
        if k == "unicodes" then
            local glyphlist = resolvers.findfile"luaotfload-glyphlist.lua"
            if glyphlist then
                names_report("log", 1, "load", "loading the Adobe glyph list")
            else
                glyphlist = resolvers.findfile"font-age.lua"
                names_report("both", 0, "load",
                    "loading the extended glyph list from ConTeXt")
            end
            local unicodes      = dofile(glyphlist)
            fonts.encodings.agl = { unicodes = unicodes }
            return unicodes
        else
            return nil
        end
    end })
end

-- vim:tw=71:sw=4:ts=4:expandtab