back-lua.lua /size: 9494 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
back-lua
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to lpdf-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
-- we can remap fonts
10 11
local
fontproperties
=
fonts
.
hashes
.
properties
12
local
fontparameters
=
fonts
.
hashes
.
parameters
13
local
fontshapes
=
fonts
.
hashes
.
shapes
14 15
local
starttiming
=
statistics
.
starttiming
16
local
stoptiming
=
statistics
.
stoptiming
17 18
local
bpfactor
=
number
.
dimenfactors
.
bp
19
local
texgetbox
=
tex
.
getbox
20 21
local
rulecodes
=
nodes
.
rulecodes
22
local
normalrule_code
=
rulecodes
.
normal
23
----- boxrule_code = rulecodes.box
24
----- imagerule_code = rulecodes.image
25
----- emptyrule_code = rulecodes.empty
26
----- userrule_code = rulecodes.user
27
----- overrule_code = rulecodes.over
28
----- underrule_code = rulecodes.under
29
----- fractionrule_code = rulecodes.fraction
30
----- radicalrule_code = rulecodes.radical
31
local
outlinerule_code
=
rulecodes
.
outline
32 33
-- todo : per instance
34 35
local
pages
=
{
}
36
local
fonts
=
{
}
37
local
names
=
{
}
38
local
mapping
=
{
}
39
local
used
=
{
}
40
local
shapes
=
{
}
41
local
glyphs
=
{
}
42
local
buffer
=
{
}
43
local
b
=
0
44
local
converter
=
nil
45 46
local
compact
=
false
-- true
47
local
shapestoo
=
true
48 49
local
x
,
y
,
d
,
f
,
c
,
w
,
h
,
t
,
r
,
o
50 51
local
function
reset
(
)
52
buffer
=
{
}
53
b
=
0
54
t
=
nil
55
x
=
nil
56
y
=
nil
57
d
=
nil
58
f
=
nil
59
c
=
nil
60
w
=
nil
61
h
=
nil
62
r
=
nil
63
o
=
nil
64
end
65 66
local
function
result
(
)
67
-- todo: we're now still in the pdf backend but need different codeinjections
68
local
codeinjections
=
backends
.
pdf
.
codeinjections
69
local
getvariable
=
codeinjections
.
getidentityvariable
or
function
(
)
end
70
local
jobname
=
environment
.
jobname
or
tex
.
jobname
or
"
unknown
"
71
return
{
72
metadata
=
{
73
unit
=
"
bp
"
,
74
jobname
=
jobname
,
75
title
=
getvariable
(
"
title
"
)
or
jobname
,
76
subject
=
getvariable
(
"
subject
"
)
,
77
author
=
getvariable
(
"
author
"
)
,
78
keywords
=
getvariable
(
"
keywords
"
)
,
79
time
=
os
.
date
(
"
%Y-%m-%d %H:%M
"
)
,
80
engine
=
environment
.
luatexengine
.
.
"
"
.
.
environment
.
luatexversion
,
81
context
=
environment
.
version
,
82
}
,
83
fonts
=
fonts
,
84
pages
=
pages
,
85
shapes
=
shapes
,
86
}
87
end
88 89
-- actions
90 91
local
function
outputfilename
(
driver
)
92
return
tex
.
jobname
.
.
"
-output.lua
"
93
end
94 95
local
function
save
(
)
-- might become a driver function that already is plugged into stopactions
96
local
filename
=
outputfilename
(
)
97
drivers
.
report
(
"
saving result in %a
"
,
filename
)
98
starttiming
(
drivers
)
99
local
data
=
result
(
)
100
if
data
then
101
io
.
savedata
(
filename
,
table
.
serialize
(
data
)
)
102
end
103
stoptiming
(
drivers
)
104
end
105 106
local
function
prepare
(
driver
)
107
converter
=
drivers
.
converters
.
lmtx
108
luatex
.
registerstopactions
(
1
,
function
(
)
109
save
(
)
110
end
)
111
end
112 113
local
function
initialize
(
driver
,
details
)
114
reset
(
)
115
end
116 117
local
function
finalize
(
driver
,
details
)
118
local
n
=
details
.
pagenumber
119
local
b
=
details
.
boundingbox
120
pages
[
n
]
=
{
121
number
=
n
,
122
content
=
buffer
,
123
boundingbox
=
{
124
b
[
1
]
*
bpfactor
,
125
b
[
2
]
*
bpfactor
,
126
b
[
3
]
*
bpfactor
,
127
b
[
4
]
*
bpfactor
,
128
}
,
129
}
130
end
131 132
local
function
wrapup
(
driver
)
133
end
134 135
local
function
cleanup
(
driver
)
136
reset
(
)
137
end
138 139
local
function
convert
(
driver
,
boxnumber
,
pagenumber
)
140
converter
(
driver
,
texgetbox
(
boxnumber
)
,
"
page
"
,
pagenumber
)
141
end
142 143
-- flushers
144 145
local
function
updatefontstate
(
id
)
146
if
not
mapping
[
id
]
then
147
local
fn
=
#
fonts
+
1
148
mapping
[
id
]
=
fn
149
local
properties
=
fontproperties
[
id
]
150
local
parameters
=
fontparameters
[
id
]
151
local
filename
=
file
.
basename
(
properties
.
filename
)
152
local
fontname
=
properties
.
fullname
or
properties
.
fontname
153
if
shapestoo
then
154
if
not
names
[
fontname
]
then
155
local
sn
=
#
shapes
+
1
156
names
[
fontname
]
=
sn
157
shapes
[
sn
]
=
{
}
158
glyphs
[
sn
]
=
fontshapes
[
id
]
.
glyphs
159
end
160
end
161
fonts
[
fn
]
=
{
162
filename
=
filename
,
163
name
=
fontname
,
164
size
=
parameters
.
size
*
bpfactor
,
165
shapes
=
shapestoo
and
names
[
fontname
]
or
nil
,
166
}
167
end
168
end
169 170
local
function
flushcharacter
(
current
,
pos_h
,
pos_v
,
pos_r
,
font
,
char
)
171
local
fnt
=
mapping
[
font
]
172
b
=
b
+
1
173
buffer
[
b
]
=
{
174
t
=
"
glyph
"
~
=
t
and
"
glyph
"
or
nil
,
175
f
=
font
~
=
f
and
fnt
or
nil
,
176
c
=
char
~
=
c
and
char
or
nil
,
177
x
=
pos_h
~
=
x
and
(
pos_h
*
bpfactor
)
or
nil
,
178
y
=
pos_v
~
=
y
and
(
pos_v
*
bpfactor
)
or
nil
,
179
d
=
pos_r
~
=
d
and
(
pos_r
=
=
1
and
"
r2l
"
or
"
l2r
"
)
or
nil
,
180
}
181
t
=
"
glyph
"
182
f
=
font
183
c
=
char
184
x
=
pos_h
185
y
=
pos_v
186
d
=
pos_r
187
if
shapestoo
then
188
-- can be sped up if needed
189
local
sn
=
fonts
[
fnt
]
.
shapes
190
local
shp
=
shapes
[
sn
]
191
if
not
shp
[
char
]
then
192
shp
[
char
]
=
glyphs
[
sn
]
[
char
]
193
end
194
end
195
end
196 197
local
function
rule
(
pos_h
,
pos_v
,
pos_r
,
size_h
,
size_v
,
rule_s
,
rule_o
)
198
b
=
b
+
1
199
buffer
[
b
]
=
{
200
t
=
"
rule
"
~
=
t
and
"
rule
"
or
nil
,
201
r
=
rule_s
~
=
r
and
rule_s
or
nil
,
202
o
=
rule_s
=
=
"
outline
"
and
rule_o
~
=
o
and
(
rule_o
*
bpfactor
)
or
nil
,
203
w
=
size_h
~
=
w
and
(
size_h
*
bpfactor
)
or
nil
,
204
h
=
size_v
~
=
h
and
(
size_v
*
bpfactor
)
or
nil
,
205
x
=
pos_h
~
=
x
and
(
pos_h
*
bpfactor
)
or
nil
,
206
y
=
pos_v
~
=
y
and
(
pos_v
*
bpfactor
)
or
nil
,
207
d
=
pos_r
~
=
d
and
(
pos_r
=
=
1
and
"
r2l
"
or
"
l2r
"
)
or
nil
,
208
}
209
t
=
"
rule
"
210
w
=
size_h
211
h
=
size_v
212
x
=
pos_h
213
y
=
pos_v
214
d
=
pos_r
215
end
216 217
local
function
flushrule
(
current
,
pos_h
,
pos_v
,
pos_r
,
size_h
,
size_v
,
subtype
)
218
local
rule_s
,
rule_o
219
if
subtype
=
=
normalrule_code
then
220
rule_s
=
"
normal
"
221
elseif
subtype
=
=
outlinerule_code
then
222
rule_s
=
"
outline
"
223
rule_o
=
getdata
(
current
)
224
else
225
return
226
end
227
return
rule
(
pos_h
,
pos_v
,
pos_r
,
size_h
,
size_v
,
rule_s
,
rule_o
)
228
end
229 230
local
function
flushsimplerule
(
current
,
pos_h
,
pos_v
,
pos_r
,
size_h
,
size_v
)
231
return
rule
(
pos_h
,
pos_v
,
pos_r
,
size_h
,
size_v
,
"
normal
"
,
nil
)
232
end
233 234
-- file stuff too
235
-- todo: default flushers
236
-- also color (via hash)
237 238
-- installer
239 240
drivers
.
install
{
241
name
=
"
lua
"
,
242
actions
=
{
243
prepare
=
prepare
,
244
initialize
=
initialize
,
245
finalize
=
finalize
,
246
wrapup
=
wrapup
,
247
cleanup
=
cleanup
,
248
convert
=
convert
,
249
outputfilename
=
outputfilename
,
250
}
,
251
flushers
=
{
252
updatefontstate
=
updatefontstate
,
253
character
=
flushcharacter
,
254
rule
=
flushrule
,
255
}
256
}
257 258
-- actions
259 260
local
function
outputfilename
(
driver
)
261
return
tex
.
jobname
.
.
"
-output.json
"
262
end
263 264
local
function
save
(
)
-- might become a driver function that already is plugged into stopactions
265
local
filename
=
outputfilename
(
)
266
drivers
.
report
(
"
saving result in %a
"
,
filename
)
267
starttiming
(
drivers
)
268
local
data
=
result
(
)
269
if
data
then
270
if
not
utilities
.
json
then
271
require
(
"
util-jsn
"
)
272
end
273
io
.
savedata
(
filename
,
utilities
.
json
.
tostring
(
data
,
not
compact
)
)
274
end
275
stoptiming
(
drivers
)
276
end
277 278
local
function
prepare
(
driver
)
279
converter
=
drivers
.
converters
.
lmtx
280
luatex
.
registerstopactions
(
1
,
function
(
)
281
save
(
)
282
end
)
283
end
284 285
-- installer
286 287
drivers
.
install
{
288
name
=
"
json
"
,
289
actions
=
{
290
prepare
=
prepare
,
291
initialize
=
initialize
,
292
finalize
=
finalize
,
293
wrapup
=
wrapup
,
294
cleanup
=
cleanup
,
295
convert
=
convert
,
296
outputfilename
=
outputfilename
,
297
}
,
298
flushers
=
{
299
updatefontstate
=
updatefontstate
,
300
character
=
flushcharacter
,
301
rule
=
flushrule
,
302
}
303
}
304 305
-- actions
306 307
local
function
outputfilename
(
driver
)
308
return
tex
.
jobname
.
.
"
-output.js
"
309
end
310 311
local
function
save
(
)
-- might become a driver function that already is plugged into stopactions
312
local
filename
=
outputfilename
(
)
313
drivers
.
report
(
"
saving result in %a
"
,
filename
)
314
starttiming
(
drivers
)
315
local
data
=
result
(
)
316
if
data
then
317
if
not
utilities
.
json
then
318
require
(
"
util-jsn
"
)
319
end
320
io
.
savedata
(
filename
,
321
"
const JSON_CONTEXT = JSON.parse ( `
"
.
.
322
utilities
.
json
.
tostring
(
data
,
not
compact
)
.
.
323
"
` ) ;\n
"
324
)
325
end
326
stoptiming
(
drivers
)
327
end
328 329
local
function
prepare
(
driver
)
330
converter
=
drivers
.
converters
.
lmtx
331
luatex
.
registerstopactions
(
1
,
function
(
)
332
save
(
)
333
end
)
334
end
335 336
-- installer
337 338
drivers
.
install
{
339
name
=
"
js
"
,
340
actions
=
{
341
prepare
=
prepare
,
342
initialize
=
initialize
,
343
finalize
=
finalize
,
344
wrapup
=
wrapup
,
345
cleanup
=
cleanup
,
346
convert
=
convert
,
347
outputfilename
=
outputfilename
,
348
}
,
349
flushers
=
{
350
updatefontstate
=
updatefontstate
,
351
character
=
flushcharacter
,
352
rule
=
flushrule
,
353
}
354
}
355