driv-ini.lua /size: 6538 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
driv-ini
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to driv-ini.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 9
local
type
=
type
10
local
addsuffix
=
file
.
addsuffix
11 12
local
setmetatableindex
=
table
.
setmetatableindex
13
local
formatters
=
string
.
formatters
14 15
local
starttiming
=
statistics
.
starttiming
16
local
stoptiming
=
statistics
.
stoptiming
17 18
local
report
=
logs
.
reporter
(
"
drivers
"
)
19 20
local
instances
=
{
}
21
local
helpers
=
{
}
22
local
converters
=
{
}
23
local
prepared
=
{
}
24
local
wrappedup
=
{
}
25
local
cleanedup
=
{
}
26
local
currentdriver
=
"
default
"
27
local
currentinstance
=
nil
28 29
local
shipout
=
tex
.
shipout
30
local
texgetbox
=
tex
.
getbox
31
local
texgetcount
=
tex
.
getcount
32 33
function
converters
.
engine
(
driver
,
boxnumber
,
mode
,
number
,
specification
)
34
return
shipout
(
boxnumber
)
35
end
36 37
local
prepare
=
nil
38
local
convert
=
nil
39
local
wrapup
=
nil
40
local
cleanup
=
nil
41
local
outputfilename
=
nil
42 43
drivers
=
drivers
or
{
44
instances
=
instances
,
45
helpers
=
helpers
,
46
converters
=
converters
,
47
lmtxversion
=
0
.
10
,
48
report
=
report
,
49
}
50 51
local
dummy
=
function
(
)
end
52 53
local
defaulthandlers
=
{
54
prepare
=
dummy
,
55
initialize
=
dummy
,
56
finalize
=
dummy
,
57
updatefontstate
=
dummy
,
58
wrapup
=
dummy
,
59
cleanup
=
dummy
,
60
convert
=
dummy
,
61
outputfilename
=
dummy
,
62
}
63 64
function
drivers
.
install
(
specification
)
65
local
name
=
specification
.
name
66
if
not
name
then
67
report
(
"
missing driver name
"
)
68
return
69
end
70
local
actions
=
specification
.
actions
71
if
not
actions
then
72
report
(
"
no actions for driver %a
"
,
name
)
73
return
74
end
75
local
flushers
=
specification
.
flushers
76
if
not
flushers
then
77
report
(
"
no flushers for driver %a
"
,
name
)
78
return
79
end
80
-- report("driver %a is installed",name)
81
setmetatableindex
(
actions
,
defaulthandlers
)
82
setmetatableindex
(
flushers
,
function
(
)
return
dummy
end
)
83
instances
[
name
]
=
specification
84
end
85 86
function
drivers
.
convert
(
boxnumber
)
87
if
currentinstance
then
88
callbacks
.
functions
.
start_page_number
(
)
89
starttiming
(
drivers
)
90
convert
(
currentinstance
,
boxnumber
,
texgetcount
(
"
realpageno
"
)
)
91
stoptiming
(
drivers
)
92
callbacks
.
functions
.
stop_page_number
(
)
93
end
94
end
95 96
function
drivers
.
outputfilename
(
)
97
if
currentinstance
then
98
return
outputfilename
(
currentinstance
)
99
end
100
end
101 102
luatex
.
wrapup
(
function
(
)
103
if
currentinstance
and
wrapup
and
not
wrappedup
[
currentdriver
]
then
104
starttiming
(
drivers
)
105
wrapup
(
currentinstance
)
106
stoptiming
(
drivers
)
107
wrappedup
[
currentdriver
]
=
true
108
cleanedup
[
currentdriver
]
=
true
109
end
110
end
)
111 112
luatex
.
cleanup
(
function
(
)
113
if
currentinstance
and
cleanup
and
not
cleanedup
[
currentdriver
]
then
114
starttiming
(
drivers
)
115
cleanup
(
currentinstance
)
116
stoptiming
(
drivers
)
117
wrappedup
[
currentdriver
]
=
true
118
cleanedup
[
currentdriver
]
=
true
119
end
120
end
)
121 122
function
drivers
.
enable
(
name
)
123
if
name
~
=
currentdriver
then
124
if
currentinstance
then
125
starttiming
(
drivers
)
126
cleanup
(
currentinstance
)
127
stoptiming
(
drivers
)
128
end
129
currentdriver
=
name
or
"
default
"
130
currentinstance
=
instances
[
currentdriver
]
131
if
currentinstance
then
132
local
actions
=
currentinstance
.
actions
133
prepare
=
actions
.
prepare
134
wrapup
=
actions
.
wrapup
135
cleanup
=
actions
.
cleanup
136
convert
=
actions
.
convert
137
outputfilename
=
actions
.
outputfilename
138
--
139
if
prepare
and
not
prepared
[
currentdriver
]
then
140
starttiming
(
drivers
)
141
prepare
(
currentinstance
)
142
stoptiming
(
drivers
)
143
prepared
[
currentdriver
]
=
true
144
end
145
else
146
report
(
"
bad driver
"
)
147
end
148
end
149
end
150 151
statistics
.
register
(
"
driver time
"
,
function
(
)
152
return
statistics
.
elapsedseconds
(
drivers
)
153
end
)
154 155
interfaces
.
implement
{
156
name
=
"
shipoutpage
"
,
157
arguments
=
"
integer
"
,
158
actions
=
drivers
.
convert
,
159
}
160 161
interfaces
.
implement
{
162
name
=
"
enabledriver
"
,
163
arguments
=
"
string
"
,
164
actions
=
drivers
.
enable
,
165
}
166 167
-- The default driver:
168 169
do
170 171
local
filename
=
nil
172 173
drivers
.
install
{
174
name
=
"
default
"
,
175
actions
=
{
176
convert
=
drivers
.
converters
.
engine
,
177
outputfilename
=
function
(
driver
)
178
if
not
filename
then
179
filename
=
addsuffix
(
tex
.
jobname
,
"
pdf
"
)
180
end
181
return
filename
182
end
,
183
}
,
184
flushers
=
{
185
-- we always need this entry
186
}
,
187
}
188 189
end
190 191
-- No driver:
192 193
do
194 195
drivers
.
install
{
196
name
=
"
none
"
,
197
actions
=
{
}
,
198
flushers
=
{
}
,
199
}
200 201
end
202 203
do
204 205
local
function
prepare
(
driver
)
206
converter
=
drivers
.
converters
.
lmtx
207
end
208 209
local
function
convert
(
driver
,
boxnumber
,
pagenumber
)
210
converter
(
driver
,
texgetbox
(
boxnumber
)
,
"
page
"
,
pagenumber
)
211
end
212 213
drivers
.
install
{
214
name
=
"
empty
"
,
215
actions
=
{
216
prepare
=
prepare
,
217
convert
=
convert
,
218
}
,
219
flushers
=
{
}
,
220
}
221 222
end
223 224
--
225 226
setmetatableindex
(
instances
,
function
(
)
return
instances
.
default
end
)
227 228
-- for now:
229 230
drivers
.
enable
(
"
default
"
)
231 232
-- helpers
233 234
local
s_matrix_0
=
"
1 0 0 1
"
235
local
f_matrix_2
=
formatters
[
"
%.6N 0 0 %.6N
"
]
236
local
f_matrix_4
=
formatters
[
"
%.6N %.6N %.6N %.6N
"
]
237 238
function
helpers
.
tomatrix
(
rx
,
sx
,
sy
,
ry
,
tx
,
ty
)
-- todo: tx ty
239
if
type
(
rx
)
=
=
"
string
"
then
240
return
rx
241
else
242
if
not
rx
then
243
rx
=
1
244
elseif
rx
=
=
0
then
245
rx
=
0
.
0001
246
end
247
if
not
ry
then
248
ry
=
1
249
elseif
ry
=
=
0
then
250
ry
=
0
.
0001
251
end
252
if
not
sx
then
253
sx
=
0
254
end
255
if
not
sy
then
256
sy
=
0
257
end
258
if
sx
=
=
0
and
sy
=
=
0
then
259
if
rx
=
=
1
and
ry
=
=
1
then
260
return
s_matrix_0
261
else
262
return
f_matrix_2
(
rx
,
ry
)
263
end
264
else
265
return
f_matrix_4
(
rx
,
sx
,
sy
,
ry
)
266
end
267
end
268
end
269