oxideav-ttf 0.1.2

Pure-Rust TrueType font parser for the oxideav framework — sfnt + cmap + glyf + hmtx + GSUB ligatures + GPOS kerning
Documentation
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
//! `GSUB` — Glyph Substitution Table.
//!
//! Implemented lookup types:
//! - **LookupType 1** (Single Substitution) — formats 1 (delta) and 2
//!   (indexed substitute array). Used by Arabic shaping (`init`/`medi`/
//!   `fina`/`isol`), small-caps, vertical alternates, and most other
//!   one-in/one-out feature lookups.
//! - **LookupType 2** (Multiple Substitution) — format 1. Splits one
//!   input glyph into a sequence of N replacement glyphs (`Sequence`
//!   record per coverage index). Used for some script normalisations
//!   (e.g. expanding a precomposed glyph into its base + mark cluster).
//! - **LookupType 3** (Alternate Substitution) — format 1. Each
//!   covered input glyph carries an `AlternateSet` of substitute
//!   glyphs; the caller picks an index (default 0). Used by `aalt` /
//!   `salt` for stylistic alternates.
//! - **LookupType 4** (Ligature Substitution) — format 1. Both the
//!   "walk every lookup" entry point ([`GsubTable::lookup_ligature`])
//!   and the lookup-index-specific entry point
//!   ([`GsubTable::apply_lookup_type_4`]) are exposed; the latter is
//!   how a feature-driven shaper dispatches `liga` / `rlig` / `dlig`.
//! - **LookupType 5** (Contextual Substitution) — formats 1 (glyph
//!   sequence), 2 (class-based) and 3 (coverage-based). Predecessor of
//!   LookupType 6 minus backtrack/lookahead — the input window is the
//!   only context. Older fonts encode contextual rules here.
//! - **LookupType 6** (Chained Contexts Substitution) — formats 1
//!   (glyph sequence), 2 (class-based) and 3 (coverage-based). Each
//!   match runs the referenced sub-lookups (typically LookupType 1 or
//!   LookupType 4) at the recorded sequence positions and returns the
//!   rewritten glyph run via [`GsubTable::apply_lookup_type_6`].
//! - **LookupType 8** (Reverse Chained Context Substitution) — format
//!   1. A single-glyph substitution under
//!      `(backtrack, input_coverage, lookahead)` context, processed in
//!      reverse text order. Used by some Arabic fonts for isolated forms.
//!
//! ExtensionSubst (LookupType 7) is unwrapped transparently for every
//! lookup type above.
//!
//! In addition to the per-lookup walkers, this module decodes the
//! **ScriptList** + **FeatureList** at parse time so callers can ask
//! "which lookup indices implement feature `init` for script `arab`?"
//! via [`Font::gsub_features_for_script`]. The lookup-index list is
//! the bridge between feature tags (what a shaper asks for) and the
//! per-lookup substitution machinery (what changes the glyph stream).
//!
//! Spec: Microsoft OpenType §"GSUB — Glyph Substitution Table",
//! §"Common Table Formats" (ScriptList / FeatureList / LookupList),
//! Apple TrueType Reference §"GSUB", ISO/IEC 14496-22 §6 (OFF).

use crate::parser::{read_u16, read_u32};
use crate::tables::gdef::{class_def_lookup, coverage_lookup, lookup_table_slice};
use crate::Error;

const LOOKUP_SINGLE_SUBST: u16 = 1;
const LOOKUP_MULTIPLE_SUBST: u16 = 2;
const LOOKUP_ALTERNATE_SUBST: u16 = 3;
const LOOKUP_LIGATURE_SUBST: u16 = 4;
const LOOKUP_CONTEXT_SUBST: u16 = 5;
const LOOKUP_CHAIN_CONTEXT_SUBST: u16 = 6;
const LOOKUP_EXTENSION_SUBST: u16 = 7;
const LOOKUP_REVERSE_CHAIN_CONTEXT_SUBST: u16 = 8;

/// Maximum recursion depth for nested chained-context substitutions.
/// Prevents pathological self-referential lookup graphs from blowing
/// the stack — the spec doesn't bound this so we set a conservative
/// fence (HarfBuzz uses 6).
const MAX_NESTED_LOOKUP_DEPTH: u8 = 8;

/// One feature record from the GSUB FeatureList, resolved to the
/// list of lookup indices that implement it. Returned by
/// [`super::super::Font::gsub_features_for_script`] in the order the
/// active LangSys lists its features.
///
/// The `tag` field is a four-byte ASCII feature identifier such as
/// `*b"init"`, `*b"medi"`, `*b"fina"`, `*b"isol"`, `*b"liga"`,
/// `*b"smcp"` — the OpenType registered-feature catalogue.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GsubFeature {
    pub tag: [u8; 4],
    pub lookup_indices: Vec<u16>,
}

#[derive(Debug, Clone)]
pub struct GsubTable<'a> {
    bytes: &'a [u8],
    script_list_off: u32,
    feature_list_off: u32,
    lookup_list_off: u32,
}

impl<'a> GsubTable<'a> {
    pub fn parse(bytes: &'a [u8]) -> Result<Self, Error> {
        if bytes.len() < 10 {
            return Err(Error::UnexpectedEof);
        }
        let major = read_u16(bytes, 0)?;
        if major != 1 {
            return Err(Error::BadStructure("GSUB: unsupported major version"));
        }
        // u16 minor at +2 (we tolerate 0 or 1)
        // Offset16 scriptList at +4
        // Offset16 featureList at +6
        // Offset16 lookupList at +8
        let script_list_off = read_u16(bytes, 4)? as u32;
        let feature_list_off = read_u16(bytes, 6)? as u32;
        let lookup_list_off = read_u16(bytes, 8)? as u32;
        // Each offset must either be 0 (table absent) or fit inside `bytes`.
        for off in [script_list_off, feature_list_off, lookup_list_off] {
            if off != 0 && off as usize >= bytes.len() {
                return Err(Error::BadOffset);
            }
        }
        Ok(Self {
            bytes,
            script_list_off,
            feature_list_off,
            lookup_list_off,
        })
    }

    /// Return all features active for `script_tag` under `lang_tag`.
    ///
    /// `lang_tag = None` → use the script's `DefaultLangSys`. If
    /// `lang_tag` is supplied but isn't enumerated for the script, we
    /// fall back to `DefaultLangSys`. If neither resolves the script
    /// at all (or the table has no ScriptList) the returned `Vec` is
    /// empty.
    ///
    /// The order of returned features matches the LangSys's
    /// `featureIndices` array order (which is the order a shaper
    /// should apply them). Each `GsubFeature` carries the resolved
    /// lookup-index list ready for [`Self::apply_lookup_type_1`].
    pub fn features_for_script(
        &self,
        script_tag: [u8; 4],
        lang_tag: Option<[u8; 4]>,
    ) -> Vec<GsubFeature> {
        let mut out = Vec::new();
        if self.script_list_off == 0 || self.feature_list_off == 0 {
            return out;
        }
        let script_list = match self.bytes.get(self.script_list_off as usize..) {
            Some(s) => s,
            None => return out,
        };
        let feature_list = match self.bytes.get(self.feature_list_off as usize..) {
            Some(s) => s,
            None => return out,
        };

        // ScriptList layout: u16 scriptCount; ScriptRecord{ Tag tag,
        // Offset16 scriptOffset } scriptRecords[scriptCount];
        // Each scriptOffset is RELATIVE to the ScriptList start.
        let script_count = match read_u16(script_list, 0) {
            Ok(v) => v as usize,
            Err(_) => return out,
        };
        if script_list.len() < 2 + script_count * 6 {
            return out;
        }
        let mut script_off: Option<usize> = None;
        for i in 0..script_count {
            let r = 2 + i * 6;
            let tag = [
                script_list[r],
                script_list[r + 1],
                script_list[r + 2],
                script_list[r + 3],
            ];
            if tag == script_tag {
                let o = match read_u16(script_list, r + 4) {
                    Ok(v) => v as usize,
                    Err(_) => return out,
                };
                script_off = Some(o);
                break;
            }
        }
        let script_off = match script_off {
            Some(o) => o,
            None => return out,
        };
        let script = match script_list.get(script_off..) {
            Some(s) => s,
            None => return out,
        };

        // Script layout:
        //   Offset16 defaultLangSysOffset    (0 if absent)
        //   u16      langSysCount
        //   LangSysRecord{ Tag tag, Offset16 langSysOffset }
        //                                    langSysRecords[langSysCount];
        // langSysOffsets are relative to the Script table start.
        if script.len() < 4 {
            return out;
        }
        let default_off = match read_u16(script, 0) {
            Ok(v) => v as usize,
            Err(_) => return out,
        };
        let lang_count = match read_u16(script, 2) {
            Ok(v) => v as usize,
            Err(_) => return out,
        };
        let mut chosen_off: Option<usize> = None;
        if let Some(want) = lang_tag {
            // Linear scan — there's only ever a handful of LangSysRecords.
            for i in 0..lang_count {
                let r = 4 + i * 6;
                if script.len() < r + 6 {
                    break;
                }
                let tag = [script[r], script[r + 1], script[r + 2], script[r + 3]];
                if tag == want {
                    chosen_off = match read_u16(script, r + 4) {
                        Ok(v) => Some(v as usize),
                        Err(_) => None,
                    };
                    break;
                }
            }
        }
        let chosen_off = chosen_off.or(if default_off == 0 {
            None
        } else {
            Some(default_off)
        });
        let chosen_off = match chosen_off {
            Some(o) if o != 0 => o,
            _ => return out,
        };
        let langsys = match script.get(chosen_off..) {
            Some(s) => s,
            None => return out,
        };

        // LangSys layout:
        //   Offset16 lookupOrderOffset (= 0 reserved)
        //   u16      requiredFeatureIndex (0xFFFF = none)
        //   u16      featureIndexCount
        //   u16      featureIndices[featureIndexCount]
        if langsys.len() < 6 {
            return out;
        }
        let required = match read_u16(langsys, 2) {
            Ok(v) => v,
            Err(_) => return out,
        };
        let feat_count = match read_u16(langsys, 4) {
            Ok(v) => v as usize,
            Err(_) => return out,
        };
        if langsys.len() < 6 + feat_count * 2 {
            return out;
        }

        // FeatureList layout:
        //   u16 featureCount;
        //   FeatureRecord{ Tag tag, Offset16 featureOffset } records[];
        // featureOffsets are relative to the FeatureList start.
        let total_features = match read_u16(feature_list, 0) {
            Ok(v) => v as usize,
            Err(_) => return out,
        };

        // Helper to resolve one feature index → GsubFeature.
        let push_feature = |fi: u16, into: &mut Vec<GsubFeature>| {
            if (fi as usize) >= total_features {
                return;
            }
            let r = 2 + fi as usize * 6;
            if feature_list.len() < r + 6 {
                return;
            }
            let tag = [
                feature_list[r],
                feature_list[r + 1],
                feature_list[r + 2],
                feature_list[r + 3],
            ];
            let foff = match read_u16(feature_list, r + 4) {
                Ok(v) => v as usize,
                Err(_) => return,
            };
            let feature = match feature_list.get(foff..) {
                Some(s) => s,
                None => return,
            };
            // Feature layout: Offset16 featureParamsOffset; u16
            // lookupIndexCount; u16 lookupListIndices[count].
            if feature.len() < 4 {
                return;
            }
            let count = match read_u16(feature, 2) {
                Ok(v) => v as usize,
                Err(_) => return,
            };
            if feature.len() < 4 + count * 2 {
                return;
            }
            let mut idxs = Vec::with_capacity(count);
            for i in 0..count {
                if let Ok(v) = read_u16(feature, 4 + i * 2) {
                    idxs.push(v);
                }
            }
            into.push(GsubFeature {
                tag,
                lookup_indices: idxs,
            });
        };

        if required != 0xFFFF {
            push_feature(required, &mut out);
        }
        for i in 0..feat_count {
            let fi = match read_u16(langsys, 6 + i * 2) {
                Ok(v) => v,
                Err(_) => continue,
            };
            push_feature(fi, &mut out);
        }
        out
    }

    /// Apply GSUB LookupType 1 (Single Substitution) lookup `lookup_index`
    /// to `gid`. Returns `Some(replacement_gid)` when the lookup's
    /// coverage covers `gid`, or `None` when no substitution applies
    /// (caller keeps the input glyph unchanged).
    ///
    /// Walks every sub-table in the lookup; the first hit (per spec
    /// "first matching subtable in lookup order") wins. ExtensionSubst
    /// (LookupType 7) wrappers are unwrapped transparently.
    pub fn apply_lookup_type_1(&self, lookup_index: u16, gid: u16) -> Option<u16> {
        if self.lookup_list_off == 0 {
            return None;
        }
        let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, lookup_index)?;
        if lookup.len() < 6 {
            return None;
        }
        let kind = read_u16(lookup, 0).ok()?;
        let sub_count = read_u16(lookup, 4).ok()? as usize;
        for s in 0..sub_count {
            let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
            let sub = lookup.get(sub_off..)?;
            let (effective_kind, effective_sub) = if kind == LOOKUP_EXTENSION_SUBST {
                if sub.len() < 8 {
                    continue;
                }
                let ext_type = read_u16(sub, 2).ok()?;
                let ext_off = read_u32(sub, 4).ok()? as usize;
                let ext = match sub.get(ext_off..) {
                    Some(s) => s,
                    None => continue,
                };
                (ext_type, ext)
            } else {
                (kind, sub)
            };
            if effective_kind != LOOKUP_SINGLE_SUBST {
                continue;
            }
            if let Some(hit) = single_subst_lookup(effective_sub, gid) {
                return Some(hit);
            }
        }
        None
    }

    /// Apply GSUB LookupType 4 (Ligature Substitution) lookup
    /// `lookup_index` to a prefix of `glyphs`.
    ///
    /// Returns `Some((replacement_gid, consumed))` when one of the
    /// lookup's sub-tables matches a prefix of `glyphs` of length
    /// `consumed >= 1` (in practice always `>= 2` for real ligatures —
    /// `componentCount = 1` would be a no-op single substitution and
    /// is allowed but vanishingly rare). Returns `None` when no
    /// ligature applies. ExtensionSubst (LookupType 7) wrappers are
    /// unwrapped transparently.
    ///
    /// This is the lookup-index-specific counterpart of
    /// [`Self::lookup_ligature`] and is what a feature-driven shaper
    /// calls after resolving the `liga` / `rlig` / `dlig` feature for
    /// the active script via [`Self::features_for_script`].
    pub fn apply_lookup_type_4(&self, lookup_index: u16, glyphs: &[u16]) -> Option<(u16, usize)> {
        if glyphs.is_empty() || self.lookup_list_off == 0 {
            return None;
        }
        let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, lookup_index)?;
        if lookup.len() < 6 {
            return None;
        }
        let kind = read_u16(lookup, 0).ok()?;
        let sub_count = read_u16(lookup, 4).ok()? as usize;
        for s in 0..sub_count {
            let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
            let sub = lookup.get(sub_off..)?;
            let (effective_kind, effective_sub) = if kind == LOOKUP_EXTENSION_SUBST {
                if sub.len() < 8 {
                    continue;
                }
                let ext_type = read_u16(sub, 2).ok()?;
                let ext_off = read_u32(sub, 4).ok()? as usize;
                let ext = match sub.get(ext_off..) {
                    Some(s) => s,
                    None => continue,
                };
                (ext_type, ext)
            } else {
                (kind, sub)
            };
            if effective_kind != LOOKUP_LIGATURE_SUBST {
                continue;
            }
            if let Some(hit) = ligature_subst_lookup(effective_sub, glyphs) {
                return Some(hit);
            }
        }
        None
    }

    /// Apply GSUB LookupType 6 (Chained Contexts Substitution) lookup
    /// `lookup_index` to the glyph run starting at `pos`.
    ///
    /// Returns `Some(rewritten)` — a fresh `Vec<u16>` containing the
    /// full run (`gids[..pos]` unchanged followed by the substituted
    /// tail) — when one of the lookup's sub-tables matches the
    /// `(backtrack, input, lookahead)` window around `pos`. Returns
    /// `None` when no chained-context rule applies.
    ///
    /// All three sub-table formats are supported:
    ///
    /// - **Format 1** — Coverage on the first input glyph + per-coverage
    ///   ChainSubRuleSet of explicit `(backtrack, input, lookahead)`
    ///   glyph sequences plus per-rule `SubstLookupRecord[]`.
    /// - **Format 2** — Coverage on the first input glyph + three
    ///   ClassDefs (backtrack/input/lookahead) + per-input-class
    ///   ChainSubClassSet whose rules are class sequences instead of
    ///   glyph sequences.
    /// - **Format 3** — three independent Coverage[] arrays
    ///   (backtrack / input / lookahead) + a single
    ///   `SubstLookupRecord[]`.
    ///
    /// Each `SubstLookupRecord { sequenceIndex, lookupListIndex }` is
    /// recursively dispatched: LookupType 1 substitutes one glyph,
    /// LookupType 4 substitutes `componentCount` glyphs starting at
    /// the relative `sequenceIndex`. ExtensionSubst (LookupType 7)
    /// wrappers are unwrapped transparently. Recursive sub-lookup
    /// expansion is bounded by `MAX_NESTED_LOOKUP_DEPTH` (8) to
    /// defuse pathological self-referential lookup graphs.
    pub fn apply_lookup_type_6(
        &self,
        lookup_index: u16,
        gids: &[u16],
        pos: usize,
    ) -> Option<Vec<u16>> {
        self.apply_chain_context_at(lookup_index, gids, pos, 0)
    }

    /// Apply GSUB LookupType 2 (Multiple Substitution) lookup
    /// `lookup_index` to a single input glyph `gid`.
    ///
    /// Multiple-substitution rules expand one glyph into a sequence of
    /// glyphs (the inverse of a ligature). Returns
    /// `Some(substitute_sequence)` — the `Sequence` record's glyph IDs
    /// — when the lookup's coverage covers `gid`, or `None` when no
    /// rule applies. ExtensionSubst (LookupType 7) wrappers are
    /// unwrapped transparently.
    ///
    /// The OpenType spec allows `glyphCount = 0` (deletion); we surface
    /// such entries as `Some(Vec::new())`. Per the spec the same
    /// `Sequence` is shared by every coverage index when only one is
    /// listed, but each coverage index can have its own.
    pub fn apply_lookup_type_2(&self, lookup_index: u16, gid: u16) -> Option<Vec<u16>> {
        if self.lookup_list_off == 0 {
            return None;
        }
        let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, lookup_index)?;
        if lookup.len() < 6 {
            return None;
        }
        let kind = read_u16(lookup, 0).ok()?;
        let sub_count = read_u16(lookup, 4).ok()? as usize;
        for s in 0..sub_count {
            let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
            let sub = lookup.get(sub_off..)?;
            let (effective_kind, effective_sub) = if kind == LOOKUP_EXTENSION_SUBST {
                if sub.len() < 8 {
                    continue;
                }
                let ext_type = read_u16(sub, 2).ok()?;
                let ext_off = read_u32(sub, 4).ok()? as usize;
                let ext = match sub.get(ext_off..) {
                    Some(s) => s,
                    None => continue,
                };
                (ext_type, ext)
            } else {
                (kind, sub)
            };
            if effective_kind != LOOKUP_MULTIPLE_SUBST {
                continue;
            }
            if let Some(hit) = multiple_subst_lookup(effective_sub, gid) {
                return Some(hit);
            }
        }
        None
    }

    /// Apply GSUB LookupType 3 (Alternate Substitution) lookup
    /// `lookup_index` to a single input glyph `gid`, picking
    /// `alternate_index` from the resolved `AlternateSet`.
    ///
    /// Returns `Some(replacement_gid)` when the lookup's coverage
    /// covers `gid` AND `alternate_index` is in range for that
    /// coverage's `AlternateSet`. Returns `None` on coverage miss,
    /// out-of-range alternate index, or non-alternate-substitution
    /// referenced lookup. ExtensionSubst (LookupType 7) wrappers are
    /// unwrapped transparently.
    ///
    /// Default callers should pass `alternate_index = 0` — the spec
    /// does not register a per-feature variant index, so picking the
    /// first alternate is the conventional `aalt` / `salt` default.
    /// Stylistic-set features like `ss01..ss20` typically encode their
    /// choice as a separate single-substitution lookup instead.
    pub fn apply_lookup_type_3(
        &self,
        lookup_index: u16,
        gid: u16,
        alternate_index: u16,
    ) -> Option<u16> {
        if self.lookup_list_off == 0 {
            return None;
        }
        let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, lookup_index)?;
        if lookup.len() < 6 {
            return None;
        }
        let kind = read_u16(lookup, 0).ok()?;
        let sub_count = read_u16(lookup, 4).ok()? as usize;
        for s in 0..sub_count {
            let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
            let sub = lookup.get(sub_off..)?;
            let (effective_kind, effective_sub) = if kind == LOOKUP_EXTENSION_SUBST {
                if sub.len() < 8 {
                    continue;
                }
                let ext_type = read_u16(sub, 2).ok()?;
                let ext_off = read_u32(sub, 4).ok()? as usize;
                let ext = match sub.get(ext_off..) {
                    Some(s) => s,
                    None => continue,
                };
                (ext_type, ext)
            } else {
                (kind, sub)
            };
            if effective_kind != LOOKUP_ALTERNATE_SUBST {
                continue;
            }
            if let Some(hit) = alternate_subst_lookup(effective_sub, gid, alternate_index) {
                return Some(hit);
            }
        }
        None
    }

    /// Apply GSUB LookupType 5 (Contextual Substitution) lookup
    /// `lookup_index` to the glyph run starting at `pos`.
    ///
    /// LookupType 5 is the predecessor of LookupType 6 minus the
    /// backtrack and lookahead arrays — the input window IS the
    /// context. All three sub-table formats are decoded:
    ///
    /// - **Format 1** — Coverage on the first input glyph + per-coverage
    ///   `SubRuleSet` of explicit input glyph sequences plus per-rule
    ///   `SubstLookupRecord[]`.
    /// - **Format 2** — Coverage on the first input glyph + a single
    ///   `ClassDef` + per-input-class `SubClassSet` whose rules are
    ///   class sequences instead of glyph sequences.
    /// - **Format 3** — `Coverage[]` array (one per input position) +
    ///   a single `SubstLookupRecord[]`.
    ///
    /// Each match's `SubstLookupRecord` is dispatched the same way as
    /// LookupType 6's records (LookupType 1 / 4 / 5 / 6 nested, bounded
    /// recursion). ExtensionSubst (LookupType 7) wrappers are unwrapped
    /// transparently. Returns `Some(rewritten_run)` on a match or
    /// `None` when no contextual rule fires.
    pub fn apply_lookup_type_5(
        &self,
        lookup_index: u16,
        gids: &[u16],
        pos: usize,
    ) -> Option<Vec<u16>> {
        self.apply_context_at(lookup_index, gids, pos, 0)
    }

    /// Apply GSUB LookupType 8 (Reverse Chained Context Substitution)
    /// lookup `lookup_index` to the glyph at `gids[pos]`.
    ///
    /// LookupType 8 has only Format 1: coverage on the input glyph,
    /// plus backtrack and lookahead `Coverage[]` arrays, plus a
    /// `substituteGlyphIDs[]` array indexed by the input coverage
    /// index. Unlike LookupType 6, the substitution is single-glyph
    /// (no `SubstLookupRecord[]`) and the spec mandates reverse-text
    /// processing of the input run — a higher-level shaper is what
    /// honours that ordering; this entry point answers "does this rule
    /// fire at `pos`?".
    ///
    /// Returns `Some(replacement_gid)` when the input coverage covers
    /// `gids[pos]` AND every backtrack / lookahead coverage matches
    /// the surrounding glyphs. Returns `None` otherwise.
    /// ExtensionSubst (LookupType 7) wrappers are unwrapped
    /// transparently.
    pub fn apply_lookup_type_8(&self, lookup_index: u16, gids: &[u16], pos: usize) -> Option<u16> {
        if self.lookup_list_off == 0 || pos >= gids.len() {
            return None;
        }
        let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, lookup_index)?;
        if lookup.len() < 6 {
            return None;
        }
        let kind = read_u16(lookup, 0).ok()?;
        let sub_count = read_u16(lookup, 4).ok()? as usize;
        for s in 0..sub_count {
            let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
            let sub = lookup.get(sub_off..)?;
            let (effective_kind, effective_sub) = if kind == LOOKUP_EXTENSION_SUBST {
                if sub.len() < 8 {
                    continue;
                }
                let ext_type = read_u16(sub, 2).ok()?;
                let ext_off = read_u32(sub, 4).ok()? as usize;
                let ext = match sub.get(ext_off..) {
                    Some(s) => s,
                    None => continue,
                };
                (ext_type, ext)
            } else {
                (kind, sub)
            };
            if effective_kind != LOOKUP_REVERSE_CHAIN_CONTEXT_SUBST {
                continue;
            }
            if let Some(hit) = reverse_chain_context_lookup(effective_sub, gids, pos) {
                return Some(hit);
            }
        }
        None
    }

    fn apply_context_at(
        &self,
        lookup_index: u16,
        gids: &[u16],
        pos: usize,
        depth: u8,
    ) -> Option<Vec<u16>> {
        if depth >= MAX_NESTED_LOOKUP_DEPTH {
            return None;
        }
        if pos >= gids.len() || self.lookup_list_off == 0 {
            return None;
        }
        let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, lookup_index)?;
        if lookup.len() < 6 {
            return None;
        }
        let kind = read_u16(lookup, 0).ok()?;
        let sub_count = read_u16(lookup, 4).ok()? as usize;
        for s in 0..sub_count {
            let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
            let sub = lookup.get(sub_off..)?;
            let (effective_kind, effective_sub) = if kind == LOOKUP_EXTENSION_SUBST {
                if sub.len() < 8 {
                    continue;
                }
                let ext_type = read_u16(sub, 2).ok()?;
                let ext_off = read_u32(sub, 4).ok()? as usize;
                let ext = match sub.get(ext_off..) {
                    Some(s) => s,
                    None => continue,
                };
                (ext_type, ext)
            } else {
                (kind, sub)
            };
            if effective_kind != LOOKUP_CONTEXT_SUBST {
                continue;
            }
            if effective_sub.len() < 2 {
                continue;
            }
            let format = match read_u16(effective_sub, 0) {
                Ok(v) => v,
                Err(_) => continue,
            };
            let matched = match format {
                1 => context_format1_match(effective_sub, gids, pos),
                2 => context_format2_match(effective_sub, gids, pos),
                3 => context_format3_match(effective_sub, gids, pos),
                _ => None,
            };
            if let Some(m) = matched {
                return self.apply_subst_records(gids, pos, &m, depth);
            }
        }
        None
    }

    fn apply_chain_context_at(
        &self,
        lookup_index: u16,
        gids: &[u16],
        pos: usize,
        depth: u8,
    ) -> Option<Vec<u16>> {
        if depth >= MAX_NESTED_LOOKUP_DEPTH {
            return None;
        }
        if pos >= gids.len() || self.lookup_list_off == 0 {
            return None;
        }
        let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, lookup_index)?;
        if lookup.len() < 6 {
            return None;
        }
        let kind = read_u16(lookup, 0).ok()?;
        let sub_count = read_u16(lookup, 4).ok()? as usize;
        for s in 0..sub_count {
            let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
            let sub = lookup.get(sub_off..)?;
            let (effective_kind, effective_sub) = if kind == LOOKUP_EXTENSION_SUBST {
                if sub.len() < 8 {
                    continue;
                }
                let ext_type = read_u16(sub, 2).ok()?;
                let ext_off = read_u32(sub, 4).ok()? as usize;
                let ext = match sub.get(ext_off..) {
                    Some(s) => s,
                    None => continue,
                };
                (ext_type, ext)
            } else {
                (kind, sub)
            };
            if effective_kind != LOOKUP_CHAIN_CONTEXT_SUBST {
                continue;
            }
            if effective_sub.len() < 2 {
                continue;
            }
            let format = match read_u16(effective_sub, 0) {
                Ok(v) => v,
                Err(_) => continue,
            };
            let matched = match format {
                1 => chain_context_format1_match(effective_sub, gids, pos),
                2 => chain_context_format2_match(effective_sub, gids, pos),
                3 => chain_context_format3_match(effective_sub, gids, pos),
                _ => None,
            };
            if let Some(m) = matched {
                return self.apply_subst_records(gids, pos, &m, depth);
            }
        }
        None
    }

    /// Apply a chain-context match's `SubstLookupRecord[]` against the
    /// input run, returning the rewritten glyph slice.
    ///
    /// `m.input_len` glyphs at `gids[pos..pos + input_len]` are the
    /// chained-context "input" window. We walk the records in declared
    /// order, dispatching each (sequenceIndex, lookupListIndex) into
    /// the appropriate per-type apply path. Glyph indices in the input
    /// window are remapped as substitutions consume / replace glyphs.
    fn apply_subst_records(
        &self,
        gids: &[u16],
        pos: usize,
        m: &ChainMatch,
        depth: u8,
    ) -> Option<Vec<u16>> {
        // Working buffer: gids with the input window mutable.
        let mut out: Vec<u16> = gids.to_vec();
        // Track the current logical→physical remapping inside the input
        // window. Each entry maps a *logical* (pre-subst) sequenceIndex
        // to its current physical offset inside `out`. When a ligature
        // substitution consumes N input glyphs, the entries beyond it
        // collapse into a single physical position.
        let mut logical_to_phys: Vec<Option<usize>> =
            (0..m.input_len).map(|i| Some(pos + i)).collect();
        for rec in &m.records {
            let seq_idx = rec.sequence_index as usize;
            if seq_idx >= logical_to_phys.len() {
                continue;
            }
            let phys = match logical_to_phys[seq_idx] {
                Some(p) => p,
                None => continue,
            };
            // Resolve the referenced lookup type and dispatch.
            let lookup =
                match lookup_table_slice(self.bytes, self.lookup_list_off, rec.lookup_index) {
                    Some(s) => s,
                    None => continue,
                };
            if lookup.len() < 2 {
                continue;
            }
            let mut nested_kind = match read_u16(lookup, 0) {
                Ok(v) => v,
                Err(_) => continue,
            };
            // ExtensionSubst (LookupType 7) at the nested-lookup
            // top-level: peek at the first sub-table's effective type.
            if nested_kind == LOOKUP_EXTENSION_SUBST {
                if let Some(t) = peek_extension_type(lookup) {
                    nested_kind = t;
                }
            }
            match nested_kind {
                LOOKUP_SINGLE_SUBST => {
                    if let Some(replacement) = self.apply_lookup_type_1(rec.lookup_index, out[phys])
                    {
                        out[phys] = replacement;
                    }
                }
                LOOKUP_LIGATURE_SUBST => {
                    let tail = &out[phys..];
                    if let Some((lig_glyph, consumed)) =
                        self.apply_lookup_type_4(rec.lookup_index, tail)
                    {
                        if consumed >= 1 && phys + consumed <= out.len() {
                            // Replace `consumed` glyphs at `phys` with
                            // a single ligature glyph.
                            out.splice(phys..phys + consumed, std::iter::once(lig_glyph));
                            // Collapse the consumed logical indices
                            // and shift later positions left.
                            let removed = consumed - 1;
                            // Mark consumed logical slots None.
                            for slot in logical_to_phys.iter_mut().skip(seq_idx + 1) {
                                if let Some(p) = *slot {
                                    if p < phys + consumed {
                                        *slot = None;
                                    } else {
                                        *slot = Some(p - removed);
                                    }
                                }
                            }
                        }
                    }
                }
                LOOKUP_CHAIN_CONTEXT_SUBST => {
                    if let Some(rewritten) =
                        self.apply_chain_context_at(rec.lookup_index, &out, phys, depth + 1)
                    {
                        // Recompute remap based on length delta.
                        let old_len = out.len();
                        out = rewritten;
                        let delta = out.len() as isize - old_len as isize;
                        if delta != 0 {
                            for slot in logical_to_phys.iter_mut().skip(seq_idx + 1) {
                                if let Some(p) = *slot {
                                    let np = p as isize + delta;
                                    if np < phys as isize {
                                        *slot = None;
                                    } else {
                                        *slot = Some(np as usize);
                                    }
                                }
                            }
                        }
                    }
                }
                LOOKUP_CONTEXT_SUBST => {
                    if let Some(rewritten) =
                        self.apply_context_at(rec.lookup_index, &out, phys, depth + 1)
                    {
                        let old_len = out.len();
                        out = rewritten;
                        let delta = out.len() as isize - old_len as isize;
                        if delta != 0 {
                            for slot in logical_to_phys.iter_mut().skip(seq_idx + 1) {
                                if let Some(p) = *slot {
                                    let np = p as isize + delta;
                                    if np < phys as isize {
                                        *slot = None;
                                    } else {
                                        *slot = Some(np as usize);
                                    }
                                }
                            }
                        }
                    }
                }
                LOOKUP_MULTIPLE_SUBST => {
                    if let Some(seq) = self.apply_lookup_type_2(rec.lookup_index, out[phys]) {
                        let inserted = seq.len();
                        out.splice(phys..phys + 1, seq);
                        // Length delta = inserted - 1.
                        let delta = inserted as isize - 1;
                        if delta != 0 {
                            for slot in logical_to_phys.iter_mut().skip(seq_idx + 1) {
                                if let Some(p) = *slot {
                                    let np = p as isize + delta;
                                    if np < phys as isize {
                                        *slot = None;
                                    } else {
                                        *slot = Some(np as usize);
                                    }
                                }
                            }
                        }
                    }
                }
                LOOKUP_ALTERNATE_SUBST => {
                    // Default to alternate index 0 — the spec doesn't
                    // register a per-feature variant index for nested
                    // lookups.
                    if let Some(replacement) =
                        self.apply_lookup_type_3(rec.lookup_index, out[phys], 0)
                    {
                        out[phys] = replacement;
                    }
                }
                _ => {
                    // Other nested lookup types (8 reverse-chain) are
                    // not used as nested sub-lookups by the spec; skip
                    // silently rather than abort the whole record.
                }
            }
        }
        Some(out)
    }

    /// Look up a ligature substitution that matches a prefix of `glyphs`.
    /// Walks every lookup; returns the *first* hit by lookup order.
    pub fn lookup_ligature(&self, glyphs: &[u16]) -> Option<(u16, usize)> {
        if glyphs.is_empty() {
            return None;
        }
        let lookup_list = self.bytes.get(self.lookup_list_off as usize..)?;
        if lookup_list.len() < 2 {
            return None;
        }
        let lookup_count = read_u16(lookup_list, 0).ok()?;
        for i in 0..lookup_count {
            let lookup = match lookup_table_slice(self.bytes, self.lookup_list_off, i) {
                Some(s) => s,
                None => continue,
            };
            if lookup.len() < 6 {
                continue;
            }
            let kind = read_u16(lookup, 0).ok()?;
            // Subtable count + offsets.
            let sub_count = read_u16(lookup, 4).ok()? as usize;
            for s in 0..sub_count {
                let sub_off = match read_u16(lookup, 6 + s * 2) {
                    Ok(o) => o as usize,
                    Err(_) => continue,
                };
                let sub = match lookup.get(sub_off..) {
                    Some(b) => b,
                    None => continue,
                };
                let (effective_kind, effective_sub) = if kind == LOOKUP_EXTENSION_SUBST {
                    // ExtensionSubst format 1:
                    //   u16 format=1, u16 extensionLookupType, Offset32 extensionOffset
                    if sub.len() < 8 {
                        continue;
                    }
                    let ext_type = read_u16(sub, 2).ok().unwrap_or(0);
                    let ext_off = read_u32(sub, 4).ok().unwrap_or(0) as usize;
                    let ext = match sub.get(ext_off..) {
                        Some(s) => s,
                        None => continue,
                    };
                    (ext_type, ext)
                } else {
                    (kind, sub)
                };
                if effective_kind != LOOKUP_LIGATURE_SUBST {
                    continue;
                }
                if let Some(hit) = ligature_subst_lookup(effective_sub, glyphs) {
                    return Some(hit);
                }
            }
        }
        None
    }
}

/// Walk a SingleSubst sub-table looking for a substitution.
///
/// Two formats per the OpenType spec:
///
///   Format 1: u16 format=1, Offset16 coverageOffset, i16 deltaGlyphID
///     - if `gid` is in coverage, return `gid + delta` (mod 2^16).
///   Format 2: u16 format=2, Offset16 coverageOffset, u16 glyphCount,
///             u16 substituteGlyphIDs[glyphCount]
///     - if `gid` is at coverage_index `i`, return
///       `substituteGlyphIDs[i]`.
fn single_subst_lookup(sub: &[u8], gid: u16) -> Option<u16> {
    if sub.len() < 6 {
        return None;
    }
    let format = read_u16(sub, 0).ok()?;
    let coverage_off = read_u16(sub, 2).ok()? as usize;
    let coverage = sub.get(coverage_off..)?;
    let cov_idx = coverage_lookup(coverage, gid)?;
    match format {
        1 => {
            // delta is signed; spec adds modulo 65536.
            let delta = read_u16(sub, 4).ok()? as i16 as i32;
            let result = (gid as i32 + delta) & 0xFFFF;
            Some(result as u16)
        }
        2 => {
            // glyphCount at +4, substituteGlyphIDs starts at +6.
            let count = read_u16(sub, 4).ok()? as usize;
            if cov_idx as usize >= count {
                return None;
            }
            let off = 6 + cov_idx as usize * 2;
            if sub.len() < off + 2 {
                return None;
            }
            read_u16(sub, off).ok()
        }
        _ => None,
    }
}

/// Walk a LigatureSubstFormat1 sub-table looking for a match.
///
/// Layout:
///   u16 format == 1
///   Offset16 coverageOffset             // glyph[0] coverage
///   u16 ligatureSetCount
///   Offset16 ligatureSetOffsets[ligatureSetCount]
///   ...
///   LigatureSet { u16 ligatureCount; Offset16 ligatureOffsets[]; }
///   Ligature    { u16 ligGlyph; u16 componentCount;
///                 u16 componentGlyphIDs[componentCount - 1]; }
fn ligature_subst_lookup(sub: &[u8], glyphs: &[u16]) -> Option<(u16, usize)> {
    if sub.len() < 6 {
        return None;
    }
    let format = read_u16(sub, 0).ok()?;
    if format != 1 {
        return None;
    }
    let coverage_off = read_u16(sub, 2).ok()? as usize;
    let coverage = sub.get(coverage_off..)?;
    let cov_idx = coverage_lookup(coverage, glyphs[0])? as usize;

    let lig_set_count = read_u16(sub, 4).ok()? as usize;
    if cov_idx >= lig_set_count {
        return None;
    }
    let lig_set_off = read_u16(sub, 6 + cov_idx * 2).ok()? as usize;
    let lig_set = sub.get(lig_set_off..)?;

    if lig_set.len() < 2 {
        return None;
    }
    let lig_count = read_u16(lig_set, 0).ok()? as usize;
    // For each ligature in this set, see if its component sequence
    // matches a prefix of `glyphs` after the first.
    for i in 0..lig_count {
        let lig_off = read_u16(lig_set, 2 + i * 2).ok()? as usize;
        let lig = lig_set.get(lig_off..)?;
        if lig.len() < 4 {
            continue;
        }
        let lig_glyph = read_u16(lig, 0).ok()?;
        let comp_count = read_u16(lig, 2).ok()? as usize;
        if comp_count < 1 {
            continue;
        }
        if comp_count > glyphs.len() {
            continue;
        }
        // First glyph already matched via coverage; compare remaining
        // (comp_count - 1) glyphs against componentGlyphIDs.
        let remaining = comp_count - 1;
        if lig.len() < 4 + remaining * 2 {
            continue;
        }
        let mut ok = true;
        for j in 0..remaining {
            let want = read_u16(lig, 4 + j * 2).ok()?;
            if glyphs[1 + j] != want {
                ok = false;
                break;
            }
        }
        if ok {
            return Some((lig_glyph, comp_count));
        }
    }
    None
}

/// Walk a MultipleSubstFormat1 sub-table looking for a Sequence record.
///
/// Layout (per OpenType §"Multiple Substitution Subtable"):
///   u16 format = 1
///   Offset16 coverageOffset
///   u16 sequenceCount
///   Offset16 sequenceOffsets[sequenceCount]
///
///   Sequence { u16 glyphCount; u16 substituteGlyphIDs[glyphCount]; }
///
/// All offsets are relative to the start of the sub-table.
fn multiple_subst_lookup(sub: &[u8], gid: u16) -> Option<Vec<u16>> {
    if sub.len() < 6 {
        return None;
    }
    let format = read_u16(sub, 0).ok()?;
    if format != 1 {
        return None;
    }
    let coverage_off = read_u16(sub, 2).ok()? as usize;
    let coverage = sub.get(coverage_off..)?;
    let cov_idx = coverage_lookup(coverage, gid)? as usize;
    let seq_count = read_u16(sub, 4).ok()? as usize;
    if cov_idx >= seq_count {
        return None;
    }
    let seq_off = read_u16(sub, 6 + cov_idx * 2).ok()? as usize;
    let seq = sub.get(seq_off..)?;
    if seq.len() < 2 {
        return None;
    }
    let glyph_count = read_u16(seq, 0).ok()? as usize;
    if seq.len() < 2 + glyph_count * 2 {
        return None;
    }
    let mut out = Vec::with_capacity(glyph_count);
    for i in 0..glyph_count {
        out.push(read_u16(seq, 2 + i * 2).ok()?);
    }
    Some(out)
}

/// Walk an AlternateSubstFormat1 sub-table looking for an alternate.
///
/// Layout (per OpenType §"Alternate Substitution Subtable"):
///   u16 format = 1
///   Offset16 coverageOffset
///   u16 alternateSetCount
///   Offset16 alternateSetOffsets[alternateSetCount]
///
///   AlternateSet { u16 glyphCount; u16 alternateGlyphIDs[glyphCount]; }
///
/// All offsets are relative to the start of the sub-table.
fn alternate_subst_lookup(sub: &[u8], gid: u16, alternate_index: u16) -> Option<u16> {
    if sub.len() < 6 {
        return None;
    }
    let format = read_u16(sub, 0).ok()?;
    if format != 1 {
        return None;
    }
    let coverage_off = read_u16(sub, 2).ok()? as usize;
    let coverage = sub.get(coverage_off..)?;
    let cov_idx = coverage_lookup(coverage, gid)? as usize;
    let alt_count = read_u16(sub, 4).ok()? as usize;
    if cov_idx >= alt_count {
        return None;
    }
    let alt_off = read_u16(sub, 6 + cov_idx * 2).ok()? as usize;
    let alt_set = sub.get(alt_off..)?;
    if alt_set.len() < 2 {
        return None;
    }
    let glyph_count = read_u16(alt_set, 0).ok()? as usize;
    let idx = alternate_index as usize;
    if idx >= glyph_count {
        return None;
    }
    if alt_set.len() < 2 + (idx + 1) * 2 {
        return None;
    }
    read_u16(alt_set, 2 + idx * 2).ok()
}

/// Walk a ReverseChainSingleSubstFormat1 sub-table.
///
/// Layout (per OpenType §"Reverse Chaining Contextual Single
/// Substitution Subtable"):
///   u16 format = 1
///   Offset16 coverageOffset                  (input glyph coverage)
///   u16 backtrackGlyphCount
///   Offset16 backtrackCoverageOffsets[backtrackGlyphCount]
///   u16 lookaheadGlyphCount
///   Offset16 lookaheadCoverageOffsets[lookaheadGlyphCount]
///   u16 glyphCount                           (= input coverage size)
///   u16 substituteGlyphIDs[glyphCount]
///
/// Backtrack coverages are listed in reverse-text order
/// (`backtrackCoverageOffsets[0]` covers the glyph immediately before
/// `gids[pos]`). Substitute glyph at `substituteGlyphIDs[cov_idx]`
/// where `cov_idx` is the input coverage index of `gids[pos]`.
fn reverse_chain_context_lookup(sub: &[u8], gids: &[u16], pos: usize) -> Option<u16> {
    if sub.len() < 6 {
        return None;
    }
    let format = read_u16(sub, 0).ok()?;
    if format != 1 {
        return None;
    }
    let coverage_off = read_u16(sub, 2).ok()? as usize;
    let coverage = sub.get(coverage_off..)?;
    let cov_idx = coverage_lookup(coverage, gids[pos])? as usize;
    let mut cur = 4usize;
    if sub.len() < cur + 2 {
        return None;
    }
    let bt_count = read_u16(sub, cur).ok()? as usize;
    cur += 2;
    if sub.len() < cur + bt_count * 2 {
        return None;
    }
    if pos < bt_count {
        return None;
    }
    for i in 0..bt_count {
        let cov_off = read_u16(sub, cur + i * 2).ok()? as usize;
        let cov = sub.get(cov_off..)?;
        coverage_lookup(cov, gids[pos - 1 - i])?;
    }
    cur += bt_count * 2;
    if sub.len() < cur + 2 {
        return None;
    }
    let la_count = read_u16(sub, cur).ok()? as usize;
    cur += 2;
    if sub.len() < cur + la_count * 2 {
        return None;
    }
    if pos + 1 + la_count > gids.len() {
        return None;
    }
    for i in 0..la_count {
        let cov_off = read_u16(sub, cur + i * 2).ok()? as usize;
        let cov = sub.get(cov_off..)?;
        coverage_lookup(cov, gids[pos + 1 + i])?;
    }
    cur += la_count * 2;
    if sub.len() < cur + 2 {
        return None;
    }
    let glyph_count = read_u16(sub, cur).ok()? as usize;
    cur += 2;
    if cov_idx >= glyph_count {
        return None;
    }
    if sub.len() < cur + (cov_idx + 1) * 2 {
        return None;
    }
    read_u16(sub, cur + cov_idx * 2).ok()
}

/// Outcome of a chained-context match: how many input glyphs the rule
/// covers, plus the SubstLookupRecord array to apply against them.
#[derive(Debug)]
struct ChainMatch {
    input_len: usize,
    records: Vec<SubstLookupRecord>,
}

#[derive(Debug, Clone, Copy)]
struct SubstLookupRecord {
    sequence_index: u16,
    lookup_index: u16,
}

/// Peek through a Lookup table that holds a single ExtensionSubst
/// sub-table and return the wrapped lookup type. Returns `None` if the
/// shape doesn't match (no sub-tables, malformed extension header, …).
fn peek_extension_type(lookup: &[u8]) -> Option<u16> {
    if lookup.len() < 8 {
        return None;
    }
    let sub_count = read_u16(lookup, 4).ok()? as usize;
    if sub_count == 0 {
        return None;
    }
    let sub_off = read_u16(lookup, 6).ok()? as usize;
    let sub = lookup.get(sub_off..)?;
    if sub.len() < 8 {
        return None;
    }
    // ExtensionSubstFormat1: u16 format=1, u16 extensionLookupType, Offset32.
    read_u16(sub, 2).ok()
}

/// Decode a SubstLookupRecord array of length `count` starting at
/// `offset` inside `bytes`. Returns `None` on truncation.
fn read_subst_lookup_records(
    bytes: &[u8],
    offset: usize,
    count: usize,
) -> Option<Vec<SubstLookupRecord>> {
    if bytes.len() < offset + count * 4 {
        return None;
    }
    let mut out = Vec::with_capacity(count);
    for i in 0..count {
        let off = offset + i * 4;
        let seq = read_u16(bytes, off).ok()?;
        let lk = read_u16(bytes, off + 2).ok()?;
        out.push(SubstLookupRecord {
            sequence_index: seq,
            lookup_index: lk,
        });
    }
    Some(out)
}

/// Match a ChainContextSubstFormat1 sub-table against `gids[pos..]`.
///
/// Layout (per OpenType §"Chained Sequence Context Format 1: simple
/// glyph contexts"):
///   u16 format = 1
///   Offset16 coverageOffset             (input[0] coverage)
///   u16 chainSubRuleSetCount
///   Offset16 chainSubRuleSetOffsets[chainSubRuleSetCount]
///
///   ChainSubRuleSet { u16 chainSubRuleCount; Offset16 chainSubRuleOffsets[]; }
///   ChainSubRule    { u16 backtrackGlyphCount; u16 backtrackSequence[];
///                     u16 inputGlyphCount;     u16 inputSequence[inputGlyphCount-1];
///                     u16 lookaheadGlyphCount; u16 lookaheadSequence[];
///                     u16 substCount;          SubstLookupRecord substRecords[]; }
///
/// All offsets are relative to the start of the sub-table (Format 1
/// header), then to each ChainSubRuleSet, in turn.
fn chain_context_format1_match(sub: &[u8], gids: &[u16], pos: usize) -> Option<ChainMatch> {
    if sub.len() < 6 {
        return None;
    }
    let coverage_off = read_u16(sub, 2).ok()? as usize;
    let coverage = sub.get(coverage_off..)?;
    let cov_idx = coverage_lookup(coverage, gids[pos])? as usize;
    let set_count = read_u16(sub, 4).ok()? as usize;
    if cov_idx >= set_count {
        return None;
    }
    let set_off = read_u16(sub, 6 + cov_idx * 2).ok()? as usize;
    let set = sub.get(set_off..)?;
    if set.len() < 2 {
        return None;
    }
    let rule_count = read_u16(set, 0).ok()? as usize;
    for r in 0..rule_count {
        let rule_off = read_u16(set, 2 + r * 2).ok()? as usize;
        let rule = match set.get(rule_off..) {
            Some(b) => b,
            None => continue,
        };
        if let Some(m) = chain_context_format1_rule_match(rule, gids, pos) {
            return Some(m);
        }
    }
    None
}

fn chain_context_format1_rule_match(rule: &[u8], gids: &[u16], pos: usize) -> Option<ChainMatch> {
    // backtrackGlyphCount + sequence
    let mut cur = 0usize;
    if rule.len() < cur + 2 {
        return None;
    }
    let bt_count = read_u16(rule, cur).ok()? as usize;
    cur += 2;
    if rule.len() < cur + bt_count * 2 {
        return None;
    }
    if pos < bt_count {
        return None;
    }
    // Backtrack sequence is stored in *reverse text* order (closest to
    // the input window first). Compare against gids working backwards
    // from pos.
    for i in 0..bt_count {
        let want = read_u16(rule, cur + i * 2).ok()?;
        if gids[pos - 1 - i] != want {
            return None;
        }
    }
    cur += bt_count * 2;
    // inputGlyphCount + inputSequence (length = count - 1)
    if rule.len() < cur + 2 {
        return None;
    }
    let in_count = read_u16(rule, cur).ok()? as usize;
    if in_count == 0 {
        return None;
    }
    cur += 2;
    let in_extra = in_count - 1;
    if rule.len() < cur + in_extra * 2 {
        return None;
    }
    if pos + in_count > gids.len() {
        return None;
    }
    for i in 0..in_extra {
        let want = read_u16(rule, cur + i * 2).ok()?;
        if gids[pos + 1 + i] != want {
            return None;
        }
    }
    cur += in_extra * 2;
    // lookaheadGlyphCount + sequence
    if rule.len() < cur + 2 {
        return None;
    }
    let la_count = read_u16(rule, cur).ok()? as usize;
    cur += 2;
    if rule.len() < cur + la_count * 2 {
        return None;
    }
    if pos + in_count + la_count > gids.len() {
        return None;
    }
    for i in 0..la_count {
        let want = read_u16(rule, cur + i * 2).ok()?;
        if gids[pos + in_count + i] != want {
            return None;
        }
    }
    cur += la_count * 2;
    // substCount + records
    if rule.len() < cur + 2 {
        return None;
    }
    let subst_count = read_u16(rule, cur).ok()? as usize;
    cur += 2;
    let records = read_subst_lookup_records(rule, cur, subst_count)?;
    Some(ChainMatch {
        input_len: in_count,
        records,
    })
}

/// Match a ChainContextSubstFormat2 sub-table against `gids[pos..]`.
///
/// Layout:
///   u16 format = 2
///   Offset16 coverageOffset
///   Offset16 backtrackClassDefOffset
///   Offset16 inputClassDefOffset
///   Offset16 lookaheadClassDefOffset
///   u16 chainSubClassSetCount
///   Offset16 chainSubClassSetOffsets[chainSubClassSetCount]
///
///   ChainSubClassSet { u16 chainSubClassRuleCount; Offset16 chainSubClassRuleOffsets[]; }
///   ChainSubClassRule { u16 backtrackGlyphCount; u16 backtrackSequence[]; (class IDs)
///                       u16 inputGlyphCount;     u16 inputSequence[inputGlyphCount-1];
///                       u16 lookaheadGlyphCount; u16 lookaheadSequence[];
///                       u16 substCount;          SubstLookupRecord substRecords[]; }
fn chain_context_format2_match(sub: &[u8], gids: &[u16], pos: usize) -> Option<ChainMatch> {
    if sub.len() < 12 {
        return None;
    }
    let coverage_off = read_u16(sub, 2).ok()? as usize;
    let bt_cd_off = read_u16(sub, 4).ok()? as usize;
    let in_cd_off = read_u16(sub, 6).ok()? as usize;
    let la_cd_off = read_u16(sub, 8).ok()? as usize;
    let set_count = read_u16(sub, 10).ok()? as usize;
    let coverage = sub.get(coverage_off..)?;
    coverage_lookup(coverage, gids[pos])?;
    // The Class of the input[0] glyph picks the ChainSubClassSet.
    let in_cd = sub.get(in_cd_off..)?;
    let in_class0 = class_def_lookup(in_cd, gids[pos]).unwrap_or(0);
    if in_class0 as usize >= set_count {
        return None;
    }
    let set_off = read_u16(sub, 12 + in_class0 as usize * 2).ok()? as usize;
    if set_off == 0 {
        // Empty set offsets are valid per spec; nothing matches.
        return None;
    }
    let set = sub.get(set_off..)?;
    if set.len() < 2 {
        return None;
    }
    let rule_count = read_u16(set, 0).ok()? as usize;
    let bt_cd = sub.get(bt_cd_off..);
    let la_cd = sub.get(la_cd_off..);
    for r in 0..rule_count {
        let rule_off = read_u16(set, 2 + r * 2).ok()? as usize;
        let rule = match set.get(rule_off..) {
            Some(b) => b,
            None => continue,
        };
        if let Some(m) =
            chain_context_format2_rule_match(rule, gids, pos, bt_cd, in_cd, la_cd, in_class0)
        {
            return Some(m);
        }
    }
    None
}

fn chain_context_format2_rule_match(
    rule: &[u8],
    gids: &[u16],
    pos: usize,
    bt_cd: Option<&[u8]>,
    in_cd: &[u8],
    la_cd: Option<&[u8]>,
    in_class0: u16,
) -> Option<ChainMatch> {
    let mut cur = 0usize;
    if rule.len() < cur + 2 {
        return None;
    }
    let bt_count = read_u16(rule, cur).ok()? as usize;
    cur += 2;
    if rule.len() < cur + bt_count * 2 {
        return None;
    }
    if pos < bt_count {
        return None;
    }
    // Backtrack classes; reverse text order.
    if bt_count > 0 {
        let bt_cd = bt_cd?;
        for i in 0..bt_count {
            let want = read_u16(rule, cur + i * 2).ok()?;
            let got = class_def_lookup(bt_cd, gids[pos - 1 - i]).unwrap_or(0);
            if want != got {
                return None;
            }
        }
    }
    cur += bt_count * 2;
    if rule.len() < cur + 2 {
        return None;
    }
    let in_count = read_u16(rule, cur).ok()? as usize;
    if in_count == 0 {
        return None;
    }
    cur += 2;
    let in_extra = in_count - 1;
    if rule.len() < cur + in_extra * 2 {
        return None;
    }
    if pos + in_count > gids.len() {
        return None;
    }
    // input[0] class is implicitly in_class0 (the caller-picked set);
    // verify input[1..] classes against in_cd.
    for i in 0..in_extra {
        let want = read_u16(rule, cur + i * 2).ok()?;
        let got = class_def_lookup(in_cd, gids[pos + 1 + i]).unwrap_or(0);
        if want != got {
            return None;
        }
    }
    cur += in_extra * 2;
    // (in_class0 is implicit, so no rule field consumed for it.)
    let _ = in_class0;
    if rule.len() < cur + 2 {
        return None;
    }
    let la_count = read_u16(rule, cur).ok()? as usize;
    cur += 2;
    if rule.len() < cur + la_count * 2 {
        return None;
    }
    if pos + in_count + la_count > gids.len() {
        return None;
    }
    if la_count > 0 {
        let la_cd = la_cd?;
        for i in 0..la_count {
            let want = read_u16(rule, cur + i * 2).ok()?;
            let got = class_def_lookup(la_cd, gids[pos + in_count + i]).unwrap_or(0);
            if want != got {
                return None;
            }
        }
    }
    cur += la_count * 2;
    if rule.len() < cur + 2 {
        return None;
    }
    let subst_count = read_u16(rule, cur).ok()? as usize;
    cur += 2;
    let records = read_subst_lookup_records(rule, cur, subst_count)?;
    Some(ChainMatch {
        input_len: in_count,
        records,
    })
}

/// Match a ChainContextSubstFormat3 sub-table against `gids[pos..]`.
///
/// Layout:
///   u16 format = 3
///   u16 backtrackGlyphCount
///   Offset16 backtrackCoverageOffsets[backtrackGlyphCount]
///   u16 inputGlyphCount
///   Offset16 inputCoverageOffsets[inputGlyphCount]
///   u16 lookaheadGlyphCount
///   Offset16 lookaheadCoverageOffsets[lookaheadGlyphCount]
///   u16 seqLookupCount
///   SubstLookupRecord seqLookupRecords[seqLookupCount]
///
/// All coverage offsets are relative to the start of the sub-table.
/// Backtrack coverages are listed in *reverse text* order (the spec is
/// explicit: "backtrackCoverageOffsets[0] points to the Coverage table
/// for the glyph immediately preceding input[0]").
fn chain_context_format3_match(sub: &[u8], gids: &[u16], pos: usize) -> Option<ChainMatch> {
    if sub.len() < 4 {
        return None;
    }
    let mut cur = 2usize;
    let bt_count = read_u16(sub, cur).ok()? as usize;
    cur += 2;
    if sub.len() < cur + bt_count * 2 {
        return None;
    }
    if pos < bt_count {
        return None;
    }
    for i in 0..bt_count {
        let cov_off = read_u16(sub, cur + i * 2).ok()? as usize;
        let cov = sub.get(cov_off..)?;
        coverage_lookup(cov, gids[pos - 1 - i])?;
    }
    cur += bt_count * 2;
    if sub.len() < cur + 2 {
        return None;
    }
    let in_count = read_u16(sub, cur).ok()? as usize;
    if in_count == 0 {
        return None;
    }
    cur += 2;
    if sub.len() < cur + in_count * 2 {
        return None;
    }
    if pos + in_count > gids.len() {
        return None;
    }
    for i in 0..in_count {
        let cov_off = read_u16(sub, cur + i * 2).ok()? as usize;
        let cov = sub.get(cov_off..)?;
        coverage_lookup(cov, gids[pos + i])?;
    }
    cur += in_count * 2;
    if sub.len() < cur + 2 {
        return None;
    }
    let la_count = read_u16(sub, cur).ok()? as usize;
    cur += 2;
    if sub.len() < cur + la_count * 2 {
        return None;
    }
    if pos + in_count + la_count > gids.len() {
        return None;
    }
    for i in 0..la_count {
        let cov_off = read_u16(sub, cur + i * 2).ok()? as usize;
        let cov = sub.get(cov_off..)?;
        coverage_lookup(cov, gids[pos + in_count + i])?;
    }
    cur += la_count * 2;
    if sub.len() < cur + 2 {
        return None;
    }
    let subst_count = read_u16(sub, cur).ok()? as usize;
    cur += 2;
    let records = read_subst_lookup_records(sub, cur, subst_count)?;
    Some(ChainMatch {
        input_len: in_count,
        records,
    })
}

/// Match a SequenceContextFormat1 sub-table (LookupType 5 Format 1)
/// against `gids[pos..]`.
///
/// Layout (per OpenType §"Sequence Context Format 1: simple glyph
/// contexts"):
///   u16 format = 1
///   Offset16 coverageOffset                  (input[0] coverage)
///   u16 subRuleSetCount
///   Offset16 subRuleSetOffsets[subRuleSetCount]
///
///   SubRuleSet { u16 subRuleCount; Offset16 subRuleOffsets[]; }
///   SubRule    { u16 inputGlyphCount; u16 inputSequence[inputGlyphCount-1];
///                u16 substCount;       SubstLookupRecord substRecords[]; }
///
/// All offsets are relative to the start of the sub-table, then to
/// each SubRuleSet, in turn.
fn context_format1_match(sub: &[u8], gids: &[u16], pos: usize) -> Option<ChainMatch> {
    if sub.len() < 6 {
        return None;
    }
    let coverage_off = read_u16(sub, 2).ok()? as usize;
    let coverage = sub.get(coverage_off..)?;
    let cov_idx = coverage_lookup(coverage, gids[pos])? as usize;
    let set_count = read_u16(sub, 4).ok()? as usize;
    if cov_idx >= set_count {
        return None;
    }
    let set_off = read_u16(sub, 6 + cov_idx * 2).ok()? as usize;
    let set = sub.get(set_off..)?;
    if set.len() < 2 {
        return None;
    }
    let rule_count = read_u16(set, 0).ok()? as usize;
    for r in 0..rule_count {
        let rule_off = read_u16(set, 2 + r * 2).ok()? as usize;
        let rule = match set.get(rule_off..) {
            Some(b) => b,
            None => continue,
        };
        if let Some(m) = context_format1_rule_match(rule, gids, pos) {
            return Some(m);
        }
    }
    None
}

fn context_format1_rule_match(rule: &[u8], gids: &[u16], pos: usize) -> Option<ChainMatch> {
    let mut cur = 0usize;
    if rule.len() < cur + 2 {
        return None;
    }
    let in_count = read_u16(rule, cur).ok()? as usize;
    if in_count == 0 {
        return None;
    }
    cur += 2;
    let in_extra = in_count - 1;
    if rule.len() < cur + in_extra * 2 {
        return None;
    }
    if pos + in_count > gids.len() {
        return None;
    }
    for i in 0..in_extra {
        let want = read_u16(rule, cur + i * 2).ok()?;
        if gids[pos + 1 + i] != want {
            return None;
        }
    }
    cur += in_extra * 2;
    if rule.len() < cur + 2 {
        return None;
    }
    let subst_count = read_u16(rule, cur).ok()? as usize;
    cur += 2;
    let records = read_subst_lookup_records(rule, cur, subst_count)?;
    Some(ChainMatch {
        input_len: in_count,
        records,
    })
}

/// Match a SequenceContextFormat2 sub-table (LookupType 5 Format 2)
/// against `gids[pos..]`.
///
/// Layout:
///   u16 format = 2
///   Offset16 coverageOffset
///   Offset16 classDefOffset
///   u16 subClassSetCount
///   Offset16 subClassSetOffsets[subClassSetCount]
///
///   SubClassSet  { u16 subClassRuleCount; Offset16 subClassRuleOffsets[]; }
///   SubClassRule { u16 glyphCount; u16 inputSequence[glyphCount-1]; (class IDs)
///                  u16 substCount; SubstLookupRecord substRecords[]; }
fn context_format2_match(sub: &[u8], gids: &[u16], pos: usize) -> Option<ChainMatch> {
    if sub.len() < 8 {
        return None;
    }
    let coverage_off = read_u16(sub, 2).ok()? as usize;
    let cd_off = read_u16(sub, 4).ok()? as usize;
    let set_count = read_u16(sub, 6).ok()? as usize;
    let coverage = sub.get(coverage_off..)?;
    coverage_lookup(coverage, gids[pos])?;
    let cd = sub.get(cd_off..)?;
    let class0 = class_def_lookup(cd, gids[pos]).unwrap_or(0);
    if class0 as usize >= set_count {
        return None;
    }
    let set_off = read_u16(sub, 8 + class0 as usize * 2).ok()? as usize;
    if set_off == 0 {
        return None;
    }
    let set = sub.get(set_off..)?;
    if set.len() < 2 {
        return None;
    }
    let rule_count = read_u16(set, 0).ok()? as usize;
    for r in 0..rule_count {
        let rule_off = read_u16(set, 2 + r * 2).ok()? as usize;
        let rule = match set.get(rule_off..) {
            Some(b) => b,
            None => continue,
        };
        if let Some(m) = context_format2_rule_match(rule, gids, pos, cd) {
            return Some(m);
        }
    }
    None
}

fn context_format2_rule_match(
    rule: &[u8],
    gids: &[u16],
    pos: usize,
    cd: &[u8],
) -> Option<ChainMatch> {
    let mut cur = 0usize;
    if rule.len() < cur + 2 {
        return None;
    }
    let in_count = read_u16(rule, cur).ok()? as usize;
    if in_count == 0 {
        return None;
    }
    cur += 2;
    let in_extra = in_count - 1;
    if rule.len() < cur + in_extra * 2 {
        return None;
    }
    if pos + in_count > gids.len() {
        return None;
    }
    // input[0] class is implicit (the caller-picked set selects it).
    for i in 0..in_extra {
        let want = read_u16(rule, cur + i * 2).ok()?;
        let got = class_def_lookup(cd, gids[pos + 1 + i]).unwrap_or(0);
        if want != got {
            return None;
        }
    }
    cur += in_extra * 2;
    if rule.len() < cur + 2 {
        return None;
    }
    let subst_count = read_u16(rule, cur).ok()? as usize;
    cur += 2;
    let records = read_subst_lookup_records(rule, cur, subst_count)?;
    Some(ChainMatch {
        input_len: in_count,
        records,
    })
}

/// Match a SequenceContextFormat3 sub-table (LookupType 5 Format 3)
/// against `gids[pos..]`.
///
/// Layout:
///   u16 format = 3
///   u16 glyphCount
///   u16 substCount
///   Offset16 coverageOffsets[glyphCount]
///   SubstLookupRecord substRecords[substCount]
///
/// All coverage offsets are relative to the start of the sub-table.
fn context_format3_match(sub: &[u8], gids: &[u16], pos: usize) -> Option<ChainMatch> {
    if sub.len() < 6 {
        return None;
    }
    let glyph_count = read_u16(sub, 2).ok()? as usize;
    let subst_count = read_u16(sub, 4).ok()? as usize;
    if glyph_count == 0 {
        return None;
    }
    let mut cur = 6usize;
    if sub.len() < cur + glyph_count * 2 {
        return None;
    }
    if pos + glyph_count > gids.len() {
        return None;
    }
    for i in 0..glyph_count {
        let cov_off = read_u16(sub, cur + i * 2).ok()? as usize;
        let cov = sub.get(cov_off..)?;
        coverage_lookup(cov, gids[pos + i])?;
    }
    cur += glyph_count * 2;
    let records = read_subst_lookup_records(sub, cur, subst_count)?;
    Some(ChainMatch {
        input_len: glyph_count,
        records,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Build a self-contained GSUB blob with one LookupType-4 sub-table
    /// covering glyph 100 → ligature glyph 999 with components [200,300].
    fn build_simple_gsub() -> (Vec<u8>, u16, u16, u16, u16) {
        // We hand-build offsets in nested tables.
        // Layout plan (relative to start of GSUB table):
        //   0..10  GSUB header (v1.0). lookupListOffset = 10.
        //  10..14  LookupList: count=1, offset to lookup
        //  14..22  Lookup: type=4, flag=0, subTableCount=1, subOffset=8
        //  22..30  LigatureSubstFormat1 header (we'll write it)
        //   ...

        // Build sub-objects bottom-up.
        // Ligature table: ligGlyph=999, componentCount=3, componentGlyphIDs=[200,300]
        let mut lig = Vec::new();
        lig.extend_from_slice(&999u16.to_be_bytes());
        lig.extend_from_slice(&3u16.to_be_bytes());
        lig.extend_from_slice(&200u16.to_be_bytes());
        lig.extend_from_slice(&300u16.to_be_bytes());

        // LigatureSet: count=1, offset to lig (after the 4-byte header).
        let mut lig_set = Vec::new();
        lig_set.extend_from_slice(&1u16.to_be_bytes());
        lig_set.extend_from_slice(&4u16.to_be_bytes()); // ligature offset (2 + 2)
        lig_set.extend_from_slice(&lig);

        // Coverage Format 1 covering glyph 100.
        let mut cov = Vec::new();
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&100u16.to_be_bytes());

        // LigatureSubstFormat1: format=1, coverageOffset, ligSetCount=1, ligSetOffsets[1].
        // Header is 8 bytes (format(2) + cov(2) + count(2) + offset(2)).
        let lig_subst_header_len = 8;
        let cov_off = lig_subst_header_len;
        let lig_set_off = cov_off + cov.len();
        let mut lig_subst = Vec::new();
        lig_subst.extend_from_slice(&1u16.to_be_bytes());
        lig_subst.extend_from_slice(&(cov_off as u16).to_be_bytes());
        lig_subst.extend_from_slice(&1u16.to_be_bytes());
        lig_subst.extend_from_slice(&(lig_set_off as u16).to_be_bytes());
        lig_subst.extend_from_slice(&cov);
        lig_subst.extend_from_slice(&lig_set);

        // Lookup table: type=4, flag=0, subCount=1, subOffsets=[8].
        // Header is 6 bytes; one subtable offset = 2 bytes; subtable
        // starts at offset 8 from the lookup-table start.
        let mut lookup = Vec::new();
        lookup.extend_from_slice(&4u16.to_be_bytes());
        lookup.extend_from_slice(&0u16.to_be_bytes());
        lookup.extend_from_slice(&1u16.to_be_bytes());
        lookup.extend_from_slice(&8u16.to_be_bytes());
        lookup.extend_from_slice(&lig_subst);

        // LookupList: lookupCount=1, lookupOffsets=[4].
        let mut lookup_list = Vec::new();
        lookup_list.extend_from_slice(&1u16.to_be_bytes());
        lookup_list.extend_from_slice(&4u16.to_be_bytes());
        lookup_list.extend_from_slice(&lookup);

        // GSUB header: v1.0, scriptList=NULL(0), featureList=NULL(0), lookupListOffset=10.
        let mut gsub = Vec::new();
        gsub.extend_from_slice(&1u16.to_be_bytes()); // major
        gsub.extend_from_slice(&0u16.to_be_bytes()); // minor
        gsub.extend_from_slice(&0u16.to_be_bytes()); // scriptList
        gsub.extend_from_slice(&0u16.to_be_bytes()); // featureList
        gsub.extend_from_slice(&10u16.to_be_bytes()); // lookupList
        gsub.extend_from_slice(&lookup_list);

        (gsub, 100, 200, 300, 999)
    }

    #[test]
    fn round_trip_3_glyph_ligature() {
        let (bytes, a, b, c, lig) = build_simple_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        assert_eq!(g.lookup_ligature(&[a, b, c]), Some((lig, 3)));
        // Wrong second glyph → no match.
        assert_eq!(g.lookup_ligature(&[a, b, 999]), None);
        // First glyph not covered → no match.
        assert_eq!(g.lookup_ligature(&[42, b, c]), None);
        // Too short input.
        assert_eq!(g.lookup_ligature(&[a]), None);
    }

    /// Build a GSUB blob exercising:
    /// - one ScriptList with `arab` → DefaultLangSys → 4 feature indices
    /// - one FeatureList with the four entries `init`/`medi`/`fina`/`isol`,
    ///   each pointing at one lookup index (0..=3)
    /// - one LookupList with two LookupType-1 subtables:
    ///   * Format 1 (delta = +5) at lookup index 0
    ///   * Format 2 (substituteGlyphIDs) at lookup index 1
    ///   * (lookups 2 and 3 are stubs that share the same payload)
    ///
    /// We hand-pack offsets at build time and return `(blob, off_table)`
    /// where `off_table` lists the offsets the tests want to assert
    /// against.
    fn build_feature_tagged_gsub() -> Vec<u8> {
        // ----- LookupList -----
        //
        // Lookup #0 — SingleSubst Format 1: covers gid 10, delta = +5.
        //   Coverage Format 1: count=1, [10]
        //   SingleSubstFmt1: format=1, coverageOffset(=6), delta=5
        let mut cov0 = Vec::new();
        cov0.extend_from_slice(&1u16.to_be_bytes()); // format
        cov0.extend_from_slice(&1u16.to_be_bytes()); // glyphCount
        cov0.extend_from_slice(&10u16.to_be_bytes());
        let mut sub0 = Vec::new();
        sub0.extend_from_slice(&1u16.to_be_bytes()); // format
        sub0.extend_from_slice(&6u16.to_be_bytes()); // coverageOffset (after 6-byte header)
        sub0.extend_from_slice(&5i16.to_be_bytes()); // delta
        sub0.extend_from_slice(&cov0);

        // Lookup #1 — SingleSubst Format 2: covers gids [20,21,22] →
        //   substitutes [200,201,202].
        let mut cov1 = Vec::new();
        cov1.extend_from_slice(&1u16.to_be_bytes()); // format
        cov1.extend_from_slice(&3u16.to_be_bytes()); // glyphCount
        cov1.extend_from_slice(&20u16.to_be_bytes());
        cov1.extend_from_slice(&21u16.to_be_bytes());
        cov1.extend_from_slice(&22u16.to_be_bytes());
        let mut sub1 = Vec::new();
        sub1.extend_from_slice(&2u16.to_be_bytes()); // format
                                                     // header is 6 bytes (format + cov + count); we put coverage AFTER the
                                                     // substitute-array. substituteGlyphIDs[3] = 6 bytes.
        let cov1_off_in_sub1: u16 = 6 + 6; // = 12
        sub1.extend_from_slice(&cov1_off_in_sub1.to_be_bytes());
        sub1.extend_from_slice(&3u16.to_be_bytes()); // glyphCount
        sub1.extend_from_slice(&200u16.to_be_bytes());
        sub1.extend_from_slice(&201u16.to_be_bytes());
        sub1.extend_from_slice(&202u16.to_be_bytes());
        sub1.extend_from_slice(&cov1);

        // Build a Lookup wrapper for one subtable:
        //   u16 lookupType, u16 lookupFlag, u16 subTableCount, u16 subTableOffsets[]
        // Header is 6 bytes + 2 per subtable; the subtable lives
        // immediately after.
        fn wrap_lookup(lookup_type: u16, sub: &[u8]) -> Vec<u8> {
            let mut out = Vec::new();
            out.extend_from_slice(&lookup_type.to_be_bytes());
            out.extend_from_slice(&0u16.to_be_bytes()); // flag
            out.extend_from_slice(&1u16.to_be_bytes()); // subTableCount
            out.extend_from_slice(&8u16.to_be_bytes()); // subTableOffset = 8 (right after the offset)
            out.extend_from_slice(sub);
            out
        }

        let lookup0 = wrap_lookup(LOOKUP_SINGLE_SUBST, &sub0);
        let lookup1 = wrap_lookup(LOOKUP_SINGLE_SUBST, &sub1);
        let lookup2 = wrap_lookup(LOOKUP_SINGLE_SUBST, &sub0);
        let lookup3 = wrap_lookup(LOOKUP_SINGLE_SUBST, &sub0);

        // LookupList: u16 count, Offset16 lookupOffsets[count].
        // The LookupList is a self-contained sub-table; lookupOffsets
        // are RELATIVE TO the LookupList start.
        let lookup_list_header_len = 2 + 4 * 2; // count + 4 offsets
        let mut lookup_list = Vec::new();
        lookup_list.extend_from_slice(&4u16.to_be_bytes());
        let mut running = lookup_list_header_len as u16;
        // Offsets first…
        for lk in [&lookup0, &lookup1, &lookup2, &lookup3] {
            lookup_list.extend_from_slice(&running.to_be_bytes());
            running += lk.len() as u16;
        }
        // …then payloads in declaration order.
        for lk in [&lookup0, &lookup1, &lookup2, &lookup3] {
            lookup_list.extend_from_slice(lk);
        }

        // ----- FeatureList -----
        //
        // Feature 0: init → lookup [0]
        // Feature 1: medi → lookup [1]
        // Feature 2: fina → lookup [2]
        // Feature 3: isol → lookup [3]
        fn build_feature(lookup_idx: u16) -> Vec<u8> {
            let mut f = Vec::new();
            f.extend_from_slice(&0u16.to_be_bytes()); // featureParamsOffset = NULL
            f.extend_from_slice(&1u16.to_be_bytes()); // lookupIndexCount
            f.extend_from_slice(&lookup_idx.to_be_bytes());
            f
        }
        let feat_init = build_feature(0);
        let feat_medi = build_feature(1);
        let feat_fina = build_feature(2);
        let feat_isol = build_feature(3);
        let tags: [&[u8; 4]; 4] = [b"init", b"medi", b"fina", b"isol"];

        // FeatureList: u16 featureCount, FeatureRecord{ Tag(4),
        // Offset16 featureOffset } records[count];
        // featureOffsets are relative to FeatureList start.
        let feature_list_header_len = 2 + 4 * 6; // count + 4 records of 6 bytes
        let mut feature_list = Vec::new();
        feature_list.extend_from_slice(&4u16.to_be_bytes());
        let mut running_f = feature_list_header_len as u16;
        let payloads = [&feat_init, &feat_medi, &feat_fina, &feat_isol];
        for (tag, fbytes) in tags.iter().zip(payloads.iter()) {
            feature_list.extend_from_slice(*tag);
            feature_list.extend_from_slice(&running_f.to_be_bytes());
            running_f += fbytes.len() as u16;
        }
        for fbytes in payloads {
            feature_list.extend_from_slice(fbytes);
        }

        // ----- ScriptList -----
        //
        // One script: arab → DefaultLangSys → feature indices [0,1,2,3].
        //
        // LangSys: lookupOrderOffset(0), required(0xFFFF), featCount(4),
        //   featureIndices[4] = [0,1,2,3]
        let mut langsys = Vec::new();
        langsys.extend_from_slice(&0u16.to_be_bytes());
        langsys.extend_from_slice(&0xFFFFu16.to_be_bytes());
        langsys.extend_from_slice(&4u16.to_be_bytes());
        langsys.extend_from_slice(&0u16.to_be_bytes());
        langsys.extend_from_slice(&1u16.to_be_bytes());
        langsys.extend_from_slice(&2u16.to_be_bytes());
        langsys.extend_from_slice(&3u16.to_be_bytes());

        // Script: defaultLangSysOffset, langSysCount(0), then the
        //   DefaultLangSys payload immediately after (its offset = 4).
        let mut script = Vec::new();
        script.extend_from_slice(&4u16.to_be_bytes()); // defaultLangSysOffset
        script.extend_from_slice(&0u16.to_be_bytes()); // langSysCount
        script.extend_from_slice(&langsys);

        // ScriptList: u16 scriptCount, ScriptRecord{ Tag(4), Offset16
        //   scriptOffset } [count]; scriptOffsets relative to
        //   ScriptList start.
        let mut script_list = Vec::new();
        script_list.extend_from_slice(&1u16.to_be_bytes());
        script_list.extend_from_slice(b"arab");
        let script_off: u16 = 2 + 6; // after 1 record
        script_list.extend_from_slice(&script_off.to_be_bytes());
        script_list.extend_from_slice(&script);

        // ----- GSUB header -----
        //
        // u16 major=1, u16 minor=0, Offset16 scriptList, Offset16
        //   featureList, Offset16 lookupList. All offsets are relative
        //   to the GSUB start.
        let header_len = 10u16;
        let script_list_off = header_len;
        let feature_list_off = script_list_off + script_list.len() as u16;
        let lookup_list_off = feature_list_off + feature_list.len() as u16;

        let mut gsub = Vec::new();
        gsub.extend_from_slice(&1u16.to_be_bytes());
        gsub.extend_from_slice(&0u16.to_be_bytes());
        gsub.extend_from_slice(&script_list_off.to_be_bytes());
        gsub.extend_from_slice(&feature_list_off.to_be_bytes());
        gsub.extend_from_slice(&lookup_list_off.to_be_bytes());
        gsub.extend_from_slice(&script_list);
        gsub.extend_from_slice(&feature_list);
        gsub.extend_from_slice(&lookup_list);
        gsub
    }

    #[test]
    fn gsub_header_parses_version_1_0() {
        let bytes = build_feature_tagged_gsub();
        let g = GsubTable::parse(&bytes).expect("v1.0 header parses");
        assert!(g.script_list_off > 0);
        assert!(g.feature_list_off > 0);
        assert!(g.lookup_list_off > 0);
    }

    #[test]
    fn script_list_finds_arab_script() {
        let bytes = build_feature_tagged_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        let feats = g.features_for_script(*b"arab", None);
        assert_eq!(feats.len(), 4, "arab script should expose 4 features");
        // Unknown script → empty.
        let none = g.features_for_script(*b"latn", None);
        assert!(none.is_empty(), "latn isn't in the test ScriptList");
    }

    #[test]
    fn feature_list_returns_init_medi_fina_isol_for_arab() {
        let bytes = build_feature_tagged_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        let feats = g.features_for_script(*b"arab", None);
        let tags: Vec<[u8; 4]> = feats.iter().map(|f| f.tag).collect();
        assert_eq!(
            tags,
            vec![*b"init", *b"medi", *b"fina", *b"isol"],
            "expected the four positional-form tags in declaration order"
        );
        // Each feature points at exactly one lookup, in matching order.
        for (i, f) in feats.iter().enumerate() {
            assert_eq!(f.lookup_indices, vec![i as u16]);
        }
    }

    #[test]
    fn lookup_type_1_format_1_delta_substitution_applies() {
        let bytes = build_feature_tagged_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        // Lookup 0 is Format 1 with delta = +5 over coverage [10].
        assert_eq!(g.apply_lookup_type_1(0, 10), Some(15));
    }

    #[test]
    fn lookup_type_1_format_2_array_substitution_applies() {
        let bytes = build_feature_tagged_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        // Lookup 1 is Format 2 mapping [20,21,22] → [200,201,202].
        assert_eq!(g.apply_lookup_type_1(1, 20), Some(200));
        assert_eq!(g.apply_lookup_type_1(1, 21), Some(201));
        assert_eq!(g.apply_lookup_type_1(1, 22), Some(202));
    }

    #[test]
    fn gsub_apply_returns_none_when_gid_not_in_coverage() {
        let bytes = build_feature_tagged_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        // gid 99 isn't in either coverage table.
        assert_eq!(g.apply_lookup_type_1(0, 99), None);
        assert_eq!(g.apply_lookup_type_1(1, 99), None);
        // Out-of-range lookup index → None (no panic).
        assert_eq!(g.apply_lookup_type_1(123, 10), None);
    }

    #[test]
    fn signed_delta_wraps_modulo_65536() {
        // Build a SingleSubstFormat1 with a NEGATIVE delta covering glyph
        // 5 → 5 + (-3) = 2.
        let mut cov = Vec::new();
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&5u16.to_be_bytes());
        let mut sub = Vec::new();
        sub.extend_from_slice(&1u16.to_be_bytes()); // format
        sub.extend_from_slice(&6u16.to_be_bytes()); // coverageOffset
        sub.extend_from_slice(&(-3i16).to_be_bytes()); // delta
        sub.extend_from_slice(&cov);
        assert_eq!(single_subst_lookup(&sub, 5), Some(2));
        // Wrap: glyph 1 + (-3) = 65534.
        let mut cov2 = Vec::new();
        cov2.extend_from_slice(&1u16.to_be_bytes());
        cov2.extend_from_slice(&1u16.to_be_bytes());
        cov2.extend_from_slice(&1u16.to_be_bytes());
        let mut sub2 = Vec::new();
        sub2.extend_from_slice(&1u16.to_be_bytes());
        sub2.extend_from_slice(&6u16.to_be_bytes());
        sub2.extend_from_slice(&(-3i16).to_be_bytes());
        sub2.extend_from_slice(&cov2);
        assert_eq!(single_subst_lookup(&sub2, 1), Some(65534));
    }

    #[test]
    fn apply_lookup_type_4_consumes_correct_count() {
        // Re-use build_simple_gsub: lookup 0 covers gid 100 + [200,300]
        // -> ligature 999, componentCount=3.
        let (bytes, a, b, c, lig) = build_simple_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        assert_eq!(g.apply_lookup_type_4(0, &[a, b, c]), Some((lig, 3)));
        // Out-of-range lookup index → None.
        assert_eq!(g.apply_lookup_type_4(99, &[a, b, c]), None);
        // Wrong second glyph → None.
        assert_eq!(g.apply_lookup_type_4(0, &[a, b, 555]), None);
        // Empty input → None.
        assert_eq!(g.apply_lookup_type_4(0, &[]), None);
    }

    #[test]
    fn apply_lookup_type_4_skips_non_ligature_lookups() {
        // build_feature_tagged_gsub publishes only LookupType-1 (single
        // substitution) lookups; apply_lookup_type_4 should silently
        // return None for any of them.
        let bytes = build_feature_tagged_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        for li in 0..4 {
            assert_eq!(g.apply_lookup_type_4(li, &[10, 20, 30]), None);
        }
    }

    /// Build a GSUB blob with two lookups:
    /// - Lookup 0 — SingleSubst Format 1 covering gid 5 → +100 (= 105).
    /// - Lookup 1 — ChainContextSubstFormat 1: at gid 10 with backtrack
    ///   [1] and lookahead [99], invoke lookup 0 at sequenceIndex 0
    ///   for input [10] → 110 (single subst delta = +100).
    ///
    /// Wait: lookup 0 covers gid 5, not gid 10. Let's instead make
    /// lookup 0 cover gids 5 AND 10 with delta +100.
    fn build_chain_context_format1_gsub() -> Vec<u8> {
        // SingleSubst Format 1: covers [5, 10], delta = +100.
        let mut cov0 = Vec::new();
        cov0.extend_from_slice(&1u16.to_be_bytes()); // format
        cov0.extend_from_slice(&2u16.to_be_bytes()); // glyphCount
        cov0.extend_from_slice(&5u16.to_be_bytes());
        cov0.extend_from_slice(&10u16.to_be_bytes());
        let mut sub0 = Vec::new();
        sub0.extend_from_slice(&1u16.to_be_bytes()); // format
        sub0.extend_from_slice(&6u16.to_be_bytes()); // coverageOffset
        sub0.extend_from_slice(&100i16.to_be_bytes()); // delta
        sub0.extend_from_slice(&cov0);

        // ChainContextSubstFormat1
        // Layout we want:
        //   header (8 bytes: format + cov_off + setCount + setOffset[1])
        //   coverage covering gid 10
        //   ChainSubRuleSet: count=1, offset to rule
        //   ChainSubRule: bt=[1], in=[10] (count=1, no extra), la=[99],
        //                 substRecords=[(seq=0, lookup=0)]
        let mut cov_in = Vec::new();
        cov_in.extend_from_slice(&1u16.to_be_bytes());
        cov_in.extend_from_slice(&1u16.to_be_bytes());
        cov_in.extend_from_slice(&10u16.to_be_bytes());
        // Rule body
        let mut rule = Vec::new();
        rule.extend_from_slice(&1u16.to_be_bytes()); // backtrackGlyphCount
        rule.extend_from_slice(&1u16.to_be_bytes()); // backtrack[0] = gid 1
        rule.extend_from_slice(&1u16.to_be_bytes()); // inputGlyphCount = 1
                                                     // (no extra inputs)
        rule.extend_from_slice(&1u16.to_be_bytes()); // lookaheadGlyphCount
        rule.extend_from_slice(&99u16.to_be_bytes()); // lookahead[0]
        rule.extend_from_slice(&1u16.to_be_bytes()); // substCount
        rule.extend_from_slice(&0u16.to_be_bytes()); // sequenceIndex
        rule.extend_from_slice(&0u16.to_be_bytes()); // lookupListIndex

        // ChainSubRuleSet
        let rule_set_header_len = 4u16; // count + 1 offset
        let mut rule_set = Vec::new();
        rule_set.extend_from_slice(&1u16.to_be_bytes()); // ruleCount
        rule_set.extend_from_slice(&rule_set_header_len.to_be_bytes()); // offset
        rule_set.extend_from_slice(&rule);

        // Sub-table header
        let header_len = 8u16; // format + cov + setCount + setOffset
        let cov_off = header_len;
        let set_off = cov_off + cov_in.len() as u16;
        let mut sub1 = Vec::new();
        sub1.extend_from_slice(&1u16.to_be_bytes()); // format
        sub1.extend_from_slice(&cov_off.to_be_bytes());
        sub1.extend_from_slice(&1u16.to_be_bytes()); // setCount
        sub1.extend_from_slice(&set_off.to_be_bytes());
        sub1.extend_from_slice(&cov_in);
        sub1.extend_from_slice(&rule_set);

        fn wrap_lookup(lookup_type: u16, sub: &[u8]) -> Vec<u8> {
            let mut out = Vec::new();
            out.extend_from_slice(&lookup_type.to_be_bytes());
            out.extend_from_slice(&0u16.to_be_bytes()); // flag
            out.extend_from_slice(&1u16.to_be_bytes()); // subCount
            out.extend_from_slice(&8u16.to_be_bytes()); // subOffset
            out.extend_from_slice(sub);
            out
        }
        let lookup0 = wrap_lookup(LOOKUP_SINGLE_SUBST, &sub0);
        let lookup1 = wrap_lookup(LOOKUP_CHAIN_CONTEXT_SUBST, &sub1);

        // LookupList
        let lookup_list_header_len = 2 + 2 * 2;
        let mut lookup_list = Vec::new();
        lookup_list.extend_from_slice(&2u16.to_be_bytes());
        let mut running = lookup_list_header_len as u16;
        lookup_list.extend_from_slice(&running.to_be_bytes());
        running += lookup0.len() as u16;
        lookup_list.extend_from_slice(&running.to_be_bytes());
        lookup_list.extend_from_slice(&lookup0);
        lookup_list.extend_from_slice(&lookup1);

        // GSUB header — no script/feature lists.
        let mut gsub = Vec::new();
        gsub.extend_from_slice(&1u16.to_be_bytes()); // major
        gsub.extend_from_slice(&0u16.to_be_bytes()); // minor
        gsub.extend_from_slice(&0u16.to_be_bytes()); // scriptList
        gsub.extend_from_slice(&0u16.to_be_bytes()); // featureList
        gsub.extend_from_slice(&10u16.to_be_bytes()); // lookupList
        gsub.extend_from_slice(&lookup_list);
        gsub
    }

    #[test]
    fn gsub_lookup_type_6_format_1_chained_context_simple_sequence() {
        let bytes = build_chain_context_format1_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        // Run is [1, 10, 99]. Apply chain-context lookup 1 at pos=1.
        // Backtrack [1] matches; input [10] matches; lookahead [99]
        // matches; sub-record (seq=0, lookup=0) → SingleSubst delta+100.
        let out = g.apply_lookup_type_6(1, &[1, 10, 99], 1).unwrap();
        assert_eq!(out, vec![1, 110, 99]);
    }

    #[test]
    fn chain_context_format1_no_match_when_backtrack_differs() {
        let bytes = build_chain_context_format1_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        // Backtrack expects gid 1 immediately before pos. Use gid 2.
        assert_eq!(g.apply_lookup_type_6(1, &[2, 10, 99], 1), None);
        // No backtrack room (pos=0).
        assert_eq!(g.apply_lookup_type_6(1, &[10, 99], 0), None);
        // Wrong lookahead.
        assert_eq!(g.apply_lookup_type_6(1, &[1, 10, 50], 1), None);
        // Out-of-range lookup index → None.
        assert_eq!(g.apply_lookup_type_6(99, &[1, 10, 99], 1), None);
    }

    /// Build a Format-3 chain-context sub-table:
    /// backtrack covers [1], input covers [10], lookahead covers [99],
    /// invoke single-subst lookup 0 (gids [5,10] → +100).
    fn build_chain_context_format3_gsub() -> Vec<u8> {
        // Same SingleSubst lookup 0 as Format-1 build.
        let mut cov_lookup0 = Vec::new();
        cov_lookup0.extend_from_slice(&1u16.to_be_bytes());
        cov_lookup0.extend_from_slice(&2u16.to_be_bytes());
        cov_lookup0.extend_from_slice(&5u16.to_be_bytes());
        cov_lookup0.extend_from_slice(&10u16.to_be_bytes());
        let mut sub0 = Vec::new();
        sub0.extend_from_slice(&1u16.to_be_bytes());
        sub0.extend_from_slice(&6u16.to_be_bytes());
        sub0.extend_from_slice(&100i16.to_be_bytes());
        sub0.extend_from_slice(&cov_lookup0);

        // Three coverages for the chain.
        let mut cov_bt = Vec::new();
        cov_bt.extend_from_slice(&1u16.to_be_bytes());
        cov_bt.extend_from_slice(&1u16.to_be_bytes());
        cov_bt.extend_from_slice(&1u16.to_be_bytes());
        let mut cov_in = Vec::new();
        cov_in.extend_from_slice(&1u16.to_be_bytes());
        cov_in.extend_from_slice(&1u16.to_be_bytes());
        cov_in.extend_from_slice(&10u16.to_be_bytes());
        let mut cov_la = Vec::new();
        cov_la.extend_from_slice(&1u16.to_be_bytes());
        cov_la.extend_from_slice(&1u16.to_be_bytes());
        cov_la.extend_from_slice(&99u16.to_be_bytes());

        // Format-3 sub-table:
        // u16 format=3
        // u16 bt_count=1, Offset16 bt_off[1]
        // u16 in_count=1, Offset16 in_off[1]
        // u16 la_count=1, Offset16 la_off[1]
        // u16 substCount=1, SubstLookupRecord[1] = (0, 0)
        // header = 2 + (2+2) + (2+2) + (2+2) + 2 + 4 = 18 bytes
        let header_len: u16 = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 4;
        let bt_off = header_len;
        let in_off = bt_off + cov_bt.len() as u16;
        let la_off = in_off + cov_in.len() as u16;

        let mut sub1 = Vec::new();
        sub1.extend_from_slice(&3u16.to_be_bytes()); // format
        sub1.extend_from_slice(&1u16.to_be_bytes()); // btCount
        sub1.extend_from_slice(&bt_off.to_be_bytes());
        sub1.extend_from_slice(&1u16.to_be_bytes()); // inCount
        sub1.extend_from_slice(&in_off.to_be_bytes());
        sub1.extend_from_slice(&1u16.to_be_bytes()); // laCount
        sub1.extend_from_slice(&la_off.to_be_bytes());
        sub1.extend_from_slice(&1u16.to_be_bytes()); // substCount
        sub1.extend_from_slice(&0u16.to_be_bytes()); // seqIndex
        sub1.extend_from_slice(&0u16.to_be_bytes()); // lookupIndex
        sub1.extend_from_slice(&cov_bt);
        sub1.extend_from_slice(&cov_in);
        sub1.extend_from_slice(&cov_la);

        fn wrap_lookup(lookup_type: u16, sub: &[u8]) -> Vec<u8> {
            let mut out = Vec::new();
            out.extend_from_slice(&lookup_type.to_be_bytes());
            out.extend_from_slice(&0u16.to_be_bytes());
            out.extend_from_slice(&1u16.to_be_bytes());
            out.extend_from_slice(&8u16.to_be_bytes());
            out.extend_from_slice(sub);
            out
        }
        let lookup0 = wrap_lookup(LOOKUP_SINGLE_SUBST, &sub0);
        let lookup1 = wrap_lookup(LOOKUP_CHAIN_CONTEXT_SUBST, &sub1);

        let lookup_list_header_len = 2 + 2 * 2;
        let mut lookup_list = Vec::new();
        lookup_list.extend_from_slice(&2u16.to_be_bytes());
        let mut running = lookup_list_header_len as u16;
        lookup_list.extend_from_slice(&running.to_be_bytes());
        running += lookup0.len() as u16;
        lookup_list.extend_from_slice(&running.to_be_bytes());
        lookup_list.extend_from_slice(&lookup0);
        lookup_list.extend_from_slice(&lookup1);

        let mut gsub = Vec::new();
        gsub.extend_from_slice(&1u16.to_be_bytes());
        gsub.extend_from_slice(&0u16.to_be_bytes());
        gsub.extend_from_slice(&0u16.to_be_bytes());
        gsub.extend_from_slice(&0u16.to_be_bytes());
        gsub.extend_from_slice(&10u16.to_be_bytes());
        gsub.extend_from_slice(&lookup_list);
        gsub
    }

    #[test]
    fn gsub_lookup_type_6_format_3_coverage_based_chained_context() {
        let bytes = build_chain_context_format3_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        let out = g.apply_lookup_type_6(1, &[1, 10, 99], 1).unwrap();
        assert_eq!(out, vec![1, 110, 99]);
    }

    #[test]
    fn chain_context_format3_no_match_when_window_short() {
        let bytes = build_chain_context_format3_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        // Backtrack required, pos=0 leaves none.
        assert_eq!(g.apply_lookup_type_6(1, &[10, 99], 0), None);
        // No lookahead.
        assert_eq!(g.apply_lookup_type_6(1, &[1, 10], 1), None);
        // Lookahead glyph not covered.
        assert_eq!(g.apply_lookup_type_6(1, &[1, 10, 12], 1), None);
    }

    /// Build a Format-2 (class-based) chain-context sub-table:
    /// - inputClassDef: gid 10 → class 1
    /// - backtrackClassDef: gid 1 → class 2
    /// - lookaheadClassDef: gid 99 → class 3
    /// - rule under set[class=1]: bt=[2], in=[1] (count=1), la=[3],
    ///   subst=(0, 0) → invokes single-subst lookup 0.
    fn build_chain_context_format2_gsub() -> Vec<u8> {
        // SingleSubst lookup 0 — covers gid 10 with delta +100.
        let mut cov_lookup0 = Vec::new();
        cov_lookup0.extend_from_slice(&1u16.to_be_bytes());
        cov_lookup0.extend_from_slice(&1u16.to_be_bytes());
        cov_lookup0.extend_from_slice(&10u16.to_be_bytes());
        let mut sub0 = Vec::new();
        sub0.extend_from_slice(&1u16.to_be_bytes());
        sub0.extend_from_slice(&6u16.to_be_bytes());
        sub0.extend_from_slice(&100i16.to_be_bytes());
        sub0.extend_from_slice(&cov_lookup0);

        // Format-2 sub-table header:
        //   u16 format=2, Offset16 cov, Offset16 btCD, Offset16 inCD,
        //     Offset16 laCD, u16 setCount, Offset16 setOffsets[setCount]
        // Coverage on input[0]: covers gid 10.
        let mut cov_in = Vec::new();
        cov_in.extend_from_slice(&1u16.to_be_bytes());
        cov_in.extend_from_slice(&1u16.to_be_bytes());
        cov_in.extend_from_slice(&10u16.to_be_bytes());

        // ClassDefs (format 1: startGlyph + count + classes[])
        // bt: gid 1 → class 2
        let mut bt_cd = Vec::new();
        bt_cd.extend_from_slice(&1u16.to_be_bytes()); // format
        bt_cd.extend_from_slice(&1u16.to_be_bytes()); // startGlyph
        bt_cd.extend_from_slice(&1u16.to_be_bytes()); // count
        bt_cd.extend_from_slice(&2u16.to_be_bytes()); // class
                                                      // in: gid 10 → class 1
        let mut in_cd = Vec::new();
        in_cd.extend_from_slice(&1u16.to_be_bytes());
        in_cd.extend_from_slice(&10u16.to_be_bytes());
        in_cd.extend_from_slice(&1u16.to_be_bytes());
        in_cd.extend_from_slice(&1u16.to_be_bytes());
        // la: gid 99 → class 3
        let mut la_cd = Vec::new();
        la_cd.extend_from_slice(&1u16.to_be_bytes());
        la_cd.extend_from_slice(&99u16.to_be_bytes());
        la_cd.extend_from_slice(&1u16.to_be_bytes());
        la_cd.extend_from_slice(&3u16.to_be_bytes());

        // ChainSubClassRule:
        //   u16 backtrackGlyphCount=1, u16 bt[1]=2
        //   u16 inputGlyphCount=1, (no extra)
        //   u16 lookaheadGlyphCount=1, u16 la[1]=3
        //   u16 substCount=1, SubstLookupRecord (0,0)
        let mut rule = Vec::new();
        rule.extend_from_slice(&1u16.to_be_bytes()); // bt count
        rule.extend_from_slice(&2u16.to_be_bytes()); // bt class 2
        rule.extend_from_slice(&1u16.to_be_bytes()); // in count
        rule.extend_from_slice(&1u16.to_be_bytes()); // la count
        rule.extend_from_slice(&3u16.to_be_bytes()); // la class 3
        rule.extend_from_slice(&1u16.to_be_bytes()); // substCount
        rule.extend_from_slice(&0u16.to_be_bytes()); // seqIndex
        rule.extend_from_slice(&0u16.to_be_bytes()); // lookupIndex

        // ChainSubClassSet for input class 1:
        //   u16 ruleCount=1, Offset16 ruleOffsets[1]
        let rule_set_header_len = 4u16;
        let mut rule_set = Vec::new();
        rule_set.extend_from_slice(&1u16.to_be_bytes());
        rule_set.extend_from_slice(&rule_set_header_len.to_be_bytes());
        rule_set.extend_from_slice(&rule);

        // setCount = number of distinct input classes including 0. We
        // need entries [0, 1] → setCount=2; offset for class 0 = 0
        // (no rules), offset for class 1 = past header + coverage + 3 CDs.
        // Header layout is:
        //   2 (fmt) + 2 (cov) + 2 (btCD) + 2 (inCD) + 2 (laCD)
        //   + 2 (setCount) + 2*2 (setOffsets[2]) = 16 bytes
        let header_len: u16 = 2 + 2 + 2 + 2 + 2 + 2 + 4;
        let cov_off = header_len;
        let bt_cd_off = cov_off + cov_in.len() as u16;
        let in_cd_off = bt_cd_off + bt_cd.len() as u16;
        let la_cd_off = in_cd_off + in_cd.len() as u16;
        let class1_set_off = la_cd_off + la_cd.len() as u16;

        let mut sub1 = Vec::new();
        sub1.extend_from_slice(&2u16.to_be_bytes()); // format
        sub1.extend_from_slice(&cov_off.to_be_bytes());
        sub1.extend_from_slice(&bt_cd_off.to_be_bytes());
        sub1.extend_from_slice(&in_cd_off.to_be_bytes());
        sub1.extend_from_slice(&la_cd_off.to_be_bytes());
        sub1.extend_from_slice(&2u16.to_be_bytes()); // setCount
        sub1.extend_from_slice(&0u16.to_be_bytes()); // class 0 set offset
        sub1.extend_from_slice(&class1_set_off.to_be_bytes()); // class 1
        sub1.extend_from_slice(&cov_in);
        sub1.extend_from_slice(&bt_cd);
        sub1.extend_from_slice(&in_cd);
        sub1.extend_from_slice(&la_cd);
        sub1.extend_from_slice(&rule_set);

        fn wrap_lookup(lookup_type: u16, sub: &[u8]) -> Vec<u8> {
            let mut out = Vec::new();
            out.extend_from_slice(&lookup_type.to_be_bytes());
            out.extend_from_slice(&0u16.to_be_bytes());
            out.extend_from_slice(&1u16.to_be_bytes());
            out.extend_from_slice(&8u16.to_be_bytes());
            out.extend_from_slice(sub);
            out
        }
        let lookup0 = wrap_lookup(LOOKUP_SINGLE_SUBST, &sub0);
        let lookup1 = wrap_lookup(LOOKUP_CHAIN_CONTEXT_SUBST, &sub1);

        let lookup_list_header_len = 2 + 2 * 2;
        let mut lookup_list = Vec::new();
        lookup_list.extend_from_slice(&2u16.to_be_bytes());
        let mut running = lookup_list_header_len as u16;
        lookup_list.extend_from_slice(&running.to_be_bytes());
        running += lookup0.len() as u16;
        lookup_list.extend_from_slice(&running.to_be_bytes());
        lookup_list.extend_from_slice(&lookup0);
        lookup_list.extend_from_slice(&lookup1);

        let mut gsub = Vec::new();
        gsub.extend_from_slice(&1u16.to_be_bytes());
        gsub.extend_from_slice(&0u16.to_be_bytes());
        gsub.extend_from_slice(&0u16.to_be_bytes());
        gsub.extend_from_slice(&0u16.to_be_bytes());
        gsub.extend_from_slice(&10u16.to_be_bytes());
        gsub.extend_from_slice(&lookup_list);
        gsub
    }

    #[test]
    fn chain_context_format2_class_based_substitutes() {
        let bytes = build_chain_context_format2_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        let out = g.apply_lookup_type_6(1, &[1, 10, 99], 1).unwrap();
        assert_eq!(out, vec![1, 110, 99]);
    }

    #[test]
    fn chain_context_format2_no_match_when_class_differs() {
        let bytes = build_chain_context_format2_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        // Replace gid 1 (class 2) with gid 2 (class 0) in backtrack.
        assert_eq!(g.apply_lookup_type_6(1, &[2, 10, 99], 1), None);
    }

    /// Wrap a single sub-table in a Lookup record.
    fn wrap_lookup_helper(lookup_type: u16, sub: &[u8]) -> Vec<u8> {
        let mut out = Vec::new();
        out.extend_from_slice(&lookup_type.to_be_bytes());
        out.extend_from_slice(&0u16.to_be_bytes()); // flag
        out.extend_from_slice(&1u16.to_be_bytes()); // subTableCount
        out.extend_from_slice(&8u16.to_be_bytes()); // subTableOffset = 8
        out.extend_from_slice(sub);
        out
    }

    /// Build a GSUB blob that holds a single lookup at index 0 of the
    /// given lookup_type, wrapping the supplied sub-table bytes.
    fn build_singleton_gsub(lookup_type: u16, sub: &[u8]) -> Vec<u8> {
        let lookup = wrap_lookup_helper(lookup_type, sub);
        let lookup_list_header_len = 2 + 2; // count + 1 offset
        let mut lookup_list = Vec::new();
        lookup_list.extend_from_slice(&1u16.to_be_bytes());
        lookup_list.extend_from_slice(&(lookup_list_header_len as u16).to_be_bytes());
        lookup_list.extend_from_slice(&lookup);

        let mut gsub = Vec::new();
        gsub.extend_from_slice(&1u16.to_be_bytes()); // major
        gsub.extend_from_slice(&0u16.to_be_bytes()); // minor
        gsub.extend_from_slice(&0u16.to_be_bytes()); // scriptList
        gsub.extend_from_slice(&0u16.to_be_bytes()); // featureList
        gsub.extend_from_slice(&10u16.to_be_bytes()); // lookupList
        gsub.extend_from_slice(&lookup_list);
        gsub
    }

    // ----- LookupType 2: Multiple Substitution -----

    /// MultipleSubstFormat1 with one Sequence: gid 7 → [10, 11, 12].
    fn build_mult_subst_sub() -> Vec<u8> {
        // Sequence: glyphCount=3, [10, 11, 12]
        let mut seq = Vec::new();
        seq.extend_from_slice(&3u16.to_be_bytes());
        seq.extend_from_slice(&10u16.to_be_bytes());
        seq.extend_from_slice(&11u16.to_be_bytes());
        seq.extend_from_slice(&12u16.to_be_bytes());

        // Coverage Format 1 covering gid 7.
        let mut cov = Vec::new();
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&7u16.to_be_bytes());

        // Header: u16 fmt(=1), Off16 cov, u16 seqCount, Off16 seqOffsets[1]
        let header_len = 8u16;
        let cov_off = header_len;
        let seq_off = cov_off + cov.len() as u16;

        let mut sub = Vec::new();
        sub.extend_from_slice(&1u16.to_be_bytes()); // format
        sub.extend_from_slice(&cov_off.to_be_bytes());
        sub.extend_from_slice(&1u16.to_be_bytes()); // seqCount
        sub.extend_from_slice(&seq_off.to_be_bytes());
        sub.extend_from_slice(&cov);
        sub.extend_from_slice(&seq);
        sub
    }

    #[test]
    fn lookup_type_2_expands_one_glyph_into_sequence() {
        let bytes = build_singleton_gsub(LOOKUP_MULTIPLE_SUBST, &build_mult_subst_sub());
        let g = GsubTable::parse(&bytes).unwrap();
        assert_eq!(g.apply_lookup_type_2(0, 7), Some(vec![10, 11, 12]));
    }

    #[test]
    fn lookup_type_2_returns_none_off_coverage() {
        let bytes = build_singleton_gsub(LOOKUP_MULTIPLE_SUBST, &build_mult_subst_sub());
        let g = GsubTable::parse(&bytes).unwrap();
        assert_eq!(g.apply_lookup_type_2(0, 99), None);
        // Wrong lookup index.
        assert_eq!(g.apply_lookup_type_2(99, 7), None);
        // Wrong lookup type.
        let single_bytes = build_singleton_gsub(LOOKUP_SINGLE_SUBST, &build_mult_subst_sub());
        let g2 = GsubTable::parse(&single_bytes).unwrap();
        assert_eq!(g2.apply_lookup_type_2(0, 7), None);
    }

    #[test]
    fn lookup_type_2_zero_glyph_count_means_deletion() {
        // Build a Sequence record with glyphCount = 0 (legal per spec).
        let mut seq = Vec::new();
        seq.extend_from_slice(&0u16.to_be_bytes());
        let mut cov = Vec::new();
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&7u16.to_be_bytes());
        let header_len = 8u16;
        let cov_off = header_len;
        let seq_off = cov_off + cov.len() as u16;
        let mut sub = Vec::new();
        sub.extend_from_slice(&1u16.to_be_bytes());
        sub.extend_from_slice(&cov_off.to_be_bytes());
        sub.extend_from_slice(&1u16.to_be_bytes());
        sub.extend_from_slice(&seq_off.to_be_bytes());
        sub.extend_from_slice(&cov);
        sub.extend_from_slice(&seq);
        let bytes = build_singleton_gsub(LOOKUP_MULTIPLE_SUBST, &sub);
        let g = GsubTable::parse(&bytes).unwrap();
        assert_eq!(g.apply_lookup_type_2(0, 7), Some(Vec::new()));
    }

    // ----- LookupType 3: Alternate Substitution -----

    /// AlternateSubstFormat1 with one AlternateSet: gid 5 → [50, 51, 52].
    fn build_alt_subst_sub() -> Vec<u8> {
        let mut alt_set = Vec::new();
        alt_set.extend_from_slice(&3u16.to_be_bytes()); // glyphCount
        alt_set.extend_from_slice(&50u16.to_be_bytes());
        alt_set.extend_from_slice(&51u16.to_be_bytes());
        alt_set.extend_from_slice(&52u16.to_be_bytes());

        let mut cov = Vec::new();
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&5u16.to_be_bytes());

        let header_len = 8u16;
        let cov_off = header_len;
        let alt_off = cov_off + cov.len() as u16;

        let mut sub = Vec::new();
        sub.extend_from_slice(&1u16.to_be_bytes()); // format
        sub.extend_from_slice(&cov_off.to_be_bytes());
        sub.extend_from_slice(&1u16.to_be_bytes()); // altCount
        sub.extend_from_slice(&alt_off.to_be_bytes());
        sub.extend_from_slice(&cov);
        sub.extend_from_slice(&alt_set);
        sub
    }

    #[test]
    fn lookup_type_3_picks_default_alternate_zero() {
        let bytes = build_singleton_gsub(LOOKUP_ALTERNATE_SUBST, &build_alt_subst_sub());
        let g = GsubTable::parse(&bytes).unwrap();
        assert_eq!(g.apply_lookup_type_3(0, 5, 0), Some(50));
    }

    #[test]
    fn lookup_type_3_picks_indexed_alternates() {
        let bytes = build_singleton_gsub(LOOKUP_ALTERNATE_SUBST, &build_alt_subst_sub());
        let g = GsubTable::parse(&bytes).unwrap();
        assert_eq!(g.apply_lookup_type_3(0, 5, 1), Some(51));
        assert_eq!(g.apply_lookup_type_3(0, 5, 2), Some(52));
    }

    #[test]
    fn lookup_type_3_out_of_range_alternate_returns_none() {
        let bytes = build_singleton_gsub(LOOKUP_ALTERNATE_SUBST, &build_alt_subst_sub());
        let g = GsubTable::parse(&bytes).unwrap();
        // AlternateSet has 3 entries; index 3 is out of range.
        assert_eq!(g.apply_lookup_type_3(0, 5, 3), None);
        // Off-coverage glyph.
        assert_eq!(g.apply_lookup_type_3(0, 99, 0), None);
        // Wrong lookup type silently returns None.
        let other = build_singleton_gsub(LOOKUP_SINGLE_SUBST, &build_alt_subst_sub());
        let g2 = GsubTable::parse(&other).unwrap();
        assert_eq!(g2.apply_lookup_type_3(0, 5, 0), None);
    }

    // ----- LookupType 5: Contextual Substitution -----

    /// Build a SingleSubst Format-1 lookup body: covers [10], delta = +100.
    fn build_singlesubst_for_10_plus_100() -> Vec<u8> {
        let mut cov = Vec::new();
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&10u16.to_be_bytes());
        let mut sub = Vec::new();
        sub.extend_from_slice(&1u16.to_be_bytes()); // format
        sub.extend_from_slice(&6u16.to_be_bytes()); // coverageOffset
        sub.extend_from_slice(&100i16.to_be_bytes()); // delta
        sub.extend_from_slice(&cov);
        sub
    }

    /// Build a SequenceContextFormat1 sub-table: at gid 10 with input
    /// run [10] (count=1), invoke single-subst lookup 0.
    fn build_context_format1_sub() -> Vec<u8> {
        // Coverage covering gid 10.
        let mut cov = Vec::new();
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&10u16.to_be_bytes());

        // Rule: inputGlyphCount=1, no extras, substCount=1, (seq=0,
        // lookup=0)
        let mut rule = Vec::new();
        rule.extend_from_slice(&1u16.to_be_bytes()); // inputGlyphCount
        rule.extend_from_slice(&1u16.to_be_bytes()); // substCount
        rule.extend_from_slice(&0u16.to_be_bytes()); // seqIndex
        rule.extend_from_slice(&0u16.to_be_bytes()); // lookupIndex

        // SubRuleSet: count=1, offset to rule.
        let rule_set_header = 4u16; // count + 1 offset
        let mut rule_set = Vec::new();
        rule_set.extend_from_slice(&1u16.to_be_bytes());
        rule_set.extend_from_slice(&rule_set_header.to_be_bytes());
        rule_set.extend_from_slice(&rule);

        // Header: u16 fmt(=1), Off16 cov, u16 setCount, Off16 setOffsets[1]
        let header_len = 8u16;
        let cov_off = header_len;
        let set_off = cov_off + cov.len() as u16;
        let mut sub = Vec::new();
        sub.extend_from_slice(&1u16.to_be_bytes()); // format
        sub.extend_from_slice(&cov_off.to_be_bytes());
        sub.extend_from_slice(&1u16.to_be_bytes()); // setCount
        sub.extend_from_slice(&set_off.to_be_bytes());
        sub.extend_from_slice(&cov);
        sub.extend_from_slice(&rule_set);
        sub
    }

    fn build_context_lookup_gsub(context_format_sub: Vec<u8>) -> Vec<u8> {
        let lookup0 = wrap_lookup_helper(LOOKUP_SINGLE_SUBST, &build_singlesubst_for_10_plus_100());
        let lookup1 = wrap_lookup_helper(LOOKUP_CONTEXT_SUBST, &context_format_sub);
        let lookup_list_header = 2 + 2 * 2;
        let mut lookup_list = Vec::new();
        lookup_list.extend_from_slice(&2u16.to_be_bytes());
        let mut running = lookup_list_header as u16;
        lookup_list.extend_from_slice(&running.to_be_bytes());
        running += lookup0.len() as u16;
        lookup_list.extend_from_slice(&running.to_be_bytes());
        lookup_list.extend_from_slice(&lookup0);
        lookup_list.extend_from_slice(&lookup1);

        let mut gsub = Vec::new();
        gsub.extend_from_slice(&1u16.to_be_bytes());
        gsub.extend_from_slice(&0u16.to_be_bytes());
        gsub.extend_from_slice(&0u16.to_be_bytes());
        gsub.extend_from_slice(&0u16.to_be_bytes());
        gsub.extend_from_slice(&10u16.to_be_bytes());
        gsub.extend_from_slice(&lookup_list);
        gsub
    }

    #[test]
    fn lookup_type_5_format_1_simple_glyph_context() {
        let bytes = build_context_lookup_gsub(build_context_format1_sub());
        let g = GsubTable::parse(&bytes).unwrap();
        // Run [10] at pos=0: input matches; SingleSubst delta+100.
        let out = g.apply_lookup_type_5(1, &[10], 0).unwrap();
        assert_eq!(out, vec![110]);
        // Off-coverage glyph → no match.
        assert_eq!(g.apply_lookup_type_5(1, &[11], 0), None);
        // Out-of-range lookup → no match.
        assert_eq!(g.apply_lookup_type_5(99, &[10], 0), None);
    }

    /// SequenceContextFormat3: glyphCount=1 covering gid 10, invoke
    /// single-subst lookup 0.
    fn build_context_format3_sub() -> Vec<u8> {
        let mut cov_in = Vec::new();
        cov_in.extend_from_slice(&1u16.to_be_bytes());
        cov_in.extend_from_slice(&1u16.to_be_bytes());
        cov_in.extend_from_slice(&10u16.to_be_bytes());

        // Header: u16 fmt(=3), u16 glyphCount, u16 substCount,
        // Off16 covOffsets[1], SubstLookupRecord rec(=4 bytes)
        let header_len: u16 = 2 + 2 + 2 + 2 + 4;
        let cov_off = header_len;
        let mut sub = Vec::new();
        sub.extend_from_slice(&3u16.to_be_bytes()); // format
        sub.extend_from_slice(&1u16.to_be_bytes()); // glyphCount
        sub.extend_from_slice(&1u16.to_be_bytes()); // substCount
        sub.extend_from_slice(&cov_off.to_be_bytes());
        sub.extend_from_slice(&0u16.to_be_bytes()); // seqIndex
        sub.extend_from_slice(&0u16.to_be_bytes()); // lookupIndex
        sub.extend_from_slice(&cov_in);
        sub
    }

    #[test]
    fn lookup_type_5_format_3_coverage_based_context() {
        let bytes = build_context_lookup_gsub(build_context_format3_sub());
        let g = GsubTable::parse(&bytes).unwrap();
        let out = g.apply_lookup_type_5(1, &[10], 0).unwrap();
        assert_eq!(out, vec![110]);
        // Coverage misses gid 11.
        assert_eq!(g.apply_lookup_type_5(1, &[11], 0), None);
    }

    /// SequenceContextFormat2: ClassDef maps gid 10 → class 1,
    /// rule under class-1 set runs single-subst lookup 0.
    fn build_context_format2_sub() -> Vec<u8> {
        let mut cov_in = Vec::new();
        cov_in.extend_from_slice(&1u16.to_be_bytes());
        cov_in.extend_from_slice(&1u16.to_be_bytes());
        cov_in.extend_from_slice(&10u16.to_be_bytes());

        // ClassDef format 1: startGlyph=10, count=1, classes=[1]
        let mut cd = Vec::new();
        cd.extend_from_slice(&1u16.to_be_bytes());
        cd.extend_from_slice(&10u16.to_be_bytes());
        cd.extend_from_slice(&1u16.to_be_bytes());
        cd.extend_from_slice(&1u16.to_be_bytes());

        // Rule: glyphCount=1, no extras, substCount=1, (seq=0, lookup=0)
        let mut rule = Vec::new();
        rule.extend_from_slice(&1u16.to_be_bytes());
        rule.extend_from_slice(&1u16.to_be_bytes());
        rule.extend_from_slice(&0u16.to_be_bytes());
        rule.extend_from_slice(&0u16.to_be_bytes());

        let rule_set_header = 4u16;
        let mut rule_set = Vec::new();
        rule_set.extend_from_slice(&1u16.to_be_bytes());
        rule_set.extend_from_slice(&rule_set_header.to_be_bytes());
        rule_set.extend_from_slice(&rule);

        // Header: u16 fmt(=2), Off16 cov, Off16 cd, u16 setCount,
        // Off16 setOffsets[2] (class 0 + class 1)
        let header_len: u16 = 2 + 2 + 2 + 2 + 4;
        let cov_off = header_len;
        let cd_off = cov_off + cov_in.len() as u16;
        let class1_set_off = cd_off + cd.len() as u16;

        let mut sub = Vec::new();
        sub.extend_from_slice(&2u16.to_be_bytes()); // format
        sub.extend_from_slice(&cov_off.to_be_bytes());
        sub.extend_from_slice(&cd_off.to_be_bytes());
        sub.extend_from_slice(&2u16.to_be_bytes()); // setCount
        sub.extend_from_slice(&0u16.to_be_bytes()); // class 0 set offset
        sub.extend_from_slice(&class1_set_off.to_be_bytes());
        sub.extend_from_slice(&cov_in);
        sub.extend_from_slice(&cd);
        sub.extend_from_slice(&rule_set);
        sub
    }

    #[test]
    fn lookup_type_5_format_2_class_based_context() {
        let bytes = build_context_lookup_gsub(build_context_format2_sub());
        let g = GsubTable::parse(&bytes).unwrap();
        let out = g.apply_lookup_type_5(1, &[10], 0).unwrap();
        assert_eq!(out, vec![110]);
        // gid 11 isn't covered.
        assert_eq!(g.apply_lookup_type_5(1, &[11], 0), None);
    }

    // ----- LookupType 8: Reverse Chained Context Substitution -----

    /// ReverseChainSingleSubstFormat1:
    /// - input coverage covers [10] → coverage index 0
    /// - backtrack[0] covers [1] (immediately preceding glyph)
    /// - lookahead[0] covers [99]
    /// - substituteGlyphIDs[0] = 200
    fn build_reverse_chain_sub() -> Vec<u8> {
        let mut cov_in = Vec::new();
        cov_in.extend_from_slice(&1u16.to_be_bytes());
        cov_in.extend_from_slice(&1u16.to_be_bytes());
        cov_in.extend_from_slice(&10u16.to_be_bytes());
        let mut cov_bt = Vec::new();
        cov_bt.extend_from_slice(&1u16.to_be_bytes());
        cov_bt.extend_from_slice(&1u16.to_be_bytes());
        cov_bt.extend_from_slice(&1u16.to_be_bytes());
        let mut cov_la = Vec::new();
        cov_la.extend_from_slice(&1u16.to_be_bytes());
        cov_la.extend_from_slice(&1u16.to_be_bytes());
        cov_la.extend_from_slice(&99u16.to_be_bytes());

        // Header: u16 fmt(=1), Off16 cov, u16 btCount, Off16 btCov[1],
        //         u16 laCount, Off16 laCov[1], u16 glyphCount, u16 sub[1]
        // size = 2 + 2 + 2+2 + 2+2 + 2+2 = 16
        let header_len: u16 = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
        let cov_off = header_len;
        let bt_off = cov_off + cov_in.len() as u16;
        let la_off = bt_off + cov_bt.len() as u16;

        let mut sub = Vec::new();
        sub.extend_from_slice(&1u16.to_be_bytes()); // format
        sub.extend_from_slice(&cov_off.to_be_bytes());
        sub.extend_from_slice(&1u16.to_be_bytes()); // btCount
        sub.extend_from_slice(&bt_off.to_be_bytes());
        sub.extend_from_slice(&1u16.to_be_bytes()); // laCount
        sub.extend_from_slice(&la_off.to_be_bytes());
        sub.extend_from_slice(&1u16.to_be_bytes()); // glyphCount
        sub.extend_from_slice(&200u16.to_be_bytes()); // substituteGlyphIDs[0]
        sub.extend_from_slice(&cov_in);
        sub.extend_from_slice(&cov_bt);
        sub.extend_from_slice(&cov_la);
        sub
    }

    #[test]
    fn lookup_type_8_reverse_chain_substitutes_under_context() {
        let bytes = build_singleton_gsub(
            LOOKUP_REVERSE_CHAIN_CONTEXT_SUBST,
            &build_reverse_chain_sub(),
        );
        let g = GsubTable::parse(&bytes).unwrap();
        // Run [1, 10, 99] at pos=1.
        assert_eq!(g.apply_lookup_type_8(0, &[1, 10, 99], 1), Some(200));
    }

    #[test]
    fn lookup_type_8_no_match_when_backtrack_or_lookahead_misses() {
        let bytes = build_singleton_gsub(
            LOOKUP_REVERSE_CHAIN_CONTEXT_SUBST,
            &build_reverse_chain_sub(),
        );
        let g = GsubTable::parse(&bytes).unwrap();
        // Wrong backtrack glyph.
        assert_eq!(g.apply_lookup_type_8(0, &[2, 10, 99], 1), None);
        // Wrong lookahead glyph.
        assert_eq!(g.apply_lookup_type_8(0, &[1, 10, 50], 1), None);
        // No backtrack room (pos=0).
        assert_eq!(g.apply_lookup_type_8(0, &[10, 99], 0), None);
        // No lookahead room.
        assert_eq!(g.apply_lookup_type_8(0, &[1, 10], 1), None);
        // Off-coverage input.
        assert_eq!(g.apply_lookup_type_8(0, &[1, 11, 99], 1), None);
        // Out-of-range lookup index.
        assert_eq!(g.apply_lookup_type_8(99, &[1, 10, 99], 1), None);
    }

    // ----- Nested-lookup dispatch through chained context -----

    /// Verify that a Chain-Context (LT6) record referencing a
    /// LookupType-2 (multiple subst) lookup correctly expands.
    #[test]
    fn chain_context_can_dispatch_nested_lookup_type_2() {
        // Lookup 0: MultipleSubst gid 10 → [10, 99] (split a glyph).
        let mult_sub = {
            let mut seq = Vec::new();
            seq.extend_from_slice(&2u16.to_be_bytes());
            seq.extend_from_slice(&10u16.to_be_bytes());
            seq.extend_from_slice(&99u16.to_be_bytes());
            let mut cov = Vec::new();
            cov.extend_from_slice(&1u16.to_be_bytes());
            cov.extend_from_slice(&1u16.to_be_bytes());
            cov.extend_from_slice(&10u16.to_be_bytes());
            let header_len = 8u16;
            let cov_off = header_len;
            let seq_off = cov_off + cov.len() as u16;
            let mut sub = Vec::new();
            sub.extend_from_slice(&1u16.to_be_bytes());
            sub.extend_from_slice(&cov_off.to_be_bytes());
            sub.extend_from_slice(&1u16.to_be_bytes());
            sub.extend_from_slice(&seq_off.to_be_bytes());
            sub.extend_from_slice(&cov);
            sub.extend_from_slice(&seq);
            sub
        };
        // Lookup 1: ChainContextFormat1 over gid 10 (no bt/la), invokes lookup 0.
        let chain_sub = {
            let mut cov = Vec::new();
            cov.extend_from_slice(&1u16.to_be_bytes());
            cov.extend_from_slice(&1u16.to_be_bytes());
            cov.extend_from_slice(&10u16.to_be_bytes());
            let mut rule = Vec::new();
            rule.extend_from_slice(&0u16.to_be_bytes()); // bt count
            rule.extend_from_slice(&1u16.to_be_bytes()); // input count
            rule.extend_from_slice(&0u16.to_be_bytes()); // la count
            rule.extend_from_slice(&1u16.to_be_bytes()); // substCount
            rule.extend_from_slice(&0u16.to_be_bytes()); // seqIndex
            rule.extend_from_slice(&0u16.to_be_bytes()); // lookupIndex
            let rule_set_header = 4u16;
            let mut rule_set = Vec::new();
            rule_set.extend_from_slice(&1u16.to_be_bytes());
            rule_set.extend_from_slice(&rule_set_header.to_be_bytes());
            rule_set.extend_from_slice(&rule);
            let header_len = 8u16;
            let cov_off = header_len;
            let set_off = cov_off + cov.len() as u16;
            let mut sub = Vec::new();
            sub.extend_from_slice(&1u16.to_be_bytes());
            sub.extend_from_slice(&cov_off.to_be_bytes());
            sub.extend_from_slice(&1u16.to_be_bytes());
            sub.extend_from_slice(&set_off.to_be_bytes());
            sub.extend_from_slice(&cov);
            sub.extend_from_slice(&rule_set);
            sub
        };
        let lookup0 = wrap_lookup_helper(LOOKUP_MULTIPLE_SUBST, &mult_sub);
        let lookup1 = wrap_lookup_helper(LOOKUP_CHAIN_CONTEXT_SUBST, &chain_sub);
        let lookup_list_header = 2 + 2 * 2;
        let mut lookup_list = Vec::new();
        lookup_list.extend_from_slice(&2u16.to_be_bytes());
        let mut running = lookup_list_header as u16;
        lookup_list.extend_from_slice(&running.to_be_bytes());
        running += lookup0.len() as u16;
        lookup_list.extend_from_slice(&running.to_be_bytes());
        lookup_list.extend_from_slice(&lookup0);
        lookup_list.extend_from_slice(&lookup1);

        let mut gsub = Vec::new();
        gsub.extend_from_slice(&1u16.to_be_bytes());
        gsub.extend_from_slice(&0u16.to_be_bytes());
        gsub.extend_from_slice(&0u16.to_be_bytes());
        gsub.extend_from_slice(&0u16.to_be_bytes());
        gsub.extend_from_slice(&10u16.to_be_bytes());
        gsub.extend_from_slice(&lookup_list);

        let g = GsubTable::parse(&gsub).unwrap();
        let out = g.apply_lookup_type_6(1, &[10], 0).unwrap();
        // Multiple subst expands gid 10 → [10, 99].
        assert_eq!(out, vec![10, 99]);
    }
}