summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/math-noa.lmt
blob: 3f77c2225fdf2e471d889dede3a95b663d35715d (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
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
if not modules then modules = { } end modules ['math-noa'] = {
    version   = 1.001,
    optimize  = true,
    comment   = "companion to math-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- TODO: SET CLASSES !

-- if specials and (specials[1] == "char" or specials[1] == "font") then -- can we avoid this
-- ... better create a reverse mapping from the already present vectors

-- beware: this is experimental code and there will be a more generic (attribute value
-- driven) interface too but for the moment this is ok (sometime in 2015-2016 i will
-- start cleaning up as by then the bigger picture is clear and code has been used for
-- years; the main handlers will get some extensions)
--
-- we will also make dedicated processors (faster)
--
-- beware: names will change as we wil make noads.xxx.handler i.e. xxx
-- subnamespaces

-- 20D6 -> 2190
-- 20D7 -> 2192

-- todo: most is mathchar_code so we can have simple dedicated loops

-- nota bene: uunderdelimiter uoverdelimiter etc are radicals (we have 5 types)

local next, tonumber = next, tonumber
local utfchar, utfbyte = utf.char, utf.byte
local formatters, gmatch = string.formatters, string.gmatch
local sortedhash = table.sortedhash
local insert, remove = table.insert, table.remove
local div, round = math.div, math.round

local fonts              = fonts
local nodes              = nodes
local node               = node
local mathematics        = mathematics
local context            = context

local otf                = fonts.handlers.otf
local otffeatures        = fonts.constructors.features.otf
local registerotffeature = otffeatures.register

local privateattribute   = attributes.private
local registertracker    = trackers.register
local registerdirective  = directives.register
local logreporter        = logs.reporter
local setmetatableindex  = table.setmetatableindex

local texgetmode         = tex.getmode
local mathmode_code      = tex.modelevels.math

local colortracers       = nodes.tracers.colors

-- most trace/report will move into the closures

local trace_remapping     = false  registertracker("math.remapping",    function(v) trace_remapping    = v end)
local trace_processing    = false  registertracker("math.processing",   function(v) trace_processing   = v end)
local trace_analyzing     = false  registertracker("math.analyzing",    function(v) trace_analyzing    = v end)
local trace_normalizing   = false  registertracker("math.normalizing",  function(v) trace_normalizing  = v end)
local trace_collapsing    = false  registertracker("math.collapsing",   function(v) trace_collapsing   = v end)
local trace_fixing        = false  registertracker("math.fixing",       function(v) trace_fixing       = v end)
local trace_patching      = false  registertracker("math.patching",     function(v) trace_patching     = v end)
local trace_goodies       = false  registertracker("math.goodies",      function(v) trace_goodies      = v end)
local trace_variants      = false  registertracker("math.variants",     function(v) trace_variants     = v end)
local trace_alternates    = false  registertracker("math.alternates",   function(v) trace_alternates   = v end)
local trace_italics       = false  registertracker("math.italics",      function(v) trace_italics      = v end)
local trace_kernpairs     = false  registertracker("math.kernpairs",    function(v) trace_kernpairs    = v end)

local check_coverage      = true   registerdirective("math.checkcoverage", function(v) check_coverage   = v     end)
local use_math_goodies    = true   registerdirective("math.nogoodies",     function(v) use_math_goodies = not v end)

local report_processing   = logreporter("mathematics","processing")
local report_remapping    = logreporter("mathematics","remapping")
local report_normalizing  = logreporter("mathematics","normalizing")
local report_collapsing   = logreporter("mathematics","collapsing")
local report_fixing       = logreporter("mathematics","fixing")
local report_patching     = logreporter("mathematics","patching")
local report_goodies      = logreporter("mathematics","goodies")
local report_variants     = logreporter("mathematics","variants")
local report_alternates   = logreporter("mathematics","alternates")
local report_italics      = logreporter("mathematics","italics")
local report_kernpairs    = logreporter("mathematics","kernpairs")

local a_mathrendering    = privateattribute("mathrendering")
local a_exportstatus     = privateattribute("exportstatus")

local nuts               = nodes.nuts
local nodepool           = nuts.pool
local tonut              = nuts.tonut
local nutstring          = nuts.tostring

local setfield           = nuts.setfield
local setlink            = nuts.setlink
local setlist            = nuts.setlist
local setnext            = nuts.setnext
local setprev            = nuts.setprev
local setchar            = nuts.setchar
local setfam             = nuts.setfam
local setsubtype         = nuts.setsubtype
local setattr            = nuts.setattr
local setattrlist        = nuts.setattrlist
local setwidth           = nuts.setwidth
local setheight          = nuts.setheight
local setdepth           = nuts.setdepth

local getfield           = nuts.getfield
local getnext            = nuts.getnext
local getprev            = nuts.getprev
local getboth            = nuts.getboth
local getid              = nuts.getid
local getsubtype         = nuts.getsubtype
local getchar            = nuts.getchar
local getfont            = nuts.getfont
local getfam             = nuts.getfam
local getcharspec        = nuts.getcharspec
local getattr            = nuts.getattr
local getattrs           = nuts.getattrs
local getlist            = nuts.getlist
local getwidth           = nuts.getwidth
local getheight          = nuts.getheight
local getdepth           = nuts.getdepth
local getwhd             = nuts.getwhd
local getdelimiter       = nuts.getdelimiter
local getleftdelimiter   = nuts.getleftdelimiter
local getrightdelimiter  = nuts.getrightdelimiter
local getnumerator       = nuts.getnumerator
local getdenominator     = nuts.getdenominator
local getdegree          = nuts.getdegree
local gettop             = nuts.gettop
local getmiddle          = nuts.getmiddle
local getbottom          = nuts.getbottom
local getchoice          = nuts.getchoice

local getnucleus         = nuts.getnucleus
local getsub             = nuts.getsub
local getsup             = nuts.getsup
local getsubpre          = nuts.getsubpre
local getsuppre          = nuts.getsuppre
local getprime           = nuts.getprime

local setnucleus         = nuts.setnucleus
local setsub             = nuts.setsub
local setsup             = nuts.setsup
local setsubpre          = nuts.setsubpre
local setsuppre          = nuts.setsuppre
local setprime           = nuts.setprime

local getoffsets         = nuts.getoffsets
local setoffsets         = nuts.setoffsets

local flushnode          = nuts.flush
local copy_node          = nuts.copy
local slide_nodes        = nuts.slide
local set_visual         = nuts.setvisual

local mlisttohlist       = nuts.mlisttohlist

local new_kern           = nodepool.kern
local new_submlist       = nodepool.submlist
local new_noad           = nodepool.noad
local new_delimiter      = nodepool.delimiter
local new_fence          = nodepool.fence

local fonthashes         = fonts.hashes
local fontdata           = fonthashes.identifiers
local fontcharacters     = fonthashes.characters
local fontitalics        = fonthashes.italics
local fontparameters     = fonthashes.parameters

local variables          = interfaces.variables
local texsetattribute    = tex.setattribute
local texgetattribute    = tex.getattribute
local getfontoffamily    = tex.getfontoffamily
local unsetvalue         = attributes.unsetvalue
local implement          = interfaces.implement

local v_reset            = variables.reset
local v_small            = variables.small
local v_medium           = variables.medium
local v_big              = variables.big
local v_line             = variables.line

local chardata           = characters.data

noads                    = noads or { }  -- todo: only here
local noads              = noads

noads.processors         = noads.processors or { }
local processors         = noads.processors

noads.handlers           = noads.handlers   or { }
local handlers           = noads.handlers

local tasks              = nodes.tasks
local enableaction       = tasks.enableaction
local setaction          = tasks.setaction

local nodecodes          = nodes.nodecodes
local noadcodes          = nodes.noadcodes
local fencecodes         = nodes.fencecodes

local ordinarynoad_code    = noadcodes.ordinary
local operatornoad_code    = noadcodes.operator
local binarynoad_code      = noadcodes.binary
local relationnoad_code    = noadcodes.relation
local opennoad_code        = noadcodes.open
local closenoad_code       = noadcodes.close
local middlenoad_code      = noadcodes.middle
local punctuationnoad_code = noadcodes.punctuation
local innernoad_code       = noadcodes.inner
local fencednoad_code      = noadcodes.fenced
local undernoad_code       = noadcodes.under
local overnoad_code        = noadcodes.over
local vcenternoad_code     = noadcodes.vcenter
local fractionnoad_code    = noadcodes.fraction
local radicalnoad_code     = noadcodes.radical
local accentnoad_code      = noadcodes.accent

local noad_code          = nodecodes.noad
local accent_code        = nodecodes.accent
local radical_code       = nodecodes.radical
local fraction_code      = nodecodes.fraction
local subbox_code        = nodecodes.subbox
local submlist_code      = nodecodes.submlist
local mathchar_code      = nodecodes.mathchar
local mathtextchar_code  = nodecodes.mathtextchar
local delimiter_code     = nodecodes.delimiter
----- style_code         = nodecodes.style
----- parameter_code     = nodecodes.parameter
local math_choice        = nodecodes.choice
local fence_code         = nodecodes.fence

local leftfence_code     = fencecodes.left
local middlefence_code   = fencecodes.middle
local rightfence_code    = fencecodes.right

-- this initial stuff is tricky as we can have removed and new nodes with the same address
-- the only way out is a free-per-page list of nodes (not bad anyway)

-- local gf = getfield local gt = setmetatableindex("number") getfield = function(n,f)   gt[f] = gt[f] + 1 return gf(n,f)   end mathematics.GETFIELD = gt
-- local sf = setfield local st = setmetatableindex("number") setfield = function(n,f,v) st[f] = st[f] + 1        sf(n,f,v) end mathematics.SETFIELD = st

-- TODO : get rid of done

local function process(start,what,n,parent)

    if n then
        n = n + 1
    else
        n = 0
    end
    --
    local initial = start
    --
    slide_nodes(start) -- we still miss a prev in noads -- fences test code
    --
    while start do
        local id = getid(start)
        if trace_processing then
            if id == noad_code then
                report_processing("%w%S, class %a",n*2,nutstring(start),noadcodes[getsubtype(start)])
            elseif id == mathchar_code then
                local char, font, fam = getcharspec(start)
                report_processing("%w%S, family %a, font %a, char %a, shape %c",n*2,nutstring(start),fam,font,char,char)
            else
                report_processing("%w%S",n*2,nutstring(start))
            end
        end
        local proc = what[id]
        if proc then
         -- report_processing("start processing")
            local done, newstart, newinitial = proc(start,what,n,parent) -- prev is bugged:  or getprev(start)
            if newinitial then
                initial = newinitial -- temp hack .. we will make all return head
                if newstart then
                    start = newstart
                 -- report_processing("stop processing (new start)")
                else
                 -- report_processing("quit processing (done)")
                    break
                end
            else
                if newstart then
                    start = newstart
                 -- report_processing("stop processing (new start)")
                else
                 -- report_processing("stop processing")
                end
            end
        elseif id == noad_code then
            -- single characters are like this
            local noad = getnucleus(start)        if noad then process(noad,what,n,start) end -- list
                  noad = getsup(start)            if noad then process(noad,what,n,start) end -- list
                  noad = getsub(start)            if noad then process(noad,what,n,start) end -- list
                  noad = getsuppre(start)         if noad then process(noad,what,n,start) end -- list
                  noad = getsubpre(start)         if noad then process(noad,what,n,start) end -- list
                  noad = getprime(start)          if noad then process(noad,what,n,start) end -- list
        elseif id == mathchar_code or id == mathtextchar_code or id == delimiter_code then
            break
        elseif id == subbox_code or id == submlist_code then
            local noad = getlist(start)           if noad then process(noad,what,n,start) end -- list (not getlist !)
        elseif id == fraction_code then
            local noad = getnumerator(start)      if noad then process(noad,what,n,start) end -- list
                  noad = getdenominator(start)    if noad then process(noad,what,n,start) end -- list
                  noad = getleftdelimiter(start)  if noad then process(noad,what,n,start) end -- delimiter
                  noad = getdelimiter(start)      if noad then process(noad,what,n,start) end -- delimiter
                  noad = getrightdelimiter(start) if noad then process(noad,what,n,start) end -- delimiter
        elseif id == math_choice then
            local noad = getchoice(start,1)       if noad then process(noad,what,n,start) end -- list
                  noad = getchoice(start,2)       if noad then process(noad,what,n,start) end -- list
                  noad = getchoice(start,3)       if noad then process(noad,what,n,start) end -- list
                  noad = getchoice(start,4)       if noad then process(noad,what,n,start) end -- list
        elseif id == fence_code then
            local noad = getdelimiter(start)      if noad then process(noad,what,n,start) end -- delimiter
                  noad = gettop(start)            if noad then process(noad,what,n,start) end -- list
                  noad = getbottom(start)         if noad then process(noad,what,n,start) end -- list
        elseif id == radical_code then
            local noad = getnucleus(start)        if noad then process(noad,what,n,start) end -- list
                  noad = getsup(start)            if noad then process(noad,what,n,start) end -- list
                  noad = getsub(start)            if noad then process(noad,what,n,start) end -- list
                  noad = getsuppre(start)         if noad then process(noad,what,n,start) end -- list
                  noad = getsubpre(start)         if noad then process(noad,what,n,start) end -- list
                  noad = getprime(start)          if noad then process(noad,what,n,start) end -- delimiter
                  noad = getleftdelimiter(start)  if noad then process(noad,what,n,start) end -- delimiter
                  noad = getrightdelimiter(start) if noad then process(noad,what,n,start) end -- delimiter
                  noad = getdegree(start)         if noad then process(noad,what,n,start) end -- list
        elseif id == accent_code then
            local noad = getnucleus(start)        if noad then process(noad,what,n,start) end -- list
                  noad = getsup(start)            if noad then process(noad,what,n,start) end -- list
                  noad = getsub(start)            if noad then process(noad,what,n,start) end -- list
                  noad = getsuppre(start)         if noad then process(noad,what,n,start) end -- list
                  noad = getsubpre(start)         if noad then process(noad,what,n,start) end -- list
                  noad = getprime(start)          if noad then process(noad,what,n,start) end -- list
                  noad = gettop(start)            if noad then process(noad,what,n,start) end -- list
                  noad = getmiddle(start)         if noad then process(noad,what,n,start) end -- list
                  noad = getbottom(start)         if noad then process(noad,what,n,start) end -- list
     -- elseif id == style_code then
     --     -- has a next
     -- elseif id == parameter_code then
     --     -- has a next
     -- else
     --     -- glue, penalty, etc
        end
        start = getnext(start)
    end
    if not parent then
        return initial -- only first level -- for now
    end
end

local function processnested(current,what,n)
    local noad = nil
    local id   = getid(current)
    if id == noad_code then
        noad = getnucleus(current)        if noad then process(noad,what,n,current) end -- list
        noad = getsup(current)            if noad then process(noad,what,n,current) end -- list
        noad = getsub(current)            if noad then process(noad,what,n,current) end -- list
        noad = getsuppre(current)         if noad then process(noad,what,n,current) end -- list
        noad = getsubpre(current)         if noad then process(noad,what,n,current) end -- list
        noad = getprime(current)          if noad then process(noad,what,n,current) end -- list
    elseif id == subbox_code or id == submlist_code then
        noad = getlist(current)           if noad then process(noad,what,n,current) end -- list (not getlist !)
    elseif id == fraction_code then
        noad = getnumerator(current)      if noad then process(noad,what,n,current) end -- list
        noad = getdenominator(current)    if noad then process(noad,what,n,current) end -- list
        noad = getleftdelimiter(current)  if noad then process(noad,what,n,current) end -- delimiter
        noad = getdelimiter(current)      if noad then process(noad,what,n,current) end -- delimiter
        noad = getrightdelimiter(current) if noad then process(noad,what,n,current) end -- delimiter
    elseif id == math_choice then
        noad = getchoice(start,1)         if noad then process(noad,what,n,current) end -- list
        noad = getchoice(start,2)         if noad then process(noad,what,n,current) end -- list
        noad = getchoice(start,3)         if noad then process(noad,what,n,current) end -- list
        noad = getchoice(start,4)         if noad then process(noad,what,n,current) end -- list
    elseif id == fence_code then
        noad = getdelimiter(current)      if noad then process(noad,what,n,current) end -- delimiter
        noad = gettop(current)            if noad then process(noad,what,n,current) end -- list
        noad = getbottom(current)         if noad then process(noad,what,n,current) end -- list
    elseif id == radical_code then
        noad = getnucleus(current)        if noad then process(noad,what,n,current) end -- list
        noad = getsup(current)            if noad then process(noad,what,n,current) end -- list
        noad = getsub(current)            if noad then process(noad,what,n,current) end -- list
        noad = getsuppre(current)         if noad then process(noad,what,n,current) end -- list
        noad = getsubpre(current)         if noad then process(noad,what,n,current) end -- list
        noad = getprime(current)          if noad then process(noad,what,n,current) end -- list
        noad = getleftdelimiter(current)  if noad then process(noad,what,n,current) end -- delimiter
        noad = getrightdelimiter(current) if noad then process(noad,what,n,current) end -- delimiter
        noad = getdegree(current)         if noad then process(noad,what,n,current) end -- list
    elseif id == accent_code then
        noad = getnucleus(current)        if noad then process(noad,what,n,current) end -- list
        noad = getsup(current)            if noad then process(noad,what,n,current) end -- list
        noad = getsub(current)            if noad then process(noad,what,n,current) end -- list
        noad = getsuppre(current)         if noad then process(noad,what,n,current) end -- list
        noad = getsubpre(current)         if noad then process(noad,what,n,current) end -- list
        noad = getprime(current)          if noad then process(noad,what,n,current) end -- list
        noad = gettop(current)            if noad then process(noad,what,n,current) end -- list
        noad = getmiddle(current)         if noad then process(noad,what,n,current) end -- list
        noad = getbottom(current)         if noad then process(noad,what,n,current) end -- list
    end
end

local function processstep(current,process,n,id)
    local noad = nil
    local id   = id or getid(current)
    if id == noad_code then
        noad = getnucleus(current)        if noad then process(noad,n,current) end -- list
        noad = getsup(current)            if noad then process(noad,n,current) end -- list
        noad = getsub(current)            if noad then process(noad,n,current) end -- list
        noad = getsuppre(current)         if noad then process(noad,n,current) end -- list
        noad = getsubpre(current)         if noad then process(noad,n,current) end -- list
        noad = getprime(current)          if noad then process(noad,n,current) end -- list
    elseif id == subbox_code or id == submlist_code then
        noad = getlist(current)           if noad then process(noad,n,current) end -- list (not getlist !)
    elseif id == fraction_code then
        noad = getnumerator(current)      if noad then process(noad,n,current) end -- list
        noad = getdenominator(current)    if noad then process(noad,n,current) end -- list
        noad = getleftdelimiter(current)  if noad then process(noad,n,current) end -- delimiter
        noad = getdelimiter(current)      if noad then process(noad,n,current) end -- delimiter
        noad = getrightdelimiter(current) if noad then process(noad,n,current) end -- delimiter
    elseif id == math_choice then
        noad = getchoice(start,1)         if noad then process(noad,n,current) end -- list
        noad = getchoice(start,2)         if noad then process(noad,n,current) end -- list
        noad = getchoice(start,3)         if noad then process(noad,n,current) end -- list
        noad = getchoice(start,4)         if noad then process(noad,n,current) end -- list
    elseif id == fence_code then
        noad = getdelimiter(current)      if noad then process(noad,n,current) end -- delimiter
        noad = gettop(current)            if noad then process(noad,n,current) end -- list
        noad = getbottom(current)         if noad then process(noad,n,current) end -- list
    elseif id == radical_code then
        noad = getnucleus(current)        if noad then process(noad,n,current) end -- list
        noad = getsup(current)            if noad then process(noad,n,current) end -- list
        noad = getsub(current)            if noad then process(noad,n,current) end -- list
        noad = getsuppre(current)         if noad then process(noad,n,current) end -- list
        noad = getsubpre(current)         if noad then process(noad,n,current) end -- list
        noad = getprime(current)          if noad then process(noad,n,current) end -- delimiter
        noad = getleftdelimiter(current)  if noad then process(noad,n,current) end -- delimiter
        noad = getrightdelimiter(current) if noad then process(noad,n,current) end -- delimiter
        noad = getdegree(current)         if noad then process(noad,n,current) end -- list
    elseif id == accent_code then
        noad = getnucleus(current)        if noad then process(noad,n,current) end -- list
        noad = getsup(current)            if noad then process(noad,n,current) end -- list
        noad = getsub(current)            if noad then process(noad,n,current) end -- list
        noad = getsuppre(current)         if noad then process(noad,n,current) end -- list
        noad = getsubpre(current)         if noad then process(noad,n,current) end -- list
        noad = getprime(current)          if noad then process(noad,n,current) end -- list
        noad = gettop(current)            if noad then process(noad,n,current) end -- list
        noad = getmiddle(current)         if noad then process(noad,n,current) end -- list
        noad = getbottom(current)         if noad then process(noad,n,current) end -- list
    end
end

local function processnoads(head,actions,banner)
    if trace_processing then
        report_processing("start %a",banner)
        head = process(head,actions)
        report_processing("stop %a",banner)
    else
        head = process(head,actions)
    end
    return head
end

noads.process       = processnoads
noads.processnested = processnested
noads.processouter  = process

-- experiment (when not present fall back to fam 0) -- needs documentation

local unknowns = { }
local checked  = { } -- simple case
local tracked  = false  trackers.register("fonts.missing", function(v) tracked = v end)
local cached   = setmetatableindex("table") -- complex case

local variantselectors = {
    [0xFE00] = true,
    [0xFE01] = true
}

local function errorchar(font,char)
    if variantselectors[char] then
        return char
    end
    local done = unknowns[char]
    if done then
        unknowns[char] = done  + 1
    else
        unknowns[char] = 1
    end
    if tracked then
        -- slower as we check each font too and we always replace as math has
        -- more demands than text
        local fake = cached[font][char]
        if fake then
            return fake
        else
            local kind, fake = fonts.checkers.placeholder(font,char)
            if not fake or kind ~= "char" then -- Also check for "with" here?
                fake = 0x3F
            end
            cached[font][char] = fake
            return fake
        end
    else
        -- only simple checking, report at the end so one should take
        -- action anyway ... we can miss a few checks but that is ok
        -- as there is at least one reported
        if not checked[char] then
            if trace_normalizing then
                report_normalizing("character %C is not available",char)
            end
            checked[char] = true
        end
        return 0x3F
    end
end

-- 0-2 regular
-- 3-5 bold
-- 6-8 pseudobold

-- this could best be integrated in the remapper, and if we run into problems, we
-- might as well do this

do

    local families     = { }
    local a_mathfamily = privateattribute("mathfamily")
    local boldmap      = mathematics.boldmap

    local trace_families  = false  registertracker("math.families", function(v) trace_families = v end)
    local report_families = logreporter("mathematics","families")

    local familymap = { [0] =
        "regular",
        "regular",
        "regular",
        "bold",
        "bold",
        "bold",
        "pseudobold",
        "pseudobold",
        "pseudobold",
    }

    families[fraction_code] = function(pointer,what,n,parent)
        local a = getattr(pointer,a_mathfamily)
        if a and a >= 0 then
            if a > 0 then
                setattr(pointer,a_mathfamily,0)
                if a > 5 then
                    a = a - 3
                end
            end
            setfam(pointer,a)
        end
        processnested(pointer,families,n+1)
    end

    families[noad_code] = function(pointer,what,n,parent)
        local a = getattr(pointer,a_mathfamily)
        if a and a >= 0 then
            if a > 0 then
                setattr(pointer,a_mathfamily,0)
                if a > 5 then
                    a = a - 3
                end
            end
            setfam(pointer,a)
        end
        processnested(pointer,families,n+1)
    end

    families[mathchar_code] = function(pointer)
        if getfam(pointer) == 0 then
            local a = getattr(pointer,a_mathfamily)
            if a and a > 0 then
                setattr(pointer,a_mathfamily,0)
                if a > 5 then
                    local char = getchar(pointer)
                    local bold = boldmap[char]
                    local newa = a - 3
                    if not bold then
                        if trace_families then
                            report_families("no bold replacement for %C, family %s with remap %s becomes %s with remap %s",char,a,familymap[a],newa,familymap[newa])
                        end
                        setfam(pointer,newa)
                    elseif not fontcharacters[getfontoffamily(newa)][bold] then
                        if trace_families then
                            report_families("no bold character for %C, family %s with remap %s becomes %s with remap %s",char,a,familymap[a],newa,familymap[newa])
                        end
                        if newa > 3 then
                            setfam(pointer,newa-3)
                        end
                    else
                        setattr(pointer,a_exportstatus,char)
                        setchar(pointer,bold)
                        if trace_families then
                            report_families("replacing %C by bold %C, family %s with remap %s becomes %s with remap %s",char,bold,a,familymap[a],newa,familymap[newa])
                        end
                        setfam(pointer,newa)
                    end
                else
                    local char = getchar(pointer)
                    if not fontcharacters[getfontoffamily(a)][char] then
                        if trace_families then
                            report_families("no bold replacement for %C",char)
                        end
                    else
                        if trace_families then
                            report_families("family of %C becomes %s with remap %s",char,a,familymap[a])
                        end
                        setfam(pointer,a)
                    end
                end
            end
        end
    end

 -- families[delimiter_code] = function(pointer)
 --     if getfield(pointer,"smallfamily") == 0 then
 --         local a = getattr(pointer,a_mathfamily)
 --         if a and a > 0 then
 --             setattr(pointer,a_mathfamily,0)
 --             if a > 5 then
 --                 -- no bold delimiters in unicode
 --                 a = a - 3
 --             end
 --             local fam  = getfontoffamily(a)
 --             local char = getfield(pointer,"smallchar")
 --             local okay = fontcharacters[fam][char]
 --             if okay then
 --                 setfield(pointer,"smallfamily",a)
 --             elseif a > 2 then
 --                 setfield(pointer,"smallfamily",a-3)
 --             end
 --             local char = getfield(pointer,"largechar")
 --             local okay = fontcharacters[fam][char]
 --             if okay then
 --                 setfield(pointer,"largefamily",a)
 --             elseif a > 2 then
 --                 setfield(pointer,"largefamily",a-3)
 --             end
 --         else
 --             setfield(pointer,"smallfamily",0)
 --             setfield(pointer,"largefamily",0)
 --         end
 --     end
 -- end

    -- has become:

    families[delimiter_code] = function(pointer)
        if getfam(pointer) == 0 then
            local a = getattr(pointer,a_mathfamily)
            if a and a > 0 then
                setattr(pointer,a_mathfamily,0)
                if a > 5 then
                    -- no bold delimiters in unicode
                    a = a - 3
                end
                local char = getchar(pointer)
                local okay = fontcharacters[getfontoffamily(a)][char]
                if okay then
                    setfam(pointer,a)
                elseif a > 2 then
                    setfam(pointer,a-3)
                end
            else
                setfam(pointer,0)
            end
        end
    end

    families[mathtextchar_code] = families[mathchar_code]

    function handlers.families(head,style,penalties)
        processnoads(head,families,"families")
        return true -- not needed
    end

end

-- character remapping

do

    local a_mathalphabet    = privateattribute("mathalphabet")
    local a_mathgreek       = privateattribute("mathgreek")

    local relocate          = { }

    local remapalphabets    = mathematics.remapalphabets
    local fallbackstyleattr = mathematics.fallbackstyleattr

    local function report_remap(tag,id,old,new,extra)
        if new then
            report_remapping("remapping %s in font (%s,%s) from %C to %C%s",
                tag,id,fontdata[id].properties.fontname or "",old,new,extra)
        else
            -- error !
            report_remapping("remapping %s in font (%s,%s) from %C to ?",
                tag,id,fontdata[id].properties.fontname or "",old)
        end
    end

    local function checked(pointer)
        local char, font = getcharspec(pointer)
        local data = fontcharacters[font]
        if not data[char] then
            local specials = characters.data[char].specials
            if specials and (specials[1] == "char" or specials[1] == "font") then
                local newchar = specials[#specials]
                if trace_remapping then
                    report_remap("fallback",font,char,newchar)
                end
                if trace_analyzing then
                    setnodecolor(pointer,"font:isol")
                end
                setattr(pointer,a_exportstatus,char) -- testcase: exponentiale
                setchar(pointer,newchar)
                return true
            end
        end
    end

    -- We can optimize this if we really think that math is a bottleneck which it never
    -- really is. Beware: the font is the text font in the family, so we only check the
    -- text font here.

    relocate[mathchar_code] = function(pointer)
        local g          = getattr(pointer,a_mathgreek) or 0
        local a          = getattr(pointer,a_mathalphabet) or 0
     -- local g, a       = getattrs(pointer,a_mathgreek,a_mathalphabet)
     -- if not a then a = 0 end
     -- if not g then g = 0 end
        local char, font, fam = getcharspec(pointer)
        local characters = fontcharacters[font]
        if a > 0 or g > 0 then
            if a > 0 then
                -- not really critital but we could use properties
                setattr(pointer,a_mathgreek,0)
            end
            if g > 0 then
                -- not really critital but we could use properties
                setattr(pointer,a_mathalphabet,0)
            end
            local newchar = remapalphabets(char,a,g)
            if newchar then
                local newchardata = characters[newchar]
                if newchardata then
                    if trace_remapping then
                        report_remap("char",font,char,newchar,newchardata.commands and " (virtual)" or "")
                    end
                    if trace_analyzing then
                        setnodecolor(pointer,"font:isol")
                    end
                    setchar(pointer,newchar)
                    return true
                else
                    local fallback = fallbackstyleattr(a)
                    if fallback then
                        local newchar = remapalphabets(char,fallback,g)
                        if newchar then
                            if characters[newchar] then
                                if trace_remapping then
                                    report_remap("char",font,char,newchar," (fallback remapping used)")
                                end
                                if trace_analyzing then
                                    setnodecolor(pointer,"font:isol")
                                end
                                setchar(pointer,newchar)
                                return true
                            elseif trace_remapping then
                                report_remap("char",font,char,newchar," fails (no fallback character)")
                            end
                        elseif trace_remapping then
                            report_remap("char",font,char,newchar," fails (no fallback remap character)")
                        end
                    elseif trace_remapping then
                        report_remap("char",font,char,newchar," fails (no fallback style)")
                    end
                end
            elseif trace_remapping then
                local chardata = characters[char]
                if chardata and chardata.commands then
                    report_remap("char",font,char,char," (virtual)")
                end
            end
        end
        if not characters[char] then
            local fnt = getfontoffamily(fam,1)
            setchar(pointer,errorchar(font,char))
            if font ~= fnt then
                errorchar(fnt,char)
                errorchar(getfontoffamily(fam,2),char)
            end
        end
        if trace_analyzing then
            setnodecolor(pointer,"font:medi")
        end
        if check_coverage then
            return checked(pointer)
        end
    end

    relocate[mathtextchar_code] = function(pointer)
        if trace_analyzing then
            setnodecolor(pointer,"font:init")
        end
    end

    relocate[delimiter_code] = function(pointer)
        if trace_analyzing then
            setnodecolor(pointer,"font:fina")
        end
    end

    function handlers.relocate(head,style,penalties)
        processnoads(head,relocate,"relocate")
        return true -- not needed
    end

end

-- rendering (beware, not exported)

do

    local render     = { }

    local rendersets = mathematics.renderings.numbers or { } -- store

    render[mathchar_code] = function(pointer)
        local attr = getattr(pointer,a_mathrendering)
        if attr and attr > 0 then
            local char, font = getcharspec(pointer)
            local renderset = rendersets[attr]
            if renderset then
                local newchar = renderset[char]
                if newchar then
                    local characters = fontcharacters[font]
                    if characters and characters[newchar] then
                        setchar(pointer,newchar)
                        setattr(pointer,a_exportstatus,char)
                    end
                end
            end
        end
    end

    function handlers.render(head,style,penalties)
        processnoads(head,render,"render")
        return true -- not needed
    end

end

-- some resize options (this works ok because the content is
-- empty and no larger next will be forced)
--
-- beware: we don't use \delcode but \Udelcode and as such have
-- no largefamily; also, we need to check for subtype and/or
-- smallfamily not being 0 because \. sits in 0,0 by default
--
-- todo: just replace the character by an ord noad
-- and remove the right delimiter as well

do

    -- todo: use registerattribute

    local a_mathsize  = privateattribute("mathsize") -- this might move into other fence code
    local resize      = { }

    local trace_fences  = false  registertracker("math.fences", function(v) trace_fences = v end)
    local report_fences = logreporter("mathematics","fences")

    resize[fence_code] = function(pointer)
        local subtype = getsubtype(pointer)
     -- if subtype == leftfence_code or subtype == rightfence_code then
            local a = getattr(pointer,a_mathsize)
            if a and a > 0 then
                local method = div(a,100)
                local size   = a % 100
                setattr(pointer,a_mathsize,0)
                local delimiter = getfield(pointer,"delimiter")
                if delimiter then
                    local chr, fnt, fam = getcharspec(delimiter)
                    if chr > 0 and fnt > 0 then
                        local data = fontdata[fnt]
                        local char = mathematics.big(data,chr,size,method)
                        local ht   = getheight(pointer)
                        local dp   = getdepth(pointer)
                        if ht == 1 or dp == 1 then -- 1 scaled point is a signal
                            local chardata = data.characters[char]
                            if ht == 1 then
                                setheight(pointer,chardata.height)
                            end
                            if dp == 1 then
                                setdepth(pointer,chardata.depth)
                            end
                        end
                        if trace_fences then
                            report_fences("replacing %C by %C using method %a and size %a",chr,char,method,size)
                        end
                        setchar(delimiter,char)
                    end
                end
            end
     -- end
    end

    function handlers.resize(head,style,penalties)
        processnoads(head,resize,"resize")
        return true -- not needed
    end

end

-- still not perfect:

do

    local a_autofence     = privateattribute("mathautofence")
    local autofences      = { }
    local dummyfencechar  = 0x2E

    local function makefence(what,char)
        local d = new_delimiter() -- todo: attr
        local f = new_fence()     -- todo: attr
        if char then
            local sym = getnucleus(char)
            local chr, fnt, fam = getcharspec(sym)
            if chr == dummyfencechar then
                chr = 0
            end
            setchar(d,chr)
            setfam(d,fam)
            flushnode(sym)
        end
        setattrlist(d,char)
        setattrlist(f,char)
        setsubtype(f,what)
        setfield(f,"delimiter",d)
        setfield(f,"class",-1) -- tex itself does this, so not fenceclasses[what]
        return f
    end

    local function show(where,pointer)
        print("")
        local i = 0
        for n in nuts.traverse(pointer) do
            i = i + 1
            print(i,where,nuts.tonode(n))
        end
        print("")
    end

    local function makelist(middle,noad,f_o,o_next,c_prev,f_c)
-- report_fences(
--     "middle %s, noad %s, open %s, opennext %s, closeprev %s, close %s",
--     middle or "?",
--     noad   or "?",
--     f_o    or "?",
--     o_next or "?",
--     c_prev or "?",
--     f_c    or "?"
-- )
        local list = new_submlist()
        setsubtype(noad,fencednoad_code)
        setnucleus(noad,list)
        setlist(list,f_o)
        setlink(f_o,o_next) -- prev of list is nil
        setlink(c_prev,f_c) -- next of list is nil
-- show("list",f_o)
        if middle and next(middle) then
            local prev    = f_o
            local current = o_next
            while current ~= f_c do
                local midl = middle[current]
                local next = getnext(current)
                if midl then
                    local fence = makefence(middlefence_code,current)
                    setnucleus(current)
                    flushnode(current)
                    middle[current] = nil
                    -- replace_node
                    setlink(prev,fence,next)
                    prev = fence
                else
                    prev = current
                end
                current = next
            end
        end
        return noad
    end

    -- relinking is now somewhat overdone

    local function convert_both(open,close,middle)
        local o_next = getnext(open)
        if o_next == close then
            return close
        else
            local c_prev, c_next = getboth(close)
            local f_o = makefence(leftfence_code,open)
            local f_c = makefence(rightfence_code,close)
            makelist(middle,open,f_o,o_next,c_prev,f_c)
            setnucleus(close)
            flushnode(close)
            -- open is now a list
            setlink(open,c_next)
            return open
        end
    end

    local function convert_open(open,last,middle) -- last is really last (final case)
        local f_o = makefence(leftfence_code,open)
        local f_c = makefence(rightfence_code)
        local o_next = getnext(open)
        makelist(middle,open,f_o,o_next,last,nil)
        -- open is now a list
        setlink(open,l_next)
        return open
    end

    local function convert_close(first,close,middle)
        local f_o = makefence(leftfence_code)
        local f_c = makefence(rightfence_code,close)
        local c_prev = getprev(close)
        local f_next = getnext(first)
        makelist(middle,close,f_o,f_next,c_prev,f_c)
        -- close is now a list : inner but should be fenced
        if c_prev ~= first then
            setlink(first,close)
        end
        return close
    end

    local stacks = setmetatableindex("table")

    -- 1=open 2=close 3=middle 4=both : todo check both

    local function processfences(pointer,n,parent)
        local current = pointer
        local last    = pointer
        local start   = pointer
        local done    = false
        local initial = pointer
        local stack   = nil
        local middle  = nil -- todo: use properties
        while current do
-- show("before",pointer)
            local id = getid(current)
            if id == noad_code then
                local a = getattr(current,a_autofence)
                if a and a > 0 then
                    local stack = stacks[n]
                    setattr(current,a_autofence,0) -- hm, better use a property
                    local level = #stack
                    if a == 1 then
                        if trace_fences then
                            report_fences("%2i: level %i, handling %s, action %s",n,level,"open","open")
                        end
                        insert(stack,current)
                    elseif a == 2 then
                        local open = remove(stack)
                        if open then
                            if trace_fences then
                                report_fences("%2i: level %i, handling %s, action %s",n,level,"close","both")
                            end
                            current = convert_both(open,current,middle)
                        elseif current == start then
                            if trace_fences then
                                report_fences("%2i: level %i, handling %s, action %s",n,level,"close","skip")
                            end
                        else
                            if trace_fences then
                                report_fences("%2i: level %i, handling %s, action %s",n,level,"close","close")
                            end
                            current = convert_close(initial,current,middle)
                            if not parent then
                                initial = current
                            end
                        end
                    elseif a == 3 then
                        if trace_fences then
                            report_fences("%2i: level %i, handling %s, action %s",n,level,"middle","middle")
                        end
                        if middle then
                            middle[current] = last
                        else
                            middle = { [current] = last }
                        end
                    elseif a == 4 then
                        if not stack or #stack == 0 then
                            if trace_fences then
                                report_fences("%2i: level %i, handling %s, action %s",n,level,"both","open")
                            end
                            insert(stack,current)
                        else
                            local open = remove(stack)
                            if open then
                                if trace_fences then
                                    report_fences("%2i: level %i, handling %s, action %s",n,level,"both","both")
                                end
                                current = convert_both(open,current,middle)
                            elseif current == start then
                                if trace_fences then
                                    report_fences("%2i: level %i, handling %s, action %s",n,level,"both","skip")
                                end
                            else
                                if trace_fences then
                                    report_fences("%2i: level %i, handling %s, action %s",n,level,"both","close")
                                end
                                current = convert_close(initial,current,middle)
                                if not parent then
                                    initial = current
                                end
                            end
                        end
                    end
                    done = true
                else
                    processstep(current,processfences,n+1,id)
                end
            else
                -- next at current level
                processstep(current,processfences,n,id)
            end
-- show("after",pointer)
            last    = current
            current = getnext(current)
        end
        if done then
            local stack = stacks[n]
            local s = #stack
            if s > 0 then
                for i=1,s do
                    local open = remove(stack)
                    if trace_fences then
                        report_fences("%2i: level %i, handling %s, action %s",n,#stack,"flush","open")
                    end
                    last = convert_open(open,last,middle)
                end
-- show("done",pointer)
            end
        end
    end

    -- we can have a first changed node .. an option is to have a leading dummy node in math
    -- lists like the par node as it can save a  lot of mess

    local enabled = false

    implement {
        name     = "enableautofences",
        onlyonce = true,
        actions  = function()
            enableaction("math","noads.handlers.autofences")
            enabled = true
        end
    }

    function handlers.autofences(head,style,penalties)
        if enabled then -- tex.modes.c_math_fences_auto
         -- inspect(nodes.totree(head))
            processfences(head,1)
         -- inspect(nodes.totree(head))
        end
    end

end

-- normalize scripts

do

    local unscript     = { }  noads.processors.unscript = unscript
    local superscripts = characters.superscripts
    local subscripts   = characters.subscripts
    local fractions    = characters.fractions
    local replaced     = { }

    local function replace(pointer,what,n,parent)
        pointer = parent -- we're following the parent list (chars trigger this)
        local next = getnext(pointer)
        local start_super, stop_super, start_sub, stop_sub
        local mode = "unset"
        while next and getid(next) == noad_code do
            local nextnucleus = getnucleus(next)
            if nextnucleus and getid(nextnucleus) == mathchar_code and not getsub(next) and not getsup(next) then
                local char = getchar(nextnucleus)
                local s = superscripts[char]
                if s then
                    if not start_super then
                        start_super = next
                        mode = "super"
                    elseif mode == "sub" then
                        break
                    end
                    stop_super = next
                    next = getnext(next)
                    setchar(nextnucleus,s)
                    replaced[char] = (replaced[char] or 0) + 1
                    if trace_normalizing then
                        report_normalizing("superscript %C becomes %C",char,s)
                    end
                else
                    local s = subscripts[char]
                    if s then
                        if not start_sub then
                            start_sub = next
                            mode = "sub"
                        elseif mode == "super" then
                            break
                        end
                        stop_sub = next
                        next = getnext(next)
                        setchar(nextnucleus,s)
                        replaced[char] = (replaced[char] or 0) + 1
                        if trace_normalizing then
                            report_normalizing("subscript %C becomes %C",char,s)
                        end
                    else
                        break
                    end
                end
            else
                break
            end
        end
        if start_super then
            if start_super == stop_super then
                setsup(pointer,getnucleus(start_super))
            else
                local list = new_submlist() -- todo attr
                setlist(list,start_super)
                setsup(pointer,list)
            end
            if mode == "super" then
                setnext(pointer,getnext(stop_super))
            end
            setnext(stop_super)
        end
        if start_sub then

--             if mode == "sub" then
--                 local sup = getsup(pointer)
--                 if sup and not getsub(pointer) then
--                     local nxt = getnext(pointer)
--                     local new = new_noad(pointer)
--                     setnucleus(new,new_submlist())
--                     setlink(pointer,new,nxt)
--                     pointer = new
--                 end
--             end

            if start_sub == stop_sub then
                setsub(pointer,getnucleus(start_sub))
            else
                local list = new_submlist() -- todo attr
                setlist(list,start_sub)
                setsub(pointer,list)
            end
            if mode == "sub" then
                setnext(pointer,getnext(stop_sub))
            end
            setnext(stop_sub)
        end
        -- we could return stop
    end

    unscript[mathchar_code] = replace -- not noads as we need to recurse

    function handlers.unscript(head,style,penalties)
        processnoads(head,unscript,"unscript")
        return true -- not needed
    end

end

do

    local unstack   = { }    noads.processors.unstack = unstack
    local enabled   = false
    local a_unstack = privateattribute("mathunstack")

    local trace_unstacking  = false  registertracker("math.unstack", function(v) trace_unstacking  = v end)
    local report_unstacking = logreporter("mathematics","unstack")

    unstack[noad_code] = function(pointer)
        if getattr(pointer,a_unstack) then
            local sup = getsup(pointer)
            local sub = getsub(pointer)
            if sup and sub then
             -- if trace_unstacking then
             --     report_unstacking() -- todo ... what to show ...
             -- end
                local nxt = getnext(pointer)
                local new = new_noad(pointer)
                setnucleus(new,new_submlist())
                setsub(pointer)
                setsub(new,sub)
                setlink(pointer,new,nxt)
            end
        end
    end

    function handlers.unstack(head,style,penalties)
        if enabled then
            processnoads(head,unstack,"unstack")
            return true -- not needed
        end
    end

    implement {
        name     = "enablescriptunstacking",
        onlyonce = true,
        actions  = function()
            enableaction("math","noads.handlers.unstack")
            enabled = true
        end
    }

end

do

    local function collected(list)
        if list and next(list) then
            local n, t = 0, { }
            for k, v in sortedhash(list) do
                n = n + 1
                t[n] = formatters["%C"](k)
            end
            return formatters["% t (n=%s)"](t,n)
        end
    end

    statistics.register("math script replacements", function()
        return collected(replaced)
    end)

    statistics.register("unknown math characters", function()
        return collected(unknowns)
    end)

end

-- math alternates: (in xits     lgf: $ABC$ $\cal ABC$ $\mathalternate{cal}\cal ABC$)
-- math alternates: (in lucidaot lgf: $ABC \mathalternate{italic} ABC$)

-- todo: set alternate for specific symbols
-- todo: no need to do this when already loaded
-- todo: use a fonts.hashes.mathalternates

do

    local last = 0

    local known = setmetatableindex(function(t,k)
        local v = 0 | 2^last
        t[k] = v
        last = last + 1
        return v
    end)

    local defaults = {
        dotless = { feature = 'dtls', value = 1, comment = "Mathematical Dotless Forms" },
     -- zero    = { feature = 'zero', value = 1, comment = "Slashed or Dotted Zero" }, -- in no math font (yet)
    }

    local function initializemathalternates(tfmdata)
        if use_math_goodies then

            local goodies  = tfmdata.goodies
            local autolist = defaults -- table.copy(defaults)

            local function setthem(newalternates)
                local resources      = tfmdata.resources -- was tfmdata.shared
                local mathalternates = resources.mathalternates
                local alternates, attributes, registered, presets
                if mathalternates then
                    alternates = mathalternates.alternates
                    attributes = mathalternates.attributes
                    registered = mathalternates.registered
                else
                    alternates, attributes, registered = { }, { }, { }
                    mathalternates = {
                        attributes = attributes,
                        alternates = alternates,
                        registered = registered,
                        presets    = { },
                        resets     = { },
                        hashes     = setmetatableindex("table")
                    }
                    resources.mathalternates = mathalternates
                end
                --
                for name, data in sortedhash(newalternates) do
                    if alternates[name] then
                        -- ignore
                    else
                        local attr = known[name]
                        attributes[attr] = data
                        alternates[name] = attr
                        registered[#registered+1] = attr
                    end
                end
            end

            if goodies then
                local done = { }
                for i=1,#goodies do
                    -- first one counts
                    -- we can consider sharing the attributes ... todo (only once scan)
                    local mathgoodies = goodies[i].mathematics
                    local alternates  = mathgoodies and mathgoodies.alternates
                    if alternates then
                        if trace_goodies then
                            report_goodies("loading alternates for font %a",tfmdata.properties.name)
                        end
                        for k, v in next, autolist do
                            if not alternates[k] then
                                alternates[k] = v
                            end
                        end
                        setthem(alternates)
                        return
                    end
                end
            end

            if trace_goodies then
                report_goodies("loading default alternates for font %a",tfmdata.properties.name)
            end
            setthem(autolist)
        end

    end

    registerotffeature {
        name        = "mathalternates",
        description = "additional math alternative shapes",
        initializers = {
            base = initializemathalternates,
            node = initializemathalternates,
        }
    }

    -- local getalternate = otf.getalternate (runtime new method so ...)

    -- todo: not shared but copies ... one never knows

    local a_mathalternate = privateattribute("mathalternate")
    local alternate       = { } -- processors.alternate = alternate
    local fontdata        = fonts.hashes.identifiers
    local fontresources   = fonts.hashes.resources

    local function getalternate(fam,tag,current)
        -- fam is always zero, so we assume a very consistent setup
        local resources = fontresources[getfontoffamily(fam)]
        local attribute = unsetvalue
        if resources then
            local mathalternates = resources.mathalternates
            if mathalternates then
                local presets = mathalternates.presets
                if presets then
                    local resets = mathalternates.resets
                    attribute = presets[tag]
                    if not attribute then
                        attribute = 0
                        local alternates = mathalternates.alternates
                        for s in gmatch(tag,"[^, ]+") do
                            if s == v_reset then
                                resets[tag] = true
                                current = unsetvalue
                            else
                                local a = alternates[s] -- or known[s]
                                if a then
                                    attribute = attribute | a
                                end
                            end
                        end
                        if attribute == 0 then
                            attribute = unsetvalue
                        end
                        presets[tag] = attribute
                    elseif resets[tag] then
                        current = unsetvalue
                    end
                end
            end
        end
        if attribute > 0 and current and current > 0 then
            return current | attribute
        else
            return attribute
        end
    end

    implement {
        name      = "presetmathfontalternate",
        arguments = "argument",
        public    = true,
        protected = true,
        actions   = function(tag)
            if texgetmode() == mathmode_code then
                texsetattribute(a_mathalternate,getalternate(0,tag))
            end
        end,
    }

    implement {
        name      = "setmathfontalternate",
        arguments = "argument",
        public    = true,
        protected = true,
        actions   = function(tag)
            if texgetmode() == mathmode_code then
                texsetattribute(a_mathalternate,getalternate(0,tag,texgetattribute(a_mathalternate)))
            end
        end,
    }

    alternate[mathchar_code] = function(pointer) -- slow
        local a = getattr(pointer,a_mathalternate)
        if a and a > 0 then
            setattr(pointer,a_mathalternate,0)
            local fontid    = getfont(pointer)
            local resources = fontresources[fontid]
            if resources then
                local mathalternates = resources.mathalternates
                if mathalternates then
                    local attributes = mathalternates.attributes
                    local registered = mathalternates.registered
                    local hashes     = mathalternates.hashes
                    for i=1,#registered do
                        local r = registered[i]
                        if (a & r) ~= 0 then
                            local char = getchar(pointer)
                            local alt  = hashes[i][char]
                            if alt == nil then
                                local what = attributes[r]
                                alt = otf.getalternate(fontdata[fontid],char,what.feature,what.value) or false
                                if alt == char then
                                    alt = false
                                end
                                hashes[i][char] = alt
                            end
                            if alt then
                                if trace_alternates then
                                    local what = attributes[r]
                                    report_alternates("alternate %a, value %a, replacing glyph %U by glyph %U",
                                        tostring(what.feature),tostring(what.value),char,alt)
                                end
                                setchar(pointer,alt)
                                break
                            end
                        end
                    end
                end
            end
        end
    end

    alternate[delimiter_code] = alternate[mathchar_code]

    function handlers.alternates(head,style,penalties)
        processnoads(head,alternate,"alternate")
        return true -- not needed
    end

end

-- italics: we assume that only characters matter
--
-- = we check for correction first because accessing nodes is slower
-- = the actual glyph is not that important (we can control it with numbers)

-- Italic correction in luatex math is (was) a mess. There are all kind of assumptions based on
-- old fonts and new fonts. Eventually there should be a flag that can signal to ignore all
-- those heuristics. We want to deal with it ourselves also in the perspective of mixed math
-- and text. Also, for a while in context we had to deal with a mix of virtual math fonts and
-- real ones.

-- in opentype the italic correction of a limop is added to the width and luatex does
-- some juggling that we want to avoid but we need to do something here (in fact, we could
-- better fix the width of the character)

do

    local a_mathitalics = privateattribute("mathitalics")

    local italics        = { }
    local default_factor = 1/20

    local setcolor     = colortracers.set
    local resetcolor   = colortracers.reset
    local italic_kern  = new_kern

    local c_positive_d = "trace:dg"
    local c_negative_d = "trace:dr"

    local function insert_kern(current,kern)
        local sub  = new_submlist() -- todo: attr
        local noad = new_noad()     -- todo: attr
        setlist(sub,kern)
        setnext(kern,noad)
        setnucleus(noad,current)
        return sub
    end

    registertracker("math.italics.visualize", function(v)
        if v then
            italic_kern = function(k)
                local n = new_kern(k) -- todo: attr
                set_visual(n,"italic")
                return n
            end
        else
            italic_kern = new_kern
        end
    end)

    local function getcorrection(method,font,char) -- -- or character.italic -- (this one is for tex)

        local visual = chardata[char].visual

        if method == 1 then
            -- check on state
            local italics = fontitalics[font]
            if italics then
                local character = fontcharacters[font][char]
                if character then
                    local correction = character.italic
                    if correction and correction ~= 0 then
                        return correction, visual
                    end
                end
            end
        elseif method == 2 then
            -- no check
            local character = fontcharacters[font][char]
            if character then
                local correction = character.italic
                if correction and correction ~= 0 then
                    return correction, visual
                end
            end
        elseif method == 3 then
            -- check on visual
            if visual == "it" or visual == "bi" then
                local character = fontcharacters[font][char]
                if character then
                    local correction = character.italic
                    if correction and correction ~= 0 then
                        return correction, visual
                    end
                end
            end
        elseif method == 4 then
            -- combination of 1 and 3
            local italics = fontitalics[font]
            if italics and (visual == "it" or visual == "bi") then
                local character = fontcharacters[font][char]
                if character then
                    local correction = character.italic
                    if correction and correction ~= 0 then
                        return correction, visual
                    end
                end
            end
        end

    end

    italics[mathchar_code] = function(pointer,what,n,parent)
        local method = getattr(pointer,a_mathitalics)
        if method and method > 0 and method < 100 then
            local char, font = getcharspec(pointer)
            local correction, visual = getcorrection(method,font,char)
            if correction and correction ~= 0 then
                local next_noad = getnext(parent)
                if not next_noad then
                    if n == 1 then
                        -- only at the outer level .. will become an option (always,endonly,none)
                        if trace_italics then
                            report_italics("method %a, flagging italic correction %p between %C and end math",method,correction,char)
                        end
                        if correction > 0 then
                            correction = correction + 100
                        else
                            correction = correction - 100
                        end
                        correction = round(correction)
                        setattr(pointer,a_mathitalics,correction)
                        setattr(parent,a_mathitalics,correction)
                        return -- so no reset later on
                    end
                end
            end
        end
        setattr(pointer,a_mathitalics,unsetvalue)
    end

    function handlers.italics(head,style,penalties)
        processnoads(head,italics,"italics")
        return true -- not needed
    end

    local enable = function()
        enableaction("math", "noads.handlers.italics")
        if trace_italics then
            report_italics("enabling math italics")
        end
        -- we enable math (unless already enabled elsewhere)
        typesetters.italics.enablemath()
        enable = false
    end

    -- best do this only on math mode (less overhead)

    function mathematics.setitalics(name)
        if enable then
            enable()
        end
        texsetattribute(a_mathitalics,name and name ~= v_reset and tonumber(name) or unsetvalue) -- maybe also v_none
    end

    function mathematics.getitalics(name)
        if enable then
            enable()
        end
        context(name and name ~= v_reset and tonumber(name) or unsetvalue)
    end

    function mathematics.resetitalics()
        texsetattribute(a_mathitalics,unsetvalue)
    end

    implement {
        name      = "initializemathitalics",
        actions   = enable,
        onlyonce  = true,
    }

    implement {
        name      = "setmathitalics",
        actions   = mathematics.setitalics,
        arguments = "string",
    }

    implement {
        name      = "getmathitalics",
        actions   = mathematics.getitalics,
        arguments = "string",
    }

    implement {
        name      = "resetmathitalics",
        actions   = mathematics.resetitalics
    }

end

do

    -- math kerns (experiment) in goodies:
    --
    -- mathematics = {
    --     kernpairs = {
    --         [0x1D44E] = {
    --             [0x1D44F] = 400, -- 𝑎𝑏
    --         }
    --     },
    -- }

    local a_kernpairs = privateattribute("mathkernpairs")
    local kernpairs   = { }

    local function enable()
        enableaction("math", "noads.handlers.kernpairs")
        if trace_kernpairs then
            report_kernpairs("enabling math kern pairs")
        end
        enable = false
    end

    implement {
        name      = "initializemathkernpairs",
        actions   = enable,
        onlyonce  = true,
    }

    local hash = setmetatableindex(function(t,font)
        local g = fontdata[font].goodies
        local m = g and g[1] and g[1].mathematics
        local k = m and m.kernpairs
        t[font] = k
        return k
    end)

    -- no correction after prime because that moved to a superscript

    kernpairs[mathchar_code] = function(pointer,what,n,parent)
        if getattr(pointer,a_kernpairs) == 1 then
            local first, firstfont = getcharspec(pointer)
            local list = hash[firstfont]
            if list then
                local found = list[first]
                if found then
                    local next = getnext(parent)
                    if next and getid(next) == noad_code then
                        pointer = getnucleus(next)
                        if pointer then
                            local second, secondfont = getcharspec(pointer)
                            if secondfont == firstfont then
                                local kern = found[second]
                                if kern then
                                    kern = kern * fontparameters[firstfont].hfactor
                                    if trace_kernpairs then
                                        report_kernpairs("adding %p kerning between %C and %C",kern,first,second)
                                    end
                                    setlink(parent,new_kern(kern),getnext(parent)) -- todo: attr
                                end
                            end
                        end
                    end
                end
            end
        end
    end

    function handlers.kernpairs(head,style,penalties)
        if use_math_goodies then
            processnoads(head,kernpairs,"kernpairs")
        end
        return true -- not needed
    end

end

-- primes and such

do

    -- is validpair stil needed? why not always now?

    local a_mathcollapsing = privateattribute("mathcollapsing")
    local collapse         = { }
    local mathlists        = characters.mathlists
    local validpair        = {
        [ordinarynoad_code]    = true,
        [operatornoad_code]    = true,
        [binarynoad_code]      = true, -- new
        [relationnoad_code]    = true,
        [opennoad_code]        = true, -- new
        [middlenoad_code]      = true, -- new
        [closenoad_code]       = true, -- new
        [punctuationnoad_code] = true, -- new
        [innernoad_code]       = false,
        [fencednoad_code]      = false,
        [undernoad_code]       = false,
        [overnoad_code]        = false,
        [vcenternoad_code]     = false,
        [fractionnoad_code]    = true,
        [radicalnoad_code]     = false,
        [accentnoad_code]      = true,
    }

    local reported = setmetatableindex("table")

    collapse[mathchar_code] = function(pointer,what,n,parent)

        if parent and mathlists[getchar(pointer)] then
            local found, last, lucleus, lsup, lsub, category
            local tree    = mathlists
            local current = parent
            while current and validpair[getsubtype(current)] do
                local nucleus = getnucleus(current) -- == pointer
                local sub     = getsub(current)
                local sup     = getsup(current)
                local char    = getchar(nucleus)
                if char then
                    local match = tree[char]
                    if match then
                        local method = getattr(current,a_mathcollapsing)
                        if method and method > 0 and method <= 3 then
                            local specials = match.specials
                            local mathlist = match.mathlist
                            local ligature
                            if method == 1 then
                                ligature = specials
                            elseif method == 2 then
                                ligature = specials or mathlist
                            else -- 3
                                ligature = mathlist or specials
                            end
                            if ligature then
                                category = mathlist and "mathlist" or "specials"
                                found    = ligature
                                last     = current
                                lucleus  = nucleus
                                lsup     = sup
                                lsub     = sub
                            end
                            tree = match
                            if sub or sup then
                                break
                            else
                                current = getnext(current)
                            end
                        else
                            break
                        end
                    else
                        break
                    end
                else
                    break
                end
            end
            if found and last and lucleus then
                local id         = getfont(lucleus)
                local characters = fontcharacters[id]
                local replace    = characters and characters[found]
                if not replace then
                    if not reported[id][found] then
                        reported[id][found] = true
                        report_collapsing("%s ligature %C from %s","ignoring",found,category)
                    end
                elseif trace_collapsing then
                    report_collapsing("%s ligature %C from %s","creating",found,category)
                end
                setchar(pointer,found)
                local l = getnext(last)
                local c = getnext(parent)
                if lsub then
                    setsub(parent,lsub)
                    setsub(last)
                end
                if lsup then
                    setsup(parent,lsup)
                    setsup(last)
                end
                while c ~= l do
                    local n = getnext(c)
                    flushnode(c)
                    c = n
                end
                setlink(parent,l)
            end
        end
    end

    function noads.handlers.collapse(head,style,penalties)
        processnoads(head,collapse,"collapse")
        return true -- not needed
    end

    local enable = function()
        enableaction("math", "noads.handlers.collapse")
        if trace_collapsing then
            report_collapsing("enabling math collapsing")
        end
        enable = false
    end

    implement {
        name      = "initializemathcollapsing",
        actions   = enable,
        onlyonce  = true,
    }

end

do

    local fixscripts = { }

    local primes = {
        -- primes
        [0x2032] = true,
        [0x2033] = true,
        [0x2034] = true,
        [0x2057] = true,
        -- reverse primes
        [0x2035] = true,
        [0x2036] = true,
        [0x2037] = true,
    }

    local fixable = {
        [noad_code]     = true,
        [accent_code]   = true,
        [radical_code]  = true,
        [fraction_code] = true,
    }

    -- [prime|sub|sup]first

    fixscripts[mathchar_code] = function(pointer,what,n,parent,nested) -- todo: switch to turn it on and off
        if parent then
            local char = getchar(pointer)
            if char and primes[char] then
                local nuc = getnucleus(parent)
                if pointer == nuc then
                    local prev = getprev(parent)
                    if prev and fixable[getid(prev)] then
                        local prevsup  = getsup(prev)
                        local prevsub  = getsub(prev)
                        local primesup = getsup(parent)
                        local primesub = getsub(parent)
                        setfield(prev,"scriptorder",prevsub and 2 or 1) -- sub first, then prime
                        if primesup and not prevsup then
                            setsup(prev,primesup)
                            primesup = nil
                        end
                        if primesub and not prevsub then
                            setsub(prev,primesub)
                            primesub = nil
                        end
                        setprime(prev,nuc)
                        setnucleus(parent)
                        if not primesup then
                            setsup(parent)
                        end
                        if not primesub then
                            setsub(parent)
                        end
                        if not (primesup or primesub) then
                            setlink(prev,getnext(parent))
                            flushnode(parent)
                            return true, prev, prev
                        end
                    end
                end
            end
        end
    end

    function noads.handlers.fixscripts(head,style,penalties)
        processnoads(head,fixscripts,"fixscripts")
        return true -- not needed
    end

end

-- variants (upgraded for script too)

do

    local variants  = { }
    local chardata  = characters.data
    local a_variant = privateattribute("mathvariant")

    local function setvariant(pointer,selector,char)
        local tfmdata      = fontdata[getfont(pointer)]
        local mathvariants = tfmdata.resources.variants -- and variantdata / can be a hash
        if mathvariants then
            mathvariants = mathvariants[selector]
            if mathvariants then
                local variant = mathvariants[char]
                if variant then
                    setchar(pointer,variant)
                    setattr(pointer,a_exportstatus,char) -- we don't export the variant as it's visual markup
                    if trace_variants then
                        report_variants("variant (%U,%U) replaced by %U",char,selector,variant)
                    end
                else
                    if trace_variants then
                        report_variants("no variant (%U,%U)",char,selector)
                    end
                end
            end
        end
    end

    variants[mathchar_code] = function(pointer,what,n,parent) -- also set export value
        local char = getchar(pointer)
        local data = chardata[char]
        if data then
            local variants = data.variants
            if variants then
                local next = getnext(parent)
                if next and getid(next) == noad_code then
                    local nucleus = getnucleus(next)
                    if nucleus and getid(nucleus) == mathchar_code then
                        local selector = getchar(nucleus)
                        if variants[selector] then
                            setvariant(pointer,selector,char)
                            setprev(next,pointer)
                            setnext(parent,getnext(next))
                            flushnode(next)
                        end
                    end
                end
                local selector = getattr(pointer,a_variant)
                if selector and variants[selector] then
                    setvariant(pointer,selector,char)
                end
            end
        end
    end

    function mathematics.addvariant(tfmdata,char,variant,selector)
        if char and variant and selector then
            local data = chardata[char]
            if data then
                local variants = data.variants
                if variants and variants[selector] then
                    local resources = tfmdata.resources
                    local variants  = resources.variants -- and variantdata
                    if not variants then
                        variants           = { }
                        resources.variants = variants
                    end
                    local selectors = variants[selector]
                    if not selectors then
                        selectors          = { }
                        variants[selector] = selectors
                    end
                    selectors[char] = variant
                    return true
                end
            end
        end
        return false
    end

    function handlers.variants(head,style,penalties)
        processnoads(head,variants,"unicode variant")
        return true -- not needed
    end

    local valid = {
        calligraphic = 0xFE00,
        calligraphy  = 0xFE00,
        script       = 0xFE01,
        handwriting  = 0xFE01,
    }

    function mathematics.setvariant(s)
        texsetattribute(a_variant,valid[s] or unsetvalue)
    end

    implement {
        name      = "setmathvariant",
        public    = true,
        protected = true,
        arguments = "argument",
        actions   = mathematics.setvariant,
    }

end

-- for manuals

do

    -- Given the amount of classes this no longer makes much sense or we need to
    -- extend it.

    local classes = { }
    local colors  = {
        [relationnoad_code]    = "trace:dr",
        [ordinarynoad_code]    = "trace:db",
        [binarynoad_code]      = "trace:dg",
        [opennoad_code]        = "trace:dm",
        [middlenoad_code]      = "trace:dm",
        [closenoad_code]       = "trace:dm",
        [punctuationnoad_code] = "trace:dc",
     -- [operatornoad_code]    = "",
     -- [innernoad_code        = "",
     -- [fencednoad_code       = "",
     -- [undernoad_code]       = "",
     -- [overnoad_code]        = "",
     -- [vcenternoad_code]     = "",
     -- [fractionnoad_code]    = "",
     -- [radicalnoad_code]     = "",
    }

    local setcolor   = colortracers.set
    local resetcolor = colortracers.reset

    classes[mathchar_code] = function(pointer,what,n,parent)
        local color = colors[getsubtype(parent)]
        if color then
            setcolor(pointer,color)
        else
            resetcolor(pointer)
        end
    end

    function handlers.classes(head,style,penalties)
        processnoads(head,classes,"classes")
        return true -- not needed
    end

    registertracker("math.classes",function(v)
        setaction("math","noads.handlers.classes",v)
    end)

end


do

    local traversehlist = nuts.traversers.hlist

    local getshift      = nuts.getshift
    local setwhd        = nuts.setwhd
    local setshift      = nuts.setshift

    -- normalizer: can become engine feature (native tex loves shifts)

    local function normalize(h)
        for n, s in traversehlist, h do
            if s > 0 then
                local sh = getshift(n)
                local ox, oy = getoffsets(n)
                if sh ~= 0 then
                    local w, h, d = getwhd(n)
                    h = h - sh
                    d = d + sh
                    setshift(n)
                    setwhd(n,w,h > 0 and h or 0,d > 0 and d or 0)
                    setoffsets(n,ox,oy - sh)
                end
            end
            local l = getlist(l)
            if l then
                normalize(l)
            end
        end
    end

    function handlers.normalize(h)
        return normalize(h)
    end

end

do

    local traversehlist = nuts.traversers.hlist

    local texgetdimen   = tex.getdimen
    local texgetglue    = tex.getglue
    local texgetcount   = tex.getcount

    local newrule       = nuts.pool.outlinerule
    local newkern       = nuts.pool.kern
    local setcolor      = nodes.tracers.colors.set

    local a_mathsnap    = attributes.private("mathsnap")

    local trace_snapping  = false  registertracker("math.snapping", function(v) trace_snapping = v end)
    local report_snapping = logreporter("mathematics","snapping")

    function handlers.snap(h,_,_,_,_,level)
     -- if not level or level == 0 then
        if texgetcount("mathnestinglevel") == 1 then
            local trace_color
            if trace_snapping == "frame" then
                trace_color = "darkgray"
            elseif type(trace_snapping) == "string" then
                trace_color = trace_snapping
            else
                trace_color = false
            end
            local ht, dp, dd, hs, ds, hd
            for n, s in traversehlist, h do
                local step = getattr(n,a_mathsnap)
                if step then
                    local done = false
                    if not dd then
                        ht = texgetdimen("mathstrutht")
                        dp = texgetdimen("mathstrutdp")
                        hd = ht + dp
                        -- lineskip can be large in alignments
                     -- dd = hd / 12
                        dd = hd / 6
                        if step == 0xFFFF then
                            hs = dd
                            ds = dd
                        else
                            hs = ht/step
                            ds = dp/step
                        end
                    end
                    local w, h, d = getwhd(n)
                    -- snap to line
                  ::height::
                    if h-dd < ht then
                        if trace_snapping == true then
                            report_snapping("adapting ht: old %p, new %p, lineskip %p",h,ht,dd)
                        end
                        done = true
                        setheight(n,ht)
                        goto depth
                    end
                    if h > ht then
--                         while ht < (h-dd) do
                        while ht < h do
                            ht = round(ht + hs)
                        end
                        if h ~= ht then
                            setheight(n,ht)
                            if trace_snapping == true then
                                report_snapping("enlarging ht: old %p, new %p, step %p",h,ht,hs)
                            end
                            done = true
                        end
                    end
                  ::depth::
                    if d-dd < dp then
                        if trace_snapping == true then
                            report_snapping("adapting dp: old %p, new %p, lineskip %p",d,dp,dd)
                        end
                        setdepth(n,dp)
                        done = true
                        goto done
                    end
                    if d > dp then
--                         while dp < (d-dd) do
                        while dp < d do
                            dp = round(dp + ds)
                        end
                        if d ~= dp then
                            setdepth(n,dp)
                            if trace_snapping == true then
                                report_snapping("enlarging dp: old %p, new %p, step %p",d,dp,ds)
                            end
                            done = true
                        end
                    end
                 ::done::
                    if done and trace_color then
                     -- w, h, d = getwhd(n)
                     -- local r = newrule(w,h,d,65536)
                     -- setcolor(r,trace_color)
                     -- setlink(r,newkern(-w),getlist(n))
                     -- setlist(n,r)

                        local old = newrule(w,h,d,65536)
                        setcolor(old,"middlegray")
                        w, h, d = getwhd(n)
                        local new = newrule(w,h,d,65536/4)
                        setcolor(new,trace_color)
                        setlink(old,newkern(-w),new,newkern(-w),getlist(n))
                        local ox, oy = getoffsets(n)
                        setoffsets(old,-ox,-oy)
                        setoffsets(new,-ox,-oy)
                        setlist(n,old)
                    end
                end
            end
        end
    end

    local valid = {
        [v_reset]  = unsetvalue,
        [v_line]   = 0xFFFF,
        [v_small]  = 8,
        [v_medium] = 4,
        [v_big]    = 2,
    }

    function mathematics.setsnapping(s)
        if not enabled then
            enableaction("math", "noads.handlers.snap")
            enabled = true
        end
        texsetattribute(a_mathsnap,valid[s] or unsetvalue)
    end

    implement {
        name      = "setmathsnapping",
        public    = true,
        protected = true,
        arguments = "argument",
        actions   = mathematics.setsnapping,
    }

end

-- experimental : replaced by dictionaries but for now we keep the code
--
-- do
--
--  -- mathematics.registerdomain {
--  --     name       = "foo",
--  --     parents    = { "bar" },
--  --     characters = {
--  --         [0x123] = { char = 0x234, class = binary },
--  --     },
--  -- }
--
--     local trace_domains  = false  registertracker("math.domains", function(v) trace_domains = v end)
--     local report_domains = logreporter("mathematics","domains")
--
--     local domains       = { }
--     local categories    = { }
--     local numbers       = { }
--     local a_mathdomain  = privateattribute("mathdomain")
--     mathematics.domains = categories
--     local permitted     = {
--         ordinary    = ordinarynoad_code,
--         binary      = binarynoad_code,
--         relation    = relationnoad_code,
--         punctuation = punctuationnoad_code,
--         inner       = innernoad_code,
--         fenced      = fencednoad_code,
--      -- fraction    = fractionnoad_code,
--      -- radical     = radicalnoad_code,
--     }
--
--     function mathematics.registerdomain(data)
--         local name = data.name
--         if not name then
--             return
--         end
--         local attr       = #numbers + 1
--         categories[name] = data
--         numbers[attr]    = data
--         data.attribute   = attr
--         -- we delay hashing
--         return attr
--     end
--
--     local enable
--
--     enable = function()
--         enableaction("math", "noads.handlers.domains")
--         if trace_domains then
--             report_domains("enabling math domains")
--         end
--         enable = false
--     end
--
--     function mathematics.setdomain(name)
--         if enable then
--             enable()
--         end
--         local data = name and name ~= v_reset and categories[name]
--         texsetattribute(a_mathdomain,data and data.attribute or unsetvalue)
--     end
--
--     function mathematics.getdomain(name)
--         if enable then
--             enable()
--         end
--         local data = name and name ~= v_reset and categories[name]
--         context(data and data.attribute or unsetvalue)
--     end
--
--     implement {
--         name      = "initializemathdomain",
--         actions   = enable,
--         onlyonce  = true,
--     }
--
--     implement {
--         name      = "setmathdomain",
--         arguments = "string",
--         actions   = mathematics.setdomain,
--     }
--
--     implement {
--         name      = "getmathdomain",
--         arguments = "string",
--         actions   = mathematics.getdomain,
--     }
--
--     local function makehash(data)
--         local hash    = { }
--         local parents = data.parents
--         if parents then
--             local function merge(name)
--                 if name then
--                     local c = categories[name]
--                     if c then
--                         local hash = c.hash
--                         if not hash then
--                             hash = makehash(c)
--                         end
--                         for k, v in next, hash do
--                             hash[k] = v
--                         end
--                     end
--                 end
--             end
--             if type(parents) == "string" then
--                 merge(parents)
--             elseif type(parents) == "table" then
--                 for i=1,#parents do
--                     merge(parents[i])
--                 end
--             end
--         end
--         local characters = data.characters
--         if characters then
--             for k, v in next, characters do
--              -- local chr = n.char
--                 local cls = v.class
--                 if cls then
--                     v.code = permitted[cls]
--                 else
--                     -- invalid class
--                 end
--                 hash[k] = v
--             end
--         end
--         data.hash = hash
--         return hash
--     end
--
--     domains[mathchar_code] = function(pointer,what,n,parent)
--         local attr = getattr(pointer,a_mathdomain)
--         if attr then
--             local domain = numbers[attr]
--             if domain then
--                 local hash = domain.hash
--                 if not hash then
--                     hash = makehash(domain)
--                 end
--                 local char = getchar(pointer)
--                 local okay = hash[char]
--                 if okay then
--                     local chr = okay.char
--                     local cls = okay.code
--                     if chr and chr ~= char then
--                         setchar(pointer,chr)
--                     end
--                     if cls and cls ~= getsubtype(parent) then
--                         setsubtype(parent,cls)
--                     end
--                 end
--             end
--         end
--     end
--
--     function handlers.domains(head,style,penalties)
--         processnoads(head,domains,"domains")
--         return true -- not needed
--     end
--
-- end

-- just for me

function handlers.showtree(head,style,penalties)
    inspect(nodes.totree(tonut(head)))
end

registertracker("math.showtree",function(v)
    setaction("math","noads.handlers.showtree",v)
end)

-- also for me

do

    local applyvisuals = nuts.applyvisuals
    local visual       = false

    function handlers.makeup(head)
        applyvisuals(head,visual)
    end

    registertracker("math.makeup",function(v)
        visual = v
        setaction("math","noads.handlers.makeup",v)
    end)

end

-- Musical timestamp: August 2022 with "Meditation by Cory Wong (Live @ Brooklyn
-- Steel FEB 2022). Seen live earlier that year and its gets better and better!
--
-- As we also try to do here:

do

    local trace_dictionaries  = false  registertracker("math.dictionaries", function(v) trace_dictionaries = v end)
    local report_dictionaries = logreporter("mathematics","dictionaries")

    local setnodecolor = colortracers.set
    local getchardict  = nuts.getchardict
    local setchardict  = nuts.setchardict

    local dictionaries = { }  noads.processors.dictionaries = dictionaries
    local groups       = mathematics.dictionaries.groups
    local sets         = mathematics.dictionaries.sets
    local variants     = mathematics.dictionaries.variants
    local defaults     = mathematics.dictionaries.defaults

    local function check(pointer,group,index)
        local v = variants[index]
        if v and v[group] then
            return group, class
        end
        return 1
    end

    dictionaries[mathchar_code] = function(pointer,what,n,parent)
        local properties, oldgroup, index, font, char = getchardict(pointer)
        local newgroup = 1
        local newclass = false
        local oldclass = getsubtype(pointer)
        if (properties & 0x1) == 0x1 then
            newclass = oldclass
            newgroup = oldgroup
        else
            local set = sets[oldgroup]
            if set then
                local groups    = set.groups
                local nofgroups = groups and #groups
                if nofgroups > 0 then
                    for i=1,nofgroups do
                        real, class = check(pointer,groups[i],index)
                        if real ~= 1  then
                            goto done
                        end
                    end
                end
            else
                newgroup, newclass = check(pointer,group,index)
            end
          ::done::
            if newgroup == 1 then
                newgroup = defaults[index] or 1
            end
            setchardict(pointer,properties,newgroup,index)
            if type(newclass) == "number" then
                setsubtype(parent,newclass)
            else
                newclass = oldclass
            end
        end
        if trace_dictionaries then
            if newgroup > 1 then
                local groupname = groups[newgroup]
                if groupname then
                    setnodecolor(pointer,"dictionary:"..groupname)
                end
            end
            report_dictionaries("group 0x%02X -> 0x%02X, class 0x%02X -> 0x%02X, index %05X, %U %c",oldgroup,newgroup,oldclass,newclass,index,char,char)
        end
    end

    function handlers.dictionaries(head,style,penalties)
        processnoads(head,dictionaries,"dictionaries")
    end

end

do

    local trace_suspicious  = false  registertracker("math.suspicious", function(v) trace_suspicious = v end)
    local report_suspicious = logreporter("mathematics","suspicious")

    local suspicious = { }  noads.processors.suspicious = suspicious

    local candidates = {
        [mathematics.classes.maybeordinary] = "maybeordinary",
        [mathematics.classes.mayberelation] = "mayberelation",
        [mathematics.classes.maybebinary  ] = "maybebinary",
    }
    local registered = setmetatableindex("table")

    suspicious[mathchar_code] = function(pointer,what,n,parent)
        local class = getsubtype(pointer)
        local found = candidates[class]
        if found then
            local char = getchar(pointer)
            if not registered[class][char] then
                report_suspicious("class %a, %U %c",found,char,char)
                registered[class][char] = true
            end
        end
    end

    function handlers.suspicious(head,style,penalties)
        if trace_suspicious then
            processnoads(head,suspicious,"suspicious")
        end
    end

end

-- the normal builder

-- do
--
--     local force_penalties = false
--
--     function builders.kernel.mlisttohlist(head,style,penalties)
--         return mlisttohlist(head,style,force_penalties or penalties)
--     end
--
--     implement {
--         name      = "setmathpenalties",
--         arguments = "integer",
--         actions   = function(p)
--             force_penalties = p > 0
--         end,
--     }
--
-- end

builders.kernel.mlisttohlist = mlisttohlist

local actions = tasks.actions("math") -- head, style, penalties

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

function processors.mlisttohlist(head,style,penalties,beginclass,endclass,level)
    starttiming(noads)
    head = actions(head,style,penalties,beginclass,endclass,level)
    stoptiming(noads)
    return head
end

callbacks.register('mlist_to_hlist',processors.mlisttohlist,"preprocessing math list")

-- tracing

statistics.register("math processing time", function()
    return statistics.elapsedseconds(noads)
end)