node-ini.lua /size: 11 Kb    last modification: 2021-10-28 13:50
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
node-ini
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to node-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
--[[ldx-- 10<p>Most of the code that had accumulated here is now separated in modules.</p> 11--ldx]]
--
12 13
-- I need to clean up this module as it's a bit of a mess now. The latest luatex
14
-- has most tables but we have a few more in luametatex. Also, some are different
15
-- between these engines. We started out with hardcoded tables, that then ended
16
-- up as comments and are now gone (as they differ per engine anyway).
17 18
local
next
,
type
,
tostring
=
next
,
type
,
tostring
19
local
gsub
=
string
.
gsub
20
local
concat
,
remove
=
table
.
concat
,
table
.
remove
21
local
sortedhash
,
sortedkeys
,
swapped
=
table
.
sortedhash
,
table
.
sortedkeys
,
table
.
swapped
22 23
--[[ldx-- 24<p>Access to nodes is what gives <l n='luatex'/> its power. Here we implement a 25few helper functions. These functions are rather optimized.</p> 26--ldx]]
--
27 28
--[[ldx-- 29<p>When manipulating node lists in <l n='context'/>, we will remove nodes and 30insert new ones. While node access was implemented, we did quite some experiments 31in order to find out if manipulating nodes in <l n='lua'/> was feasible from the 32perspective of performance.</p> 33 34<p>First of all, we noticed that the bottleneck is more with excessive callbacks 35(some gets called very often) and the conversion from and to <l n='tex'/>'s 36datastructures. However, at the <l n='lua'/> end, we found that inserting and 37deleting nodes in a table could become a bottleneck.</p> 38 39<p>This resulted in two special situations in passing nodes back to <l n='tex'/>: 40a table entry with value <type>false</type> is ignored, and when instead of a 41table <type>true</type> is returned, the original table is used.</p> 42 43<p>Insertion is handled (at least in <l n='context'/> as follows. When we need to 44insert a node at a certain position, we change the node at that position by a 45dummy node, tagged <type>inline</type> which itself has_attribute the original 46node and one or more new nodes. Before we pass back the list we collapse the 47list. Of course collapsing could be built into the <l n='tex'/> engine, but this 48is a not so natural extension.</p> 49 50<p>When we collapse (something that we only do when really needed), we also 51ignore the empty nodes. [This is obsolete!]</p> 52--ldx]]
--
53 54
-- local gf = node.direct.getfield
55
-- local n = table.setmetatableindex("number")
56
-- function node.direct.getfield(a,b) n[b] = n[b] + 1 print(b,n[b]) return gf(a,b) end
57 58
nodes
=
nodes
or
{
}
59
local
nodes
=
nodes
60
nodes
.
handlers
=
nodes
.
handlers
or
{
}
61 62
local
mark
=
utilities
.
storage
.
mark
63
local
allocate
=
utilities
.
storage
.
allocate
64
local
formatcolumns
=
utilities
.
formatters
.
formatcolumns
65 66
local
getsubtypes
=
node
.
subtypes
67
local
getvalues
=
node
.
values
68 69
tex
.
magicconstants
=
{
-- we use tex.constants for something else
70
running
=
-1073741824
,
71
maxdimen
=
1073741823
,
-- 0x3FFFFFFF or 2^30-1
72
trueinch
=
4736286
,
73
}
74 75
local
listcodes
=
mark
(
getsubtypes
(
"
list
"
)
)
76
local
rulecodes
=
mark
(
getsubtypes
(
"
rule
"
)
)
77
local
dircodes
=
mark
(
getsubtypes
(
"
dir
"
)
)
78
local
glyphcodes
=
mark
(
getsubtypes
(
"
glyph
"
)
)
79
local
disccodes
=
mark
(
getsubtypes
(
"
disc
"
)
)
80
local
gluecodes
=
mark
(
getsubtypes
(
"
glue
"
)
)
81
local
fillcodes
=
mark
(
getsubtypes
(
"
fill
"
)
)
82
local
boundarycodes
=
mark
(
getsubtypes
(
"
boundary
"
)
)
83
local
penaltycodes
=
mark
(
getsubtypes
(
"
penalty
"
)
)
84
local
kerncodes
=
mark
(
getsubtypes
(
"
kern
"
)
)
85
local
margincodes
=
mark
(
getsubtypes
(
"
marginkern
"
)
)
86
local
mathcodes
=
mark
(
getsubtypes
(
"
math
"
)
)
87
local
noadcodes
=
mark
(
getsubtypes
(
"
noad
"
)
)
88
local
radicalcodes
=
mark
(
getsubtypes
(
"
radical
"
)
)
89
local
accentcodes
=
mark
(
getsubtypes
(
"
accent
"
)
)
90
local
fencecodes
=
mark
(
getsubtypes
(
"
fence
"
)
)
91
----- fractioncodes = mark(getsubtypes("fraction"))
92
local
parcodes
=
allocate
{
[
0
]
=
"
vmode_par
"
,
"
local_box
"
,
"
hmode_par
"
,
"
penalty
"
,
"
math
"
}
93 94
local
function
simplified
(
t
)
95
local
r
=
{
}
96
for
k
,
v
in
next
,
t
do
97
r
[
k
]
=
gsub
(
v
,
"
_
"
,
"
"
)
98
end
99
return
r
100
end
101 102
local
nodecodes
=
simplified
(
node
.
types
(
)
)
103
local
whatcodes
=
simplified
(
node
.
whatsits
and
node
.
whatsits
(
)
or
{
}
)
104 105
local
usercodes
=
allocate
{
106
[
97
]
=
"
attribute
"
,
-- a
107
[
100
]
=
"
number
"
,
-- d
108
[
102
]
=
"
float
"
,
-- f
109
[
108
]
=
"
lua
"
,
-- l
110
[
110
]
=
"
node
"
,
-- n
111
[
115
]
=
"
string
"
,
-- s
112
[
116
]
=
"
token
"
-- t
113
}
114 115
local
noadoptions
=
allocate
{
116
set
=
0x08
,
117
unused_1
=
0x00
+
0x08
,
118
unused_2
=
0x01
+
0x08
,
119
axis
=
0x02
+
0x08
,
120
no_axis
=
0x04
+
0x08
,
121
exact
=
0x10
+
0x08
,
122
left
=
0x11
+
0x08
,
123
middle
=
0x12
+
0x08
,
124
right
=
0x14
+
0x08
,
125
}
126 127
local
dirvalues
=
allocate
{
128
[
0
]
=
"
lefttoright
"
,
129
[
1
]
=
"
righttoleft
"
,
130
}
131 132
local
literalvalues
=
allocate
{
133
[
0
]
=
"
origin
"
,
134
[
1
]
=
"
page
"
,
135
[
2
]
=
"
always
"
,
136
[
3
]
=
"
raw
"
,
137
[
4
]
=
"
text
"
,
138
[
5
]
=
"
font
"
,
139
[
6
]
=
"
special
"
,
140
}
141 142
local
gluevalues
=
mark
(
getvalues
(
"
glue
"
)
)
143 144
gluecodes
=
allocate
(
swapped
(
gluecodes
,
gluecodes
)
)
145
dircodes
=
allocate
(
swapped
(
dircodes
,
dircodes
)
)
146
boundarycodes
=
allocate
(
swapped
(
boundarycodes
,
boundarycodes
)
)
147
noadcodes
=
allocate
(
swapped
(
noadcodes
,
noadcodes
)
)
148
radicalcodes
=
allocate
(
swapped
(
radicalcodes
,
radicalcodes
)
)
149
nodecodes
=
allocate
(
swapped
(
nodecodes
,
nodecodes
)
)
150
whatcodes
=
allocate
(
swapped
(
whatcodes
,
whatcodes
)
)
151
listcodes
=
allocate
(
swapped
(
listcodes
,
listcodes
)
)
152
glyphcodes
=
allocate
(
swapped
(
glyphcodes
,
glyphcodes
)
)
153
kerncodes
=
allocate
(
swapped
(
kerncodes
,
kerncodes
)
)
154
penaltycodes
=
allocate
(
swapped
(
penaltycodes
,
penaltycodes
)
)
155
mathcodes
=
allocate
(
swapped
(
mathcodes
,
mathcodes
)
)
156
fillcodes
=
allocate
(
swapped
(
fillcodes
,
fillcodes
)
)
157
margincodes
=
allocate
(
swapped
(
margincodes
,
margincodes
)
)
158
disccodes
=
allocate
(
swapped
(
disccodes
,
disccodes
)
)
159
accentcodes
=
allocate
(
swapped
(
accentcodes
,
accentcodes
)
)
160
fencecodes
=
allocate
(
swapped
(
fencecodes
,
fencecodes
)
)
161
parcodes
=
allocate
(
swapped
(
parcodes
,
parcodes
)
)
162
rulecodes
=
allocate
(
swapped
(
rulecodes
,
rulecodes
)
)
163
usercodes
=
allocate
(
swapped
(
usercodes
,
usercodes
)
)
164
noadoptions
=
allocate
(
swapped
(
noadoptions
,
noadoptions
)
)
165
dirvalues
=
allocate
(
swapped
(
dirvalues
,
dirvalues
)
)
166
gluevalues
=
allocate
(
swapped
(
gluevalues
,
gluevalues
)
)
167
literalvalues
=
allocate
(
swapped
(
literalvalues
,
literalvalues
)
)
168 169
if
not
nodecodes
.
delimiter
then
170
-- as in luametatex / lmtx
171
local
d
=
nodecodes
.
delim
172
nodecodes
.
delimiter
=
d
173
nodecodes
[
d
]
=
"
delimiter
"
174
nodecodes
.
delim
=
nil
175
end
176 177
if
not
nodecodes
.
par
then
178
-- as in luametatex / lmtx
179
local
p
=
nodecodes
.
localpar
180
nodecodes
.
par
=
p
181
nodecodes
[
p
]
=
"
par
"
182
end
183 184
if
not
nodecodes
.
insert
then
185
-- as in luametatex / lmtx
186
local
i
=
nodecodes
.
ins
187
nodecodes
.
insert
=
i
188
nodecodes
[
i
]
=
"
insert
"
189
nodecodes
.
ins
=
nil
190
end
191 192
if
not
gluecodes
.
indentskip
then
193
gluecodes
.
indentskip
=
gluecodes
.
userskip
194
gluecodes
.
lefthangskip
=
gluecodes
.
userskip
195
gluecodes
.
righthangskip
=
gluecodes
.
userskip
196
gluecodes
.
correctionskip
=
gluecodes
.
userskip
197
gluecodes
.
intermathskip
=
gluecodes
.
userskip
198
gluecodes
.
parfillleftskip
=
gluecodes
.
parfillskip
199
gluecodes
.
parfillrightskip
=
gluecodes
.
parfillskip
200
end
201 202
if
not
whatcodes
.
literal
then
203
whatcodes
.
literal
=
whatcodes
.
pdfliteral
204
whatcodes
.
save
=
whatcodes
.
pdfsave
205
whatcodes
.
restore
=
whatcodes
.
pdfrestore
206
whatcodes
.
setmatrix
=
whatcodes
.
pdfsetmatrix
207
end
208 209
nodes
.
gluecodes
=
gluecodes
210
nodes
.
dircodes
=
dircodes
211
nodes
.
boundarycodes
=
boundarycodes
212
nodes
.
noadcodes
=
noadcodes
213
nodes
.
nodecodes
=
nodecodes
214
nodes
.
whatcodes
=
whatcodes
215
nodes
.
listcodes
=
listcodes
216
nodes
.
glyphcodes
=
glyphcodes
217
nodes
.
kerncodes
=
kerncodes
218
nodes
.
penaltycodes
=
penaltycodes
219
nodes
.
mathcodes
=
mathcodes
220
nodes
.
fillcodes
=
fillcodes
221
nodes
.
margincodes
=
margincodes
222
nodes
.
disccodes
=
disccodes
223
nodes
.
accentcodes
=
accentcodes
224
nodes
.
radicalcodes
=
radicalcodes
225
nodes
.
fencecodes
=
fencecodes
226
nodes
.
parcodes
=
parcodes
227
nodes
.
rulecodes
=
rulecodes
228
nodes
.
usercodes
=
usercodes
229
nodes
.
noadoptions
=
noadoptions
230
nodes
.
dirvalues
=
dirvalues
231
nodes
.
gluevalues
=
gluevalues
232
nodes
.
literalvalues
=
literalvalues
233 234
nodes
.
subtypes
=
allocate
{
235
[
nodecodes
.
accent
]
=
accentcodes
,
236
[
nodecodes
.
boundary
]
=
boundarycodes
,
237
[
nodecodes
.
dir
]
=
dircodes
,
238
[
nodecodes
.
disc
]
=
disccodes
,
239
[
nodecodes
.
fence
]
=
fencecodes
,
240
[
nodecodes
.
glue
]
=
gluecodes
,
241
[
nodecodes
.
glyph
]
=
glyphcodes
,
242
[
nodecodes
.
hlist
]
=
listcodes
,
243
[
nodecodes
.
kern
]
=
kerncodes
,
244
[
nodecodes
.
par
]
=
parcodes
,
245
-- [nodecodes.marginkern] = margincodes,
246
[
nodecodes
.
math
]
=
mathcodes
,
247
[
nodecodes
.
noad
]
=
noadcodes
,
248
[
nodecodes
.
penalty
]
=
penaltycodes
,
249
[
nodecodes
.
radical
]
=
radicalcodes
,
250
[
nodecodes
.
rule
]
=
rulecodes
,
251
-- [nodecodes.user] = usercodes,
252
[
nodecodes
.
vlist
]
=
listcodes
,
253
[
nodecodes
.
whatsit
]
=
whatcodes
,
254
[
nodecodes
.
marginkern
]
=
margincodes
255
}
256 257
table
.
setmetatableindex
(
nodes
.
subtypes
,
function
(
t
,
k
)
258
local
v
=
{
}
259
t
[
k
]
=
v
260
return
v
261
end
)
262 263
-- a few more friendly aliases:
264 265
nodes
.
skipcodes
=
gluecodes
266
nodes
.
directioncodes
=
dircodes
267
nodes
.
whatsitcodes
=
whatcodes
268
nodes
.
marginkerncodes
=
margincodes
269
nodes
.
discretionarycodes
=
disccodes
270
nodes
.
directionvalues
=
dirvalues
271
nodes
.
skipvalues
=
gluevalues
272
nodes
.
literalvalues
=
literalvalues
273 274
glyphcodes
.
glyph
=
glyphcodes
.
character
275 276
listcodes
.
row
=
listcodes
.
alignment
277
listcodes
.
column
=
listcodes
.
alignment
278 279
kerncodes
.
kerning
=
kerncodes
.
fontkern
280 281
kerncodes
.
italiccorrection
=
kerncodes
.
italiccorrection
or
1
-- new
282 283
literalvalues
.
direct
=
literalvalues
.
always
284 285
nodes
.
codes
=
allocate
{
-- mostly for listing
286
glue
=
skipcodes
,
287
boundary
=
boundarycodes
,
288
noad
=
noadcodes
,
289
node
=
nodecodes
,
290
hlist
=
listcodes
,
291
vlist
=
listcodes
,
292
glyph
=
glyphcodes
,
293
kern
=
kerncodes
,
294
penalty
=
penaltycodes
,
295
math
=
mathnodes
,
296
fill
=
fillcodes
,
297
margin
=
margincodes
,
298
disc
=
disccodes
,
299
whatsit
=
whatcodes
,
300
accent
=
accentcodes
,
301
fence
=
fencecodes
,
302
rule
=
rulecodes
,
303
user
=
usercodes
,
304
noadoptions
=
noadoptions
,
305
}
306 307
nodes
.
noadoptions
=
{
308
set
=
0x08
,
309
unused_1
=
0x00
+
0x08
,
310
unused_2
=
0x01
+
0x08
,
311
axis
=
0x02
+
0x08
,
312
no_axis
=
0x04
+
0x08
,
313
exact
=
0x10
+
0x08
,
314
left
=
0x11
+
0x08
,
315
middle
=
0x12
+
0x08
,
316
right
=
0x14
+
0x08
,
317
}
318 319
local
report_codes
=
logs
.
reporter
(
"
nodes
"
,
"
codes
"
)
320 321
function
nodes
.
showcodes
(
)
322
local
t
=
{
}
323
for
name
,
codes
in
sortedhash
(
nodes
.
codes
)
do
324
local
sorted
=
sortedkeys
(
codes
)
325
for
i
=
1
,
#
sorted
do
326
local
s
=
sorted
[
i
]
327
if
type
(
s
)
~
=
"
number
"
then
328
t
[
#
t
+
1
]
=
{
name
,
s
,
codes
[
s
]
}
329
end
330
end
331
end
332
formatcolumns
(
t
)
333
for
k
=
1
,
#
t
do
334
report_codes
(
t
[
k
]
)
335
end
336
end
337 338
trackers
.
register
(
"
system.showcodes
"
,
nodes
.
showcodes
)
339 340
-- We don't need this sanitize-after-callback in ConTeXt and by disabling it we
341
-- also have a way to check if LuaTeX itself does the right thing.
342 343
if
node
.
fix_node_lists
then
344
node
.
fix_node_lists
(
false
)
345
end
346