node-tra.lua /size: 21 Kb    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
node-tra
'
]
=
{
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>This is rather experimental. We need more control and some of this 11might become a runtime module instead. This module will be cleaned up!</p> 12--ldx]]
--
13 14
local
next
=
next
15
local
utfchar
=
utf
.
char
16
local
format
,
match
,
gmatch
,
concat
,
rep
=
string
.
format
,
string
.
match
,
string
.
gmatch
,
table
.
concat
,
string
.
rep
17
local
lpegmatch
=
lpeg
.
match
18
local
clock
=
os
.
gettimeofday
or
os
.
clock
-- should go in environment
19 20
local
report_nodes
=
logs
.
reporter
(
"
nodes
"
,
"
tracing
"
)
21 22
local
nodes
,
node
,
context
=
nodes
,
node
,
context
23 24
local
texgetattribute
=
tex
.
getattribute
25 26
local
tracers
=
nodes
.
tracers
or
{
}
27
nodes
.
tracers
=
tracers
28 29
local
tasks
=
nodes
.
tasks
or
{
}
30
nodes
.
tasks
=
tasks
31 32
local
handlers
=
nodes
.
handlers
or
{
}
33
nodes
.
handlers
=
handlers
34 35
local
injections
=
nodes
.
injections
or
{
}
36
nodes
.
injections
=
injections
37 38
local
nuts
=
nodes
.
nuts
39
local
tonut
=
nuts
.
tonut
40
local
tonode
=
nuts
.
tonode
41 42
local
getnext
=
nuts
.
getnext
43
local
getprev
=
nuts
.
getprev
44
local
getid
=
nuts
.
getid
45
local
getsubtype
=
nuts
.
getsubtype
46
local
getlist
=
nuts
.
getlist
47
local
getdisc
=
nuts
.
getdisc
48
local
setattr
=
nuts
.
setattr
49
local
getglue
=
nuts
.
getglue
50
local
isglyph
=
nuts
.
isglyph
51
local
getdirection
=
nuts
.
getdirection
52
local
getwidth
=
nuts
.
getwidth
53 54
local
flush_list
=
nuts
.
flush_list
55
local
count_nodes
=
nuts
.
countall
56
local
used_nodes
=
nuts
.
usedlist
57 58
local
nextnode
=
nuts
.
traversers
.
node
59
local
nextglyph
=
nuts
.
traversers
.
glyph
60 61
local
d_tostring
=
nuts
.
tostring
62 63
local
nutpool
=
nuts
.
pool
64
local
new_rule
=
nutpool
.
rule
65 66
local
nodecodes
=
nodes
.
nodecodes
67
local
whatsitcodes
=
nodes
.
whatsitcodes
68
local
fillcodes
=
nodes
.
fillcodes
69 70
local
subtypes
=
nodes
.
subtypes
71 72
local
glyph_code
=
nodecodes
.
glyph
73
local
hlist_code
=
nodecodes
.
hlist
74
local
vlist_code
=
nodecodes
.
vlist
75
local
disc_code
=
nodecodes
.
disc
76
local
glue_code
=
nodecodes
.
glue
77
local
kern_code
=
nodecodes
.
kern
78
local
rule_code
=
nodecodes
.
rule
79
local
dir_code
=
nodecodes
.
dir
80
local
localpar_code
=
nodecodes
.
localpar
81
local
whatsit_code
=
nodecodes
.
whatsit
82 83
local
dimenfactors
=
number
.
dimenfactors
84
local
formatters
=
string
.
formatters
85 86
local
start_of_par
=
nuts
.
start_of_par
87 88
-- this will be reorganized:
89 90
function
nodes
.
showlist
(
head
,
message
)
91
if
message
then
92
report_nodes
(
message
)
93
end
94
for
n
in
nextnode
,
tonut
(
head
)
do
95
report_nodes
(
d_tostring
(
n
)
)
96
end
97
end
98 99
function
nodes
.
handlers
.
checkglyphs
(
head
,
message
)
100
local
h
=
tonut
(
head
)
-- tonut needed?
101
local
t
=
{
}
102
local
n
=
0
103
local
f
=
formatters
[
"
%U:%s
"
]
104
for
g
,
char
,
font
in
nextglyph
,
h
do
105
n
=
n
+
1
106
t
[
n
]
=
f
(
char
,
getsubtype
(
g
)
)
107
end
108
if
n
=
=
0
then
109
-- nothing to report
110
elseif
message
and
message
~
=
"
"
then
111
report_nodes
(
"
%s, %s glyphs: % t
"
,
message
,
n
,
t
)
112
else
113
report_nodes
(
"
%s glyphs: % t
"
,
n
,
t
)
114
end
115
return
false
116
end
117 118
function
nodes
.
handlers
.
checkforleaks
(
sparse
)
119
local
l
=
{
}
120
local
q
=
used_nodes
(
)
121
for
p
,
id
in
nextnode
,
q
do
122
local
s
=
table
.
serialize
(
nodes
.
astable
(
p
,
sparse
)
,
nodecodes
[
id
]
)
123
l
[
s
]
=
(
l
[
s
]
or
0
)
+
1
124
end
125
flush_list
(
q
)
126
for
k
,
v
in
next
,
l
do
127
report_nodes
(
"
%s * %s
"
,
v
,
k
)
128
end
129
end
130 131
local
fontcharacters
-- = fonts.hashes.descriptions
132 133
local
function
tosequence
(
start
,
stop
,
compact
)
134
if
start
then
135
if
not
fontcharacters
then
136
fontcharacters
=
fonts
.
hashes
.
descriptions
137
if
not
fontcharacters
then
138
return
"
[no char data]
"
139
end
140
end
141
local
f_sequence
=
formatters
[
"
U+%04X:%s
"
]
142
local
f_subrange
=
formatters
[
"
[[ %s ][ %s ][ %s ]]
"
]
143
start
=
tonut
(
start
)
144
stop
=
stop
and
tonut
(
stop
)
145
local
t
=
{
}
146
local
n
=
0
147
while
start
do
148
local
c
,
id
=
isglyph
(
start
)
149
if
c
then
150
local
u
=
fontcharacters
[
id
]
[
c
]
-- id == font id
151
u
=
u
and
u
.
unicode
or
c
152
if
type
(
u
)
=
=
"
table
"
then
153
local
tt
=
{
}
154
for
i
=
1
,
#
u
do
155
local
c
=
u
[
i
]
156
tt
[
i
]
=
compact
and
utfchar
(
c
)
or
f_sequence
(
c
,
utfchar
(
c
)
)
157
end
158
n
=
n
+
1
;
t
[
n
]
=
"
(
"
.
.
concat
(
tt
,
"
"
)
.
.
"
)
"
159
else
160
n
=
n
+
1
;
t
[
n
]
=
compact
and
utfchar
(
c
)
or
f_sequence
(
c
,
utfchar
(
c
)
)
161
end
162
elseif
id
=
=
disc_code
then
163
local
pre
,
post
,
replace
=
getdisc
(
start
)
164
t
[
#
t
+
1
]
=
f_subrange
(
pre
and
tosequence
(
pre
)
,
post
and
tosequence
(
post
)
,
replace
and
tosequence
(
replace
)
)
165
elseif
id
=
=
rule_code
then
166
n
=
n
+
1
;
t
[
n
]
=
compact
and
"
|
"
or
nodecodes
[
id
]
or
"
?
"
167
elseif
id
=
=
dir_code
then
168
local
d
,
p
=
getdirection
(
start
)
169
n
=
n
+
1
;
t
[
n
]
=
"
[<
"
.
.
(
p
and
"
-
"
or
"
+
"
)
.
.
d
.
.
"
>]
"
-- todo l2r etc
170
elseif
id
=
=
localpar_code
and
start_of_par
(
current
)
then
171
n
=
n
+
1
;
t
[
n
]
=
"
[<
"
.
.
getdirection
(
start
)
.
.
"
>]
"
-- todo l2r etc
172
elseif
compact
then
173
n
=
n
+
1
;
t
[
n
]
=
"
[]
"
174
else
175
n
=
n
+
1
;
t
[
n
]
=
nodecodes
[
id
]
176
end
177
if
start
=
=
stop
then
178
break
179
else
180
start
=
getnext
(
start
)
181
end
182
end
183
if
compact
then
184
return
concat
(
t
)
185
else
186
return
concat
(
t
,
"
"
)
187
end
188
else
189
return
"
[empty]
"
190
end
191
end
192 193
nodes
.
tosequence
=
tosequence
194
nuts
.
tosequence
=
tosequence
195 196
function
nodes
.
report
(
t
)
197
report_nodes
(
"
output %a, %s nodes
"
,
status
.
output_active
,
count_nodes
(
t
)
)
198
end
199 200
function
nodes
.
packlist
(
head
)
201
local
t
=
{
}
202
for
n
in
nextnode
,
tonut
(
head
)
do
203
t
[
#
t
+
1
]
=
d_tostring
(
n
)
204
end
205
return
t
206
end
207 208
function
nodes
.
idstostring
(
head
,
tail
)
209
head
=
tonut
(
head
)
210
tail
=
tail
and
tonut
(
tail
)
211
local
t
=
{
}
212
local
last_id
=
nil
213
local
last_n
=
0
214
local
f_two
=
formatters
[
"
[%s*%s]
"
]
215
local
f_one
=
formatters
[
"
[%s]
"
]
216
for
n
,
id
,
subtype
in
nextnode
,
head
do
217
if
id
=
=
whatsit_code
then
218
id
=
whatsitcodes
[
subtype
]
219
else
220
id
=
nodecodes
[
id
]
221
end
222
if
not
last_id
then
223
last_id
=
id
224
last_n
=
1
225
elseif
last_id
=
=
id
then
226
last_n
=
last_n
+
1
227
else
228
if
last_n
>
1
then
229
t
[
#
t
+
1
]
=
f_two
(
last_n
,
last_id
)
230
else
231
t
[
#
t
+
1
]
=
f_one
(
last_id
)
232
end
233
last_id
=
id
234
last_n
=
1
235
end
236
if
n
=
=
tail
then
237
break
238
end
239
end
240
if
not
last_id
then
241
t
[
#
t
+
1
]
=
"
no nodes
"
242
else
243
if
last_n
>
1
then
244
t
[
#
t
+
1
]
=
f_two
(
last_n
,
last_id
)
245
else
246
t
[
#
t
+
1
]
=
f_one
(
last_id
)
247
end
248
end
249
return
concat
(
t
,
"
"
)
250
end
251 252
function
nodes
.
idsandsubtypes
(
head
)
253
local
h
=
tonut
(
head
)
254
local
t
=
{
}
255
local
f
=
formatters
[
"
%s:%s
"
]
256
for
n
,
id
,
subtype
in
nextnode
,
h
do
257
local
c
=
nodecodes
[
id
]
258
if
subtype
then
259
t
[
#
t
+
1
]
=
f
(
c
,
subtypes
[
id
]
[
subtype
]
)
260
else
261
t
[
#
t
+
1
]
=
c
262
end
263
end
264
return
concat
(
t
,
"
"
)
265
end
266 267
-- function nodes.xidstostring(head,tail) -- only for special tracing of backlinks
268
-- head = tonut(head)
269
-- tail = tonut(tail)
270
-- local n = head
271
-- while n.next do
272
-- n = n.next
273
-- end
274
-- local t, last_id, last_n = { }, nil, 0
275
-- while n do
276
-- local id = n.id
277
-- if not last_id then
278
-- last_id, last_n = id, 1
279
-- elseif last_id == id then
280
-- last_n = last_n + 1
281
-- else
282
-- if last_n > 1 then
283
-- t[#t+1] = formatters["[%s*%s]"](last_n,nodecodes[last_id] or "?")
284
-- else
285
-- t[#t+1] = formatters["[%s]"](nodecodes[last_id] or "?")
286
-- end
287
-- last_id, last_n = id, 1
288
-- end
289
-- if n == head then
290
-- break
291
-- end
292
-- n = getprev(n)
293
-- end
294
-- if not last_id then
295
-- t[#t+1] = "no nodes"
296
-- elseif last_n > 1 then
297
-- t[#t+1] = formatters["[%s*%s]"](last_n,nodecodes[last_id] or "?")
298
-- else
299
-- t[#t+1] = formatters["[%s]"](nodecodes[last_id] or "?")
300
-- end
301
-- return table.concat(table.reversed(t)," ")
302
-- end
303 304
local
function
showsimplelist
(
h
,
depth
,
n
)
305
h
=
h
and
tonut
(
h
)
306
while
h
do
307
report_nodes
(
"
% w%s
"
,
n
,
d_tostring
(
h
)
)
308
if
not
depth
or
n
<
depth
then
309
local
id
=
getid
(
h
)
310
if
id
=
=
hlist_code
or
id
=
=
vlist_code
then
311
showsimplelist
(
getlist
(
h
)
,
depth
,
n
+
1
)
312
end
313
end
314
h
=
getnext
(
h
)
315
end
316
end
317 318
-- \startluacode
319
-- callbacks.register('buildpage_filter',function() nodes.show_simple_list(tex.lists.contrib_head) end)
320
-- \stopluacode
321
-- \vbox{b\footnote{n}a}
322
-- \startluacode
323
-- callbacks.register('buildpage_filter',nil)
324
-- \stopluacode
325 326
nodes
.
showsimplelist
=
function
(
h
,
depth
)
showsimplelist
(
h
,
depth
,
0
)
end
327 328
local
function
listtoutf
(
h
,
joiner
,
textonly
,
last
,
nodisc
)
329
local
w
=
{
}
330
local
n
=
0
331
local
g
=
formatters
[
"
<%i>
"
]
332
local
d
=
formatters
[
"
[%s|%s|%s]
"
]
333
while
h
do
334
local
c
,
id
=
isglyph
(
h
)
335
if
c
then
336
n
=
n
+
1
;
w
[
n
]
=
c
>
=
0
and
utfchar
(
c
)
or
g
(
c
)
337
if
joiner
then
338
n
=
n
+
1
;
w
[
n
]
=
joiner
339
end
340
elseif
id
=
=
disc_code
then
341
local
pre
,
pos
,
rep
=
getdisc
(
h
)
342
if
not
nodisc
then
343
n
=
n
+
1
;
w
[
n
]
=
d
(
344
pre
and
listtoutf
(
pre
,
joiner
,
textonly
)
or
"
"
,
345
pos
and
listtoutf
(
pos
,
joiner
,
textonly
)
or
"
"
,
346
rep
and
listtoutf
(
rep
,
joiner
,
textonly
)
or
"
"
347
)
348
elseif
rep
then
349
n
=
n
+
1
;
w
[
n
]
=
listtoutf
(
rep
,
joiner
,
textonly
)
or
"
"
350
end
351
if
joiner
then
352
n
=
n
+
1
;
w
[
n
]
=
joiner
353
end
354
elseif
textonly
then
355
if
id
=
=
glue_code
then
356
if
getwidth
(
h
)
>
0
then
357
n
=
n
+
1
;
w
[
n
]
=
"
"
358
end
359
elseif
id
=
=
hlist_code
or
id
=
=
vlist_code
then
360
n
=
n
+
1
;
w
[
n
]
=
"
[
"
361
n
=
n
+
1
;
w
[
n
]
=
listtoutf
(
getlist
(
h
)
,
joiner
,
textonly
,
last
,
nodisc
)
362
n
=
n
+
1
;
w
[
n
]
=
"
]
"
363
end
364
else
365
n
=
n
+
1
;
w
[
n
]
=
"
[-]
"
366
end
367
if
h
=
=
last
then
368
break
369
else
370
h
=
getnext
(
h
)
371
end
372
end
373
return
concat
(
w
,
"
"
,
1
,
(
w
[
n
]
=
=
joiner
)
and
(
n
-1
)
or
n
)
374
end
375 376
function
nodes
.
listtoutf
(
h
,
joiner
,
textonly
,
last
,
nodisc
)
377
if
h
then
378
local
joiner
=
joiner
=
=
true
and
utfchar
(
0x200C
)
or
joiner
-- zwnj
379
return
listtoutf
(
tonut
(
h
)
,
joiner
,
textonly
,
last
and
tonut
(
last
)
,
nodisc
)
380
else
381
return
"
"
382
end
383
end
384 385
local
what
=
{
[
0
]
=
"
unknown
"
,
"
line
"
,
"
box
"
,
"
indent
"
,
"
row
"
,
"
cell
"
}
386 387
local
function
showboxes
(
n
,
symbol
,
depth
)
388
depth
=
depth
or
0
389
symbol
=
symbol
or
"
.
"
390
for
n
,
id
,
subtype
in
nextnode
,
tonut
(
n
)
do
391
if
id
=
=
hlist_code
or
id
=
=
vlist_code
then
392
report_nodes
(
rep
(
symbol
,
depth
)
.
.
what
[
subtype
]
or
subtype
)
393
showboxes
(
getlist
(
n
)
,
symbol
,
depth
+
1
)
394
end
395
end
396
end
397 398
nodes
.
showboxes
=
showboxes
399 400
local
ptfactor
=
dimenfactors
.
pt
401
local
bpfactor
=
dimenfactors
.
bp
402
local
stripper
=
lpeg
.
patterns
.
stripzeros
403 404
local
f_f_f
=
formatters
[
"
%0.5Fpt plus %0.5F%s minus %0.5F%s
"
]
405
local
f_f_m
=
formatters
[
"
%0.5Fpt plus %0.5F%s minus %0.5Fpt
"
]
406
local
f_p_f
=
formatters
[
"
%0.5Fpt plus %0.5Fpt minus %0.5F%s
"
]
407
local
f_p_m
=
formatters
[
"
%0.5Fpt plus %0.5Fpt minus %0.5Fpt
"
]
408
local
f_f_z
=
formatters
[
"
%0.5Fpt plus %0.5F%s
"
]
409
local
f_p_z
=
formatters
[
"
%0.5Fpt plus %0.5Fpt
"
]
410
local
f_z_f
=
formatters
[
"
%0.5Fpt minus %0.5F%s
"
]
411
local
f_z_m
=
formatters
[
"
%0.5Fpt minus %0.5Fpt
"
]
412
local
f_z_z
=
formatters
[
"
%0.5Fpt
"
]
413 414
local
tonut
=
nodes
.
tonut
415 416
local
function
nodetodimen
(
n
)
417
n
=
tonut
(
n
)
418
local
id
=
getid
(
n
)
419
if
id
=
=
kern_code
then
420
local
width
=
getwidth
(
n
)
421
if
width
=
=
0
then
422
return
"
0pt
"
423
else
424
return
f_z_z
(
width
)
425
end
426
elseif
id
~
=
glue_code
then
427
return
"
0pt
"
428
end
429
local
width
,
stretch
,
shrink
,
stretch_order
,
shrink_order
=
getglue
(
n
)
430
stretch
=
stretch
/
65536
431
shrink
=
shrink
/
65536
432
width
=
width
/
65536
433
if
stretch_order
~
=
0
then
434
if
shrink_order
~
=
0
then
435
return
f_f_f
(
width
,
stretch
,
fillcodes
[
stretch_order
]
,
shrink
,
fillcodes
[
shrink_order
]
)
436
elseif
shrink
~
=
0
then
437
return
f_f_m
(
width
,
stretch
,
fillcodes
[
stretch_order
]
,
shrink
)
438
else
439
return
f_f_z
(
width
,
stretch
,
fillcodes
[
stretch_order
]
)
440
end
441
elseif
shrink_order
~
=
0
then
442
if
stretch
~
=
0
then
443
return
f_p_f
(
width
,
stretch
,
shrink
,
fillcodes
[
shrink_order
]
)
444
else
445
return
f_z_f
(
width
,
shrink
,
fillcodes
[
shrink_order
]
)
446
end
447
elseif
stretch
~
=
0
then
448
if
shrink
~
=
0
then
449
return
f_p_m
(
width
,
stretch
,
shrink
)
450
else
451
return
f_p_z
(
width
,
stretch
)
452
end
453
elseif
shrink
~
=
0
then
454
return
f_z_m
(
width
,
shrink
)
455
elseif
width
=
=
0
then
456
return
"
0pt
"
457
else
458
return
f_z_z
(
width
)
459
end
460
end
461 462 463
-- number.todimen(123)
464
-- number.todimen(123,"cm")
465
-- number.todimen(123,false,"%F))
466 467
local
f_pt
=
formatters
[
"
%p
"
]
468
local
f_un
=
formatters
[
"
%F%s
"
]
469 470
dimenfactors
[
"
"
]
=
dimenfactors
.
pt
471 472
local
function
numbertodimen
(
d
,
unit
,
fmt
)
473
if
not
d
or
d
=
=
0
then
474
if
fmt
then
475
return
formatters
[
fmt
]
(
0
,
unit
or
"
pt
"
)
476
elseif
unit
then
477
return
0
.
.
unit
478
else
479
return
"
0pt
"
480
end
481
elseif
fmt
then
482
if
not
unit
then
483
unit
=
"
pt
"
484
end
485
return
formatters
[
fmt
]
(
d
*
dimenfactors
[
unit
]
,
unit
)
486
elseif
not
unit
or
unit
=
=
"
pt
"
then
487
return
f_pt
(
d
)
488
else
489
return
f_un
(
d
*
dimenfactors
[
unit
]
,
unit
)
490
end
491
end
492 493
number
.
todimen
=
numbertodimen
494
nodes
.
todimen
=
nodetodimen
495 496
function
number
.
topoints
(
n
,
fmt
)
return
numbertodimen
(
n
,
"
pt
"
,
fmt
)
end
497
function
number
.
toinches
(
n
,
fmt
)
return
numbertodimen
(
n
,
"
in
"
,
fmt
)
end
498
function
number
.
tocentimeters
(
n
,
fmt
)
return
numbertodimen
(
n
,
"
cm
"
,
fmt
)
end
499
function
number
.
tomillimeters
(
n
,
fmt
)
return
numbertodimen
(
n
,
"
mm
"
,
fmt
)
end
500
function
number
.
toscaledpoints
(
n
,
fmt
)
return
numbertodimen
(
n
,
"
sp
"
,
fmt
)
end
501
function
number
.
toscaledpoints
(
n
)
return
n
.
.
"
sp
"
end
502
function
number
.
tobasepoints
(
n
,
fmt
)
return
numbertodimen
(
n
,
"
bp
"
,
fmt
)
end
503
function
number
.
topicas
(
n
,
fmt
)
return
numbertodimen
(
n
"
pc
"
,
fmt
)
end
504
function
number
.
todidots
(
n
,
fmt
)
return
numbertodimen
(
n
,
"
dd
"
,
fmt
)
end
505
function
number
.
tociceros
(
n
,
fmt
)
return
numbertodimen
(
n
,
"
cc
"
,
fmt
)
end
506
function
number
.
tonewdidots
(
n
,
fmt
)
return
numbertodimen
(
n
,
"
nd
"
,
fmt
)
end
507
function
number
.
tonewciceros
(
n
,
fmt
)
return
numbertodimen
(
n
,
"
nc
"
,
fmt
)
end
508 509
function
nodes
.
topoints
(
n
,
fmt
)
return
nodetodimen
(
n
,
"
pt
"
,
fmt
)
end
510
function
nodes
.
toinches
(
n
,
fmt
)
return
nodetodimen
(
n
,
"
in
"
,
fmt
)
end
511
function
nodes
.
tocentimeters
(
n
,
fmt
)
return
nodetodimen
(
n
,
"
cm
"
,
fmt
)
end
512
function
nodes
.
tomillimeters
(
n
,
fmt
)
return
nodetodimen
(
n
,
"
mm
"
,
fmt
)
end
513
function
nodes
.
toscaledpoints
(
n
,
fmt
)
return
nodetodimen
(
n
,
"
sp
"
,
fmt
)
end
514
function
nodes
.
toscaledpoints
(
n
)
return
n
.
.
"
sp
"
end
515
function
nodes
.
tobasepoints
(
n
,
fmt
)
return
nodetodimen
(
n
,
"
bp
"
,
fmt
)
end
516
function
nodes
.
topicas
(
n
,
fmt
)
return
nodetodimen
(
n
"
pc
"
,
fmt
)
end
517
function
nodes
.
todidots
(
n
,
fmt
)
return
nodetodimen
(
n
,
"
dd
"
,
fmt
)
end
518
function
nodes
.
tociceros
(
n
,
fmt
)
return
nodetodimen
(
n
,
"
cc
"
,
fmt
)
end
519
function
nodes
.
tonewdidots
(
n
,
fmt
)
return
nodetodimen
(
n
,
"
nd
"
,
fmt
)
end
520
function
nodes
.
tonewciceros
(
n
,
fmt
)
return
nodetodimen
(
n
,
"
nc
"
,
fmt
)
end
521 522
-- stop redefinition
523 524
local
points
=
function
(
n
)
525
if
not
n
or
n
=
=
0
then
526
return
"
0pt
"
527
elseif
type
(
n
)
=
=
"
number
"
then
528
return
lpegmatch
(
stripper
,
format
(
"
%.5fpt
"
,
n
*
ptfactor
)
)
-- faster than formatter
529
else
530
return
numbertodimen
(
n
,
"
pt
"
)
-- also deals with nodes
531
end
532
end
533 534
local
basepoints
=
function
(
n
)
535
if
not
n
or
n
=
=
0
then
536
return
"
0bp
"
537
elseif
type
(
n
)
=
=
"
number
"
then
538
return
lpegmatch
(
stripper
,
format
(
"
%.5fbp
"
,
n
*
bpfactor
)
)
-- faster than formatter
539
else
540
return
numbertodimen
(
n
,
"
bp
"
)
-- also deals with nodes
541
end
542
end
543 544
local
pts
=
function
(
n
)
545
if
not
n
or
n
=
=
0
then
546
return
"
0pt
"
547
elseif
type
(
n
)
=
=
"
number
"
then
548
return
format
(
"
%.5fpt
"
,
n
*
ptfactor
)
-- faster than formatter
549
else
550
return
numbertodimen
(
n
,
"
pt
"
)
-- also deals with nodes
551
end
552
end
553 554
local
nopts
=
function
(
n
)
555
if
not
n
or
n
=
=
0
then
556
return
"
0
"
557
else
558
return
format
(
"
%.5f
"
,
n
*
ptfactor
)
-- faster than formatter
559
end
560
end
561 562
number
.
points
=
points
563
number
.
basepoints
=
basepoints
564
number
.
pts
=
pts
565
number
.
nopts
=
nopts
566 567
nodes
.
points
=
function
(
n
)
return
numbertodimen
(
n
,
"
pt
"
)
end
568
nodes
.
basepoints
=
function
(
n
)
return
numbertodimen
(
n
,
"
bp
"
)
end
569
nodes
.
pts
=
function
(
n
)
return
numbertodimen
(
n
,
"
pt
"
)
end
570
nodes
.
nopts
=
function
(
n
)
return
format
(
"
%.5f
"
,
n
*
ptfactor
)
end
571 572
local
colors
=
{
}
573
tracers
.
colors
=
colors
574 575
local
unsetvalue
=
attributes
.
unsetvalue
576 577
local
a_color
=
attributes
.
private
(
'
color
'
)
578
local
a_colormodel
=
attributes
.
private
(
'
colormodel
'
)
579
local
m_color
=
attributes
.
list
[
a_color
]
or
{
}
580 581
function
colors
.
set
(
n
,
c
,
s
)
582
local
mc
=
m_color
[
c
]
583
local
nn
=
tonut
(
n
)
584
if
mc
then
585
local
mm
=
s
or
texgetattribute
(
a_colormodel
)
586
setattr
(
nn
,
a_colormodel
,
mm
<
=
0
and
mm
or
1
)
587
setattr
(
nn
,
a_color
,
mc
)
588
else
589
setattr
(
nn
,
a_color
,
unsetvalue
)
590
end
591
return
n
592
end
593 594
function
colors
.
setlist
(
n
,
c
,
s
)
595
local
nn
=
tonut
(
n
)
596
local
mc
=
m_color
[
c
]
or
unsetvalue
597
local
mm
=
s
or
texgetattribute
(
a_colormodel
)
598
if
mm
<
=
0
then
599
mm
=
1
600
end
601
while
nn
do
602
setattr
(
nn
,
a_colormodel
,
mm
)
603
setattr
(
nn
,
a_color
,
mc
)
604
nn
=
getnext
(
nn
)
605
end
606
return
n
607
end
608 609
function
colors
.
reset
(
n
)
610
setattr
(
tonut
(
n
)
,
a_color
,
unsetvalue
)
611
return
n
612
end
613 614
-- maybe
615 616
local
transparencies
=
{
}
617
tracers
.
transparencies
=
transparencies
618 619
local
a_transparency
=
attributes
.
private
(
'
transparency
'
)
620
local
m_transparency
=
attributes
.
list
[
a_transparency
]
or
{
}
621 622
function
transparencies
.
set
(
n
,
t
)
623
setattr
(
tonut
(
n
)
,
a_transparency
,
m_transparency
[
t
]
or
unsetvalue
)
624
return
n
625
end
626 627
function
transparencies
.
setlist
(
n
,
c
,
s
)
628
local
nn
=
tonut
(
n
)
629
local
mt
=
m_transparency
[
c
]
or
unsetvalue
630
while
nn
do
631
setattr
(
nn
,
a_transparency
,
mt
)
632
nn
=
getnext
(
nn
)
633
end
634
return
n
635
end
636 637
function
transparencies
.
reset
(
n
)
638
setattr
(
n
,
a_transparency
,
unsetvalue
)
639
return
n
640
end
641 642
-- for the moment here
643 644
local
visualizers
=
nodes
.
visualizers
or
{
}
645
nodes
.
visualizers
=
visualizers
646 647
function
visualizers
.
handler
(
head
)
648
return
head
,
false
649
end
650 651
-- we could cache attribute lists and set attr (copy will increment count) .. todo ..
652
-- although tracers are used seldom
653 654
local
function
setproperties
(
n
,
c
,
s
)
655
local
nn
=
tonut
(
n
)
656
local
mm
=
texgetattribute
(
a_colormodel
)
657
setattr
(
nn
,
a_colormodel
,
mm
>
0
and
mm
or
1
)
658
setattr
(
nn
,
a_color
,
m_color
[
c
]
)
659
setattr
(
nn
,
a_transparency
,
m_transparency
[
c
]
)
660
return
n
661
end
662 663
tracers
.
setproperties
=
setproperties
664 665
-- setting attrlist entries instead of attr for successive entries doesn't
666
-- speed up much (this function is only used in tracers anyway)
667 668
function
tracers
.
setlist
(
n
,
c
,
s
)
669
local
nn
=
tonut
(
n
)
670
local
mc
=
m_color
[
c
]
671
local
mt
=
m_transparency
[
c
]
672
local
mm
=
texgetattribute
(
a_colormodel
)
673
if
mm
<
=
0
then
674
mm
=
1
675
end
676
while
nn
do
677
setattr
(
nn
,
a_colormodel
,
mm
)
678
setattr
(
nn
,
a_color
,
mc
)
679
setattr
(
nn
,
a_transparency
,
mt
)
680
nn
=
getnext
(
nn
)
681
end
682
return
n
683
end
684 685
function
tracers
.
resetproperties
(
n
)
686
local
nn
=
tonut
(
n
)
687
setattr
(
nn
,
a_color
,
unsetvalue
)
688
setattr
(
nn
,
a_transparency
,
unsetvalue
)
689
return
n
690
end
691 692
-- this one returns a nut
693 694
local
nodestracerpool
=
{
}
695
local
nutstracerpool
=
{
}
696 697
tracers
.
pool
=
{
698
nodes
=
nodestracerpool
,
699
nuts
=
nutstracerpool
,
700
}
701 702
table
.
setmetatableindex
(
nodestracerpool
,
function
(
t
,
k
,
v
)
703
local
f
=
nutstracerpool
[
k
]
704
local
v
=
function
(
...
)
705
return
tonode
(
f
(
...
)
)
706
end
707
t
[
k
]
=
v
708
return
v
709
end
)
710 711
function
nutstracerpool
.
rule
(
w
,
h
,
d
,
c
,
s
)
-- so some day we can consider using literals (speedup)
712
return
setproperties
(
new_rule
(
w
,
h
,
d
)
,
c
,
s
)
713
end
714 715
tracers
.
rule
=
nodestracerpool
.
rule
-- for a while
716 717
-- local function show(head,n,message)
718
-- print("START",message or "")
719
-- local i = 0
720
-- for current in traverse(head) do
721
-- local prev = getprev(current)
722
-- local next = getnext(current)
723
-- i = i + 1
724
-- print(i, prev and nodecodes[getid(prev)],nodecodes[getid(current)],next and nodecodes[getid(next)])
725
-- if i == n then
726
-- break
727
-- end
728
-- end
729
-- print("STOP", message or "")
730
-- end
731