Debian Codegraph @

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

raw
1 -----------------------------------------------------------------------
2 -- FILE: luaotfload.lua
3 -- DESCRIPTION: OpenType layout system / luaotfload entry point
4 -- REQUIREMENTS: luatex v.0.95.0 or later; package lualibs
5 -- AUTHOR: Élie Roux, Khaled Hosny, Philipp Gesang, Ulrike Fischer, Marcel Krüger
6 -----------------------------------------------------------------------
7
8 local authors = "\z
9 Hans Hagen,\z
10 Khaled Hosny,\z
11 Elie Roux,\z
12 Will Robertson,\z
13 Philipp Gesang,\z
14 Dohyun Kim,\z
15 Reuben Thomas,\z
16 David Carlisle,\
17 Ulrike Fischer,\z
18 Marcel Krüger\z
19 "
20 -- version number is used below!
21 local ProvidesLuaModule = {
22 name = "luaotfload",
23 version = "3.29", --TAGVERSION
24 date = "2024-12-03", --TAGDATE
25 description = "Lua based OpenType font support",
26 author = authors,
27 copyright = authors,
28 license = "GPL v2.0"
29 }
30
31 if luatexbase and luatexbase.provides_module then
32 luatexbase.provides_module (ProvidesLuaModule)
33 end
34
35 if luaotfload_module == nil then
36 local print_warning = luatexbase and luatexbase.module_warning or function(module, text)
37 return texio.write_nl("%s WARNING: %s", module, text)
38 end
39 local saved_version = ProvidesLuaModule.version
40 local trace_submodules = tonumber(os.getenv'LUAOTFLOAD_TRACE_SUBMODULES')
41 if luatexbase and luatexbase.provides_module and trace_submodules and trace_submodules ~= 0 then
42 luaotfload_module = luatexbase.provides_module
43 else
44 function luaotfload_module(module)
45 local module_version = module.version
46 if module_version ~= saved_version then
47 local filenames
48 if debug then
49 filenames = string.format("luaotfload.lua is found at\n%q\n%s.lua is found at\n%q\n",
50 debug.getinfo(1, "S").source:sub(2),
51 module.name,
52 debug.getinfo(2, "S").source:sub(2))
53 else
54 filenames = ""
55 end
56 print_warning("luaotfload", string.format("Version inconsistency detected.\n\z
57 luaotfload is loaded in version %s, while %q is loaded \z
58 in version %s.\n%sI will try to continue anyway.\nHIC SUNT DRACONES",
59 saved_version, module.name, module_version, filenames))
60 end
61 end
62 end
63 else
64 error[[luaotfload is reloading itself nested. This can't happen.]]
65 end
66
67 local osgettimeofday = os.gettimeofday
68 config = config or { }
69 luaotfload = luaotfload or { }
70 local luaotfload = luaotfload
71 luaotfload.log = luaotfload.log or { }
72 local logreport
73 luaotfload.version = ProvidesLuaModule.version
74 luaotfload.loaders = { }
75 luaotfload.fontloader_package = "reference" --- default: from current Context
76
77 if not tex or not tex.luatexversion then
78 error "this program must be run in TeX mode" --- or call tex.initialize() =)
79 end
80
81 --- version check
82 local revno = tonumber(tex.luatexrevision)
83 local minimum = { 110, 0 }
84 if tex.luatexversion < minimum[1] or tex.luatexversion == minimum[1] and revno < minimum[2] then
85 texio.write_nl ("term and log",
86 string.format ("\tFATAL ERROR\n\z
87 \tLuaotfload requires a Luatex version >= %d.%d.%d.\n\z
88 \tPlease update your TeX distribution!\n\n",
89 math.floor(minimum[1] / 100), minimum[1] % 100, minimum[2]))
90 error "version check failed"
91 end
92
93 if not utf8 then
94 texio.write_nl("term and log", string.format("\z
95 \tluaotfload: module utf8 is unavailable\n\z
96 \tutf8 is available in Lua 5.3+; engine\'s _VERSION is %q\n\z
97 \tThis probably means that the engine is not supported\n\z
98 \n",
99 _VERSION))
100 error "module utf8 is unavailable"
101 end
102
103 if status.safer_option ~= 0 then
104 texio.write_nl("term and log","luaotfload can't run with option --safer. Aborting")
105 error("safer_option used")
106 end
107
108
109
110
111 --[[doc--
112
113 This file initializes the system and loads the font loader. To
114 minimize potential conflicts between other packages and the code
115 imported from \CONTEXT, several precautions are in order. Some of
116 the functionality that the font loader expects to be present, like
117 raw access to callbacks, are assumed to have been disabled by
118 \identifier{luatexbase} when this file is processed. In some cases
119 it is possible to trick it by putting dummies into place and
120 restoring the behavior from \identifier{luatexbase} after
121 initialization. Other cases such as attribute allocation require
122 that we hook the functionality from \identifier{luatexbase} into
123 locations where they normally wouldn’t be.
124
125 Anyways we can import the code base without modifications, which is
126 due mostly to the extra effort by Hans Hagen to make \LUATEX-Fonts
127 self-contained and encapsulate it, and especially due to his
128 willingness to incorporate our suggestions.
129
130 --doc]]--
131
132 local luatexbase = luatexbase
133 local require = require
134 local type = type
135
136
137 --[[doc--
138
139 \subsection{Module loading}
140 We load the files imported from \CONTEXT with function derived this way. It
141 automatically prepends a prefix to its argument, so we can refer to the
142 files with their actual \CONTEXT name.
143
144 --doc]]--
145
146 local function make_loader_name (prefix, name)
147 local msg = luaotfload.log and luaotfload.log.report
148 or function (stream, lvl, cat, ...)
149 if lvl > 1 then --[[not pressing]] return end
150 texio.write_nl ("log",
151 string.format ("luaotfload | %s : ",
152 tostring (cat)))
153 texio.write (string.format (...))
154 end
155 if not name then
156 msg ("both", 0, "load",
157 "Fatal error: make_loader_name (%q, %q).",
158 tostring (prefix), tostring (name))
159 return "dummy-name"
160 end
161 name = tostring (name)
162 if prefix == false then
163 msg ("log", 9, "load",
164 "No prefix requested, passing module name %q unmodified.",
165 name)
166 return tostring (name) .. ".lua"
167 end
168 prefix = tostring (prefix)
169 msg ("log", 9, "load",
170 "Composing module name from constituents %s, %s.",
171 prefix, name)
172 return prefix .. "-" .. name .. ".lua"
173 end
174
175 local function make_loader (prefix, load_helper)
176 return function (name)
177 local t_0 = osgettimeofday ()
178 local modname = make_loader_name (prefix, name)
179 --- We don’t want the stack info from inside, so just pcall().
180 local ok, data = pcall (load_helper or require, modname)
181 local t_end = osgettimeofday ()
182 if not ok then
183 io.write "\n"
184 local msg = luaotfload.log and luaotfload.log.report or print
185 msg ("both", 0, "load", "FATAL ERROR")
186 msg ("both", 0, "load", " × Failed to load %q module %q.",
187 tostring (prefix), tostring (name))
188 local lines = string.split (data, "\n\t")
189 if not lines then
190 msg ("both", 0, "load", " × Error message: %q", data)
191 else
192 msg ("both", 0, "load", " × Error message:")
193 for i = 1, #lines do
194 msg ("both", 0, "load", " × %q.", lines [i])
195 end
196 end
197 io.write "\n\n"
198 local debug = debug
199 if debug then
200 io.write (debug.traceback())
201 io.write "\n\n"
202 end
203 os.exit(-1)
204 end
205 return data
206 end
207 end
208
209 --[[doc--
210 Certain files are kept around that aren’t loaded because they are part of
211 the imported fontloader. In order to keep the initialization structure
212 intact we also provide a no-op version of the module loader that can be
213 called in the expected places.
214 --doc]]--
215
216 local function dummy_loader (name)
217 luaotfload.log.report ("log", 3, "load",
218 "Skipping module %q on purpose.",
219 name)
220 end
221
222 local context_environment = setmetatable({}, {__index = _G})
223 luaotfload.fontloader = context_environment
224 local function context_isolated_load(name)
225 local fullname = kpse.find_file(name, 'lua')
226 if not fullname then
227 error(string.format('Fontloader module %q could not be found.', name))
228 end
229 return assert(loadfile(fullname, nil, context_environment))(name)
230 end
231
232 local function context_loader (name, path)
233 luaotfload.log.report ("log", 3, "load",
234 "Loading module %q from Context.",
235 name)
236 local t_0 = osgettimeofday ()
237 local modname = make_loader_name (false, name)
238 local modpath = modname
239 if path then
240 if lfs.isdir (path) then
241 luaotfload.log.report ("log", 3, "load",
242 "Prepending path %q.",
243 path)
244 modpath = file.join (path, modname)
245 else
246 luaotfload.log.report ("both", 0, "load",
247 "Non-existent path %q specified, ignoring.",
248 path)
249 end
250 end
251 local ret = context_isolated_load (modpath)
252 local t_end = osgettimeofday ()
253
254 if ret ~= nil then
255 --- require () returns “true” upon success unless the loaded file
256 --- yields a non-zero exit code. This isn’t per se indicating that
257 --- something isn’t right, but against HH’s coding practices. We’ll
258 --- silently ignore this ever happening on lower log levels.
259 luaotfload.log.report ("log", 4, "load",
260 "Module %q returned %q.", modname, ret)
261 end
262 return ret
263 end
264
265 local function install_loaders ()
266 local loaders = { }
267 local loadmodule = make_loader "luaotfload"
268 loaders.luaotfload = loadmodule
269 loaders.fontloader = make_loader ("fontloader", context_isolated_load)
270 loaders.context = context_loader
271 loaders.ignore = dummy_loader
272 ----loaders.plaintex = make_loader "luatex" --=> for Luatex-Plain
273
274 function loaders.initialize (name)
275 local tmp = loadmodule (name)
276 local init = type(tmp) == "table" and tmp.init or tmp
277 if init and type (init) == "function" then
278 local t_0 = osgettimeofday ()
279 if not init () then
280 logreport ("log", 0, "load",
281 "Failed to load module %q.", name)
282 return
283 end
284 local t_end = osgettimeofday ()
285 local d_t = t_end - t_0
286 logreport ("log", 4, "load",
287 "Module %q loaded in %g ms.",
288 name, d_t * 1000)
289 end
290 end
291
292 return loaders
293 end
294
295 local luaotfload_initialized = false --- prevent multiple invocations
296
297 luaotfload.main = function ()
298
299 if luaotfload_initialized then
300 logreport ("log", 0, "load",
301 "Luaotfload initialization requested but is already \z
302 loaded, ignoring.")
303 return
304 end
305 luaotfload_initialized = true
306
307 luaotfload.loaders = install_loaders ()
308 local loaders = luaotfload.loaders
309 local loadmodule = loaders.luaotfload
310 local initialize = loaders.initialize
311
312 local starttime = osgettimeofday ()
313
314 -- Feature detect HarfBuzz. This is done early to allow easy HarfBuzz
315 -- detection in other modules
316 local harfstatus, harfbuzz = pcall(require, 'luaharfbuzz')
317 if harfstatus then
318 luaotfload.harfbuzz = harfbuzz
319 end
320
321 local init = loadmodule "init" --- fontloader initialization
322 init (function ()
323
324 logreport = luaotfload.log.report
325 initialize "parsers" --- fonts.conf and syntax
326 initialize "configuration" --- configuration options
327 end)
328
329 initialize "loaders" --- Font loading; callbacks
330 initialize "database" --- Font management.
331 initialize "colors" --- Per-font colors.
332
333 local init_resolvers = loadmodule "resolvers" --- Font lookup
334 init_resolvers ()
335
336 if not config.actions.reconfigure () then
337 logreport ("log", 0, "load", "Post-configuration hooks failed.")
338 end
339
340 initialize "features" --- font request and feature handling
341
342 if harfstatus then
343 loadmodule "harf-define"
344 loadmodule "harf-plug"
345 end
346 loadmodule "letterspace" --- extra character kerning
347 loadmodule "embolden" --- fake bold
348 loadmodule "notdef" --- missing glyph handling
349 loadmodule "suppress" --- suppress ligatures by adding ZWNJ
350 loadmodule "szss" --- missing glyph handling
351 initialize "auxiliary" --- additional high-level functionality
352 loadmodule "tounicode"
353 loadmodule "case"
354 if tex.outputmode == 0 then
355 loadmodule "dvi" --- allow writing fonts to DVI files
356 end
357
358 luaotfload.aux.start_rewrite_fontname () --- to be migrated to fontspec
359
360 logreport ("log", 1, "main",
361 "initialization completed in %0.3f seconds\n",
362 osgettimeofday() - starttime)
363 luaotfload_module = nil
364 end
365
366 -- vim:tw=79:sw=4:ts=4:et