Debian Codegraph @

texmf-dist/tex/luatex/luaotfload/fontloader-basics-gen.lua

raw
1 if not modules then modules = { } end modules ['luat-basics-gen'] = {
2 version = 1.100,
3 comment = "companion to luatex-*.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 if context then
10 os.exit()
11 end
12
13 -- We could load a few more of the general context libraries but it would
14 -- not make plain / latex users more happy I guess. So, we stick to some
15 -- placeholders.
16
17 local match, gmatch, gsub, lower = string.match, string.gmatch, string.gsub, string.lower
18 local formatters, split, format, dump = string.formatters, string.split, string.format, string.dump
19 local loadfile, type = loadfile, type
20 local setmetatable, getmetatable, collectgarbage = setmetatable, getmetatable, collectgarbage
21 local floor = math.floor
22 local mkdirp = lfs.mkdirp or lfs.mkdirs
23
24 local dummyfunction = function()
25 end
26
27 local dummyreporter = function(c)
28 return function(f,...)
29 local r = texio.reporter or texio.write_nl
30 if f then
31 r(c .. " : " .. (formatters or format)(f,...))
32 else
33 r("")
34 end
35 end
36 end
37
38 local dummyreport = function(c,f,...)
39 local r = texio.reporter or texio.write_nl
40 if f then
41 r(c .. " : " .. (formatters or format)(f,...))
42 else
43 r("")
44 end
45 end
46
47 statistics = {
48 register = dummyfunction,
49 starttiming = dummyfunction,
50 stoptiming = dummyfunction,
51 elapsedtime = nil,
52 }
53
54 directives = {
55 register = dummyfunction,
56 enable = dummyfunction,
57 disable = dummyfunction,
58 }
59
60 trackers = {
61 register = dummyfunction,
62 enable = dummyfunction,
63 disable = dummyfunction,
64 }
65
66 experiments = {
67 register = dummyfunction,
68 enable = dummyfunction,
69 disable = dummyfunction,
70 }
71
72 storage = { -- probably no longer needed
73 register = dummyfunction,
74 shared = { },
75 }
76
77 logs = {
78 new = dummyreporter,
79 reporter = dummyreporter,
80 messenger = dummyreporter,
81 report = dummyreport,
82 }
83
84 callbacks = {
85 register = function(n,f)
86 return callback.register(n,f)
87 end,
88 }
89
90 utilities = utilities or { }
91
92 utilities.storage = utilities.storage or {
93 allocate = function(t)
94 return t or { }
95 end,
96 mark = function(t)
97 return t or { }
98 end,
99 }
100
101 utilities.parsers = utilities.parsers or {
102 -- these are less flexible than in context but ok
103 -- for generic purpose
104 settings_to_array = function(s)
105 return split(s,",")
106 end,
107 settings_to_hash = function(s)
108 local t = { }
109 for k, v in gmatch((gsub(s,"^{(.*)}$", "%1")),"([^%s,=]+)=([^%s,]+)") do
110 t[k] = v
111 end
112 return t
113 end,
114 settings_to_hash_colon_too = function(s)
115 local t = { }
116 for k, v in gmatch((gsub(s,"^{(.*)}$", "%1")),"([^%s,=:]+)[=:]([^%s,]+)") do
117 t[k] = v
118 end
119 return t
120 end,
121 }
122
123 characters = characters or {
124 data = { }
125 }
126
127 -- we need to cheat a bit here
128
129 texconfig.kpse_init = true
130
131 resolvers = resolvers or { } -- no fancy file helpers used
132
133 local remapper = {
134 otf = "opentype fonts",
135 ttf = "truetype fonts",
136 ttc = "truetype fonts",
137 cid = "cid maps",
138 cidmap = "cid maps",
139 -- fea = "font feature files", -- no longer supported
140 pfb = "type1 fonts", -- needed for vector loading
141 afm = "afm",
142 enc = "enc files",
143 lua = "tex",
144 }
145
146 function resolvers.findfile(name,fileformat)
147 name = gsub(name,"\\","/")
148 if not fileformat or fileformat == "" then
149 fileformat = file.suffix(name)
150 if fileformat == "" then
151 fileformat = "tex"
152 end
153 end
154 fileformat = lower(fileformat)
155 fileformat = remapper[fileformat] or fileformat
156 local found = kpse.find_file(name,fileformat)
157 if not found or found == "" then
158 found = kpse.find_file(name,"other text files")
159 end
160 return found
161 end
162
163 resolvers.findbinfile = resolvers.findfile
164
165 function resolvers.loadbinfile(filename,filetype)
166 local data = io.loaddata(filename)
167 return true, data, #data
168 end
169
170 function resolvers.resolve(s)
171 return s
172 end
173
174 function resolvers.unresolve(s)
175 return s
176 end
177
178 -- Caches ... I will make a real stupid version some day when I'm in the
179 -- mood. After all, the generic code does not need the more advanced
180 -- ConTeXt features. Cached data is not shared between ConTeXt and other
181 -- usage as I don't want any dependency at all. Also, ConTeXt might have
182 -- different needs and tricks added.
183
184 --~ containers.usecache = true
185
186 caches = { }
187
188 local writable = nil
189 local readables = { }
190 local usingjit = jit
191
192 if not caches.namespace or caches.namespace == "" or caches.namespace == "context" then
193 caches.namespace = 'generic'
194 end
195
196 do
197
198 -- standard context tree setup
199
200 local cachepaths = kpse.expand_var('$TEXMFCACHE') or ""
201
202 -- quite like tex live or so (the weird $TEXMFCACHE test seems to be needed on miktex)
203
204 if cachepaths == "" or cachepaths == "$TEXMFCACHE" then
205 cachepaths = kpse.expand_var('$TEXMFVAR') or ""
206 end
207
208 -- this also happened to be used (the weird $TEXMFVAR test seems to be needed on miktex)
209
210 if cachepaths == "" or cachepaths == "$TEXMFVAR" then
211 cachepaths = kpse.expand_var('$VARTEXMF') or ""
212 end
213
214 -- and this is a last resort (hm, we could use TEMP or TEMPDIR)
215
216 if cachepaths == "" then
217 local fallbacks = { "TMPDIR", "TEMPDIR", "TMP", "TEMP", "HOME", "HOMEPATH" }
218 for i=1,#fallbacks do
219 cachepaths = os.getenv(fallbacks[i]) or ""
220 if cachepath ~= "" and lfs.isdir(cachepath) then
221 break
222 end
223 end
224 end
225
226 if cachepaths == "" then
227 cachepaths = "."
228 end
229
230 cachepaths = split(cachepaths,os.type == "windows" and ";" or ":")
231
232 for i=1,#cachepaths do
233 local cachepath = cachepaths[i]
234 if not lfs.isdir(cachepath) then
235 mkdirp(cachepath .. '/') -- needed for texlive and latex
236 if lfs.isdir(cachepath) then
237 logs.report("system","creating cache path '%s'",cachepath)
238 end
239 end
240 if file.is_writable(cachepath) then
241 writable = file.join(cachepath,"luatex-cache")
242 lfs.mkdir(writable)
243 writable = file.join(writable,caches.namespace)
244 lfs.mkdir(writable)
245 break
246 end
247 end
248
249 for i=1,#cachepaths do
250 if file.is_readable(cachepaths[i]) then
251 readables[#readables+1] = file.join(cachepaths[i],"luatex-cache",caches.namespace)
252 end
253 end
254
255 if not writable then
256 logs.report("system","no writeable cache path, quiting")
257 os.exit()
258 elseif #readables == 0 then
259 logs.report("system","no readable cache path, quiting")
260 os.exit()
261 elseif #readables == 1 and readables[1] == writable then
262 logs.report("system","using cache '%s'",writable)
263 else
264 logs.report("system","using write cache '%s'",writable)
265 logs.report("system","using read cache '%s'",table.concat(readables," "))
266 end
267
268 end
269
270 function caches.getwritablepath(category,subcategory)
271 local path = file.join(writable,category)
272 lfs.mkdir(path)
273 path = file.join(path,subcategory)
274 lfs.mkdir(path)
275 return path
276 end
277
278 function caches.getreadablepaths(category,subcategory)
279 local t = { }
280 for i=1,#readables do
281 t[i] = file.join(readables[i],category,subcategory)
282 end
283 return t
284 end
285
286 local function makefullname(path,name)
287 if path and path ~= "" then
288 return file.addsuffix(file.join(path,name),"lua"), file.addsuffix(file.join(path,name),usingjit and "lub" or "luc")
289 end
290 end
291
292 function caches.is_writable(path,name)
293 local fullname = makefullname(path,name)
294 return fullname and file.is_writable(fullname)
295 end
296
297 function caches.loaddata(readables,name,writable)
298 for i=1,#readables do
299 local path = readables[i]
300 local loader = false
301 local luaname, lucname = makefullname(path,name)
302 if lfs.isfile(lucname) then
303 logs.report("system","loading luc file '%s'",lucname)
304 loader = loadfile(lucname)
305 end
306 if not loader and lfs.isfile(luaname) then
307 -- can be different paths when we read a file database from disk
308 local luacrap, lucname = makefullname(writable,name)
309 logs.report("system","compiling luc file '%s'",lucname)
310 if lfs.isfile(lucname) then
311 loader = loadfile(lucname)
312 end
313 caches.compile(data,luaname,lucname)
314 if lfs.isfile(lucname) then
315 logs.report("system","loading luc file '%s'",lucname)
316 loader = loadfile(lucname)
317 else
318 logs.report("system","error in loading luc file '%s'",lucname)
319 end
320 if not loader then
321 logs.report("system","loading lua file '%s'",luaname)
322 loader = loadfile(luaname)
323 else
324 logs.report("system","error in loading lua file '%s'",luaname)
325 end
326 end
327 if loader then
328 loader = loader()
329 collectgarbage("step")
330 return loader
331 end
332 end
333 return false
334 end
335
336 function caches.savedata(path,name,data)
337 local luaname, lucname = makefullname(path,name)
338 if luaname then
339 logs.report("system","saving lua file '%s'",luaname)
340 table.tofile(luaname,data,true)
341 if lucname and type(caches.compile) == "function" then
342 os.remove(lucname) -- better be safe
343 logs.report("system","saving luc file '%s'",lucname)
344 caches.compile(data,luaname,lucname)
345 end
346 end
347 end
348
349 -- The method here is slightly different from the one we have in context. We
350 -- also use different suffixes as we don't want any clashes (sharing cache
351 -- files is not that handy as context moves on faster.)
352
353 function caches.compile(data,luaname,lucname)
354 local d = io.loaddata(luaname)
355 if not d or d == "" then
356 d = table.serialize(data,true) -- slow
357 end
358 if d and d ~= "" then
359 local f = io.open(lucname,'wb')
360 if f then
361 local s = loadstring(d)
362 if s then
363 f:write(dump(s,true))
364 end
365 f:close()
366 end
367 end
368 end
369
370 -- simplfied version:
371
372 function table.setmetatableindex(t,f)
373 if type(t) ~= "table" then
374 f, t = t, { }
375 end
376 local m = getmetatable(t)
377 if f == "table" then
378 f = function(t,k) local v = { } t[k] = v return v end
379 end
380 if m then
381 m.__index = f
382 else
383 setmetatable(t,{ __index = f })
384 end
385 return t
386 end
387
388 function table.makeweak(t)
389 local m = getmetatable(t)
390 if m then
391 m.__mode = "v"
392 else
393 setmetatable(t,{ __mode = "v" })
394 end
395 return t
396 end
397
398 -- helper for plain:
399
400 arguments = { }
401
402 if arg then
403 for i=1,#arg do
404 local k, v = match(arg[i],"^%-%-([^=]+)=?(.-)$")
405 if k and v then
406 arguments[k] = v
407 end
408 end
409 end
410
411 -- another one
412
413 if not number.idiv then
414 function number.idiv(i,d)
415 return floor(i/d) -- i//d in 5.3
416 end
417 end
418
419 -- hook into unicode
420
421 local u = unicode and unicode.utf8
422
423 if u then
424
425 utf.lower = u.lower
426 utf.upper = u.upper
427 utf.char = u.char
428 utf.byte = u.byte
429 utf.len = u.len
430
431 -- needed on font-*
432
433 if lpeg.setutfcasers then
434 lpeg.setutfcasers(u.lower,u.upper)
435 end
436
437 -- needed on font-otr
438
439 local bytepairs = string.bytepairs
440 local utfchar = utf.char
441 local concat = table.concat
442
443 function utf.utf16_to_utf8_be(s)
444 if not s then
445 return nil
446 elseif s == "" then
447 return ""
448 end
449 local result, r, more = { }, 0, 0
450 for left, right in bytepairs(s) do
451 if right then
452 local now = 256*left + right
453 if more > 0 then
454 now = (more-0xD800)*0x400 + (now-0xDC00) + 0x10000
455 more = 0
456 r = r + 1
457 result[r] = utfchar(now)
458 elseif now >= 0xD800 and now <= 0xDBFF then
459 more = now
460 else
461 r = r + 1
462 result[r] = utfchar(now)
463 end
464 end
465 end
466 return concat(result)
467 end
468
469 local characters = string.utfcharacters
470
471 function utf.split(str)
472 local t, n = { }, 0
473 for s in characters(str) do
474 n = n + 1
475 t[n] = s
476 end
477 return t
478 end
479
480 end