oxideav-otf 0.1.3

Pure-Rust OpenType/CFF font parser for the oxideav framework — sfnt + CFF Top/Private DICT + Type 2 charstrings (cubic-Bezier outlines)
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
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
//! `GSUB` — Glyph Substitution Table (header + ScriptList / FeatureList
//! / LookupList walk).
//!
//! Spec: Microsoft / ISO/IEC 14496-22 OpenType `GSUB` table
//! (`docs/text/opentype/otspec-gsub.html`), with the
//! `ScriptList` / `FeatureList` / `LookupList` / `Lookup` /
//! `LookupFlag` structures sourced from
//! `docs/text/opentype/otspec-chapter2-common-layout-tables.html`.
//!
//! Two header versions are defined:
//! ```text
//!   GSUB Header, version 1.0           (10 bytes)
//!   0 / 2 / majorVersion (= 1)
//!   2 / 2 / minorVersion (= 0)
//!   4 / 2 / scriptListOffset    (Offset16, from start of GSUB)
//!   6 / 2 / featureListOffset   (Offset16, from start of GSUB)
//!   8 / 2 / lookupListOffset    (Offset16, from start of GSUB)
//!
//!   GSUB Header, version 1.1           (14 bytes; adds:)
//!  10 / 4 / featureVariationsOffset    (Offset32; may be NULL)
//! ```
//!
//! This module surfaces the header, the three principal sub-tables,
//! and shaping-relevant convenience methods. Lookup-type subtable
//! decoders are added incrementally; what currently lands as a typed
//! view:
//!
//! * **GsubLookupType 1 — Single substitution** (one glyph → one
//!   glyph) via [`SingleSubst`]. Both on-disk formats are decoded
//!   ([`docs/text/opentype/otspec-gsub.html` §"Lookup type 1 subtable:
//!   single substitution"]):
//!   * Format 1 — `(format, coverageOffset, deltaGlyphID)`. Output
//!     glyph ID = `(input + deltaGlyphID) mod 65536`.
//!   * Format 2 — `(format, coverageOffset, glyphCount,
//!     substituteGlyphIDs[glyphCount])`. Output glyph ID =
//!     `substituteGlyphIDs[coverage_index_of(input)]`.
//! * **GsubLookupType 2 — Multiple substitution** (one glyph replaced
//!   by a sequence of glyphs) via [`MultipleSubst`]. The one on-disk
//!   format is decoded
//!   ([`docs/text/opentype/otspec-gsub.html` §"Lookup type 2 subtable:
//!   multiple substitution"]):
//!   * Format 1 — `(format, coverageOffset, sequenceCount,
//!     sequenceOffsets[sequenceCount])`. Each Sequence table is
//!     `(glyphCount, substituteGlyphIDs[glyphCount])`. The covered
//!     input glyph at Coverage Index `i` is replaced by the
//!     `substituteGlyphIDs[]` of the Sequence at offset `i`. Per spec,
//!     `sequenceCount` must equal the Coverage table's glyph count, and
//!     every Sequence's `glyphCount` must be greater than zero (the
//!     spec explicitly prohibits using Multiple substitution as a
//!     deletion).
//! * **GsubLookupType 3 — Alternate substitution** (one glyph replaced
//!   by one of a set of aesthetic alternatives, the choice being a
//!   higher-layer decision) via [`AlternateSubst`]. The one on-disk
//!   format is decoded
//!   ([`docs/text/opentype/otspec-gsub.html` §"Lookup type 3 subtable:
//!   alternate substitution"]):
//!   * Format 1 — `(format, coverageOffset, alternateSetCount,
//!     alternateSetOffsets[alternateSetCount])`. Each AlternateSet is
//!     `(glyphCount, alternateGlyphIDs[glyphCount])`, in arbitrary
//!     order per spec.
//! * **GsubLookupType 4 — Ligature substitution** (a sequence of glyphs
//!   replaced by a single ligature glyph) via [`LigatureSubst`]. The
//!   one on-disk format is decoded
//!   ([`docs/text/opentype/otspec-gsub.html` §"Lookup type 4 subtable:
//!   ligature substitution"]):
//!   * Format 1 — `(format, coverageOffset, ligatureSetCount,
//!     ligatureSetOffsets[ligatureSetCount])`. Each LigatureSet is
//!     `(ligatureCount, ligatureOffsets[ligatureCount])`; each Ligature
//!     is `(ligatureGlyph, componentCount,
//!     componentGlyphIDs[componentCount - 1])`. The first component
//!     glyph is the Coverage entry; the tail of the input sequence is
//!     matched against `componentGlyphIDs[..]` in order, and a full
//!     match yields `ligatureGlyph`. Within a LigatureSet, the array
//!     order is the preference order — longer / preferred ligatures
//!     come first.
//!
//! * **GsubLookupType 7 — Substitution extension** (a 32-bit-offset
//!   indirection wrapping a subtable of any other lookup type) via
//!   [`ExtensionSubst`]. The one on-disk format is decoded
//!   ([`docs/text/opentype/otspec-gsub.html` §"Lookup type 7 subtable:
//!   substitution subtable extension"]):
//!   * Format 1 — `(format, extensionLookupType, extensionOffset)`.
//!     The `Offset32 extensionOffset` (relative to the start of the
//!     ExtensionSubstFormat1 subtable) reaches the real subtable, whose
//!     type is `extensionLookupType` (any GsubLookupType other than 7).
//!     Typed resolvers wrap the types this crate already decodes
//!     (1 / 2 / 3 / 4); the rest are reachable as raw bytes.
//!
//! Other GSUB subtable types (5 Contextual, 6 Chained-context, 8
//! Reverse-chained-single) remain raw sub-slices via
//! [`super::layout::Lookup::subtable_bytes`]; decoding their interiors
//! is deferred to a future round.

use crate::parser::{read_i16, read_u16, read_u32};
use crate::tables::gdef::Coverage;
use crate::tables::layout::{FeatureList, LayoutHeader, Lookup, LookupList, Script, ScriptList};
use crate::Error;

// ---------------------------------------------------------------------------
// GsubLookupType enumeration (otspec-gsub.html §"GsubLookupType enumeration")
// ---------------------------------------------------------------------------

/// `GsubLookupType` value for **single substitution** — one glyph
/// replaced by one glyph (§"Lookup type 1 subtable: single
/// substitution").
pub const GSUB_LOOKUP_TYPE_SINGLE: u16 = 1;
/// `GsubLookupType` value for **multiple substitution** — one glyph
/// replaced by a sequence of glyphs (§"Lookup type 2 subtable:
/// multiple substitution").
pub const GSUB_LOOKUP_TYPE_MULTIPLE: u16 = 2;
/// `GsubLookupType` value for **alternate substitution** — one glyph
/// replaced by one of a list of alternates.
pub const GSUB_LOOKUP_TYPE_ALTERNATE: u16 = 3;
/// `GsubLookupType` value for **ligature substitution** — a sequence
/// of glyphs replaced by a single ligature glyph.
pub const GSUB_LOOKUP_TYPE_LIGATURE: u16 = 4;
/// `GsubLookupType` value for **contextual substitution**.
pub const GSUB_LOOKUP_TYPE_CONTEXT: u16 = 5;
/// `GsubLookupType` value for **chained contexts substitution**.
pub const GSUB_LOOKUP_TYPE_CHAINED_CONTEXT: u16 = 6;
/// `GsubLookupType` value for **substitution extension** — 32-bit
/// offset to one of the other lookup-type formats.
pub const GSUB_LOOKUP_TYPE_EXTENSION: u16 = 7;
/// `GsubLookupType` value for **reverse chaining contextual single
/// substitution**.
pub const GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE: u16 = 8;

// ---------------------------------------------------------------------------
// Lookup type 1: Single substitution (otspec-gsub.html §"Lookup type 1
// subtable: single substitution")
// ---------------------------------------------------------------------------

/// Parsed `SingleSubst` subtable — the GSUB `lookupType = 1` payload.
///
/// Spec: `docs/text/opentype/otspec-gsub.html` §"Lookup type 1
/// subtable: single substitution".
///
/// Two on-disk formats are defined; both carry a Coverage table that
/// names the *input* glyphs:
///
/// * **Format 1** (`SingleSubstFormat1`, 6 bytes): a single
///   `int16 deltaGlyphID` is added (mod 65536) to every covered input
///   glyph to produce the output. The Coverage Index is unused.
///   ```text
///   0 / 2 / format (= 1)
///   2 / 2 / coverageOffset  (Offset16, from start of subtable)
///   4 / 2 / deltaGlyphID    (int16; sum is mod 65536)
///   ```
/// * **Format 2** (`SingleSubstFormat2`): an explicit per-input
///   substitute array indexed by Coverage Index.
///   ```text
///   0 / 2 / format (= 2)
///   2 / 2 / coverageOffset  (Offset16)
///   4 / 2 / glyphCount      (== Coverage.len())
///   6 / 2 / substituteGlyphIDs[glyphCount]
///   ```
///
/// [`Self::substitute`] returns the output glyph (if any) for an input
/// glyph. The parser keeps zero copies — the borrowed byte window
/// covers the whole subtable.
#[derive(Debug, Clone, Copy)]
pub struct SingleSubst<'a> {
    inner: SingleSubstInner<'a>,
}

#[derive(Debug, Clone, Copy)]
enum SingleSubstInner<'a> {
    /// Format 1: a single `deltaGlyphID` applied to every covered glyph.
    Format1 { coverage: Coverage<'a>, delta: i16 },
    /// Format 2: a `glyphCount`-element substitute array indexed by
    /// Coverage Index.
    Format2 {
        coverage: Coverage<'a>,
        /// Raw `substituteGlyphIDs[]` payload (`2 * glyphCount` bytes,
        /// big-endian `u16` each).
        substitutes: &'a [u8],
    },
}

impl<'a> SingleSubst<'a> {
    /// Parse a SingleSubst subtable from a buffer whose first two
    /// bytes are the `format` identifier.
    ///
    /// Validates the format discriminant, the `coverageOffset` window,
    /// and (for format 2) that `glyphCount` matches the Coverage
    /// length and that the trailing array fits inside the supplied
    /// slice.
    pub fn parse(bytes: &'a [u8]) -> Result<Self, Error> {
        let format = read_u16(bytes, 0)?;
        let cov_off = read_u16(bytes, 2)? as usize;
        if cov_off == 0 || cov_off >= bytes.len() {
            return Err(Error::BadStructure(
                "GSUB/SingleSubst: coverageOffset out of range",
            ));
        }
        let coverage = Coverage::parse(&bytes[cov_off..])?;
        match format {
            1 => {
                // 6-byte header: format + coverageOffset + deltaGlyphID.
                let delta = read_i16(bytes, 4)?;
                Ok(Self {
                    inner: SingleSubstInner::Format1 { coverage, delta },
                })
            }
            2 => {
                let glyph_count = read_u16(bytes, 4)? as usize;
                // glyphCount must equal Coverage.len() per the spec:
                // "The substituteGlyphIDs array must contain the same
                // number of glyph indices as the Coverage table".
                if glyph_count != coverage.len() {
                    return Err(Error::BadStructure(
                        "GSUB/SingleSubstFormat2: glyphCount != coverage.len()",
                    ));
                }
                let array_start = 6usize;
                let need = array_start
                    .checked_add(glyph_count.checked_mul(2).ok_or(Error::BadStructure(
                        "GSUB/SingleSubstFormat2 length overflow",
                    ))?)
                    .ok_or(Error::BadStructure(
                        "GSUB/SingleSubstFormat2 length overflow",
                    ))?;
                if bytes.len() < need {
                    return Err(Error::UnexpectedEof);
                }
                Ok(Self {
                    inner: SingleSubstInner::Format2 {
                        coverage,
                        substitutes: &bytes[array_start..need],
                    },
                })
            }
            _ => Err(Error::BadStructure(
                "GSUB/SingleSubst: unknown subtable format",
            )),
        }
    }

    /// Subtable format discriminant (`1` or `2`).
    pub fn format(&self) -> u16 {
        match self.inner {
            SingleSubstInner::Format1 { .. } => 1,
            SingleSubstInner::Format2 { .. } => 2,
        }
    }

    /// The input-side [`Coverage`] table. Glyphs not in this set are
    /// not substituted by this subtable.
    pub fn coverage(&self) -> Coverage<'a> {
        match self.inner {
            SingleSubstInner::Format1 { coverage, .. } => coverage,
            SingleSubstInner::Format2 { coverage, .. } => coverage,
        }
    }

    /// `deltaGlyphID` for a Format 1 subtable, or `None` on Format 2.
    pub fn delta_glyph_id(&self) -> Option<i16> {
        match self.inner {
            SingleSubstInner::Format1 { delta, .. } => Some(delta),
            SingleSubstInner::Format2 { .. } => None,
        }
    }

    /// `glyphCount` (== Coverage.len()) for a Format 2 subtable, or
    /// `None` on Format 1.
    pub fn glyph_count(&self) -> Option<u16> {
        match self.inner {
            SingleSubstInner::Format1 { .. } => None,
            SingleSubstInner::Format2 { substitutes, .. } => Some((substitutes.len() / 2) as u16),
        }
    }

    /// Look up the substitute for `input` — i.e. apply this subtable
    /// as a shaper would.
    ///
    /// Returns `None` when `input` is not covered. Format 1 returns
    /// `(input as i32 + delta as i32) mod 65536` cast back to `u16`
    /// (the spec's "Addition of deltaGlyphID is modulo 65536", "If the
    /// result … is less than zero, add 65536"). Format 2 returns
    /// `substituteGlyphIDs[coverage_index]`.
    pub fn substitute(&self, input: u16) -> Option<u16> {
        match self.inner {
            SingleSubstInner::Format1 { coverage, delta } => {
                coverage.index_of(input)?;
                // Spec: addition is modulo 65536. Sign-extending the
                // i16 delta to i32 and reducing mod 2**16 implements
                // the "if result < 0, add 65536" wrap-around for free.
                let sum = (input as i32 + delta as i32).rem_euclid(65536);
                Some(sum as u16)
            }
            SingleSubstInner::Format2 {
                coverage,
                substitutes,
            } => {
                let idx = coverage.index_of(input)? as usize;
                let off = idx.checked_mul(2).filter(|&o| o + 2 <= substitutes.len())?;
                Some(u16::from_be_bytes([substitutes[off], substitutes[off + 1]]))
            }
        }
    }

    /// Iterate over every `(input_glyph, output_glyph)` pair this
    /// subtable rewrites, in ascending input-glyph order.
    pub fn iter(&self) -> SingleSubstIter<'a> {
        SingleSubstIter {
            cov: self.coverage().iter(),
            sub: self.inner,
        }
    }
}

/// Iterator yielded by [`SingleSubst::iter`].
#[derive(Debug, Clone)]
pub struct SingleSubstIter<'a> {
    cov: crate::tables::gdef::CoverageIter<'a>,
    sub: SingleSubstInner<'a>,
}

impl<'a> Iterator for SingleSubstIter<'a> {
    type Item = (u16, u16);
    fn next(&mut self) -> Option<Self::Item> {
        let (g, idx) = self.cov.next()?;
        let out = match self.sub {
            SingleSubstInner::Format1 { delta, .. } => {
                ((g as i32 + delta as i32).rem_euclid(65536)) as u16
            }
            SingleSubstInner::Format2 { substitutes, .. } => {
                let off = (idx as usize).checked_mul(2)?;
                if off + 2 > substitutes.len() {
                    return None;
                }
                u16::from_be_bytes([substitutes[off], substitutes[off + 1]])
            }
        };
        Some((g, out))
    }
}

// ---------------------------------------------------------------------------
// Lookup type 2: Multiple substitution (otspec-gsub.html §"Lookup type 2
// subtable: multiple substitution")
// ---------------------------------------------------------------------------

/// Parsed `MultipleSubst` subtable — the GSUB `lookupType = 2` payload.
///
/// Spec: `docs/text/opentype/otspec-gsub.html` §"Lookup type 2 subtable:
/// multiple substitution".
///
/// One on-disk format is defined. The Coverage table names the input
/// glyphs; each covered glyph maps (by Coverage Index) to a
/// per-input-glyph `Sequence` table that carries the output glyph
/// sequence.
///
/// ```text
/// MultipleSubstFormat1 subtable
///   0 / 2 / format = 1
///   2 / 2 / coverageOffset       (Offset16, from start of subtable)
///   4 / 2 / sequenceCount        (== Coverage.len())
///   6 / 2 * n / sequenceOffsets[sequenceCount]
///                                 (Offset16, from start of subtable,
///                                  ordered by Coverage index)
///
/// Sequence table
///   0 / 2 / glyphCount           (must be > 0; the spec prohibits
///                                 using MultipleSubst as a deletion)
///   2 / 2 * n / substituteGlyphIDs[glyphCount]
/// ```
///
/// [`Self::substitute`] returns the replacement glyph sequence (if any)
/// for an input glyph as a borrowed slice of `u16`-valued bytes; use
/// [`Self::substitute_iter`] for an iterator over the typed `u16`
/// outputs.
#[derive(Debug, Clone, Copy)]
pub struct MultipleSubst<'a> {
    /// Raw subtable bytes (offsets in the on-disk records are relative
    /// to this buffer's start).
    bytes: &'a [u8],
    coverage: Coverage<'a>,
    /// Slice of the `sequenceOffsets[]` array (2 bytes per offset).
    seq_offsets: &'a [u8],
}

impl<'a> MultipleSubst<'a> {
    /// Parse a MultipleSubst subtable from a buffer whose first two
    /// bytes are the `format` identifier.
    ///
    /// Validates the format discriminant (only `1` is defined), the
    /// `coverageOffset` window, that `sequenceCount` equals the
    /// Coverage length (spec: "the sequenceOffsets array … must contain
    /// the same number of offsets as the Coverage table"), and that the
    /// trailing `sequenceOffsets[]` array fits inside the supplied
    /// slice. Per-Sequence payloads are validated lazily —
    /// [`Self::sequence`] re-validates on each call so a malformed
    /// inner record can't poison the top-level view.
    pub fn parse(bytes: &'a [u8]) -> Result<Self, Error> {
        let format = read_u16(bytes, 0)?;
        if format != 1 {
            return Err(Error::BadStructure(
                "GSUB/MultipleSubst: unknown subtable format",
            ));
        }
        let cov_off = read_u16(bytes, 2)? as usize;
        if cov_off == 0 || cov_off >= bytes.len() {
            return Err(Error::BadStructure(
                "GSUB/MultipleSubst: coverageOffset out of range",
            ));
        }
        let coverage = Coverage::parse(&bytes[cov_off..])?;
        let seq_count = read_u16(bytes, 4)? as usize;
        // Spec: "the sequenceOffsets array … must contain the same
        // number of offsets as the Coverage table."
        if seq_count != coverage.len() {
            return Err(Error::BadStructure(
                "GSUB/MultipleSubst: sequenceCount != coverage.len()",
            ));
        }
        let array_start = 6usize;
        let need = array_start
            .checked_add(
                seq_count
                    .checked_mul(2)
                    .ok_or(Error::BadStructure("GSUB/MultipleSubst length overflow"))?,
            )
            .ok_or(Error::BadStructure("GSUB/MultipleSubst length overflow"))?;
        if bytes.len() < need {
            return Err(Error::UnexpectedEof);
        }
        Ok(Self {
            bytes,
            coverage,
            seq_offsets: &bytes[array_start..need],
        })
    }

    /// Subtable format discriminant (always `1`).
    pub fn format(&self) -> u16 {
        1
    }

    /// The input-side [`Coverage`] table. Each covered glyph maps (by
    /// Coverage Index) to a per-input-glyph [`Sequence`].
    pub fn coverage(&self) -> Coverage<'a> {
        self.coverage
    }

    /// `sequenceCount` — number of Sequence tables. Equal to
    /// `coverage().len()` by spec invariant.
    pub fn sequence_count(&self) -> u16 {
        (self.seq_offsets.len() / 2) as u16
    }

    /// Borrow the [`Sequence`] at the given Coverage index. Returns
    /// `None` for an out-of-range index, `Some(Err(...))` when the
    /// referenced bytes are malformed or violate the spec's
    /// "glyphCount > 0" rule.
    pub fn sequence(&self, seq_i: u16) -> Option<Result<Sequence<'a>, Error>> {
        let off2 = (seq_i as usize).checked_mul(2)?;
        if off2 + 2 > self.seq_offsets.len() {
            return None;
        }
        let off = u16::from_be_bytes([self.seq_offsets[off2], self.seq_offsets[off2 + 1]]) as usize;
        if off == 0 || off >= self.bytes.len() {
            return Some(Err(Error::BadStructure(
                "GSUB/MultipleSubst: sequenceOffset out of range",
            )));
        }
        Some(Sequence::parse(&self.bytes[off..]))
    }

    /// Apply this subtable as a shaper would.
    ///
    /// Returns `Some(sequence)` when `input` is in [`Self::coverage`];
    /// the returned [`Sequence`] carries the substitute glyph sequence.
    /// Returns `None` when `input` is uncovered or the per-input
    /// Sequence bytes are unreachable / malformed (the typed accessor
    /// [`Self::sequence`] is the diagnosis path for the latter).
    pub fn substitute(&self, input: u16) -> Option<Sequence<'a>> {
        let i = self.coverage.index_of(input)?;
        self.sequence(i)?.ok()
    }

    /// Iterate the `(input_glyph, Sequence)` pairs in this subtable in
    /// ascending Coverage order. Malformed Sequence references are
    /// surfaced as `Err`.
    pub fn iter(&self) -> MultipleSubstIter<'a> {
        MultipleSubstIter {
            cov: self.coverage.iter(),
            outer: *self,
        }
    }
}

/// Iterator yielded by [`MultipleSubst::iter`].
#[derive(Debug, Clone)]
pub struct MultipleSubstIter<'a> {
    cov: crate::tables::gdef::CoverageIter<'a>,
    outer: MultipleSubst<'a>,
}

impl<'a> Iterator for MultipleSubstIter<'a> {
    type Item = (u16, Result<Sequence<'a>, Error>);
    fn next(&mut self) -> Option<Self::Item> {
        let (g, idx) = self.cov.next()?;
        let seq = self.outer.sequence(idx)?;
        Some((g, seq))
    }
}

/// Parsed `Sequence` table — a count + an array of output glyph IDs
/// (the substitute glyph sequence for one covered input glyph).
///
/// The on-disk record is `(glyphCount,
/// substituteGlyphIDs[glyphCount])`. Per spec, `glyphCount` "must
/// always be greater than 0" — the prohibition against using
/// MultipleSubst as a deletion. [`Sequence::parse`] enforces the rule;
/// a zero `glyphCount` surfaces as `Error::BadStructure`.
#[derive(Debug, Clone, Copy)]
pub struct Sequence<'a> {
    /// Raw `substituteGlyphIDs[]` payload — `2 * glyphCount` bytes,
    /// big-endian `u16` per entry.
    glyphs: &'a [u8],
}

impl<'a> Sequence<'a> {
    /// Parse a Sequence table from a buffer whose first two bytes are
    /// `glyphCount`.
    pub fn parse(bytes: &'a [u8]) -> Result<Self, Error> {
        let count = read_u16(bytes, 0)? as usize;
        if count == 0 {
            // Spec: "The use of multiple substitution for deletion of
            // an input glyph is prohibited. The glyphCount value must
            // always be greater than 0."
            return Err(Error::BadStructure(
                "GSUB/Sequence: glyphCount must be >= 1",
            ));
        }
        let array_start = 2usize;
        let need = array_start
            .checked_add(
                count
                    .checked_mul(2)
                    .ok_or(Error::BadStructure("GSUB/Sequence length overflow"))?,
            )
            .ok_or(Error::BadStructure("GSUB/Sequence length overflow"))?;
        if bytes.len() < need {
            return Err(Error::UnexpectedEof);
        }
        Ok(Self {
            glyphs: &bytes[array_start..need],
        })
    }

    /// `glyphCount` — number of glyph IDs in the substitute sequence.
    /// Always >= 1 per spec.
    pub fn glyph_count(&self) -> u16 {
        (self.glyphs.len() / 2) as u16
    }

    /// The substitute glyph at output index `i` (`0 .. glyphCount`).
    pub fn glyph(&self, i: u16) -> Option<u16> {
        let off = (i as usize).checked_mul(2)?;
        if off + 2 > self.glyphs.len() {
            return None;
        }
        Some(u16::from_be_bytes([self.glyphs[off], self.glyphs[off + 1]]))
    }

    /// Iterator over every substitute glyph in output order.
    pub fn glyphs(&self) -> SequenceGlyphIter<'a> {
        SequenceGlyphIter {
            bytes: self.glyphs,
            pos: 0,
        }
    }
}

/// Iterator over a [`Sequence`]'s `substituteGlyphIDs[]` in output
/// order.
#[derive(Debug, Clone)]
pub struct SequenceGlyphIter<'a> {
    bytes: &'a [u8],
    pos: usize,
}

impl<'a> Iterator for SequenceGlyphIter<'a> {
    type Item = u16;
    fn next(&mut self) -> Option<Self::Item> {
        if self.pos + 2 > self.bytes.len() {
            return None;
        }
        let g = u16::from_be_bytes([self.bytes[self.pos], self.bytes[self.pos + 1]]);
        self.pos += 2;
        Some(g)
    }
}

// ---------------------------------------------------------------------------
// Lookup type 3: Alternate substitution (otspec-gsub.html §"Lookup type 3
// subtable: alternate substitution")
// ---------------------------------------------------------------------------

/// Parsed `AlternateSubst` subtable — the GSUB `lookupType = 3` payload.
///
/// Spec: `docs/text/opentype/otspec-gsub.html` §"Lookup type 3 subtable:
/// alternate substitution".
///
/// An alternate substitution identifies any number of functionally
/// equivalent but different-looking forms (aesthetic alternatives) of a
/// glyph. The Coverage table names the input glyphs; each covered glyph
/// maps (by Coverage Index) to an `AlternateSet` table that lists the
/// alternative glyph IDs the client may choose from. Per spec, the
/// alternatives "can be in any order in the array" — the choice of which
/// alternate to use is a higher-level (feature / UI) decision and is not
/// encoded here.
///
/// One on-disk format is defined.
///
/// ```text
/// AlternateSubstFormat1 subtable
///   0 / 2 / format = 1
///   2 / 2 / coverageOffset       (Offset16, from start of subtable)
///   4 / 2 / alternateSetCount    (== Coverage.len())
///   6 / 2 * n / alternateSetOffsets[alternateSetCount]
///                                 (Offset16, from start of subtable,
///                                  ordered by Coverage index)
///
/// AlternateSet table
///   0 / 2 / glyphCount
///   2 / 2 * n / alternateGlyphIDs[glyphCount]  (arbitrary order)
/// ```
#[derive(Debug, Clone, Copy)]
pub struct AlternateSubst<'a> {
    /// Raw subtable bytes (offsets in the on-disk records are relative
    /// to this buffer's start).
    bytes: &'a [u8],
    coverage: Coverage<'a>,
    /// Slice of the `alternateSetOffsets[]` array (2 bytes per offset).
    set_offsets: &'a [u8],
}

impl<'a> AlternateSubst<'a> {
    /// Parse an AlternateSubst subtable from a buffer whose first two
    /// bytes are the `format` identifier.
    ///
    /// Validates the format discriminant (only `1` is defined), the
    /// `coverageOffset` window, that `alternateSetCount` equals the
    /// Coverage length (the `alternateSetOffsets` array is "ordered by
    /// Coverage index", so the two counts must agree), and that the
    /// trailing `alternateSetOffsets[]` array fits inside the supplied
    /// slice. Per-AlternateSet payloads are validated lazily —
    /// [`Self::alternate_set`] re-validates on each call so a malformed
    /// inner record can't poison the top-level view.
    pub fn parse(bytes: &'a [u8]) -> Result<Self, Error> {
        let format = read_u16(bytes, 0)?;
        if format != 1 {
            return Err(Error::BadStructure(
                "GSUB/AlternateSubst: unknown subtable format",
            ));
        }
        let cov_off = read_u16(bytes, 2)? as usize;
        if cov_off == 0 || cov_off >= bytes.len() {
            return Err(Error::BadStructure(
                "GSUB/AlternateSubst: coverageOffset out of range",
            ));
        }
        let coverage = Coverage::parse(&bytes[cov_off..])?;
        let set_count = read_u16(bytes, 4)? as usize;
        // Spec: alternateSetOffsets are "ordered by Coverage index", so
        // the array must carry exactly one offset per covered glyph.
        if set_count != coverage.len() {
            return Err(Error::BadStructure(
                "GSUB/AlternateSubst: alternateSetCount != coverage.len()",
            ));
        }
        let array_start = 6usize;
        let need = array_start
            .checked_add(
                set_count
                    .checked_mul(2)
                    .ok_or(Error::BadStructure("GSUB/AlternateSubst length overflow"))?,
            )
            .ok_or(Error::BadStructure("GSUB/AlternateSubst length overflow"))?;
        if bytes.len() < need {
            return Err(Error::UnexpectedEof);
        }
        Ok(Self {
            bytes,
            coverage,
            set_offsets: &bytes[array_start..need],
        })
    }

    /// Subtable format discriminant (always `1`).
    pub fn format(&self) -> u16 {
        1
    }

    /// The input-side [`Coverage`] table. Each covered glyph maps (by
    /// Coverage Index) to a per-input-glyph [`AlternateSet`].
    pub fn coverage(&self) -> Coverage<'a> {
        self.coverage
    }

    /// `alternateSetCount` — number of AlternateSet tables. Equal to
    /// `coverage().len()` by spec invariant.
    pub fn alternate_set_count(&self) -> u16 {
        (self.set_offsets.len() / 2) as u16
    }

    /// Borrow the [`AlternateSet`] at the given Coverage index. Returns
    /// `None` for an out-of-range index, `Some(Err(...))` when the
    /// referenced bytes are malformed.
    pub fn alternate_set(&self, set_i: u16) -> Option<Result<AlternateSet<'a>, Error>> {
        let off2 = (set_i as usize).checked_mul(2)?;
        if off2 + 2 > self.set_offsets.len() {
            return None;
        }
        let off = u16::from_be_bytes([self.set_offsets[off2], self.set_offsets[off2 + 1]]) as usize;
        if off == 0 || off >= self.bytes.len() {
            return Some(Err(Error::BadStructure(
                "GSUB/AlternateSubst: alternateSetOffset out of range",
            )));
        }
        Some(AlternateSet::parse(&self.bytes[off..]))
    }

    /// Apply this subtable as a shaper would.
    ///
    /// Returns `Some(alternate_set)` when `input` is in
    /// [`Self::coverage`]; the returned [`AlternateSet`] carries the
    /// alternative glyph IDs from which a client picks. Returns `None`
    /// when `input` is uncovered or the per-input AlternateSet bytes are
    /// unreachable / malformed (the typed accessor
    /// [`Self::alternate_set`] is the diagnosis path for the latter).
    ///
    /// Note: this does **not** itself choose an alternate — the spec
    /// leaves alternate selection to a higher layer ("the client could
    /// use the default glyph or substitute any of the alternatives").
    pub fn substitute(&self, input: u16) -> Option<AlternateSet<'a>> {
        let i = self.coverage.index_of(input)?;
        self.alternate_set(i)?.ok()
    }

    /// Iterate the `(input_glyph, AlternateSet)` pairs in this subtable
    /// in ascending Coverage order. Malformed AlternateSet references are
    /// surfaced as `Err`.
    pub fn iter(&self) -> AlternateSubstIter<'a> {
        AlternateSubstIter {
            cov: self.coverage.iter(),
            outer: *self,
        }
    }
}

/// Iterator yielded by [`AlternateSubst::iter`].
#[derive(Debug, Clone)]
pub struct AlternateSubstIter<'a> {
    cov: crate::tables::gdef::CoverageIter<'a>,
    outer: AlternateSubst<'a>,
}

impl<'a> Iterator for AlternateSubstIter<'a> {
    type Item = (u16, Result<AlternateSet<'a>, Error>);
    fn next(&mut self) -> Option<Self::Item> {
        let (g, idx) = self.cov.next()?;
        let set = self.outer.alternate_set(idx)?;
        Some((g, set))
    }
}

/// Parsed `AlternateSet` table — a count + an array of alternate glyph
/// IDs (the aesthetic alternatives for one covered input glyph).
///
/// The on-disk record is `(glyphCount, alternateGlyphIDs[glyphCount])`.
/// Per spec the alternates are "in arbitrary order"; the spec sets no
/// lower bound on `glyphCount`, so an empty AlternateSet (no alternates)
/// is accepted rather than rejected — it simply yields zero choices.
#[derive(Debug, Clone, Copy)]
pub struct AlternateSet<'a> {
    /// Raw `alternateGlyphIDs[]` payload — `2 * glyphCount` bytes,
    /// big-endian `u16` per entry.
    glyphs: &'a [u8],
}

impl<'a> AlternateSet<'a> {
    /// Parse an AlternateSet table from a buffer whose first two bytes
    /// are `glyphCount`.
    pub fn parse(bytes: &'a [u8]) -> Result<Self, Error> {
        let count = read_u16(bytes, 0)? as usize;
        let array_start = 2usize;
        let need = array_start
            .checked_add(
                count
                    .checked_mul(2)
                    .ok_or(Error::BadStructure("GSUB/AlternateSet length overflow"))?,
            )
            .ok_or(Error::BadStructure("GSUB/AlternateSet length overflow"))?;
        if bytes.len() < need {
            return Err(Error::UnexpectedEof);
        }
        Ok(Self {
            glyphs: &bytes[array_start..need],
        })
    }

    /// `glyphCount` — number of alternate glyph IDs.
    pub fn glyph_count(&self) -> u16 {
        (self.glyphs.len() / 2) as u16
    }

    /// The alternate glyph at index `i` (`0 .. glyphCount`). The order is
    /// arbitrary per spec — index `0` is not privileged.
    pub fn glyph(&self, i: u16) -> Option<u16> {
        let off = (i as usize).checked_mul(2)?;
        if off + 2 > self.glyphs.len() {
            return None;
        }
        Some(u16::from_be_bytes([self.glyphs[off], self.glyphs[off + 1]]))
    }

    /// Iterator over every alternate glyph in on-disk (arbitrary) order.
    pub fn glyphs(&self) -> AlternateGlyphIter<'a> {
        AlternateGlyphIter {
            bytes: self.glyphs,
            pos: 0,
        }
    }
}

/// Iterator over an [`AlternateSet`]'s `alternateGlyphIDs[]` in on-disk
/// (arbitrary) order.
#[derive(Debug, Clone)]
pub struct AlternateGlyphIter<'a> {
    bytes: &'a [u8],
    pos: usize,
}

impl<'a> Iterator for AlternateGlyphIter<'a> {
    type Item = u16;
    fn next(&mut self) -> Option<Self::Item> {
        if self.pos + 2 > self.bytes.len() {
            return None;
        }
        let g = u16::from_be_bytes([self.bytes[self.pos], self.bytes[self.pos + 1]]);
        self.pos += 2;
        Some(g)
    }
}

// ---------------------------------------------------------------------------
// Lookup type 4: Ligature substitution (otspec-gsub.html §"Lookup type 4
// subtable: ligature substitution")
// ---------------------------------------------------------------------------

/// Parsed `LigatureSubst` subtable — the GSUB `lookupType = 4` payload.
///
/// Spec: `docs/text/opentype/otspec-gsub.html` §"Lookup type 4 subtable:
/// ligature substitution".
///
/// On-disk layout (one format defined):
///
/// ```text
/// LigatureSubstFormat1 subtable
///   0 / 2 / format = 1
///   2 / 2 / coverageOffset       (Offset16, from start of subtable)
///   4 / 2 / ligatureSetCount
///   6 / 2 * n / ligatureSetOffsets[ligatureSetCount]
///                                 (Offset16, from start of subtable,
///                                  ordered by Coverage index)
///
/// LigatureSet table
///   0 / 2 / ligatureCount
///   2 / 2 * n / ligatureOffsets[ligatureCount]
///                                 (Offset16, from start of LigatureSet,
///                                  ordered by preference; longer /
///                                  preferred ligatures first)
///
/// Ligature table
///   0 / 2 / ligatureGlyph
///   2 / 2 / componentCount        (total components incl. the first)
///   4 / 2 * (componentCount - 1) / componentGlyphIDs[componentCount - 1]
/// ```
///
/// The Coverage table names the **first** component glyph of every
/// ligature in the subtable; the tail components live in the per-Ligature
/// `componentGlyphIDs[]` array, starting from the second component
/// (input glyph sequence index = 1). Ligature lookup is therefore
/// driven by the first glyph: an input sequence whose first glyph is
/// in Coverage selects the LigatureSet at that Coverage Index, then
/// each Ligature inside the set is tried in array order and the first
/// whose `componentGlyphIDs[..]` matches the input tail is the result.
///
/// All offsets are validated at parse time. [`Self::substitute`]
/// applies the spec's "first-match wins" rule against an input slice
/// and returns `(ligatureGlyph, componentCount)` for the matching
/// Ligature, or `None` if no Ligature in the selected LigatureSet
/// matches. The component count tells the caller how many input glyphs
/// the ligature consumed (the first glyph + `componentCount - 1` tail
/// glyphs).
#[derive(Debug, Clone, Copy)]
pub struct LigatureSubst<'a> {
    /// Raw subtable bytes (offsets in the on-disk records are relative
    /// to this buffer's start).
    bytes: &'a [u8],
    coverage: Coverage<'a>,
    /// Slice of the `ligatureSetOffsets[]` array (2 bytes per offset).
    set_offsets: &'a [u8],
}

impl<'a> LigatureSubst<'a> {
    /// Parse a LigatureSubst subtable from a buffer whose first two
    /// bytes are the `format` identifier.
    ///
    /// Validates the format discriminant (only `1` is defined), the
    /// `coverageOffset` window, and the trailing `ligatureSetOffsets[]`
    /// array length. The per-LigatureSet and per-Ligature payloads
    /// themselves are validated lazily — [`Self::ligature_set`] and
    /// [`Self::ligature`] re-validate on each call so a malformed
    /// inner record can't poison the top-level view.
    pub fn parse(bytes: &'a [u8]) -> Result<Self, Error> {
        let format = read_u16(bytes, 0)?;
        if format != 1 {
            return Err(Error::BadStructure(
                "GSUB/LigatureSubst: unknown subtable format",
            ));
        }
        let cov_off = read_u16(bytes, 2)? as usize;
        if cov_off == 0 || cov_off >= bytes.len() {
            return Err(Error::BadStructure(
                "GSUB/LigatureSubst: coverageOffset out of range",
            ));
        }
        let coverage = Coverage::parse(&bytes[cov_off..])?;
        let set_count = read_u16(bytes, 4)? as usize;
        let array_start = 6usize;
        let need = array_start
            .checked_add(
                set_count
                    .checked_mul(2)
                    .ok_or(Error::BadStructure("GSUB/LigatureSubst length overflow"))?,
            )
            .ok_or(Error::BadStructure("GSUB/LigatureSubst length overflow"))?;
        if bytes.len() < need {
            return Err(Error::UnexpectedEof);
        }
        Ok(Self {
            bytes,
            coverage,
            set_offsets: &bytes[array_start..need],
        })
    }

    /// Subtable format discriminant (always `1`).
    pub fn format(&self) -> u16 {
        1
    }

    /// The input-side [`Coverage`] table. Each covered glyph is the
    /// **first** component of every ligature in the corresponding
    /// LigatureSet.
    pub fn coverage(&self) -> Coverage<'a> {
        self.coverage
    }

    /// `ligatureSetCount` — number of LigatureSet tables.
    pub fn ligature_set_count(&self) -> u16 {
        (self.set_offsets.len() / 2) as u16
    }

    /// Borrow the [`LigatureSet`] at the given Coverage index. Returns
    /// `None` for an out-of-range index, `Some(Err(...))` when the
    /// referenced bytes are malformed.
    pub fn ligature_set(&self, set_i: u16) -> Option<Result<LigatureSet<'a>, Error>> {
        let off2 = (set_i as usize).checked_mul(2)?;
        if off2 + 2 > self.set_offsets.len() {
            return None;
        }
        let off = u16::from_be_bytes([self.set_offsets[off2], self.set_offsets[off2 + 1]]) as usize;
        if off == 0 || off >= self.bytes.len() {
            return Some(Err(Error::BadStructure(
                "GSUB/LigatureSubst: ligatureSetOffset out of range",
            )));
        }
        Some(LigatureSet::parse(&self.bytes[off..]))
    }

    /// Apply this subtable as a shaper would.
    ///
    /// `input` is the current glyph sequence starting at the position
    /// the shaper is trying to ligate. `input[0]` must be in
    /// [`Self::coverage`]; the tail `input[1..]` is matched against
    /// each Ligature's `componentGlyphIDs[]` in array order.
    ///
    /// Returns `Some((ligature_glyph, component_count))` for the first
    /// matching Ligature — `component_count` is the total number of
    /// input glyphs consumed (including `input[0]`). Returns `None`
    /// when `input` is empty, when `input[0]` is uncovered, or when no
    /// Ligature in the selected LigatureSet matches the input tail.
    ///
    /// Per the spec, "the order in the Ligature offset array defines
    /// the preference for using the ligatures" — first-match wins,
    /// even if a later Ligature in the set would also match a (shorter)
    /// prefix.
    pub fn substitute(&self, input: &[u16]) -> Option<(u16, u16)> {
        let first = *input.first()?;
        let set_i = self.coverage.index_of(first)?;
        let set = self.ligature_set(set_i)?.ok()?;
        for j in 0..set.ligature_count() {
            let lig = set.ligature(j)?.ok()?;
            let comp_count = lig.component_count() as usize;
            if comp_count == 0 {
                // Spec says componentCount is the total count *including*
                // the first; zero is malformed. Skip silently rather
                // than error: this is shaper-path code, not parse-path.
                continue;
            }
            if comp_count > input.len() {
                continue;
            }
            // The first component is the Coverage entry; we already
            // matched it. Match the tail.
            let mut ok = true;
            for k in 0..(comp_count - 1) {
                let want = lig.component_glyph(k as u16)?;
                if want != input[k + 1] {
                    ok = false;
                    break;
                }
            }
            if ok {
                return Some((lig.ligature_glyph(), comp_count as u16));
            }
        }
        None
    }

    /// Iterate the `(coverage_glyph, LigatureSet)` pairs in this
    /// subtable in ascending Coverage order. Malformed LigatureSet
    /// references are surfaced as `Err`.
    pub fn iter(&self) -> LigatureSubstIter<'a> {
        LigatureSubstIter {
            cov: self.coverage.iter(),
            outer: *self,
        }
    }
}

/// Iterator yielded by [`LigatureSubst::iter`].
#[derive(Debug, Clone)]
pub struct LigatureSubstIter<'a> {
    cov: crate::tables::gdef::CoverageIter<'a>,
    outer: LigatureSubst<'a>,
}

impl<'a> Iterator for LigatureSubstIter<'a> {
    type Item = (u16, Result<LigatureSet<'a>, Error>);
    fn next(&mut self) -> Option<Self::Item> {
        let (g, idx) = self.cov.next()?;
        let set = self.outer.ligature_set(idx)?;
        Some((g, set))
    }
}

/// Parsed `LigatureSet` table — a count + an offset array pointing at
/// the individual `Ligature` tables for a single first-component glyph.
#[derive(Debug, Clone, Copy)]
pub struct LigatureSet<'a> {
    bytes: &'a [u8],
    /// Slice of the `ligatureOffsets[]` array (2 bytes per offset).
    lig_offsets: &'a [u8],
}

impl<'a> LigatureSet<'a> {
    /// Parse a LigatureSet table from a buffer whose first two bytes
    /// are `ligatureCount`.
    pub fn parse(bytes: &'a [u8]) -> Result<Self, Error> {
        let count = read_u16(bytes, 0)? as usize;
        let array_start = 2usize;
        let need = array_start
            .checked_add(
                count
                    .checked_mul(2)
                    .ok_or(Error::BadStructure("GSUB/LigatureSet length overflow"))?,
            )
            .ok_or(Error::BadStructure("GSUB/LigatureSet length overflow"))?;
        if bytes.len() < need {
            return Err(Error::UnexpectedEof);
        }
        Ok(Self {
            bytes,
            lig_offsets: &bytes[array_start..need],
        })
    }

    /// `ligatureCount` — number of Ligature tables in this set.
    pub fn ligature_count(&self) -> u16 {
        (self.lig_offsets.len() / 2) as u16
    }

    /// Borrow the [`Ligature`] at preference index `i` (`0 ..
    /// ligature_count`). Returns `None` for an out-of-range index,
    /// `Some(Err(...))` when the referenced bytes are malformed.
    pub fn ligature(&self, i: u16) -> Option<Result<Ligature<'a>, Error>> {
        let off2 = (i as usize).checked_mul(2)?;
        if off2 + 2 > self.lig_offsets.len() {
            return None;
        }
        let off = u16::from_be_bytes([self.lig_offsets[off2], self.lig_offsets[off2 + 1]]) as usize;
        if off == 0 || off >= self.bytes.len() {
            return Some(Err(Error::BadStructure(
                "GSUB/LigatureSet: ligatureOffset out of range",
            )));
        }
        Some(Ligature::parse(&self.bytes[off..]))
    }
}

/// Parsed `Ligature` table — one ligature substitution candidate.
///
/// The on-disk record is `(ligatureGlyph, componentCount,
/// componentGlyphIDs[componentCount - 1])`. The first component glyph
/// is **not** stored here — it is the LigatureSet's covered glyph,
/// surfaced through [`LigatureSubst::coverage`].
#[derive(Debug, Clone, Copy)]
pub struct Ligature<'a> {
    glyph: u16,
    component_count: u16,
    /// Raw `componentGlyphIDs[]` payload — `2 * (componentCount - 1)`
    /// bytes, big-endian `u16` per entry.
    tail: &'a [u8],
}

impl<'a> Ligature<'a> {
    /// Parse a Ligature table from a buffer whose first two bytes are
    /// `ligatureGlyph`.
    ///
    /// A `componentCount` of zero is rejected as `BadStructure`: the
    /// spec specifies "Number of components in the ligature" including
    /// the first, so zero leaves the first-component invariant
    /// unsatisfiable.
    pub fn parse(bytes: &'a [u8]) -> Result<Self, Error> {
        let glyph = read_u16(bytes, 0)?;
        let component_count = read_u16(bytes, 2)?;
        if component_count == 0 {
            return Err(Error::BadStructure(
                "GSUB/Ligature: componentCount must be >= 1",
            ));
        }
        let tail_entries = (component_count - 1) as usize;
        let tail_start = 4usize;
        let need = tail_start
            .checked_add(
                tail_entries
                    .checked_mul(2)
                    .ok_or(Error::BadStructure("GSUB/Ligature length overflow"))?,
            )
            .ok_or(Error::BadStructure("GSUB/Ligature length overflow"))?;
        if bytes.len() < need {
            return Err(Error::UnexpectedEof);
        }
        Ok(Self {
            glyph,
            component_count,
            tail: &bytes[tail_start..need],
        })
    }

    /// `ligatureGlyph` — the substitute glyph ID for this ligature.
    pub fn ligature_glyph(&self) -> u16 {
        self.glyph
    }

    /// `componentCount` — total number of input glyphs (including the
    /// first, Coverage-supplied component) this ligature replaces.
    pub fn component_count(&self) -> u16 {
        self.component_count
    }

    /// The component glyph at tail index `i` (`0 .. componentCount -
    /// 1`). Index `0` is the **second** component glyph (input glyph
    /// sequence index = 1) per the spec.
    pub fn component_glyph(&self, i: u16) -> Option<u16> {
        let off = (i as usize).checked_mul(2)?;
        if off + 2 > self.tail.len() {
            return None;
        }
        Some(u16::from_be_bytes([self.tail[off], self.tail[off + 1]]))
    }

    /// Iterator over every tail-component glyph in input order.
    pub fn component_glyphs(&self) -> LigatureComponentIter<'a> {
        LigatureComponentIter {
            bytes: self.tail,
            pos: 0,
        }
    }
}

/// Iterator over a [`Ligature`]'s tail `componentGlyphIDs[]` in input
/// order (i.e. starting at the second component).
#[derive(Debug, Clone)]
pub struct LigatureComponentIter<'a> {
    bytes: &'a [u8],
    pos: usize,
}

impl<'a> Iterator for LigatureComponentIter<'a> {
    type Item = u16;
    fn next(&mut self) -> Option<Self::Item> {
        if self.pos + 2 > self.bytes.len() {
            return None;
        }
        let g = u16::from_be_bytes([self.bytes[self.pos], self.bytes[self.pos + 1]]);
        self.pos += 2;
        Some(g)
    }
}

// ---------------------------------------------------------------------------
// Lookup type 7: Substitution extension (otspec-gsub.html §"Lookup type 7
// subtable: substitution subtable extension")
// ---------------------------------------------------------------------------

/// Parsed `ExtensionSubst` subtable — the GSUB `lookupType = 7` payload.
///
/// Spec: `docs/text/opentype/otspec-gsub.html` §"Lookup type 7 subtable:
/// substitution subtable extension".
///
/// This lookup type is a *format extension mechanism*, not a
/// substitution action: it lets a Lookup reach its real subtable
/// through a 32-bit offset, for fonts whose accumulated subtable sizes
/// exceed what the usual 16-bit offsets can address. The spec's
/// processing model: proceed as though the Lookup's `lookupType` were
/// the `extensionLookupType` of the subtables, and as though each
/// extension subtable referenced by `extensionOffset` replaced the
/// type 7 subtable that referenced it.
///
/// One on-disk format is defined.
///
/// ```text
/// SubstExtensionFormat1 subtable (8 bytes)
///   0 / 2 / format = 1
///   2 / 2 / extensionLookupType   (any GsubLookupType other than 7)
///   4 / 4 / extensionOffset       (Offset32, from start of this
///                                  ExtensionSubstFormat1 subtable)
/// ```
///
/// Parse-time validation: `format == 1`; `extensionLookupType` must be
/// a defined GsubLookupType (`1..=8`) **other than 7** (the spec
/// forbids an extension pointing at another extension); and
/// `extensionOffset` must land inside the supplied byte window. The
/// wrapped subtable is surfaced both raw
/// ([`Self::extension_subtable_bytes`]) and through typed resolvers for
/// the lookup types this crate already decodes
/// ([`Self::as_single_subst`] / [`Self::as_multiple_subst`] /
/// [`Self::as_alternate_subst`] / [`Self::as_ligature_subst`]).
#[derive(Debug, Clone, Copy)]
pub struct ExtensionSubst<'a> {
    /// Raw subtable bytes (`extensionOffset` is relative to this
    /// buffer's start).
    bytes: &'a [u8],
    ext_lookup_type: u16,
    ext_offset: u32,
}

impl<'a> ExtensionSubst<'a> {
    /// Parse an ExtensionSubst subtable from a buffer whose first two
    /// bytes are the `format` identifier.
    pub fn parse(bytes: &'a [u8]) -> Result<Self, Error> {
        let format = read_u16(bytes, 0)?;
        if format != 1 {
            return Err(Error::BadStructure(
                "GSUB/ExtensionSubst: unknown subtable format",
            ));
        }
        let ext_lookup_type = read_u16(bytes, 2)?;
        // Spec: "The extensionLookupType field must be set to any
        // lookup type other than 7." The GsubLookupType vocabulary is
        // 1..=8, so anything outside that range is equally undefined.
        if ext_lookup_type == GSUB_LOOKUP_TYPE_EXTENSION {
            return Err(Error::BadStructure(
                "GSUB/ExtensionSubst: extensionLookupType must not be 7",
            ));
        }
        if !(GSUB_LOOKUP_TYPE_SINGLE..=GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE)
            .contains(&ext_lookup_type)
        {
            return Err(Error::BadStructure(
                "GSUB/ExtensionSubst: extensionLookupType out of range",
            ));
        }
        let ext_offset = read_u32(bytes, 4)?;
        // "All offsets to extension subtables are set in the usual
        // way—that is, relative to start of the ExtensionSubstFormat1
        // subtable." A NULL offset has no defined meaning here, and the
        // wrapped subtable must start inside the byte window.
        if ext_offset == 0 || (ext_offset as usize) >= bytes.len() {
            return Err(Error::BadStructure(
                "GSUB/ExtensionSubst: extensionOffset out of range",
            ));
        }
        Ok(Self {
            bytes,
            ext_lookup_type,
            ext_offset,
        })
    }

    /// Subtable format discriminant (always `1`).
    pub fn format(&self) -> u16 {
        1
    }

    /// `extensionLookupType` — the lookup type of the wrapped subtable.
    /// Guaranteed to be in `1..=8` and never `7`.
    pub fn extension_lookup_type(&self) -> u16 {
        self.ext_lookup_type
    }

    /// `extensionOffset` — byte offset of the wrapped subtable,
    /// relative to the start of this ExtensionSubstFormat1 subtable.
    pub fn extension_offset(&self) -> u32 {
        self.ext_offset
    }

    /// Raw bytes of the wrapped ("extension") subtable, starting at
    /// `extensionOffset`. Feed these to the typed parser matching
    /// [`Self::extension_lookup_type`] — or use the `as_*` resolvers
    /// below for the lookup types this crate already decodes.
    pub fn extension_subtable_bytes(&self) -> &'a [u8] {
        &self.bytes[self.ext_offset as usize..]
    }

    /// Resolve the wrapped subtable as a [`SingleSubst`]
    /// (`extensionLookupType = 1`). `Err(BadStructure)` when the
    /// declared type disagrees or the wrapped bytes are malformed.
    pub fn as_single_subst(&self) -> Result<SingleSubst<'a>, Error> {
        if self.ext_lookup_type != GSUB_LOOKUP_TYPE_SINGLE {
            return Err(Error::BadStructure(
                "GSUB/ExtensionSubst: extensionLookupType is not 1",
            ));
        }
        SingleSubst::parse(self.extension_subtable_bytes())
    }

    /// Resolve the wrapped subtable as a [`MultipleSubst`]
    /// (`extensionLookupType = 2`). `Err(BadStructure)` when the
    /// declared type disagrees or the wrapped bytes are malformed.
    pub fn as_multiple_subst(&self) -> Result<MultipleSubst<'a>, Error> {
        if self.ext_lookup_type != GSUB_LOOKUP_TYPE_MULTIPLE {
            return Err(Error::BadStructure(
                "GSUB/ExtensionSubst: extensionLookupType is not 2",
            ));
        }
        MultipleSubst::parse(self.extension_subtable_bytes())
    }

    /// Resolve the wrapped subtable as an [`AlternateSubst`]
    /// (`extensionLookupType = 3`). `Err(BadStructure)` when the
    /// declared type disagrees or the wrapped bytes are malformed.
    pub fn as_alternate_subst(&self) -> Result<AlternateSubst<'a>, Error> {
        if self.ext_lookup_type != GSUB_LOOKUP_TYPE_ALTERNATE {
            return Err(Error::BadStructure(
                "GSUB/ExtensionSubst: extensionLookupType is not 3",
            ));
        }
        AlternateSubst::parse(self.extension_subtable_bytes())
    }

    /// Resolve the wrapped subtable as a [`LigatureSubst`]
    /// (`extensionLookupType = 4`). `Err(BadStructure)` when the
    /// declared type disagrees or the wrapped bytes are malformed.
    pub fn as_ligature_subst(&self) -> Result<LigatureSubst<'a>, Error> {
        if self.ext_lookup_type != GSUB_LOOKUP_TYPE_LIGATURE {
            return Err(Error::BadStructure(
                "GSUB/ExtensionSubst: extensionLookupType is not 4",
            ));
        }
        LigatureSubst::parse(self.extension_subtable_bytes())
    }
}

/// Parsed `GSUB` header view.
#[derive(Debug, Clone, Copy)]
pub struct GsubTable<'a> {
    bytes: &'a [u8],
    header: LayoutHeader,
}

impl<'a> GsubTable<'a> {
    /// Parse a GSUB table from the raw `bytes` of the table.
    pub fn parse(bytes: &'a [u8]) -> Result<Self, Error> {
        let header = LayoutHeader::parse(bytes)?;
        let len = bytes.len();
        if (header.script_list_off as usize) >= len
            || (header.feature_list_off as usize) >= len
            || (header.lookup_list_off as usize) >= len
        {
            return Err(Error::BadStructure("GSUB: header offset out of range"));
        }
        if header.feature_variations_off != 0 && (header.feature_variations_off as usize) >= len {
            return Err(Error::BadStructure(
                "GSUB: featureVariationsOffset out of range",
            ));
        }
        Ok(Self { bytes, header })
    }

    /// `(majorVersion, minorVersion)` pair (`(1, 0)` or `(1, 1)`).
    pub fn version(&self) -> (u16, u16) {
        (self.header.major, self.header.minor)
    }

    /// Raw `featureVariationsOffset` (`0` = NULL or absent). The
    /// FeatureVariations table itself is not yet decoded; callers
    /// wanting its bytes can index `self.raw()` at this offset.
    pub fn feature_variations_offset(&self) -> u32 {
        self.header.feature_variations_off
    }

    /// `true` iff the v1.1 `featureVariationsOffset` field is present
    /// and non-zero.
    pub fn has_feature_variations(&self) -> bool {
        self.header.minor >= 1 && self.header.feature_variations_off != 0
    }

    /// Raw table bytes.
    pub fn raw(&self) -> &'a [u8] {
        self.bytes
    }

    /// Parsed [`ScriptList`].
    pub fn script_list(&self) -> Result<ScriptList<'a>, Error> {
        ScriptList::parse(&self.bytes[self.header.script_list_off as usize..])
    }

    /// Parsed [`FeatureList`].
    pub fn feature_list(&self) -> Result<FeatureList<'a>, Error> {
        FeatureList::parse(&self.bytes[self.header.feature_list_off as usize..])
    }

    /// Parsed [`LookupList`].
    pub fn lookup_list(&self) -> Result<LookupList<'a>, Error> {
        LookupList::parse(&self.bytes[self.header.lookup_list_off as usize..])
    }

    /// Convenience: find a [`Script`] by 4-byte tag (e.g. `b"DFLT"`,
    /// `b"latn"`). Returns `None` when the tag is absent or the
    /// ScriptList itself fails to parse.
    pub fn find_script(&self, tag: &[u8; 4]) -> Option<Script<'a>> {
        self.script_list().ok()?.find(tag)?.ok()
    }

    /// Convenience: total `lookupCount` (matches
    /// `LookupList::count()`).
    pub fn lookup_count(&self) -> u16 {
        self.lookup_list().map(|l| l.count()).unwrap_or(0)
    }

    /// Convenience: total `featureCount`.
    pub fn feature_count(&self) -> u16 {
        self.feature_list().map(|f| f.count()).unwrap_or(0)
    }

    /// Convenience: total `scriptCount`.
    pub fn script_count(&self) -> u16 {
        self.script_list().map(|s| s.count()).unwrap_or(0)
    }

    /// Borrow lookup `i` by index. Returns `None` for an out-of-range
    /// index or a malformed list.
    pub fn lookup(&self, i: u16) -> Option<Lookup<'a>> {
        self.lookup_list().ok()?.lookup(i)?.ok()
    }

    /// Decode subtable `sub_i` of lookup `lookup_i` as a
    /// [`SingleSubst`] (`GsubLookupType = 1`).
    ///
    /// Returns:
    /// * `None` — `lookup_i` or `sub_i` is out of range, or the
    ///   referenced subtable bytes are unreachable.
    /// * `Some(Err(Error::BadStructure))` — the lookup is not
    ///   declared as `GSUB_LOOKUP_TYPE_SINGLE`, or the subtable bytes
    ///   are malformed.
    /// * `Some(Ok(SingleSubst))` — the typed subtable view.
    pub fn single_subst(
        &self,
        lookup_i: u16,
        sub_i: u16,
    ) -> Option<Result<SingleSubst<'a>, Error>> {
        let lk = self.lookup(lookup_i)?;
        if lk.lookup_type() != GSUB_LOOKUP_TYPE_SINGLE {
            return Some(Err(Error::BadStructure(
                "GSUB/SingleSubst: lookup is not type 1",
            )));
        }
        let bytes = lk.subtable_bytes(sub_i)?;
        Some(SingleSubst::parse(bytes))
    }

    /// Decode subtable `sub_i` of lookup `lookup_i` as a
    /// [`MultipleSubst`] (`GsubLookupType = 2`).
    ///
    /// Returns:
    /// * `None` — `lookup_i` or `sub_i` is out of range, or the
    ///   referenced subtable bytes are unreachable.
    /// * `Some(Err(Error::BadStructure))` — the lookup is not declared
    ///   as `GSUB_LOOKUP_TYPE_MULTIPLE`, or the subtable bytes are
    ///   malformed.
    /// * `Some(Ok(MultipleSubst))` — the typed subtable view.
    pub fn multiple_subst(
        &self,
        lookup_i: u16,
        sub_i: u16,
    ) -> Option<Result<MultipleSubst<'a>, Error>> {
        let lk = self.lookup(lookup_i)?;
        if lk.lookup_type() != GSUB_LOOKUP_TYPE_MULTIPLE {
            return Some(Err(Error::BadStructure(
                "GSUB/MultipleSubst: lookup is not type 2",
            )));
        }
        let bytes = lk.subtable_bytes(sub_i)?;
        Some(MultipleSubst::parse(bytes))
    }

    /// Decode subtable `sub_i` of lookup `lookup_i` as an
    /// [`AlternateSubst`] (`GsubLookupType = 3`).
    ///
    /// Returns:
    /// * `None` — `lookup_i` or `sub_i` is out of range, or the
    ///   referenced subtable bytes are unreachable.
    /// * `Some(Err(Error::BadStructure))` — the lookup is not declared
    ///   as `GSUB_LOOKUP_TYPE_ALTERNATE`, or the subtable bytes are
    ///   malformed.
    /// * `Some(Ok(AlternateSubst))` — the typed subtable view.
    pub fn alternate_subst(
        &self,
        lookup_i: u16,
        sub_i: u16,
    ) -> Option<Result<AlternateSubst<'a>, Error>> {
        let lk = self.lookup(lookup_i)?;
        if lk.lookup_type() != GSUB_LOOKUP_TYPE_ALTERNATE {
            return Some(Err(Error::BadStructure(
                "GSUB/AlternateSubst: lookup is not type 3",
            )));
        }
        let bytes = lk.subtable_bytes(sub_i)?;
        Some(AlternateSubst::parse(bytes))
    }

    /// Decode subtable `sub_i` of lookup `lookup_i` as a
    /// [`LigatureSubst`] (`GsubLookupType = 4`).
    ///
    /// Returns:
    /// * `None` — `lookup_i` or `sub_i` is out of range, or the
    ///   referenced subtable bytes are unreachable.
    /// * `Some(Err(Error::BadStructure))` — the lookup is not
    ///   declared as `GSUB_LOOKUP_TYPE_LIGATURE`, or the subtable bytes
    ///   are malformed.
    /// * `Some(Ok(LigatureSubst))` — the typed subtable view.
    pub fn ligature_subst(
        &self,
        lookup_i: u16,
        sub_i: u16,
    ) -> Option<Result<LigatureSubst<'a>, Error>> {
        let lk = self.lookup(lookup_i)?;
        if lk.lookup_type() != GSUB_LOOKUP_TYPE_LIGATURE {
            return Some(Err(Error::BadStructure(
                "GSUB/LigatureSubst: lookup is not type 4",
            )));
        }
        let bytes = lk.subtable_bytes(sub_i)?;
        Some(LigatureSubst::parse(bytes))
    }

    /// Decode subtable `sub_i` of lookup `lookup_i` as an
    /// [`ExtensionSubst`] (`GsubLookupType = 7`).
    ///
    /// Returns:
    /// * `None` — `lookup_i` or `sub_i` is out of range, or the
    ///   referenced subtable bytes are unreachable.
    /// * `Some(Err(Error::BadStructure))` — the lookup is not
    ///   declared as `GSUB_LOOKUP_TYPE_EXTENSION`, or the subtable
    ///   bytes are malformed.
    /// * `Some(Ok(ExtensionSubst))` — the typed subtable view; resolve
    ///   the wrapped subtable through
    ///   [`ExtensionSubst::extension_subtable_bytes`] or one of the
    ///   typed `as_*` resolvers.
    pub fn extension_subst(
        &self,
        lookup_i: u16,
        sub_i: u16,
    ) -> Option<Result<ExtensionSubst<'a>, Error>> {
        let lk = self.lookup(lookup_i)?;
        if lk.lookup_type() != GSUB_LOOKUP_TYPE_EXTENSION {
            return Some(Err(Error::BadStructure(
                "GSUB/ExtensionSubst: lookup is not type 7",
            )));
        }
        let bytes = lk.subtable_bytes(sub_i)?;
        Some(ExtensionSubst::parse(bytes))
    }
}

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

    fn be(u: u16) -> [u8; 2] {
        u.to_be_bytes()
    }

    /// Build a minimal valid v1.0 GSUB byte tower with one DFLT
    /// script + one `liga` feature + one Lookup (type 4 = ligature
    /// substitution; we surface only the header). Tests the offsets +
    /// the three accessors.
    #[test]
    fn parses_minimal_v10_table() {
        // -------- layout planning --------
        // 0   /  10 / header (script=10, feature=22, lookup=44)
        // 10  /  12 / ScriptList: count=1, [DFLT, scriptOffset=8 → 18]
        // 18  /   4 / Script: defaultLangSys=0, langSysCount=0
        // 22  /  10 / FeatureList: count=1, [liga, featureOffset=8 → 30]
        // 30  /   6 / Feature: paramsOffset=0, lookupCount=1, lookupIdx=[0]
        // 44  /   4 / LookupList: count=1, [lookupOffset=4 → 48]
        // 48  /   6 / Lookup: type=4, flag=0, subTableCount=0
        let mut bytes = vec![0u8; 54];
        // header
        bytes[0..2].copy_from_slice(&be(1));
        bytes[2..4].copy_from_slice(&be(0));
        bytes[4..6].copy_from_slice(&be(10));
        bytes[6..8].copy_from_slice(&be(22));
        bytes[8..10].copy_from_slice(&be(44));
        // ScriptList
        bytes[10..12].copy_from_slice(&be(1));
        bytes[12..16].copy_from_slice(b"DFLT");
        bytes[16..18].copy_from_slice(&be(8));
        // Script
        bytes[18..20].copy_from_slice(&be(0));
        bytes[20..22].copy_from_slice(&be(0));
        // FeatureList
        bytes[22..24].copy_from_slice(&be(1));
        bytes[24..28].copy_from_slice(b"liga");
        bytes[28..30].copy_from_slice(&be(8));
        // Feature
        bytes[30..32].copy_from_slice(&be(0));
        bytes[32..34].copy_from_slice(&be(1));
        bytes[34..36].copy_from_slice(&be(0));
        // LookupList
        bytes[44..46].copy_from_slice(&be(1));
        bytes[46..48].copy_from_slice(&be(4));
        // Lookup
        bytes[48..50].copy_from_slice(&be(4));
        bytes[50..52].copy_from_slice(&be(0));
        bytes[52..54].copy_from_slice(&be(0));

        let g = GsubTable::parse(&bytes).unwrap();
        assert_eq!(g.version(), (1, 0));
        assert!(!g.has_feature_variations());
        assert_eq!(g.feature_variations_offset(), 0);
        assert_eq!(g.script_count(), 1);
        assert_eq!(g.feature_count(), 1);
        assert_eq!(g.lookup_count(), 1);

        let scripts = g.script_list().unwrap();
        assert_eq!(scripts.count(), 1);
        assert_eq!(scripts.tag(0), Some(*b"DFLT"));
        let dflt = g.find_script(b"DFLT").expect("DFLT script");
        assert!(!dflt.has_default_lang_sys());
        assert_eq!(dflt.lang_sys_count(), 0);

        let feats = g.feature_list().unwrap();
        assert_eq!(feats.tag(0), Some(*b"liga"));
        let liga = feats.feature(0).unwrap().unwrap();
        assert_eq!(liga.lookup_count(), 1);
        assert_eq!(liga.lookup_index(0), Some(0));

        let l0 = g.lookup(0).unwrap();
        assert_eq!(l0.lookup_type(), 4);
        assert!(!l0.flag().ignore_marks());
    }

    // -------------- SingleSubst Format 1 -------------------------------

    /// Build a SingleSubstFormat1 subtable that maps glyphs
    /// `{20, 21, 22}` to `{120, 121, 122}` via `deltaGlyphID = 100`.
    fn build_single_subst_fmt1(delta: i16, glyphs: &[u16]) -> Vec<u8> {
        // Layout:
        //   0  / 2 / format = 1
        //   2  / 2 / coverageOffset = 6
        //   4  / 2 / deltaGlyphID
        //   6  / 2 / coverage format = 1
        //   8  / 2 / glyphCount
        //  10  / 2*N / glyphArray
        let mut out = Vec::new();
        out.extend_from_slice(&be(1)); // format
        out.extend_from_slice(&be(6)); // coverageOffset
        out.extend_from_slice(&(delta as u16).to_be_bytes()); // deltaGlyphID

        // Coverage Format 1
        out.extend_from_slice(&be(1)); // coverage format
        out.extend_from_slice(&be(glyphs.len() as u16));
        for &g in glyphs {
            out.extend_from_slice(&be(g));
        }
        out
    }

    #[test]
    fn single_subst_fmt1_round_trip() {
        let raw = build_single_subst_fmt1(100, &[20, 21, 22]);
        let ss = SingleSubst::parse(&raw).unwrap();
        assert_eq!(ss.format(), 1);
        assert_eq!(ss.delta_glyph_id(), Some(100));
        assert_eq!(ss.glyph_count(), None);
        assert_eq!(ss.substitute(20), Some(120));
        assert_eq!(ss.substitute(21), Some(121));
        assert_eq!(ss.substitute(22), Some(122));
        // Uncovered glyph: no substitution.
        assert_eq!(ss.substitute(23), None);
        assert_eq!(ss.substitute(0), None);
        // Iteration produces the (input, output) pairs in sorted order.
        let pairs: Vec<_> = ss.iter().collect();
        assert_eq!(pairs, vec![(20, 120), (21, 121), (22, 122)]);
    }

    #[test]
    fn single_subst_fmt1_negative_delta_wraps_mod_65536() {
        // Spec: "If the result after adding deltaGlyphID to the input
        // glyph index is less than zero, add 65536 to obtain a valid
        // glyph ID." Verified with input = 5, delta = -10 → 65531.
        let raw = build_single_subst_fmt1(-10, &[5]);
        let ss = SingleSubst::parse(&raw).unwrap();
        assert_eq!(ss.substitute(5), Some(65531));
    }

    #[test]
    fn single_subst_fmt1_positive_delta_wraps_mod_65536() {
        // Spec: "Addition of deltaGlyphID is modulo 65536." Verified
        // with input = 65530, delta = 10 → 4.
        let raw = build_single_subst_fmt1(10, &[65530]);
        let ss = SingleSubst::parse(&raw).unwrap();
        assert_eq!(ss.substitute(65530), Some(4));
    }

    // -------------- SingleSubst Format 2 -------------------------------

    /// Build a SingleSubstFormat2 subtable.
    fn build_single_subst_fmt2(inputs: &[u16], outputs: &[u16]) -> Vec<u8> {
        assert_eq!(inputs.len(), outputs.len());
        // Layout (Coverage Format 1):
        //   0   / 2 / format = 2
        //   2   / 2 / coverageOffset
        //   4   / 2 / glyphCount
        //   6   / 2*N / substituteGlyphIDs
        //   cov / 2 / coverage format = 1
        //   cov+2 / 2 / glyphCount
        //   cov+4 / 2*N / glyphArray
        let n = inputs.len();
        let cov_off = 6 + 2 * n;
        let mut out = Vec::new();
        out.extend_from_slice(&be(2));
        out.extend_from_slice(&be(cov_off as u16));
        out.extend_from_slice(&be(n as u16));
        for &g in outputs {
            out.extend_from_slice(&be(g));
        }
        // Coverage Format 1
        out.extend_from_slice(&be(1));
        out.extend_from_slice(&be(n as u16));
        for &g in inputs {
            out.extend_from_slice(&be(g));
        }
        out
    }

    #[test]
    fn single_subst_fmt2_round_trip() {
        let raw = build_single_subst_fmt2(&[10, 30, 50], &[1010, 3030, 5050]);
        let ss = SingleSubst::parse(&raw).unwrap();
        assert_eq!(ss.format(), 2);
        assert_eq!(ss.delta_glyph_id(), None);
        assert_eq!(ss.glyph_count(), Some(3));
        assert_eq!(ss.substitute(10), Some(1010));
        assert_eq!(ss.substitute(30), Some(3030));
        assert_eq!(ss.substitute(50), Some(5050));
        // Uncovered glyph IDs (including a value between covered
        // entries) return None.
        assert_eq!(ss.substitute(0), None);
        assert_eq!(ss.substitute(20), None);
        assert_eq!(ss.substitute(60), None);

        let pairs: Vec<_> = ss.iter().collect();
        assert_eq!(pairs, vec![(10, 1010), (30, 3030), (50, 5050)]);
    }

    #[test]
    fn single_subst_fmt2_rejects_glyph_count_mismatch() {
        // Build a valid Format 2 then poke glyphCount to a value that
        // disagrees with the Coverage length.
        let mut raw = build_single_subst_fmt2(&[10, 30], &[100, 300]);
        // glyphCount lives at offset 4..6.
        raw[4..6].copy_from_slice(&be(7));
        assert!(matches!(
            SingleSubst::parse(&raw),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn single_subst_rejects_unknown_format() {
        // format = 3, plus enough trailing bytes that the
        // coverageOffset window check would succeed.
        let mut raw = vec![0u8; 16];
        raw[0..2].copy_from_slice(&be(3));
        raw[2..4].copy_from_slice(&be(8));
        // Plausible coverage payload at offset 8.
        raw[8..10].copy_from_slice(&be(1));
        raw[10..12].copy_from_slice(&be(1));
        raw[12..14].copy_from_slice(&be(5));
        assert!(matches!(
            SingleSubst::parse(&raw),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn single_subst_rejects_truncated_array() {
        // Build a Format 2 subtable then chop off the trailing
        // substituteGlyphIDs[] bytes.
        let raw = build_single_subst_fmt2(&[1, 2, 3], &[11, 22, 33]);
        // The trailing array starts at offset 6 and is 2*3 = 6 bytes;
        // dropping the last 2 bytes makes glyphCount-many entries
        // unreadable.
        let truncated = &raw[..raw.len() - 2 /* steal from Coverage tail */];
        // It's the Coverage that gets truncated by this cut, which
        // surfaces as UnexpectedEof when the Coverage parser walks the
        // shortened range.
        assert!(matches!(
            SingleSubst::parse(truncated),
            Err(Error::UnexpectedEof) | Err(Error::BadStructure(_))
        ));
    }

    // -------------- GsubTable::single_subst integration ----------------

    /// Build a tiny GSUB table whose only lookup is a type-1
    /// SingleSubstFormat1 subtable, then drive the whole walk from the
    /// `GsubTable::single_subst` convenience accessor.
    #[test]
    fn gsub_single_subst_end_to_end() {
        // Mostly mirrors `parses_minimal_v10_table` but inflates the
        // Lookup with one real subtable.
        //
        // Layout plan (all offsets relative to start of GSUB):
        //   0   /  10 / header
        //   10  /  12 / ScriptList (1 record, DFLT @18)
        //   18  /   4 / Script (no LangSys)
        //   22  /  10 / FeatureList (1 record, "calt" @ 30 [unused])
        //   30  /   6 / Feature
        //   36  /   4 / LookupList (1 entry → 40)
        //   40  /   8 / Lookup type=1, flag=0, subTableCount=1, subOff=8
        //   48  /  14 / SingleSubstFormat1 subtable:
        //                  format=1, coverageOffset=6, deltaGlyphID=200,
        //                  coverage Format 1: glyphCount=2, glyphs=[50, 51]
        let mut bytes = vec![0u8; 62];
        // header
        bytes[0..2].copy_from_slice(&be(1));
        bytes[2..4].copy_from_slice(&be(0));
        bytes[4..6].copy_from_slice(&be(10));
        bytes[6..8].copy_from_slice(&be(22));
        bytes[8..10].copy_from_slice(&be(36));
        // ScriptList
        bytes[10..12].copy_from_slice(&be(1));
        bytes[12..16].copy_from_slice(b"DFLT");
        bytes[16..18].copy_from_slice(&be(8));
        // Script
        bytes[18..20].copy_from_slice(&be(0));
        bytes[20..22].copy_from_slice(&be(0));
        // FeatureList
        bytes[22..24].copy_from_slice(&be(1));
        bytes[24..28].copy_from_slice(b"calt");
        bytes[28..30].copy_from_slice(&be(8));
        // Feature
        bytes[30..32].copy_from_slice(&be(0));
        bytes[32..34].copy_from_slice(&be(1));
        bytes[34..36].copy_from_slice(&be(0));
        // LookupList
        bytes[36..38].copy_from_slice(&be(1));
        bytes[38..40].copy_from_slice(&be(4));
        // Lookup: type=1, flag=0, subTableCount=1, subtableOffsets=[8]
        bytes[40..42].copy_from_slice(&be(1));
        bytes[42..44].copy_from_slice(&be(0));
        bytes[44..46].copy_from_slice(&be(1));
        bytes[46..48].copy_from_slice(&be(8));
        // SingleSubstFormat1 subtable @ 48
        bytes[48..50].copy_from_slice(&be(1)); // format
        bytes[50..52].copy_from_slice(&be(6)); // coverageOffset
        bytes[52..54].copy_from_slice(&be(200)); // deltaGlyphID
        bytes[54..56].copy_from_slice(&be(1)); // coverage format
        bytes[56..58].copy_from_slice(&be(2)); // glyphCount
        bytes[58..60].copy_from_slice(&be(50));
        bytes[60..62].copy_from_slice(&be(51));

        let g = GsubTable::parse(&bytes).unwrap();
        let ss = g.single_subst(0, 0).expect("subtable exists").unwrap();
        assert_eq!(ss.format(), 1);
        assert_eq!(ss.substitute(50), Some(250));
        assert_eq!(ss.substitute(51), Some(251));
        assert_eq!(ss.substitute(52), None);

        // Wrong subtable index -> None.
        assert!(g.single_subst(0, 1).is_none());
        // Wrong lookup index -> None.
        assert!(g.single_subst(99, 0).is_none());
    }

    #[test]
    fn gsub_single_subst_rejects_non_type_1_lookup() {
        // Reuse the minimal_v10 layout but declare the Lookup as
        // type = 4 (ligature), then assert the typed accessor rejects.
        let mut bytes = vec![0u8; 54];
        bytes[0..2].copy_from_slice(&be(1));
        bytes[2..4].copy_from_slice(&be(0));
        bytes[4..6].copy_from_slice(&be(10));
        bytes[6..8].copy_from_slice(&be(22));
        bytes[8..10].copy_from_slice(&be(44));
        bytes[10..12].copy_from_slice(&be(1));
        bytes[12..16].copy_from_slice(b"DFLT");
        bytes[16..18].copy_from_slice(&be(8));
        bytes[18..20].copy_from_slice(&be(0));
        bytes[20..22].copy_from_slice(&be(0));
        bytes[22..24].copy_from_slice(&be(1));
        bytes[24..28].copy_from_slice(b"liga");
        bytes[28..30].copy_from_slice(&be(8));
        bytes[30..32].copy_from_slice(&be(0));
        bytes[32..34].copy_from_slice(&be(1));
        bytes[34..36].copy_from_slice(&be(0));
        bytes[44..46].copy_from_slice(&be(1));
        bytes[46..48].copy_from_slice(&be(4));
        // Lookup: declare type = 4 (ligature), not type 1.
        bytes[48..50].copy_from_slice(&be(4));
        bytes[50..52].copy_from_slice(&be(0));
        bytes[52..54].copy_from_slice(&be(0));

        let g = GsubTable::parse(&bytes).unwrap();
        // The lookup exists but is the wrong type; we surface
        // BadStructure rather than None so callers can distinguish a
        // missing lookup from a type mismatch.
        assert!(matches!(g.single_subst(0, 0), Some(Err(_))));
    }

    #[test]
    fn rejects_unknown_minor_version() {
        let mut bytes = vec![0u8; 14];
        bytes[0..2].copy_from_slice(&be(1));
        bytes[2..4].copy_from_slice(&be(2));
        assert!(matches!(
            GsubTable::parse(&bytes),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn rejects_offset_past_table() {
        let mut bytes = vec![0u8; 10];
        bytes[0..2].copy_from_slice(&be(1));
        bytes[2..4].copy_from_slice(&be(0));
        bytes[4..6].copy_from_slice(&be(99));
        assert!(matches!(
            GsubTable::parse(&bytes),
            Err(Error::BadStructure(_))
        ));
    }

    // -------------- MultipleSubst Format 1 -----------------------------

    /// Build the spec's worked Example 4: replace the "ffi" ligature
    /// glyph (`0x00F1` = 241) with the three-glyph sequence
    /// `[f=0x1A=26, f=0x1A=26, i=0x1D=29]` (the bytes from §"Example 4:
    /// MultipleSubstFormat1 subtable"). The layout below uses hand-set
    /// offsets so the resulting bytes match the spec's hex listing
    /// exactly through the header + Coverage + Sequence rows.
    fn build_example_4_subtable() -> Vec<u8> {
        // Layout plan (matches the spec's Example 4 byte listing):
        //   off  0 .. 2  / format = 1
        //   off  2 .. 4  / coverageOffset = 8
        //   off  4 .. 6  / sequenceCount = 1
        //   off  6 .. 8  / sequenceOffsets[0] = 14   (→ Sequence @ off 14)
        //   off  8 .. 10 / coverage format = 1
        //   off 10 .. 12 / glyphCount = 1
        //   off 12 .. 14 / glyphArray[0] = 0x00F1 (ffi)
        //   off 14 .. 16 / glyphCount = 3
        //   off 16 .. 18 / substituteGlyphIDs[0] = 0x001A (f)
        //   off 18 .. 20 / substituteGlyphIDs[1] = 0x001A (f)
        //   off 20 .. 22 / substituteGlyphIDs[2] = 0x001D (i)
        let mut out = vec![0u8; 22];
        out[0..2].copy_from_slice(&be(1));
        out[2..4].copy_from_slice(&be(8));
        out[4..6].copy_from_slice(&be(1));
        out[6..8].copy_from_slice(&be(14));
        // Coverage Format 1
        out[8..10].copy_from_slice(&be(1));
        out[10..12].copy_from_slice(&be(1));
        out[12..14].copy_from_slice(&be(0x00F1));
        // Sequence
        out[14..16].copy_from_slice(&be(3));
        out[16..18].copy_from_slice(&be(0x001A));
        out[18..20].copy_from_slice(&be(0x001A));
        out[20..22].copy_from_slice(&be(0x001D));
        out
    }

    #[test]
    fn multiple_subst_example_4_round_trip() {
        // Replays the spec's Example 4: the "ffi" ligature (glyph 241)
        // decomposes into [f=26, f=26, i=29].
        let raw = build_example_4_subtable();
        let ms = MultipleSubst::parse(&raw).unwrap();
        assert_eq!(ms.format(), 1);
        assert_eq!(ms.sequence_count(), 1);

        let seq = ms.sequence(0).unwrap().unwrap();
        assert_eq!(seq.glyph_count(), 3);
        assert_eq!(seq.glyph(0), Some(0x001A));
        assert_eq!(seq.glyph(1), Some(0x001A));
        assert_eq!(seq.glyph(2), Some(0x001D));
        assert_eq!(seq.glyph(3), None);
        let collected: Vec<_> = seq.glyphs().collect();
        assert_eq!(collected, vec![0x001A, 0x001A, 0x001D]);

        // substitute() routes Coverage → Sequence and yields the same
        // bytes for the covered input glyph.
        let out_seq = ms.substitute(0x00F1).expect("ffi is covered");
        let outputs: Vec<_> = out_seq.glyphs().collect();
        assert_eq!(outputs, vec![0x001A, 0x001A, 0x001D]);
        // Uncovered glyphs return None.
        assert_eq!(
            ms.substitute(0x00F2).map(|s| s.glyph_count()),
            None,
            "uncovered glyph must not substitute",
        );
    }

    #[test]
    fn multiple_subst_iter_walks_coverage_in_order() {
        // Two covered glyphs `{5, 9}`; their sequences are
        // `[100, 101]` and `[200]` respectively.
        //
        // Layout plan:
        //   off  0 .. 2  / format = 1
        //   off  2 .. 4  / coverageOffset = 22
        //   off  4 .. 6  / sequenceCount = 2
        //   off  6 .. 8  / sequenceOffsets[0] = 10  (→ seq0 @ off 10)
        //   off  8 .. 10 / sequenceOffsets[1] = 16  (→ seq1 @ off 16)
        //   off 10 .. 12 / glyphCount = 2
        //   off 12 .. 14 / substituteGlyphIDs[0] = 100
        //   off 14 .. 16 / substituteGlyphIDs[1] = 101
        //   off 16 .. 18 / glyphCount = 1
        //   off 18 .. 20 / substituteGlyphIDs[0] = 200
        //   off 20 .. 22 / padding (Coverage starts at 22)
        //   off 22 .. 24 / coverage format = 1
        //   off 24 .. 26 / glyphCount = 2
        //   off 26 .. 28 / glyphArray[0] = 5
        //   off 28 .. 30 / glyphArray[1] = 9
        let mut raw = vec![0u8; 30];
        raw[0..2].copy_from_slice(&be(1));
        raw[2..4].copy_from_slice(&be(22));
        raw[4..6].copy_from_slice(&be(2));
        raw[6..8].copy_from_slice(&be(10));
        raw[8..10].copy_from_slice(&be(16));
        raw[10..12].copy_from_slice(&be(2));
        raw[12..14].copy_from_slice(&be(100));
        raw[14..16].copy_from_slice(&be(101));
        raw[16..18].copy_from_slice(&be(1));
        raw[18..20].copy_from_slice(&be(200));
        raw[22..24].copy_from_slice(&be(1));
        raw[24..26].copy_from_slice(&be(2));
        raw[26..28].copy_from_slice(&be(5));
        raw[28..30].copy_from_slice(&be(9));

        let ms = MultipleSubst::parse(&raw).unwrap();
        assert_eq!(ms.sequence_count(), 2);

        let pairs: Vec<(u16, Vec<u16>)> = ms
            .iter()
            .map(|(g, s)| (g, s.unwrap().glyphs().collect()))
            .collect();
        assert_eq!(pairs, vec![(5, vec![100, 101]), (9, vec![200])],);
    }

    #[test]
    fn multiple_subst_rejects_unknown_format() {
        // format = 2 is undefined for Lookup Type 2.
        let mut raw = vec![0u8; 14];
        raw[0..2].copy_from_slice(&be(2));
        raw[2..4].copy_from_slice(&be(8));
        // Plausible coverage payload at offset 8 so the format check
        // fires before the coverage walk.
        raw[8..10].copy_from_slice(&be(1));
        raw[10..12].copy_from_slice(&be(1));
        raw[12..14].copy_from_slice(&be(5));
        assert!(matches!(
            MultipleSubst::parse(&raw),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn multiple_subst_rejects_coverage_offset_out_of_range() {
        let mut raw = vec![0u8; 10];
        raw[0..2].copy_from_slice(&be(1));
        raw[2..4].copy_from_slice(&be(99));
        raw[4..6].copy_from_slice(&be(0));
        assert!(matches!(
            MultipleSubst::parse(&raw),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn multiple_subst_rejects_sequence_count_mismatch() {
        // Build the Example-4 subtable, then poke sequenceCount to a
        // value that disagrees with the Coverage's glyphCount = 1.
        let mut raw = build_example_4_subtable();
        // sequenceCount lives at offset 4..6. We patch to 7 and the
        // parser should refuse rather than fall through to a stale
        // offset.
        raw[4..6].copy_from_slice(&be(7));
        assert!(matches!(
            MultipleSubst::parse(&raw),
            Err(Error::BadStructure(_) | Error::UnexpectedEof),
        ));
    }

    #[test]
    fn multiple_subst_rejects_truncated_sequence_offsets_array() {
        // Header claims sequenceCount = 4 (needs 8 bytes of offsets at
        // offsets 6..14) but the buffer ends mid-array. Coverage is
        // placed in-range so the coverageOffset check passes; we then
        // need Coverage.len() to also be 4 so the sequenceCount /
        // Coverage.len() check passes and the trailing-array length
        // check actually fires.
        //
        //   0  / 2 / format = 1
        //   2  / 2 / coverageOffset = 8
        //   4  / 2 / sequenceCount = 4 (needs 8 bytes from off 6 →
        //                              need = 14; buffer = 12.)
        //   6  / 2 / sequenceOffsets[0]
        //   8  / 2 / coverage format = 1
        //  10  / 2 / glyphCount = 4 (so sequenceCount == coverage.len())
        let mut raw = vec![0u8; 12];
        raw[0..2].copy_from_slice(&be(1));
        raw[2..4].copy_from_slice(&be(8));
        raw[4..6].copy_from_slice(&be(4));
        raw[6..8].copy_from_slice(&be(0));
        raw[8..10].copy_from_slice(&be(1));
        raw[10..12].copy_from_slice(&be(4));
        // The Coverage parser also wants room for the 4-glyph array,
        // which the buffer doesn't have. Either rejection (truncated
        // sequenceOffsets[] or truncated Coverage) is spec-correct.
        assert!(matches!(
            MultipleSubst::parse(&raw),
            Err(Error::UnexpectedEof) | Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn multiple_subst_rejects_zero_glyph_count_sequence() {
        // A Sequence with glyphCount = 0 is spec-prohibited
        // ("The use of multiple substitution for deletion of an input
        // glyph is prohibited."). The top-level subtable parses, but
        // walking into the bad Sequence surfaces BadStructure.
        //
        // Layout:
        //   off  0 .. 2 / format = 1
        //   off  2 .. 4 / coverageOffset = 12
        //   off  4 .. 6 / sequenceCount = 1
        //   off  6 .. 8 / sequenceOffsets[0] = 10 (→ Sequence @ off 10)
        //   off  8 .. 10 / padding (Coverage starts at 12)
        //   off 10 .. 12 / glyphCount = 0 (Sequence)
        //   off 12 .. 14 / coverage format = 1
        //   off 14 .. 16 / glyphCount = 1
        //   off 16 .. 18 / glyphArray[0] = 7
        let mut raw = vec![0u8; 18];
        raw[0..2].copy_from_slice(&be(1));
        raw[2..4].copy_from_slice(&be(12));
        raw[4..6].copy_from_slice(&be(1));
        raw[6..8].copy_from_slice(&be(10));
        raw[10..12].copy_from_slice(&be(0));
        raw[12..14].copy_from_slice(&be(1));
        raw[14..16].copy_from_slice(&be(1));
        raw[16..18].copy_from_slice(&be(7));

        let ms = MultipleSubst::parse(&raw).unwrap();
        let bad = ms.sequence(0).unwrap();
        assert!(matches!(bad, Err(Error::BadStructure(_))));
        // substitute() returns None when the inner Sequence is bad —
        // shaper-path callers can't act on a malformed subtable.
        assert!(ms.substitute(7).is_none());
    }

    // -------------- GsubTable::multiple_subst integration --------------

    /// Build a minimal v1.0 GSUB table whose only Lookup is a type-2
    /// MultipleSubst subtable (the spec's Example 4).
    fn build_minimal_multiple_gsub() -> Vec<u8> {
        let sub = build_example_4_subtable();

        // GSUB layout (mirrors the LigatureSubst end-to-end fixture):
        //   0   /  10 / header (script=10, feature=22, lookup=36)
        //   10  /  12 / ScriptList
        //   18  /   4 / Script
        //   22  /  10 / FeatureList ("ccmp" — the canonical Multiple
        //                            user, though the tag is purely
        //                            informational here)
        //   30  /   6 / Feature
        //   36  /   4 / LookupList
        //   40  /   8 / Lookup type=2, flag=0, subTableCount=1, subOff=8
        //   48  /  ?  / MultipleSubstFormat1 subtable (sub.len() bytes)
        let head_end = 48 + sub.len();
        let mut bytes = vec![0u8; head_end];
        bytes[0..2].copy_from_slice(&be(1));
        bytes[2..4].copy_from_slice(&be(0));
        bytes[4..6].copy_from_slice(&be(10));
        bytes[6..8].copy_from_slice(&be(22));
        bytes[8..10].copy_from_slice(&be(36));
        bytes[10..12].copy_from_slice(&be(1));
        bytes[12..16].copy_from_slice(b"DFLT");
        bytes[16..18].copy_from_slice(&be(8));
        bytes[18..20].copy_from_slice(&be(0));
        bytes[20..22].copy_from_slice(&be(0));
        bytes[22..24].copy_from_slice(&be(1));
        bytes[24..28].copy_from_slice(b"ccmp");
        bytes[28..30].copy_from_slice(&be(8));
        bytes[30..32].copy_from_slice(&be(0));
        bytes[32..34].copy_from_slice(&be(1));
        bytes[34..36].copy_from_slice(&be(0));
        bytes[36..38].copy_from_slice(&be(1));
        bytes[38..40].copy_from_slice(&be(4));
        // Lookup: type=2, flag=0, subTableCount=1, subtableOffsets=[8]
        bytes[40..42].copy_from_slice(&be(2));
        bytes[42..44].copy_from_slice(&be(0));
        bytes[44..46].copy_from_slice(&be(1));
        bytes[46..48].copy_from_slice(&be(8));
        // Subtable
        bytes[48..head_end].copy_from_slice(&sub);
        bytes
    }

    #[test]
    fn gsub_multiple_subst_end_to_end() {
        let bytes = build_minimal_multiple_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        assert_eq!(g.lookup_count(), 1);
        let l0 = g.lookup(0).unwrap();
        assert_eq!(l0.lookup_type(), GSUB_LOOKUP_TYPE_MULTIPLE);

        let ms = g.multiple_subst(0, 0).expect("subtable exists").unwrap();
        let seq = ms.substitute(0x00F1).expect("ffi is covered");
        let outputs: Vec<_> = seq.glyphs().collect();
        assert_eq!(outputs, vec![0x001A, 0x001A, 0x001D]);

        // Wrong subtable index -> None.
        assert!(g.multiple_subst(0, 1).is_none());
        // Wrong lookup index -> None.
        assert!(g.multiple_subst(99, 0).is_none());
    }

    #[test]
    fn gsub_multiple_subst_rejects_non_type_2_lookup() {
        // Reuse the minimal_v10 layout but declare the Lookup as
        // type = 4 (ligature), then assert the typed accessor rejects.
        let mut bytes = vec![0u8; 54];
        bytes[0..2].copy_from_slice(&be(1));
        bytes[2..4].copy_from_slice(&be(0));
        bytes[4..6].copy_from_slice(&be(10));
        bytes[6..8].copy_from_slice(&be(22));
        bytes[8..10].copy_from_slice(&be(44));
        bytes[10..12].copy_from_slice(&be(1));
        bytes[12..16].copy_from_slice(b"DFLT");
        bytes[16..18].copy_from_slice(&be(8));
        bytes[18..20].copy_from_slice(&be(0));
        bytes[20..22].copy_from_slice(&be(0));
        bytes[22..24].copy_from_slice(&be(1));
        bytes[24..28].copy_from_slice(b"liga");
        bytes[28..30].copy_from_slice(&be(8));
        bytes[30..32].copy_from_slice(&be(0));
        bytes[32..34].copy_from_slice(&be(1));
        bytes[34..36].copy_from_slice(&be(0));
        bytes[44..46].copy_from_slice(&be(1));
        bytes[46..48].copy_from_slice(&be(4));
        // Lookup: declare type = 4 (ligature), not type 2.
        bytes[48..50].copy_from_slice(&be(4));
        bytes[50..52].copy_from_slice(&be(0));
        bytes[52..54].copy_from_slice(&be(0));

        let g = GsubTable::parse(&bytes).unwrap();
        assert!(matches!(g.multiple_subst(0, 0), Some(Err(_))));
    }

    // -------------- AlternateSubst Format 1 ----------------------------

    /// Build the spec's worked Example 5: the default ampersand glyph
    /// (`0x003A` = 58) maps to an AlternateSet of two alternative
    /// ampersand glyphs `[0x00C9 = 201, 0x00CA = 202]`. The offsets are
    /// hand-set to match §"Example 5: AlternateSubstFormat 1 subtable"
    /// exactly (coverageOffset = 8, alternateSetOffsets[0] = 14).
    fn build_example_5_subtable() -> Vec<u8> {
        // Layout plan (matches the spec's Example 5 byte listing):
        //   off  0 .. 2  / format = 1
        //   off  2 .. 4  / coverageOffset = 8
        //   off  4 .. 6  / alternateSetCount = 1
        //   off  6 .. 8  / alternateSetOffsets[0] = 14 (→ AltSet @ off 14)
        //   off  8 .. 10 / coverage format = 1
        //   off 10 .. 12 / glyphCount = 1
        //   off 12 .. 14 / glyphArray[0] = 0x003A (default ampersand)
        //   off 14 .. 16 / glyphCount = 2 (AlternateSet)
        //   off 16 .. 18 / alternateGlyphIDs[0] = 0x00C9
        //   off 18 .. 20 / alternateGlyphIDs[1] = 0x00CA
        let mut out = vec![0u8; 20];
        out[0..2].copy_from_slice(&be(1));
        out[2..4].copy_from_slice(&be(8));
        out[4..6].copy_from_slice(&be(1));
        out[6..8].copy_from_slice(&be(14));
        // Coverage Format 1
        out[8..10].copy_from_slice(&be(1));
        out[10..12].copy_from_slice(&be(1));
        out[12..14].copy_from_slice(&be(0x003A));
        // AlternateSet
        out[14..16].copy_from_slice(&be(2));
        out[16..18].copy_from_slice(&be(0x00C9));
        out[18..20].copy_from_slice(&be(0x00CA));
        out
    }

    #[test]
    fn alternate_subst_example_5_round_trip() {
        let raw = build_example_5_subtable();
        let alt = AlternateSubst::parse(&raw).unwrap();
        assert_eq!(alt.format(), 1);
        assert_eq!(alt.alternate_set_count(), 1);

        let set = alt.alternate_set(0).unwrap().unwrap();
        assert_eq!(set.glyph_count(), 2);
        assert_eq!(set.glyph(0), Some(0x00C9));
        assert_eq!(set.glyph(1), Some(0x00CA));
        assert_eq!(set.glyph(2), None);
        let collected: Vec<_> = set.glyphs().collect();
        assert_eq!(collected, vec![0x00C9, 0x00CA]);

        // substitute() routes Coverage → AlternateSet for the covered
        // ampersand and yields the same alternatives.
        let out_set = alt.substitute(0x003A).expect("ampersand is covered");
        let outputs: Vec<_> = out_set.glyphs().collect();
        assert_eq!(outputs, vec![0x00C9, 0x00CA]);
        // Uncovered glyphs return None.
        assert_eq!(
            alt.substitute(0x003B).map(|s| s.glyph_count()),
            None,
            "uncovered glyph must not substitute",
        );
    }

    #[test]
    fn alternate_subst_iter_walks_coverage_in_order() {
        // Two covered glyphs `{5, 9}`; their AlternateSets are
        // `[100, 101]` and `[200]` respectively.
        //
        // Layout plan:
        //   off  0 .. 2  / format = 1
        //   off  2 .. 4  / coverageOffset = 22
        //   off  4 .. 6  / alternateSetCount = 2
        //   off  6 .. 8  / alternateSetOffsets[0] = 10  (→ set0 @ off 10)
        //   off  8 .. 10 / alternateSetOffsets[1] = 16  (→ set1 @ off 16)
        //   off 10 .. 12 / glyphCount = 2
        //   off 12 .. 14 / alternateGlyphIDs[0] = 100
        //   off 14 .. 16 / alternateGlyphIDs[1] = 101
        //   off 16 .. 18 / glyphCount = 1
        //   off 18 .. 20 / alternateGlyphIDs[0] = 200
        //   off 20 .. 22 / padding (Coverage starts at 22)
        //   off 22 .. 24 / coverage format = 1
        //   off 24 .. 26 / glyphCount = 2
        //   off 26 .. 28 / glyphArray[0] = 5
        //   off 28 .. 30 / glyphArray[1] = 9
        let mut raw = vec![0u8; 30];
        raw[0..2].copy_from_slice(&be(1));
        raw[2..4].copy_from_slice(&be(22));
        raw[4..6].copy_from_slice(&be(2));
        raw[6..8].copy_from_slice(&be(10));
        raw[8..10].copy_from_slice(&be(16));
        raw[10..12].copy_from_slice(&be(2));
        raw[12..14].copy_from_slice(&be(100));
        raw[14..16].copy_from_slice(&be(101));
        raw[16..18].copy_from_slice(&be(1));
        raw[18..20].copy_from_slice(&be(200));
        raw[22..24].copy_from_slice(&be(1));
        raw[24..26].copy_from_slice(&be(2));
        raw[26..28].copy_from_slice(&be(5));
        raw[28..30].copy_from_slice(&be(9));

        let alt = AlternateSubst::parse(&raw).unwrap();
        assert_eq!(alt.alternate_set_count(), 2);

        let pairs: Vec<(u16, Vec<u16>)> = alt
            .iter()
            .map(|(g, s)| (g, s.unwrap().glyphs().collect()))
            .collect();
        assert_eq!(pairs, vec![(5, vec![100, 101]), (9, vec![200])],);
    }

    #[test]
    fn alternate_subst_rejects_unknown_format() {
        // format = 2 is undefined for Lookup Type 3.
        let mut raw = vec![0u8; 14];
        raw[0..2].copy_from_slice(&be(2));
        raw[2..4].copy_from_slice(&be(8));
        // Plausible coverage payload at offset 8 so the format check
        // fires before the coverage walk.
        raw[8..10].copy_from_slice(&be(1));
        raw[10..12].copy_from_slice(&be(1));
        raw[12..14].copy_from_slice(&be(5));
        assert!(matches!(
            AlternateSubst::parse(&raw),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn alternate_subst_rejects_coverage_offset_out_of_range() {
        let mut raw = vec![0u8; 10];
        raw[0..2].copy_from_slice(&be(1));
        raw[2..4].copy_from_slice(&be(99));
        raw[4..6].copy_from_slice(&be(0));
        assert!(matches!(
            AlternateSubst::parse(&raw),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn alternate_subst_rejects_set_count_mismatch() {
        // Build the Example-5 subtable, then poke alternateSetCount to a
        // value that disagrees with the Coverage's glyphCount = 1.
        let mut raw = build_example_5_subtable();
        raw[4..6].copy_from_slice(&be(7));
        assert!(matches!(
            AlternateSubst::parse(&raw),
            Err(Error::BadStructure(_) | Error::UnexpectedEof),
        ));
    }

    #[test]
    fn alternate_subst_rejects_truncated_set_offsets_array() {
        // Header claims alternateSetCount = 4 (needs 8 bytes of offsets
        // at offsets 6..14) but the buffer ends mid-array. Coverage is
        // placed in-range with a matching glyphCount = 4 so the count
        // invariant passes and the trailing-array length check fires.
        //
        //   0  / 2 / format = 1
        //   2  / 2 / coverageOffset = 8
        //   4  / 2 / alternateSetCount = 4
        //   6  / 2 / alternateSetOffsets[0]
        //   8  / 2 / coverage format = 1
        //  10  / 2 / glyphCount = 4
        let mut raw = vec![0u8; 12];
        raw[0..2].copy_from_slice(&be(1));
        raw[2..4].copy_from_slice(&be(8));
        raw[4..6].copy_from_slice(&be(4));
        raw[6..8].copy_from_slice(&be(0));
        raw[8..10].copy_from_slice(&be(1));
        raw[10..12].copy_from_slice(&be(4));
        assert!(matches!(
            AlternateSubst::parse(&raw),
            Err(Error::UnexpectedEof) | Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn alternate_subst_accepts_empty_alternate_set() {
        // The spec sets no lower bound on AlternateSet.glyphCount, so an
        // empty AlternateSet (no alternatives) is accepted — it simply
        // yields zero choices. Contrast MultipleSubst, which prohibits a
        // zero-length Sequence.
        //
        // Layout:
        //   off  0 .. 2 / format = 1
        //   off  2 .. 4 / coverageOffset = 12
        //   off  4 .. 6 / alternateSetCount = 1
        //   off  6 .. 8 / alternateSetOffsets[0] = 10 (→ AltSet @ off 10)
        //   off  8 .. 10 / padding (Coverage starts at 12)
        //   off 10 .. 12 / glyphCount = 0 (AlternateSet)
        //   off 12 .. 14 / coverage format = 1
        //   off 14 .. 16 / glyphCount = 1
        //   off 16 .. 18 / glyphArray[0] = 7
        let mut raw = vec![0u8; 18];
        raw[0..2].copy_from_slice(&be(1));
        raw[2..4].copy_from_slice(&be(12));
        raw[4..6].copy_from_slice(&be(1));
        raw[6..8].copy_from_slice(&be(10));
        raw[10..12].copy_from_slice(&be(0));
        raw[12..14].copy_from_slice(&be(1));
        raw[14..16].copy_from_slice(&be(1));
        raw[16..18].copy_from_slice(&be(7));

        let alt = AlternateSubst::parse(&raw).unwrap();
        let set = alt.alternate_set(0).unwrap().unwrap();
        assert_eq!(set.glyph_count(), 0);
        assert_eq!(set.glyph(0), None);
        assert_eq!(set.glyphs().count(), 0);
        // substitute() of the covered glyph yields the (empty) set.
        let out = alt.substitute(7).expect("covered");
        assert_eq!(out.glyph_count(), 0);
    }

    // -------------- GsubTable::alternate_subst integration -------------

    /// Build a minimal v1.0 GSUB table whose only Lookup is a type-3
    /// AlternateSubst subtable (the spec's Example 5).
    fn build_minimal_alternate_gsub() -> Vec<u8> {
        let sub = build_example_5_subtable();

        // GSUB layout (mirrors the MultipleSubst end-to-end fixture):
        //   0   /  10 / header (script=10, feature=22, lookup=36)
        //   10  /  12 / ScriptList
        //   22  /  10 / FeatureList ("aalt" — the canonical Alternate
        //                            user; the tag is informational here)
        //   36  /   4 / LookupList
        //   40  /   8 / Lookup type=3, flag=0, subTableCount=1, subOff=8
        //   48  /  ?  / AlternateSubstFormat1 subtable (sub.len() bytes)
        let head_end = 48 + sub.len();
        let mut bytes = vec![0u8; head_end];
        bytes[0..2].copy_from_slice(&be(1));
        bytes[2..4].copy_from_slice(&be(0));
        bytes[4..6].copy_from_slice(&be(10));
        bytes[6..8].copy_from_slice(&be(22));
        bytes[8..10].copy_from_slice(&be(36));
        bytes[10..12].copy_from_slice(&be(1));
        bytes[12..16].copy_from_slice(b"DFLT");
        bytes[16..18].copy_from_slice(&be(8));
        bytes[18..20].copy_from_slice(&be(0));
        bytes[20..22].copy_from_slice(&be(0));
        bytes[22..24].copy_from_slice(&be(1));
        bytes[24..28].copy_from_slice(b"aalt");
        bytes[28..30].copy_from_slice(&be(8));
        bytes[30..32].copy_from_slice(&be(0));
        bytes[32..34].copy_from_slice(&be(1));
        bytes[34..36].copy_from_slice(&be(0));
        bytes[36..38].copy_from_slice(&be(1));
        bytes[38..40].copy_from_slice(&be(4));
        // Lookup: type=3, flag=0, subTableCount=1, subtableOffsets=[8]
        bytes[40..42].copy_from_slice(&be(3));
        bytes[42..44].copy_from_slice(&be(0));
        bytes[44..46].copy_from_slice(&be(1));
        bytes[46..48].copy_from_slice(&be(8));
        // Subtable
        bytes[48..head_end].copy_from_slice(&sub);
        bytes
    }

    #[test]
    fn gsub_alternate_subst_end_to_end() {
        let bytes = build_minimal_alternate_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        assert_eq!(g.lookup_count(), 1);
        let l0 = g.lookup(0).unwrap();
        assert_eq!(l0.lookup_type(), GSUB_LOOKUP_TYPE_ALTERNATE);

        let alt = g.alternate_subst(0, 0).expect("subtable exists").unwrap();
        let set = alt.substitute(0x003A).expect("ampersand is covered");
        let outputs: Vec<_> = set.glyphs().collect();
        assert_eq!(outputs, vec![0x00C9, 0x00CA]);

        // Wrong subtable index -> None.
        assert!(g.alternate_subst(0, 1).is_none());
        // Wrong lookup index -> None.
        assert!(g.alternate_subst(99, 0).is_none());
    }

    #[test]
    fn gsub_alternate_subst_rejects_non_type_3_lookup() {
        // Reuse the minimal layout but declare the Lookup as type = 4
        // (ligature), then assert the typed accessor rejects.
        let mut bytes = vec![0u8; 54];
        bytes[0..2].copy_from_slice(&be(1));
        bytes[2..4].copy_from_slice(&be(0));
        bytes[4..6].copy_from_slice(&be(10));
        bytes[6..8].copy_from_slice(&be(22));
        bytes[8..10].copy_from_slice(&be(44));
        bytes[10..12].copy_from_slice(&be(1));
        bytes[12..16].copy_from_slice(b"DFLT");
        bytes[16..18].copy_from_slice(&be(8));
        bytes[18..20].copy_from_slice(&be(0));
        bytes[20..22].copy_from_slice(&be(0));
        bytes[22..24].copy_from_slice(&be(1));
        bytes[24..28].copy_from_slice(b"liga");
        bytes[28..30].copy_from_slice(&be(8));
        bytes[30..32].copy_from_slice(&be(0));
        bytes[32..34].copy_from_slice(&be(1));
        bytes[34..36].copy_from_slice(&be(0));
        bytes[44..46].copy_from_slice(&be(1));
        bytes[46..48].copy_from_slice(&be(4));
        // Lookup: declare type = 4 (ligature), not type 3.
        bytes[48..50].copy_from_slice(&be(4));
        bytes[50..52].copy_from_slice(&be(0));
        bytes[52..54].copy_from_slice(&be(0));

        let g = GsubTable::parse(&bytes).unwrap();
        assert!(matches!(g.alternate_subst(0, 0), Some(Err(_))));
    }

    // -------------- LigatureSubst Format 1 -----------------------------

    /// Build a `LigatureSubstFormat1` subtable that mirrors the spec's
    /// Example 6: Coverage = `{e, f}`, e-set = `[etc]`, f-set = `[ffi,
    /// fi]` (ffi preferred over fi per the spec).
    ///
    /// Component glyph IDs are made-up flat numbers so we can assert
    /// the actual decoded bytes; the layout matches the OFF spec
    /// exactly.
    fn build_example_6_subtable() -> Vec<u8> {
        // Choose glyph IDs:
        //   e = 5, f = 6, t = 20, c = 21, i = 9, etc = 100,
        //   ffi = 101, fi = 102.
        //
        // Layout plan:
        //   0   /  2 / format = 1
        //   2   /  2 / coverageOffset → 30
        //   4   /  2 / ligatureSetCount = 2
        //   6   /  2 / ligatureSetOffsets[0] = 10   (→ e-set @ 10)
        //   8   /  2 / ligatureSetOffsets[1] = 18   (→ f-set @ 18)
        //  10   /  2 / ligatureCount = 1            (e-set: just "etc")
        //  12   /  2 / ligatureOffsets[0] = 4       (→ etc @ 14)
        //  14   /  2 / ligatureGlyph = 100  (etc)
        //  16   /  2 / componentCount = 3
        //  18   /  ⋮ / componentGlyphIDs unused for e-set (it's only 1
        //              ligature, but its bytes overlap the f-set start —
        //              fix the plan: lay out per-set independently.)
        //
        // Replan to avoid overlap. Use:
        //   off 0 .. 10 = header (format, covOff, setCount, setOffsets[0..2])
        //   off 10 .. ?  = e-set (1 ligature: "etc" — 3 components)
        //                  10 / 2 / ligatureCount = 1
        //                  12 / 2 / ligatureOffsets[0] = 4 (→ off 14)
        //                  14 / 2 / ligatureGlyph = 100
        //                  16 / 2 / componentCount = 3
        //                  18 / 2 / componentGlyphIDs[0] = t = 20
        //                  20 / 2 / componentGlyphIDs[1] = c = 21
        //                  → e-set ends at 22.
        //   off 22 .. ?  = f-set (2 ligatures: ffi, fi)
        //                  22 / 2 / ligatureCount = 2
        //                  24 / 2 / ligatureOffsets[0] = 8 (→ off 30; ffi)
        //                  26 / 2 / ligatureOffsets[1] = 16 (→ off 38; fi)
        //                  Wait — those offsets are from start of LigatureSet,
        //                  i.e. from off 22. Compute the on-disk offsets:
        //                    ffi @ off 30 → 30 - 22 = 8.   OK
        //                    fi  @ off 38 → 38 - 22 = 16.  OK
        //                  ffi Ligature @ 30:
        //                    30 / 2 / ligatureGlyph = 101
        //                    32 / 2 / componentCount = 3   (f, f, i)
        //                    34 / 2 / componentGlyphIDs[0] = f = 6
        //                    36 / 2 / componentGlyphIDs[1] = i = 9
        //                    → ends at 38.
        //                  fi Ligature @ 38:
        //                    38 / 2 / ligatureGlyph = 102
        //                    40 / 2 / componentCount = 2   (f, i)
        //                    42 / 2 / componentGlyphIDs[0] = i = 9
        //                    → ends at 44.
        //   off 44 .. 50 = Coverage Format 1 with [e, f] (sorted)
        //                  44 / 2 / coverage format = 1
        //                  46 / 2 / glyphCount = 2
        //                  48 / 2 / glyphArray[0] = e = 5
        //                  50 / 2 / glyphArray[1] = f = 6
        //                  → ends at 52.
        //
        // Re-pin Coverage offset to 44 and ligatureSetOffsets to [10, 22].
        let mut out = vec![0u8; 52];
        // Header
        out[0..2].copy_from_slice(&be(1)); // format
        out[2..4].copy_from_slice(&be(44)); // coverageOffset
        out[4..6].copy_from_slice(&be(2)); // ligatureSetCount
        out[6..8].copy_from_slice(&be(10)); // ligatureSetOffsets[0]
        out[8..10].copy_from_slice(&be(22)); // ligatureSetOffsets[1]
                                             // e-set @ 10 (1 ligature, "etc")
        out[10..12].copy_from_slice(&be(1)); // ligatureCount
        out[12..14].copy_from_slice(&be(4)); // ligatureOffsets[0]
                                             // etc Ligature @ 14
        out[14..16].copy_from_slice(&be(100)); // ligatureGlyph
        out[16..18].copy_from_slice(&be(3)); // componentCount
        out[18..20].copy_from_slice(&be(20)); // t
        out[20..22].copy_from_slice(&be(21)); // c
                                              // f-set @ 22 (2 ligatures, ffi then fi)
        out[22..24].copy_from_slice(&be(2)); // ligatureCount
        out[24..26].copy_from_slice(&be(8)); // ligatureOffsets[0] -> ffi
        out[26..28].copy_from_slice(&be(16)); // ligatureOffsets[1] -> fi
                                              // ffi Ligature @ 30
        out[28..30].copy_from_slice(&[0, 0]); // padding (set table only goes
                                              // through offset 28; bytes 28..30 unused, set to 0)
        out[30..32].copy_from_slice(&be(101)); // ligatureGlyph
        out[32..34].copy_from_slice(&be(3)); // componentCount
        out[34..36].copy_from_slice(&be(6)); // f
        out[36..38].copy_from_slice(&be(9)); // i
                                             // fi Ligature @ 38
        out[38..40].copy_from_slice(&be(102)); // ligatureGlyph
        out[40..42].copy_from_slice(&be(2)); // componentCount
        out[42..44].copy_from_slice(&be(9)); // i
                                             // Coverage Format 1 @ 44
        out[44..46].copy_from_slice(&be(1)); // coverage format
        out[46..48].copy_from_slice(&be(2)); // glyphCount
        out[48..50].copy_from_slice(&be(5)); // e
        out[50..52].copy_from_slice(&be(6)); // f
        out
    }

    #[test]
    fn ligature_subst_example_6_round_trip() {
        // Replays the spec's Example 6: Coverage = {e, f}; e → [etc];
        // f → [ffi, fi].
        let raw = build_example_6_subtable();
        let ls = LigatureSubst::parse(&raw).unwrap();
        assert_eq!(ls.format(), 1);
        assert_eq!(ls.ligature_set_count(), 2);

        // e-set: one ligature "etc" matching glyphs [e=5, t=20, c=21].
        let e_set = ls.ligature_set(0).unwrap().unwrap();
        assert_eq!(e_set.ligature_count(), 1);
        let etc = e_set.ligature(0).unwrap().unwrap();
        assert_eq!(etc.ligature_glyph(), 100);
        assert_eq!(etc.component_count(), 3);
        let etc_tail: Vec<_> = etc.component_glyphs().collect();
        assert_eq!(etc_tail, vec![20, 21]);

        // f-set: ffi (preferred) then fi.
        let f_set = ls.ligature_set(1).unwrap().unwrap();
        assert_eq!(f_set.ligature_count(), 2);
        let ffi = f_set.ligature(0).unwrap().unwrap();
        assert_eq!(ffi.ligature_glyph(), 101);
        assert_eq!(ffi.component_count(), 3);
        let ffi_tail: Vec<_> = ffi.component_glyphs().collect();
        assert_eq!(ffi_tail, vec![6, 9]);
        let fi = f_set.ligature(1).unwrap().unwrap();
        assert_eq!(fi.ligature_glyph(), 102);
        assert_eq!(fi.component_count(), 2);
        let fi_tail: Vec<_> = fi.component_glyphs().collect();
        assert_eq!(fi_tail, vec![9]);
    }

    #[test]
    fn ligature_subst_substitute_matches_etc() {
        let raw = build_example_6_subtable();
        let ls = LigatureSubst::parse(&raw).unwrap();
        // Input sequence (e, t, c) → etc-ligature (gid 100), 3 glyphs
        // consumed.
        assert_eq!(ls.substitute(&[5, 20, 21]), Some((100, 3)));
        // Trailing glyphs past the ligature length are ignored.
        assert_eq!(ls.substitute(&[5, 20, 21, 99]), Some((100, 3)));
    }

    #[test]
    fn ligature_subst_substitute_prefers_ffi_over_fi() {
        let raw = build_example_6_subtable();
        let ls = LigatureSubst::parse(&raw).unwrap();
        // Per spec: "the order in the Ligature offset array defines
        // the preference for using the ligatures". ffi precedes fi in
        // f-set's offset list, so an (f, f, i) input matches ffi first
        // — even though (f, f) is not a valid fi prefix, this fixture
        // mostly demonstrates that the *first* matching ligature wins.
        assert_eq!(ls.substitute(&[6, 6, 9]), Some((101, 3)));
        // An (f, i) input — too short for ffi — falls through to fi.
        assert_eq!(ls.substitute(&[6, 9]), Some((102, 2)));
    }

    #[test]
    fn ligature_subst_substitute_returns_none_when_no_match() {
        let raw = build_example_6_subtable();
        let ls = LigatureSubst::parse(&raw).unwrap();
        // Empty input → None.
        assert_eq!(ls.substitute(&[]), None);
        // First glyph uncovered → None.
        assert_eq!(ls.substitute(&[7, 9]), None);
        // First glyph covered (e) but no Ligature in e-set matches the
        // tail (e-set only contains "etc" which expects t, c).
        assert_eq!(ls.substitute(&[5, 0, 0]), None);
        // First glyph f but no matching tail (f-set wants either
        // [f, i] or [f, i] — wait, ffi tail is [f, i] and fi tail is [i].
        // An (f, x) input where x != i and second-glyph != f matches
        // nothing.
        assert_eq!(ls.substitute(&[6, 99]), None);
    }

    #[test]
    fn ligature_subst_iter_walks_coverage_in_order() {
        let raw = build_example_6_subtable();
        let ls = LigatureSubst::parse(&raw).unwrap();
        let glyphs: Vec<_> = ls.iter().map(|(g, _)| g).collect();
        assert_eq!(glyphs, vec![5, 6]);
        // Each set still decodes from the iter view.
        for (_, set_res) in ls.iter() {
            let set = set_res.unwrap();
            assert!(set.ligature_count() >= 1);
        }
    }

    #[test]
    fn ligature_subst_rejects_unknown_format() {
        // format = 2 — undefined for Lookup Type 4.
        let mut raw = vec![0u8; 16];
        raw[0..2].copy_from_slice(&be(2));
        raw[2..4].copy_from_slice(&be(8));
        // Plausible coverage payload at offset 8.
        raw[8..10].copy_from_slice(&be(1));
        raw[10..12].copy_from_slice(&be(1));
        raw[12..14].copy_from_slice(&be(5));
        assert!(matches!(
            LigatureSubst::parse(&raw),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn ligature_subst_rejects_truncated_set_offsets_array() {
        // Header claims ligatureSetCount = 4 (needs 8 bytes of
        // setOffsets at offsets 6..14) but the buffer ends mid-array.
        // The Coverage table is placed in-range so that the
        // coverageOffset check passes and the trailing-array length
        // check actually fires.
        //
        //   0  / 2 / format = 1
        //   2  / 2 / coverageOffset = 8   (must be < buffer.len() = 12)
        //   4  / 2 / ligatureSetCount = 4 (needs 8 bytes from off 6 →
        //                                  need = 14; buffer = 12.)
        //   6  / 2 / ligatureSetOffsets[0]
        //   8  / 2 / coverage format = 1
        //  10  / 2 / glyphCount = 0
        let mut raw = vec![0u8; 12];
        raw[0..2].copy_from_slice(&be(1));
        raw[2..4].copy_from_slice(&be(8));
        raw[4..6].copy_from_slice(&be(4));
        raw[6..8].copy_from_slice(&be(0));
        raw[8..10].copy_from_slice(&be(1));
        raw[10..12].copy_from_slice(&be(0));
        assert!(matches!(
            LigatureSubst::parse(&raw),
            Err(Error::UnexpectedEof)
        ));
    }

    #[test]
    fn ligature_subst_rejects_coverage_offset_out_of_range() {
        let mut raw = vec![0u8; 10];
        raw[0..2].copy_from_slice(&be(1)); // format
        raw[2..4].copy_from_slice(&be(99)); // coverageOffset past end
        raw[4..6].copy_from_slice(&be(0)); // ligatureSetCount
        assert!(matches!(
            LigatureSubst::parse(&raw),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn ligature_subst_zero_component_count_rejected() {
        // A Ligature with componentCount = 0 is malformed by spec
        // (componentCount counts the first glyph too). Build a 1-set,
        // 1-lig subtable whose Ligature claims componentCount = 0.
        //
        //   0   / 2 / format = 1
        //   2   / 2 / coverageOffset = 14
        //   4   / 2 / ligatureSetCount = 1
        //   6   / 2 / ligatureSetOffsets[0] = 8 (→ off 8)
        //   8   / 2 / ligatureCount = 1
        //  10   / 2 / ligatureOffsets[0] = 4 (→ off 12)
        //  12   / 2 / ligatureGlyph = 50
        //  14   / 2 / componentCount = 0   (overlapping with Coverage —
        //                                   we'll lay it out so it's
        //                                   non-overlapping)
        //  Replan: put Coverage AFTER the Ligature payload.
        //   0   / 2 / format = 1
        //   2   / 2 / coverageOffset = 16
        //   4   / 2 / ligatureSetCount = 1
        //   6   / 2 / ligatureSetOffsets[0] = 8 (→ off 8)
        //   8   / 2 / ligatureCount = 1
        //  10   / 2 / ligatureOffsets[0] = 4 (→ off 12)
        //  12   / 2 / ligatureGlyph = 50
        //  14   / 2 / componentCount = 0
        //  16   / 2 / coverage format = 1
        //  18   / 2 / glyphCount = 1
        //  20   / 2 / glyph = 10
        let mut raw = vec![0u8; 22];
        raw[0..2].copy_from_slice(&be(1));
        raw[2..4].copy_from_slice(&be(16));
        raw[4..6].copy_from_slice(&be(1));
        raw[6..8].copy_from_slice(&be(8));
        raw[8..10].copy_from_slice(&be(1));
        raw[10..12].copy_from_slice(&be(4));
        raw[12..14].copy_from_slice(&be(50));
        raw[14..16].copy_from_slice(&be(0)); // componentCount = 0 → reject
        raw[16..18].copy_from_slice(&be(1));
        raw[18..20].copy_from_slice(&be(1));
        raw[20..22].copy_from_slice(&be(10));
        let ls = LigatureSubst::parse(&raw).unwrap();
        let set = ls.ligature_set(0).unwrap().unwrap();
        let lig = set.ligature(0).unwrap();
        assert!(matches!(lig, Err(Error::BadStructure(_))));
    }

    // -------------- GsubTable::ligature_subst integration --------------

    /// Build a tiny GSUB table whose only lookup is a type-4
    /// LigatureSubst subtable.
    fn build_minimal_ligature_gsub() -> Vec<u8> {
        // Subtable bytes copied wholesale from the Example-6 subtable.
        let sub = build_example_6_subtable();

        // GSUB layout (all offsets relative to start of GSUB):
        //   0   /  10 / header (script=10, feature=22, lookup=36)
        //   10  /  12 / ScriptList (1 record, DFLT @18)
        //   18  /   4 / Script (no LangSys)
        //   22  /  10 / FeatureList (1 record, "liga" @ 30)
        //   30  /   6 / Feature
        //   36  /   4 / LookupList (1 entry → 40)
        //   40  /   8 / Lookup type=4, flag=0, subTableCount=1, subOff=8
        //   48  /  ?  / LigatureSubstFormat1 subtable (sub.len() bytes)
        let head_end = 48 + sub.len();
        let mut bytes = vec![0u8; head_end];
        // header
        bytes[0..2].copy_from_slice(&be(1));
        bytes[2..4].copy_from_slice(&be(0));
        bytes[4..6].copy_from_slice(&be(10));
        bytes[6..8].copy_from_slice(&be(22));
        bytes[8..10].copy_from_slice(&be(36));
        // ScriptList
        bytes[10..12].copy_from_slice(&be(1));
        bytes[12..16].copy_from_slice(b"DFLT");
        bytes[16..18].copy_from_slice(&be(8));
        // Script
        bytes[18..20].copy_from_slice(&be(0));
        bytes[20..22].copy_from_slice(&be(0));
        // FeatureList
        bytes[22..24].copy_from_slice(&be(1));
        bytes[24..28].copy_from_slice(b"liga");
        bytes[28..30].copy_from_slice(&be(8));
        // Feature
        bytes[30..32].copy_from_slice(&be(0));
        bytes[32..34].copy_from_slice(&be(1));
        bytes[34..36].copy_from_slice(&be(0));
        // LookupList
        bytes[36..38].copy_from_slice(&be(1));
        bytes[38..40].copy_from_slice(&be(4));
        // Lookup: type=4, flag=0, subTableCount=1, subtableOffsets=[8]
        bytes[40..42].copy_from_slice(&be(4));
        bytes[42..44].copy_from_slice(&be(0));
        bytes[44..46].copy_from_slice(&be(1));
        bytes[46..48].copy_from_slice(&be(8));
        // Subtable
        bytes[48..head_end].copy_from_slice(&sub);
        bytes
    }

    #[test]
    fn gsub_ligature_subst_end_to_end() {
        let bytes = build_minimal_ligature_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        assert_eq!(g.lookup_count(), 1);
        let l0 = g.lookup(0).unwrap();
        assert_eq!(l0.lookup_type(), GSUB_LOOKUP_TYPE_LIGATURE);

        let ls = g.ligature_subst(0, 0).expect("subtable exists").unwrap();
        assert_eq!(ls.format(), 1);
        // Same end-to-end substitution as the standalone test.
        assert_eq!(ls.substitute(&[5, 20, 21]), Some((100, 3)));
        assert_eq!(ls.substitute(&[6, 6, 9]), Some((101, 3)));
        assert_eq!(ls.substitute(&[6, 9]), Some((102, 2)));
        assert_eq!(ls.substitute(&[7, 7]), None);
    }

    #[test]
    fn gsub_ligature_subst_rejects_non_type_4_lookup() {
        // Reuse the minimal_v10 layout but declare the Lookup as type
        // = 1 (single), then assert the typed accessor rejects.
        let mut bytes = vec![0u8; 54];
        bytes[0..2].copy_from_slice(&be(1));
        bytes[2..4].copy_from_slice(&be(0));
        bytes[4..6].copy_from_slice(&be(10));
        bytes[6..8].copy_from_slice(&be(22));
        bytes[8..10].copy_from_slice(&be(44));
        bytes[10..12].copy_from_slice(&be(1));
        bytes[12..16].copy_from_slice(b"DFLT");
        bytes[16..18].copy_from_slice(&be(8));
        bytes[18..20].copy_from_slice(&be(0));
        bytes[20..22].copy_from_slice(&be(0));
        bytes[22..24].copy_from_slice(&be(1));
        bytes[24..28].copy_from_slice(b"liga");
        bytes[28..30].copy_from_slice(&be(8));
        bytes[30..32].copy_from_slice(&be(0));
        bytes[32..34].copy_from_slice(&be(1));
        bytes[34..36].copy_from_slice(&be(0));
        bytes[44..46].copy_from_slice(&be(1));
        bytes[46..48].copy_from_slice(&be(4));
        // Lookup: declare type = 1 (single), not type 4.
        bytes[48..50].copy_from_slice(&be(1));
        bytes[50..52].copy_from_slice(&be(0));
        bytes[52..54].copy_from_slice(&be(0));

        let g = GsubTable::parse(&bytes).unwrap();
        assert!(matches!(g.ligature_subst(0, 0), Some(Err(_))));
    }

    #[test]
    fn gsub_ligature_subst_out_of_range_indices_return_none() {
        let bytes = build_minimal_ligature_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        // Subtable index past the lookup's subTableCount.
        assert!(g.ligature_subst(0, 1).is_none());
        // Lookup index past the lookupCount.
        assert!(g.ligature_subst(99, 0).is_none());
    }

    // -------------- ExtensionSubst (lookup type 7) ----------------------

    /// Build a SubstExtensionFormat1 subtable wrapping `inner` at the
    /// minimal `extensionOffset = 8` (immediately after the 8-byte
    /// extension header).
    fn build_extension_subst(ext_type: u16, inner: &[u8]) -> Vec<u8> {
        // Layout:
        //   0 / 2 / format = 1
        //   2 / 2 / extensionLookupType
        //   4 / 4 / extensionOffset = 8 (Offset32)
        //   8 / n / wrapped subtable
        let mut out = Vec::new();
        out.extend_from_slice(&be(1)); // format
        out.extend_from_slice(&be(ext_type)); // extensionLookupType
        out.extend_from_slice(&8u32.to_be_bytes()); // extensionOffset
        out.extend_from_slice(inner);
        out
    }

    #[test]
    fn extension_subst_round_trip_wrapping_single_subst() {
        // Wrap a SingleSubstFormat1 (delta = 100 over {20, 21, 22})
        // behind a type-7 extension and resolve it through the typed
        // path. Per spec, the engine proceeds "as though each extension
        // subtable referenced by extensionOffset replaced the type 7
        // subtable that referenced it".
        let inner = build_single_subst_fmt1(100, &[20, 21, 22]);
        let raw = build_extension_subst(GSUB_LOOKUP_TYPE_SINGLE, &inner);
        let ext = ExtensionSubst::parse(&raw).unwrap();
        assert_eq!(ext.format(), 1);
        assert_eq!(ext.extension_lookup_type(), GSUB_LOOKUP_TYPE_SINGLE);
        assert_eq!(ext.extension_offset(), 8);
        // The raw window starts exactly at the wrapped subtable.
        assert_eq!(ext.extension_subtable_bytes(), &inner[..]);

        let ss = ext.as_single_subst().unwrap();
        assert_eq!(ss.format(), 1);
        assert_eq!(ss.substitute(20), Some(120));
        assert_eq!(ss.substitute(23), None);

        // The declared type gates the other resolvers.
        assert!(matches!(
            ext.as_multiple_subst(),
            Err(Error::BadStructure(_))
        ));
        assert!(matches!(
            ext.as_alternate_subst(),
            Err(Error::BadStructure(_))
        ));
        assert!(matches!(
            ext.as_ligature_subst(),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn extension_subst_round_trip_wrapping_ligature_subst() {
        // Same indirection over the spec's Example-6 ligature subtable.
        let inner = build_example_6_subtable();
        let raw = build_extension_subst(GSUB_LOOKUP_TYPE_LIGATURE, &inner);
        let ext = ExtensionSubst::parse(&raw).unwrap();
        assert_eq!(ext.extension_lookup_type(), GSUB_LOOKUP_TYPE_LIGATURE);
        let ls = ext.as_ligature_subst().unwrap();
        assert_eq!(ls.substitute(&[5, 20, 21]), Some((100, 3)));
        assert!(matches!(ext.as_single_subst(), Err(Error::BadStructure(_))));
    }

    #[test]
    fn extension_subst_undecoded_type_exposes_raw_bytes() {
        // extensionLookupType = 8 (reverse chained single) has no typed
        // view yet; the parse must still validate the header and expose
        // the wrapped bytes raw.
        let inner = [0xAAu8, 0xBB, 0xCC, 0xDD];
        let raw = build_extension_subst(GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE, &inner);
        let ext = ExtensionSubst::parse(&raw).unwrap();
        assert_eq!(
            ext.extension_lookup_type(),
            GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE
        );
        assert_eq!(ext.extension_subtable_bytes(), &inner[..]);
        // No typed resolver matches type 8.
        assert!(matches!(ext.as_single_subst(), Err(Error::BadStructure(_))));
        assert!(matches!(
            ext.as_ligature_subst(),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn extension_subst_rejects_unknown_format() {
        let inner = build_single_subst_fmt1(1, &[10]);
        let mut raw = build_extension_subst(GSUB_LOOKUP_TYPE_SINGLE, &inner);
        raw[0..2].copy_from_slice(&be(2)); // format = 2 is undefined
        assert!(matches!(
            ExtensionSubst::parse(&raw),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn extension_subst_rejects_nested_extension_type() {
        // Spec: "The extensionLookupType field must be set to any
        // lookup type other than 7."
        let inner = build_single_subst_fmt1(1, &[10]);
        let raw = build_extension_subst(GSUB_LOOKUP_TYPE_EXTENSION, &inner);
        assert!(matches!(
            ExtensionSubst::parse(&raw),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn extension_subst_rejects_out_of_vocabulary_lookup_type() {
        // The GsubLookupType vocabulary is 1..=8; 0 and 9 are undefined.
        let inner = build_single_subst_fmt1(1, &[10]);
        for bad in [0u16, 9, 0xFFFF] {
            let raw = build_extension_subst(bad, &inner);
            assert!(
                matches!(ExtensionSubst::parse(&raw), Err(Error::BadStructure(_))),
                "extensionLookupType = {bad} must be rejected"
            );
        }
    }

    #[test]
    fn extension_subst_rejects_offset_out_of_range() {
        let inner = build_single_subst_fmt1(1, &[10]);
        // NULL offset: no defined meaning for an extension subtable.
        let mut raw = build_extension_subst(GSUB_LOOKUP_TYPE_SINGLE, &inner);
        raw[4..8].copy_from_slice(&0u32.to_be_bytes());
        assert!(matches!(
            ExtensionSubst::parse(&raw),
            Err(Error::BadStructure(_))
        ));
        // Offset == buffer length: the wrapped subtable would start
        // past the end of the byte window.
        let mut raw = build_extension_subst(GSUB_LOOKUP_TYPE_SINGLE, &inner);
        let len = raw.len() as u32;
        raw[4..8].copy_from_slice(&len.to_be_bytes());
        assert!(matches!(
            ExtensionSubst::parse(&raw),
            Err(Error::BadStructure(_))
        ));
    }

    #[test]
    fn extension_subst_rejects_truncated_header() {
        // The header is 8 bytes (format + extensionLookupType +
        // Offset32); chopping the Offset32 must surface as EOF.
        let inner = build_single_subst_fmt1(1, &[10]);
        let raw = build_extension_subst(GSUB_LOOKUP_TYPE_SINGLE, &inner);
        assert!(matches!(
            ExtensionSubst::parse(&raw[..6]),
            Err(Error::UnexpectedEof)
        ));
        assert!(matches!(
            ExtensionSubst::parse(&raw[..2]),
            Err(Error::UnexpectedEof)
        ));
    }

    // -------------- GsubTable::extension_subst integration --------------

    /// Build a tiny GSUB table whose only lookup is a type-7 extension
    /// wrapping a SingleSubstFormat1 subtable.
    fn build_minimal_extension_gsub() -> Vec<u8> {
        let inner = build_single_subst_fmt1(200, &[50, 51]);
        let sub = build_extension_subst(GSUB_LOOKUP_TYPE_SINGLE, &inner);

        // GSUB layout (all offsets relative to start of GSUB):
        //   0   /  10 / header (script=10, feature=22, lookup=36)
        //   10  /  12 / ScriptList (1 record, DFLT @18)
        //   18  /   4 / Script (no LangSys)
        //   22  /  10 / FeatureList (1 record, "calt" @ 30)
        //   30  /   6 / Feature
        //   36  /   4 / LookupList (1 entry → 40)
        //   40  /   8 / Lookup type=7, flag=0, subTableCount=1, subOff=8
        //   48  /  ?  / SubstExtensionFormat1 subtable (sub.len() bytes)
        let head_end = 48 + sub.len();
        let mut bytes = vec![0u8; head_end];
        // header
        bytes[0..2].copy_from_slice(&be(1));
        bytes[2..4].copy_from_slice(&be(0));
        bytes[4..6].copy_from_slice(&be(10));
        bytes[6..8].copy_from_slice(&be(22));
        bytes[8..10].copy_from_slice(&be(36));
        // ScriptList
        bytes[10..12].copy_from_slice(&be(1));
        bytes[12..16].copy_from_slice(b"DFLT");
        bytes[16..18].copy_from_slice(&be(8));
        // Script
        bytes[18..20].copy_from_slice(&be(0));
        bytes[20..22].copy_from_slice(&be(0));
        // FeatureList
        bytes[22..24].copy_from_slice(&be(1));
        bytes[24..28].copy_from_slice(b"calt");
        bytes[28..30].copy_from_slice(&be(8));
        // Feature
        bytes[30..32].copy_from_slice(&be(0));
        bytes[32..34].copy_from_slice(&be(1));
        bytes[34..36].copy_from_slice(&be(0));
        // LookupList
        bytes[36..38].copy_from_slice(&be(1));
        bytes[38..40].copy_from_slice(&be(4));
        // Lookup: type=7, flag=0, subTableCount=1, subtableOffsets=[8]
        bytes[40..42].copy_from_slice(&be(7));
        bytes[42..44].copy_from_slice(&be(0));
        bytes[44..46].copy_from_slice(&be(1));
        bytes[46..48].copy_from_slice(&be(8));
        // Subtable
        bytes[48..head_end].copy_from_slice(&sub);
        bytes
    }

    #[test]
    fn gsub_extension_subst_end_to_end() {
        let bytes = build_minimal_extension_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        assert_eq!(g.lookup_count(), 1);
        let l0 = g.lookup(0).unwrap();
        assert_eq!(l0.lookup_type(), GSUB_LOOKUP_TYPE_EXTENSION);

        let ext = g.extension_subst(0, 0).expect("subtable exists").unwrap();
        assert_eq!(ext.format(), 1);
        assert_eq!(ext.extension_lookup_type(), GSUB_LOOKUP_TYPE_SINGLE);
        // Resolve the indirection and apply the wrapped substitution.
        let ss = ext.as_single_subst().unwrap();
        assert_eq!(ss.substitute(50), Some(250));
        assert_eq!(ss.substitute(51), Some(251));
        assert_eq!(ss.substitute(52), None);

        // The type-1 accessor must NOT bypass the declared lookup type:
        // the Lookup says 7, so single_subst() rejects it.
        assert!(matches!(g.single_subst(0, 0), Some(Err(_))));
    }

    #[test]
    fn gsub_extension_subst_rejects_non_type_7_lookup() {
        // The ligature GSUB declares its lookup as type 4.
        let bytes = build_minimal_ligature_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        assert!(matches!(g.extension_subst(0, 0), Some(Err(_))));
    }

    #[test]
    fn gsub_extension_subst_out_of_range_indices_return_none() {
        let bytes = build_minimal_extension_gsub();
        let g = GsubTable::parse(&bytes).unwrap();
        // Subtable index past the lookup's subTableCount.
        assert!(g.extension_subst(0, 1).is_none());
        // Lookup index past the lookupCount.
        assert!(g.extension_subst(99, 0).is_none());
    }
}