summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/trac-vis.lmt
blob: 0cf6cb6a3814c3f517f7ff41d047a4d1aa896a29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
if not modules then modules = { } end modules ['trac-vis'] = {
    version   = 1.001,
    optimize  = true,
    comment   = "companion to trac-vis.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local node, nodes, attributes, tex = node, nodes, attributes, tex
local type, tonumber, next, rawget = type, tonumber, next, rawget
local gmatch, gsub = string.gmatch, string.gsub
local formatters = string.formatters
local round = math.round

-- This module started out in the early days of mkiv and luatex with visualizing
-- kerns related to fonts. In the process of cleaning up the visual debugger code it
-- made sense to integrate some other code that I had laying around and replace the
-- old supp-vis debugging code. As only a subset of the old visual debugger makes
-- sense it has become a different implementation. Soms of the m-visual
-- functionality will also be ported. The code is rather trivial. The caching is not
-- really needed but saves upto 50% of the time needed to add visualization. Of
-- course the overall runtime is larger because of color and layer processing in the
-- backend (can be times as much) so the runtime is somewhat larger with full
-- visualization enabled. In practice this will never happen unless one is demoing.

-- todo: global switch (so no attributes)
-- todo: maybe also xoffset, yoffset of glyph
-- todo: inline concat (more efficient)
-- todo: tags can also be numbers (just add to hash)
-- todo: make a lmtx variant (a few more efficient fetchers)

local nuts                = nodes.nuts
local tonut               = nuts.tonut

local setboth             = nuts.setboth
local setlink             = nuts.setlink
local setlist             = nuts.setlist
local setsubtype          = nuts.setsubtype
local setattr             = nuts.setattr
local setwidth            = nuts.setwidth
local setshift            = nuts.setshift
local setoffsets          = nuts.setoffsets
local getglyphdimensions  = nuts.getglyphdimensions

local getid               = nuts.getid
local getfont             = nuts.getfont
local getattr             = nuts.getattr
local getsubtype          = nuts.getsubtype
local getbox              = nuts.getbox
local getlist             = nuts.getlist
local getprev             = nuts.getprev
local getnext             = nuts.getnext
local getboth             = nuts.getboth
local getwhd              = nuts.getwhd
local getkern             = nuts.getkern
local getpenalty          = nuts.getpenalty
local getwidth            = nuts.getwidth
local getdepth            = nuts.getdepth
local getexpansion        = nuts.getexpansion
local getstate            = nuts.getstate
local getoffsets          = nuts.getoffsets
local getindex            = nuts.getindex
local getprop             = nuts.getprop

local isglyph             = nuts.isglyph

local hpack_nodes         = nuts.hpack
local vpack_nodes         = nuts.vpack
local copylist            = nuts.copylist
local copy_node           = nuts.copy
local insertnodebefore    = nuts.insertbefore
local insertnodeafter     = nuts.insertafter
local apply_to_nodes      = nuts.apply
local flushnodelist       = nuts.flushlist

local hpack_string        = nuts.typesetters.tohpack

local texgetattribute     = tex.getattribute
local texsetattribute     = tex.setattribute

local setmetatableindex   = table.setmetatableindex

local unsetvalue          = attributes.unsetvalue

local current_font        = font.current

local fonthashes          = fonts.hashes
local chardata            = fonthashes.characters
local exheights           = fonthashes.exheights
local emwidths            = fonthashes.emwidths
local pt_factor           = number.dimenfactors.pt

local nodepool            = nuts.pool
local new_rule            = nodepool.rule
local new_kern            = nodepool.kern
local new_glue            = nodepool.glue
local new_hlist           = nodepool.hlist
local new_vlist           = nodepool.vlist

local tracers             = nodes.tracers
local visualizers         = nodes.visualizers

local setcolor            = tracers.colors.set
local setlistcolor        = tracers.colors.setlist
local settransparency     = tracers.transparencies.set
local setlisttransparency = tracers.transparencies.setlist

local starttiming         = statistics.starttiming
local stoptiming          = statistics.stoptiming

local a_visual            = attributes.private("visual")
local a_layer             = attributes.private("viewerlayer")

local enableaction        = nodes.tasks.enableaction

local report_visualize = logs.reporter("visualize")

local modes = {
    hbox          = 0x0000001,
    vbox          = 0x0000002,
    vtop          = 0x0000004,
    kern          = 0x0000008,
    glue          = 0x0000010,
    penalty       = 0x0000020,
    fontkern      = 0x0000040,
    strut         = 0x0000080,
    whatsit       = 0x0000100,
    glyph         = 0x0000200,
    simple        = 0x0000400,
    simplehbox    = 0x0000401,
    simplevbox    = 0x0000402,
    simplevtop    = 0x0000404,
    user          = 0x0000800,
    math          = 0x0001000,
    italic        = 0x0002000,
    origin        = 0x0004000,
    discretionary = 0x0008000,
    expansion     = 0x0010000,
    line          = 0x0020000,
    space         = 0x0040000,
    depth         = 0x0080000,
    marginkern    = 0x0100000,
    mathkern      = 0x0200000, --
    dir           = 0x0400000,
    par           = 0x0800000,
    mathglue      = 0x1000000, --
    mark          = 0x2000000,
    insert        = 0x4000000,
    boundary      = 0x8000000,
}

visualizers.modes = modes

local usedfont, exheight, emwidth
local l_penalty, l_glue, l_kern, l_fontkern, l_hbox, l_vbox, l_vtop, l_strut, l_whatsit, l_glyph, l_user, l_math, l_marginkern, l_mathkern, l_mathshape, l_italic, l_origin, l_discretionary, l_expansion, l_line, l_space, l_depth,
    l_dir, l_whatsit, l_mark, l_insert, l_boundary

local enabled = false
local layers  = { }

local preset_boxes  = modes.hbox + modes.vbox + modes.vtop + modes.origin
local preset_makeup = preset_boxes
                    + modes.kern + modes.glue + modes.penalty + modes.boundary
local preset_all    = preset_makeup
                    + modes.fontkern + modes.marginkern + modes.mathkern
                    + modes.whatsit + modes.glyph + modes.user + modes.math
                    + modes.dir + modes.mathglue + modes.mark + modes.insert

function visualizers.setfont(id)
    usedfont = id or current_font()
    exheight = exheights[usedfont]
    emwidth  = emwidths[usedfont]
end

-- we can preset a bunch of bits

local userrule    -- bah, not yet defined: todo, delayed(nuts.rules,"userrule")
local outlinerule -- bah, not yet defined: todo, delayed(nuts.rules,"userrule")
local emptyrule   -- bah, not yet defined: todo, delayed(nuts.rules,"userrule")

local function initialize()
    --
    if not usedfont then
        -- we use a narrow monospaced font -- infofont ?
        visualizers.setfont(fonts.definers.define { name = "lmmonoltcond10regular", size = tex.sp("4pt") })
    end
    --
    for mode, value in next, modes do
        local tag = formatters["v_%s"](mode)
        attributes.viewerlayers.define {
            tag       = tag,
            title     = formatters["visualizer %s"](mode),
            visible   = "start",
            editable  = "yes",
            printable = "yes"
        }
        layers[mode] = attributes.viewerlayers.register(tag,true)
    end
    l_hbox          = layers.hbox
    l_vbox          = layers.vbox
    l_vtop          = layers.vtop
    l_glue          = layers.glue
    l_kern          = layers.kern
    l_penalty       = layers.penalty
    l_fontkern      = layers.fontkern
    l_strut         = layers.strut
    l_whatsit       = layers.whatsit
    l_glyph         = layers.glyph
    l_user          = layers.user
    l_math          = layers.math
    l_italic        = layers.italic
    l_marginkern    = layers.marginkern
    l_mathkern      = layers.mathkern
    l_mathshapekern = layers.mathshapekern
    l_origin        = layers.origin
    l_discretionary = layers.discretionary
    l_expansion     = layers.expansion
    l_line          = layers.line
    l_space         = layers.space
    l_depth         = layers.depth
    l_dir           = layers.dir
    l_par           = layers.par
    l_mark          = layers.mark
    l_insert        = layers.insert
    l_boundary      = layers.boundary
    --
    if not userrule then
       userrule = nuts.rules.userrule
    end
    --
    if not outlinerule then
       outlinerule = nuts.pool.outlinerule
    end
    --
    if not emptyrule then
       emptyrule = nuts.pool.emptyrule
    end
    initialize = false
end

local function enable()
    if initialize then
        initialize()
    end
    enableaction("shipouts","nodes.visualizers.handler")
    report_visualize("enabled")
    enabled = true
    tex.setcount("global","c_syst_visualizers_state",1) -- so that we can optimize at the tex end
end

local function setvisual(n,a,what,list) -- this will become more efficient when we have the bit lib linked in
    if not n or n == "reset" then
        return unsetvalue
    elseif n == true or n == "makeup" then
        if not a or a == 0 or a == unsetvalue then
            a = preset_makeup
        else
            a = a | preset_makeup
        end
    elseif n == "boxes" then
        if not a or a == 0 or a == unsetvalue then
            a = preset_boxes
        else
            a = a | preset_boxes
        end
    elseif n == "all" then
        if what == false then
            return unsetvalue
        elseif not a or a == 0 or a == unsetvalue then
            a = preset_all
        else
            a = a | preset_all
        end
    elseif type(n) == "string" then
        for s in gmatch(n,"[a-z]+") do
            local m = modes[s]
            if not m then
                -- go on
            elseif not a or a == 0 or a == unsetvalue then
                a = m
            else
                a = a | m
            end
        end
    elseif type(n) == "number" then
        if not a or a == 0 or a == unsetvalue then
            a = n
        else
            a = a | n
        end
    end
    if not a or a == 0 or a == unsetvalue then
        return unsetvalue
    elseif not enabled then -- must happen at runtime (as we don't store layers yet)
        enable()
    end
    return a
end

function nuts.setvisual(n,mode)
    if mode then
        setattr(n,a_visual,setvisual(mode,getattr(n,a_visual),true))
    else
        local a = texgetattribute(a_visual)
        if a ~= unsetvalue then
            setattr(n,a_visual,a)
        end
    end
end

function nuts.setvisuals(n,mode) -- currently the same
    setattr(n,a_visual,setvisual(mode,getattr(n,a_visual),true,true))
end

-- fast setters

do

    local cached = setmetatableindex(function(t,k)
        if k == true then
            return texgetattribute(a_visual)
        elseif not k then
            t[k] = unsetvalue
            return unsetvalue
        else
            local v = setvisual(k)
            t[k] = v
            return v
        end
    end)

 -- local function applyvisuals(n,mode)
 --     local a = cached[mode]
 --     apply_to_nodes(n,function(n) setattr(n,a_visual,a) end)
 -- end

    local a = unsetvalue

    local f = function(n) setattr(n,a_visual,a) end

    local function applyvisuals(n,mode)
        a = cached[mode]
        apply_to_nodes(n,f)
    end

    nuts.applyvisuals = applyvisuals

    function nodes.applyvisuals(n,mode)
        applyvisuals(tonut(n),mode)
    end

    function visualizers.attribute(mode)
        return cached[mode]
    end

    visualizers.attributes = cached

end

function nuts.copyvisual(n,m)
    setattr(n,a_visual,getattr(m,a_visual))
end

function visualizers.setvisual(n)
    texsetattribute(a_visual,setvisual(n,texgetattribute(a_visual)))
end

function visualizers.setlayer(n)
    texsetattribute(a_layer,layers[n] or unsetvalue)
end

local function set(mode,v)
    texsetattribute(a_visual,setvisual(mode,texgetattribute(a_visual),v))
end

for mode, value in next, modes do
    trackers.register(formatters["visualizers.%s"](mode), function(v) set(mode,v) end)
end

local fraction  = 10

trackers  .register("visualizers.reset",    function(v) set("reset", v) end)
trackers  .register("visualizers.all",      function(v) set("all",   v) end)
trackers  .register("visualizers.makeup",   function(v) set("makeup",v) end)
trackers  .register("visualizers.boxes",    function(v) set("boxes", v) end)
directives.register("visualizers.fraction", function(v) fraction = (v and tonumber(v)) or (v == "more" and 5) or 10 end)

-- we can just paste verbatim together .. no typesetting needed

-- experiment ... we can move the font definition away too

    local expandmacro = token.expand_macro
    local takebox     = tex.takebox

    local function hpack_string(str)
        expandmacro("syst_v_p",true,str)
        return tonut(takebox("scratchbox"))
    end

-- ... if okay it will replace the current hpack_string in node_typ

local function sometext(str,layer,color,textcolor,lap,variant)
    local text = hpack_string(str,usedfont)
    local size = getwidth(text)
    local rule = new_rule(size,2*exheight,exheight/2)
    local kern = new_kern(-size)
    if color then
        setcolor(rule,color)
    end
    if textcolor then
        setlistcolor(getlist(text),textcolor)
    end
    local info = setlink(rule,kern,text)
    setlisttransparency(info,"trace:g")
    info = hpack_nodes(info)
    local width = getwidth(info)
    if variant then
        setoffsets(info,0,variant*exheight)
    end
    if lap then
        info = new_hlist(setlink(new_kern(-width),info))
    else
        info = new_hlist(info) -- a bit overkill: double wrapped
    end
    if layer then
        setattr(info,a_layer,layer)
    end
    return info, width
end

local function someblob(str,layer,color,textcolor,width)
    local text = hpack_string(str,usedfont)
    local size = getwidth(text)
    local rule = new_rule(width,2*exheight,exheight/2)
    local kern = new_kern(-width + (width-size)/2)
    if color then
        setcolor(rule,color)
    end
    if textcolor then
        setlistcolor(getlist(text),textcolor)
    end
    local info = setlink(rule,kern,text)
    setlisttransparency(info,"trace:g")
    info = hpack_nodes(info)
    local width = getwidth(info)
    info = new_hlist(info)
    if layer then
        setattr(info,a_layer,layer)
    end
    return info, width
end

local caches = setmetatableindex("table")

local fontkern, italickern, marginkern, mathkern do

    local f_cache = caches["fontkern"]
    local i_cache = caches["italickern"]
    local s_cache = caches["shapekern"]
    local m_cache = caches["marginkern"]
    local l_cache = caches["mathkern"]

    local function somekern(head,current,cache,color,layer)
        local width = getkern(current)
        local extra = getexpansion(current)
        local kern  = width + extra
        local info  = cache[kern]
        if not info then
            local text = hpack_string(formatters[" %0.3f"](kern*pt_factor),usedfont)
            local rule = new_rule(emwidth/fraction,6*exheight,2*exheight)
            local list = getlist(text)
            if kern > 0 then
                setlistcolor(list,"trace:db")
            elseif kern < 0 then
                setlistcolor(list,"trace:dr")
            else
                setlistcolor(list,"trace:dg")
            end
            setlisttransparency(list,color)
            setcolor(rule,color)
            settransparency(rule,color)
            setshift(text,-5 * exheight)
            info = new_hlist(setlink(rule,text))
            setattr(info,a_layer,layer)
            f_cache[kern] = info
        end
        head = insertnodebefore(head,current,copylist(info))
        return head, current
    end

    fontkern = function(head,current)
        return somekern(head,current,f_cache,"trace:ds",l_fontkern)
    end

    italickern = function(head,current)
        return somekern(head,current,i_cache,"trace:do",l_italic)
    end

    mathshapekern = function(head,current)
        return somekern(head,current,s_cache,"trace:do",l_mathshapekern)
    end

    marginkern = function(head,current)
        return somekern(head,current,m_cache,"trace:do",l_marginkern)
    end

    mathkern = function(head,current)
        return somekern(head,current,l_cache,"trace:do",l_mathkern)
    end

end

local ruledglyphexpansion do

    local f_cache = caches["glyphexpansion"]

    ruledglyphexpansion = function(head,current)
        local extra = getexpansion(current)
        if extra and extra ~= 0 then
            extra = extra / 1000
            local info = f_cache[extra]
            if not info then
                local text = hpack_string(round(extra),usedfont)
                local rule = new_rule(emwidth/fraction,exheight,2*exheight)
                local list = getlist(text)
                if extra > 0 then
                    setlistcolor(list,"trace:db")
                elseif extra < 0 then
                    setlistcolor(list,"trace:dr")
                end
                setlisttransparency(list,"trace:ds")
                setcolor(rule,"trace:ds")
                settransparency(rule,"trace:ds")
                setshift(text,1.5 * exheight)
                info = new_hlist(setlink(rule,text))
                setattr(info,a_layer,l_expansion)
                f_cache[extra] = info
            end
            head = insertnodebefore(head,current,copylist(info))
            return head, current
        end
        return head, current
    end

end

local kernexpansion do

    local f_cache = caches["kernexpansion"]

    -- in mkiv we actually need to reconstruct but let's not do that now

    kernexpansion = function(head,current)
        local extra = getexpansion(current)
        if extra ~= 0 then
            extra = extra / 1000
            local info = f_cache[extra]
            if not info then
                local text = hpack_string(round(extra),usedfont)
                local rule = new_rule(emwidth/fraction,exheight,4*exheight)
                local list = getlist(text)
                if extra > 0 then
                    setlistcolor(list,"trace:db")
                elseif extra < 0 then
                    setlistcolor(list,"trace:dr")
                end
                setlisttransparency(list,"trace:ds")
                setcolor(rule,"trace:ds")
                settransparency(rule,"trace:ds")
                setshift(text,3.5 * exheight)
                info = new_hlist(setlink(rule,text))
                setattr(info,a_layer,l_expansion)
                f_cache[extra] = info
            end
            head = insertnodebefore(head,current,copylist(info))
            return head, current
        end
        return head, current
    end

end

local ruledmark do

    local set_code = nodes.markcodes.set

    local sm_cache = setmetatableindex(caches["setmark"], function(t,index)
        local info = sometext(formatters["SM:%i"](index),usedfont,nil,"trace:w") -- whatsit
        setattr(info,a_layer,l_mark)
        t[index] = info
        return info
    end)

    local rm_cache = setmetatableindex(caches["resetmark"], function(t,index)
        local info = sometext(formatters["RM:%i"](index),usedfont,nil,"trace:w") -- whatsit
        setattr(info,a_layer,l_mark)
        t[index] = info
        return info
    end)

    ruledmark = function(head,current)
        local index = getindex(current)
        local info  = getsubtype(current) == set_code and sm_cache[index] or rm_cache[index]
        head, current = insertnodeafter(head,current,copylist(info))
        return head, current
    end

end

local ruledinsert do

    local si_cache = setmetatableindex(caches["insert"], function(f,index)
        local info = sometext(formatters["SI:%i"](index),usedfont,nil,"trace:w") -- whatsit
        setattr(info,a_layer,l_insert)
        si_cache[index] = info
    end)

    ruledinsert = function(head,current)
        local info = si_cache[getindex(current)]
        head, current = insertnodeafter(head,current,copylist(info))
        return head, current
    end

end

local ruledwhatsit do

    local whatsitcodes = nodes.whatsitcodes
    local w_cache      = caches["whatsit"]

    local tags         = {
        [whatsitcodes.open]           = "OPN",
        [whatsitcodes.write]          = "WRI",
        [whatsitcodes.close]          = "CLS",
     -- [whatsitcodes.special]        = "SPE",
        [whatsitcodes.latelua]        = "LUA",
        [whatsitcodes.savepos]        = "POS",
        [whatsitcodes.userdefined]    = "USR",
        [whatsitcodes.literal]        = "LIT",
        [whatsitcodes.setmatrix]      = "MAT",
        [whatsitcodes.save]           = "SAV",
        [whatsitcodes.restore]        = "RES",
        [whatsitcodes.startscaling]   = "+SCA",
        [whatsitcodes.stopscaling]    = "-SCA",
        [whatsitcodes.startrotation]  = "+ROT",
        [whatsitcodes.stoprotation]   = "-ROT",
        [whatsitcodes.startmirroring] = "+MIR",
        [whatsitcodes.stopmirroring]  = "-MIR",
        [whatsitcodes.startclipping]  = "+CLP",
        [whatsitcodes.stopclipping]   = "-CLP",
        [whatsitcodes.startmatrix]    = "+MAT",
        [whatsitcodes.stopmatrix]     = "-MAT",
        [whatsitcodes.setstate]       = "SET",   -- can't happen because these are added after visualizing
    }

    ruledwhatsit = function(head,current)
        local what = getsubtype(current)
        local info = w_cache[what]
        if info then
            -- print("hit whatsit")
        else
            info = sometext(formatters["W:%s"](tags[what] or what),usedfont,nil,"trace:w")
            setattr(info,a_layer,l_whatsit)
            w_cache[what] = info
        end
        head, current = insertnodeafter(head,current,copylist(info))
        return head, current
    end

end

local ruledboundary do

    local boundarycodes = nodes.boundarycodes
    local b_cache       = caches["boundary"]

    local tags          = {
        [boundarycodes.cancel]     = "CAN",
        [boundarycodes.user]       = "USR",
        [boundarycodes.protrusion] = "PRO",
        [boundarycodes.word]       = "WRD",
    }

    ruledboundary = function(head,current)
        local what = getsubtype(current)
        local info = b_cache[what]
        if info then
            -- print("hit whatsit")
        else
            info = sometext(formatters["B:%s"](tags[what] or what),usedfont,nil,"trace:w")
            setattr(info,a_layer,l_boundary)
            b_cache[what] = info
        end
        head, current = insertnodeafter(head,current,copylist(info))
        return head, current
    end

end

local ruleddir, ruledpar do

    local dircodes     = nodes.dircodes
    local dirvalues    = nodes.dirvalues

    local cancel_code  = dircodes.cancel
    local l2r_code     = dirvalues.l2r
    local r2l_code     = dirvalues.r2l

    local d_cache      = caches["dir"]

    local getdirection = nuts.getdirection

    local tags = {
        l2r    = "L2R",
        r2l    = "R2L",
        cancel = "CAN",
        par    = "PAR",
    }

    ruledpar = function(head,current)
        local what = "par" -- getsubtype(current)
        local info = d_cache[what]
        if info then
            -- print("hit par")
        else
            info = sometext(formatters["L:%s"](what),usedfont,nil,"trace:w")
            setattr(info,a_layer,l_dir)
            d_cache[what] = info
        end
        head, current = insertnodeafter(head,current,copylist(info))
        return head, current
    end

    ruleddir = function(head,current)
        local what = getsubtype(current)
        if what == cancel_code then
            what = "cancel"
        elseif getdirection(current) == r2l_code then
            what = "r2l"
        else
            what = "l2r"
        end
        local info = d_cache[what]
        if info then
            -- print("hit dir")
        else
            info = sometext(formatters["D:%s"](what),usedfont,nil,"trace:w")
            setattr(info,a_layer,l_dir)
            d_cache[what] = info
        end
        head, current = insertnodeafter(head,current,copylist(info))
        return head, current
    end

end

local ruleduser do

    local u_cache = caches["user"]

    ruleduser = function(head,current)
        local what = getsubtype(current)
        local info = u_cache[what]
        if info then
            -- print("hit user")
        else
            info = sometext(formatters["U:%s"](what),usedfont)
            setattr(info,a_layer,l_user)
            u_cache[what] = info
        end
        head, current = insertnodeafter(head,current,copylist(info))
        return head, current
    end

end

local ruleddepth do

    ruleddepth = function(current,wd,ht,dp)
        local wd, ht, dp = getwhd(current)
        if dp ~= 0 then
            local rule = new_rule(wd,0,dp)
            setcolor(rule,"trace:o")
            settransparency(rule,"trace:g")
            setattr(rule,a_layer,l_depth)
            setlist(current,setlink(rule,new_kern(-wd),getlist(current)))
        end
    end

end

local ruledbox do

    local b_cache = caches["box"]
    local o_cache = caches["origin"]

    local getshift       = nuts.getshift
    local getorientation = nuts.getorientation
    local setorientation = nuts.setorientation
    local getheight      = nuts.getheight

    setmetatableindex(o_cache,function(t,size)
        local rule   = new_rule(2*size,size,size)
        local origin = hpack_nodes(rule)
        setcolor(rule,"trace:do")
        settransparency(rule,"trace:do")
        setattr(rule,a_layer,l_origin)
        t[size] = origin
        return origin
    end)

    ruledbox = function(head,current,vertical,layer,what,simple,previous,trace_origin,parent)
        local wd, ht, dp = getwhd(current)
--       local wd, ht, dh, shift = nuts.getlistdimensions(current) -- MAYBE
        local force_origin = wd == 0 or (dp + ht) == 0
        local shift = getshift(current)
-- print(getorientation(current,true))
        local orientation, xoffset, yoffset, w, h, d = getorientation(current) -- TODO
if orientation and orientation ~= 0 and (h ~= 0 or d ~= 0) then
--     wd = w
    ht = h
    dp = d
end
        local next = getnext(current)
        local prev = previous
        setboth(current)
        local linewidth = emwidth/fraction
        local size      = 2*linewidth
        local this
        if not simple then
            this = b_cache[what]
            if not this then
                local text = hpack_string(what,usedfont)
                this = setlink(new_kern(-getwidth(text)),text)
                setlisttransparency(this,"trace:s")
                this = new_hlist(this)
                b_cache[what] = this
            end
        end
        local rest, more
        if force_origin then
            rest = emptyrule(wd,ht,dp) -- we accept some overhead
        elseif what == "_D_" then
            -- also the other line
            local up = nil
            local list = getlist(current)
            if list then
                up = getheight(list)
            end
            rest = userrule {
                width  = wd,
                height = ht,
                depth  = dp,
                line   = linewidth,
                type   = "box",
                dashed = 3*size,
                double = ht - up,
            }
        elseif dp == 0 then
            rest = userrule {
                width    = wd,
                height   = ht,
                line     = linewidth,
                type     = "box",
                baseline = false,
            }
        else
            rest = userrule {
                width  = wd,
                height = ht,
                depth  = dp,
                line   = linewidth,
                type   = "box",
                dashed = 3*size,
            }
        end
        --
        local info = setlink(this and copylist(this) or nil,rest,more)
        --
        setlisttransparency(info,"trace:s")
        info = new_hlist(info) -- important
        --
        setattr(info,a_layer,layer)
        if vertical then
            if not force_origin and shift == 0 then
                info = setlink(current,dp ~= 0 and new_kern(-dp) or nil,info)
            elseif trace_origin or force_origin then
                local size   = 2*size
                local origin = o_cache[size]
                origin = copylist(origin)
                if getid(parent) == vlist_code then
                    setshift(origin,-shift)
                    info = setlink(current,new_kern(-size),origin,new_kern(-size-dp),info)
                else
                    -- todo .. i need an example
                    info = setlink(current,dp ~= 0 and new_kern(-dp) or nil,info)
                end
                setshift(current,0)
            else
                info = setlink(current,new_dp ~= 0 and new_kern(-dp) or nil,info)
                setshift(current,0)
            end
            info = new_vlist(info,wd,ht,dp,shift)
        else
            if not force_origin and shift == 0 then
                info = setlink(current,new_kern(-wd),info)
            elseif trace_origin or force_origin then
                local size   = 2*size
                local origin = o_cache[size]
                origin = copylist(origin)
                if getid(parent) == vlist_code then
                    info = setlink(current,new_kern(-wd-size-shift),origin,new_kern(-size+shift),info)
                else
                    setshift(origin,-shift)
                    info = setlink(current,new_kern(-wd-size),origin,new_kern(-size),info)
                end
                setshift(current,0)
            else
                info = setlink(current,new_kern(-wd),info)
                setshift(current,0)
            end
            info = new_hlist(info,wd,ht,dp,shift)
        end
-- if orientation then
--     setorientation(current,0,0)
--     setorientation(info,orientation,xoffset,yoffset)
-- end
        if next then
            setlink(info,next)
        end
        if prev and prev > 0 then
            setlink(prev,info)
        end
        if head == current then
            return info, info
        else
            return head, info
        end
    end

end

local ruledglyph do

    -- see boundingbox feature .. maybe a pdf stream is more efficient, after all we
    -- have a frozen color anyway or i need a more detailed cache .. below is a more
    -- texie approach

 -- local ligature_code = 0x8000 + nodes.glyphcodes.ligature

    ruledglyph = function(head,current,previous) -- wrong for vertical glyphs
        local wd, ht, dp = getglyphdimensions(current)
        if wd ~= 0 then
            local next = getnext(current)
            local prev = previous
            setboth(current)
            local linewidth = emwidth/(2*fraction)
            local x_offset, y_offset, l_margin, r_margin, raise = getoffsets(current)
--             local info = setlink((dp == 0 and outlinerule and outlinerule(wd,ht,dp,linewidth)) or userrule {
--                 width  = wd,
--                 height = ht,
--                 depth  = dp,
--                 line   = linewidth,
--                 type   = "box",
--             },new_kern(-wd))
-- if ht == 0 and dp == 0 then
--     ht = linewidth/2
--     dp = ht
-- end
            local info = (dp == 0 and outlinerule and outlinerule(wd,ht,dp,linewidth)) or userrule {
                width  = wd,
                height = ht,
                depth  = dp,
                line   = linewidth,
                type   = "box",
            }
            local c, f = isglyph(current)
            local char = chardata[f][c]
            if char and type(char.unicode) == "table" then -- hackery test
                setlistcolor(info,"trace:s")
                setlisttransparency(info,"trace:ds")
            else
                setlistcolor(info,"trace:o")
                setlisttransparency(info,"trace:do")
            end
            info = new_hlist(info)
            setattr(info,a_layer,l_glyph)
            local info = setlink(current,new_kern(-wd),info)
            info = hpack_nodes(info)
            setwidth(info,wd)
            if next then
                setlink(info,next)
            end
            if prev then
                setlink(prev,info)
            end
            if head == current then
                return info, info
            else
                return head, info
            end
        else
            return head, current
        end
    end

    function visualizers.setruledglyph(f)
        ruledglyph = f or ruledglyph
    end

end

local ruledglue, ruledmathglue do

    local effectiveglue         = nuts.effectiveglue
    local iszeroglue            = nuts.iszeroglue

    local gluecodes             = nodes.gluecodes

    local userskip_code         = gluecodes.userskip
    local spaceskip_code        = gluecodes.spaceskip
    local xspaceskip_code       = gluecodes.xspaceskip
    local zerospaceskip_code    = gluecodes.zerospaceskip or gluecodes.userskip
 -- local keepskip_code         = gluecodes.keepskip or gluecodes.userskip
    local leftskip_code         = gluecodes.leftskip
    local rightskip_code        = gluecodes.rightskip
    local lefthangskip_code     = gluecodes.lefthangskip
    local righthangskip_code    = gluecodes.righthangskip
    local parfillleftskip_code  = gluecodes.parfillleftskip or parfillskip_code
    local parfillrightskip_code = gluecodes.parfillrightskip or parfillskip_code
    local parinitleftskip_code  = gluecodes.parinitleftskip
    local parinitrightskip_code = gluecodes.parinitrightskip
    local indentskip_code       = gluecodes.indentskip
    local intermathskip_code    = gluecodes.intermathskip
    local correctionskip_code   = gluecodes.correctionskip
    local tabskip_code          = gluecodes.tabskip

    local g_cache_v  = caches["vglue"]
    local g_cache_h  = caches["hglue"]

    local g_cache_gn = caches["gluename"]
    local g_cache_gz = caches["gluezero"]
    local g_cache_gf = caches["gluefixed"]

    local tags = {
     -- [userskip_code]                   = "US",
        [gluecodes.lineskip]              = "LI",
        [gluecodes.baselineskip]          = "BS",
        [gluecodes.parskip]               = "PS",
        [gluecodes.abovedisplayskip]      = "DA",
        [gluecodes.belowdisplayskip]      = "DB",
        [gluecodes.abovedisplayshortskip] = "SA",
        [gluecodes.belowdisplayshortskip] = "SB",
        [gluecodes.topskip]               = "TS",
        [gluecodes.splittopskip]          = "ST",
        [tabskip_code]                    = "TB",
        [gluecodes.thinmuskip]            = "MS",
        [gluecodes.medmuskip]             = "MM",
        [gluecodes.thickmuskip]           = "ML",
        [intermathskip_code]              = "IM",
        [gluecodes.keepskip or 99]        = "KS",
        [gluecodes.mathskip]              = "MT",
        [gluecodes.leaders]               = "NL",
        [gluecodes.cleaders]              = "CL",
        [gluecodes.xleaders]              = "XL",
        [gluecodes.gleaders]              = "GL",
     -- true                              = "VS",
     -- false                             = "HS",
        [leftskip_code]                   = "LS",
        [rightskip_code]                  = "RS",
        [lefthangskip_code]               = "LH",
        [righthangskip_code]              = "RH",
        [spaceskip_code]                  = "SP",
        [xspaceskip_code]                 = "XS",
        [zerospaceskip_code]              = "ZS",
        [parfillleftskip_code]            = "PL",
        [parfillrightskip_code]           = "PR",
        [parinitleftskip_code]            = "IL",
        [parinitrightskip_code]           = "IR",
        [indentskip_code]                 = "IN",
        [correctionskip_code]             = "CS",
    }

    local stags = {
        [lefthangskip_code]     =  0.5,
        [righthangskip_code]    =  0.5,
        [leftskip_code]         = -2,
        [rightskip_code]        = -2,
        [parinitleftskip_code]  = -1.3775,
        [parinitrightskip_code] = -1.3775,
        [parfillleftskip_code]  = -0.75,
        [parfillrightskip_code] = -0.75,
    }

    local f_amount = formatters["%s:%0.3f"]

--     ruledglue = function(head,current,vertical,parent)
--         local subtype = getsubtype(current)
--         local width   = effectiveglue(current,parent)
--         local stag    = stags[subtype]
--         local amount  = f_amount(tags[subtype] or (vertical and "VS") or "HS",width*pt_factor) -- can be sped up
--         local info    = (vertical and g_cache_v or g_cache_h)[amount]
--         if subtype == intermathskip_code then
--             head = ruledmathglue(head, current)
--         end
--         if info then
--             -- print("glue hit")
--         else
--             if subtype == spaceskip_code or subtype == xspaceskip_code or subtype == zerospaceskip_code then
--                 info = sometext(amount,l_glue,"trace:y")
--             elseif subtype == userskip_code then
--                 if width > 0 then
--                     info = sometext(amount,l_glue,"trace:b")
--                 elseif width < 0 then
--                     info = sometext(amount,l_glue,"trace:r")
--                 else
--                     info = sometext(amount,l_glue,"trace:g")
--                 end
--             elseif subtype == tabskip_code then
--                 info = sometext(amount,l_glue,"trace:s")
--             elseif subtype == indentskip_code or subtype == correctionskip_code then
--                 info = sometext(amount,l_glue,"trace:s")
--             elseif subtype == leftskip_code then
--                 info = sometext(amount,l_glue,"trace:y",false,true,stag)
--             elseif subtype == rightskip_code then
--                 info = sometext(amount,l_glue,"trace:y",false,false,stag)
--             elseif subtype == lefthangskip_code then
--                 info = sometext(amount,l_glue,"trace:y",false,true,stag)
--             elseif subtype == righthangskip_code then
--                 info = sometext(amount,l_glue,"trace:y",false,false,stag)
--             elseif subtype == parfillleftskip_code then
--                 info = sometext(amount,l_glue,"trace:s",false,true,stag)
--             elseif subtype == parfillrightskip_code then
--                 info = sometext(amount,l_glue,"trace:s",false,false,stag)
--             elseif subtype == parinitleftskip_code then
--                 info = sometext(amount,l_glue,"trace:s",false,true,stag)
--             elseif subtype == parinitrightskip_code then
--                 info = sometext(amount,l_glue,"trace:s",false,false,stag)
--             else
--                 info = sometext(amount,l_glue,"trace:m")
--             end
--             (vertical and g_cache_v or g_cache_h)[amount] = info
--         end
--         info = copylist(info)
--         if vertical then
--             info = vpack_nodes(info)
--         end
--         head, current = insertnodebefore(head,current,info)
--         return head, getnext(current)
--     end

    local g_caches = { } for k, v in next, tags do g_caches[k] = caches[v] end

    ruledglue = function(head,current,vertical,parent)
        local subtype = getsubtype(current)
        local width   = effectiveglue(current,parent)
        local stag    = stags[subtype]
        local cache   = g_caches[subtype] or (vertical and g_cache_v) or g_cache_h
        local info    = cache[amount]
        if subtype == intermathskip_code then
            head = ruledmathglue(head, current)
        end
        if info then
            -- print("glue hit")
        else
            local amount = f_amount(tags[subtype] or (vertical and "VS") or "HS",width*pt_factor)
            if subtype == spaceskip_code or subtype == xspaceskip_code or subtype == zerospaceskip_code then
                info = sometext(amount,l_glue,"trace:y")
            elseif subtype == userskip_code then
                if width > 0 then
                    info = sometext(amount,l_glue,"trace:b")
                elseif width < 0 then
                    info = sometext(amount,l_glue,"trace:r")
                else
                    info = sometext(amount,l_glue,"trace:g")
                end
            elseif subtype == tabskip_code then
                info = sometext(amount,l_glue,"trace:s")
            elseif subtype == indentskip_code or subtype == correctionskip_code then
                info = sometext(amount,l_glue,"trace:s")
            elseif subtype == leftskip_code then
                info = sometext(amount,l_glue,"trace:y",false,true,stag)
            elseif subtype == rightskip_code then
                info = sometext(amount,l_glue,"trace:y",false,false,stag)
            elseif subtype == lefthangskip_code then
                info = sometext(amount,l_glue,"trace:y",false,true,stag)
            elseif subtype == righthangskip_code then
                info = sometext(amount,l_glue,"trace:y",false,false,stag)
            elseif subtype == parfillleftskip_code then
                info = sometext(amount,l_glue,"trace:s",false,true,stag)
            elseif subtype == parfillrightskip_code then
                info = sometext(amount,l_glue,"trace:s",false,false,stag)
            elseif subtype == parinitleftskip_code then
                info = sometext(amount,l_glue,"trace:s",false,true,stag)
            elseif subtype == parinitrightskip_code then
                info = sometext(amount,l_glue,"trace:s",false,false,stag)
            else
                info = sometext(amount,l_glue,"trace:m")
            end
            cache[amount] = info
        end
        info = copylist(info)
        if vertical then
            info = vpack_nodes(info)
        end
        head, current = insertnodebefore(head,current,info)
        return head, getnext(current)
    end

    local g_cache_s = caches["space"]
    local g_cache_x = caches["xspace"]

    ruledspace = function(head,current,parent)
        local subtype = getsubtype(current)
        local width = effectiveglue(current,parent)
        local info
        if subtype == spaceskip_code then
            info = g_cache_s[width]
            if not info then
                info = someblob("SP",l_glue,"trace:y",nil,width)
                g_cache_s[width] = info
            end
        else
            info = g_cache_x[width]
            if not info then
                info = someblob("XS",l_glue,"trace:m",nil,width)
                g_cache_x[width] = info
            end
        end
        info = copylist(info)
        head, current = insertnodebefore(head,current,info)
        return head, getnext(current)
    end

    -- we sometimes pass previous as we can have issues in math (not watertight for all)

    local mathvalues = nodes.mathvalues

    mathvalues[-1] = "left"
    mathvalues[-2] = "right"

    local morehack = setmetatableindex(function(t,k)
        local v = mathematics.classnames[k] -- yet unknown
        v = v and string.sub(v,1,3) or string.formatters["x%02x"](k)
        t[k] = v
        return v
    end)

    local temphack = setmetatableindex(function(t,k)
        local v = mathvalues[k]
        if v then
            v = gsub(v,"spacing","") -- old, quads and so
        else
            v = k - 256
            v = morehack[v//64] .. morehack[v%64]
        end
        t[k] = v
        return v
    end)

    local g_cache_qd = caches["mathquad"]

    ruledmathglue = function(head,current,parent)
        local name  = getfont(current)
        local zero  = iszeroglue(current)
        local fixed = getprop(current,"fixedmathalign")
        local color = false
        local info  = zero and g_cache_gz[name]
        local quad  = name == 0
        local width = quad and effectiveglue(current,parent)
        if not info then
            if quad then
                info  = g_cache_qd[width]
                color = "trace:z"
            elseif fixed then
                info  = g_cache_gf[name]
                color = "trace:dr"
            else
                info  = g_cache_gn[name]
                color = "trace:z"
            end
        end
        if not info then
            local amount = quad and f_amount("QUAD",width*pt_factor) or temphack[name]
         -- local amount = quad and f_amount("QD",width*pt_factor) or temphack[name]
            local text   = hpack_string(amount,usedfont)
            local rule   = new_rule(emwidth/fraction,2*exheight,(zero and 4.25 or 2.75)*exheight)
            local list   = getlist(text)
            setlistcolor(list,color)
            setcolor(rule,color)
            setlisttransparency(list,color)
            settransparency(rule,color)
            setshift(text,(zero and 3.5 or 2)*exheight)
            info = new_hlist(setlink(rule,text))
            setattr(info,a_layer,l_glue)
            if quad then
                g_cache_qd[width] = info
            elseif fixed then
                g_cache_gf[name] = info
            else
                g_cache_gn[name] = info
            end
        end
        return insertnodebefore(head,current,copylist(info))
    end

end

local ruledkern do

    local v_cache = caches["vkern"]
    local h_cache = caches["hkern"]

    ruledkern = function(head,current,vertical)
        local kern  = getkern(current)
        local cache = vertical and v_cache or h_cache
        local info  = cache[kern]
        if not info then
            local amount = formatters["%s:%0.3f"](vertical and "VK" or "HK",kern*pt_factor)
            if kern > 0 then
                info = sometext(amount,l_kern,"trace:b")
            elseif kern < 0 then
                info = sometext(amount,l_kern,"trace:r")
            else
                info = sometext(amount,l_kern,"trace:g")
            end
            cache[kern] = info
        end
        info = copylist(info)
        if vertical then
            info = vpack_nodes(info)
        end
        head, current = insertnodebefore(head,current,info)
        return head, getnext(current)
    end

end

local ruledstrut do

    local strut_size      = 65536 * 8 / 10
    local strut_code      = nodes.rulecodes.strut
    local math_code       = nodes.nodecodes.math
    local traverseid      = nuts.traverseid
    local rangedimensions = nuts.rangedimensions
    local a_mathaxis      = attributes.private("mathaxis")

    ruledstrut = function(head,current,parent)
        if getwidth(current) == 0 then
            if getsubtype(current) == strut_code  then
                local w = strut_size
                local a = getattr(current,a_mathaxis)
                setattr(current,a_layer,l_strut)
                if a then
                    local p = getprev(current)
                    local b, e
                    for n in traverseid(math_code,current) do
                        e = n
                        break
                    end
                    for n in traverseid(math_code,current,true) do
                        b = n
                        break
                    end
                    if not b then
                        b = head
                    end
                    if not e then
                        e = nuts.tail(b)
                    end
                    w = rangedimensions(parent,b,e)
                    setwidth(current,w)
                    setcolor(current,"trace:ds")
                    settransparency(current,"trace:ds")
                    head, current, rule = nuts.remove(head,current)
                    local kern = new_kern(-w)
                    if a == 2 then
                        head = insertnodebefore(head,e,kern)
                        head = insertnodebefore(head,e,rule)
                    else
                        insertnodeafter(head,b,kern)
                        insertnodeafter(head,b,rule)
                    end
                    current = p
                else
                    setwidth(current,w)
                    head, current = insertnodeafter(head,current,new_kern(-w))
                end
            end
        end
        return head, current
    end

end

local ruleditalic do

    local i_cache = caches["italic"]

    ruleditalic = function(head,current)
        local kern = getkern(current)
        local info = i_cache[kern]
        if not info then
            local amount = formatters["%s:%0.3f"]("IC",kern*pt_factor)
            if kern > 0 then
                info = sometext(amount,l_kern,"trace:b")
            elseif kern < 0 then
                info = sometext(amount,l_kern,"trace:r")
            else
                info = sometext(amount,l_kern,"trace:g")
            end
            i_cache[kern] = info
        end
        info = copylist(info)
        head, current = insertnodebefore(head,current,info)
        return head, getnext(current)
    end

end

local ruledmarginkern do

    local l_cache = caches["leftmarginkern"]
    local r_cache = caches["rightmarginkern"]

    ruledmarginkern = function(head,current,subtype)
        local kern  = getkern(current)
        local left  = subtype == leftmarginkern_code
        local cache = left and l_cache or r_cache
        local info  = cache[kern]
        if not info then
            local amount = formatters["%s:%0.3f"](left and "ML" or "MR",kern*pt_factor)
            if kern > 0 then
                info = sometext(amount,l_marginkern,"trace:b")
            elseif kern < 0 then
                info = sometext(amount,l_marginkern,"trace:r")
            else
                info = sometext(amount,l_marginkern,"trace:g")
            end
            cache[kern] = info
        end
        info = copylist(info)
        head, current = insertnodebefore(head,current,info)
        return head, getnext(current)
    end

end

local ruledmathkern do

    local h_cache = caches["horizontalmathkern"]
    local v_cache = caches["verticalmathkern"]

    ruledmathkern = function(head,current,vertical)
        local kern  = getkern(current)
        local cache = vertical and v_cache or h_cache
        local info  = cache[kern]
        if not info then
            local amount = formatters["%s:%0.3f"](vertical and "MV" or "MH",kern*pt_factor)
            if kern > 0 then
                info = sometext(amount,l_mathkern,"trace:b")
            elseif kern < 0 then
                info = sometext(amount,l_mathkern,"trace:r")
            else
                info = sometext(amount,l_mathkern,"trace:g")
            end
            cache[kern] = info
        end
        info = copylist(info)
        head, current = insertnodebefore(head,current,info)
        return head, getnext(current)
    end

end

local ruleddiscretionary do

    local d_cache = caches["discretionary"]

    ruleddiscretionary = function(head,current)
        local d = d_cache[true]
        if not the_discretionary then
            local rule = new_rule(4*emwidth/fraction,4*exheight,exheight)
            local kern = new_kern(-2*emwidth/fraction)
            setlink(kern,rule)
            setcolor(rule,"trace:dd")
            settransparency(rule,"trace:dd")
            setattr(rule,a_layer,l_discretionary)
            d = new_hlist(kern)
            d_cache[true] = d
        end
        insertnodeafter(head,current,copylist(d))
        return head, current
    end

end

local ruledpenalty do

    local p_cache_v = caches["vpenalty"]
    local p_cache_h = caches["hpenalty"]
    local p_cache_m = caches["mpenalty"]

    local raisepenalties = false

    ----- getpenalty        = nuts.getpenalty
    local pre_penalty_code  = nodes.penaltycodes.mathprepenalty
    local post_penalty_code = nodes.penaltycodes.mathpostpenalty

    directives.register("visualizers.raisepenalties",function(v) raisepenalties = v end)

    ruledpenalty = function(head,current,vertical,subtype)
        local penalty = getpenalty(current)
        local ismath  = subtype == pre_penalty_code or subtype == post_penalty_code or subtype == true
        local cache   = (ismath and p_cache_m) or (vertical and p_cache_v) or p_cache_h
        local info    = cache[penalty]
        if info then
            -- print("penalty hit")
        else
            local amount = formatters["%s:%s"]((ismath and "MP") or (vertical and "VP") or "HP",penalty)
            if ismath then
                info = sometext(amount,l_penalty,"trace:s")
            elseif penalty > 0 then
                info = sometext(amount,l_penalty,"trace:b")
            elseif penalty < 0 then
                info = sometext(amount,l_penalty,"trace:r")
            else
                info = sometext(amount,l_penalty,"trace:g")
            end
            cache[penalty] = info
        end
        info = copylist(info)
        if vertical then
            info = vpack_nodes(info)
        elseif ismath then
            setshift(info, 65536*4)
        elseif raisepenalties then
            setshift(info,-65536*4)
        end
        head, current = insertnodebefore(head,current,info)
        return head, getnext(current)
    end

end

local ruledmath do

    local mathcodes = nodes.mathcodes
    local m_cache   = {
        beginmath = caches["bmath"],
        endmath   = caches["emath"],
    }
    local tags      = {
        beginmath = "B",
        endmath   = "E",
    }

    ruledmath = function(head,current)
        local what = getsubtype(current)
        local tag  = mathcodes[what]
        local skip = getkern(current) + getwidth(current) -- surround
        local info = m_cache[tag][skip]
        if info then
            -- print("hit math")
        else
            local text, width = sometext(formatters["M:%s"](tag and tags[tag] or what),usedfont,nil,"trace:dr")
            local rule = new_rule(skip,-655360/fraction,2*655360/fraction)
            local dist = tag == "beginmath" and width or skip
            setcolor(rule,"trace:dr")
            settransparency(rule,"trace:dr")
            setattr(rule,a_layer,l_math)
            info = new_hlist(setlink(new_glue(-skip),rule,new_glue(-dist),text))
            setattr(info,a_layer,l_math)
            m_cache[tag][skip] = info
        end
        local saved = current
        head, current = insertnodeafter(head,current,copylist(info))
        if getpenalty(saved) ~= 0 then
            head, current = ruledpenalty(head,saved,false,true)
        end
        return head, current
    end

end

do

    local nodecodes               = nodes.nodecodes
    local disc_code               = nodecodes.disc
    local kern_code               = nodecodes.kern
    local glyph_code              = nodecodes.glyph
    local glue_code               = nodecodes.glue
    local penalty_code            = nodecodes.penalty
    local whatsit_code            = nodecodes.whatsit
    local user_code               = nodecodes.user
    local math_code               = nodecodes.math
    local hlist_code              = nodecodes.hlist
    local vlist_code              = nodecodes.vlist
    local dir_code                = nodecodes.dir
    local par_code                = nodecodes.par
    local mark_code               = nodecodes.mark
    local insert_code             = nodecodes.insert
    local rule_code               = nodecodes.rule
    local boundary_code           = nodecodes.boundary

    local kerncodes               = nodes.kerncodes
    local fontkern_code           = kerncodes.fontkern
    local italickern_code         = kerncodes.italiccorrection
    local leftmarginkern_code     = kerncodes.leftmarginkern
    local rightmarginkern_code    = kerncodes.rightmarginkern
    local mathshapekern_code      = kerncodes.mathshapekern
    local horizontalmathkern_code = kerncodes.horizontalmathkern
    local verticalmathkern_code   = kerncodes.verticalmathkern
    ----- userkern_code           = kerncodes.userkern

    local skipcodes               = nodes.skipcodes
    local spaceskip_code          = skipcodes.spaceskip
    local xspaceskip_code         = skipcodes.xspaceskip
    local zerospaceskip_code      = skipcodes.zerospaceskip
    local intermathskip_code      = skipcodes.intermathskip

    local listcodes               = nodes.listcodes
    local linelist_code           = listcodes.line
    local rowlist_code            = listcodes.alignment

    local vtop_package_state      = 3 -- todo: symbolic
    local dbox_package_state      = 4 -- todo: symbolic

    local getleader               = nuts.getleader
    local getdisc                 = nuts.getdisc

    local setleader               = nuts.setleader
    local setdisc                 = nuts.setdisc
    local hpack                   = nuts.hpack

 -- local cache

    local function visualize(head,vertical,forced,parent)
        local trace_hbox            = false
        local trace_vbox            = false
        local trace_vtop            = false
        local trace_kern            = false
        local trace_glue            = false
        local trace_penalty         = false
        local trace_fontkern        = false
        local trace_strut           = false
        local trace_whatsit         = false
        local trace_glyph           = false
        local trace_simple          = false
        local trace_user            = false
        local trace_math            = false
        local trace_mathkern        = false
        local trace_marginkern      = false
        local trace_italic          = false
        local trace_origin          = false
        local trace_discretionary   = false
        local trace_expansion       = false
        local trace_line            = false
        local trace_space           = false
        local trace_depth           = false
        local trace_dir             = false
        local trace_par             = false
        local trace_mathglue        = false
        local trace_mark            = false
        local trace_insert          = false
        local trace_boundary        = false
        local current               = head
        local previous              = nil
        local attr                  = unsetvalue
        local prev_trace_fontkern   = nil
        local prev_trace_italic     = nil
        local prev_trace_expansion  = nil

        while current do
            local id = getid(current)
            local a = forced or getattr(current,a_visual) or unsetvalue
            local subtype, content
            if a ~= attr then
                prev_trace_fontkern  = trace_fontkern
                prev_trace_italic    = trace_italic
                prev_trace_expansion = trace_expansion
                attr = a
                if a == unsetvalue then
                    trace_hbox          = false
                    trace_vbox          = false
                    trace_vtop          = false
                    trace_kern          = false
                    trace_glue          = false
                    trace_penalty       = false
                    trace_fontkern      = false
                    trace_strut         = false
                    trace_whatsit       = false
                    trace_glyph         = false
                    trace_simple        = false
                    trace_user          = false
                    trace_math          = false
                    trace_italic        = false
                    trace_origin        = false
                    trace_discretionary = false
                    trace_expansion     = false
                    trace_line          = false
                    trace_space         = false
                    trace_depth         = false
                    trace_marginkern    = false
                    trace_mathkern      = false
                    trace_dir           = false
                    trace_par           = false
                    trace_mathglue      = false
                    trace_mark          = false
                    trace_insert        = false
                    trace_boundary      = false
                    if id == kern_code then
                        goto kern
                    else
                        goto list
                    end
                else -- we need them to be booleans
                 -- cache[a]()
                    trace_hbox          = a & 0x0000001 ~= 0
                    trace_vbox          = a & 0x0000002 ~= 0
                    trace_vtop          = a & 0x0000004 ~= 0
                    trace_kern          = a & 0x0000008 ~= 0
                    trace_glue          = a & 0x0000010 ~= 0
                    trace_penalty       = a & 0x0000020 ~= 0
                    trace_fontkern      = a & 0x0000040 ~= 0
                    trace_strut         = a & 0x0000080 ~= 0
                    trace_whatsit       = a & 0x0000100 ~= 0
                    trace_glyph         = a & 0x0000200 ~= 0
                    trace_simple        = a & 0x0000400 ~= 0
                    trace_user          = a & 0x0000800 ~= 0
                    trace_math          = a & 0x0001000 ~= 0
                    trace_italic        = a & 0x0002000 ~= 0
                    trace_origin        = a & 0x0004000 ~= 0
                    trace_discretionary = a & 0x0008000 ~= 0
                    trace_expansion     = a & 0x0010000 ~= 0
                    trace_line          = a & 0x0020000 ~= 0
                    trace_space         = a & 0x0040000 ~= 0
                    trace_depth         = a & 0x0080000 ~= 0
                    trace_marginkern    = a & 0x0100000 ~= 0
                    trace_mathkern      = a & 0x0200000 ~= 0
                    trace_dir           = a & 0x0400000 ~= 0
                    trace_par           = a & 0x0800000 ~= 0
                    trace_mathglue      = a & 0x1000000 ~= 0
                    trace_mark          = a & 0x2000000 ~= 0
                    trace_insert        = a & 0x4000000 ~= 0
                    trace_boundary      = a & 0x8000000 ~= 0
                end
            elseif a == unsetvalue then
                goto list
            end
         -- if trace_strut then
         --     setattr(current,a_layer,l_strut)
         -- else
            if id == glyph_code then
                if trace_glyph then
                    head, current = ruledglyph(head,current,previous)
                end
                if trace_expansion then
                    head, current = ruledglyphexpansion(head,current)
                end
            elseif id == disc_code then
                if trace_discretionary then
                    head, current = ruleddiscretionary(head,current)
                end
                local pre, post, replace = getdisc(current)
                if pre then
                    pre = visualize(pre,false,a,parent)
                end
                if post then
                    post = visualize(post,false,a,parent)
                end
                if replace then
                    replace = visualize(replace,false,a,parent)
                end
                setdisc(current,pre,post,replace)
            elseif id == kern_code then
                goto kern
            elseif id == glue_code then
                goto glue
            elseif id == penalty_code then
                if trace_penalty then
                    subtype = getsubtype(current)
                    head, current = ruledpenalty(head,current,vertical,subtype)
                end
            elseif id == hlist_code or id == vlist_code then
                goto list
            elseif id == rule_code then
                if trace_strut then
                    head, current = ruledstrut(head,current,parent)
                end
            elseif id == whatsit_code then
                if trace_whatsit then
                    head, current = ruledwhatsit(head,current)
                end
            elseif id == user_code then
                if trace_user then
                    head, current = ruleduser(head,current)
                end
            elseif id == math_code then
                local saved = current
                if trace_math then
                    head, current = ruledmath(head,current)
                end
            elseif id == dir_code then
                if trace_dir then
                    head, current = ruleddir(head,current)
                end
            elseif id == par_code then
                if trace_par then
                    head, current = ruledpar(head,current)
                end
            elseif id == mark_code then
                if trace_mark then
                    head, current = ruledmark(head,current)
                end
            elseif id == insert_code then
                if trace_insert then
                    head, current = ruledinsert(head,current)
                end
            elseif id == boundary_code then
                if trace_boundary then
                    head, current = ruledboundary(head,current)
                end
            end
            goto next
            ::glue::
            content = getleader(current)
            if content then
             -- setleader(current,hpack(visualize(content,false,nil,parent)))
                setleader(current,visualize(content,false,nil,parent))
            elseif trace_glue then
                head, current = ruledglue(head,current,vertical,parent)
            else
                subtype = getsubtype(current)
                if subtype == spaceskip_code or subtype == xspaceskip_code or subtype == zerospaceskip_code then -- not yet all space
                    if trace_space then
                        head, current = ruledspace(head,current,parent)
                    end
                elseif subtype == intermathskip_code then
                    if trace_math or trace_mathglue then
                        head = ruledmathglue(head,current,parent)
                    end
                end
            end
            goto next
            ::kern::
            subtype = getsubtype(current)
            if subtype == fontkern_code then
                if trace_fontkern or prev_trace_fontkern then
                    head, current = fontkern(head,current)
                end
                if trace_expansion or prev_trace_expansion then
                    head, current = kernexpansion(head,current)
                end
            elseif subtype == italickern_code then
                if trace_italic or prev_trace_italic then
                    head, current = italickern(head,current)
                elseif trace_kern then
                    head, current = ruleditalic(head,current)
                end
            elseif subtype == leftmarginkern_code or subtype == rightmarginkern_code then
                if trace_marginkern then
                    head, current = marginkern(head,current)
                elseif trace_kern then
                    head, current = ruledmarginkern(head,current,subtype)
                end
            elseif subtype == verticalmathkern_code then
                if trace_mathkern or trace_kern then
                    head, current = ruledmathkern(head,current,true)
                end
            elseif subtype == horizontalmathkern_code then
                if trace_mathkern then
                    head, current = mathkern(head,current)
                elseif trace_kern then
                    head, current = ruledmathkern(head,current,false)
                end
            elseif subtype == mathshapekern_code then
                if trace_mathkern or trace_italic then
                    head, current = mathshapekern(head,current)
                elseif trace_kern then
                    head, current = ruledmathkern(head,current,false)
                end
            else
                if trace_kern then
                    head, current = ruledkern(head,current,vertical)
                end
            end
            goto next;
            ::list::
            if id == hlist_code then
                local content = getlist(current)
                if content then
                    setlist(current,visualize(content,false,nil,current))
                end
                if trace_depth then
                    ruleddepth(current)
                end
                if trace_line and (getsubtype(current) == linelist_code or getsubtype(current) == rowlist_code) then
                    head, current = ruledbox(head,current,false,l_line,"L__",trace_simple,previous,trace_origin,parent)
                elseif trace_hbox then
                    head, current = ruledbox(head,current,false,l_hbox,"H__",trace_simple,previous,trace_origin,parent)
                end
            elseif id == vlist_code then
                local content = getlist(current)
                local state   = getstate(current)
                local isvtop  = state == vtop_package_state
                local isdbox  = state == dbox_package_state
                local tag     = nil
                local layer   = nil
                if content then
                    setlist(current,visualize(content,true,nil,current))
                end
                if trace_vtop then
                    if isdbox then
                        tag   = "_D_"
                        layer = l_vtop
                    elseif isvtop then
                        tag   = "_T_"
                        layer = l_vtop
                    elseif trace_vbox then
                        tag   = "__V"
                        layer = l_vbox
                    end
                elseif trace_vbox then
                    if isdbox then
                        tag   = "_D_"
                        layer = l_vtop
                    elseif not isvtop then
                        tag   = "__V"
                        layer = l_vbox
                    end
                end
                if tag then
                    head, current = ruledbox(head,current,true,layer,tag,trace_simple,previous,trace_origin,parent)
                end
            end
            ::next::
            previous = current
            current  = getnext(current)
        end
        return head
    end

    local function cleanup()
        for tag, cache in next, caches do
            for k, v in next, cache do
                flushnodelist(v)
            end
        end
        cleanup = function()
            report_visualize("error, duplicate cleanup")
        end
    end

    luatex.registerstopactions(cleanup)

    function visualizers.handler(head)
        if usedfont then
            starttiming(visualizers)
            head = visualize(head,true)
            stoptiming(visualizers)
            return head, true
        else
            return head, false
        end
    end

    function visualizers.box(n)
        if usedfont then
            starttiming(visualizers)
            local box = getbox(n)
            if box then
                setlist(box,visualize(getlist(box),getid(box) == vlist_code))
            end
            stoptiming(visualizers)
            return head, true
        else
            return head, false
        end
    end

end

do

    local nodecodes  = nodes.nodecodes
    local hlist_code = nodecodes.hlist
    local vlist_code = nodecodes.vlist
    local nextnode   = nuts.traversers.node

    local last       = nil
    local used       = nil

    local mark       = {
        "trace:1", "trace:2", "trace:3",
        "trace:4", "trace:5", "trace:6",
        "trace:7",
    }

    local function markfonts(list)
        for n, id in nextnode, list do
            if id == glyph_code then
                local font = getfont(n)
                local okay = used[font]
                if not okay then
                    last = last + 1
                    okay = mark[last]
                    used[font] = okay
                end
                setcolor(n,okay)
            elseif id == hlist_code or id == vlist_code then
                markfonts(getlist(n))
            end
        end
    end

    function visualizers.markfonts(list)
        last, used = 0, { }
        markfonts(type(n) == "number" and getlist(getbox(n)) or n)
    end

end

statistics.register("visualization time",function()
    if enabled then
     -- cleanup() -- in case we don't don't do it each time
        return formatters["%s seconds"](statistics.elapsedtime(visualizers))
    end
end)

-- interface

do

    local implement = interfaces.implement

    implement {
        name      = "setvisual",
        arguments = "string",
        actions   = visualizers.setvisual
    }

    implement {
        name      = "setvisuals",
        arguments = "string",
        actions   = visualizers.setvisual
    }

    implement {
        name      = "getvisual",
        arguments = "string",
        actions   = { setvisual, context }
    }

    implement {
        name      = "setvisuallayer",
        arguments = "string",
        actions   = visualizers.setlayer
    }

    implement {
        name      = "markvisualfonts",
        arguments = "integer",
        actions   = visualizers.markfonts
    }

    implement {
        name      = "setvisualfont",
        arguments = "integer",
        actions   = visualizers.setfont
    }

end

-- Here for now:

do

    local function make(str,forecolor,rulecolor,layer)
        if initialize then
            initialize()
        end
        local rule = new_rule(emwidth/fraction,exheight,4*exheight)
        setcolor(rule,rulecolor)
        settransparency(rule,rulecolor)
        local info
        if str == "" then
            info = new_hlist(rule)
        else
            local text = hpack_string(str,usedfont)
            local list = getlist(text)
            setlistcolor(list,textcolor)
            setlisttransparency(list,textcolor)
            setshift(text,3.5 * exheight)
            info = new_hlist(setlink(rule,text))
        end
        setattr(info,a_layer,layer)
        return info
    end

    function visualizers.register(name,textcolor,rulecolor)
        if rawget(layers,name) then
            -- message
            return
        end
        local cache = caches[name]
        local layer = layers[name]
        if not textcolor then
            textcolor = "trace:ds"
        end
        if not rulecolor then
            rulecolor = "trace:do"
        end
        return function(str)
            if not str then
                str = ""
            end
            local info = cache[str]
            if not info then
                info = make(str,textcolor,rulecolor,layer)
                cache[str] = info
            end
            return copy_node(info)
        end
    end

end