Debian Codegraph @

texmf-dist/tex/luatex/luaotfload/luaotfload-init.lua

raw
1 -----------------------------------------------------------------------
2 -- FILE: luaotfload-init.lua
3 -- DESCRIPTION: part of luaotfload / font loader initialization
4 -- REQUIREMENTS: luatex v.0.80 or later; packages lualibs
5 -- AUTHOR: Philipp Gesang (Phg), <[email protected]>, Marcel Krüger
6 -----------------------------------------------------------------------
7
8 assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") {
9 name = "luaotfload-init",
10 version = "3.29", --TAGVERSION
11 date = "2024-12-03", --TAGDATE
12 description = "luaotfload submodule / initialization",
13 license = "GPL v2.0"
14 }
15 -----------------------------------------------------------------------
16
17
18 local setmetatable = setmetatable
19 local kpsefind_file = kpse.find_file
20 local lfsisdir = lfs.isdir
21
22 --[[doc--
23
24 Initialization phases:
25
26 - Load Lualibs from package
27 - Set up the logger routines
28 - Load Fontloader
29 - as package specified in configuration
30 - from Context install
31 - (optional: from raw unpackaged files distributed with
32 Luaotfload)
33
34 The initialization of the Lualibs may be made configurable in the
35 future as well allowing to load both the files and the merged package
36 depending on a configuration setting. However, this would require
37 separating out the configuration parser into a self-contained
38 package, which might be problematic due to its current dependency on
39 the Lualibs itself.
40
41 --doc]]--
42
43 local log --- filled in after loading the log module
44 local logreport --- filled in after loading the log module
45
46 --[[doc--
47
48 \subsection{Preparing the Font Loader}
49 We treat the fontloader as a semi-black box so behavior is
50 consistent between formats.
51 We load the fontloader code directly in the same fashion as the
52 Plain format \identifier{luatex-fonts} that is part of Context.
53 How this is executed depends on the presence on the
54 \emphasis{merged font loader code}.
55 In \identifier{luaotfload} this is contained in the file
56 \fileent{luaotfload-merged.lua}.
57 If this file cannot be found, the original libraries from \CONTEXT
58 of which the merged code was composed are loaded instead.
59 Since these files are not shipped with Luaotfload, an installation
60 of Context is required.
61 (Since we pull the fontloader directly from the Context minimals,
62 the necessary Context version is likely to be more recent than that
63 of other TeX distributions like Texlive.)
64 The imported font loader will call \luafunction{callback.register}
65 once while reading \fileent{font-def.lua}.
66 This is unavoidable unless we modify the imported files, but
67 harmless if we make it call a dummy instead.
68 However, this problem might vanish if we decide to do the merging
69 ourselves, like the \identifier{lualibs} package does.
70 With this step we would obtain the freedom to load our own
71 overrides in the process right where they are needed, at the cost
72 of losing encapsulation.
73 The decision on how to progress is currently on indefinite hold.
74
75 --doc]]--
76
77 --- below paths are relative to the texmf-context
78
79 local ignore, ltx, ctx = 0, 1, 2
80
81 local context_module_paths = { -- [ltx] =
82 "tex/generic/context/luatex", -- [ctx] =
83 {"tex/context/base/mkiv", "tex/context/base" },
84 }
85
86 local context_modules = {
87
88 --- Since 2.6 those are directly provided by the Lualibs package.
89 { ignore, "l-lua" },
90 { ignore, "l-lpeg" },
91 { ignore, "l-function" },
92 { ignore, "l-string" },
93 { ignore, "l-table" },
94 { ignore, "l-io" },
95 { ignore, "l-file" },
96 { ignore, "l-boolean" },
97 { ignore, "l-math" },
98 { ignore, "l-unicode" }, -- NEW UF 19.09.2018
99 { ignore, "util-str" },
100 { ignore, "util-fil" },
101
102 --- These constitute the fontloader proper.
103 { ltx, "basics-gen" },
104 { ctx, "data-con" },
105 { ltx, "basics-nod" },
106 { ltx, "basics-chr" }, -- NEW UF 14.12.2018
107 { ctx, "font-ini" },
108 { ltx, "fonts-mis" }, -- NEW UF 19.09.2018
109 { ctx, "font-con" },
110 { ltx, "fonts-enc" },
111 { ctx, "font-cid" },
112 { ctx, "font-map" },
113 -- { ltx, "fonts-syn" }, -- ignore??
114 { ctx, "font-vfc" }, -- NEW UF 19.09.2018
115 { ctx, "font-otr" },
116 { ctx, "font-cff" },
117 { ctx, "font-ttf" },
118 { ctx, "font-dsp" },
119 { ctx, "font-oti" },
120 { ctx, "font-ott" },
121 { ctx, "font-otl" },
122 { ctx, "font-oto" },
123 { ctx, "font-otj" },
124 { ctx, "font-oup" },
125 { ctx, "font-ota" },
126 { ctx, "font-ots" },
127 { ctx, "font-otc" },
128 { ctx, "font-osd" },
129 { ctx, "font-ocl" },
130 { ctx, "font-onr" },
131 { ctx, "font-one" },
132 { ctx, "font-afk" },
133 { ltx, "fonts-tfm" },
134 { ctx, "font-lua" },
135 { ctx, "font-def" },
136 { ctx, "font-shp" },
137 { ltx, "fonts-def" },
138 { ltx, "fonts-ext" },
139 { ctx, "font-imp-tex" },
140 { ctx, "font-imp-ligatures"},
141 { ctx, "font-imp-italics" },
142 { ctx, "font-imp-effects" },
143 { ltx, "fonts-lig" },
144 { ltx, "fonts-gbn" },
145
146 } --[[context_modules]]
147
148 local function load_context_modules (pth)
149
150 local load_module = luaotfload.loaders.context
151 local ignore_module = luaotfload.loaders.ignore
152
153 logreport ("both", 2, "init",
154 "Loading fontloader components from context.")
155 for i = 1, #context_modules do
156 local kind, spec = unpack (context_modules [i])
157 if kind == ignore then
158 ignore_module (spec)
159 else
160 if kind == ltx then
161 spec = 'luatex-' .. spec
162 end
163 local sub = context_module_paths[kind]
164 local tsub = type (sub)
165 if not pth then
166 load_module (spec)
167 elseif tsub == "string" then
168 load_module (spec, file.join (pth, sub))
169 elseif tsub == "table" then
170 local pfx
171 local nsub = #sub
172 for j = 1, nsub do
173 local full = file.join (pth, sub [j])
174 if lfsisdir (full) then --- pick the first real one
175 pfx = full
176 break
177 end
178 end
179 if pfx then
180 load_module (spec, pfx)
181 else
182 logreport ("both", 0, "init",
183 "None of the %d search paths for module %q exist; \z
184 falling back to default path.",
185 nsub, tostring (spec))
186 load_module (spec) --- maybe we’ll get by after all?
187 end
188 else
189 logreport ("both", 0, "init",
190 "Internal error, please report. \z
191 This is not your fault.")
192 os.exit (-1)
193 end
194 end
195 end
196
197 end
198
199 local function verify_context_dir (pth)
200 if lfsisdir(file.join(pth, context_module_paths[ltx])) then
201 return true
202 end
203 for _, d in ipairs(context_module_paths[ctx]) do
204 if lfsisdir(file.join(pth, d)) then
205 return true
206 end
207 end
208 logreport("both", 0, "init", "A directory name has been passed as \z
209 fontloader name but this directory does not actually seem to contain \z
210 a font loader. I will try to interpret your fontloader name in another \z
211 way for now, but please fix your settings.")
212 return false
213 end
214
215 local function init_main(early_hook)
216 config = config or { } --- global
217 config.luaotfload = config.luaotfload or { }
218 config.lualibs = config.lualibs or { }
219 config.lualibs.verbose = false
220 config.lualibs.prefer_merged = true
221 config.lualibs.load_extended = true
222 fonts = fonts or { }
223
224 require "lualibs"
225
226 if not lualibs then error "this module requires Luaotfload" end
227 if not luaotfload then error "this module requires Luaotfload" end
228
229 --[[doc--
230
231 The logger needs to be in place prior to loading the fontloader due
232 to order of initialization being crucial for the logger functions
233 that are swapped.
234
235 --doc]]--
236
237 luaotfload.loaders.luaotfload "log"
238 log = luaotfload.log
239 logreport = log.report
240 log.set_loglevel (default_log_level)
241
242 logreport ("log", 4, "init", "Concealing callback.register().")
243 local trapped_register = callback.register
244
245 function callback.register (id)
246 logreport ("log", 4, "init",
247 "Dummy callback.register() invoked on %s.",
248 id)
249 end
250
251 --[[doc--
252
253 By default, the fontloader requires a number of \emphasis{private
254 attributes} for internal use.
255 These must be kept consistent with the attribute handling methods
256 as provided by \identifier{luatexbase}.
257 Our strategy is to override the function that allocates new
258 attributes before we initialize the font loader, making it a
259 wrapper around \luafunction{luatexbase.new_attribute}.\footnote{%
260 Many thanks, again, to Hans Hagen for making this part
261 configurable!
262 }
263 The attribute identifiers are prefixed “\fileent{luaotfload@}” to
264 avoid name clashes.
265
266 --doc]]--
267
268 local new_attribute = luatexbase.new_attribute
269 local the_attributes = luatexbase.attributes
270
271 local context_environment = luaotfload.fontloader
272 context_environment.CONTEXTLMTXMODE = 0
273 context_environment.attributes = {
274 private = function (name)
275 local attr = "luaotfload@" .. name --- used to be: “otfl@”
276 local number = the_attributes[attr]
277 if not number then number = new_attribute(attr) end
278 return number
279 end
280 }
281
282 --[[doc--
283
284 First save a copy of font.each in order to also catch old-style fonts
285
286 --doc]]--
287
288 font.originaleach = font.each
289
290 local load_fontloader_module = luaotfload.loaders.fontloader
291 local ignore_module = luaotfload.loaders.ignore
292
293 do
294 local saved_reporter = texio.reporter
295 local saved_exit = os.exit
296 local errmsg
297 function texio.reporter(msg, ...)
298 errmsg = msg
299 end
300 function os.exit()
301 error(errmsg)
302 end
303 load_fontloader_module "basics-gen"
304 texio.reporter = saved_reporter
305 os.exit = saved_exit
306 end
307
308 if early_hook then early_hook() end
309
310 --[[doc--
311
312 The font loader requires that the attribute with index zero be
313 zero. We happily oblige.
314 (Cf. \fileent{luatex-fonts-nod.lua}.)
315
316 --doc]]--
317
318 tex.attribute[0] = 0
319
320 --[[doc--
321
322 Now that things are sorted out we can finally load the
323 fontloader.
324
325 --doc]]--
326
327 local fontloader = config.luaotfload and config.luaotfload.run.fontloader
328 or "reference"
329 fontloader = tostring (fontloader)
330
331 if fontloader == "reference" then
332 logreport ("log", 0, "init", "Using reference fontloader.")
333 load_fontloader_module (luaotfload.fontloader_package)
334
335 elseif fontloader == "default" then
336 --- Same as above but loader name not correctly replaced by the file name
337 --- of our fontloader package. Perhaps something’s wrong with the status
338 --- file which contains the datestamped filename? In any case, it can’t
339 --- hurt reporting it as a bug.
340 logreport ("both", 0, "init", "Fontloader substitution failed, got \"default\".")
341 logreport ("log", 4, "init", "Falling back to reference fontloader.")
342 load_fontloader_module (luaotfload.fontloader_package)
343
344 elseif fontloader == "unpackaged" then
345
346 logreport ("log", 0, "init",
347 "Loading fontloader components individually.")
348 for i = 1, #context_modules do
349 local mod = context_modules[i];
350 (mod[1] == ignore and ignore_module or load_fontloader_module)(mod[2])
351 end
352
353 elseif fontloader == "context" then
354 logreport ("log", 0, "init",
355 "Loading Context modules in lookup path.")
356 load_context_modules ()
357
358 elseif lfsisdir (fontloader) and verify_context_dir (fontloader) then
359 logreport ("log", 0, "init",
360 "Loading Context files under prefix %q.",
361 fontloader)
362 load_context_modules (fontloader)
363
364 elseif lfs.isfile (fontloader) then
365 logreport ("log", 0, "init",
366 "Loading fontloader from absolute path %q.",
367 fontloader)
368 local _void = assert (loadfile (fontloader, nil, context_environment)) ()
369
370 elseif kpsefind_file (fontloader) then
371 local path = kpsefind_file (fontloader)
372 logreport ("log", 0, "init",
373 "Loading fontloader %q from kpse-resolved path %q.",
374 fontloader, path)
375 local _void = assert (loadfile (path, nil, context_environment)) ()
376
377 elseif kpsefind_file (("fontloader-%s.lua"):format(fontloader)) then
378 logreport ("log", 0, "init",
379 "Using predefined fontloader %q.",
380 fontloader)
381 load_fontloader_module (fontloader)
382
383 else
384 logreport ("both", 0, "init",
385 "No match for requested fontloader %q.",
386 fontloader)
387 fontloader = luaotfload.fontloader_package
388 logreport ("both", 0, "init",
389 "Defaulting to predefined fontloader %q.",
390 fontloader)
391 load_fontloader_module (fontloader)
392 end
393
394 ---load_fontloader_module "font-odv.lua" --- <= Devanagari support from Context
395
396 logreport ("log", 0, "init",
397 "Context OpenType loader version %.3f",
398 fonts.handlers.otf.version)
399 callback.register = trapped_register
400 nodes = context_environment.nodes
401 -- setmetatable(context_environment, nil) -- Would be nice, might break the
402 -- fontloader
403 end --- [init_main]
404
405 if not luatexbase.callbacktypes.pre_shaping_filter then
406 luatexbase.create_callback('pre_shaping_filter', 'list')
407 luatexbase.create_callback('post_shaping_filter', 'reverselist')
408 end
409
410 local init_post_install_callbacks = function ()
411 --[[doc--
412
413 we do our own callback handling with the means provided by
414 luatexbase.
415 note: \luafunction{pre_linebreak_filter} and
416 \luafunction{hpack_filter} are coupled in \context in the
417 concept of \emphasis{node processor}.
418
419 --doc]]--
420
421 -- The order is important here: multiscript=auto needs to look at the
422 -- fallback fonts, so they already have to be processed at that stage
423 local fallback = luaotfload.loaders.luaotfload "fallback".process
424 local multiscript = luaotfload.loaders.luaotfload "multiscript".process
425
426 local call_callback = luatexbase.call_callback
427 local tex_get = tex.get
428 local flush_list = node.flush_list
429 local handler = luaotfload.fontloader.nodes.simple_font_handler
430 local function callback(head, groupcode, _, _, direction)
431 if not direction then
432 direction = tex_get'textdirection'
433 else
434 direction = direction == "TRT" and 1 or 0
435 end
436 local result = call_callback("pre_shaping_filter", head, groupcode, direction)
437 if result == false then
438 return false
439 elseif result ~= true then
440 head = result
441 end
442 multiscript(head, nil, nil, nil, direction)
443 fallback(head, nil, nil, nil, direction)
444 result = handler(head, groupcode, nil, nil, direction)
445 -- handler never returns a boolean and only returns nil if it was passed in
446 -- We keep it general though for consistency
447 if result == false then
448 flush_list(head)
449 return nil
450 elseif result ~= true then
451 head = result
452 end
453 result = call_callback("post_shaping_filter", head, groupcode, direction)
454 if result == false then
455 flush_list(head)
456 return nil
457 elseif result == true then
458 return head
459 else
460 return result
461 end
462 end
463 luatexbase.add_to_callback("pre_linebreak_filter",
464 callback,
465 "luaotfload.node_processor",
466 1)
467 luatexbase.add_to_callback("hpack_filter",
468 callback,
469 "luaotfload.node_processor",
470 1)
471
472 local streams = fonts.hashes.streams
473 luatexbase.add_to_callback("glyph_stream_provider",function(id,index,mode)
474 if id <= 0 then return "" end
475 local stream = streams[id].streams
476 if not stream then return "" end
477 return stream[index] or ""
478 end, "luaotfload.glyph_stream")
479 end
480
481 local function init_post_load_agl ()
482
483 --[[doc--
484
485 Adobe Glyph List.
486 -----------------------------------------------------------------
487
488 Context provides a somewhat different font-age.lua from an
489 unclear origin. Unfortunately, the file name it reads from is
490 hard-coded in font-enc.lua, so we have to replace the entire
491 table.
492
493 This shouldn’t cause any complications. Due to its implementation
494 the glyph list will be loaded upon loading a OTF or TTF for the
495 first time during a TeX run. (If one sticks to TFM/OFM then it is
496 never read at all.) For this reason we can install a metatable
497 that looks up the file of our choosing and only falls back to the
498 Context one in case it cannot be found.
499
500 --doc]]--
501
502 local encodings = fonts.encodings
503
504 if not encodings then
505 --- Might happen during refactoring; we continue graciously but in
506 --- a somewhat defect state.
507 logreport ("log", 0, "init",
508 "preconditions unmet, skipping the Adobe Glyph List; "
509 .. "this is a Luaotfload bug.")
510 return
511 end
512
513 if next (fonts.encodings.agl) then
514 --- unnecessary because the file shouldn’t be loaded at this time
515 --- but we’re just making sure
516 fonts.encodings.agl = nil
517 collectgarbage"collect"
518 end
519
520 local agl_init = { } --- start out empty, fill on demand
521 encodings.agl = agl_init --- ugh, replaced again later
522
523 setmetatable (agl_init, { __index = function (t, k)
524
525 if k ~= "unicodes" then
526 return nil
527 end
528
529 local glyphlist = kpsefind_file "luaotfload-glyphlist.lua"
530 if glyphlist then
531 logreport ("log", 1, "init", "loading the Adobe glyph list")
532 else
533 glyphlist = kpsefind_file "font-age.lua"
534 logreport ("both", 0, "init",
535 "loading the extended glyph list from ConTeXt")
536 end
537
538 if not glyphlist then
539 logreport ("both", 4, "init",
540 "Adobe glyph list not found, please check your installation.")
541 return nil
542 end
543 logreport ("both", 4, "init",
544 "found Adobe glyph list file at %q, using that.",
545 glyphlist)
546
547 local unicodes = dofile(glyphlist)
548 encodings.agl = { unicodes = unicodes }
549 return unicodes
550 end })
551
552 end
553
554 local function init_post_apply_keepnames ()
555 local keepnames = config.luaotfload.misc.keepnames
556 luaotfload.fontloader.fonts.privateoffsets.keepnames = keepnames
557 end
558
559 --- (unit -> unit) list
560 local init_post_actions = {
561 init_post_install_callbacks,
562 init_post_load_agl,
563 init_post_apply_keepnames
564 }
565
566 --- unit -> size_t
567 local function init_post ()
568 --- hook for actions that need to take place after the fontloader is
569 --- installed
570
571 local n = #init_post_actions
572 for i = 1, n do
573 local action = init_post_actions[i]
574 local taction = type (action)
575 if not action or taction ~= "function" then
576 logreport ("both", 1, "init",
577 "post hook WARNING: action %d not a function but %s/%s; ignoring.",
578 i, action, taction)
579 else
580 --- call closure
581 action ()
582 end
583 end
584
585 return n
586 end --- [init_post]
587
588 return function (early_hook)
589 local starttime = os.gettimeofday ()
590 init_main (early_hook)
591 logreport ("both", 1, "init",
592 "fontloader loaded in %0.3f seconds",
593 os.gettimeofday() - starttime)
594 local n = init_post ()
595 logreport ("both", 5, "init", "post hook terminated, %d actions performed", n)
596 return true
597 end
598 -- vim:tw=79:sw=2:ts=2:expandtab