summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-init.lua
blob: 6223d1dfd01e475bcce7156e6fd30ec25535e666 (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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
-----------------------------------------------------------------------
--         FILE:  luaotfload-init.lua
--  DESCRIPTION:  part of luaotfload / font loader initialization
-- REQUIREMENTS:  luatex v.0.80 or later; packages lualibs
--       AUTHOR:  Philipp Gesang (Phg), <phg@phi-gamma.net>, Marcel Krüger
-----------------------------------------------------------------------

local ProvidesLuaModule = { 
    name          = "luaotfload-init",
    version       = "2.93",       --TAGVERSION
    date          = "2018-10-28", --TAGDATE
    description   = "luaotfload submodule / initialization",
    license       = "GPL v2.0"
}

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


local setmetatable = setmetatable
local kpselookup   = kpse.lookup
local lfsisdir     = lfs.isdir

--[[doc--

  Initialization phases:

      - Load Lualibs from package
      - Set up the logger routines
      - Load Fontloader
          - as package specified in configuration
          - from Context install
          - (optional: from raw unpackaged files distributed with
            Luaotfload)

  The initialization of the Lualibs may be made configurable in the
  future as well allowing to load both the files and the merged package
  depending on a configuration setting. However, this would require
  separating out the configuration parser into a self-contained
  package, which might be problematic due to its current dependency on
  the Lualibs itself.

--doc]]--

local log        --- filled in after loading the log module
local logreport  --- filled in after loading the log module

--[[doc--

    \subsection{Preparing the Font Loader}
    We treat the fontloader as a semi-black box so behavior is
    consistent between formats.
    We load the fontloader code directly in the same fashion as the
    Plain format \identifier{luatex-fonts} that is part of Context.
    How this is executed depends on the presence on the
    \emphasis{merged font loader code}.
    In \identifier{luaotfload} this is contained in the file
    \fileent{luaotfload-merged.lua}.
    If this file cannot be found, the original libraries from \CONTEXT
    of which the merged code was composed are loaded instead.
    Since these files are not shipped with Luaotfload, an installation
    of Context is required.
    (Since we pull the fontloader directly from the Context minimals,
    the necessary Context version is likely to be more recent than that
    of other TeX distributions like Texlive.)
    The imported font loader will call \luafunction{callback.register}
    once while reading \fileent{font-def.lua}.
    This is unavoidable unless we modify the imported files, but
    harmless if we make it call a dummy instead.
    However, this problem might vanish if we decide to do the merging
    ourselves, like the \identifier{lualibs} package does.
    With this step we would obtain the freedom to load our own
    overrides in the process right where they are needed, at the cost
    of losing encapsulation.
    The decision on how to progress is currently on indefinite hold.

--doc]]--

local glyph_codes =
  { [0] = "character"
  , [1] = "glyph"
  , [2] = "ligature"
  , [3] = "ghost"
  , [4] = "left"
  , [5] = "right"
  }

local disc_codes  =
  { [0] = "discretionary"
  , [1] = "explicit"
  , [2] = "automatic"
  , [3] = "regular"
  , [4] = "first"
  , [5] = "second"
  }

local node_types = { disc = disc_codes, glyph = glyph_codes }

local luatex_stubs = function ()
  if not node.subtypes then
    node.subtypes = function (t) return node_types [t] or { } end
    local direct = node.direct

    local getfield = direct.getfield
    local setfield = direct.setfield

    direct.setchar = direct.setchar or function (n, ...) setfield (n, "char", ...) end
    direct.setprev = direct.setprev or function (n, ...) setfield (n, "prev", ...) end
    direct.setnext = direct.setnext or function (n, ...) setfield (n, "next", ...) end

    direct.getchar = direct.getchar or function (n) getfield (n, "char") end
    direct.getprev = direct.getprev or function (n) getfield (n, "prev") end
    direct.getnext = direct.getnext or function (n) getfield (n, "next") end
  end
end

local init_early = function ()

  local store                  = { }
  config                       = config or { } --- global
  config.luaotfload            = config.luaotfload or { }
  config.lualibs               = config.lualibs    or { }
  config.lualibs.verbose       = false
  config.lualibs.prefer_merged = true
  config.lualibs.load_extended = true
  fonts                        = fonts or { }

  require "lualibs"

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

  luatex_stubs ()

  --[[doc--

    The logger needs to be in place prior to loading the fontloader due
    to order of initialization being crucial for the logger functions
    that are swapped.

  --doc]]--

  luaotfload.loaders.luaotfload "log"
  log       = luaotfload.log
  logreport = log.report
  log.set_loglevel (default_log_level)

  logreport ("log", 4, "init", "Concealing callback.register().")
  store.trapped_register = callback.register

  callback.register = function (id)
    logreport ("log", 4, "init",
               "Dummy callback.register() invoked on %s.",
               id)
  end

  --[[doc--

    By default, the fontloader requires a number of \emphasis{private
    attributes} for internal use.
    These must be kept consistent with the attribute handling methods
    as provided by \identifier{luatexbase}.
    Our strategy is to override the function that allocates new
    attributes before we initialize the font loader, making it a
    wrapper around \luafunction{luatexbase.new_attribute}.\footnote{%
        Many thanks, again, to Hans Hagen for making this part
        configurable!
    }
    The attribute identifiers are prefixed “\fileent{luaotfload@}” to
    avoid name clashes.

  --doc]]--

  local new_attribute    = luatexbase.new_attribute
  local the_attributes   = luatexbase.attributes

  attributes = attributes or { } --- this writes a global, sorry

  attributes.private = function (name)
    local attr   = "luaotfload@" .. name --- used to be: “otfl@”
    local number = the_attributes[attr]
    if not number then number = new_attribute(attr) end
    return number
  end

  luaotfload.loaders.fontloader "basics-gen"

  return store
end --- [init_early]

--[[doc--

    These next lines replicate the behavior of
    \fileent{luatex-fonts.lua}.

--doc]]--

local push_namespaces = function ()
    logreport ("log", 4, "init", "push namespace for font loader")
    local normalglobal = { }
    for k, v in next, _G do
        normalglobal[k] = v
    end
    return normalglobal
end

local pop_namespaces = function (normalglobal,
                                 isolate,
                                 context_environment)
    if normalglobal then
        local _G = _G
        local mode = "non-destructive"
        if isolate then mode = "destructive" end
        logreport ("log", 4, "init", "pop namespace from font loader -- " .. mode)
        for k, v in next, _G do
            if not normalglobal[k] then
                context_environment[k] = v
                if isolate then
                    _G[k] = nil
                end
            end
        end
        for k, v in next, normalglobal do
            _G[k] = v
        end
        -- just to be sure:
        setmetatable(context_environment, _G)
    else
        logreport ("both", 0, "init",
                   "irrecoverable error during pop_namespace: no globals to restore")
        os.exit ()
    end
end

--- below paths are relative to the texmf-context
local ltx = "tex/generic/context/luatex"
local ctx = { "tex/context/base/mkiv", "tex/context/base" }

local context_modules = {

  --- Since 2.6 those are directly provided by the Lualibs package.
  { false, "l-lua"      },
  { false, "l-lpeg"     },
  { false, "l-function" },
  { false, "l-string"   },
  { false, "l-table"    },
  { false, "l-io"       },
  { false, "l-file"     },
  { false, "l-boolean"  },
  { false, "l-math"     },
  { false, "l-unicode"  }, -- NEW UF 19.09.2018
  { false, "util-str"   },
  { false, "util-fil"   },

  --- These constitute the fontloader proper.
  { ltx,   "luatex-basics-gen" },
  { ctx,   "data-con"          },
  { ltx,   "luatex-basics-nod" },
  { ctx,   "font-ini"          },
  { ltx,   "luatex-fonts-mis"  }, -- NEW UF 19.09.2018
  { ctx,   "font-con"          },
  { ltx,   "luatex-fonts-enc"  },
  { ctx,   "font-cid"          },
  { ctx,   "font-map"          },
--  { ltx,   "luatex-fonts-syn"  }, -- ignore??
  { ctx,   "font-vfc"          }, -- NEW UF 19.09.2018
  { ctx,   "font-oti"          },
  { ctx,   "font-otr"          },
  { ctx,   "font-ott"          }, 
  { ctx,   "font-cff"          },
  { ctx,   "font-ttf"          },
  { ctx,   "font-dsp"          },
  { ctx,   "font-oup"          },
  { ctx,   "font-otl"          },
  { ctx,   "font-oto"          },
  { ctx,   "font-otj"          },
  { ctx,   "font-ota"          },
  { ctx,   "font-ots"          },
  { ctx,   "font-osd"          },
  { ctx,   "font-ocl"          },
  { ctx,   "font-otc"          },
  { ctx,   "font-onr"          },
  { ctx,   "font-one"          },
  { ctx,   "font-afk"          },
  { ctx,   "font-tfm"          },
  { ctx,   "font-lua"          },
  { ctx,   "font-def"          },
  { ltx,   "luatex-fonts-def"  },  
  { ltx,   "luatex-fonts-ext"  },
  { ctx,   "font-imp-tex"      },
  { ctx,   "font-imp-ligatures"},
  { ctx,   "font-imp-italics"  },
  { ctx,   "font-imp-effects"  },
  { ltx,   "luatex-fonts-lig"  },
  { ltx,   "luatex-fonts-gbn"  },
  

} --[[context_modules]]

local load_context_modules = function (pth)

  local load_module   = luaotfload.loaders.context
  local ignore_module = luaotfload.loaders.ignore

  logreport ("both", 2, "init",
             "Loading fontloader components from context.")
  local n = #context_modules
  for i = 1, n do
    local sub, spec = unpack (context_modules [i])
    if sub == false then
      ignore_module (spec)
    else
      local tsub = type (sub)
      if not pth then
        load_module (spec)
      elseif tsub == "string" then
        load_module (spec, file.join (pth, sub))
      elseif tsub == "table" then
        local pfx
        local nsub = #sub
        for j = 1, nsub do
          local full = file.join (pth, sub [j])
          if lfsisdir (full) then --- pick the first real one
            pfx = full
            break
          end
        end
        if pfx then
          load_module (spec, pfx)
        else
          logreport ("both", 0, "init",
                     "None of the %d search paths for module %q exist; \z
                      falling back to default path.",
                     nsub, tostring (spec))
          load_module (spec) --- maybe we’ll get by after all?
        end
      else
        logreport ("both", 0, "init",
                   "Internal error, please report. \z
                    This is not your fault.")
        os.exit (-1)
      end
    end
  end

end

local init_adapt = function ()

  local context_environment  = { }
  local our_environment      = push_namespaces ()

  --[[doc--

      The font loader requires that the attribute with index zero be
      zero. We happily oblige.
      (Cf. \fileent{luatex-fonts-nod.lua}.)

  --doc]]--

  tex.attribute[0] = 0

  return our_environment, context_environment

end --- [init_adapt]

--[[doc--

  In Context, characters.data is where the data from char-def.lua
  resides. The file is huge (>4.4 MB as of 2016) and only a stripped
  down version is part of the isolated font loader. Nevertheless, we
  include an excerpt generated by the mkcharacters script that contains
  a subset of the fields of each character defined and some extra
  metadata.

  Currently, these are (compare the mkcharacters script!)

    · "direction"
    · "mirror"
    · "category"
    · "textclass"

  The directional information is required for packages like Simurgh [0]
  to work correctly. In an early stage [1] it was necessary to load
  further files from Context directly, including the full blown version
  of char-def.  Since we have no use for most of the so imported
  functionality, the required parts have been isolated and are now
  instated along with luaotfload-characters.lua. We can extend the set
  of imported features easily should it not be enough.

  [0] https://github.com/persian-tex/simurgh
  [1] http://tex.stackexchange.com/a/132301/14066

--doc]]--

characters         = characters or { } --- should be created in basics-gen
characters.data    = nil
local chardef      = "luaotfload-characters"

do
  local setmetatableindex = function (t, f)
    local mt = getmetatable (t)
    if mt then
      mt.__index = f
    else
      setmetatable (t, { __index = f })
    end
  end

  --- there are some special tables for each field that provide access
  --- to fields of the character table by means of a metatable

  local mkcharspecial = function (characters, tablename, field)

    local chardata = characters.data

    if chardata then
      local newspecial        = { }
      characters [tablename]  = newspecial --> e.g. “characters.data.mirrors”

      local idx = function (t, char)
        local c = chardata [char]
        if c then
          local m = c [field] --> e.g. “mirror”
          if m then
            t [char] = m
            return m
          end
        end
        newspecial [char] = false
        return char
      end

      setmetatableindex (newspecial, idx)
    end

  end

  local mkcategories = function (characters) -- different from the others

    local chardata         = characters.data
    local categories       = characters.categories or { }
    characters.categories  = categories

    setmetatable (categories, { __index = function (t, char)
      if char then
        local c = chardata [char]
        c = c.category or char
        t [char] = c
        return c
      end
    end})

  end

  local load_failed = false
  local chardata --> characters.data; loaded on demand

  local load_chardef = function ()

    logreport ("both", 1, "aux", "Loading character metadata from %s.", chardef)
    chardata = dofile (kpse.find_file (chardef, "lua"))

    if chardata == nil then
      logreport ("both", 0, "aux",
                 "Could not load %s; continuing with empty character table.",
                 chardef)
      chardata    = { }
      load_failed = true
    end

    characters             = { } --- nuke metatable
    characters.data        = chardata
    characters.classifiers = chardata.classifiers
    chardata.classifiers   = nil

    --- institute some of the functionality from char-ini.lua

    mkcharspecial (characters, "mirrors",     "mirror")
    mkcharspecial (characters, "directions",  "direction")
    mkcharspecial (characters, "textclasses", "textclass")
    mkcategories  (characters)

  end

  local charindex = function (t, k)
    if chardata == nil and load_failed ~= true then
      load_chardef ()
    end

    return rawget (characters, k)
  end

  setmetatableindex (characters, charindex)

end

local init_main = function ()

  local load_fontloader_module = luaotfload.loaders.fontloader
  local ignore_module          = luaotfload.loaders.ignore

  --[[doc--

      Now that things are sorted out we can finally load the
      fontloader.

  --doc]]--

  local fontloader = config.luaotfload and config.luaotfload.run.fontloader
                                        or "reference"
  fontloader = tostring (fontloader)

  if fontloader == "reference" then
    logreport ("log", 0, "init", "Using reference fontloader.")
    load_fontloader_module (luaotfload.fontloader_package)

  elseif fontloader == "default" then
    --- Same as above but loader name not correctly replaced by the file name
    --- of our fontloader package. Perhaps something’s wrong with the status
    --- file which contains the datestamped filename? In any case, it can’t
    --- hurt reporting it as a bug.
    logreport ("both", 0, "init", "Fontloader substitution failed, got “default”.")
    logreport ("log",  4, "init", "Falling back to reference fontloader.")
    load_fontloader_module (luaotfload.fontloader_package)

  elseif fontloader == "unpackaged" then
    logreport ("log", 0, "init",
               "Loading fontloader components individually.")
    --- The loading sequence is known to change, so this might have to be
    --- updated with future updates. Do not modify it though unless there is
    --- a change to the upstream package!

    --- Since 2.6 those are directly provided by the Lualibs package.
    ignore_module "l-lua"
    ignore_module "l-lpeg"
    ignore_module "l-function"
    ignore_module "l-string"
    ignore_module "l-table"
    ignore_module "l-io"
    ignore_module "l-file"
    ignore_module "l-boolean"
    ignore_module "l-math"
    ignore_module "util-str"
    ignore_module "util-fil"
    ignore_module "luatex-basics-gen"

    load_fontloader_module "data-con"
    load_fontloader_module "basics-nod"
    load_fontloader_module "font-ini"
    load_fontloader_module "fonts-mis"
    load_fontloader_module "font-con"
    load_fontloader_module "fonts-enc"
    load_fontloader_module "font-cid"
    load_fontloader_module "font-map"
--    load_fontloader_module "fonts-syn" -- ignore?
    load_fontloader_module "font-vfc"
    load_fontloader_module "font-otr"
    load_fontloader_module "font-oti"    
    load_fontloader_module "font-ott"
    load_fontloader_module "font-cff"
    load_fontloader_module "font-ttf"
    load_fontloader_module "font-dsp"
    load_fontloader_module "font-oup"
    load_fontloader_module "font-otl"
    load_fontloader_module "font-oto"
    load_fontloader_module "font-otj"
    load_fontloader_module "font-ota"
    load_fontloader_module "font-ots"
    load_fontloader_module "font-osd"
    load_fontloader_module "font-ocl"
    load_fontloader_module "font-otc"
    load_fontloader_module "font-onr"
    load_fontloader_module "font-one"
    load_fontloader_module "font-afk"
    load_fontloader_module "font-tfm"
    load_fontloader_module "font-lua"
    load_fontloader_module "font-def"
    load_fontloader_module "fonts-def"
    load_fontloader_module "fonts-ext"
    load_fontloader_module "font-imp-tex"
    load_fontloader_module "font-imp-ligatures"
    load_fontloader_module "font-imp-italics"
    load_fontloader_module "font-imp-effects"
    load_fontloader_module "fonts-lig"
    load_fontloader_module "fonts-gbn"

  elseif fontloader == "context" then
    logreport ("log", 0, "init",
               "Loading Context modules in lookup path.")
    load_context_modules ()

  elseif lfsisdir (fontloader) then
    logreport ("log", 0, "init",
               "Loading Context files under prefix “%s”.",
               fontloader)
    load_context_modules (fontloader)

  elseif lfs.isfile (fontloader) then
    logreport ("log", 0, "init",
               "Loading fontloader from absolute path “%s”.",
               fontloader)
    local _void = require (fontloader)

  elseif kpselookup (fontloader) then
    local path = kpselookup (fontloader)
    logreport ("log", 0, "init",
               "Loading fontloader “%s” from kpse-resolved path “%s”.",
               fontloader, path)
    local _void = require (path)

  elseif kpselookup (("fontloader-%s.lua"):format(fontloader)) then
    logreport ("log", 0, "init",
               "Using predefined fontloader “%s”.",
               fontloader)
    load_fontloader_module (fontloader)

  else
    logreport ("both", 0, "init",
               "No match for requested fontloader “%s”.",
               fontloader)
    fontloader = luaotfload.fontloader_package
    logreport ("both", 0, "init",
               "Defaulting to predefined fontloader “%s”.",
               fontloader)
    load_fontloader_module (fontloader)
  end

  ---load_fontloader_module "font-odv.lua" --- <= Devanagari support from Context

  logreport ("log", 0, "init",
             "Context OpenType loader version “%s”",
             fonts.handlers.otf.version)
end --- [init_main]

local init_cleanup = function (store)
  --- reinstate all the stuff we had to move out of the way to
  --- accomodate the loader

  --[[doc--

      Here we adjust the globals created during font loader
      initialization. If the second argument to
      \luafunction{pop_namespaces()} is \verb|true| this will restore the
      state of \luafunction{_G}, eliminating every global generated since
      the last call to \luafunction{push_namespaces()}. At the moment we
      see no reason to do this, and since the font loader is considered
      an essential part of \identifier{luatex} as well as a very well
      organized piece of code, we happily concede it the right to add to
      \luafunction{_G} if needed.

  --doc]]--

  pop_namespaces (store.our_environment,
                  false,
                  store.context_environment)

  --[[doc--

      \subsection{Callbacks}
        After the fontloader is ready we can restore the callback trap
        from \identifier{luatexbase}.

  --doc]]--

  logreport ("log", 4, "init",
             "Restoring original callback.register().")
  callback.register = store.trapped_register
end --- [init_cleanup]

local init_post_install_callbacks = function ()
  --[[doc--

    we do our own callback handling with the means provided by
    luatexbase.
    note: \luafunction{pre_linebreak_filter} and
    \luafunction{hpack_filter} are coupled in \context in the
    concept of \emphasis{node processor}.

  --doc]]--

  -- MK Pass current text direction to simple_font_handler
  local handler = nodes.simple_font_handler
  local callback = function(head, groupcode, _, _, direction)
    return handler(head, groupcode, nil, nil, direction or tex.get'textdir')
  end
  luatexbase.add_to_callback("pre_linebreak_filter",
                             callback,
                             "luaotfload.node_processor",
                             1)
  luatexbase.add_to_callback("hpack_filter",
                             callback,
                             "luaotfload.node_processor",
                             1)
  -- /MK
end

local init_post_load_agl = function ()

  --[[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]]--

  local findfile  = resolvers.findfile
  local encodings = fonts.encodings

  if not findfile or not encodings then
    --- Might happen during refactoring; we continue graciously but in
    --- a somewhat defect state.
    logreport ("log", 0, "init",
               "preconditions unmet, skipping the Adobe Glyph List; "
               .. "this is a Luaotfload bug.")
    return
  end

  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

  local agl_init = { }      --- start out empty, fill on demand
  encodings.agl  = agl_init --- ugh, replaced again later

  setmetatable (agl_init, { __index = function (t, k)

    if k ~= "unicodes" then
      return nil
    end

    local glyphlist = findfile "luaotfload-glyphlist.lua"
    if glyphlist then
      logreport ("log", 1, "init", "loading the Adobe glyph list")
    else
      glyphlist = findfile "font-age.lua"
      logreport ("both", 0, "init",
                 "loading the extended glyph list from ConTeXt")
    end

    if not glyphlist then
      logreport ("both", 4, "init",
                 "Adobe glyph list not found, please check your installation.")
      return nil
    end
    logreport ("both", 4, "init",
               "found Adobe glyph list file at ``%s``, using that.",
               glyphlist)

    local unicodes = dofile(glyphlist)
    encodings.agl  = { unicodes = unicodes }
    return unicodes
  end })

end

--- (unit -> unit) list
local init_post_actions = {
  init_post_install_callbacks,
  init_post_load_agl,
}

--- unit -> size_t
local init_post = function ()
  --- hook for actions that need to take place after the fontloader is
  --- installed

  local n = #init_post_actions
  for i = 1, n do
    local action = init_post_actions[i]
    local taction = type (action)
    if not action or taction ~= "function" then
      logreport ("both", 1, "init",
                 "post hook WARNING: action %d not a function but %s/%s; ignoring.",
                 i, action, taction)
    else
      --- call closure
      action ()
    end
  end

  return n
end --- [init_post]

return {
  early = init_early,
  main = function (store)
    local starttime = os.gettimeofday ()
    store.our_environment, store.context_environment = init_adapt ()
    init_main ()
    init_cleanup (store)
    logreport ("both", 1, "init",
               "fontloader loaded in %0.3f seconds",
               os.gettimeofday() - starttime)
    local n = init_post ()
    logreport ("both", 5, "init", "post hook terminated, %d actions performed", n)
    return true
  end
}

-- vim:tw=79:sw=2:ts=2:expandtab