Debian Codegraph @

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

raw
1 -----------------------------------------------------------------------
2 -- FILE: luaotfload-fallback.lua
3 -- DESCRIPTION: part of luaotfload / fallback
4 -----------------------------------------------------------------------
5
6 assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") {
7 name = "luaotfload-fallback",
8 version = "3.29", --TAGVERSION
9 date = "2024-12-03", --TAGDATE
10 description = "luaotfload submodule / fallback",
11 license = "GPL v2.0",
12 author = "Marcel Krüger"
13 }
14
15 local nodenew = node.direct.new
16 local getfont = font.getfont
17 local setfont = node.direct.setfont
18 local getwhd = node.direct.getwhd
19 local insert_after = node.direct.insert_after
20 local traverse_char = node.direct.traverse_char
21 local protect_glyph = node.direct.protect_glyph
22 local otffeatures = fonts.constructors.newfeatures "otf"
23 -- local normalize = fonts.handlers.otf.features.normalize
24 local definers = fonts.definers
25 local define_font -- = luaotfload.define_font % This is set when the first font is loaded.
26
27 local fallback_table_fontnames = {}
28
29 local fallback_table = setmetatable({}, {
30 __index = function(t, name)
31 local names = fallback_table_fontnames[name]
32 if not names then
33 t[name] = false
34 return false
35 end
36 local res = setmetatable({}, {
37 __index = function(tt, size)
38 local lookup = {}
39 for i=#names,1,-1 do
40 local f = define_font(names[i] .. ';-fallback', size)
41 local fid
42 if type(f) == 'table' then
43 fid = font.define(f)
44 definers.register(f, fid)
45 elseif f then
46 fid = f
47 f = font.getfont(fid)
48 end
49 lookup[-i] = f -- Needed for multiscript interactions
50 for uni, _ in next, f.characters do
51 rawset(lookup, uni, fid)
52 end
53 end
54 tt[size] = lookup
55 return lookup
56 end,
57 })
58 t[name] = res
59 return res
60 end,
61 })
62
63 local fallback_lookups = setmetatable({}, {
64 __index = function(t, fid)
65 local f = font.getfont(fid)
66 -- table.tofile('myfont2', f)
67 local res = f and f.fallback_lookup or false
68 if res then
69 res = table.merged(res)
70 for uni in next, f.characters do
71 rawset(res, uni, nil)
72 end
73 end
74 t[fid] = res
75 return res
76 end,
77 })
78
79 local function makefallbackfont(tfmdata, _, fallback)
80 local t = fallback_table[fallback]
81 if not t then error(string.format("Unknown fallback table %s", fallback)) end
82 fallback = t[tfmdata.size]
83 local fallback_lookup = {}
84 tfmdata.fallback_lookup = fallback
85 end
86
87 local glyph_id = node.id'glyph'
88 -- TODO: inherited fonts (combining accents etc.)
89 local function dofallback(head, _, _, _, direction)
90 head = node.direct.todirect(head)
91 local last_fid, last_fallbacks
92 for cur, cid, fid in traverse_char(head) do
93 if fid ~= last_fid then
94 last_fid, last_fallbacks = fid, fallback_lookups[fid]
95 end
96 if last_fallbacks then
97 local new_fid = last_fallbacks[cid]
98 if new_fid then
99 setfont(cur, new_fid)
100 end
101 end
102 end
103 end
104
105 function luaotfload.add_fallback(name, fonts)
106 define_font = define_font or luaotfload.define_font -- Lazy loading because this file gets loaded before define_font is defined
107 if fonts == nil then
108 fonts = name
109 name = #fallback_table_fontnames + 1
110 else
111 name = name:lower()
112 end
113 fallback_table_fontnames[name] = fonts
114 fallback_table[name] = nil
115 return name
116 end
117
118 otffeatures.register {
119 name = "fallback",
120 description = "Fill in missing glyphs from other fonts",
121 manipulators = {
122 node = makefallbackfont,
123 plug = makefallbackfont,
124 },
125 -- processors = { -- processors would be nice, but they are applied
126 -- -- too late for our purposes
127 -- node = donotdef,
128 -- }
129 }
130
131 return {
132 process = dofallback,
133 }
134 --- vim:sw=2:ts=2:expandtab:tw=71