luatex-fonts.lua /size: 14 Kb    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['luatex-fonts'] = {
2    version   = 1.001,
3    comment   = "companion to luatex-fonts.tex",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- A merged file is generated with:
10--
11--   mtxrun --script package --merge --stripcontext luatex-fonts.lua
12--
13-- A needed resource file is made by:
14--
15--   mtxrun --script context luatex-basics-prepare.tex
16--
17-- A font (generic) database is created with:
18--
19--   mtxrun --script font --reload --simple
20
21-- The following code isolates the generic context code from already defined or to be defined
22-- namespaces. This is the reference loader for plain tex. This generic code is also used in
23-- luaotfload which is a low level lualatex opentype font loader but somehow has gotten a bit
24-- too generic name / prefix, originally set up and maintained by Khaled Hosny. Currently that
25-- set of derived files is maintained by a larger team lead by Philipp Gesang so when there are
26-- issues with this code in latex, you can best contact him. It might make sense then to first
27-- check if context has the same issue. We do our best to keep the interface as clean as possible.
28--
29-- The code base is rather stable now, especially if you stay away from the non generic code. All
30-- relevant data is organized in tables within the main table of a font instance. There are a few
31-- places where in context other code is plugged in, but this does not affect the core code. Users
32-- can (given that their macro package provides this option) access the font data (characters,
33-- descriptions, properties, parameters, etc) of this main table. The documentation is part of
34-- context. There is also a manual for the helper libraries (maintained as part of the cld manuals).
35--
36-- Future versions will probably have some more specific context code removed, like tracing and
37-- obscure hooks, so that we have a more efficient version (and less files too). So, don't depend
38-- too much on low level code that is meant for context as it can change without notice. We might
39-- also add more helper code here, but that depends to what extend metatex (sidetrack of context)
40-- evolves into a low level layer (depends on time, as usual).
41
42-- The code here is the same as in context version 2015.09.11 but the rendering in context can be
43-- different from generic. This can be a side effect of additional callbacks, additional features
44-- and interferences between mechanisms between macro packages. We use the rendering in context
45-- and luatex-plain as reference for issues.
46
47-- I might as well remove some code that is not used in generic (or not used by generic users)
48-- like color fonts (emoji etc) and variable fonts thereby making the code base smaller. However
49-- I might keep ity just for the sake of testing the plain loader that comes with context. We'll
50-- see.
51
52-- As a side effect of cleaning up some context code, like code meant for older version of luatex,
53-- as well replacing code for more recent versions (post 1.12) there can be changes in the modules
54-- used here, especially where we check for 'context' being used. Hopefully there are no side
55-- effects. Because we can now assume that the the glyph injection callback is in recent texlive
56-- installations, the variable font code is now enabled in the generic version that comes with
57-- context (as unofficial bonus; when it was demonstrated at bachotex 2017 it worked ok for the
58-- generic loader but was kind of disabled there as no one needs it). I waited with adding the
59-- pending code for type 3 support till texlive 2020 was fozen but it will be in texlive 2021 (it
60-- is already tested in context back in 2019 and I wanted to release it at the canceled BT 2020
61-- meeting, so I consider it stable, read: this is it). To what extend and when I will adapt the
62-- generic code (for color support) to that is yet to be decided because in context we do things
63-- a bit differently. We anyway have to wait a few years till that callback is omnipresent so I'm
64-- not in that much of a hurry. (There will be a TB article about it first and after that I will
65-- add some examples to the manual.)
66
67utf = utf or (unicode and unicode.utf8) or { }
68
69-- We have some (global) hooks (for latex). Maybe I'll use this signal to disable some of the
70-- more tricky features like variable fonts and emoji (because afaik latex uses hb for that).
71
72if not non_generic_context then
73    non_generic_context = { }
74end
75
76if not non_generic_context.luatex_fonts then
77    non_generic_context.luatex_fonts = {
78     -- load_before  = nil,
79     -- load_after   = nil,
80     -- skip_loading = nil,
81    }
82end
83
84if not generic_context then
85    generic_context  = { }
86end
87
88if not generic_context.push_namespaces then
89
90    function generic_context.push_namespaces()
91     -- logs.report("system","push namespace")
92        local normalglobal = { }
93        for k, v in next, _G do
94            normalglobal[k] = v
95        end
96        return normalglobal
97    end
98
99    function generic_context.pop_namespaces(normalglobal,isolate)
100        if normalglobal then
101         -- logs.report("system","pop namespace")
102            for k, v in next, _G do
103                if not normalglobal[k] then
104                    generic_context[k] = v
105                    if isolate then
106                        _G[k] = nil
107                    end
108                end
109            end
110            for k, v in next, normalglobal do
111                _G[k] = v
112            end
113            -- just to be sure:
114            setmetatable(generic_context,_G)
115        else
116            logs.report("system","fatal error: invalid pop of generic_context")
117            os.exit()
118        end
119    end
120
121end
122
123local whatever = generic_context.push_namespaces()
124
125-- We keep track of load time by storing the current time. That way we cannot be accused
126-- of slowing down loading too much. Anyhow, there is no reason for this library to perform
127-- slower in any other package as it does in context.
128--
129-- Please don't update to this version without proper testing. It might be that this version
130-- lags behind stock context and the only formal release takes place around tex live code
131-- freeze.
132
133local starttime = os.gettimeofday()
134
135-- As we don't use the context file searching, we need to initialize the kpse library. As the
136-- progname can be anything we will temporary switch to the context namespace if needed. Just
137-- adding the context paths to the path specification is somewhat faster.
138--
139-- Now, with lua 5.2 being used we might create a special ENV for this.
140
141-- kpse.set_program_name("luatex")
142
143-- One can define texio.reporter as alternative terminal/log writer. That's as far
144-- as I want to go with this.
145
146local ctxkpse = nil
147local verbose = true
148
149if not logs or not logs.report then
150    if not logs then
151        logs = { }
152    end
153    function logs.report(c,f,...)
154        local r = texio.reporter or texio.write_nl
155        if f then
156            r(c .. " : " .. string.format(f,...))
157        else
158            r("")
159        end
160    end
161end
162
163local function loadmodule(name,continue)
164    local foundname = kpse.find_file(name,"tex") or ""
165    if not foundname then
166        if not ctxkpse then
167            ctxkpse = kpse.new("luatex","context")
168        end
169        foundname = ctxkpse:find_file(name,"tex") or ""
170    end
171    if foundname == "" then
172        if not continue then
173            logs.report("system","unable to locate file '%s'",name)
174            os.exit()
175        end
176    else
177        if verbose then
178            logs.report("system","loading '%s'",foundname) -- no file.basename yet
179        end
180        dofile(foundname)
181    end
182end
183
184if non_generic_context.luatex_fonts.load_before then
185    loadmodule(non_generic_context.luatex_fonts.load_before,true)
186end
187
188if non_generic_context.luatex_fonts.skip_loading ~= true then
189
190    loadmodule('luatex-fonts-merged.lua',true)
191
192    if fonts then
193
194        if not fonts._merge_loaded_message_done_ then
195            texio.write_nl("log", "!")
196            texio.write_nl("log", "! I am using the merged version of 'luatex-fonts.lua' here. If")
197            texio.write_nl("log", "! you run into problems or experience unexpected behaviour, and")
198            texio.write_nl("log", "! if you have ConTeXt installed you can try to delete the file")
199            texio.write_nl("log", "! 'luatex-font-merged.lua' as I might then use the possibly")
200            texio.write_nl("log", "! updated libraries. The merged version is not supported as it")
201            texio.write_nl("log", "! is a frozen instance. Problems can be reported to the ConTeXt")
202            texio.write_nl("log", "! mailing list.")
203            texio.write_nl("log", "!")
204        end
205
206        fonts._merge_loaded_message_done_ = true
207
208    else
209
210        -- The following helpers are a bit overkill but I don't want to mess up
211        -- context code for the sake of general generality. Around version 1.0
212        -- there will be an official api defined.
213        --
214        -- So, I will strip these libraries and see what is really needed so that
215        -- we don't have this overhead in the generic modules. The next section
216        -- is only there for the packager, so stick to using luatex-fonts with
217        -- luatex-fonts-merged.lua and forget about the rest. The following list
218        -- might change without prior notice (for instance because we shuffled
219        -- code around).
220
221        loadmodule("l-lua.lua")
222        loadmodule("l-lpeg.lua")
223        loadmodule("l-function.lua")
224        loadmodule("l-string.lua")
225        loadmodule("l-table.lua")
226        loadmodule("l-io.lua")
227        loadmodule("l-file.lua")
228        loadmodule("l-boolean.lua")
229        loadmodule("l-math.lua")
230
231        -- A few slightly higher level support modules:
232
233        loadmodule("util-str.lua") -- future versions can ship without this one
234        loadmodule("util-fil.lua") -- future versions can ship without this one
235
236        -- The following modules contain code that is either not used at all
237        -- outside context or will fail when enabled due to lack of other
238        -- modules.
239
240        -- First we load a few helper modules. This is about the miminum needed
241        -- to let the font modules do their work. Don't depend on their functions
242        -- as we might strip them in future versions of this generic variant.
243
244        loadmodule('luatex-basics-gen.lua')
245        loadmodule('data-con.lua')
246
247        -- We do need some basic node support. The code in there is not for
248        -- general use as it might change.
249
250        loadmodule('luatex-basics-nod.lua')
251
252        -- We ship a resources needed for font handling (more might end up here).
253
254        loadmodule('luatex-basics-chr.lua')
255
256        -- Now come the font modules that deal with traditional tex fonts as well
257        -- as open type fonts.
258        --
259        -- The font database file (if used at all) must be put someplace visible
260        -- for kpse and is not shared with context. The mtx-fonts script can be
261        -- used to generate this file (using the --reload --force --simple option).
262
263        loadmodule('font-ini.lua')
264        loadmodule('luatex-fonts-mis.lua')
265        loadmodule('font-con.lua')
266        loadmodule('luatex-fonts-enc.lua')
267        loadmodule('font-cid.lua')
268        loadmodule('font-map.lua')
269
270        -- We use a bit simpler database because using the context one demands
271        -- loading more helper code and although it is more flexible (more ways
272        -- to resolve and so) it will never be uses in plain/latex anyway, so
273        -- let's stick to a simple approach.
274
275        loadmodule('luatex-fonts-syn.lua')
276
277        -- We need some helpers.
278
279        loadmodule('font-vfc.lua')
280
281        -- This is the bulk of opentype code. The color and variable font support (as for
282        -- emoji) can (and might) actually go away here because it has never been used
283        -- outside context so in retrospect there was no need for it being generic.
284
285        loadmodule('font-otr.lua')
286        loadmodule('font-cff.lua')
287        loadmodule('font-ttf.lua')
288        loadmodule('font-dsp.lua')
289        loadmodule('font-oti.lua')
290        loadmodule('font-ott.lua')
291        loadmodule('font-otl.lua')
292        loadmodule('font-oto.lua')
293        loadmodule('font-otj.lua')
294        loadmodule('font-oup.lua')
295        loadmodule('font-ota.lua')
296        loadmodule('font-ots.lua')
297        loadmodule('font-otc.lua')
298        loadmodule('font-osd.lua')
299        loadmodule('font-ocl.lua')
300
301        -- The code for type one fonts.
302
303        loadmodule('font-onr.lua')
304        loadmodule('font-one.lua')
305        loadmodule('font-afk.lua')
306
307        -- And for traditional TeX fonts.
308
309        loadmodule('luatex-fonts-tfm.lua')
310
311        -- Some common code.
312
313        loadmodule('font-lua.lua')
314        loadmodule('font-def.lua')
315        loadmodule('font-shp.lua')
316
317        -- We support xetex compatible specifiers (plain/latex only).
318
319        loadmodule('luatex-fonts-def.lua') -- was font-xtx.lua
320
321        -- Here come some additional features.
322
323        loadmodule('luatex-fonts-ext.lua')
324        loadmodule('font-imp-tex.lua')
325        loadmodule('font-imp-ligatures.lua')
326        loadmodule('font-imp-italics.lua')
327        loadmodule('font-imp-effects.lua')
328        loadmodule('luatex-fonts-lig.lua')
329
330        -- We need to plug into a callback and the following module implements the
331        -- handlers. Actual plugging in happens later.
332
333        loadmodule('luatex-fonts-gbn.lua')
334
335    end
336
337end
338
339if non_generic_context.luatex_fonts.load_after then
340    loadmodule(non_generic_context.luatex_fonts.load_after,true)
341end
342
343resolvers.loadmodule = loadmodule
344
345-- In order to deal with the fonts we need to initialize some callbacks. One can overload them later
346-- on if needed. First a bit of abstraction.
347
348generic_context.callback_ligaturing           = false
349generic_context.callback_kerning              = false
350generic_context.callback_pre_linebreak_filter = nodes.simple_font_handler
351generic_context.callback_hpack_filter         = nodes.simple_font_handler
352generic_context.callback_define_font          = fonts.definers.read
353
354-- The next ones can be done at a different moment if needed. You can create a generic_context namespace
355-- and set no_callbacks_yet to true, load this module, and enable the callbacks later. So, there is really
356-- *no* need to create a alternative for luatex-fonts.lua and luatex-fonts-merged.lua: just load this one
357-- and overload if needed.
358
359if not generic_context.no_callbacks_yet then
360
361    callback.register('ligaturing',           generic_context.callback_ligaturing)
362    callback.register('kerning',              generic_context.callback_kerning)
363    callback.register('pre_linebreak_filter', generic_context.callback_pre_linebreak_filter)
364    callback.register('hpack_filter',         generic_context.callback_hpack_filter)
365    callback.register('define_font' ,         generic_context.callback_define_font)
366
367end
368
369-- We're done.
370
371logs.report("system","luatex-fonts.lua loaded in %0.3f seconds", os.gettimeofday()-starttime)
372
373generic_context.pop_namespaces(whatever)
374