summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/context/lua/mtx-dvi.lua
blob: 57f146ee659548307fb4cbc24946e71d7151b8f8 (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
if not modules then modules = { } end modules ['mtx-dvi'] = {
    version   = 1.001,
    comment   = "companion to mtxrun.lua",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- This is just a tool that I use for checking dvi issues in LuaTeX and it has
-- no real use otherwise. When needed (or on request) I can extend this script.
-- Speed is hardly an issue and I didn't spend much time on generalizing the
-- code either.

local formatters = string.formatters
local byte = string.byte

local P, R, S, C, Cc, Ct, Cmt = lpeg.P, lpeg.R, lpeg.S, lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt
local lpegmatch = lpeg.match

local readbyte      = utilities.files.readbyte
local readcardinal1 = utilities.files.readcardinal1
local readcardinal2 = utilities.files.readcardinal2
local readcardinal3 = utilities.files.readcardinal3
local readcardinal4 = utilities.files.readcardinal4
local readinteger1  = utilities.files.readinteger1
local readinteger2  = utilities.files.readinteger2
local readinteger3  = utilities.files.readinteger3
local readinteger4  = utilities.files.readinteger4
local readstring    = utilities.files.readstring

local helpinfo = [[
<?xml version="1.0"?>
<application>
 <metadata>
  <entry name="name">mtx-dvi</entry>
  <entry name="detail">ConTeXt DVI Helpers</entry>
  <entry name="version">0.01</entry>
 </metadata>
 <flags>
  <category name="basic">
   <subcategory>
    <flag name="list"><short>list dvi commands</short></flag>
   </subcategory>
  </category>
 </flags>
</application>
]]

local application = logs.application {
    name     = "mtx-dvi",
    banner   = "ConTeXt DVI Helpers 0.10",
    helpinfo = helpinfo,
}

local report = application.report

local f_set_char_fast   = formatters["set char fast    %C"]
local f_set_char        = formatters["set char         %C"]
local f_set_rule        = formatters["set rule         height=%p width=%p (%s %s)"]
local f_put_char        = formatters["put char         %C"]
local f_put_rule        = formatters["put rule         height=%p width=%p (%s %s)"]
local f_set_font        = formatters["set font         %i"]
local f_set_font_fast   = formatters["set font fast    %i"]
local f_right           = formatters["right            %p (%s)"]
local f_right_w         = formatters["right            w"]
local f_right_x         = formatters["right            x"]
local f_right_w_set     = formatters["right set        w %p (%s)"]
local f_right_x_set     = formatters["right set        x %p (%s)"]
local f_down            = formatters["down             %p (%s)"]
local f_down_y          = formatters["down             y"]
local f_down_z          = formatters["down             z"]
local f_down_y_set      = formatters["down set         y %p (%s)"]
local f_down_z_set      = formatters["down set         z %p (%s)"]
local f_page_begin      = formatters["page begin       (% t) %i"]
local f_page_end        = formatters["page end"]
local f_nop             = formatters["nop"]
local f_push            = formatters["push             %i"]
local f_pop             = formatters["pop              %i"]
local f_special         = formatters["special          %s"]
local f_preamble        = formatters["preamble         version=%s numerator=%s denominator=%s mag=%s comment=%s"]
local f_postamble_begin = formatters["postamble"]
local f_postamble_end   = formatters["postamble end    offset=%s version=%s"]
local f_define_font     = formatters["define font      k=%i checksum=%i scale=%p designsize=%p area=%s name=%s"]

local currentdepth = 0
local usedprinter  = texio and texio.write_ln or print

local handler = { } for i=0,255 do handler[i] = false end

local function define_font(f,size)
    local k = size == 1 and readcardinal1(f)
           or size == 2 and readcardinal2(f)
           or size == 3 and readcardinal3(f)
           or               readcardinal4(f)
    local c = readcardinal4(f)
    local s = readcardinal4(f)
    local d = readcardinal4(f)
    local a = readcardinal1(f)
    local l = readcardinal1(f)
    local a = readstring(f,a)
    local l = readstring(f,l)
    usedprinter(f_define_font(k,c,s,d,area,name))
end

handler[000] = function(f,b)
    usedprinter(f_set_char_fast(b))
end

handler[128] = function(f)
    usedprinter(f_set_char(readcardinal1(f)))
end
handler[129] = function(f)
    usedprinter(f_set_char(readcardinal2(f)))
end
handler[130] = function(f)
    usedprinter(f_set_char(readcardinal3(f)))
end
handler[131] = function(f)
    usedprinter(f_set_char(readcardinal4(f)))
end

handler[132] = function(f)
    usedprinter(f_set_rule(readinteger4(f),readinteger4(f)))
end

handler[133] = function(f)
    usedprinter(f_put_char(readcardinal1(f)))
end
handler[134] = function(f)
    usedprinter(f_put_char(readcardinal2(f)))
end
handler[135] = function(f)
    usedprinter(f_put_char(readcardinal3(f)))
end
handler[136] = function(f)
    usedprinter(f_put_char(readcardinal4(f)))
end

handler[137] = function(f)
    usedprinter(f_put_rule(readinteger4(f),readinteger4(f)))
end

handler[138] = function(f)
    usedprinter(f_nop())
end

handler[139] = function(f)
    local pages = { }
    for i=0,9 do
        pages[i] = readinteger4(f)
    end
    usedprinter(f_page_begin(pages,readinteger4(f)))
end
handler[140] = function()
    usedprinter(f_page_end())
end

handler[141] = function()
    currentdepth = currentdepth + 1
    usedprinter(f_push(currentdepth))
end
handler[142] = function()
    usedprinter(f_pop(currentdepth))
    currentdepth = currentdepth - 1
end

handler[143] = function(f)
    local d = readinteger1(f)
    usedprinter(f_right(d,d))
end
handler[144] = function(f)
    local d = readinteger2(f)
    usedprinter(f_right(d,d))
end
handler[145] = function(f)
    local d = readinteger3(f)
    usedprinter(f_right(d,d))
end
handler[146] = function(f)
    local d = readinteger4(f)
    usedprinter(f_right(d,d))
end

handler[147] = function()
    usedprinter(f_right_w())
end

handler[148] = function(f)
    local d = readinteger1(f)
    usedprinter(f_right_w_set(d,d))
end
handler[149] = function(f)
    local d = readinteger2(f)
    usedprinter(f_right_w_set(d,d))
end
handler[150] = function(f)
    local d = readinteger3(f)
    usedprinter(f_right_w_set(d,d))
end
handler[151] = function(f)
    local d = readinteger4(f)
    usedprinter(f_right_w_set(d,d))
end

handler[152] = function()
    handlers.right_x()
end

handler[153] = function(f)
    local d = readinteger1(f)
    usedprinter(f_right_x_set(d,d))
end
handler[154] = function(f)
    local d = readinteger2(f)
    usedprinter(f_right_x_set(d,d))
end
handler[155] = function(f)
    local d = readinteger3(f)
    usedprinter(f_right_x_set(d,d))
end
handler[156] = function(f)
    local d = readinteger4(f)
    usedprinter(f_right_x_set(d,d))
end

handler[157] = function(f)
    local d = readinteger1(f)
    usedprinter(f_down(d,d))
end

handler[158] = function(f)
    local d = readinteger2(f)
    usedprinter(f_down(d,d))
end
handler[159] = function(f)
    local d = readinteger3(f)
    usedprinter(f_down(d,d))
end
handler[160] = function(f)
    local d = readinteger4(f)
    usedprinter(f_down(d,d))
end
handler[161] = function()
    usedprinter(f_down_y())
end

handler[162] = function(f)
    local d = readinteger1(f)
    usedprinter(f_down_y_set(d,d))
end
handler[163] = function(f)
    local d = readinteger2(f)
    usedprinter(f_down_y_set(d,d))
end
handler[164] = function(f)
    local d = readinteger3(f)
    usedprinter(f_down_y_set(d,d))
end
handler[165] = function(f)
    local d = readinteger4(f)
    usedprinter(f_down_y_set(d,d))
end

handler[166] = function()
    handlers.down_z()
end

handler[167] = function(f)
    local d = readinteger1(f)
    usedprinter(f_down_z_set(d,d))
end
handler[168] = function(f)
    local d = readinteger2(f)
    usedprinter(f_down_z_set(d,d))
end
handler[169] = function(f)
    local d = readinteger3(f)
    usedprinter(f_down_z_set(d,d))
end
handler[170] = function(f)
    local d = readinteger4(f)
    usedprinter(f_down_z_set(d,d))
end

handler[171] = function(f,b)
    usedprinter(f_set_font_fast(b))
end

handler[235] = function(f)
    usedprinter(f_set_font(readcardinal1(f)))
end
handler[236] = function(f)
    usedprinter(f_set_font(readcardinal2(f)))
end
handler[237] = function(f)
    usedprinter(f_set_font(readcardinal3(f)))
end
handler[238] = function(f)
    usedprinter(f_set_font(readcardinal4(f)))
end

handler[239] = function(f)
    usedprinter(f_special(readstring(readcardinal1(f))))
end
handler[240] = function(f)
    usedprinter(f_special(readstring(readcardinal2(f))))
end
handler[241] = function(f)
    usedprinter(f_special(readstring(readcardinal3(f))))
end
handler[242] = function(f)
    usedprinter(f_special(readstring(readcardinal4(f))))
end

handler[243] = function(f)
    define_font(f,1)
end
handler[244] = function(f)
    define_font(f,2)
end
handler[245] = function(f)
    define_font(f,3)
end
handler[246] = function(f)
    define_font(f,4)
end

handler[247] = function(f)
    usedprinter(f_preamble(
        readcardinal1(f),
        readcardinal4(f),
        readcardinal4(f),
        readcardinal4(f),
        readstring(f,readcardinal1(f))
    ))
end

handler[248] = function(f)
    usedprinter(f_postamble_begin(
        readcardinal4(f), -- p
        readcardinal4(f), -- num
        readcardinal4(f), -- den
        readcardinal4(f), -- mag
        readcardinal4(f), -- l
        readcardinal4(f), -- u
        readcardinal2(f), -- s
        readcardinal2(f)  -- t
    ))
    while true do
        local b = readbyte(f)
        if b == 249 then
            break
        else
            handler[b](f,b)
        end
    end
    usedprinter(f_postamble_end(
        readcardinal4(f),
        readcardinal1(f)
    ))
    -- now 223's follow
end

handler[250] = function()
end

for i=   1,127 do handler[i] = handler[  0] end
for i= 172,234 do handler[i] = handler[171] end
for i= 251,255 do handler[i] = handler[250] end

scripts     = scripts     or { }
scripts.dvi = scripts.dvi or { }

function scripts.dvi.list(filename,printer)
    currentdepth = 0
    local f = io.open(filename)
    if f then
        local filesize = f:seek("end")
        local position = 0
        f:seek("set",position)
        local format = formatters["%0" .. #tostring(filesize) .. "i :  %s"]
        local flush  = printer or (texio and texio.write_ln) or print
        usedprinter = function(str)
            flush(format(position,str))
            position = f:seek()
        end
        while true do
            local b = readbyte(f)
            if b == 223 then
                return
            else
                handler[b](f,b)
            end
        end
        f:close()
    else
        report("invalid filename %a",filename)
    end
end

local filename = environment.files[1] or ""

if filename == "" then
    application.help()
elseif environment.argument("list") then
    scripts.dvi.list(filename)
elseif environment.argument("exporthelp") then
    application.export(environment.argument("exporthelp"),filename)
else
    application.help()
end