Debian Codegraph @

texmf-dist/tex/luatex/minim/minim-alloc.lua

raw
1
2 local M = {}
3
4 -- 1 messages
5
6 function M.msg(...)
7 texio.write_nl(string.format(...))
8 end
9
10 function M.amsg(...)
11 texio.write(string.format(...))
12 end
13
14 function M.log(...)
15 texio.write_nl('log', string.format(...))
16 end
17
18 function M.alog(...)
19 texio.write('log', string.format(...))
20 end
21
22 function M.term(...)
23 texio.write_nl('term', string.format(...))
24 end
25
26 function M.err (...)
27 tex.error(string.format(...))
28 end
29
30 -- 1 pdf helpers
31
32 local function insert_formatted(t, ...)
33 table.insert(t, string.format(...))
34 end
35
36 local function hexify(c)
37 return string.format('#%02x', c:byte())
38 end
39 function M.pdf_name(n)
40 return '/'..n
41 :gsub('[][(){}<>#%%/\\]', hexify)
42 :gsub('%G', hexify)
43 end
44
45 -- re-encode to utf-16
46 local function pdf_hex_string(text)
47 local str = { [1] = '<feff' }
48 for _, i in utf8.codes(text) do
49 if i <= 0xffff then
50 insert_formatted(str, '%04x', i)
51 else
52 i = i - 0x10000
53 local m = math.floor(i/0x400) + 0xd800
54 local n = ( i % 0x400 ) + 0xdc00
55 insert_formatted(str, '%04x%04x', m, n)
56 end
57 end
58 table.insert(str, '>')
59 return table.concat(str,'')
60 end
61 local function octify(c)
62 return string.format('\\%03o', c:byte())
63 end
64 function M.pdf_string(text)
65 -- do note that \ddd is decimal in lua, octal in pdf
66 if text:match('[^%g\009\010\13 ]') then
67 return pdf_hex_string(text)
68 else
69 return string.format('(%s)', text
70 :gsub('[()\\]','\\%0')
71 :gsub('%c', octify))
72 end
73 end
74
75 -- try and produce a date of the format (D:YYYYMMDD)
76 function M.pdf_date(text)
77 local y, m, d = string.match(text, '^([12][0-9][0-9][0-9])-([0-9][0-9]?)-([0-9][0-9]?)$')
78 if not y then d, m, y = string.match(text, '^([0-9][0-9]?)-([0-9][0-9]?)-([12][0-9][0-9][0-9])$') end
79 if y then
80 return string.format('(D:%4d%02d%02d)', y, m, d)
81 else
82 return string.format('(D:%s)', text)
83 end
84 end
85
86 -- 1 scanning helpers
87
88 local function make_scanner(fn)
89 return function(this, name, multiple)
90 this.scanners[name] = multiple and function()
91 this.res[name] = this.res[name] or { }
92 table.insert(this.res[name], fn())
93 end or function()
94 this.scanners[name] = nil
95 this.res[name] = fn()
96 end
97 return this
98 end
99 end
100
101 function M.options_scanner()
102 return { scanners = { }, res = { },
103 int = make_scanner(token.scan_int),
104 glue = make_scanner(token.scan_glue),
105 dimen = make_scanner(token.scan_dimen),
106 string = make_scanner(token.scan_string),
107 argument = make_scanner(token.scan_argument),
108 word = make_scanner(token.scan_word),
109 list = make_scanner(token.scan_list),
110 keyword = function(this, name, opposite)
111 this.scanners[name] = function()
112 this.res[name] = true
113 if opposite then this.res[opposite] = false end
114 end
115 if opposite then
116 this.scanners[opposite] = function()
117 this.res[name] = false
118 this.res[opposite] = true
119 end
120 end
121 return this
122 end,
123 table = function(this, name)
124 this.scanners[name] = function()
125 this.res[name] = this.res[name] or { }
126 this.res[name][token.scan_string()] = token.scan_string()
127 end
128 return this
129 end,
130 subtable = function(this, name)
131 this.scanners[name] = function()
132 this.res[name] = this.res[name] or { }
133 local sub = token.scan_string();
134 this.res[name][sub] = this.res[name][sub] or { }
135 this.res[name][sub][token.scan_string()] = token.scan_string()
136 end
137 return this
138 end,
139 scan = function(this, defaults)
140 this.res = defaults or this.res
141 repeat
142 local matched = false
143 for name, scanner in pairs(this.scanners) do
144 if token.scan_keyword(name) then
145 matched = true
146 scanner()
147 end
148 end
149 until not matched
150 return this.res
151 end
152 }
153 end
154
155 -- 1 saving modules and tables
156
157 local tables = package.loaded['minim-saved-tables']
158 or { ['minim:modules'] = { } }
159 local modules = tables ['minim:modules']
160
161 function M.remember (name)
162 if modules[name] == nil then
163 modules[name] = false -- will be a number if a bytecode register is reserved
164 modules[#modules+1] = name
165 end
166 end
167
168 function M.saved_table (identifier, table)
169 if tables[identifier] == nil then
170 tables[identifier] = table or { }
171 end
172 return tables[identifier]
173 end
174
175 -- saved tables may only contain values that can be converted to and from
176 -- strings with tostring() or other tables meeting the same requirement.
177 function M.table_to_text (tbl)
178 local r = { }
179 for i,t in pairs(tbl) do
180 local l
181 if type(i) == 'string' then
182 l = string.format('[%q] = ', i)
183 else
184 l = string.format('[%s] = ', i)
185 end
186 if type(t) == 'table' then
187 l = l .. M.table_to_text (t)
188 elseif type(t) == 'string' then
189 l = l .. string.format ('%q', t)
190 else
191 l = l .. tostring(t)
192 end
193 r[#r+1] = l
194 end
195 return '{ ' .. table.concat (r,', ') .. ' }'
196 end
197
198 -- 1 allocation functions
199
200 -- like \unset
201 M.unset = -0x7FFFFFFF
202
203 -- works for both \chardef and the likes of \countdef
204 function M.registernumber(csname)
205 if token.is_defined(csname) then
206 return token.create(csname).index
207 else
208 return -- would return 0 otherwise
209 end
210 end
211
212 -- we need remember lua-made allocations in the format file, since otherwise,
213 -- a different register will be reserved when the lua code is seen again in the
214 -- actual run.
215 local allocations = M.saved_table('minim lua-side allocations')
216
217 local function make_alloc_new(fname, globcount)
218 allocations[fname] = allocations[fname] or { }
219 M['new_'..fname] = function(id)
220 local nr
221 if id and allocations[fname][id] then
222 nr = allocations[fname][id]
223 else
224 nr = tex.count[globcount] + 1
225 tex.setcount('global', globcount, nr)
226 if id then allocations[fname][id] = nr end
227 M.log('\\%s%d : %s', fname, nr, id or '<unnamed>')
228 end
229 return nr
230 end
231 end
232
233 make_alloc_new ('attribute' , 'e@alloc@attribute@count' )
234 make_alloc_new ('whatsit' , 'e@alloc@whatsit@count' )
235 make_alloc_new ('luabytecode' , 'e@alloc@bytecode@count' )
236 make_alloc_new ('function' , 'e@alloc@luafunction@count' )
237 make_alloc_new ('luachunkname' , 'e@alloc@luachunk@count' )
238 make_alloc_new ('catcodetable' , 'e@alloc@ccodetable@count' )
239 make_alloc_new ('userrule' , 'e@alloc@rule@count' )
240
241 -- We need different allocation functions for the older registers, because
242 -- etex’s global allocation macros are off-by-one w.r.t. all other.
243 --
244 local function make_alloc_old(fname, globcount)
245 allocations[fname] = allocations[fname] or { }
246 M['new_'..fname] = function (id)
247 local nr
248 if id and allocations[fname][id] then
249 nr = allocations[fname][id]
250 else
251 nr = tex.count[globcount]
252 tex.setcount('global', globcount, nr + 1)
253 if id then allocations[fname][id] = nr end
254 M.log('\\%s%d : %s', fname, nr, id or '<unnamed>')
255 end
256 return nr
257 end
258 end
259
260 -- existing allocation counters
261 local globoffset = tex.count['alloc:globoffset']
262 make_alloc_old ('count', globoffset + 0 )
263 make_alloc_old ('dimen', globoffset + 1 )
264 make_alloc_old ('skip', globoffset + 2 )
265 make_alloc_old ('muskip', globoffset + 3 )
266 make_alloc_old ('box', globoffset + 4 )
267 make_alloc_old ('toks', globoffset + 5 )
268 make_alloc_old ('marks', globoffset + 6 )
269
270 -- 1 other extensions
271
272 local cb = require('minim-callbacks')
273 M.remember('minim-callbacks')
274 M.remember('minim-alloc')
275
276 -- Add a callback just before a box is shipped out, opposite to 'finish_pdfpage'.
277 cb.new_callback('pre_shipout', 'simple')
278 M.primitive_shipout = tex.shipout
279 function tex.shipout(nr)
280 cb.call_callback('pre_shipout', nr)
281 M.primitive_shipout(nr)
282 end
283
284 -- define tex commands from lua
285 function M.luadef (csname, fn, ...)
286 local nr = M.new_function(csname)
287 lua.get_functions_table()[nr] = fn
288 token.set_lua(csname, nr, ...)
289 end
290
291 -- the current file
292 M.luadef('minim:currentfile', function()
293 tex.sprint((status.filename:gsub('^.*/', '')))
294 end)
295
296 -- make pdf_string() available as \pdfstring{...}
297 M.luadef('pdfstring', function() tex.sprint(M.pdf_string(token.scan_string())) end)
298
299 -- uselanguage hook callback
300 cb.new_callback('uselanguage', 'simple')
301 M.luadef('minim:uselanguagecallback', function()
302 local langname = token.scan_string()
303 cb.call_callback('uselanguage', langname)
304 end)
305
306 -- copy of \Ucharcat from xetex
307 M.luadef('Ucharcat', function()
308 local chr, cat = token.scan_int(), token.scan_int()
309 token.put_next(token.new(chr, cat))
310 end)
311
312 -- 1 dumping information to the format file
313
314 -- reserve a bytecode register
315 local saved_tables_bytecode = M.new_luabytecode('saved_tables_bytecode')
316
317 -- we cannot use set_lua because lua functions are not included in the format file
318 token.set_macro('minim:restoremodules', '\\luabytecodecall'..saved_tables_bytecode)
319
320 local function dump_saved_tables()
321 M.msg('pre_dump: save modules and tables to format file')
322 -- save modules
323 for _, name in ipairs (modules) do
324 if not modules[name] then
325 M.msg('saving module '..name)
326 -- reserve (if necessary) a bytecode register
327 modules[name] = M.new_luabytecode ('module '..name)
328 -- store the file into the format file
329 lua.bytecode[modules[name]] = loadfile(kpse.find_file(name,'lua'))
330 end
331 end
332 -- save tables (and restore modules)
333 local saved_tables = [[
334
335 -- include all saved tables in this bytecode register
336 local t = ]]..M.table_to_text(tables)..[[
337
338 -- and make them available via require()
339 package.loaded['minim-saved-tables'] = t
340
341 -- restore all remembered modules from their saved bytecode
342 local s = t['minim:modules']
343 for _, name in ipairs (s) do
344 texio.write_nl('log', 'minim: restoring module '..name)
345 package.loaded[name] = lua.bytecode[ s[name] ] ()
346 end
347
348 ]]
349 lua.bytecode[saved_tables_bytecode] = load(saved_tables)
350 end
351
352 cb.register ('pre_dump', dump_saved_tables)
353
354 --
355
356 return M
357