summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/luaotfload/fontloader-font-ots.lua
blob: 48f85c3658d01300ac6751d096314b36891e4688 (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
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
if not modules then modules = { } end modules ['font-ots'] = { -- sequences
    version   = 1.001,
    optimize  = true,
    comment   = "companion to font-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files",
}

-- I need to check the description at the microsoft site ... it has been improved so
-- maybe there are some interesting details there. Most below is based on old and
-- incomplete documentation and involved quite a bit of guesswork (checking with the
-- abstract uniscribe of those days. But changing things is tricky!
--
-- This module is a bit more split up that I'd like but since we also want to test
-- with plain TeX it has to be so. This module is part of ConTeXt and discussion
-- about improvements and functionality mostly happens on the ConTeXt mailing list.
--
-- The specification of OpenType is (or at least decades ago was) kind of vague.
-- Apart from a lack of a proper free specifications there's also the problem that
-- Microsoft and Adobe may have their own interpretation of how and in what order to
-- apply features. In general the Microsoft website has more detailed specifications
-- and is a better reference. There is also some information in the FontForge help
-- files. In the end we rely most on the Microsoft specification.
--
-- Because there is so much possible, fonts might contain bugs and/or be made to
-- work with certain rederers. These may evolve over time which may have the side
-- effect that suddenly fonts behave differently. We don't want to catch all font
-- issues.
--
-- After a lot of experiments (mostly by Taco, me and Idris) the first
-- implementation was already quite useful. When it did most of what we wanted, a
-- more optimized version evolved. Of course all errors are mine and of course the
-- code can be improved. There are quite some optimizations going on here and
-- processing speed is currently quite acceptable and has been improved over time.
-- Many complex scripts are not yet supported yet, but I will look into them as soon
-- as ConTeXt users ask for it.
--
-- The specification leaves room for interpretation. In case of doubt the Microsoft
-- implementation is the reference as it is the most complete one. As they deal with
-- lots of scripts and fonts, Kai and Ivo did a lot of testing of the generic code
-- and their suggestions help improve the code. I'm aware that not all border cases
-- can be taken care of, unless we accept excessive runtime, and even then the
-- interference with other mechanisms (like hyphenation) are not trivial.
--
-- Especially discretionary handling has been improved much by Kai Eigner who uses
-- complex (latin) fonts. The current implementation is a compromis between his
-- patches and my code and in the meantime performance is quite ok. We cannot check
-- all border cases without compromising speed but so far we're okay. Given good
-- test cases we can probably improve it here and there. Especially chain lookups
-- are non trivial with discretionaries but things got much better over time thanks
-- to Kai.
--
-- Glyphs are indexed not by unicode but in their own way. This is because there is
-- no relationship with unicode at all, apart from the fact that a font might cover
-- certain ranges of characters. One character can have multiple shapes. However, at
-- the TeX end we use unicode so and all extra glyphs are mapped into a private
-- space. This is needed because we need to access them and TeX has to include then
-- in the output eventually.
--
-- The initial data table is rather close to the open type specification and also
-- not that different from the one produced by Fontforge but we uses hashes instead.
-- In ConTeXt that table is packed (similar tables are shared) and cached on disk so
-- that successive runs can use the optimized table (after loading the table is
-- unpacked).
--
-- This module is sparsely documented because it is has been a moving target. The
-- table format of the reader changed a bit over time and we experiment a lot with
-- different methods for supporting features. By now the structures are quite stable
--
-- Incrementing the version number will force a re-cache. We jump the number by one
-- when there's a fix in the reader or processing code that can result in different
-- results.
--
-- This code is also used outside ConTeXt but in ConTeXt it has to work with other
-- mechanisms. Both put some constraints on the code here.
--
-- Remark: We assume that cursives don't cross discretionaries which is okay because
-- it is only used in semitic scripts.
--
-- Remark: We assume that marks precede base characters.
--
-- Remark: When complex ligatures extend into discs nodes we can get side effects.
-- Normally this doesn't happen; ff\d{l}{l}{l} in lm works but ff\d{f}{f}{f}.
--
-- Todo: check if we copy attributes to disc nodes if needed.
--
-- Todo: it would be nice if we could get rid of components. In other places we can
-- use the unicode properties. We can just keep a lua table.
--
-- Remark: We do some disc juggling where we need to keep in mind that the pre, post
-- and replace fields can have prev pointers to a nesting node ... I wonder if that
-- is still needed.
--
-- Remark: This is not possible:
--
-- \discretionary {alpha-} {betagammadelta}
--   {\discretionary {alphabeta-} {gammadelta}
--      {\discretionary {alphabetagamma-} {delta}
--         {alphabetagammadelta}}}
--
-- Remark: Something is messed up: we have two mark / ligature indices, one at the
-- injection end and one here ... this is based on KE's patches but there is something
-- fishy there as I'm pretty sure that for husayni we need some connection (as it's much
-- more complex than an average font) but I need proper examples of all cases, not of
-- only some.
--
-- Remark: I wonder if indexed would be faster than unicoded. It would be a major
-- rewrite to have char being unicode + an index field in glyph nodes. Also more
-- assignments have to be made in order to keep things in sync. So, it's a no-go.
--
-- Remark: We can provide a fast loop when there are no disc nodes (tests show a 1%
-- gain). Smaller functions might perform better cache-wise. But ... memory becomes
-- faster anyway, so ...
--
-- Remark: Some optimizations made sense for 5.2 but seem less important for 5.3 but
-- anyway served their purpose.
--
-- Todo: just (0=l2r and 1=r2l) or maybe (r2l = true)

-- Experiments with returning the data with the ischar are positive for lmtx but
-- have a performance hit on mkiv because there we need to wrap ischardata (pending
-- extensions to luatex which is unlikely to happen for such an experiment because
-- we then can't remove it). Actually it might make generic slightly faster. Also,
-- there are some corner cases where a data check comes before a char fetch and
-- we're talking of millions of calls there. At some point I might make a version
-- for lmtx that does it slightly different anyway.

local type, next, tonumber = type, next, tonumber
local random = math.random
local formatters = string.formatters
local insert = table.insert

local registertracker      = trackers.register

local logs                 = logs
local trackers             = trackers
local nodes                = nodes
local attributes           = attributes
local fonts                = fonts

local otf                  = fonts.handlers.otf
local tracers              = nodes.tracers

local trace_singles        = false  registertracker("otf.singles",      function(v) trace_singles      = v end)
local trace_multiples      = false  registertracker("otf.multiples",    function(v) trace_multiples    = v end)
local trace_alternatives   = false  registertracker("otf.alternatives", function(v) trace_alternatives = v end)
local trace_ligatures      = false  registertracker("otf.ligatures",    function(v) trace_ligatures    = v end)
local trace_contexts       = false  registertracker("otf.contexts",     function(v) trace_contexts     = v end)
local trace_marks          = false  registertracker("otf.marks",        function(v) trace_marks        = v end)
local trace_kerns          = false  registertracker("otf.kerns",        function(v) trace_kerns        = v end)
local trace_cursive        = false  registertracker("otf.cursive",      function(v) trace_cursive      = v end)
local trace_preparing      = false  registertracker("otf.preparing",    function(v) trace_preparing    = v end)
local trace_bugs           = false  registertracker("otf.bugs",         function(v) trace_bugs         = v end)
local trace_details        = false  registertracker("otf.details",      function(v) trace_details      = v end)
local trace_steps          = false  registertracker("otf.steps",        function(v) trace_steps        = v end)
local trace_skips          = false  registertracker("otf.skips",        function(v) trace_skips        = v end)
local trace_plugins        = false  registertracker("otf.plugins",      function(v) trace_plugins      = v end)
local trace_chains         = false  registertracker("otf.chains",       function(v) trace_chains       = v end)

local trace_kernruns       = false  registertracker("otf.kernruns",     function(v) trace_kernruns     = v end)
----- trace_discruns       = false  registertracker("otf.discruns",     function(v) trace_discruns     = v end)
local trace_compruns       = false  registertracker("otf.compruns",     function(v) trace_compruns     = v end)
local trace_testruns       = false  registertracker("otf.testruns",     function(v) trace_testruns     = v end)

local forcediscretionaries = false
local forcepairadvance     = false -- for testing

local repeatablemultiples  = context or false

directives.register("otf.forcediscretionaries", function(v) forcediscretionaries = v end)
directives.register("otf.forcepairadvance",     function(v) forcepairadvance     = v end)

local report_direct      = logs.reporter("fonts","otf direct")
local report_subchain    = logs.reporter("fonts","otf subchain")
local report_chain       = logs.reporter("fonts","otf chain")
local report_process     = logs.reporter("fonts","otf process")
local report_warning     = logs.reporter("fonts","otf warning")
local report_run         = logs.reporter("fonts","otf run")

registertracker("otf.substitutions", "otf.singles","otf.multiples","otf.alternatives","otf.ligatures")
registertracker("otf.positions",     "otf.marks","otf.kerns","otf.cursive")
registertracker("otf.actions",       "otf.substitutions","otf.positions")
registertracker("otf.sample",        "otf.steps","otf.substitutions","otf.positions","otf.analyzing")
registertracker("otf.sample.silent", "otf.steps=silent","otf.substitutions","otf.positions","otf.analyzing")

local nuts               = nodes.nuts

local getnext            = nuts.getnext
local setnext            = nuts.setnext
local getprev            = nuts.getprev
local setprev            = nuts.setprev
local getboth            = nuts.getboth
local setboth            = nuts.setboth
local getid              = nuts.getid
local getstate           = nuts.getstate
local getsubtype         = nuts.getsubtype
local getchar            = nuts.getchar
local setchar            = nuts.setchar
local getdisc            = nuts.getdisc
local setdisc            = nuts.setdisc
local getreplace         = nuts.getreplace
local setlink            = nuts.setlink
local getwidth           = nuts.getwidth
local getattr            = nuts.getattr

local getglyphdata       = nuts.getglyphdata

---------------------------------------------------------------------------------------

-- Beware: In ConTeXt components no longer are real components. We only keep track of
-- their positions because some complex ligatures might need that. For the moment we
-- use an x_ prefix because for now generic follows the other approach.

local components         = nuts.components
local copynocomponents   = components.copynocomponents
local copyonlyglyphs     = components.copyonlyglyphs
local countcomponents    = components.count
local setcomponents      = components.set
local getcomponents      = components.get
local flushcomponents    = components.flush

---------------------------------------------------------------------------------------

local ischar             = nuts.ischar
local usesfont           = nuts.usesfont

local insertnodeafter    = nuts.insertafter
local copynode           = nuts.copy
local copynodelist       = nuts.copylist
local removenode         = nuts.remove
local findnodetail       = nuts.tail
local flushnodelist      = nuts.flushlist
local flushnode          = nuts.flushnode
local endofmath          = nuts.endofmath

local startofpar         = nuts.startofpar

local setmetatable       = setmetatable
local setmetatableindex  = table.setmetatableindex

local nextnode           = nuts.traversers.node

local nodecodes          = nodes.nodecodes
local glyphcodes         = nodes.glyphcodes

local glyph_code         = nodecodes.glyph
local glue_code          = nodecodes.glue
local disc_code          = nodecodes.disc
local math_code          = nodecodes.math
local dir_code           = nodecodes.dir
local par_code           = nodecodes.par

local lefttoright_code   = nodes.dirvalues.lefttoright
local righttoleft_code   = nodes.dirvalues.righttoleft

local discretionarydisc_code = nodes.disccodes.discretionary

local a_noligature       = attributes.private("noligature")

local injections         = nodes.injections
local setmark            = injections.setmark
local setcursive         = injections.setcursive
local setkern            = injections.setkern
local setmove            = injections.setmove
local setposition        = injections.setposition
local resetinjection     = injections.reset
local copyinjection      = injections.copy
local setligaindex       = injections.setligaindex
local getligaindex       = injections.getligaindex

local fontdata           = fonts.hashes.identifiers
local fontfeatures       = fonts.hashes.features

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

local onetimemessage     = fonts.loggers.onetimemessage or function() end

local getrandom          = utilities and utilities.randomizer and utilities.randomizer.get

otf.defaultnodealternate = "none" -- first last

-- We use a few semi-global variables. The handler can be called nested but this assumes
-- that the same font is used.

local tfmdata            = false
local characters         = false
local descriptions       = false
local marks              = false
local classes            = false
local currentfont        = false
local factor             = 0
local threshold          = 0
local checkmarks         = false

local discs              = false
local spaces             = false

local sweepnode          = nil
local sweephead          = { } -- we don't nil entries but false them (no collection and such)

local notmatchpre        = { } -- to be checked: can we use false instead of nil / what if a == b tests
local notmatchpost       = { } -- to be checked: can we use false instead of nil / what if a == b tests
local notmatchreplace    = { } -- to be checked: can we use false instead of nil / what if a == b tests

local handlers           = { }

local isspace            = injections.isspace
local getthreshold       = injections.getthreshold

local checkstep          = (tracers and tracers.steppers.check)    or function() end
local registerstep       = (tracers and tracers.steppers.register) or function() end
local registermessage    = (tracers and tracers.steppers.message)  or function() end

local function logprocess(...)
    if trace_steps then
        registermessage(...)
        if trace_steps == "silent" then
            return
        end
    end
    report_direct(...)
end

local function logwarning(...)
    report_direct(...)
end

local gref  do

    local f_unicode = formatters["U+%X"]      -- was ["%U"]
    local f_uniname = formatters["U+%X (%s)"] -- was ["%U (%s)"]
    local f_unilist = formatters["% t"]

    gref = function(n) -- currently the same as in font-otb
        if type(n) == "number" then
            local description = descriptions[n]
            local name = description and description.name
            if name then
                return f_uniname(n,name)
            else
                return f_unicode(n)
            end
        elseif n then
            local t = { }
            for i=1,#n do
                local ni = n[i]
                if tonumber(ni) then -- later we will start at 2
                    local di = descriptions[ni]
                    local nn = di and di.name
                    if nn then
                        t[#t+1] = f_uniname(ni,nn)
                    else
                        t[#t+1] = f_unicode(ni)
                    end
                end
            end
            return f_unilist(t)
        else
            return "<error in node mode tracing>"
        end
    end

end

local function cref(dataset,sequence,index)
    if not dataset then
        return "no valid dataset"
    end
    local merged = sequence.merged and "merged " or ""
    if index and index > 1 then
        return formatters["feature %a, type %a, %schain lookup %a, index %a"](
            dataset[4],sequence.type,merged,sequence.name,index)
    else
        return formatters["feature %a, type %a, %schain lookup %a"](
            dataset[4],sequence.type,merged,sequence.name)
    end
end

local function pref(dataset,sequence)
    return formatters["feature %a, type %a, %slookup %a"](
        dataset[4],sequence.type,sequence.merged and "merged " or "",sequence.name)
end

local function mref(rlmode)
    if not rlmode or rlmode >= 0 then
        return "l2r"
    else
        return "r2l"
    end
end

-- The next code is somewhat complicated by the fact that some fonts can have ligatures made
-- from ligatures that themselves have marks. This was identified by Kai in for instance
-- arabtype:  KAF LAM SHADDA ALEF FATHA (0x0643 0x0644 0x0651 0x0627 0x064E). This becomes
-- KAF LAM-ALEF with a SHADDA on the first and a FATHA op de second component. In a next
-- iteration this becomes a KAF-LAM-ALEF with a SHADDA on the second and a FATHA on the
-- third component.

-- We can assume that languages that use marks are not hyphenated. We can also assume
-- that at most one discretionary is present.

-- We do need components in funny kerning mode but maybe I can better reconstruct then
-- as we do have the font components info available; removing components makes the
-- previous code much simpler. Also, later on copying and freeing becomes easier.
-- However, for arabic we need to keep them around for the sake of mark placement
-- and indices.

local function flattendisk(head,disc)
    local pre, post, replace, pretail, posttail, replacetail = getdisc(disc,true)
    local prev, next = getboth(disc)
    local ishead = head == disc
    setdisc(disc)
    flushnode(disc)
    if pre then
        flushnodelist(pre)
    end
    if post then
        flushnodelist(post)
    end
    if ishead then
        if replace then
            if next then
                setlink(replacetail,next)
            end
            return replace, replace
        elseif next then
            return next, next
        else
         -- return -- maybe warning
        end
    else
        if replace then
            if next then
                setlink(replacetail,next)
            end
            setlink(prev,replace)
            return head, replace
        else
            setlink(prev,next) -- checks for next anyway
            return head, next
        end
    end
end

local function appenddisc(disc,list)
    local pre, post, replace, pretail, posttail, replacetail = getdisc(disc,true)
    local posthead    = list
    local replacehead = copynodelist(list)
    if post then
        setlink(posttail,posthead)
    else
        post = posthead
    end
    if replace then
        setlink(replacetail,replacehead)
    else
        replace = replacehead
    end
    setdisc(disc,pre,post,replace)
end

local function markstoligature(head,start,stop,char)
    if start == stop and getchar(start) == char then
        return head, start
    else
        local prev = getprev(start)
        local next = getnext(stop)
        setprev(start)
        setnext(stop)
        local base = copynocomponents(start,copyinjection)
        if head == start then
            head = base
        end
        resetinjection(base)
        setchar(base,char)
        setcomponents(base,start)
        setlink(prev,base,next)
        flushcomponents(start)
        return head, base
    end
end

-- Remark for Kai: (some arabic fonts do mark + mark = other mark and such)
--
-- The hasmarks is needed for ligatures of marks that are part of a ligature in
-- which case we assume that we can delete the marks anyway (we can always become
-- more clever if needed) .. in fact the whole logic here should be redone. We're
-- in the not discfound branch then. We now have skiphash too so we can be more
-- selective if needed (todo).

-- we can have more granularity here but for now we only do a simple check

local no_left_ligature_code  = 1
local no_right_ligature_code = 2
local no_left_kern_code      = 4
local no_right_kern_code     = 8

local hasglyphoption = function(n,c)
    if c == no_left_ligature_code or c == no_right_ligature_code then
        return getattr(n,a_noligature) == 1
    else
        return false
    end
end

-- in lmtx we need to check the components and can be slightly more clever

local function toligature(head,start,stop,char,dataset,sequence,skiphash,discfound,hasmarks) -- brr head
    if hasglyphoption(start,no_right_ligature_code) then
        return head, start
    end
    if start == stop and getchar(start) == char then
        resetinjection(start)
        setchar(start,char)
        return head, start
    end
    local prev = getprev(start)
    local next = getnext(stop)
    local comp = start
    setprev(start)
    setnext(stop)
    local base = copynocomponents(start,copyinjection)
    if start == head then
        head = base
    end
    resetinjection(base)
    setchar(base,char)
    setcomponents(base,comp)
    setlink(prev,base,next)
    if not discfound then
        local deletemarks = not skiphash or hasmarks
        local components = start -- not used
        local baseindex = 0
        local componentindex = 0
        local head = base
        local current = base
        -- first we loop over the glyphs in start ... stop
        while start do
            local char = getchar(start)
            if not marks[char] then
                baseindex = baseindex + componentindex
                componentindex = countcomponents(start,marks)
             -- we can be more clever here: "not deletemarks or (skiphash and not skiphash[char])"
             -- and such:
            elseif not deletemarks then
                -- we can get a loop when the font expects otherwise (i.e. unexpected deletemarks)
                setligaindex(start,baseindex + getligaindex(start,componentindex))
                if trace_marks then
                    logwarning("%s: keep ligature mark %s, gets index %s",pref(dataset,sequence),gref(char),getligaindex(start))
                end
                local n = copynode(start)
                copyinjection(n,start) -- is this ok ? we position later anyway
                head, current = insertnodeafter(head,current,n) -- unlikely that mark has components
            elseif trace_marks then
                logwarning("%s: delete ligature mark %s",pref(dataset,sequence),gref(char))
            end
            start = getnext(start)
        end
        -- we can have one accent as part of a lookup and another following
        local start = getnext(current)
        while start do
            local char = ischar(start)
            if char then
                -- also something skiphash here?
                if marks[char] then
                    setligaindex(start,baseindex + getligaindex(start,componentindex))
                    if trace_marks then
                        logwarning("%s: set ligature mark %s, gets index %s",pref(dataset,sequence),gref(char),getligaindex(start))
                    end
                    start = getnext(start)
                else
                    break
                end
            else
                break
            end
        end
        flushcomponents(components)
    else
        -- discfound ... forget about marks .. probably no scripts that hyphenate and have marks
        local discprev, discnext = getboth(discfound)
        if discprev and discnext then
            -- we assume normalization in context, and don't care about generic ... especially
            -- \- can give problems as there we can have a negative char but that won't match
            -- anyway
            local pre, post, replace, pretail, posttail, replacetail = getdisc(discfound,true)
            if not replace then
                -- looks like we never come here as it's not okay
                local prev = getprev(base)
             -- local comp = getcomponents(base) -- already set
                local copied = copyonlyglyphs(comp)
                if pre then
                    setlink(discprev,pre)
                else
                    setnext(discprev) -- also blocks funny assignments
                end
                pre = comp -- is start
                if post then
                    setlink(posttail,discnext)
                    setprev(post) -- nil anyway
                else
                    post = discnext
                    setprev(discnext) -- also blocks funny assignments
                end
                setlink(prev,discfound,next)
                setboth(base)
                -- here components have a pointer so we can't free it!
                setcomponents(base,copied)
                replace = base
                if forcediscretionaries then
                    setdisc(discfound,pre,post,replace,discretionarydisc_code)
                else
                    setdisc(discfound,pre,post,replace)
                end
                base = prev
            end
        end
    end
    return head, base
end

local function multiple_glyphs(head,start,multiple,skiphash,what,stop) -- what to do with skiphash matches here
    local nofmultiples = #multiple
    if nofmultiples > 0 then
        local first = start
        resetinjection(start)
        setchar(start,multiple[1])
        if nofmultiples > 1 then
         -- local sn = getnext(start)
            for i=2,nofmultiples do
             -- untested:
             --
             -- while ignoremarks and marks[getchar(sn)] then
             --     local sn = getnext(sn)
             -- end
                local n = copynode(start) -- ignore components
                resetinjection(n)
                setchar(n,multiple[i])
                insertnodeafter(head,start,n)
                start = n
            end
        end
        if what ~= true and repeatablemultiples then
            -- This is just some experimental code; we might introduce gsub_extensible
            -- some day instead. Beware: when we have a feature that mixes alternates and
            -- multiples we need to make sure we don't handle the alternate string values
            -- here.
            local kind = type(what)
            local m, f, l
            if kind == "string" then
                local what, n = string.match(what,"^repeat(.-)[:=](%d+)$")
                if what == "middle" then
                    m = tonumber(n)
                elseif what == "first" then
                    f = tonumber(n)
                elseif what == "last" then
                    l = tonumber(n)
                end
            elseif kind == "table" then
                -- won't happen because currently we don't split these values
               m = what.middle
               f = what.first
               l = what.last
            end
            if f or m or l then
                if m and m > 1 and nofmultiples == 3 then
                    local middle = getnext(first)
                    for i=2,m do
                        local n = copynode(middle) -- ignore components
                        resetinjection(n)
                        insertnodeafter(head,first,n)
                    end
                end
                if f and f > 1 then
                    for i=2,f do
                        local n = copynode(first) -- ignore components
                        resetinjection(n)
                        insertnodeafter(head,first,n)
                    end
                end
                if l and l > 1 then
                    for i=2,l do
                        local n = copynode(start) -- ignore components
                        resetinjection(n)
                        insertnodeafter(head,start,n)
                        start = n
                    end
                end
            end
        end
        return head, start, true
    else
        if trace_multiples then
            logprocess("no multiple for %s",gref(getchar(start)))
        end
        return head, start, false
    end
end

local function get_alternative_glyph(start,alternatives,value)
    local n = #alternatives
    if n == 1 then
        -- we could actually change that into a gsub and save some memory in the
        -- font loader but it makes tracing more messy
        return alternatives[1], trace_alternatives and "1 (only one present)"
    elseif value == "random" then
        local r = getrandom and getrandom("glyph",1,n) or random(1,n)
        return alternatives[r], trace_alternatives and formatters["value %a, taking %a"](value,r)
    elseif value == "first" then
        return alternatives[1], trace_alternatives and formatters["value %a, taking %a"](value,1)
    elseif value == "last" then
        return alternatives[n], trace_alternatives and formatters["value %a, taking %a"](value,n)
    end
    value = value == true and 1 or tonumber(value)
    if type(value) ~= "number" then
        return alternatives[1], trace_alternatives and formatters["invalid value %s, taking %a"](value,1)
    end
 -- local a = alternatives[value]
 -- if a then
 --     -- some kind of hash
 --     return a, trace_alternatives and formatters["value %a, taking %a"](value,a)
 -- end
    if value > n then
        local defaultalt = otf.defaultnodealternate
        if defaultalt == "first" then
            return alternatives[n], trace_alternatives and formatters["invalid value %s, taking %a"](value,1)
        elseif defaultalt == "last" then
            return alternatives[1], trace_alternatives and formatters["invalid value %s, taking %a"](value,n)
        else
            return false, trace_alternatives and formatters["invalid value %a, %s"](value,"out of range")
        end
    elseif value == 0 then
        return getchar(start), trace_alternatives and formatters["invalid value %a, %s"](value,"no change")
    elseif value < 1 then
        return alternatives[1], trace_alternatives and formatters["invalid value %a, taking %a"](value,1)
    else
        return alternatives[value], trace_alternatives and formatters["value %a, taking %a"](value,value)
    end
end

-- handlers

function handlers.gsub_single(head,start,dataset,sequence,replacement)
    if trace_singles then
        logprocess("%s: replacing %s by single %s",pref(dataset,sequence),gref(getchar(start)),gref(replacement))
    end
    resetinjection(start)
    setchar(start,replacement)
    return head, start, true
end

function handlers.gsub_alternate(head,start,dataset,sequence,alternative)
    local kind  = dataset[4]
    local what  = dataset[1]
    local value = what == true and tfmdata.shared.features[kind] or what
    local choice, comment = get_alternative_glyph(start,alternative,value)
    if choice then
        if trace_alternatives then
            logprocess("%s: replacing %s by alternative %a to %s, %s",pref(dataset,sequence),gref(getchar(start)),gref(choice),comment)
        end
        resetinjection(start)
        setchar(start,choice)
    else
        if trace_alternatives then
            logwarning("%s: no variant %a for %s, %s",pref(dataset,sequence),value,gref(getchar(start)),comment)
        end
    end
    return head, start, true
end

function handlers.gsub_multiple(head,start,dataset,sequence,multiple,rlmode,skiphash)
    if trace_multiples then
        logprocess("%s: replacing %s by multiple %s",pref(dataset,sequence),gref(getchar(start)),gref(multiple))
    end
    return multiple_glyphs(head,start,multiple,skiphash,dataset[1])
end

-- Don't we deal with disc otherwise now? I need to check if the next one can be
-- simplified. Anyway, it can be way messier: marks that get removed as well as
-- marks that are kept.

function handlers.gsub_ligature(head,start,dataset,sequence,ligature,rlmode,skiphash)
    local current   = getnext(start)
    if not current then
        return head, start, false, nil
    end
    local stop      = nil
    local startchar = getchar(start)
    if skiphash and skiphash[startchar] then
        while current do
            local char = ischar(current,currentfont)
            if char then
                local lg = not tonumber(ligature) and ligature[char]
                if lg then
                    stop     = current
                    ligature = lg
                    current  = getnext(current)
                else
                    break
                end
            else
                break
            end
        end
        if stop then
            local ligature = tonumber(ligature) or ligature.ligature
            if ligature then
                if trace_ligatures then
                    local stopchar = getchar(stop)
                    head, start = markstoligature(head,start,stop,ligature)
                    logprocess("%s: replacing %s upto %s by ligature %s case 1",pref(dataset,sequence),gref(startchar),gref(stopchar),gref(getchar(start)))
                else
                    head, start = markstoligature(head,start,stop,ligature)
                end
                return head, start, true, false
            else
                -- ok, goto next lookup
            end
        end
    else
        local discfound = false
        local hasmarks  = marks[startchar]
        while current do
            local char, id = ischar(current,currentfont)
            if char then
                if skiphash and skiphash[char] then
                    current = getnext(current)
                else
                    local lg = not tonumber(ligature) and ligature[char]
                    if lg then
                        if marks[char] then
                            hasmarks = true
                        end
                        stop     = current -- needed for fake so outside then
                        ligature = lg
                        current  = getnext(current)
                    else
                        break
                    end
                end
            elseif char == false then
                -- kind of weird
                break
            elseif id == disc_code then
                discfound = current
                break
            else
                break
            end
        end
        -- of{f-}{}{f}e  o{f-}{}{f}fe  o{-}{}{ff}e (oe and ff ligature)
        -- we can end up here when we have a start run .. testruns start at a disc but
        -- so here we have the other case: char + disc
        --
        -- Challenge for Kai (latinmodern):  \hyphenation{fii-f-f-iif} fiiffiif
        --
        if discfound then
            -- don't assume marks in a disc and we don't run over a disc (for now)
            local pre, post, replace = getdisc(discfound)
            local match
            if replace then
                local char = ischar(replace,currentfont)
                if char and (not tonumber(ligature) and ligature[char]) then
                    match = true
                end
            end
            if not match and pre then
                local char = ischar(pre,currentfont)
                if char and (not tonumber(ligature) and ligature[char]) then
                    match = true
                end
            end
            if not match and not pre or not replace then
                local n    = getnext(discfound)
                local char = ischar(n,currentfont)
                if char and (not tonumber(ligature) and ligature[char]) then
                    match = true
                end
            end
            if match then
                -- we force a restart
                local ishead = head == start
                local prev   = getprev(start)
                if stop then
                    setnext(stop)
                    local copy = copynodelist(start)
                    local tail = stop -- was: getprev(stop) -- Kai: needs checking on your samples
                    local liat = findnodetail(copy)
                    if pre then
                        setlink(liat,pre)
                    end
                    if replace then
                        setlink(tail,replace)
                    end
                    pre     = copy
                    replace = start
                else
                    setnext(start)
                    local copy = copynode(start)
                    if pre then
                        setlink(copy,pre)
                    end
                    if replace then
                        setlink(start,replace)
                    end
                    pre     = copy
                    replace = start
                end
                setdisc(discfound,pre,post,replace)
                if prev then
                    setlink(prev,discfound)
                else
                    setprev(discfound)
                    head  = discfound
                end
                start = discfound
                return head, start, true, true
            end
        end
        local ligature = tonumber(ligature) or ligature.ligature
        if ligature then
            if stop then
                if trace_ligatures then
                    local stopchar = getchar(stop)
                 -- head, start = toligature(head,start,stop,ligature,dataset,sequence,skiphash,discfound,hasmarks)
                    head, start = toligature(head,start,stop,ligature,dataset,sequence,skiphash,false,hasmarks)
                    logprocess("%s: replacing %s upto %s by ligature %s case 2",pref(dataset,sequence),gref(startchar),gref(stopchar),gref(ligature))
                 -- we can have a rare case of multiple disc in a lig but that makes no sense language wise but if really
                 -- needed we could backtrack if we're in a disc node
                else
                 -- head, start = toligature(head,start,stop,ligature,dataset,sequence,skiphash,discfound,hasmarks)
                    head, start = toligature(head,start,stop,ligature,dataset,sequence,skiphash,false,hasmarks)
                end
            else
                -- weird but happens (in some arabic font)
                resetinjection(start)
                setchar(start,ligature)
                if trace_ligatures then
                    logprocess("%s: replacing %s by (no real) ligature %s case 3",pref(dataset,sequence),gref(startchar),gref(ligature))
                end
            end
            return head, start, true, false
        else
            -- weird but happens, pseudo ligatures ... just the components
        end
    end
    return head, start, false, false
end

function handlers.gpos_single(head,start,dataset,sequence,kerns,rlmode,skiphash,step,injection)
    if hasglyphoption(start,no_right_kern_code) then
        return head, start, false
    else
        local startchar = getchar(start)
        local format    = step.format
        if format == "single" or type(kerns) == "table" then -- the table check can go
            local dx, dy, w, h = setposition(0,start,factor,rlmode,kerns,injection)
            if trace_kerns then
                logprocess("%s: shifting single %s by %s xy (%p,%p) and wh (%p,%p)",pref(dataset,sequence),gref(startchar),format,dx,dy,w,h)
            end
        else
            local k = (format == "move" and setmove or setkern)(start,factor,rlmode,kerns,injection)
            if trace_kerns then
                logprocess("%s: shifting single %s by %s %p",pref(dataset,sequence),gref(startchar),format,k)
            end
        end
        return head, start, true
    end
end

function handlers.gpos_pair(head,start,dataset,sequence,kerns,rlmode,skiphash,step,injection)
    if hasglyphoption(start,no_right_kern_code) then
        return head, start, false
    else
        local snext = getnext(start)
        if not snext then
            return head, start, false
        else
            local prev = start
            while snext do
                local nextchar = ischar(snext,currentfont)
                if nextchar then
                    if skiphash and skiphash[nextchar] then -- includes marks too when flag
                        prev  = snext
                        snext = getnext(snext)
                    else
                        local krn = kerns[nextchar]
                        if not krn then
                            break
                        end
                        local format = step.format
                        if format == "pair" then
                            local a = krn[1]
                            local b = krn[2]
                            if a == true then
                                -- zero
                            elseif a then -- #a > 0
                                local x, y, w, h = setposition(1,start,factor,rlmode,a,injection)
                                if trace_kerns then
                                    local startchar = getchar(start)
                                    logprocess("%s: shifting first of pair %s and %s by xy (%p,%p) and wh (%p,%p) as %s",pref(dataset,sequence),gref(startchar),gref(nextchar),x,y,w,h,injection or "injections")
                                end
                            end
                            if b == true then
                                -- zero
                                start = snext -- cf spec
                            elseif b then -- #b > 0
                                local x, y, w, h = setposition(2,snext,factor,rlmode,b,injection)
                                if trace_kerns then
                                    local startchar = getchar(start)
                                    logprocess("%s: shifting second of pair %s and %s by xy (%p,%p) and wh (%p,%p) as %s",pref(dataset,sequence),gref(startchar),gref(nextchar),x,y,w,h,injection or "injections")
                                end
                                start = snext -- cf spec
                            elseif forcepairadvance then
                                start = snext -- for testing, not cf spec
                            end
                            return head, start, true
                        elseif krn ~= 0 then
                            local k = (format == "move" and setmove or setkern)(snext,factor,rlmode,krn,injection)
                            if trace_kerns then
                                logprocess("%s: inserting %s %p between %s and %s as %s",pref(dataset,sequence),format,k,gref(getchar(prev)),gref(nextchar),injection or "injections")
                            end
                            return head, start, true
                        else -- can't happen
                            break
                        end
                    end
                else
                    break
                end
            end
            return head, start, false
        end
    end
end

-- We get hits on a mark, but we're not sure if the it has to be applied so we need
-- to explicitly test for basechar, baselig and basemark entries.

function handlers.gpos_mark2base(head,start,dataset,sequence,markanchors,rlmode,skiphash)
    local markchar = getchar(start)
    if marks[markchar] then
        local base = getprev(start) -- [glyph] [start=mark]
        if base then
            local basechar = ischar(base,currentfont)
            if basechar then
                if marks[basechar] then
                    while base do
                        base = getprev(base)
                        if base then
                            basechar = ischar(base,currentfont)
                            if basechar then
                                if not marks[basechar] then
                                    break
                                end
                            else
                                if trace_bugs then
                                    logwarning("%s: no base for mark %s, case %i",pref(dataset,sequence),gref(markchar),1)
                                end
                                return head, start, false
                            end
                        else
                            if trace_bugs then
                                logwarning("%s: no base for mark %s, case %i",pref(dataset,sequence),gref(markchar),2)
                            end
                            return head, start, false
                        end
                    end
                end
                local ba = markanchors[1][basechar]
                if ba then
                    local ma = markanchors[2]
                    local dx, dy, bound = setmark(start,base,factor,rlmode,ba,ma,characters[basechar],false,checkmarks)
                    if trace_marks then
                        logprocess("%s, bound %s, anchoring mark %s to basechar %s => (%p,%p)",
                            pref(dataset,sequence),bound,gref(markchar),gref(basechar),dx,dy)
                    end
                    return head, start, true
                elseif trace_bugs then
                 -- onetimemessage(currentfont,basechar,"no base anchors")
                    logwarning("%s: mark %s is not anchored to %s",pref(dataset,sequence),gref(markchar),gref(basechar))
                end
            elseif trace_bugs then
                logwarning("%s: nothing preceding, case %i",pref(dataset,sequence),1)
            end
        elseif trace_bugs then
            logwarning("%s: nothing preceding, case %i",pref(dataset,sequence),2)
        end
    elseif trace_bugs then
        logwarning("%s: mark %s is no mark",pref(dataset,sequence),gref(markchar))
    end
    return head, start, false
end

function handlers.gpos_mark2ligature(head,start,dataset,sequence,markanchors,rlmode,skiphash)
    local markchar = getchar(start)
    if marks[markchar] then
        local base = getprev(start) -- [glyph] [optional marks] [start=mark]
        if base then
            local basechar = ischar(base,currentfont)
            if basechar then
                if marks[basechar] then
                    while base do
                        base = getprev(base)
                        if base then
                            basechar = ischar(base,currentfont)
                            if basechar then
                                if not marks[basechar] then
                                    break
                                end
                            else
                                if trace_bugs then
                                    logwarning("%s: no base for mark %s, case %i",pref(dataset,sequence),gref(markchar),1)
                                end
                                return head, start, false
                            end
                        else
                            if trace_bugs then
                                logwarning("%s: no base for mark %s, case %i",pref(dataset,sequence),gref(markchar),2)
                            end
                            return head, start, false
                        end
                    end
                end
                local ba = markanchors[1][basechar]
                if ba then
                    local ma = markanchors[2]
                    if ma then
                        local index = getligaindex(start)
                        ba = ba[index]
                        if ba then
                            local dx, dy, bound = setmark(start,base,factor,rlmode,ba,ma,characters[basechar],false,checkmarks)
                            if trace_marks then
                                logprocess("%s, index %s, bound %s, anchoring mark %s to baselig %s at index %s => (%p,%p)",
                                    pref(dataset,sequence),index,bound,gref(markchar),gref(basechar),index,dx,dy)
                            end
                            return head, start, true
                        else
                            if trace_bugs then
                                logwarning("%s: no matching anchors for mark %s and baselig %s with index %a",pref(dataset,sequence),gref(markchar),gref(basechar),index)
                            end
                        end
                    end
                elseif trace_bugs then
                --  logwarning("%s: char %s is missing in font",pref(dataset,sequence),gref(basechar))
                    onetimemessage(currentfont,basechar,"no base anchors")
                end
            elseif trace_bugs then
                logwarning("%s: prev node is no char, case %i",pref(dataset,sequence),1)
            end
        elseif trace_bugs then
            logwarning("%s: prev node is no char, case %i",pref(dataset,sequence),2)
        end
    elseif trace_bugs then
        logwarning("%s: mark %s is no mark",pref(dataset,sequence),gref(markchar))
    end
    return head, start, false
end

function handlers.gpos_mark2mark(head,start,dataset,sequence,markanchors,rlmode,skiphash)
    local markchar = getchar(start)
    if marks[markchar] then
        local base = getprev(start) -- [glyph] [basemark] [start=mark]
        local slc = getligaindex(start)
        if slc then -- a rather messy loop ... needs checking with husayni
            while base do
                local blc = getligaindex(base)
                if blc and blc ~= slc then
                    base = getprev(base)
                else
                    break
                end
            end
        end
        if base then
            local basechar = ischar(base,currentfont)
            if basechar then -- subtype test can go
                local ba = markanchors[1][basechar] -- slot 1 has been made copy of the class hash
                if ba then
                    local ma = markanchors[2]
                    local dx, dy, bound = setmark(start,base,factor,rlmode,ba,ma,characters[basechar],true,checkmarks)
                    if trace_marks then
                        logprocess("%s, bound %s, anchoring mark %s to basemark %s => (%p,%p)",
                            pref(dataset,sequence),bound,gref(markchar),gref(basechar),dx,dy)
                    end
                    return head, start, true
                end
            end
        end
    elseif trace_bugs then
        logwarning("%s: mark %s is no mark",pref(dataset,sequence),gref(markchar))
    end
    return head, start, false
end

function handlers.gpos_cursive(head,start,dataset,sequence,exitanchors,rlmode,skiphash,step) -- to be checked
    local startchar = getchar(start)
    if marks[startchar] then
        if trace_cursive then
            logprocess("%s: ignoring cursive for mark %s",pref(dataset,sequence),gref(startchar))
        end
    else
        local nxt = getnext(start)
        while nxt do
            local nextchar = ischar(nxt,currentfont)
            if not nextchar then
                break
            elseif marks[nextchar] then -- always sequence.flags[1]
                nxt = getnext(nxt)
            else
                local exit = exitanchors[3]
                if exit then
                    local entry = exitanchors[1][nextchar]
                    if entry then
                        entry = entry[2]
                        if entry then
                            local r2lflag = sequence.flags[4] -- mentioned in the standard
                            local dx, dy, bound = setcursive(start,nxt,factor,rlmode,exit,entry,characters[startchar],characters[nextchar],r2lflag)
                            if trace_cursive then
                                logprocess("%s: moving %s to %s cursive (%p,%p) using bound %s in %s mode",pref(dataset,sequence),gref(startchar),gref(nextchar),dx,dy,bound,mref(rlmode))
                            end
                            return head, start, true
                        end
                    end
                end
                break
            end
        end
    end
    return head, start, false
end

-- I will implement multiple chain replacements once I run into a font that uses it.
-- It's not that complex to handle.

local chainprocs = { }

local function logprocess(...)
    if trace_steps then
        registermessage(...)
        if trace_steps == "silent" then
            return
        end
    end
    report_subchain(...)
end

local logwarning = report_subchain

local function logprocess(...)
    if trace_steps then
        registermessage(...)
        if trace_steps == "silent" then
            return
        end
    end
    report_chain(...)
end

local logwarning = report_chain

-- We could share functions but that would lead to extra function calls with many
-- arguments, redundant tests and confusing messages.

-- The reversesub is a special case, which is why we need to store the replacements
-- in a bit weird way. There is no lookup and the replacement comes from the lookup
-- itself. It is meant mostly for dealing with Urdu.

local function reversesub(head,start,stop,dataset,sequence,replacements,rlmode,skiphash)
    local char        = getchar(start)
    local replacement = replacements[char]
    if replacement then
        if trace_singles then
            logprocess("%s: single reverse replacement of %s by %s",cref(dataset,sequence),gref(char),gref(replacement))
        end
        resetinjection(start)
        setchar(start,replacement)
        return head, start, true
    else
        return head, start, false
    end
end


chainprocs.reversesub = reversesub

-- This chain stuff is somewhat tricky since we can have a sequence of actions to be
-- applied: single, alternate, multiple or ligature where ligature can be an invalid
-- one in the sense that it will replace multiple by one but not neccessary one that
-- looks like the combination (i.e. it is the counterpart of multiple then). For
-- example, the following is valid:
--
--   xxxabcdexxx [single a->A][multiple b->BCD][ligature cde->E] xxxABCDExxx
--
-- Therefore we we don't really do the replacement here already unless we have the
-- single lookup case. The efficiency of the replacements can be improved by
-- deleting as less as needed but that would also make the code even more messy.
--
-- Here we replace start by a single variant.
--
-- To be done   : what if > 1 steps (example needed)
-- This is messy: do we need this disc checking also in alternates?

local function reportzerosteps(dataset,sequence)
    logwarning("%s: no steps",cref(dataset,sequence))
end

local function reportmoresteps(dataset,sequence)
    logwarning("%s: more than 1 step",cref(dataset,sequence))
end

-- local function reportbadsteps(dataset,sequence)
--     logwarning("%s: bad step, no proper return values",cref(dataset,sequence))
-- end

local function getmapping(dataset,sequence,currentlookup)
    local steps    = currentlookup.steps
    local nofsteps = currentlookup.nofsteps
    if nofsteps == 0 then
        reportzerosteps(dataset,sequence)
        currentlookup.mapping = false
        return false
    else
        if nofsteps > 1 then
            reportmoresteps(dataset,sequence)
        end
        local mapping = steps[1].coverage
        currentlookup.mapping = mapping
        currentlookup.format  = steps[1].format
        return mapping
    end
end

function chainprocs.gsub_remove(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash,chainindex)
    if trace_chains then
        logprocess("%s: removing character %s",cref(dataset,sequence,chainindex),gref(getchar(start)))
    end
    head, start = removenode(head,start,true)
    return head, getprev(start), true
end

function chainprocs.gsub_single(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash,chainindex)
    local mapping = currentlookup.mapping
    if mapping == nil then
        mapping = getmapping(dataset,sequence,currentlookup)
    end
    if mapping then
        local current = start
        while current do
            local currentchar = ischar(current)
            if currentchar then
                local replacement = mapping[currentchar]
                if not replacement or replacement == "" then
                    if trace_bugs then
                        logwarning("%s: no single for %s",cref(dataset,sequence,chainindex),gref(currentchar))
                    end
                else
                    if trace_singles then
                        logprocess("%s: replacing single %s by %s",cref(dataset,sequence,chainindex),gref(currentchar),gref(replacement))
                    end
                    resetinjection(current)
                    setchar(current,replacement)
                end
                return head, start, true
            elseif currentchar == false then
                -- can't happen
                break
            elseif current == stop then
                break
            else
                current = getnext(current)
            end
        end
    end
    return head, start, false
end

-- Here we replace start by new glyph. First we delete the rest of the match.

-- char_1 mark_1 -> char_x mark_1 (ignore marks)
-- char_1 mark_1 -> char_x

-- to be checked: do we always have just one glyph?
-- we can also have alternates for marks
-- marks come last anyway
-- are there cases where we need to delete the mark

function chainprocs.gsub_alternate(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash,chainindex)
    local mapping = currentlookup.mapping
    if mapping == nil then
        mapping = getmapping(dataset,sequence,currentlookup)
    end
    if mapping then
        local kind    = dataset[4]
        local what    = dataset[1]
        local value   = what == true and tfmdata.shared.features[kind] or what -- todo: optimize in ctx
        local current = start
        while current do
            local currentchar = ischar(current)
            if currentchar then
                local alternatives = mapping[currentchar]
                if alternatives then
                    local choice, comment = get_alternative_glyph(current,alternatives,value)
                    if choice then
                        if trace_alternatives then
                            logprocess("%s: replacing %s by alternative %a to %s, %s",cref(dataset,sequence),gref(currentchar),choice,gref(choice),comment)
                        end
                        resetinjection(start)
                        setchar(start,choice)
                    else
                        if trace_alternatives then
                            logwarning("%s: no variant %a for %s, %s",cref(dataset,sequence),value,gref(currentchar),comment)
                        end
                    end
                end
                return head, start, true
            elseif currentchar == false then
                -- can't happen
                break
            elseif current == stop then
                break
            else
                current = getnext(current)
            end
        end
    end
    return head, start, false
end

-- Here we replace start by a sequence of new glyphs.

function chainprocs.gsub_multiple(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash,chainindex)
    local mapping = currentlookup.mapping
    if mapping == nil then
        mapping = getmapping(dataset,sequence,currentlookup)
    end
    if mapping then
        local startchar   = getchar(start)
        local replacement = mapping[startchar]
        if not replacement or replacement == "" then
            if trace_bugs then
                logwarning("%s: no multiple for %s",cref(dataset,sequence),gref(startchar))
            end
        else
            if trace_multiples then
                logprocess("%s: replacing %s by multiple characters %s",cref(dataset,sequence),gref(startchar),gref(replacement))
            end
            return multiple_glyphs(head,start,replacement,skiphash,dataset[1],stop)
        end
    end
    return head, start, false
end

-- When we replace ligatures we use a helper that handles the marks. I might change
-- this function (move code inline and handle the marks by a separate function). We
-- assume rather stupid ligatures (no complex disc nodes).

-- compare to handlers.gsub_ligature which is more complex ... why

function chainprocs.gsub_ligature(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash,chainindex)
    local mapping = currentlookup.mapping
    if mapping == nil then
        mapping = getmapping(dataset,sequence,currentlookup)
    end
    if mapping then
        local startchar = getchar(start)
        local ligatures = mapping[startchar]
        if not ligatures then
            if trace_bugs then
                logwarning("%s: no ligatures starting with %s",cref(dataset,sequence,chainindex),gref(startchar))
            end
        else
            local hasmarks        = marks[startchar]
            local current         = getnext(start)
            local discfound       = false
            local last            = stop
            local nofreplacements = 1
            while current do
                -- todo: ischar ... can there really be disc nodes here?
                local id = getid(current)
                if id == disc_code then
                    if not discfound then
                        discfound = current
                    end
                    if current == stop then
                        break -- okay? or before the disc
                    else
                        current = getnext(current)
                    end
                else
                    local schar = getchar(current)
                    if skiphash and skiphash[schar] then -- marks
                        -- if current == stop then -- maybe add this
                        --     break
                        -- else
                            current = getnext(current)
                        -- end
                    else
                        local lg = not tonumber(ligatures) and ligatures[schar]
                        if lg then
                            ligatures       = lg
                            last            = current
                            nofreplacements = nofreplacements + 1
                            if marks[char] then
                                hasmarks = true
                            end
                            if current == stop then
                                break
                            else
                                current = getnext(current)
                            end
                        else
                            break
                        end
                    end
                end
            end
            local ligature = tonumber(ligatures) or ligatures.ligature
            if ligature then
                if chainindex then
                    stop = last
                end
                if trace_ligatures then
                    if start == stop then
                        logprocess("%s: replacing character %s by ligature %s case 3",cref(dataset,sequence,chainindex),gref(startchar),gref(ligature))
                    else
                        logprocess("%s: replacing character %s upto %s by ligature %s case 4",cref(dataset,sequence,chainindex),gref(startchar),gref(getchar(stop)),gref(ligature))
                    end
                end
                head, start = toligature(head,start,stop,ligature,dataset,sequence,skiphash,discfound,hasmarks)
                return head, start, true, nofreplacements, discfound
            elseif trace_bugs then
                if start == stop then
                    logwarning("%s: replacing character %s by ligature fails",cref(dataset,sequence,chainindex),gref(startchar))
                else
                    logwarning("%s: replacing character %s upto %s by ligature fails",cref(dataset,sequence,chainindex),gref(startchar),gref(getchar(stop)))
                end
            end
        end
    end
    return head, start, false, 0, false
end

function chainprocs.gpos_single(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash,chainindex)
    -- we actually should check no_left_kern_code with next
    if not hasglyphoption(start,no_right_kern_code) then
        local mapping = currentlookup.mapping
        if mapping == nil then
            mapping = getmapping(dataset,sequence,currentlookup)
        end
        if mapping then
            local startchar = getchar(start)
            local kerns     = mapping[startchar]
            if kerns then
                local format = currentlookup.format
                if format == "single" then
                    local dx, dy, w, h = setposition(0,start,factor,rlmode,kerns) -- currentlookup.flags ?
                    if trace_kerns then
                        logprocess("%s: shifting single %s by %s (%p,%p) and correction (%p,%p)",cref(dataset,sequence),gref(startchar),format,dx,dy,w,h)
                    end
                else -- needs checking .. maybe no kerns format for single
                    local k = (format == "move" and setmove or setkern)(start,factor,rlmode,kerns,injection)
                    if trace_kerns then
                        logprocess("%s: shifting single %s by %s %p",cref(dataset,sequence),gref(startchar),format,k)
                    end
                end
                return head, start, true
            end
        end
    end
    return head, start, false
end

function chainprocs.gpos_pair(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash,chainindex) -- todo: injections ?
    -- we actually should check no_left_kern_code with next
    if not hasglyphoption(start,no_right_kern_code) then
        local mapping = currentlookup.mapping
        if mapping == nil then
            mapping = getmapping(dataset,sequence,currentlookup)
        end
        if mapping then
            local snext = getnext(start)
            if snext then
                local startchar = getchar(start)
                local kerns     = mapping[startchar] -- always 1 step
                if kerns then
                    local prev = start
                    while snext do
                        local nextchar = ischar(snext,currentfont)
                        if not nextchar then
                            break
                        end
                        if skiphash and skiphash[nextchar] then
                            prev  = snext
                            snext = getnext(snext)
                        else
                            local krn = kerns[nextchar]
                            if not krn then
                                break
                            end
                            local format = currentlookup.format
                            if format == "pair" then
                                local a = krn[1]
                                local b = krn[2]
                                if a == true then
                                    -- zero
                                elseif a then
                                    local x, y, w, h = setposition(1,start,factor,rlmode,a,"injections") -- currentlookups flags?
                                    if trace_kerns then
                                        local startchar = getchar(start)
                                        logprocess("%s: shifting first of pair %s and %s by (%p,%p) and correction (%p,%p)",cref(dataset,sequence),gref(startchar),gref(nextchar),x,y,w,h)
                                    end
                                end
                                if b == true then
                                    -- zero
                                    start = snext -- cf spec
                                elseif b then -- #b > 0
                                    local x, y, w, h = setposition(2,snext,factor,rlmode,b,"injections")
                                    if trace_kerns then
                                        local startchar = getchar(start)
                                        logprocess("%s: shifting second of pair %s and %s by (%p,%p) and correction (%p,%p)",cref(dataset,sequence),gref(startchar),gref(nextchar),x,y,w,h)
                                    end
                                    start = snext -- cf spec
                                elseif forcepairadvance then
                                    start = snext -- for testing, not cf spec
                                end
                                return head, start, true
                            elseif krn ~= 0 then
                                local k = (format == "move" and setmove or setkern)(snext,factor,rlmode,krn)
                                if trace_kerns then
                                    logprocess("%s: inserting %s %p between %s and %s",cref(dataset,sequence),format,k,gref(getchar(prev)),gref(nextchar))
                                end
                                return head, start, true
                            else
                                break
                            end
                        end
                    end
                end
            end
        end
    end
    return head, start, false
end

function chainprocs.gpos_mark2base(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash,chainindex)
    local mapping = currentlookup.mapping
    if mapping == nil then
        mapping = getmapping(dataset,sequence,currentlookup)
    end
    if mapping then
        local markchar = getchar(start)
        if marks[markchar] then
            local markanchors = mapping[markchar] -- always 1 step
            if markanchors then
                local base = getprev(start) -- [glyph] [start=mark]
                if base then
                    local basechar = ischar(base,currentfont)
                    if basechar then
                        if marks[basechar] then
                            while base do
                                base = getprev(base)
                                if base then
                                    local basechar = ischar(base,currentfont)
                                    if basechar then
                                        if not marks[basechar] then
                                            break
                                        end
                                    else
                                        if trace_bugs then
                                            logwarning("%s: no base for mark %s, case %i",pref(dataset,sequence),gref(markchar),1)
                                        end
                                        return head, start, false
                                    end
                                else
                                    if trace_bugs then
                                        logwarning("%s: no base for mark %s, case %i",pref(dataset,sequence),gref(markchar),2)
                                    end
                                    return head, start, false
                                end
                            end
                        end
                        local ba = markanchors[1][basechar]
                        if ba then
                            local ma = markanchors[2]
                            if ma then
                                local dx, dy, bound = setmark(start,base,factor,rlmode,ba,ma,characters[basechar],false,checkmarks)
                                if trace_marks then
                                    logprocess("%s, bound %s, anchoring mark %s to basechar %s => (%p,%p)",
                                        cref(dataset,sequence),bound,gref(markchar),gref(basechar),dx,dy)
                                end
                                return head, start, true
                            end
                        end
                    elseif trace_bugs then
                        logwarning("%s: prev node is no char, case %i",cref(dataset,sequence),1)
                    end
                elseif trace_bugs then
                    logwarning("%s: prev node is no char, case %i",cref(dataset,sequence),2)
                end
            elseif trace_bugs then
                logwarning("%s: mark %s has no anchors",cref(dataset,sequence),gref(markchar))
            end
        elseif trace_bugs then
            logwarning("%s: mark %s is no mark",cref(dataset,sequence),gref(markchar))
        end
    end
    return head, start, false
end

function chainprocs.gpos_mark2ligature(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash,chainindex)
    local mapping = currentlookup.mapping
    if mapping == nil then
        mapping = getmapping(dataset,sequence,currentlookup)
    end
    if mapping then
        local markchar = getchar(start)
        if marks[markchar] then
            local markanchors = mapping[markchar] -- always 1 step
            if markanchors then
                local base = getprev(start) -- [glyph] [optional marks] [start=mark]
                if base then
                    local basechar = ischar(base,currentfont)
                    if basechar then
                        if marks[basechar] then
                            while base do
                                base = getprev(base)
                                if base then
                                    local basechar = ischar(base,currentfont)
                                    if basechar then
                                        if not marks[basechar] then
                                            break
                                        end
                                    else
                                        if trace_bugs then
                                            logwarning("%s: no base for mark %s, case %i",cref(dataset,sequence),markchar,1)
                                        end
                                        return head, start, false
                                    end
                                else
                                    if trace_bugs then
                                        logwarning("%s: no base for mark %s, case %i",cref(dataset,sequence),markchar,2)
                                    end
                                    return head, start, false
                                end
                            end
                        end
                        local ba = markanchors[1][basechar]
                        if ba then
                            local ma = markanchors[2]
                            if ma then
                                local index = getligaindex(start)
                                ba = ba[index]
                                if ba then
                                    local dx, dy, bound = setmark(start,base,factor,rlmode,ba,ma,characters[basechar],false,checkmarks)
                                    if trace_marks then
                                        logprocess("%s, bound %s, anchoring mark %s to baselig %s at index %s => (%p,%p)",
                                            cref(dataset,sequence),a or bound,gref(markchar),gref(basechar),index,dx,dy)
                                    end
                                    return head, start, true
                                end
                            end
                        end
                    elseif trace_bugs then
                        logwarning("%s, prev node is no char, case %i",cref(dataset,sequence),1)
                    end
                elseif trace_bugs then
                    logwarning("%s, prev node is no char, case %i",cref(dataset,sequence),2)
                end
            elseif trace_bugs then
                logwarning("%s, mark %s has no anchors",cref(dataset,sequence),gref(markchar))
            end
        elseif trace_bugs then
            logwarning("%s, mark %s is no mark",cref(dataset,sequence),gref(markchar))
        end
    end
    return head, start, false
end

function chainprocs.gpos_mark2mark(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash,chainindex)
    local mapping = currentlookup.mapping
    if mapping == nil then
        mapping = getmapping(dataset,sequence,currentlookup)
    end
    if mapping then
        local markchar = getchar(start)
        if marks[markchar] then
            local markanchors = mapping[markchar] -- always 1 step
            if markanchors then
                local base = getprev(start) -- [glyph] [basemark] [start=mark]
                local slc = getligaindex(start)
                if slc then -- a rather messy loop ... needs checking with husayni
                    while base do
                        local blc = getligaindex(base)
                        if blc and blc ~= slc then
                            base = getprev(base)
                        else
                            break
                        end
                    end
                end
                if base then -- subtype test can go
                    local basechar = ischar(base,currentfont)
                    if basechar then
                        local ba = markanchors[1][basechar]
                        if ba then
                            local ma = markanchors[2]
                            if ma then
                                local dx, dy, bound = setmark(start,base,factor,rlmode,ba,ma,characters[basechar],true,checkmarks)
                                if trace_marks then
                                    logprocess("%s, bound %s, anchoring mark %s to basemark %s => (%p,%p)",
                                        cref(dataset,sequence),bound,gref(markchar),gref(basechar),dx,dy)
                                end
                                return head, start, true
                            end
                        end
                    elseif trace_bugs then
                        logwarning("%s: prev node is no mark, case %i",cref(dataset,sequence),1)
                    end
                elseif trace_bugs then
                    logwarning("%s: prev node is no mark, case %i",cref(dataset,sequence),2)
                end
            elseif trace_bugs then
                logwarning("%s: mark %s has no anchors",cref(dataset,sequence),gref(markchar))
            end
        elseif trace_bugs then
            logwarning("%s: mark %s is no mark",cref(dataset,sequence),gref(markchar))
        end
    end
    return head, start, false
end

function chainprocs.gpos_cursive(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash,chainindex)
    local mapping = currentlookup.mapping
    if mapping == nil then
        mapping = getmapping(dataset,sequence,currentlookup)
    end
    if mapping then
        local startchar   = getchar(start)
        local exitanchors = mapping[startchar] -- always 1 step
        if exitanchors then
            if marks[startchar] then
                if trace_cursive then
                    logprocess("%s: ignoring cursive for mark %s",pref(dataset,sequence),gref(startchar))
                end
            else
                local nxt = getnext(start)
                while nxt do
                    local nextchar = ischar(nxt,currentfont)
                    if not nextchar then
                        break
                    elseif marks[nextchar] then
                        -- should not happen (maybe warning)
                        nxt = getnext(nxt)
                    else
                        local exit = exitanchors[3]
                        if exit then
                            local entry = exitanchors[1][nextchar]
                            if entry then
                                entry = entry[2]
                                if entry then
                                    local r2lflag = sequence.flags[4] -- mentioned in the standard
                                    local dx, dy, bound = setcursive(start,nxt,factor,rlmode,exit,entry,characters[startchar],characters[nextchar],r2lflag)
                                    if trace_cursive then
                                        logprocess("%s: moving %s to %s cursive (%p,%p) using bound %s in %s mode",pref(dataset,sequence),gref(startchar),gref(nextchar),dx,dy,bound,mref(rlmode))
                                    end
                                    return head, start, true
                                end
                            end
                        elseif trace_bugs then
                            onetimemessage(currentfont,startchar,"no entry anchors")
                        end
                        break
                    end
                end
            end
        elseif trace_cursive and trace_details then
            logprocess("%s, cursive %s is already done",pref(dataset,sequence),gref(getchar(start)),alreadydone)
        end
    end
    return head, start, false
end

-- what pointer to return, spec says stop
-- to be discussed ... is bidi changer a space?
-- elseif char == zwnj and sequence[n][32] then -- brrr

local function show_skip(dataset,sequence,char,ck,class)
    logwarning("%s: skipping char %s, class %a, rule %a, lookuptype %a",cref(dataset,sequence),gref(char),class,ck[1],ck[8] or ck[2])
end

-- A previous version had disc collapsing code in the (single sub) handler plus some
-- checking in the main loop, but that left the pre/post sequences undone. The best
-- solution is to add some checking there and backtrack when a replace/post matches
-- but it takes a bit of work to figure out an efficient way (this is what the
-- sweep* names refer to). I might look into that variant one day again as it can
-- replace some other code too. In that approach we can have a special version for
-- gub and pos which gains some speed. This method does the test and passes info to
-- the handlers. Here collapsing is handled in the main loop which also makes code
-- elsewhere simpler (i.e. no need for the other special runners and disc code in
-- ligature building). I also experimented with pushing preceding glyphs sequences
-- in the replace/pre fields beforehand which saves checking afterwards but at the
-- cost of duplicate glyphs (memory) but it's too much overhead (runtime).
--
-- In the meantime Kai had moved the code from the single chain into a more general
-- handler and this one (renamed to chaindisk) is used now. I optimized the code a
-- bit and brought it in sycn with the other code. Hopefully I didn't introduce
-- errors. Note: this somewhat complex approach is meant for fonts that implement
-- (for instance) ligatures by character replacement which to some extend is not
-- that suitable for hyphenation. I also use some helpers. This method passes some
-- states but reparses the list. There is room for a bit of speed up but that will
-- be done in the context version. (In fact a partial rewrite of all code can bring
-- some more efficiency.)
--
-- I didn't test it with extremes but successive disc nodes still can give issues
-- but in order to handle that we need more complex code which also slows down even
-- more. The main loop variant could deal with that: test, collapse, backtrack.

local userkern = nuts.pool and nuts.pool.newkern -- context

do if not userkern then -- generic

    local thekern = nuts.new("kern",1) -- userkern
    local setkern = nuts.setkern       -- not injections.setkern

    userkern = function(k)
        local n = copynode(thekern)
        setkern(n,k)
        return n
    end

end end

local function checked(head)
    local current = head
    while current do
        if getid(current) == glue_code then
            local kern = userkern(getwidth(current))
            if head == current then
                local next = getnext(current)
                if next then
                    setlink(kern,next)
                end
                flushnode(current)
                head    = kern
                current = next
            else
                local prev, next = getboth(current)
                setlink(prev,kern,next)
                flushnode(current)
                current = next
            end
        else
            current = getnext(current)
        end
    end
    return head
end

local function setdiscchecked(d,pre,post,replace)
    if pre     then pre     = checked(pre)     end
    if post    then post    = checked(post)    end
    if replace then replace = checked(replace) end
    setdisc(d,pre,post,replace)
end

local noflags = { false, false, false, false }

local function chainrun(head,start,last,dataset,sequence,rlmode,skiphash,ck,where)

    local size         = ck[5] - ck[4] + 1
    local chainlookups = ck[6]
    local done         = false
    -- current match
    if chainlookups then
        -- Lookups can be like { 1, false, 3 } or { false, 2 } or basically anything and
        -- #lookups can be less than #current

        if size == 1 then

         -- if nofchainlookups > size then
         --     -- bad rules
         -- end
            local chainlookup = chainlookups[1]
            if chainlookup then
                for j=1,#chainlookup do
                    local chainstep = chainlookup[j]
                    if chainstep then
                        local chainkind = chainstep.type
                        local chainproc = chainprocs[chainkind]
                        if chainproc then
                            local ok
                         -- HH: chainindex 1 added here (for KAI to check too), there are weird ligatures e.g.
                         -- char + mark -> char where mark has to disappear
                            head, start, ok = chainproc(head,start,last,dataset,sequence,chainstep,rlmode,skiphash,1)
                            if ok then
                                done = true
                            end
                        else
                            logprocess("%s: %s is not yet supported (1)",cref(dataset,sequence),chainkind)
                        end
                    else
                        logprocess("%s: has an issue (1)",cref(dataset,sequence))
                    end
                end
            else
                -- whatever
            end

         else

            -- See LookupType 5: Contextual Substitution Subtable. Now it becomes messy. The
            -- easiest case is where #current maps on #lookups i.e. one-to-one. But what if
            -- we have a ligature. Cf the spec we then need to advance one character but we
            -- really need to test it as there are fonts out there that are fuzzy and have
            -- too many lookups:
            --
            -- U+1105 U+119E U+1105 U+119E : sourcehansansklight: script=hang ccmp=yes
            --
            -- Even worse are these family emoji shapes as they can have multiple lookups
            -- per slot (probably only for gpos).

            -- It's very unlikely that we will have skip classes here but still ... we seldom
            -- enter this branch anyway.

            local i = 1
            local laststart = start
            local nofchainlookups = #chainlookups -- useful?
            while start do
                if skiphash then -- hm, so we know we skip some
                    while start do
                        local char = ischar(start,currentfont)
                        if char then
                            if skiphash and skiphash[char] then
                                start = getnext(start)
                            else
                                break
                            end
                        else
                            break
                        end
                    end
                end
                local chainlookup = chainlookups[i]
                if chainlookup then
                    for j=1,#chainlookup do
                        local chainstep = chainlookup[j]
                        if chainstep then
                            local chainkind = chainstep.type
                            local chainproc = chainprocs[chainkind]
                            if chainproc then
                                local ok, n
                                head, start, ok, n = chainproc(head,start,last,dataset,sequence,chainstep,rlmode,skiphash,i)
                                -- messy since last can be changed !
                                if ok then
                                    done = true
                                    if n and n > 1 and i + n > nofchainlookups then
                                        -- this is a safeguard, we just ignore the rest of the lookups
                                        i = size -- prevents an advance
                                        break
                                    end
                                end
                            else
                                -- actually an error
                                logprocess("%s: %s is not yet supported (2)",cref(dataset,sequence),chainkind)
                            end
                        else
                            -- actually an error
                            logprocess("%s: has an issue (2)",cref(dataset,sequence))
                        end
                    end
                else
                    -- we skip but we could also delete as option .. what does an empty lookup actually mean
                    -- in opentype ... anyway, we could map it onto gsub_remove if needed
                end
                i = i + 1
                if i > size or not start then
                    break
                elseif start then
                    laststart = start
                    start = getnext(start)
                end
            end
            if not start then
                start = laststart
            end

        end
    else
        -- todo: needs checking for holes in the replacements
        local replacements = ck[7]
        if replacements then
            head, start, done = reversesub(head,start,last,dataset,sequence,replacements,rlmode,skiphash)
        else
            done = true
            if trace_contexts then
                logprocess("%s: skipping match @ %i",cref(dataset,sequence),where)
            end
        end
    end
    return head, start, done
end

local function chaindisk(head,start,dataset,sequence,rlmode,skiphash,ck)

    if not start then
        return head, start, false
    end

    local startishead   = start == head
    local seq           = ck[3]
    local f             = ck[4]
    local l             = ck[5]
    local s             = #seq
    local done          = false
    local sweepnode     = sweepnode
    local sweeptype     = sweeptype
    local sweepoverflow = false
    local checkdisc     = getprev(head)
    local keepdisc      = not sweepnode
    local lookaheaddisc = nil
    local backtrackdisc = nil
    local current       = start
    local last          = start
    local prev          = getprev(start)
    local hasglue       = false
    local useddisc      = nil   -- new 2022-09-25
    local usedstart     = start -- new 2022-09-25

    -- fishy: so we can overflow and then go on in the sweep?
    -- todo : id can also be glue_code as we checked spaces

    local i = f
    while i <= l do
        local id = getid(current)
        if id == glyph_code then
            i       = i + 1
            last    = current
            current = getnext(current)
        elseif id == glue_code then
            i       = i + 1
            last    = current
            current = getnext(current)
            hasglue = true
        elseif id == disc_code then
            if keepdisc then
                keepdisc      = false
                lookaheaddisc = current
                local replace = getreplace(current)
                if not replace then
                    sweepoverflow = true
                    sweepnode     = current
                    current       = getnext(current)
                else
                    -- we can use an iterator
                    while replace and i <= l do
                        if getid(replace) == glyph_code then
                            i = i + 1
                        end
                        replace = getnext(replace)
                    end
                    current = getnext(replace)
                end
                last = current
            else
                head, current = flattendisk(head,current)
            end
        else
            last    = current
            current = getnext(current)
        end
        if current then
            -- go on
        elseif sweepoverflow then
            -- we already are following up on sweepnode
            break
        elseif sweeptype == "post" or sweeptype == "replace" then
            current = getnext(sweepnode)
            if current then
                sweeptype     = nil
                sweepoverflow = true
            else
                break
            end
        else
            break -- added
        end
    end

    if sweepoverflow then
        local prev = current and getprev(current)
        if not current or prev ~= sweepnode then
            local head = getnext(sweepnode)
            local tail = nil
            if prev then
                tail = prev
                setprev(current,sweepnode)
            else
                tail = findnodetail(head)
            end
            setnext(sweepnode,current)
            setprev(head)
            setnext(tail)
            appenddisc(sweepnode,head)
        end
    end

    if l < s then
        local i = l
        local t = sweeptype == "post" or sweeptype == "replace"
        while current and i < s do
            local id = getid(current)
            if id == glyph_code then
                i       = i + 1
                current = getnext(current)
            elseif id == glue_code then
                i       = i + 1
                current = getnext(current)
                hasglue = true
            elseif id == disc_code then
                if keepdisc then
                    keepdisc = false
                    if notmatchpre[current] ~= notmatchreplace[current] then
                        lookaheaddisc = current
                    end
                    -- we assume a simple text only replace (we could use nuts.count)
                    local replace = getreplace(current)
                    while replace and i < s do
                        if getid(replace) == glyph_code then
                            i = i + 1
                        end
                        replace = getnext(replace)
                    end
                    current = getnext(current)
                elseif notmatchpre[current] ~= notmatchreplace[current] then
                    head, current = flattendisk(head,current)
                else
                    current = getnext(current) -- HH
                end
            else
                current = getnext(current)
            end
            if not current and t then
                current = getnext(sweepnode)
                if current then
                    sweeptype = nil
                end
            end
        end
    end

    if f > 1 then
        local current = prev
        local i       = f
        local t       = sweeptype == "pre" or sweeptype == "replace"
        if not current and t and current == checkdisc then
            current = getprev(sweepnode)
        end
        while current and i > 1 do -- missing getprev added / moved outside
            local id = getid(current)
            if id == glyph_code then
                i = i - 1
            elseif id == glue_code then
                i       = i - 1
                hasglue = true
            elseif id == disc_code then
                if keepdisc then
                    keepdisc = false
                    if notmatchpost[current] ~= notmatchreplace[current] then
                        backtrackdisc = current
                    end
                    -- we assume a simple text only replace (we could use nuts.count)
                    local replace = getreplace(current)
                    while replace and i > 1 do
                        if getid(replace) == glyph_code then
                            i = i - 1
                        end
                        replace = getnext(replace)
                    end
                elseif notmatchpost[current] ~= notmatchreplace[current] then
                    head, current = flattendisk(head,current)
                end
            end
            current = getprev(current)
            if t and current == checkdisc then
                current = getprev(sweepnode)
            end
        end
    end

    local done = false

    if lookaheaddisc then
        local cf            = start
        local cl            = getprev(lookaheaddisc)
        local cprev         = getprev(start)
        local insertedmarks = 0
        while cprev do
            local char = ischar(cf,currentfont)
            if char and marks[char] then
                insertedmarks = insertedmarks + 1
                cf            = cprev
                startishead   = cf == head
                cprev         = getprev(cprev)
            else
                break
            end
        end
        setlink(cprev,lookaheaddisc)
        setprev(cf)
        setnext(cl)
        if startishead then
            head = lookaheaddisc
        end
        local pre, post, replace = getdisc(lookaheaddisc)
        local new  = copynodelist(cf) -- br, how often does that happen
        local cnew = new
        if pre then
            setlink(findnodetail(cf),pre)
        end
        if replace then
            local tail = findnodetail(new)
            setlink(tail,replace)
        end
        for i=1,insertedmarks do
            cnew = getnext(cnew)
        end
        cl = start
        local clast = cnew
        for i=f,l do
            cl    = getnext(cl)
            clast = getnext(clast)
        end
        if not notmatchpre[lookaheaddisc] then
            local ok = false
            cf, start, ok = chainrun(cf,start,cl,dataset,sequence,rlmode,skiphash,ck,1)
            if ok then
                done = true
            end
        end
        if not notmatchreplace[lookaheaddisc] then
            local ok = false
            new, cnew, ok = chainrun(new,cnew,clast,dataset,sequence,rlmode,skiphash,ck,2)
            if ok then
                done = true
            end
        end
        if hasglue then
            setdiscchecked(lookaheaddisc,cf,post,new)
        else
            setdisc(lookaheaddisc,cf,post,new)
        end
        start          = getprev(lookaheaddisc)
        useddisc       = lookaheaddisc -- new 2022-09-25
        sweephead[cf]  = getnext(clast) or false
        sweephead[new] = getnext(cl) or false
    elseif backtrackdisc then
        local cf            = getnext(backtrackdisc)
        local cl            = start
        local cnext         = getnext(start)
        local insertedmarks = 0
        while cnext do
            local char = ischar(cnext,currentfont)
            if char and marks[char] then
                insertedmarks = insertedmarks + 1
                cl            = cnext
                cnext         = getnext(cnext)
            else
                break
            end
        end
        setlink(backtrackdisc,cnext)
        setprev(cf)
        setnext(cl)
        local pre, post, replace, pretail, posttail, replacetail = getdisc(backtrackdisc,true)
        local new  = copynodelist(cf)
        local cnew = findnodetail(new)
        for i=1,insertedmarks do
            cnew = getprev(cnew)
        end
        local clast = cnew
        for i=f,l do
            clast = getnext(clast)
        end
        if not notmatchpost[backtrackdisc] then
            local ok = false
            cf, start, ok = chainrun(cf,start,last,dataset,sequence,rlmode,skiphash,ck,3)
            if ok then
                done = true
            end
        end
        if not notmatchreplace[backtrackdisc] then
            local ok = false
            new, cnew, ok = chainrun(new,cnew,clast,dataset,sequence,rlmode,skiphash,ck,4)
            if ok then
                done = true
            end
        end
        if post then
            setlink(posttail,cf)
        else
            post = cf
        end
        if replace then
            setlink(replacetail,new)
        else
            replace = new
        end
        if hasglue then
            setdiscchecked(backtrackdisc,pre,post,replace)
        else
            setdisc(backtrackdisc,pre,post,replace)
        end
        start              = getprev(backtrackdisc)
        useddisc           = backtrackdisc -- new 2022-09-25
        sweephead[post]    = getnext(clast) or false
        sweephead[replace] = getnext(last) or false
    else

        local ok = false
        head, start, ok = chainrun(head,start,last,dataset,sequence,rlmode,skiphash,ck,5)
        if ok then
            done = true
        end

    end
    if useddisc and start ~= usedstart then -- make this option per font -- new 2022-09-25
       start = getnext(start)                                            -- new 2022-09-25
    end                                                                  -- new 2022-09-25
    return head, start, done, useddisc                                   -- new 2022-09-25
end

local chaintrac do

    local level = 0
    local last  = { }

    chaintrac = function(head,start,dataset,sequence,rlmode,skiphash,ck,match,discseen,sweepnode)
        if dataset then
            level = level + 1
            last[level] = start
            local rule       = ck[1]
            local lookuptype = ck[8] or ck[2]
            local nofseq     = #ck[3] -- ck[3].n
            local first      = ck[4]
            local last       = ck[5]
            local char       = getchar(start)
            logwarning("+ %i : %s: rule %s %s at char %s for (%s,%s,%s) chars, lookuptype %a, %sdisc seen, %ssweeping",
                level,cref(dataset,sequence),rule,match and "matches" or "nomatch",
                gref(char),first-1,last-first+1,nofseq-last,lookuptype,
                discseen and "" or "no ", sweepnode and "" or "not ")
        else
            -- (start,done)
            local what  = start and "done" or "continue"
            local where = head == last[level] and "same" or "different"
            local char  = getchar(head)
            if char then
                logwarning("- %i : %s at char %s, %s node",level,what,gref(char),where)
            else
                logwarning("- %i : %s, %s node",level,what,where)
            end
            level = level - 1
        end
    end

end

-- The next one is quite optimized but still somewhat slow, fonts like ebgaramond
-- are real torture tests because they have many steps with one context (having
-- multiple contexts makes more sense) also because we (can) reduce them. Instead of
-- a match boolean variable and check for that I decided to use a goto with labels
-- instead. This is one of the cases where it makes the code more readable and we
-- might even gain a bit performance.

-- when we have less replacements (lookups) then current matches we can push too much into
-- the previous disc .. such be it (<before><disc><current=fl><after> with only f done)

local function handle_contextchain(head,start,dataset,sequence,contexts,rlmode,skiphash)
    if not contexts then
        return head, start, false
    end
    -- optimizing for rlmode gains nothing
    local sweepnode    = sweepnode
    local sweeptype    = sweeptype
    local postreplace
    local prereplace
    local checkdisc
    local discseen  -- = false
    if sweeptype then
        if sweeptype == "replace" then
            postreplace = true
            prereplace  = true
        else
            postreplace = sweeptype == "post"
            prereplace  = sweeptype == "pre"
        end
        checkdisc = getprev(head)
    end
    local currentfont  = currentfont

    local skipped   -- = false

    local startprev,
          startnext    = getboth(start)
    local done      -- = false

    -- we can have multiple hits and as we scan (currently) all we need to check
    -- if we have a match ... contextchains have no real coverage table (with
    -- unique entries)

    -- fonts can have many steps (each doing one check) or many contexts

    -- todo: make a per-char cache so that we have small contexts (when we have a context
    -- n == 1 and otherwise it can be more so we can even distinguish n == 1 or more)

    local nofcontexts = contexts.n -- #contexts

    local startchar = nofcontext == 1 or ischar(start,currentfont) -- only needed in a chain

    for k=1,nofcontexts do -- does this disc mess work well with n > 1

        local ck   = contexts[k]
        local seq  = ck[3]
        local f    = ck[4] -- first current
        local last = start
        if not startchar or not seq[f][startchar] then
            -- report("no hit in %a at %i of %i contexts",sequence.type,k,nofcontexts)
            goto next
        end
        local s = seq.n -- or #seq
        if s == 1 then
            -- bit weird case: why use a chain, but it is a hit
        else
            local l       = ck[5] -- last current
            local current = start
         -- local last    = start

            -- current match

            if l > f then
                -- before/current/after | before/current | current/after
                local discfound -- = nil
                local n = f + 1
                last = startnext -- the second in current (first already matched)
                while n <= l do
                    if postreplace and not last then
                        last      = getnext(sweepnode)
                        sweeptype = nil
                    end
                    if last then
                        local char, id = ischar(last,currentfont)
                        if char then
                            if skiphash and skiphash[char] then
                                skipped = true
                                if trace_skips then
                                    show_skip(dataset,sequence,char,ck,classes[char])
                                end
                                last = getnext(last)
                            elseif seq[n][char] then
                                if n < l then
                                    last = getnext(last)
                                end
                                n = n + 1
                            elseif discfound then
                                notmatchreplace[discfound] = true
                                if notmatchpre[discfound] then
                                    goto next
                                else
                                    break
                                end
                            else
                                goto next
                            end
                        elseif char == false then
                            if discfound then
                                notmatchreplace[discfound] = true
                                if notmatchpre[discfound] then
                                    goto next
                                else
                                    break
                                end
                            else
                                goto next
                            end
                        elseif id == disc_code then
                     -- elseif id == disc_code and (not discs or discs[last]) then
                            discseen              = true
                            discfound             = last
                            notmatchpre[last]     = nil
                            notmatchpost[last]    = true
                            notmatchreplace[last] = nil
                            local pre, post, replace = getdisc(last)
                            if pre then
                                local n = n
                                while pre do
                                    if seq[n][getchar(pre)] then
                                        n = n + 1
                                        if n > l then
                                            break
                                        end
                                        pre = getnext(pre)
                                    else
                                        notmatchpre[last] = true
                                        break
                                    end
                                end
                                if n <= l then
                                    notmatchpre[last] = true
                                end
                            else
                                notmatchpre[last] = true
                            end
                            if replace then
                                -- so far we never entered this branch
                                while replace do
                                    if seq[n][getchar(replace)] then
                                        n = n + 1
                                        if n > l then
                                            break
                                        end
                                        replace = getnext(replace)
                                    else
                                        notmatchreplace[last] = true
                                        if notmatchpre[last] then
                                            goto next
                                        else
                                            break
                                        end
                                    end
                                end
                                -- why here again
                                if notmatchpre[last] then
                                    goto next
                                end
                            end
                            -- maybe only if match
                            last = getnext(last)
                        else
                            goto next
                        end
                    else
                        goto next
                    end
                end
            end

            -- before

            if f > 1 then
              -- if startprev then -- new 2022-09-25
                    local prev = startprev
                    if prereplace and prev == checkdisc then
                        prev = getprev(sweepnode)
                    end
                    if prev then
                        local discfound -- = nil
                        local n = f - 1
                        while n >= 1 do
                            if prev then
                                local char, id = ischar(prev,currentfont)
                                if char then
                                    if skiphash and skiphash[char] then
                                        skipped = true
                                        if trace_skips then
                                            show_skip(dataset,sequence,char,ck,classes[char])
                                        end
                                        prev = getprev(prev)
                                    elseif seq[n][char] then
                                        if n > 1 then
                                            prev = getprev(prev)
                                        end
                                        n = n - 1
                                    elseif discfound then
                                        notmatchreplace[discfound] = true
                                        if notmatchpost[discfound] then
                                            goto next
                                        else
                                            break
                                        end
                                    else
                                        goto next
                                    end
                                elseif char == false then
                                    if discfound then
                                        notmatchreplace[discfound] = true
                                        if notmatchpost[discfound] then
                                            goto next
                                        end
                                    else
                                        goto next
                                    end
                                    break
                                elseif id == disc_code then
                             -- elseif id == disc_code and (not discs or discs[prev]) then
                                    -- the special case: f i where i becomes dottless i ..
                                    discseen              = true
                                    discfound             = prev
                                    notmatchpre[prev]     = true
                                    notmatchpost[prev]    = nil
                                    notmatchreplace[prev] = nil
                                    local pre, post, replace, pretail, posttail, replacetail = getdisc(prev,true)
                                    -- weird test: needs checking
                                    if pre ~= start and post ~= start and replace ~= start then
                                        if post then
                                            local n = n
                                            while posttail do
                                                if seq[n][getchar(posttail)] then
                                                    n = n - 1
                                                    if posttail == post or n < 1 then
                                                        break
                                                    else
                                                        posttail = getprev(posttail)
                                                    end
                                                else
                                                    notmatchpost[prev] = true
                                                    break
                                                end
                                            end
                                            if n >= 1 then
                                                notmatchpost[prev] = true
                                            end
                                        else
                                            notmatchpost[prev] = true
                                        end
                                        if replace then
                                            -- we seldom enter this branch (e.g. on brill efficient)
                                            while replacetail do
                                                if seq[n][getchar(replacetail)] then
                                                    n = n - 1
                                                    if replacetail == replace or n < 1 then
                                                        break
                                                    else
                                                        replacetail = getprev(replacetail)
                                                    end
                                                else
                                                    notmatchreplace[prev] = true
                                                    if notmatchpost[prev] then
                                                        goto next
                                                    else
                                                        break
                                                    end
                                                end
                                            end
                                        else
                                         -- notmatchreplace[prev] = true -- not according to Kai
                                        end
                                    end
                                    prev = getprev(prev)
                             -- elseif id == glue_code and seq[n][32] and isspace(prev,threshold,id) then
                             -- elseif seq[n][32] and spaces[prev] then
                             --     n = n - 1
                             --     prev = getprev(prev)
                                elseif id == glue_code then
                                    local sn = seq[n]
                                    if (sn[32] and spaces[prev]) or sn[0xFFFC] then
                                        n = n - 1
                                        prev = getprev(prev)
                                    else
                                        goto next
                                    end
                                elseif seq[n][0xFFFC] then
                                    n = n - 1
                                    prev = getprev(prev)
                                else
                                    goto next
                                end
                            else
                                goto next
                            end
                        end
                    else
                        goto next
                    end
             -- else          -- new 2022-09-25
             --     goto next -- new 2022-09-25
             -- end           -- new 2022-09-25
            end

            -- after

            if s > l then
                local current = last and getnext(last)
                if not current and postreplace then
                    current = getnext(sweepnode)
                end
                if current then
                    local discfound -- = nil
                    local n = l + 1
                    while n <= s do
                        if current then
                            local char, id = ischar(current,currentfont)
                            if char then
                                if skiphash and skiphash[char] then
                                    skipped = true
                                    if trace_skips then
                                        show_skip(dataset,sequence,char,ck,classes[char])
                                    end
                                    current = getnext(current) -- was absent
                                elseif seq[n][char] then
                                    if n < s then -- new test
                                        current = getnext(current) -- was absent
                                    end
                                    n = n + 1
                                elseif discfound then
                                    notmatchreplace[discfound] = true
                                    if notmatchpre[discfound] then
                                        goto next
                                    else
                                        break
                                    end
                                else
                                    goto next
                                end
                            elseif char == false then
                                if discfound then
                                    notmatchreplace[discfound] = true
                                    if notmatchpre[discfound] then
                                        goto next
                                    else
                                        break
                                    end
                                else
                                    goto next
                                end
                            elseif id == disc_code then
                         -- elseif id == disc_code and (not discs or discs[current]) then
                                discseen                 = true
                                discfound                = current
                                notmatchpre[current]     = nil
                                notmatchpost[current]    = true
                                notmatchreplace[current] = nil
                                local pre, post, replace = getdisc(current)
                                if pre then
                                    local n = n
                                    while pre do
                                        if seq[n][getchar(pre)] then
                                            n = n + 1
                                            if n > s then
                                                break
                                            else
                                                pre = getnext(pre)
                                            end
                                        else
                                            notmatchpre[current] = true
                                            break
                                        end
                                    end
                                    if n <= s then
                                        notmatchpre[current] = true
                                    end
                                else
                                    notmatchpre[current] = true
                                end
                                if replace then
                                    -- so far we never entered this branch
                                    while replace do
                                        if seq[n][getchar(replace)] then
                                            n = n + 1
                                            if n > s then
                                                break
                                            else
                                                replace = getnext(replace)
                                            end
                                        else
                                            notmatchreplace[current] = true
                                            if notmatchpre[current] then
                                                goto next
                                            else
                                                break
                                            end
                                        end
                                    end
                                else
                                 -- notmatchreplace[current] = true -- not according to Kai
                                end
                                current = getnext(current)
                            elseif id == glue_code then
                                local sn = seq[n]
                                if (sn[32] and spaces[current]) or sn[0xFFFC] then
                                    n = n + 1
                                    current = getnext(current)
                                else
                                    goto next
                                end
                            elseif seq[n][0xFFFC] then
                                n = n + 1
                                current = getnext(current)
                            else
                                goto next
                            end
                        else
                            goto next
                        end
                    end
                else
                    goto next
                end
            end
        end
        if trace_contexts then
            chaintrac(head,start,dataset,sequence,rlmode,skipped and skiphash,ck,true,discseen,sweepnode)
        end
        if discseen or sweepnode then
            -- When we process a disc we can collapse and therefore we backtrack one node (start) and
            -- reprocess. This is needed because there might be more in the collapsed list.
            head, start, done = chaindisk(head,start,dataset,sequence,rlmode,skipped and skiphash,ck)
        else
            head, start, done = chainrun(head,start,last,dataset,sequence,rlmode,skipped and skiphash,ck,6)
        end
        if trace_contexts then
            chaintrac(start,done)
        end
        if done then
            break
     -- else
            -- next context
        end
      ::next::
    end
    if discseen then
        notmatchpre     = { }
        notmatchpost    = { }
        notmatchreplace = { }
     -- notmatchpre     = { a = 1, b = 1 }  notmatchpre    .a = nil notmatchpre    .b = nil
     -- notmatchpost    = { a = 1, b = 1 }  notmatchpost   .a = nil notmatchpost   .b = nil
     -- notmatchreplace = { a = 1, b = 1 }  notmatchreplace.a = nil notmatchreplace.b = nil
    end
    return head, start, done
end

handlers.gsub_context             = handle_contextchain
handlers.gsub_contextchain        = handle_contextchain
handlers.gsub_reversecontextchain = handle_contextchain
handlers.gpos_contextchain        = handle_contextchain
handlers.gpos_context             = handle_contextchain

-- local function chained_contextchain(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash)
--     local steps    = currentlookup.steps
--     local nofsteps = currentlookup.nofsteps
--     if nofsteps > 1 then
--         reportmoresteps(dataset,sequence)
--     end
--     -- probably wrong
--     local l = steps[1].coverage[getchar(start)]
--     if l then
--         return handle_contextchain(head,start,dataset,sequence,l,rlmode,skiphash)
--     else
--         return head, start, false
--     end
-- end

-- new 2022-09-25

local function chained_contextchain(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash)
    local steps    = currentlookup.steps
    local nofsteps = currentlookup.nofsteps
    local char     = getchar(start)
    if nofsteps == 1 then
        local s = steps[1]
        local l = s.coverage[char]
        if l then
            return handle_contextchain(head,start,dataset,sequence,l,rlmode,skiphash)
        end
    else
        for i=1,nofsteps do
            local s = steps[i]
            local l = s.coverage[char]
            if l then
                local h, s, d = handle_contextchain(head,start,dataset,sequence,l,rlmode,skiphash)
                if d then
                    return h, s, d
                end
            end
        end
    end
    return head, start, false
end

chainprocs.gsub_context             = chained_contextchain
chainprocs.gsub_contextchain        = chained_contextchain
chainprocs.gsub_reversecontextchain = chained_contextchain
chainprocs.gpos_contextchain        = chained_contextchain
chainprocs.gpos_context             = chained_contextchain

------------------------------

-- experiment (needs no handler in font-otc so not now):
--
-- function otf.registerchainproc(name,f)
--  -- chainprocs[name] = f
--     chainprocs[name] = function(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash)
--         local done = currentlookup.nofsteps > 0
--         if not done then
--             reportzerosteps(dataset,sequence)
--         else
--             head, start, done = f(head,start,stop,dataset,sequence,currentlookup,rlmode,skiphash)
--             if not head or not start then
--                 reportbadsteps(dataset,sequence)
--             end
--         end
--         return head, start, done
--     end
-- end

local missing    = setmetatableindex("table")
local logwarning = report_process
local resolved   = { } -- we only resolve a font,script,language pair once

local function logprocess(...)
    if trace_steps then
        registermessage(...)
        if trace_steps == "silent" then
            return
        end
    end
    report_process(...)
end

-- todo: pass all these 'locals' in a table

local sequencelists = setmetatableindex(function(t,font)
    local sequences = fontdata[font].resources.sequences
    if not sequences or not next(sequences) then
        sequences = false
    end
    t[font] = sequences
    return sequences
end)

-- fonts.hashes.sequences = sequencelists

do -- overcome local limit

    local autofeatures    = fonts.analyzers.features
    local featuretypes    = otf.tables.featuretypes
    local defaultscript   = otf.features.checkeddefaultscript
    local defaultlanguage = otf.features.checkeddefaultlanguage

    local wildcard        = "*"
    local default         = "dflt"

    local function initialize(sequence,script,language,enabled,autoscript,autolanguage)
        local features = sequence.features
        if features then
            local order = sequence.order
            if order then
                local featuretype = featuretypes[sequence.type or "unknown"]
                for i=1,#order do
                    local kind  = order[i]
                    local valid = enabled[kind]
                    if valid then
                        local scripts   = features[kind]
                        local languages = scripts and (
                            scripts[script] or
                            scripts[wildcard] or
                            (autoscript and defaultscript(featuretype,autoscript,scripts))
                        )
                        local enabled = languages and (
                            languages[language] or
                            languages[wildcard] or
                            (autolanguage and defaultlanguage(featuretype,autolanguage,languages))
                        )
                        if enabled then
                            return { valid, autofeatures[kind] or false, sequence, kind }
                        end
                    end
                end
            else
                -- can't happen
            end
        end
        return false
    end

    function otf.dataset(tfmdata,font) -- generic variant, overloaded in context
        local shared       = tfmdata.shared
        local properties   = tfmdata.properties
        local language     = properties.language or "dflt"
        local script       = properties.script   or "dflt"
        local enabled      = shared.features
        local autoscript   = enabled and enabled.autoscript
        local autolanguage = enabled and enabled.autolanguage
        local res = resolved[font]
        if not res then
            res = { }
            resolved[font] = res
        end
        local rs = res[script]
        if not rs then
            rs = { }
            res[script] = rs
        end
        local rl = rs[language]
        if not rl then
            rl = {
                -- indexed but we can also add specific data by key
            }
            rs[language] = rl
            local sequences = tfmdata.resources.sequences
            if sequences then
                for s=1,#sequences do
                    local v = enabled and initialize(sequences[s],script,language,enabled,autoscript,autolanguage)
                    if v then
                        rl[#rl+1] = v
                    end
                end
            end
        end
        return rl
    end

end

-- Functions like kernrun, comprun etc evolved over time and in the end look rather
-- complex. It's a bit of a compromise between extensive copying and creating subruns.
-- The logic has been improved a lot by Kai and Ivo who use complex fonts which
-- really helped to identify border cases on the one hand and get insight in the diverse
-- ways fonts implement features (not always that consistent and efficient). At the same
-- time I tried to keep the code relatively efficient so that the overhead in runtime
-- stays acceptable.

local function report_disc(what,n)
    report_run("%s: %s > %s",what,n,languages.serializediscretionary(n))
end

local function kernrun(disc,k_run,font,attr,...)
    --
    -- we catch <font 1><disc font 2>
    --
    if trace_kernruns then
        report_disc("kern",disc)
    end
    --
    local prev, next = getboth(disc)
    --
    local nextstart = next
    local done      = false
    --
    local pre, post, replace, pretail, posttail, replacetail = getdisc(disc,true)
    --
    local prevmarks = prev
    --
    -- can be optional, because why on earth do we get a disc after a mark (okay, maybe when a ccmp
    -- has happened but then it should be in the disc so basically this test indicates an error)
    --
    while prevmarks do
        local char = ischar(prevmarks,font)
        if char and marks[char] then
            prevmarks = getprev(prevmarks)
        else
            break
        end
    end
    --
    if prev and not ischar(prev,font) then  -- and (pre or replace)
        prev = false
    end
    if next and not ischar(next,font) then  -- and (post or replace)
        next = false
    end
    --
    -- we need to get rid of this nest mess some day .. has to be done otherwise
    --
    if pre then
        if k_run(pre,"injections",nil,font,attr,...) then
            done = true
        end
        if prev then
            setlink(prev,pre)
            if k_run(prevmarks,"preinjections",pre,font,attr,...) then -- or prev?
                done = true
            end
            setprev(pre)
            setlink(prev,disc)
        end
    end
    --
    if post then
        if k_run(post,"injections",nil,font,attr,...) then
            done = true
        end
        if next then
            setlink(posttail,next)
            if k_run(posttail,"postinjections",next,font,attr,...) then
                done = true
            end
            setnext(posttail)
            setlink(disc,next)
        end
    end
    --
    if replace then
        if k_run(replace,"injections",nil,font,attr,...) then
            done = true
        end
        if prev then
            setlink(prev,replace)
            if k_run(prevmarks,"replaceinjections",replace,font,attr,...) then -- getnext(replace))
                done = true
            end
            setprev(replace)
            setlink(prev,disc)
        end
        if next then
            setlink(replacetail,next)
            if k_run(replacetail,"replaceinjections",next,font,attr,...) then
                done = true
            end
            setnext(replacetail)
            setlink(disc,next)
        end
    elseif prev and next then
        setlink(prev,next)
        if k_run(prevmarks,"emptyinjections",next,font,attr,...) then
            done = true
        end
        setlink(prev,disc,next)
    end
    if done and trace_testruns then
        report_disc("done",disc)
    end
 -- return nextstart, done
    return nextstart
end

-- fonts like ebgaramond do ligatures this way (less efficient than e.g. dejavu which
-- will do the testrun variant)

local function comprun(disc,c_run,...) -- vararg faster than the whole list
    if trace_compruns then
        report_disc("comp",disc)
    end
    --
    local pre, post, replace = getdisc(disc)
    local renewed = false
    --
    if pre then
        sweepnode = disc
        sweeptype = "pre" -- in alternative code preinjections is used (also used then for properties, saves a variable)
        local new, done = c_run(pre,...)
        if done then
            pre     = new
            renewed = true
        end
    end
    --
    if post then
        sweepnode = disc
        sweeptype = "post"
        local new, done = c_run(post,...)
        if done then
            post    = new
            renewed = true
        end
    end
    --
    if replace then
        sweepnode = disc
        sweeptype = "replace"
        local new, done = c_run(replace,...)
        if done then
            replace = new
            renewed = true
        end
    end
    --
    sweepnode = nil
    sweeptype = nil
    if renewed then
        if trace_testruns then
            report_disc("done",disc)
        end
        setdisc(disc,pre,post,replace)
    end
    --
 -- return getnext(disc), renewed
    return getnext(disc)
end

-- if we can hyphenate in a lig then unlikely a lig so we
-- could have a option here to ignore lig

local test_flatten_start = 2 -- must start at 2 according to Kai

directives.register("otf.testrun.forceflatten", function(v)
    test_flatten_start = v and 1 or 2
end)

local function testrun(disc,t_run,c_run,...)
    if trace_testruns then
        report_disc("test",disc)
    end
    local prev, next = getboth(disc)
    if not next then
        -- weird discretionary
        return
    end
    local pre, post, replace, pretail, posttail, replacetail = getdisc(disc,true)
    local renewed = false
    if post or replace then -- and prev then -- hm, we can start with a disc
        if post then
            setlink(posttail,next)
        else
            post = next
        end
        if replace then
            setlink(replacetail,next)
        else
            replace = next
        end
        local d_post    = t_run(post,next,...)
        local d_replace = t_run(replace,next,...)
        if d_post > 0 or d_replace > 0 then
            local d = d_replace > d_post and d_replace or d_post
            local head = getnext(disc) -- is: next
            local tail = head
            for i=test_flatten_start,d do
                local nx = getnext(tail)
                local id = getid(nx)
                if id == disc_code then
                    head, tail = flattendisk(head,nx)
                elseif id == glyph_code then
                    tail = nx
                else
                    -- we can have overrun into a glue
                    break
                end
            end
            next = getnext(tail)
            setnext(tail)
            setprev(head)
            local new  = copynodelist(head)
            if posttail then
                setlink(posttail,head)
            else
                post = head
            end
            if replacetail then
                setlink(replacetail,new)
            else
                replace = new
            end
        else
            -- we stay inside the disc
            if posttail then
                setnext(posttail)
            else
                post = nil
            end
            if replacetail then
                setnext(replacetail)
            else
                replace = nil
            end
        end
        setlink(disc,next)
     -- pre, post, replace, pretail, posttail, replacetail = getdisc(disc,true)
    end
    --
    -- like comprun
    --
    if trace_testruns then
        report_disc("more",disc)
    end
    --
    if pre then
        sweepnode = disc
        sweeptype = "pre"
        local new, ok = c_run(pre,...)
        if ok then
            pre     = new
            renewed = true
        end
    end
    --
    if post then
        sweepnode = disc
        sweeptype = "post"
        local new, ok = c_run(post,...)
        if ok then
            post    = new
            renewed = true
        end
    end
    --
    if replace then
        sweepnode = disc
        sweeptype = "replace"
        local new, ok = c_run(replace,...)
        if ok then
            replace = new
            renewed = true
        end
    end
    --
    sweepnode = nil
    sweeptype = nil
    if renewed then
        setdisc(disc,pre,post,replace)
        if trace_testruns then
            report_disc("done",disc)
        end
    end
    -- next can have changed (copied list)
 -- return getnext(disc), renewed
    return getnext(disc)
end

--  1{2{\oldstyle\discretionary{3}{4}{5}}6}7\par
--  1{2\discretionary{3{\oldstyle3}}{{\oldstyle4}4}{5{\oldstyle5}5}6}7\par

local nesting = 0

local function c_run_single(head,font,attr,lookupcache,step,dataset,sequence,rlmode,skiphash,handler)
    local done  = false
    local sweep = sweephead[head]
    local start
    if sweep then
        start = sweep
     -- sweephead[head] = nil
        sweephead[head] = false
    else
        start = head
    end
    while start do
        local char, id = ischar(start,font)
        if char then
            local a -- happens often so no assignment is faster
            if attr then
                a = getglyphdata(start)
            end
            if not a or (a == attr) then
                local lookupmatch = lookupcache[char]
                if lookupmatch then
                    local ok
                    head, start, ok = handler(head,start,dataset,sequence,lookupmatch,rlmode,skiphash,step)
                    if ok then
                        done = true
                    end
                end
                if start then
                    start = getnext(start)
                end
            else
                -- go on can be a mixed one
                start = getnext(start)
            end
        elseif char == false then
            return head, done
        elseif sweep then
            -- else we loose the rest
            return head, done
        else
            -- in disc component
            start = getnext(start)
        end
    end
    return head, done
end

-- only replace?

local function t_run_single(start,stop,font,attr,lookupcache)
    local lastd = nil
    while start ~= stop do
        local char = ischar(start,font)
        if char then
            local a -- happens often so no assignment is faster
            if attr then
                a = getglyphdata(start)
            end
            local startnext = getnext(start)
            if not a or (a == attr) then
                local lookupmatch = lookupcache[char]
                if lookupmatch then -- hm, hyphens can match (tlig) so we need to really check
                    -- if we need more than ligatures we can outline the code and use functions
                    local s     = startnext
                    local ss    = nil
                    local sstop = s == stop
                    if not s then
                        s = ss
                        ss = nil
                    end
                    -- a bit weird: why multiple ... anyway we can't have a disc in a disc
                    -- how about post ... we can probably merge this into the while
                    while getid(s) == disc_code do
                        ss = getnext(s)
                        s  = getreplace(s)
                        if not s then
                            s = ss
                            ss = nil
                        end
                    end
                    local l = nil
                    local d = 0
                    while s do
                        local char = ischar(s,font)
                        if char then
                            local lg = not tonumber(lookupmatch) and lookupmatch[char]
                            if lg then
                                if sstop then
                                    d = 1
                                elseif d > 0 then
                                    d = d + 1
                                end
                                l = lg
                                s = getnext(s)
                                sstop = s == stop
                                if not s then
                                    s  = ss
                                    ss = nil
                                end
                                while getid(s) == disc_code do
                                    ss = getnext(s)
                                    s  = getreplace(s)
                                    if not s then
                                        s  = ss
                                        ss = nil
                                    end
                                end
                                lookupmatch = lg
                            else
                                break
                            end
                        else
                            break
                        end
                    end
                    if l and (tonumber(l) or l.ligature) then -- so we test for ligature
                        lastd = d
                    end
                    -- why not: if not l then break elseif l.ligature then return d end
                else
                    -- why not: break
                    -- no match (yet)
                end
            else
                -- go on can be a mixed one
                -- why not: break
            end
            if lastd then
                return lastd
            end
            start = startnext
        else
            break
        end
    end
    return 0
end

local function k_run_single(sub,injection,last,font,attr,lookupcache,step,dataset,sequence,rlmode,skiphash,handler)
    local a -- happens often so no assignment is faster
    if attr then
        a = getglyphdata(sub)
    end
    if not a or (a == attr) then
        for n in nextnode, sub do -- only gpos
            if n == last then
                break
            end
            local char = ischar(n,font)
            if char then
                local lookupmatch = lookupcache[char]
                if lookupmatch then
                    local h, d, ok = handler(sub,n,dataset,sequence,lookupmatch,rlmode,skiphash,step,injection)
                    if ok then
                        return true
                    end
                end
            end
        end
    end
end

local function c_run_multiple(head,font,attr,steps,nofsteps,dataset,sequence,rlmode,skiphash,handler)
    local done  = false
    local sweep = sweephead[head]
    local start
    if sweep then
        start = sweep
     -- sweephead[head] = nil
        sweephead[head] = false
    else
        start = head
    end
    while start do
        local char = ischar(start,font)
        if char then
            local a -- happens often so no assignment is faster
            if attr then
                a = getglyphdata(start)
            end
            if not a or (a == attr) then
                for i=1,nofsteps do
                    local step        = steps[i]
                    local lookupcache = step.coverage
                    local lookupmatch = lookupcache[char]
                    if lookupmatch then
                        -- we could move all code inline but that makes things even more unreadable
                        local ok
                        head, start, ok = handler(head,start,dataset,sequence,lookupmatch,rlmode,skiphash,step)
                        if ok then
                            done = true
                            break
                        elseif not start then
                            -- don't ask why ... shouldn't happen
                            break
                        end
                    end
                end
                if start then
                    start = getnext(start)
                end
            else
                -- go on can be a mixed one
                start = getnext(start)
            end
        elseif char == false then
            -- whatever glyph
            return head, done
        elseif sweep then
            -- else we loose the rest
            return head, done
        else
            -- in disc component
            start = getnext(start)
        end
    end
    return head, done
end

local function t_run_multiple(start,stop,font,attr,steps,nofsteps)
    local lastd = nil
    while start ~= stop do
        local char = ischar(start,font)
        if char then
            local a -- happens often so no assignment is faster
            if attr then
                a = getglyphdata(start)
            end
            local startnext = getnext(start)
            if not a or (a == attr) then
                for i=1,nofsteps do
                    local step = steps[i]
                    local lookupcache = step.coverage
                    local lookupmatch = lookupcache[char]
                    if lookupmatch then
                        -- if we need more than ligatures we can outline the code and use functions
                        local s     = startnext
                        local ss    = nil
                        local sstop = s == stop
                        if not s then
                            s  = ss
                            ss = nil
                        end
                        while getid(s) == disc_code do
                            ss = getnext(s)
                            s  = getreplace(s)
                            if not s then
                                s  = ss
                                ss = nil
                            end
                        end
                        local l = nil
                        local d = 0
                        while s do
                            local char = ischar(s)
                            if char then
                                local lg = not tonumber(lookupmatch) and lookupmatch[char]
                                if lg then
                                    if sstop then
                                        d = 1
                                    elseif d > 0 then
                                        d = d + 1
                                    end
                                    l = lg
                                    s = getnext(s)
                                    sstop = s == stop
                                    if not s then
                                        s  = ss
                                        ss = nil
                                    end
                                    while getid(s) == disc_code do
                                        ss = getnext(s)
                                        s  = getreplace(s)
                                        if not s then
                                            s  = ss
                                            ss = nil
                                        end
                                    end
                                    lookupmatch = lg
                                else
                                    break
                                end
                            else
                                break
                            end
                        end
                        if l and (tonumber(l) or l.ligature) then
                            lastd = d
                        end
                    end
                end
            else
                -- go on can be a mixed one
            end
            if lastd then
                return lastd
            end
            start = startnext
        else
            break
        end
    end
    return 0
end

local function k_run_multiple(sub,injection,last,font,attr,steps,nofsteps,dataset,sequence,rlmode,skiphash,handler)
    local a -- happens often so no assignment is faster
    if attr then
        a = getglyphdata(sub)
    end
    if not a or (a == attr) then
        for n in nextnode, sub do -- only gpos
            if n == last then
                break
            end
            local char = ischar(n)
            if char then
                for i=1,nofsteps do
                    local step        = steps[i]
                    local lookupcache = step.coverage
                    local lookupmatch = lookupcache[char]
                    if lookupmatch then
                        local h, d, ok = handler(sub,n,dataset,sequence,lookupmatch,rlmode,skiphash,step,injection) -- sub was head
                        if ok then
                            return true
                        end
                    end
                end
            end
        end
    end
end

local txtdirstate, pardirstate  do -- this might change (no need for nxt in pardirstate)

    local getdirection = nuts.getdirection

    txtdirstate = function(start,stack,top,rlparmode)
        local dir, pop = getdirection(start)
        if pop then
            if top == 1 then
                return 0, rlparmode
            else
                top = top - 1
                if stack[top] == righttoleft_code then
                    return top, -1
                else
                    return top, 1
                end
            end
        elseif dir == lefttoright_code then
            top = top + 1
            stack[top] = lefttoright_code
            return top, 1
        elseif dir == righttoleft_code then
            top = top + 1
            stack[top] = righttoleft_code
            return top, -1
        else
            return top, rlparmode
        end
    end

    pardirstate = function(start)
        local dir = getdirection(start)
        if dir == lefttoright_code then
            return 1, 1
        elseif dir == righttoleft_code then
            return -1, -1
        else
            return 0, 0
        end
    end

end

-- These are non public helpers that can change without notice!

otf.helpers             = otf.helpers or { }
otf.helpers.txtdirstate = txtdirstate
otf.helpers.pardirstate = pardirstate

-- This is the main loop. We run over the node list dealing with a specific font. The
-- attribute is a context specific thing. We could work on sub start-stop ranges instead
-- but I wonder if there is that much speed gain (experiments showed that it made not
-- much sense) and we need to keep track of directions anyway. Also at some point I
-- want to play with font interactions and then we do need the full sweeps. Apart from
-- optimizations the principles of processing the features hasn't changed much since
-- the beginning.

do

    -- This is a measurable experimental speedup (only with hyphenated text and multiple
    -- fonts per processor call), especially for fonts with lots of contextual lookups.

    local fastdisc = true
    local testdics = false

    directives.register("otf.fastdisc",function(v) fastdisc = v end) -- normally enabled

    -- using a merged combined hash as first test saves some 30% on ebgaramond and
    -- about 15% on arabtype .. then moving the a test also saves a bit (even when
    -- often a is not set at all so that one is a bit debatable

    local otfdataset = nil -- todo: make an installer

    local getfastdisc = { __index = function(t,k)
        local v = usesfont(k,currentfont)
        t[k] = v
        return v
    end }

    local getfastspace = { __index = function(t,k)
        -- we don't pass the id so that one can overload isspace
        local v = isspace(k,threshold) or false
        t[k] = v
        return v
    end }

    function otf.featuresprocessor(head,font,attr,direction,n)

        local sequences = sequencelists[font] -- temp hack

        nesting = nesting + 1

        if nesting == 1 then
            currentfont  = font
            tfmdata      = fontdata[font]
            descriptions = tfmdata.descriptions -- only needed in gref so we could pass node there instead
            characters   = tfmdata.characters   -- but this branch is not entered that often anyway
      local resources    = tfmdata.resources
            marks        = resources.marks
            classes      = resources.classes
            threshold,
            factor       = getthreshold(font)
            checkmarks   = tfmdata.properties.checkmarks

            if not otfdataset then
                otfdataset = otf.dataset
            end

            discs  = fastdisc and n and n > 1 and setmetatable({},getfastdisc) -- maybe inline
            spaces = setmetatable({},getfastspace)

        elseif currentfont ~= font then

            report_warning("nested call with a different font, level %s, quitting",nesting)
            nesting = nesting - 1
            return head, false

        end

        -- some 10% faster when no dynamics but hardly measureable on real runs .. but: it only
        -- works when we have no other dynamics as otherwise the zero run will be applied to the
        -- whole stream for which we then need to pass another variable which we won't

        -- if attr == 0 then
        --     attr = false
        -- end

        if trace_steps then
            checkstep(head)
        end

        local initialrl = 0

        if getid(head) == par_code and startofpar(head) then
            initialrl = pardirstate(head)
        elseif direction == righttoleft_code then
            initialrl = -1
        end

     -- local done      = false
        local datasets  = otfdataset(tfmdata,font,attr)
        local dirstack  = { nil } -- could move outside function but we can have local runs
        sweephead       = { }
     -- sweephead  = { a = 1, b = 1 } sweephead.a = nil sweephead.b = nil

        -- Keeping track of the headnode is needed for devanagari. (I generalized it a bit
        -- so that multiple cases are also covered.) We could prepend a temp node.

        -- We don't goto the next node when a disc node is created so that we can then treat
        -- the pre, post and replace. It's a bit of a hack but works out ok for most cases.

        for s=1,#datasets do
            local dataset      = datasets[s]
            local attribute    = dataset[2]
            local sequence     = dataset[3] -- sequences[s] -- also dataset[5]
            local rlparmode    = initialrl
            local topstack     = 0
            local typ          = sequence.type
            local gpossing     = typ == "gpos_single" or typ == "gpos_pair" -- store in dataset
            local forcetestrun = typ == "gsub_ligature" -- testrun is only for ligatures
            local handler      = handlers[typ] -- store in dataset
            local steps        = sequence.steps
            local nofsteps     = sequence.nofsteps
            local skiphash     = sequence.skiphash

            if not steps then
                -- This permits injection, watch the different arguments. Watch out, the arguments passed
                -- are not frozen as we might extend or change this. Is this used at all apart from some
                -- experiments?
                local h, ok = handler(head,dataset,sequence,initialrl,font,attr) -- less arguments now
             -- if ok then
             --     done = true
             -- end
                if h and h ~= head then
                    head = h
                end
            elseif typ == "gsub_reversecontextchain" then
                --
                -- This might need a check: if we have #before or #after > 0 then we might need to reverse
                -- the before and after lists in the loader. But first I need to see a font that uses multiple
                -- matches.
                --
                local start  = findnodetail(head)
                local rlmode = 0 -- how important is this .. do we need to check for dir?
                local merged = steps.merged
                while start do
                    local char = ischar(start,font)
                    if char then
                        local m = merged[char]
                        if m then
                            local a -- happens often so no assignment is faster
                            if attr then
                                a = getglyphdata(start)
                            end
                            if not a or (a == attr) then
                                for i=m[1],m[2] do
                                    local step = steps[i]
                             -- for i=1,#m do
                             --     local step = m[i]
                                    local lookupcache = step.coverage
                                    local lookupmatch = lookupcache[char]
                                    if lookupmatch then
                                        local ok
                                        head, start, ok = handler(head,start,dataset,sequence,lookupmatch,rlmode,skiphash,step)
                                        if ok then
                                         -- done = true
                                            break
                                        end
                                    end
                                end
                                if start then
                                    start = getprev(start)
                                end
                            else
                                start = getprev(start)
                            end
                        else
                            start = getprev(start)
                        end
                    else
                        start = getprev(start)
                    end
                end
            else
                local start  = head
                local rlmode = initialrl
                if nofsteps == 1 then -- happens often
                    local step = steps[1]
                    local lookupcache = step.coverage
                    while start do
                        local char, id = ischar(start,font)
                        if char then
                            if skiphash and skiphash[char] then -- we never needed it here but let's try
                                start = getnext(start)
                            else
                                local lookupmatch = lookupcache[char]
                                if lookupmatch then
                                    local a -- happens often so no assignment is faster
                                    if attr then
                                        if getglyphdata(start) == attr and (not attribute or getstate(start,attribute)) then
                                            a = true
                                        end
                                    elseif not attribute or getstate(start,attribute) then
                                        a = true
                                    end
                                    if a then
                                        local ok, df
                                        head, start, ok, df = handler(head,start,dataset,sequence,lookupmatch,rlmode,skiphash,step)
                                     -- if ok then
                                     --     done = true
                                     -- end
                                        if df then
-- print("restart 1",typ)
                                        elseif start then
                                            start = getnext(start)
                                        end
                                    else
                                        start = getnext(start)
                                    end
                                else
                                   start = getnext(start)
                                end
                            end
                        elseif char == false or id == glue_code then
                            -- a different font|state or glue (happens often)
                            start = getnext(start)
                        elseif id == disc_code then
                            if not discs or discs[start] == true then
                                if gpossing then
                                    start = kernrun(start,k_run_single,             font,attr,lookupcache,step,dataset,sequence,rlmode,skiphash,handler)
                                elseif forcetestrun then
                                    start = testrun(start,t_run_single,c_run_single,font,attr,lookupcache,step,dataset,sequence,rlmode,skiphash,handler)
                                else
                                    start = comprun(start,c_run_single,             font,attr,lookupcache,step,dataset,sequence,rlmode,skiphash,handler)
                                end
                            else
                                start = getnext(start)
                            end
                        elseif id == math_code then
                            start = getnext(endofmath(start))
                        elseif id == dir_code then
                            topstack, rlmode = txtdirstate(start,dirstack,topstack,rlparmode)
                            start = getnext(start)
                     -- elseif id == par_code and startofpar(start) then
                     --     rlparmode, rlmode = pardirstate(start)
                     --     start = getnext(start)
                        else
                            start = getnext(start)
                        end
                    end
                else
                    local merged = steps.merged
                    while start do
                        local char, id = ischar(start,font)
                        if char then
                            if skiphash and skiphash[char] then -- we never needed it here but let's try
                                start = getnext(start)
                            else
                                local m = merged[char]
                                if m then
                                    local a -- happens often so no assignment is faster
                                    if attr then
                                        if getglyphdata(start) == attr and (not attribute or getstate(start,attribute)) then
                                            a = true
                                        end
                                    elseif not attribute or getstate(start,attribute) then
                                        a = true
                                    end
                                    if a then
                                        local ok, df
                                        for i=m[1],m[2] do
                                            local step = steps[i]
                                     -- for i=1,#m do
                                     --     local step = m[i]
                                            local lookupcache = step.coverage
                                            local lookupmatch = lookupcache[char]
                                            if lookupmatch then
                                                -- we could move all code inline but that makes things even more unreadable
--                                                 local ok, df
                                                head, start, ok, df = handler(head,start,dataset,sequence,lookupmatch,rlmode,skiphash,step)
                                                if df then
                                                    break
                                                elseif ok then
                                                 -- done = true
                                                    break
                                                elseif not start then
                                                    -- don't ask why ... shouldn't happen
                                                    break
                                                end
                                            end
                                        end
                                        if df then
-- print("restart 2",typ)
                                        elseif start then
                                            start = getnext(start)
                                        end
                                    else
                                        start = getnext(start)
                                    end
                                else
                                    start = getnext(start)
                                end
                            end
                        elseif char == false or id == glue_code then
                            -- a different font|state or glue (happens often)
                            start = getnext(start)
                        elseif id == disc_code then
                            if not discs or discs[start] == true then
                                if gpossing then
                                    start = kernrun(start,k_run_multiple,               font,attr,steps,nofsteps,dataset,sequence,rlmode,skiphash,handler)
                                elseif forcetestrun then
                                    start = testrun(start,t_run_multiple,c_run_multiple,font,attr,steps,nofsteps,dataset,sequence,rlmode,skiphash,handler)
                                else
                                    start = comprun(start,c_run_multiple,               font,attr,steps,nofsteps,dataset,sequence,rlmode,skiphash,handler)
                                end
                            else
                                start = getnext(start)
                            end
                        elseif id == math_code then
                            start = getnext(endofmath(start))
                        elseif id == dir_code then
                            topstack, rlmode = txtdirstate(start,dirstack,topstack,rlparmode)
                            start = getnext(start)
                     -- elseif id == par_code and startofpar(start) then
                     --     rlparmode, rlmode = pardirstate(start)
                     --     start = getnext(start)
                        else
                            start = getnext(start)
                        end
                    end
                end
            end

            if trace_steps then -- ?
                registerstep(head)
            end

        end

        nesting = nesting - 1

     -- return head, done
        return head
    end

    -- This is not an official helper and used for tracing experiments. It can be changed as I like
    -- at any moment. At some point it might be used in a module that can help font development.

    function otf.datasetpositionprocessor(head,font,direction,dataset)

        currentfont     = font
        tfmdata         = fontdata[font]
        descriptions    = tfmdata.descriptions -- only needed in gref so we could pass node there instead
        characters      = tfmdata.characters   -- but this branch is not entered that often anyway
  local resources       = tfmdata.resources
        marks           = resources.marks
        classes         = resources.classes
        threshold,
        factor          = getthreshold(font)
        checkmarks      = tfmdata.properties.checkmarks

        if type(dataset) == "number" then
            dataset = otfdataset(tfmdata,font,0)[dataset]
        end

        local sequence  = dataset[3] -- sequences[s] -- also dataset[5]
        local typ       = sequence.type
     -- local gpossing  = typ == "gpos_single" or typ == "gpos_pair" -- store in dataset

     -- gpos_contextchain gpos_context

     -- if not gpossing then
     --     return head, false
     -- end

        local handler   = handlers[typ] -- store in dataset
        local steps     = sequence.steps
        local nofsteps  = sequence.nofsteps

        local done      = false
        local dirstack  = { nil } -- could move outside function but we can have local runs (maybe a few more nils)
        local start     = head
        local initialrl = (direction == righttoleft_code) and -1 or 0
        local rlmode    = initialrl
        local rlparmode = initialrl
        local topstack  = 0
        local merged    = steps.merged

     -- local matches   = false
        local position  = 0

        while start do
            local char, id = ischar(start,font)
            if char then
                position = position + 1
                local m = merged[char]
                if m then
                    if skiphash and skiphash[char] then -- we never needed it here but let's try
                        start = getnext(start)
                    else
                        for i=m[1],m[2] do
                            local step = steps[i]
                            local lookupcache = step.coverage
                            local lookupmatch = lookupcache[char]
                            if lookupmatch then
                                local ok
                                head, start, ok = handler(head,start,dataset,sequence,lookupmatch,rlmode,skiphash,step)
                                if ok then
                                 -- if matches then
                                 --     matches[position] = i
                                 -- else
                                 --     matches = { [position] = i }
                                 -- end
                                    break
                                elseif not start then
                                    break
                                end
                            end
                        end
                        if start then
                            start = getnext(start)
                        end
                    end
                else
                    start = getnext(start)
                end
            elseif char == false or id == glue_code then
                -- a different font|state or glue (happens often)
                start = getnext(start)
            elseif id == math_code then
                start = getnext(endofmath(start))
            elseif id == dir_code then
                topstack, rlmode = txtdirstate(start,dirstack,topstack,rlparmode)
                start = getnext(start)
         -- elseif id == par_code and startofpar(start) then
         --     rlparmode, rlmode = pardirstate(start)
         --     start = nxt
            else
                start = getnext(start)
            end
        end

        return head
    end

    -- end of experiment

end

-- so far

do

    local plugins = { }
    otf.plugins   = plugins

    local report  = logs.reporter("fonts")
    local warned  = false
    local okay    = { text = true }

    function otf.registerplugin(name,f)
        if type(name) == "string" and type(f) == "function" then
            plugins[name] = { name, f }
            if okay[name] then
                -- no warning (e.g. the diagnostic text plugin)
            else
                report("plugin %a has been loaded, please be aware of possible side effects",name)
                if not warned then
                    if logs.pushtarget then
                        logs.pushtarget("log")
                    end
                    report("Plugins are not officially supported unless stated otherwise. This is because")
                    report("they bypass the regular font handling and therefore some features in ConTeXt")
                    report("(especially those related to fonts) might not work as expected or might not work")
                    report("at all. Some plugins are for testing and development only and might change")
                    report("whenever we feel the need for it.")
                    report()
                    if logs.poptarget then
                        logs.poptarget()
                    end
                    warned = true
                end
            end
        end
    end

    function otf.plugininitializer(tfmdata,value)
        if type(value) == "string" then
            tfmdata.shared.plugin = plugins[value]
        end
    end

    function otf.pluginprocessor(head,font,dynamic,direction) -- n
        local s = fontdata[font].shared
        local p = s and s.plugin
        if p then
            if trace_plugins then
                report_process("applying plugin %a",p[1])
            end
            return p[2](head,font,dynamic,direction)
        else
            return head, false
        end
    end

end

function otf.featuresinitializer(tfmdata,value)
    -- nothing done here any more
end

registerotffeature {
    name         = "features",
    description  = "features",
    default      = true,
    initializers = {
        position = 1,
        node     = otf.featuresinitializer,
        plug     = otf.plugininitializer,
    },
    processors   = {
        node     = otf.featuresprocessor,
        plug     = otf.pluginprocessor,
    }
}

-- Moved here (up) a bit. This doesn't really belong in generic so it will move to a
-- context module some day.

local function markinitializer(tfmdata,value)
    local properties = tfmdata.properties
    properties.checkmarks = value
end

registerotffeature {
    name         = "checkmarks",
    description  = "check mark widths",
    default      = true,
    initializers = {
        node     = markinitializer,
    },
}

-- This can be used for extra handlers, but should be used with care! We implement one
-- here but some more can be found in the osd (script devanagary) file. Now watch out:
-- when a handler has steps, it is called as the other ones, but when we have no steps,
-- we use a different call:
--
--   function(head,dataset,sequence,initialrl,font,attr)
--       return head, done
--   end
--
-- Also see (!!).

otf.handlers = handlers

if context then
    return
else
    -- todo: move the following code someplace else
end

local setspacekerns = nodes.injections.setspacekerns if not setspacekerns then os.exit() end

local tag = "kern"

-- if fontfeatures then

--     function handlers.trigger_space_kerns(head,dataset,sequence,initialrl,font,attr)
--         local features = fontfeatures[font]
--         local enabled  = features and features.spacekern and features[tag]
--         if enabled then
--             setspacekerns(font,sequence)
--         end
--         return head, enabled
--     end

-- else -- generic (no hashes)

    function handlers.trigger_space_kerns(head,dataset,sequence,initialrl,font,attr)
        local shared   = fontdata[font].shared
        local features = shared and shared.features
        local enabled  = features and features.spacekern and features[tag]
        if enabled then
            setspacekerns(font,sequence)
        end
        return head, enabled
    end

-- end

-- There are fonts out there that change the space but we don't do that kind of
-- things in TeX.

local function hasspacekerns(data)
    local resources = data.resources
    local sequences = resources.sequences
    local validgpos = resources.features.gpos
    if validgpos and sequences then
        for i=1,#sequences do
            local sequence = sequences[i]
            local steps    = sequence.steps
            if steps and sequence.features[tag] then
                local kind = sequence.type
                if kind == "gpos_pair" or kind == "gpos_single" then
                    for i=1,#steps do
                        local step     = steps[i]
                        local coverage = step.coverage
                        local rules    = step.rules
                        if rules then
                            -- not now: analyze (simple) rules
                        elseif not coverage then
                            -- nothing to do
                        elseif kind == "gpos_single" then
                            -- maybe a message that we ignore
                        elseif kind == "gpos_pair" then
                            local format = step.format
                            if format == "move" or format == "kern" then
                                local kerns  = coverage[32]
                                if kerns then
                                    return true
                                end
                                for k, v in next, coverage do
                                    if v[32] then
                                        return true
                                    end
                                end
                            elseif format == "pair" then
                                local kerns  = coverage[32]
                                if kerns then
                                    for k, v in next, kerns do
                                        local one = v[1]
                                        if one and one ~= true then
                                            return true
                                        end
                                    end
                                end
                                for k, v in next, coverage do
                                    local kern = v[32]
                                    if kern then
                                        local one = kern[1]
                                        if one and one ~= true then
                                            return true
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
        end
    end
    return false
end

otf.readers.registerextender {
    name   = "spacekerns",
    action = function(data)
        data.properties.hasspacekerns = hasspacekerns(data)
    end
}

local function spaceinitializer(tfmdata,value) -- attr
    local resources  = tfmdata.resources
    local spacekerns = resources and resources.spacekerns
    if value and spacekerns == nil then
        local rawdata    = tfmdata.shared and tfmdata.shared.rawdata
        local properties = rawdata.properties
        if properties and properties.hasspacekerns then
            local sequences = resources.sequences
            local validgpos = resources.features.gpos
            if validgpos and sequences then
                local left  = { }
                local right = { }
                local last  = 0
                local feat  = nil
                for i=1,#sequences do
                    local sequence = sequences[i]
                    local steps    = sequence.steps
                    if steps then
                        -- we don't support space kerns in other features
                        local kern = sequence.features[tag]
                        if kern then
                            local kind = sequence.type
                            if kind == "gpos_pair" or kind == "gpos_single" then
                                if feat then
                                    for script, languages in next, kern do
                                        local f = feat[script]
                                        if f then
                                            for l in next, languages do
                                                f[l] = true
                                            end
                                        else
                                            feat[script] = languages
                                        end
                                    end
                                else
                                    feat = kern
                                end
                                for i=1,#steps do
                                    local step     = steps[i]
                                    local coverage = step.coverage
                                    local rules    = step.rules
                                    if rules then
                                        -- not now: analyze (simple) rules
                                    elseif not coverage then
                                        -- nothing to do
                                    elseif kind == "gpos_single" then
                                        -- makes no sense in TeX
                                    elseif kind == "gpos_pair" then
                                        local format = step.format
                                        if format == "move" or format == "kern" then
                                            local kerns  = coverage[32]
                                            if kerns then
                                                for k, v in next, kerns do
                                                    right[k] = v
                                                end
                                            end
                                            for k, v in next, coverage do
                                                local kern = v[32]
                                                if kern then
                                                    left[k] = kern
                                                end
                                            end
                                        elseif format == "pair" then
                                            local kerns  = coverage[32]
                                            if kerns then
                                                for k, v in next, kerns do
                                                    local one = v[1]
                                                    if one and one ~= true then
                                                        right[k] = one[3]
                                                    end
                                                end
                                            end
                                            for k, v in next, coverage do
                                                local kern = v[32]
                                                if kern then
                                                    local one = kern[1]
                                                    if one and one ~= true then
                                                        left[k] = one[3]
                                                    end
                                                end
                                            end
                                        end
                                    end
                                end
                                last = i
                            end
                        else
                            -- no steps ... needed for old one ... we could use the basekerns
                            -- instead
                        end
                    end
                end
                left  = next(left)  and left  or false
                right = next(right) and right or false
                if left or right then
                    spacekerns = {
                        left  = left,
                        right = right,
                    }
                    if last > 0 then
                        local triggersequence = {
                            -- no steps, see (!!)
                            features = { [tag] = feat or { dflt = { dflt = true, } } },
                            flags    = noflags,
                            name     = "trigger_space_kerns",
                            order    = { tag },
                            type     = "trigger_space_kerns",
                            left     = left,
                            right    = right,
                        }
                        insert(sequences,last,triggersequence)
                    end
                end
            end
        end
        resources.spacekerns = spacekerns
    end
    return spacekerns
end

registerotffeature {
    name         = "spacekern",
    description  = "space kern injection",
    default      = true,
    initializers = {
        node     = spaceinitializer,
    },
}