data-env.lua /size: 9758 b    last modification: 2023-12-21 09:44
1if not modules then modules = { } end modules ['data-env'] = {
2    version   = 1.001,
3    comment   = "companion to luat-lib.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files",
7}
8
9local lower, gsub = string.lower, string.gsub
10local next, rawget = next, rawget
11
12local resolvers = resolvers
13
14local allocate          = utilities.storage.allocate
15local setmetatableindex = table.setmetatableindex
16local sortedhash        = table.sortedhash
17local suffixonly        = file.suffixonly
18
19local formats           = allocate()
20local suffixes          = allocate()
21local dangerous         = allocate()
22local suffixmap         = allocate()
23local usertypes         = allocate()
24
25resolvers.formats       = formats
26resolvers.suffixes      = suffixes
27resolvers.dangerous     = dangerous
28resolvers.suffixmap     = suffixmap
29resolvers.usertypes     = usertypes
30
31local luasuffixes       = utilities.lua.suffixes
32
33local relations = allocate { -- todo: handlers also here
34    core = {
35        ofm = { -- will become obsolete
36            names    = { "ofm", "omega font metric", "omega font metrics" },
37            variable = 'OFMFONTS',
38            suffixes = { 'ofm', 'tfm' },
39        },
40        ovf = { -- will become obsolete
41            names    = { "ovf", "omega virtual font", "omega virtual fonts" },
42            variable = 'OVFFONTS',
43            suffixes = { 'ovf', 'vf' },
44        },
45        tfm = {
46            names    = { "tfm", "tex font metric", "tex font metrics" },
47            variable = 'TFMFONTS',
48            suffixes = { 'tfm' },
49        },
50        vf = {
51            names    = { "vf", "virtual font", "virtual fonts" },
52            variable = 'VFFONTS',
53            suffixes = { 'vf' },
54        },
55        otf = {
56            names    = { "otf", "opentype", "opentype font", "opentype fonts"},
57            variable = 'OPENTYPEFONTS',
58            suffixes = { 'otf' },
59        },
60        ttf = {
61            names    = { "ttf", "truetype", "truetype font", "truetype fonts", "truetype collection", "truetype collections", "truetype dictionary", "truetype dictionaries" },
62            variable = 'TTFONTS',
63            suffixes = { 'ttf', 'ttc', 'dfont' },
64        },
65        afm = {
66            names    = { "afm", "adobe font metric", "adobe font metrics" },
67            variable = "AFMFONTS",
68            suffixes = { "afm" },
69        },
70        pfb = {
71            names    = { "pfb", "type1", "type 1", "type1 font", "type 1 font", "type1 fonts", "type 1 fonts" },
72            variable = 'T1FONTS',
73            suffixes = { 'pfb', 'pfa' },
74        },
75        fea = {
76            names    = { "fea", "font feature", "font features", "font feature file", "font feature files" },
77            variable = 'FONTFEATURES',
78            suffixes = { 'fea' },
79        },
80        cid = {
81            names    = { "cid", "cid map", "cid maps", "cid file", "cid files" },
82            variable = 'FONTCIDMAPS',
83            suffixes = { 'cid', 'cidmap' },
84        },
85        fmt = {
86            names    = { "fmt", "format", "tex format" },
87            variable = 'TEXFORMATS',
88            suffixes = { 'fmt' },
89        },
90        mem = { -- will become obsolete
91            names    = { 'mem', "metapost format" },
92            variable = 'MPMEMS',
93            suffixes = { 'mem' },
94        },
95        mp = {
96            names    = { "mp" },
97            variable = 'MPINPUTS',
98            suffixes = CONTEXTLMTXMODE > 0
99                and { 'mpxl', 'mpvi', 'mpiv', 'mpii', 'mp' }
100                or  {         'mpvi', 'mpiv', 'mpii', 'mp' },
101            usertype = true,
102        },
103        tex = {
104            names    = { "tex" },
105            variable = 'TEXINPUTS',
106            suffixes = { "tex", "mkiv", "mkvi", "mkxl", "mklx", "mkii", "cld", "lfg", "xml" }, -- known suffixes have less lookups
107            usertype = true,
108        },
109        icc = {
110            names    = { "icc", "icc profile", "icc profiles" },
111            variable = 'ICCPROFILES',
112            suffixes = { 'icc' },
113        },
114        texmfscripts = {
115            names    = { "texmfscript", "texmfscripts", "script", "scripts" },
116            variable = 'TEXMFSCRIPTS',
117            suffixes = { 'lua', 'rb', 'pl', 'py' },
118        },
119        lua = {
120            names    = { "lua" },
121            variable = 'LUAINPUTS',
122            suffixes = { luasuffixes.lmt, luasuffixes.lua, luasuffixes.luc, luasuffixes.tma, luasuffixes.tmc },
123            usertype = true,
124        },
125        lib = {
126            names    = { "lib" },
127            variable = 'CLUAINPUTS',
128            suffixes = os.libsuffix and { os.libsuffix } or { 'dll', 'so' },
129        },
130        bib = {
131            names    = { 'bib' },
132            variable = 'BIBINPUTS',
133            suffixes = { 'bib' },
134            usertype = true,
135        },
136        bst = {
137            names    = { 'bst' },
138            variable = 'BSTINPUTS',
139            suffixes = { 'bst' },
140            usertype = true,
141        },
142        fontconfig = {
143            names    = { 'fontconfig', 'fontconfig file', 'fontconfig files' },
144            variable = 'FONTCONFIG_PATH',
145        },
146        pk = {
147            names    = { "pk" },
148            variable = 'PKFONTS',
149            suffixes = { 'pk' },
150        },
151    },
152    obsolete = {
153        enc = {
154            names    = { "enc", "enc files", "enc file", "encoding files", "encoding file" },
155            variable = 'ENCFONTS',
156            suffixes = { 'enc' },
157        },
158        map = {
159            names    = { "map", "map files", "map file" },
160            variable = 'TEXFONTMAPS',
161            suffixes = { 'map' },
162        },
163        lig = {
164            names    = { "lig files", "lig file", "ligature file", "ligature files" },
165            variable = 'LIGFONTS',
166            suffixes = { 'lig' },
167        },
168        opl = {
169            names    = { "opl" },
170            variable = 'OPLFONTS',
171            suffixes = { 'opl' },
172        },
173        ovp = {
174            names    = { "ovp" },
175            variable = 'OVPFONTS',
176            suffixes = { 'ovp' },
177        },
178    },
179    kpse = { -- subset
180        base = {
181            names    = { 'base', "metafont format" },
182            variable = 'MFBASES',
183            suffixes = { 'base', 'bas' },
184        },
185        cmap = {
186            names    = { 'cmap', 'cmap files', 'cmap file' },
187            variable = 'CMAPFONTS',
188            suffixes = { 'cmap' },
189        },
190        cnf = {
191            names    = { 'cnf' },
192            suffixes = { 'cnf' },
193        },
194        web = {
195            names    = { 'web' },
196            suffixes = { 'web', 'ch' }
197        },
198        cweb = {
199            names    = { 'cweb' },
200            suffixes = { 'w', 'web', 'ch' },
201        },
202        gf = {
203            names    = { 'gf' },
204            suffixes = { '<resolution>gf' },
205        },
206        mf = {
207            names    = { 'mf' },
208            variable = 'MFINPUTS',
209            suffixes = { 'mf' },
210        },
211        mft = {
212            names    = { 'mft' },
213            suffixes = { 'mft' },
214        },
215        pk = {
216            names    = { 'pk' },
217            suffixes = { '<resolution>pk' },
218        },
219    },
220}
221
222resolvers.relations = relations
223
224-- formats: maps a format onto a variable
225
226function resolvers.updaterelations()
227    -- we sort because we have a few duplicates and we want the same
228    -- mapping every run
229    for category, categories in sortedhash(relations) do
230        for name, relation in sortedhash(categories) do
231            local rn = relation.names
232            local rv = relation.variable
233            if rn and rv then
234                local rs = relation.suffixes
235                local ru = relation.usertype
236                for i=1,#rn do
237                    local rni = lower(gsub(rn[i]," ",""))
238                    formats[rni] = rv
239                    if rs then
240                        suffixes[rni] = rs
241                        for i=1,#rs do
242                            local rsi = rs[i]
243                            if not suffixmap[rsi] then
244                                suffixmap[rsi] = rni
245                            end
246                        end
247                    end
248                end
249                if ru then
250                    usertypes[name] = true
251                end
252            end
253        end
254    end
255end
256
257resolvers.updaterelations() -- push this in the metatable -> newindex
258
259local function simplified(t,k)
260    return k and rawget(t,lower(gsub(k," ",""))) or nil
261end
262
263setmetatableindex(formats,   simplified)
264setmetatableindex(suffixes,  simplified)
265setmetatableindex(suffixmap, simplified)
266
267-- A few accessors, mostly for command line tool.
268
269function resolvers.suffixofformat(str)
270    local s = suffixes[str]
271    return s and s[1] or ""
272end
273
274function resolvers.suffixofformat(str)
275    return suffixes[str] or { }
276end
277
278for name, format in next, formats do
279    dangerous[name] = true -- still needed ?
280end
281
282-- because vf searching is somewhat dangerous, we want to prevent
283-- too liberal searching esp because we do a lookup on the current
284-- path anyway; only tex (or any) is safe
285
286dangerous.tex = nil
287
288--~ print(table.serialize(dangerous))
289
290-- more helpers
291
292function resolvers.formatofvariable(str)
293    return formats[str] or ''
294end
295
296function resolvers.formatofsuffix(str) -- of file
297    return suffixmap[suffixonly(str)] or 'tex' -- so many map onto tex (like mkiv, cld etc)
298end
299
300function resolvers.variableofformat(str)
301    return formats[str] or ''
302end
303
304function resolvers.variableofformatorsuffix(str)
305    local v = formats[str]
306    if v then
307        return v
308    end
309    v = suffixmap[suffixonly(str)]
310    if v then
311        return formats[v]
312    end
313    return ''
314end
315
316