mtx-dvi.lua /size: 10 Kb    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
mtx-dvi
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to mtxrun.lua
"
,
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
-- This is just a tool that I use for checking dvi issues in LuaTeX and it has
10
-- no real use otherwise. When needed (or on request) I can extend this script.
11
-- Speed is hardly an issue and I didn't spend much time on generalizing the
12
-- code either.
13 14
local
formatters
=
string
.
formatters
15
local
byte
=
string
.
byte
16 17
local
P
,
R
,
S
,
C
,
Cc
,
Ct
,
Cmt
=
lpeg
.
P
,
lpeg
.
R
,
lpeg
.
S
,
lpeg
.
C
,
lpeg
.
Cc
,
lpeg
.
Ct
,
lpeg
.
Cmt
18
local
lpegmatch
=
lpeg
.
match
19 20
local
readbyte
=
utilities
.
files
.
readbyte
21
local
readcardinal1
=
utilities
.
files
.
readcardinal1
22
local
readcardinal2
=
utilities
.
files
.
readcardinal2
23
local
readcardinal3
=
utilities
.
files
.
readcardinal3
24
local
readcardinal4
=
utilities
.
files
.
readcardinal4
25
local
readinteger1
=
utilities
.
files
.
readinteger1
26
local
readinteger2
=
utilities
.
files
.
readinteger2
27
local
readinteger3
=
utilities
.
files
.
readinteger3
28
local
readinteger4
=
utilities
.
files
.
readinteger4
29
local
readstring
=
utilities
.
files
.
readstring
30 31
local
helpinfo
=
[[
32<?xml version="1.0"?> 33<application> 34 <metadata> 35 <entry name="name">mtx-dvi</entry> 36 <entry name="detail">ConTeXt DVI Helpers</entry> 37 <entry name="version">0.01</entry> 38 </metadata> 39 <flags> 40 <category name="basic"> 41 <subcategory> 42 <flag name="list"><short>list dvi commands</short></flag> 43 </subcategory> 44 </category> 45 </flags> 46</application> 47
]]
48 49
local
application
=
logs
.
application
{
50
name
=
"
mtx-dvi
"
,
51
banner
=
"
ConTeXt DVI Helpers 0.10
"
,
52
helpinfo
=
helpinfo
,
53
}
54 55
local
report
=
application
.
report
56 57
local
f_set_char_fast
=
formatters
[
"
set char fast %C
"
]
58
local
f_set_char
=
formatters
[
"
set char %C
"
]
59
local
f_set_rule
=
formatters
[
"
set rule height=%p width=%p (%s %s)
"
]
60
local
f_put_char
=
formatters
[
"
put char %C
"
]
61
local
f_put_rule
=
formatters
[
"
put rule height=%p width=%p (%s %s)
"
]
62
local
f_set_font
=
formatters
[
"
set font %i
"
]
63
local
f_set_font_fast
=
formatters
[
"
set font fast %i
"
]
64
local
f_right
=
formatters
[
"
right %p (%s)
"
]
65
local
f_right_w
=
formatters
[
"
right w
"
]
66
local
f_right_x
=
formatters
[
"
right x
"
]
67
local
f_right_w_set
=
formatters
[
"
right set w %p (%s)
"
]
68
local
f_right_x_set
=
formatters
[
"
right set x %p (%s)
"
]
69
local
f_down
=
formatters
[
"
down %p (%s)
"
]
70
local
f_down_y
=
formatters
[
"
down y
"
]
71
local
f_down_z
=
formatters
[
"
down z
"
]
72
local
f_down_y_set
=
formatters
[
"
down set y %p (%s)
"
]
73
local
f_down_z_set
=
formatters
[
"
down set z %p (%s)
"
]
74
local
f_page_begin
=
formatters
[
"
page begin (% t) %i
"
]
75
local
f_page_end
=
formatters
[
"
page end
"
]
76
local
f_nop
=
formatters
[
"
nop
"
]
77
local
f_push
=
formatters
[
"
push %i
"
]
78
local
f_pop
=
formatters
[
"
pop %i
"
]
79
local
f_special
=
formatters
[
"
special %s
"
]
80
local
f_preamble
=
formatters
[
"
preamble version=%s numerator=%s denominator=%s mag=%s comment=%s
"
]
81
local
f_postamble_begin
=
formatters
[
"
postamble
"
]
82
local
f_postamble_end
=
formatters
[
"
postamble end offset=%s version=%s
"
]
83
local
f_define_font
=
formatters
[
"
define font k=%i checksum=%i scale=%p designsize=%p area=%s name=%s
"
]
84 85
local
currentdepth
=
0
86
local
usedprinter
=
(
logs
and
logs
.
writer
)
or
(
texio
and
texio
.
write_nl
)
or
print
87 88
local
handler
=
{
}
for
i
=
0
,
255
do
handler
[
i
]
=
false
end
89 90
local
function
define_font
(
f
,
size
)
91
local
k
=
size
=
=
1
and
readcardinal1
(
f
)
92
or
size
=
=
2
and
readcardinal2
(
f
)
93
or
size
=
=
3
and
readcardinal3
(
f
)
94
or
readcardinal4
(
f
)
95
local
c
=
readcardinal4
(
f
)
96
local
s
=
readcardinal4
(
f
)
97
local
d
=
readcardinal4
(
f
)
98
local
a
=
readcardinal1
(
f
)
99
local
l
=
readcardinal1
(
f
)
100
local
a
=
readstring
(
f
,
a
)
101
local
l
=
readstring
(
f
,
l
)
102
usedprinter
(
f_define_font
(
k
,
c
,
s
,
d
,
area
,
name
)
)
103
end
104 105
handler
[
000
]
=
function
(
f
,
b
)
106
usedprinter
(
f_set_char_fast
(
b
)
)
107
end
108 109
handler
[
128
]
=
function
(
f
)
110
usedprinter
(
f_set_char
(
readcardinal1
(
f
)
)
)
111
end
112
handler
[
129
]
=
function
(
f
)
113
usedprinter
(
f_set_char
(
readcardinal2
(
f
)
)
)
114
end
115
handler
[
130
]
=
function
(
f
)
116
usedprinter
(
f_set_char
(
readcardinal3
(
f
)
)
)
117
end
118
handler
[
131
]
=
function
(
f
)
119
usedprinter
(
f_set_char
(
readcardinal4
(
f
)
)
)
120
end
121 122
handler
[
132
]
=
function
(
f
)
123
usedprinter
(
f_set_rule
(
readinteger4
(
f
)
,
readinteger4
(
f
)
)
)
124
end
125 126
handler
[
133
]
=
function
(
f
)
127
usedprinter
(
f_put_char
(
readcardinal1
(
f
)
)
)
128
end
129
handler
[
134
]
=
function
(
f
)
130
usedprinter
(
f_put_char
(
readcardinal2
(
f
)
)
)
131
end
132
handler
[
135
]
=
function
(
f
)
133
usedprinter
(
f_put_char
(
readcardinal3
(
f
)
)
)
134
end
135
handler
[
136
]
=
function
(
f
)
136
usedprinter
(
f_put_char
(
readcardinal4
(
f
)
)
)
137
end
138 139
handler
[
137
]
=
function
(
f
)
140
usedprinter
(
f_put_rule
(
readinteger4
(
f
)
,
readinteger4
(
f
)
)
)
141
end
142 143
handler
[
138
]
=
function
(
f
)
144
usedprinter
(
f_nop
(
)
)
145
end
146 147
handler
[
139
]
=
function
(
f
)
148
local
pages
=
{
}
149
for
i
=
0
,
9
do
150
pages
[
i
]
=
readinteger4
(
f
)
151
end
152
usedprinter
(
f_page_begin
(
pages
,
readinteger4
(
f
)
)
)
153
end
154
handler
[
140
]
=
function
(
)
155
usedprinter
(
f_page_end
(
)
)
156
end
157 158
handler
[
141
]
=
function
(
)
159
currentdepth
=
currentdepth
+
1
160
usedprinter
(
f_push
(
currentdepth
)
)
161
end
162
handler
[
142
]
=
function
(
)
163
usedprinter
(
f_pop
(
currentdepth
)
)
164
currentdepth
=
currentdepth
-
1
165
end
166 167
handler
[
143
]
=
function
(
f
)
168
local
d
=
readinteger1
(
f
)
169
usedprinter
(
f_right
(
d
,
d
)
)
170
end
171
handler
[
144
]
=
function
(
f
)
172
local
d
=
readinteger2
(
f
)
173
usedprinter
(
f_right
(
d
,
d
)
)
174
end
175
handler
[
145
]
=
function
(
f
)
176
local
d
=
readinteger3
(
f
)
177
usedprinter
(
f_right
(
d
,
d
)
)
178
end
179
handler
[
146
]
=
function
(
f
)
180
local
d
=
readinteger4
(
f
)
181
usedprinter
(
f_right
(
d
,
d
)
)
182
end
183 184
handler
[
147
]
=
function
(
)
185
usedprinter
(
f_right_w
(
)
)
186
end
187 188
handler
[
148
]
=
function
(
f
)
189
local
d
=
readinteger1
(
f
)
190
usedprinter
(
f_right_w_set
(
d
,
d
)
)
191
end
192
handler
[
149
]
=
function
(
f
)
193
local
d
=
readinteger2
(
f
)
194
usedprinter
(
f_right_w_set
(
d
,
d
)
)
195
end
196
handler
[
150
]
=
function
(
f
)
197
local
d
=
readinteger3
(
f
)
198
usedprinter
(
f_right_w_set
(
d
,
d
)
)
199
end
200
handler
[
151
]
=
function
(
f
)
201
local
d
=
readinteger4
(
f
)
202
usedprinter
(
f_right_w_set
(
d
,
d
)
)
203
end
204 205
handler
[
152
]
=
function
(
)
206
handlers
.
right_x
(
)
207
end
208 209
handler
[
153
]
=
function
(
f
)
210
local
d
=
readinteger1
(
f
)
211
usedprinter
(
f_right_x_set
(
d
,
d
)
)
212
end
213
handler
[
154
]
=
function
(
f
)
214
local
d
=
readinteger2
(
f
)
215
usedprinter
(
f_right_x_set
(
d
,
d
)
)
216
end
217
handler
[
155
]
=
function
(
f
)
218
local
d
=
readinteger3
(
f
)
219
usedprinter
(
f_right_x_set
(
d
,
d
)
)
220
end
221
handler
[
156
]
=
function
(
f
)
222
local
d
=
readinteger4
(
f
)
223
usedprinter
(
f_right_x_set
(
d
,
d
)
)
224
end
225 226
handler
[
157
]
=
function
(
f
)
227
local
d
=
readinteger1
(
f
)
228
usedprinter
(
f_down
(
d
,
d
)
)
229
end
230 231
handler
[
158
]
=
function
(
f
)
232
local
d
=
readinteger2
(
f
)
233
usedprinter
(
f_down
(
d
,
d
)
)
234
end
235
handler
[
159
]
=
function
(
f
)
236
local
d
=
readinteger3
(
f
)
237
usedprinter
(
f_down
(
d
,
d
)
)
238
end
239
handler
[
160
]
=
function
(
f
)
240
local
d
=
readinteger4
(
f
)
241
usedprinter
(
f_down
(
d
,
d
)
)
242
end
243
handler
[
161
]
=
function
(
)
244
usedprinter
(
f_down_y
(
)
)
245
end
246 247
handler
[
162
]
=
function
(
f
)
248
local
d
=
readinteger1
(
f
)
249
usedprinter
(
f_down_y_set
(
d
,
d
)
)
250
end
251
handler
[
163
]
=
function
(
f
)
252
local
d
=
readinteger2
(
f
)
253
usedprinter
(
f_down_y_set
(
d
,
d
)
)
254
end
255
handler
[
164
]
=
function
(
f
)
256
local
d
=
readinteger3
(
f
)
257
usedprinter
(
f_down_y_set
(
d
,
d
)
)
258
end
259
handler
[
165
]
=
function
(
f
)
260
local
d
=
readinteger4
(
f
)
261
usedprinter
(
f_down_y_set
(
d
,
d
)
)
262
end
263 264
handler
[
166
]
=
function
(
)
265
handlers
.
down_z
(
)
266
end
267 268
handler
[
167
]
=
function
(
f
)
269
local
d
=
readinteger1
(
f
)
270
usedprinter
(
f_down_z_set
(
d
,
d
)
)
271
end
272
handler
[
168
]
=
function
(
f
)
273
local
d
=
readinteger2
(
f
)
274
usedprinter
(
f_down_z_set
(
d
,
d
)
)
275
end
276
handler
[
169
]
=
function
(
f
)
277
local
d
=
readinteger3
(
f
)
278
usedprinter
(
f_down_z_set
(
d
,
d
)
)
279
end
280
handler
[
170
]
=
function
(
f
)
281
local
d
=
readinteger4
(
f
)
282
usedprinter
(
f_down_z_set
(
d
,
d
)
)
283
end
284 285
handler
[
171
]
=
function
(
f
,
b
)
286
usedprinter
(
f_set_font_fast
(
b
)
)
287
end
288 289
handler
[
235
]
=
function
(
f
)
290
usedprinter
(
f_set_font
(
readcardinal1
(
f
)
)
)
291
end
292
handler
[
236
]
=
function
(
f
)
293
usedprinter
(
f_set_font
(
readcardinal2
(
f
)
)
)
294
end
295
handler
[
237
]
=
function
(
f
)
296
usedprinter
(
f_set_font
(
readcardinal3
(
f
)
)
)
297
end
298
handler
[
238
]
=
function
(
f
)
299
usedprinter
(
f_set_font
(
readcardinal4
(
f
)
)
)
300
end
301 302
handler
[
239
]
=
function
(
f
)
303
usedprinter
(
f_special
(
readstring
(
readcardinal1
(
f
)
)
)
)
304
end
305
handler
[
240
]
=
function
(
f
)
306
usedprinter
(
f_special
(
readstring
(
readcardinal2
(
f
)
)
)
)
307
end
308
handler
[
241
]
=
function
(
f
)
309
usedprinter
(
f_special
(
readstring
(
readcardinal3
(
f
)
)
)
)
310
end
311
handler
[
242
]
=
function
(
f
)
312
usedprinter
(
f_special
(
readstring
(
readcardinal4
(
f
)
)
)
)
313
end
314 315
handler
[
243
]
=
function
(
f
)
316
define_font
(
f
,
1
)
317
end
318
handler
[
244
]
=
function
(
f
)
319
define_font
(
f
,
2
)
320
end
321
handler
[
245
]
=
function
(
f
)
322
define_font
(
f
,
3
)
323
end
324
handler
[
246
]
=
function
(
f
)
325
define_font
(
f
,
4
)
326
end
327 328
handler
[
247
]
=
function
(
f
)
329
usedprinter
(
f_preamble
(
330
readcardinal1
(
f
)
,
331
readcardinal4
(
f
)
,
332
readcardinal4
(
f
)
,
333
readcardinal4
(
f
)
,
334
readstring
(
f
,
readcardinal1
(
f
)
)
335
)
)
336
end
337 338
handler
[
248
]
=
function
(
f
)
339
usedprinter
(
f_postamble_begin
(
340
readcardinal4
(
f
)
,
-- p
341
readcardinal4
(
f
)
,
-- num
342
readcardinal4
(
f
)
,
-- den
343
readcardinal4
(
f
)
,
-- mag
344
readcardinal4
(
f
)
,
-- l
345
readcardinal4
(
f
)
,
-- u
346
readcardinal2
(
f
)
,
-- s
347
readcardinal2
(
f
)
-- t
348
)
)
349
while
true
do
350
local
b
=
readbyte
(
f
)
351
if
b
=
=
249
then
352
break
353
else
354
handler
[
b
]
(
f
,
b
)
355
end
356
end
357
usedprinter
(
f_postamble_end
(
358
readcardinal4
(
f
)
,
359
readcardinal1
(
f
)
360
)
)
361
-- now 223's follow
362
end
363 364
handler
[
250
]
=
function
(
)
365
end
366 367
for
i
=
1
,
127
do
handler
[
i
]
=
handler
[
0
]
end
368
for
i
=
172
,
234
do
handler
[
i
]
=
handler
[
171
]
end
369
for
i
=
251
,
255
do
handler
[
i
]
=
handler
[
250
]
end
370 371
scripts
=
scripts
or
{
}
372
scripts
.
dvi
=
scripts
.
dvi
or
{
}
373 374
function
scripts
.
dvi
.
list
(
filename
,
printer
)
375
currentdepth
=
0
376
local
f
=
io
.
open
(
filename
)
377
if
f
then
378
local
filesize
=
f
:
seek
(
"
end
"
)
379
local
position
=
0
380
f
:
seek
(
"
set
"
,
position
)
381
local
format
=
formatters
[
"
%0
"
.
.
#
tostring
(
filesize
)
.
.
"
i : %s
"
]
382
local
flush
=
printer
or
usedprinter
383
usedprinter
=
function
(
str
)
384
flush
(
format
(
position
,
str
)
)
385
position
=
f
:
seek
(
)
386
end
387
while
true
do
388
local
b
=
readbyte
(
f
)
389
if
b
=
=
223
then
390
return
391
else
392
handler
[
b
]
(
f
,
b
)
393
end
394
end
395
f
:
close
(
)
396
else
397
report
(
"
invalid filename %a
"
,
filename
)
398
end
399
end
400 401
local
filename
=
environment
.
files
[
1
]
or
"
"
402 403
if
filename
=
=
"
"
then
404
application
.
help
(
)
405
elseif
environment
.
argument
(
"
list
"
)
then
406
scripts
.
dvi
.
list
(
filename
)
407
elseif
environment
.
argument
(
"
exporthelp
"
)
then
408
application
.
export
(
environment
.
argument
(
"
exporthelp
"
)
,
filename
)
409
else
410
application
.
help
(
)
411
end
412