oxideav-ttf 0.1.4

Pure-Rust TrueType font parser for the oxideav framework — sfnt + cmap + glyf + hmtx + GSUB ligatures + GPOS kerning
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
//! `GPOS` — Glyph Positioning Table.
//!
//! Supported lookup types:
//! - **LookupType 1** (Single Adjustment Positioning) — Format 1: a
//!   single shared `ValueRecord` applied to every glyph the coverage
//!   table lists. Format 2: per-glyph `ValueRecord` indexed by the
//!   coverage index. Returns the four geometric fields (`xPlacement`,
//!   `yPlacement`, `xAdvance`, `yAdvance`) packed into a [`PosValue`]
//!   record. Used by `kern` features that don't need pair-context plus
//!   width-adjustment features (e.g. `cpsp`).
//! - **LookupType 2** (Pair Adjustment Positioning) — kerning. Both
//!   PairPosFormat1 (per-pair adjustments via Coverage + PairSet) and
//!   PairPosFormat2 (class-pair grid) are supported. We extract only
//!   the `xAdvance` adjustment of the first glyph in the pair — that's
//!   all "kerning" means for our consumer crate.
//! - **LookupType 3** (Cursive Attachment) — entry/exit anchor pairs
//!   on consecutive glyphs. Used by Arabic Nastaliq + script-font
//!   cursive chaining: the *exit* anchor of glyph N is chained to the
//!   *entry* anchor of glyph N+1, with the second glyph's pen origin
//!   shifted so the two anchors coincide. Returns
//!   [`CursiveAttachment`] (entry + exit points, each optional). Only
//!   CursivePosFormat1 is defined by the spec.
//! - **LookupType 4** (Mark-to-Base Attachment) — diacritic positioning
//!   for a mark glyph above / below a base glyph. Returns the offset
//!   `(dx, dy)` in font units that, when added to the mark's pen
//!   position, snaps the mark's anchor onto the base's anchor. Only
//!   MarkBasePosFormat1 is defined by the spec; both Anchor format 1
//!   (plain x/y) and format 3 (x/y + device offsets, which we ignore)
//!   are accepted. Format 2 (anchor point) is treated as format 1
//!   because we don't run the TT bytecode.
//! - **LookupType 5** (Mark-to-Ligature Attachment) — like LookupType 4
//!   but the second glyph is a *ligature* whose component the mark
//!   attaches to is selected by the caller. Each ligature carries one
//!   anchor *per (component, mark class)* slot. Returns `(dx, dy)` to
//!   shift the mark's pen origin so its class anchor lands on the
//!   selected component's anchor. Closes the "fi + dot-above"
//!   ligature+mark gap.
//! - **LookupType 6** (Mark-to-Mark Attachment) — mark-on-mark stacking
//!   used when a base glyph already carries one diacritic and a second
//!   diacritic must sit on top of (or below) the first. Layout-wise the
//!   sub-table is identical to MarkBasePos but interprets coverage 2 as
//!   the *previous mark* rather than the base. Returns `(dx, dy)` in
//!   font units to add to the second mark's pen origin.
//! - **LookupType 8** (Chained Contexts Positioning) — same wire shape
//!   as GSUB LookupType 6 (formats 1/2/3) but each
//!   `PosLookupRecord { sequenceIndex, lookupListIndex }` references
//!   another GPOS lookup. The walker returns a list of [`PosRecord`]s
//!   (`absolute index`, four geometric deltas) that the higher-level
//!   shaper folds into its glyph-position state. Nested LookupType 1 /
//!   2 / 4 / 6 / 8 dispatches are supported; the recursion fence is
//!   the same `MAX_NESTED_LOOKUP_DEPTH = 8` we use in GSUB.
//!
//! ExtensionPos (LookupType 9) is unwrapped transparently for every
//! supported sub-type AND when it sits as the outer wrapper around the
//! lookup itself (i.e. `apply_lookup_type_X` accepts an index whose
//! lookup is `kind=9, inner=X` exactly the same as a plain `kind=X`).
//!
//! Spec: Microsoft OpenType §"GPOS — Glyph Positioning Table",
//! §"Common Table Formats", Apple TrueType Reference §"GPOS",
//! ISO/IEC 14496-22 §6 (OFF).

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

const LOOKUP_SINGLE_POS: u16 = 1;
const LOOKUP_PAIR_POS: u16 = 2;
const LOOKUP_CURSIVE_POS: u16 = 3;
const LOOKUP_MARK_BASE_POS: u16 = 4;
const LOOKUP_MARK_LIGATURE_POS: u16 = 5;
const LOOKUP_MARK_MARK_POS: u16 = 6;
const LOOKUP_CHAIN_CONTEXT_POS: u16 = 8;
const LOOKUP_EXTENSION_POS: u16 = 9;

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

// ValueFormat bits (low byte holds the four geometric flags).
const VF_X_PLACEMENT: u16 = 0x0001;
const VF_Y_PLACEMENT: u16 = 0x0002;
const VF_X_ADVANCE: u16 = 0x0004;
const VF_Y_ADVANCE: u16 = 0x0008;
// High byte holds device-table offsets which we ignore (no TT
// hinting); we still account for their on-disk size when walking
// ValueRecords so subsequent fields are read at the right offset.
const VF_X_PLA_DEVICE: u16 = 0x0010;
const VF_Y_PLA_DEVICE: u16 = 0x0020;
const VF_X_ADV_DEVICE: u16 = 0x0040;
const VF_Y_ADV_DEVICE: u16 = 0x0080;

/// A GPOS `ValueRecord` decoded into its four geometric fields.
///
/// Returned by [`GposTable::lookup_single_pos`] and
/// [`GposTable::apply_lookup_type_8`]. All four fields are in TT font
/// units (Y-up); fields the on-disk record's `valueFormat` does NOT
/// flag are returned as `0`. The four device-table offsets in the
/// high byte of `valueFormat` are skipped — we don't run the TT
/// bytecode interpreter so device-pixel snapping is out of scope.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct PosValue {
    pub x_placement: i16,
    pub y_placement: i16,
    pub x_advance: i16,
    pub y_advance: i16,
}

/// One adjustment emitted by a chained-context positioning match.
///
/// `glyph_index` is an *absolute* offset into the input glyph run
/// (not the relative `sequenceIndex` from the on-disk
/// `PosLookupRecord`). The four `value` fields are in TT font units
/// (Y-up). Multiple records may target the same `glyph_index` if the
/// nested lookups stack adjustments — callers should add (not
/// replace) the deltas.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PosRecord {
    pub glyph_index: usize,
    pub value: PosValue,
}

/// Cursive attachment anchors for one glyph (GPOS LookupType 3).
///
/// Each glyph in a cursive lookup carries an *entry* anchor (the
/// connecting point on its leading edge) and an *exit* anchor (the
/// connecting point on its trailing edge). Either can be absent
/// (null offset on disk → `None` here): a "joining" glyph has both,
/// a "first-of-cluster" glyph has only `exit`, a "last-of-cluster"
/// glyph has only `entry`.
///
/// Returned by [`GposTable::lookup_cursive_attachment`]. The shaper
/// chains glyphs by translating glyph N+1 so its `entry` lands on
/// glyph N's `exit`. Coordinates are in TT font units (Y-up).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct CursiveAttachment {
    /// `(x, y)` of the entry anchor, or `None` if this glyph has no
    /// entry connection (i.e. it's the first glyph in a cursive run).
    pub entry: Option<(i16, i16)>,
    /// `(x, y)` of the exit anchor, or `None` if this glyph has no
    /// exit connection (i.e. it's the last glyph in a cursive run).
    pub exit: Option<(i16, i16)>,
}

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

impl<'a> GposTable<'a> {
    pub fn parse(bytes: &'a [u8]) -> Result<Self, Error> {
        if bytes.len() < 10 {
            return Err(Error::UnexpectedEof);
        }
        let major = read_u16(bytes, 0)?;
        if major != 1 {
            return Err(Error::BadStructure("GPOS: unsupported major version"));
        }
        let lookup_list_off = read_u16(bytes, 8)? as u32;
        if lookup_list_off as usize >= bytes.len() {
            return Err(Error::BadOffset);
        }
        Ok(Self {
            bytes,
            lookup_list_off,
        })
    }

    /// Enumerate every lookup in the LookupList as
    /// `(lookup_index, lookup_type, subtable_count)`.
    ///
    /// The reported `lookup_type` is the **effective** type after
    /// unwrapping any LookupType-9 ExtensionPos wrapper — i.e. the
    /// caller sees `2` for a kerning lookup whether it's stored as a
    /// plain LookupType-2 lookup or as a LookupType-9 wrapper around a
    /// LookupType-2 sub-table. `subtable_count` is the on-disk
    /// `subTableCount`, unchanged.
    ///
    /// Use this to find lookups of a specific type without probing
    /// every index — for example, "give me every chained-context
    /// positioning lookup" is `gpos_lookup_list().filter(|(_, t, _)| *t == 8)`.
    pub fn lookup_list(&self) -> impl Iterator<Item = (u16, u16, u16)> + '_ {
        let lookup_count = self
            .bytes
            .get(self.lookup_list_off as usize..)
            .and_then(|s| read_u16(s, 0).ok())
            .unwrap_or(0);
        (0..lookup_count).filter_map(move |i| {
            let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, i)?;
            if lookup.len() < 6 {
                return None;
            }
            let mut kind = read_u16(lookup, 0).ok()?;
            let sub_count = read_u16(lookup, 4).ok()?;
            // Peek through LookupType-9 ExtensionPos to report the
            // wrapped type — callers shouldn't have to know the
            // sub-table is wrapped.
            if kind == LOOKUP_EXTENSION_POS && sub_count > 0 {
                if let Some(t) = peek_extension_type(lookup) {
                    kind = t;
                }
            }
            Some((i, kind, sub_count))
        })
    }

    /// Apply GPOS LookupType 1 (Single Adjustment Positioning) lookup
    /// `lookup_index` to `gid`.
    ///
    /// Returns `Some(PosValue)` when the lookup's coverage covers
    /// `gid`, or `None` when no rule applies. Both formats are
    /// supported:
    ///
    /// - **Format 1** — one shared `ValueRecord` applied to every
    ///   covered glyph (typical for "shift this whole script up by N
    ///   units" features).
    /// - **Format 2** — per-glyph `ValueRecord` indexed by the
    ///   coverage index (per-glyph trim used by `cpsp` etc.).
    ///
    /// ExtensionPos (LookupType 9) wrappers are unwrapped
    /// transparently. Walks every sub-table; first hit wins per the
    /// OpenType "first matching subtable in lookup order" rule.
    pub fn apply_lookup_type_1(&self, lookup_index: u16, gid: u16) -> Option<PosValue> {
        if self.lookup_list_off == 0 {
            return None;
        }
        let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, lookup_index)?;
        if lookup.len() < 6 {
            return None;
        }
        let kind = read_u16(lookup, 0).ok()?;
        let sub_count = read_u16(lookup, 4).ok()? as usize;
        for s in 0..sub_count {
            let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
            let sub = lookup.get(sub_off..)?;
            let (effective_kind, effective_sub) = unwrap_extension(kind, sub)?;
            if effective_kind != LOOKUP_SINGLE_POS {
                continue;
            }
            if let Some(v) = single_pos_lookup(effective_sub, gid) {
                return Some(v);
            }
        }
        None
    }

    /// Apply GPOS LookupType 3 (Cursive Attachment) lookup
    /// `lookup_index` to `gid`.
    ///
    /// Returns `Some(CursiveAttachment { entry, exit })` when the
    /// lookup's coverage covers `gid`, or `None` when no rule applies.
    /// Either anchor may be `None` (the spec allows null offsets for
    /// glyphs that don't connect on one side: a "first-of-cluster"
    /// glyph carries only `exit`; a "last-of-cluster" carries only
    /// `entry`).
    ///
    /// Cursive attachment is what powers Arabic Nastaliq and most
    /// Brahmic-script "cursive" fonts: the shaper chains glyph N+1 by
    /// translating its pen origin so glyph N+1's `entry` anchor lands
    /// on glyph N's `exit` anchor — i.e. the per-glyph delta to apply
    /// is `prev.exit - this.entry` in (x, y) font units.
    ///
    /// Only CursivePosFormat1 is defined by the spec. Anchor formats
    /// 1, 2 and 3 are accepted (format 2's anchor point and format
    /// 3's device tables are silently ignored). ExtensionPos
    /// (LookupType 9) wrappers — both at the lookup level and at the
    /// sub-table level — are unwrapped transparently.
    pub fn apply_lookup_type_3(&self, lookup_index: u16, gid: u16) -> Option<CursiveAttachment> {
        if self.lookup_list_off == 0 {
            return None;
        }
        let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, lookup_index)?;
        if lookup.len() < 6 {
            return None;
        }
        let kind = read_u16(lookup, 0).ok()?;
        let sub_count = read_u16(lookup, 4).ok()? as usize;
        for s in 0..sub_count {
            let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
            let sub = lookup.get(sub_off..)?;
            let (effective_kind, effective_sub) = unwrap_extension(kind, sub)?;
            if effective_kind != LOOKUP_CURSIVE_POS {
                continue;
            }
            if let Some(v) = cursive_pos_lookup(effective_sub, gid) {
                return Some(v);
            }
        }
        None
    }

    /// Walk every GPOS LookupType-3 (Cursive Attachment) lookup looking
    /// for `gid`'s entry/exit anchor pair.
    ///
    /// Convenience wrapper that scans the entire LookupList rather than
    /// a single lookup index — useful when the caller hasn't resolved
    /// the active feature's lookup-index list yet, or when there's only
    /// one cursive lookup in the font (the common case for Arabic
    /// Nastaliq fonts that ship a single `curs` lookup). Returns the
    /// first hit in lookup order (matches the OpenType "first matching
    /// subtable in lookup order" rule).
    pub fn lookup_cursive_attachment(&self, gid: u16) -> Option<CursiveAttachment> {
        let lookup_list = self.bytes.get(self.lookup_list_off as usize..)?;
        if lookup_list.len() < 2 {
            return None;
        }
        let lookup_count = read_u16(lookup_list, 0).ok()?;
        for i in 0..lookup_count {
            let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, i)?;
            if lookup.len() < 6 {
                continue;
            }
            let kind = read_u16(lookup, 0).ok()?;
            let sub_count = read_u16(lookup, 4).ok()? as usize;
            for s in 0..sub_count {
                let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
                let sub = lookup.get(sub_off..)?;
                let (effective_kind, effective_sub) = unwrap_extension(kind, sub)?;
                if effective_kind != LOOKUP_CURSIVE_POS {
                    continue;
                }
                if let Some(v) = cursive_pos_lookup(effective_sub, gid) {
                    return Some(v);
                }
            }
        }
        None
    }

    /// Apply GPOS LookupType 5 (Mark-to-Ligature Attachment) lookup
    /// `lookup_index` to a `(ligature, ligature_component, mark)` triple.
    ///
    /// Returns `Some((dx, dy))` in font units (TT Y-up) — the offset
    /// to add to the mark's pen origin so its class anchor lands on the
    /// selected component's anchor on the ligature glyph. Returns
    /// `None` when no MarkLigPosFormat1 sub-table covers both glyphs,
    /// when `ligature_component` is out of range for the matched
    /// ligature, or when the mark's class has no anchor on that
    /// component.
    ///
    /// `ligature_component` is **0-indexed**: component 0 is the
    /// first component (e.g. `f` in the `fi` ligature), component 1
    /// is the second (e.g. `i`). The shaper picks the component
    /// based on Unicode-cluster boundaries — a base mark attaches to
    /// the component whose source codepoint it follows.
    ///
    /// Only MarkLigPosFormat1 is defined by the spec. Anchor formats
    /// 1, 2 and 3 are accepted (format 2's anchor point and format
    /// 3's device tables are silently ignored). ExtensionPos
    /// (LookupType 9) wrappers are unwrapped transparently.
    pub fn apply_lookup_type_5(
        &self,
        lookup_index: u16,
        ligature: u16,
        ligature_component: u16,
        mark: u16,
    ) -> Option<(i16, i16)> {
        if self.lookup_list_off == 0 {
            return None;
        }
        let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, lookup_index)?;
        if lookup.len() < 6 {
            return None;
        }
        let kind = read_u16(lookup, 0).ok()?;
        let sub_count = read_u16(lookup, 4).ok()? as usize;
        for s in 0..sub_count {
            let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
            let sub = lookup.get(sub_off..)?;
            let (effective_kind, effective_sub) = unwrap_extension(kind, sub)?;
            if effective_kind != LOOKUP_MARK_LIGATURE_POS {
                continue;
            }
            if let Some(v) =
                mark_ligature_pos_lookup(effective_sub, ligature, ligature_component, mark)
            {
                return Some(v);
            }
        }
        None
    }

    /// Walk every GPOS LookupType-5 (Mark-to-Ligature) lookup looking
    /// for the `(ligature, ligature_component, mark)` triple.
    ///
    /// Convenience wrapper that scans the LookupList rather than a
    /// single lookup index. First hit in lookup order wins.
    pub fn lookup_mark_to_ligature(
        &self,
        ligature: u16,
        ligature_component: u16,
        mark: u16,
    ) -> Option<(i16, i16)> {
        let lookup_list = self.bytes.get(self.lookup_list_off as usize..)?;
        if lookup_list.len() < 2 {
            return None;
        }
        let lookup_count = read_u16(lookup_list, 0).ok()?;
        for i in 0..lookup_count {
            let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, i)?;
            if lookup.len() < 6 {
                continue;
            }
            let kind = read_u16(lookup, 0).ok()?;
            let sub_count = read_u16(lookup, 4).ok()? as usize;
            for s in 0..sub_count {
                let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
                let sub = lookup.get(sub_off..)?;
                let (effective_kind, effective_sub) = unwrap_extension(kind, sub)?;
                if effective_kind != LOOKUP_MARK_LIGATURE_POS {
                    continue;
                }
                if let Some(v) =
                    mark_ligature_pos_lookup(effective_sub, ligature, ligature_component, mark)
                {
                    return Some(v);
                }
            }
        }
        None
    }

    /// Apply GPOS LookupType 8 (Chained Contexts Positioning) lookup
    /// `lookup_index` to the glyph run starting at `pos`.
    ///
    /// Returns `Some(records)` — a `Vec<PosRecord>` listing every
    /// per-glyph adjustment the matched chain rule emits — when one
    /// of the lookup's sub-tables matches the
    /// `(backtrack, input, lookahead)` window around `pos`. Each
    /// `PosRecord.glyph_index` is an absolute offset into `gids`. The
    /// caller folds the deltas into its own glyph-position state.
    ///
    /// All three sub-table formats are supported (same wire format as
    /// GSUB LookupType 6 — only the per-record dispatch differs):
    ///
    /// - **Format 1** — Coverage on the first input glyph + per-coverage
    ///   ChainPosRuleSet of explicit `(backtrack, input, lookahead)`
    ///   glyph sequences plus per-rule `PosLookupRecord[]`.
    /// - **Format 2** — Coverage on the first input glyph + three
    ///   ClassDefs (backtrack/input/lookahead) + per-input-class
    ///   ChainPosClassSet whose rules are class sequences instead of
    ///   glyph sequences.
    /// - **Format 3** — three independent Coverage[] arrays
    ///   (backtrack / input / lookahead) + a single
    ///   `PosLookupRecord[]`.
    ///
    /// Each `PosLookupRecord { sequenceIndex, lookupListIndex }` is
    /// recursively dispatched into LookupType 1 (single position),
    /// LookupType 2 (pair / kern), LookupType 4 (mark-to-base),
    /// LookupType 6 (mark-to-mark) or LookupType 8 (recursive chain).
    /// Recursion is bounded by `MAX_NESTED_LOOKUP_DEPTH = 8` to defuse
    /// pathological self-referential graphs. ExtensionPos (LookupType 9)
    /// wrappers are unwrapped transparently.
    pub fn apply_lookup_type_8(
        &self,
        lookup_index: u16,
        gids: &[u16],
        pos: usize,
    ) -> Option<Vec<PosRecord>> {
        self.apply_chain_context_at(lookup_index, gids, pos, 0)
    }

    /// Look up the mark-to-base attachment offset for a `(base, mark)`
    /// glyph pair. Returns `(dx, dy)` in font units (TT Y-up convention)
    /// to add to the mark's pen origin so its anchor lands on the base's
    /// anchor for the mark's class.
    ///
    /// Returns `None` if no MarkBasePosFormat1 sub-table covers both
    /// glyphs, or if the mark's class has no anchor on this base.
    ///
    /// Walks every LookupType 4 sub-table; the first hit wins (matches
    /// the OpenType "first matching subtable in lookup order" rule).
    pub fn lookup_mark_to_base(&self, base: u16, mark: u16) -> Option<(i16, i16)> {
        let lookup_list = self.bytes.get(self.lookup_list_off as usize..)?;
        if lookup_list.len() < 2 {
            return None;
        }
        let lookup_count = read_u16(lookup_list, 0).ok()?;
        for i in 0..lookup_count {
            let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, i)?;
            if lookup.len() < 6 {
                continue;
            }
            let kind = read_u16(lookup, 0).ok()?;
            let sub_count = read_u16(lookup, 4).ok()? as usize;
            for s in 0..sub_count {
                let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
                let sub = lookup.get(sub_off..)?;
                let (effective_kind, effective_sub) = if kind == LOOKUP_EXTENSION_POS {
                    if sub.len() < 8 {
                        continue;
                    }
                    let ext_type = read_u16(sub, 2).ok()?;
                    let ext_off = read_u32(sub, 4).ok()? as usize;
                    let ext = sub.get(ext_off..)?;
                    (ext_type, ext)
                } else {
                    (kind, sub)
                };
                if effective_kind != LOOKUP_MARK_BASE_POS {
                    continue;
                }
                if let Some(v) = mark_base_pos_lookup(effective_sub, base, mark) {
                    return Some(v);
                }
            }
        }
        None
    }

    /// Look up the mark-to-mark attachment offset for a `(mark1, mark2)`
    /// glyph pair, where `mark1` is the *previous* (already-attached)
    /// mark and `mark2` is the new mark we want to stack on top of (or
    /// below) it. Returns `(dx, dy)` in font units (TT Y-up convention)
    /// to add to `mark2`'s pen origin so its anchor lands on `mark1`'s
    /// anchor for `mark2`'s class.
    ///
    /// Returns `None` if no MarkMarkPosFormat1 sub-table covers both
    /// glyphs, or if the mark2's class has no anchor on mark1.
    ///
    /// Walks every LookupType 6 sub-table; the first hit wins (matches
    /// the OpenType "first matching subtable in lookup order" rule).
    pub fn lookup_mark_to_mark(&self, mark1: u16, mark2: u16) -> Option<(i16, i16)> {
        let lookup_list = self.bytes.get(self.lookup_list_off as usize..)?;
        if lookup_list.len() < 2 {
            return None;
        }
        let lookup_count = read_u16(lookup_list, 0).ok()?;
        for i in 0..lookup_count {
            let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, i)?;
            if lookup.len() < 6 {
                continue;
            }
            let kind = read_u16(lookup, 0).ok()?;
            let sub_count = read_u16(lookup, 4).ok()? as usize;
            for s in 0..sub_count {
                let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
                let sub = lookup.get(sub_off..)?;
                let (effective_kind, effective_sub) = if kind == LOOKUP_EXTENSION_POS {
                    if sub.len() < 8 {
                        continue;
                    }
                    let ext_type = read_u16(sub, 2).ok()?;
                    let ext_off = read_u32(sub, 4).ok()? as usize;
                    let ext = sub.get(ext_off..)?;
                    (ext_type, ext)
                } else {
                    (kind, sub)
                };
                if effective_kind != LOOKUP_MARK_MARK_POS {
                    continue;
                }
                if let Some(v) = mark_mark_pos_lookup(effective_sub, mark1, mark2) {
                    return Some(v);
                }
            }
        }
        None
    }

    /// Look up the kerning adjustment between an ordered glyph pair.
    /// Returns `xAdvance` of the first glyph's ValueRecord1 — the only
    /// field a kerning lookup is ever expected to set.
    ///
    /// `gdef` is consulted to skip mark glyphs per the spec's
    /// IGNORE_MARKS lookup-flag. Round 1 honours IGNORE_MARKS by simply
    /// refusing to attempt a lookup whose left or right glyph is a mark
    /// (per the spec, marks shouldn't kern with bases anyway).
    pub fn lookup_kerning(&self, left: u16, right: u16, gdef: Option<&GdefTable<'_>>) -> i16 {
        let lookup_list = match self.bytes.get(self.lookup_list_off as usize..) {
            Some(s) => s,
            None => return 0,
        };
        if lookup_list.len() < 2 {
            return 0;
        }
        let lookup_count = match read_u16(lookup_list, 0) {
            Ok(c) => c,
            Err(_) => return 0,
        };

        for i in 0..lookup_count {
            let lookup = match lookup_table_slice(self.bytes, self.lookup_list_off, i) {
                Some(s) => s,
                None => continue,
            };
            if lookup.len() < 6 {
                continue;
            }
            let kind = match read_u16(lookup, 0) {
                Ok(k) => k,
                Err(_) => continue,
            };
            let flag = read_u16(lookup, 2).unwrap_or(0);
            let ignore_marks = (flag & 0x0008) != 0;
            if ignore_marks {
                if let Some(g) = gdef {
                    if g.is_mark(left) || g.is_mark(right) {
                        continue;
                    }
                }
            }
            let sub_count = read_u16(lookup, 4).unwrap_or(0) as usize;
            for s in 0..sub_count {
                let sub_off = match read_u16(lookup, 6 + s * 2) {
                    Ok(o) => o as usize,
                    Err(_) => continue,
                };
                let sub = match lookup.get(sub_off..) {
                    Some(b) => b,
                    None => continue,
                };
                let (effective_kind, effective_sub) = if kind == LOOKUP_EXTENSION_POS {
                    if sub.len() < 8 {
                        continue;
                    }
                    let ext_type = read_u16(sub, 2).unwrap_or(0);
                    let ext_off = read_u32(sub, 4).unwrap_or(0) as usize;
                    let ext = match sub.get(ext_off..) {
                        Some(s) => s,
                        None => continue,
                    };
                    (ext_type, ext)
                } else {
                    (kind, sub)
                };
                if effective_kind != LOOKUP_PAIR_POS {
                    continue;
                }
                if let Some(v) = pair_pos_lookup(effective_sub, left, right) {
                    return v;
                }
            }
        }
        0
    }
}

/// Walk a PairPos subtable (format 1 or 2) looking for `(left, right)`.
fn pair_pos_lookup(sub: &[u8], left: u16, right: u16) -> Option<i16> {
    if sub.len() < 8 {
        return None;
    }
    let format = read_u16(sub, 0).ok()?;
    let coverage_off = read_u16(sub, 2).ok()? as usize;
    let value_format1 = read_u16(sub, 4).ok()?;
    let value_format2 = read_u16(sub, 6).ok()?;
    let cov = sub.get(coverage_off..)?;
    let cov_idx = coverage_lookup(cov, left)?;
    let v1_size = popcount_u16(value_format1) * 2;
    let v2_size = popcount_u16(value_format2) * 2;
    match format {
        1 => pair_pos_format1(sub, cov_idx, right, value_format1, v1_size, v2_size),
        2 => pair_pos_format2(sub, left, right, value_format1, v1_size, v2_size),
        _ => None,
    }
}

fn pair_pos_format1(
    sub: &[u8],
    cov_idx: u16,
    right: u16,
    value_format1: u16,
    v1_size: usize,
    v2_size: usize,
) -> Option<i16> {
    // Header (10 bytes) + pairSetOffsets[pairSetCount].
    let pair_set_count = read_u16(sub, 8).ok()?;
    if cov_idx >= pair_set_count {
        return None;
    }
    let pair_set_off = read_u16(sub, 10 + cov_idx as usize * 2).ok()? as usize;
    let pair_set = sub.get(pair_set_off..)?;
    if pair_set.len() < 2 {
        return None;
    }
    let pair_value_count = read_u16(pair_set, 0).ok()? as usize;
    // Each PairValueRecord = u16 secondGlyph + valueRecord1 + valueRecord2.
    let record_size = 2 + v1_size + v2_size;
    // Binary-search by secondGlyph.
    let mut lo = 0usize;
    let mut hi = pair_value_count;
    while lo < hi {
        let mid = (lo + hi) / 2;
        let off = 2 + mid * record_size;
        let sg = read_u16(pair_set, off).ok()?;
        if sg == right {
            return Some(extract_x_advance(pair_set, off + 2, value_format1));
        }
        if sg < right {
            lo = mid + 1;
        } else {
            hi = mid;
        }
    }
    None
}

fn pair_pos_format2(
    sub: &[u8],
    left: u16,
    right: u16,
    value_format1: u16,
    v1_size: usize,
    v2_size: usize,
) -> Option<i16> {
    // Header (16 bytes): format, cov, vf1, vf2, classDef1Offset,
    // classDef2Offset, class1Count, class2Count.
    let class_def1_off = read_u16(sub, 8).ok()? as usize;
    let class_def2_off = read_u16(sub, 10).ok()? as usize;
    let _class1_count = read_u16(sub, 12).ok()?;
    let class2_count = read_u16(sub, 14).ok()? as usize;
    let cd1 = sub.get(class_def1_off..)?;
    let cd2 = sub.get(class_def2_off..)?;
    let class1 = class_def_lookup(cd1, left).unwrap_or(0);
    let class2 = class_def_lookup(cd2, right).unwrap_or(0);
    let class2_record_size = v1_size + v2_size;
    let class1_record_size = class2_count * class2_record_size;
    let class1_records_start = 16usize;
    let off = class1_records_start
        + class1 as usize * class1_record_size
        + class2 as usize * class2_record_size;
    if v1_size == 0 {
        return None;
    }
    Some(extract_x_advance(sub, off, value_format1))
}

/// Walk a MarkBasePosFormat1 subtable looking for `(base, mark)` and
/// return the `(dx, dy)` mark-attachment offset in font units.
///
/// MarkBasePosFormat1 layout (OpenType spec § GPOS):
/// ```text
///   u16 format == 1
///   Offset16 markCoverageOffset       // covers all mark glyphs
///   Offset16 baseCoverageOffset       // covers all base glyphs
///   u16 markClassCount
///   Offset16 markArrayOffset
///   Offset16 baseArrayOffset
/// ```
///
/// MarkArray:
/// ```text
///   u16 markCount
///   markRecords[markCount] = { u16 markClass; Offset16 markAnchorOffset; }
/// ```
///
/// BaseArray:
/// ```text
///   u16 baseCount
///   baseRecords[baseCount] = { Offset16 baseAnchorOffset[markClassCount]; }
/// ```
///
/// The returned offset is computed as `base_anchor - mark_anchor` in TT
/// (Y-up) font units. The shaper applies it as `mark.x_offset += dx`,
/// `mark.y_offset += dy` minus the un-attached pen advance for the
/// mark, but the consumer crate handles that — this function returns
/// the raw anchor delta only.
fn mark_base_pos_lookup(sub: &[u8], base: u16, mark: u16) -> Option<(i16, i16)> {
    if sub.len() < 12 {
        return None;
    }
    let format = read_u16(sub, 0).ok()?;
    if format != 1 {
        return None;
    }
    let mark_cov_off = read_u16(sub, 2).ok()? as usize;
    let base_cov_off = read_u16(sub, 4).ok()? as usize;
    let mark_class_count = read_u16(sub, 6).ok()? as usize;
    let mark_array_off = read_u16(sub, 8).ok()? as usize;
    let base_array_off = read_u16(sub, 10).ok()? as usize;

    let mark_cov = sub.get(mark_cov_off..)?;
    let base_cov = sub.get(base_cov_off..)?;
    let mark_idx = coverage_lookup(mark_cov, mark)? as usize;
    let base_idx = coverage_lookup(base_cov, base)? as usize;

    // MarkArray: markCount + markRecord[mark_idx] = (class u16, anchor_off u16)
    let mark_array = sub.get(mark_array_off..)?;
    if mark_array.len() < 2 {
        return None;
    }
    let mark_count = read_u16(mark_array, 0).ok()? as usize;
    if mark_idx >= mark_count {
        return None;
    }
    let mr_off = 2 + mark_idx * 4;
    let mark_class = read_u16(mark_array, mr_off).ok()? as usize;
    let mark_anchor_off_local = read_u16(mark_array, mr_off + 2).ok()? as usize;
    if mark_class >= mark_class_count {
        return None;
    }
    // MarkRecord's markAnchorOffset is relative to the MarkArray start.
    let mark_anchor = mark_array.get(mark_anchor_off_local..)?;
    let (mx, my) = parse_anchor(mark_anchor)?;

    // BaseArray: baseCount + baseRecord[base_idx] = baseAnchorOffset[mark_class_count]
    let base_array = sub.get(base_array_off..)?;
    if base_array.len() < 2 {
        return None;
    }
    let base_count = read_u16(base_array, 0).ok()? as usize;
    if base_idx >= base_count {
        return None;
    }
    let br_off = 2 + base_idx * mark_class_count * 2;
    let base_anchor_off_local = read_u16(base_array, br_off + mark_class * 2).ok()? as usize;
    // A null offset (0) means "no anchor for this class on this base".
    if base_anchor_off_local == 0 {
        return None;
    }
    let base_anchor = base_array.get(base_anchor_off_local..)?;
    let (bx, by) = parse_anchor(base_anchor)?;

    // Mark gets pulled from its own anchor onto the base's anchor:
    //   (dx, dy) = base_anchor - mark_anchor
    Some((bx.wrapping_sub(mx), by.wrapping_sub(my)))
}

/// Walk a MarkMarkPosFormat1 subtable looking for `(mark1, mark2)` and
/// return the `(dx, dy)` mark-on-mark attachment offset in font units.
///
/// MarkMarkPosFormat1 layout (OpenType spec § GPOS LookupType 6) is
/// structurally identical to MarkBasePosFormat1 — only the role of
/// "second glyph" differs (it's a previous mark, not a base). Same
/// MarkArray (mark1 records: class + anchor) and same outer Mark2Array
/// (mark2 records: anchor per class). We share `parse_anchor` and the
/// arithmetic with the mark-to-base path.
fn mark_mark_pos_lookup(sub: &[u8], mark1: u16, mark2: u16) -> Option<(i16, i16)> {
    if sub.len() < 12 {
        return None;
    }
    let format = read_u16(sub, 0).ok()?;
    if format != 1 {
        return None;
    }
    let mark1_cov_off = read_u16(sub, 2).ok()? as usize;
    let mark2_cov_off = read_u16(sub, 4).ok()? as usize;
    let mark_class_count = read_u16(sub, 6).ok()? as usize;
    let mark1_array_off = read_u16(sub, 8).ok()? as usize;
    let mark2_array_off = read_u16(sub, 10).ok()? as usize;

    // Per the OpenType spec the *attaching* mark is mark1 (which we
    // emit as the second mark in source order — the spec uses "mark1"
    // for the to-be-attached glyph); the *attached-to* mark is mark2
    // (the previous, already-positioned mark). The MarkArray covers
    // mark1 (the new glyph) and Mark2Array covers mark2 (the previous
    // glyph). Our argument naming follows source order: `mark1` here
    // is the previous mark, `mark2` is the new one. Map accordingly.
    let mark2_cov = sub.get(mark1_cov_off..)?; // covers the new attaching mark
    let mark1_cov = sub.get(mark2_cov_off..)?; // covers the already-placed mark
    let mark2_idx = coverage_lookup(mark2_cov, mark2)? as usize;
    let mark1_idx = coverage_lookup(mark1_cov, mark1)? as usize;

    // MarkArray (mark1 records — really "the new mark" per spec):
    // markCount + markRecord[mark2_idx] = (class u16, anchor_off u16).
    let new_mark_array = sub.get(mark1_array_off..)?;
    if new_mark_array.len() < 2 {
        return None;
    }
    let new_mark_count = read_u16(new_mark_array, 0).ok()? as usize;
    if mark2_idx >= new_mark_count {
        return None;
    }
    let nr_off = 2 + mark2_idx * 4;
    let new_mark_class = read_u16(new_mark_array, nr_off).ok()? as usize;
    let new_anchor_off_local = read_u16(new_mark_array, nr_off + 2).ok()? as usize;
    if new_mark_class >= mark_class_count {
        return None;
    }
    let new_mark_anchor = new_mark_array.get(new_anchor_off_local..)?;
    let (mx, my) = parse_anchor(new_mark_anchor)?;

    // Mark2Array: mark2Count + mark2Record[mark1_idx] =
    // mark2AnchorOffset[mark_class_count].
    let prev_array = sub.get(mark2_array_off..)?;
    if prev_array.len() < 2 {
        return None;
    }
    let prev_count = read_u16(prev_array, 0).ok()? as usize;
    if mark1_idx >= prev_count {
        return None;
    }
    let pr_off = 2 + mark1_idx * mark_class_count * 2;
    let prev_anchor_off_local = read_u16(prev_array, pr_off + new_mark_class * 2).ok()? as usize;
    if prev_anchor_off_local == 0 {
        return None;
    }
    let prev_anchor = prev_array.get(prev_anchor_off_local..)?;
    let (bx, by) = parse_anchor(prev_anchor)?;

    // Same arithmetic as mark-to-base: pull the attaching mark from its
    // own anchor onto the previous mark's anchor for that class.
    Some((bx.wrapping_sub(mx), by.wrapping_sub(my)))
}

/// Walk a CursivePosFormat1 subtable looking for `gid` and returning
/// its `(entry, exit)` anchor pair.
///
/// CursivePosFormat1 layout (OpenType spec §"Cursive Attachment
/// Positioning Subtable"):
/// ```text
///   u16 format == 1
///   Offset16 coverageOffset
///   u16 entryExitCount
///   EntryExitRecord entryExitRecords[entryExitCount]
///       Offset16 entryAnchorOffset    // 0 = null = no entry
///       Offset16 exitAnchorOffset     // 0 = null = no exit
/// ```
///
/// Both offsets are relative to the **subtable** start. The Coverage
/// table indexes the EntryExitRecord array (record `i` belongs to the
/// `i`th covered glyph in coverage order).
fn cursive_pos_lookup(sub: &[u8], gid: u16) -> Option<CursiveAttachment> {
    if sub.len() < 6 {
        return None;
    }
    let format = read_u16(sub, 0).ok()?;
    if format != 1 {
        return None;
    }
    let coverage_off = read_u16(sub, 2).ok()? as usize;
    let entry_exit_count = read_u16(sub, 4).ok()? as usize;
    let cov = sub.get(coverage_off..)?;
    let cov_idx = coverage_lookup(cov, gid)? as usize;
    if cov_idx >= entry_exit_count {
        return None;
    }
    // Each EntryExitRecord = 4 bytes (two Offset16). Header = 6 bytes.
    let rec_off = 6 + cov_idx * 4;
    if sub.len() < rec_off + 4 {
        return None;
    }
    let entry_off = read_u16(sub, rec_off).ok()? as usize;
    let exit_off = read_u16(sub, rec_off + 2).ok()? as usize;
    let entry = if entry_off == 0 {
        None
    } else {
        sub.get(entry_off..).and_then(parse_anchor)
    };
    let exit = if exit_off == 0 {
        None
    } else {
        sub.get(exit_off..).and_then(parse_anchor)
    };
    // The spec allows both offsets to be 0 (degenerate); we still
    // surface that as Some({None, None}) so the caller can distinguish
    // "covered but no anchors" from "not covered".
    Some(CursiveAttachment { entry, exit })
}

/// Walk a MarkLigPosFormat1 subtable looking for the
/// `(ligature, ligature_component, mark)` triple and return the
/// `(dx, dy)` mark-attachment offset in font units.
///
/// MarkLigPosFormat1 layout (OpenType spec §"Mark-to-Ligature
/// Attachment Positioning Subtable"):
/// ```text
///   u16 format == 1
///   Offset16 markCoverageOffset       // covers all mark glyphs
///   Offset16 ligatureCoverageOffset   // covers all ligature glyphs
///   u16 markClassCount
///   Offset16 markArrayOffset
///   Offset16 ligatureArrayOffset
/// ```
///
/// MarkArray is identical to MarkBasePos / MarkMarkPos:
/// ```text
///   u16 markCount
///   markRecords[markCount] = { u16 markClass; Offset16 markAnchorOffset; }
/// ```
///
/// LigatureArray:
/// ```text
///   u16 ligatureCount
///   Offset16 ligatureAttachOffsets[ligatureCount]
///
///   LigatureAttach (per ligature):
///     u16 componentCount
///     componentRecords[componentCount]:
///       Offset16 ligatureAnchorOffsets[markClassCount]
/// ```
///
/// Returned offset is `ligature_anchor - mark_anchor` in TT (Y-up) font
/// units. The shaper applies it as `mark.x_offset += dx`,
/// `mark.y_offset += dy` minus the un-attached pen advance.
fn mark_ligature_pos_lookup(
    sub: &[u8],
    ligature: u16,
    ligature_component: u16,
    mark: u16,
) -> Option<(i16, i16)> {
    if sub.len() < 12 {
        return None;
    }
    let format = read_u16(sub, 0).ok()?;
    if format != 1 {
        return None;
    }
    let mark_cov_off = read_u16(sub, 2).ok()? as usize;
    let lig_cov_off = read_u16(sub, 4).ok()? as usize;
    let mark_class_count = read_u16(sub, 6).ok()? as usize;
    let mark_array_off = read_u16(sub, 8).ok()? as usize;
    let lig_array_off = read_u16(sub, 10).ok()? as usize;

    let mark_cov = sub.get(mark_cov_off..)?;
    let lig_cov = sub.get(lig_cov_off..)?;
    let mark_idx = coverage_lookup(mark_cov, mark)? as usize;
    let lig_idx = coverage_lookup(lig_cov, ligature)? as usize;

    // MarkArray: markCount + markRecord[mark_idx] = (class u16, anchor_off u16)
    let mark_array = sub.get(mark_array_off..)?;
    if mark_array.len() < 2 {
        return None;
    }
    let mark_count = read_u16(mark_array, 0).ok()? as usize;
    if mark_idx >= mark_count {
        return None;
    }
    let mr_off = 2 + mark_idx * 4;
    let mark_class = read_u16(mark_array, mr_off).ok()? as usize;
    let mark_anchor_off_local = read_u16(mark_array, mr_off + 2).ok()? as usize;
    if mark_class >= mark_class_count {
        return None;
    }
    let mark_anchor = mark_array.get(mark_anchor_off_local..)?;
    let (mx, my) = parse_anchor(mark_anchor)?;

    // LigatureArray: ligatureCount + ligatureAttachOffsets[lig_idx]
    let lig_array = sub.get(lig_array_off..)?;
    if lig_array.len() < 2 {
        return None;
    }
    let lig_count = read_u16(lig_array, 0).ok()? as usize;
    if lig_idx >= lig_count {
        return None;
    }
    let lig_attach_off = read_u16(lig_array, 2 + lig_idx * 2).ok()? as usize;
    let lig_attach = lig_array.get(lig_attach_off..)?;
    if lig_attach.len() < 2 {
        return None;
    }
    let component_count = read_u16(lig_attach, 0).ok()? as usize;
    if (ligature_component as usize) >= component_count {
        return None;
    }
    // Component record = markClassCount Offset16 anchor offsets.
    let comp_record_size = mark_class_count * 2;
    let comp_off = 2 + ligature_component as usize * comp_record_size;
    let lig_anchor_off_local = read_u16(lig_attach, comp_off + mark_class * 2).ok()? as usize;
    // Null offset → this component has no anchor for this mark class.
    if lig_anchor_off_local == 0 {
        return None;
    }
    // Component-anchor offsets are relative to the LigatureAttach start.
    let lig_anchor = lig_attach.get(lig_anchor_off_local..)?;
    let (lx, ly) = parse_anchor(lig_anchor)?;

    Some((lx.wrapping_sub(mx), ly.wrapping_sub(my)))
}

/// Parse an Anchor table. Supports format 1 (plain x/y) and format 3
/// (x/y + device tables which we ignore — not relevant without TT
/// hinting). Format 2 (x/y + anchor point) is read like format 1 since
/// we don't run the bytecode interpreter to resolve the anchor point.
fn parse_anchor(bytes: &[u8]) -> Option<(i16, i16)> {
    if bytes.len() < 6 {
        return None;
    }
    let format = read_u16(bytes, 0).ok()?;
    let x = read_i16(bytes, 2).ok()?;
    let y = read_i16(bytes, 4).ok()?;
    match format {
        1..=3 => Some((x, y)),
        _ => None,
    }
}

/// Read the `xAdvance` field out of a ValueRecord starting at `bytes[off]`,
/// given its `valueFormat`. Returns 0 if `xAdvance` isn't present.
fn extract_x_advance(bytes: &[u8], off: usize, value_format: u16) -> i16 {
    let mut p = off;
    if value_format & VF_X_PLACEMENT != 0 {
        p += 2;
    }
    if value_format & VF_Y_PLACEMENT != 0 {
        p += 2;
    }
    if value_format & VF_X_ADVANCE != 0 {
        return read_i16(bytes, p).unwrap_or(0);
    }
    0
}

/// On-disk byte size of a ValueRecord with `value_format` set.
///
/// Each set bit in the low byte (placement / advance) contributes 2
/// bytes (`int16`); each set bit in the high byte (device-table
/// offsets) contributes 2 bytes (`Offset16`) which we read past but
/// never dereference.
fn value_record_size(value_format: u16) -> usize {
    popcount_u16(value_format) * 2
}

/// Decode a ValueRecord starting at `bytes[off]` per `value_format`.
///
/// The four geometric fields are populated from their corresponding
/// `valueFormat` bits; bits that aren't set leave the field at `0`.
/// Device-table offsets in the high byte are skipped over (we read
/// past them but never dereference them — TT bytecode hinting is out
/// of scope for this crate).
fn parse_value_record(bytes: &[u8], off: usize, value_format: u16) -> PosValue {
    let mut v = PosValue::default();
    let mut p = off;
    if value_format & VF_X_PLACEMENT != 0 {
        v.x_placement = read_i16(bytes, p).unwrap_or(0);
        p += 2;
    }
    if value_format & VF_Y_PLACEMENT != 0 {
        v.y_placement = read_i16(bytes, p).unwrap_or(0);
        p += 2;
    }
    if value_format & VF_X_ADVANCE != 0 {
        v.x_advance = read_i16(bytes, p).unwrap_or(0);
        p += 2;
    }
    if value_format & VF_Y_ADVANCE != 0 {
        v.y_advance = read_i16(bytes, p).unwrap_or(0);
        p += 2;
    }
    // Skip the four device-table offsets (we don't dereference them).
    if value_format & VF_X_PLA_DEVICE != 0 {
        p += 2;
    }
    if value_format & VF_Y_PLA_DEVICE != 0 {
        p += 2;
    }
    if value_format & VF_X_ADV_DEVICE != 0 {
        p += 2;
    }
    if value_format & VF_Y_ADV_DEVICE != 0 {
        p += 2;
    }
    let _ = p;
    v
}

/// Walk a SinglePos sub-table (format 1 or 2) looking for `gid`.
///
/// SinglePosFormat1 layout:
/// ```text
///   u16 format = 1
///   Offset16 coverageOffset
///   u16 valueFormat
///   ValueRecord value
/// ```
///
/// SinglePosFormat2 layout:
/// ```text
///   u16 format = 2
///   Offset16 coverageOffset
///   u16 valueFormat
///   u16 valueCount
///   ValueRecord values[valueCount]   // indexed by coverage index
/// ```
fn single_pos_lookup(sub: &[u8], gid: u16) -> Option<PosValue> {
    if sub.len() < 6 {
        return None;
    }
    let format = read_u16(sub, 0).ok()?;
    let coverage_off = read_u16(sub, 2).ok()? as usize;
    let value_format = read_u16(sub, 4).ok()?;
    let cov = sub.get(coverage_off..)?;
    let cov_idx = coverage_lookup(cov, gid)? as usize;
    let vr_size = value_record_size(value_format);
    match format {
        1 => {
            // Single shared ValueRecord at offset 6.
            Some(parse_value_record(sub, 6, value_format))
        }
        2 => {
            let value_count = read_u16(sub, 6).ok()? as usize;
            if cov_idx >= value_count {
                return None;
            }
            let vr_off = 8 + cov_idx * vr_size;
            if sub.len() < vr_off + vr_size {
                return None;
            }
            Some(parse_value_record(sub, vr_off, value_format))
        }
        _ => None,
    }
}

/// Unwrap a LookupType-9 ExtensionPos subtable to its inner
/// `(effective_kind, effective_sub)`. Returns `None` on truncation.
/// Non-extension sub-tables pass through unchanged.
fn unwrap_extension(kind: u16, sub: &[u8]) -> Option<(u16, &[u8])> {
    if kind == LOOKUP_EXTENSION_POS {
        if sub.len() < 8 {
            return None;
        }
        let ext_type = read_u16(sub, 2).ok()?;
        let ext_off = read_u32(sub, 4).ok()? as usize;
        let ext = sub.get(ext_off..)?;
        Some((ext_type, ext))
    } else {
        Some((kind, sub))
    }
}

/// Peek through a Lookup table that holds a single ExtensionPos
/// sub-table and return the wrapped lookup type.
fn peek_extension_type(lookup: &[u8]) -> Option<u16> {
    if lookup.len() < 8 {
        return None;
    }
    let sub_count = read_u16(lookup, 4).ok()? as usize;
    if sub_count == 0 {
        return None;
    }
    let sub_off = read_u16(lookup, 6).ok()? as usize;
    let sub = lookup.get(sub_off..)?;
    if sub.len() < 8 {
        return None;
    }
    // ExtensionPosFormat1: u16 format=1, u16 extensionLookupType, Offset32.
    read_u16(sub, 2).ok()
}

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

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

/// Outcome of a chained-context positioning match: the input window
/// length plus the `PosLookupRecord` array to apply against it.
#[derive(Debug)]
struct ChainPosMatch {
    input_len: usize,
    records: Vec<PosLookupRecord>,
}

impl<'a> GposTable<'a> {
    fn apply_chain_context_at(
        &self,
        lookup_index: u16,
        gids: &[u16],
        pos: usize,
        depth: u8,
    ) -> Option<Vec<PosRecord>> {
        if depth >= MAX_NESTED_LOOKUP_DEPTH {
            return None;
        }
        if pos >= gids.len() || self.lookup_list_off == 0 {
            return None;
        }
        let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, lookup_index)?;
        if lookup.len() < 6 {
            return None;
        }
        let kind = read_u16(lookup, 0).ok()?;
        let sub_count = read_u16(lookup, 4).ok()? as usize;
        for s in 0..sub_count {
            let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
            let sub = lookup.get(sub_off..)?;
            let (effective_kind, effective_sub) = match unwrap_extension(kind, sub) {
                Some(p) => p,
                None => continue,
            };
            if effective_kind != LOOKUP_CHAIN_CONTEXT_POS {
                continue;
            }
            if effective_sub.len() < 2 {
                continue;
            }
            let format = match read_u16(effective_sub, 0) {
                Ok(v) => v,
                Err(_) => continue,
            };
            let matched = match format {
                1 => chain_context_pos_format1_match(effective_sub, gids, pos),
                2 => chain_context_pos_format2_match(effective_sub, gids, pos),
                3 => chain_context_pos_format3_match(effective_sub, gids, pos),
                _ => None,
            };
            if let Some(m) = matched {
                return Some(self.apply_pos_records(gids, pos, &m, depth));
            }
        }
        None
    }

    /// Apply a chain-context match's `PosLookupRecord[]` against the
    /// input run, returning the accumulated [`PosRecord`] adjustments.
    ///
    /// `m.input_len` glyphs at `gids[pos..pos + input_len]` are the
    /// chained-context "input" window. We walk the records in declared
    /// order, dispatching each `(sequenceIndex, lookupListIndex)` into
    /// the appropriate per-type apply path. Unlike GSUB, GPOS doesn't
    /// rewrite the glyph stream — every nested lookup just emits more
    /// `PosRecord`s into the output list.
    fn apply_pos_records(
        &self,
        gids: &[u16],
        pos: usize,
        m: &ChainPosMatch,
        depth: u8,
    ) -> Vec<PosRecord> {
        let mut out: Vec<PosRecord> = Vec::new();
        for rec in &m.records {
            let seq_idx = rec.sequence_index as usize;
            if seq_idx >= m.input_len {
                continue;
            }
            let abs_idx = pos + seq_idx;
            if abs_idx >= gids.len() {
                continue;
            }
            // Resolve the referenced lookup type and dispatch.
            let lookup =
                match lookup_table_slice(self.bytes, self.lookup_list_off, rec.lookup_index) {
                    Some(s) => s,
                    None => continue,
                };
            if lookup.len() < 2 {
                continue;
            }
            let mut nested_kind = match read_u16(lookup, 0) {
                Ok(v) => v,
                Err(_) => continue,
            };
            // ExtensionPos at the nested lookup level: peek through.
            if nested_kind == LOOKUP_EXTENSION_POS {
                if let Some(t) = peek_extension_type(lookup) {
                    nested_kind = t;
                }
            }
            match nested_kind {
                LOOKUP_SINGLE_POS => {
                    if let Some(v) = self.apply_lookup_type_1(rec.lookup_index, gids[abs_idx]) {
                        out.push(PosRecord {
                            glyph_index: abs_idx,
                            value: v,
                        });
                    }
                }
                LOOKUP_PAIR_POS if abs_idx + 1 < gids.len() => {
                    // Pair-pos under chain context: needs a *right*
                    // glyph at abs_idx + 1. Apply only the xAdvance
                    // delta on the left glyph, matching the standalone
                    // kerning entry point.
                    let dx = self.lookup_kerning(gids[abs_idx], gids[abs_idx + 1], None);
                    if dx != 0 {
                        out.push(PosRecord {
                            glyph_index: abs_idx,
                            value: PosValue {
                                x_advance: dx,
                                ..PosValue::default()
                            },
                        });
                    }
                }
                LOOKUP_CURSIVE_POS if abs_idx > 0 => {
                    // Cursive-pos under chain context: chain glyph N+1
                    // (= abs_idx) onto glyph N (= abs_idx - 1). Compute
                    // `prev.exit - this.entry`; emit only when both
                    // anchors exist for the pair.
                    let prev = self.apply_lookup_type_3(rec.lookup_index, gids[abs_idx - 1]);
                    let curr = self.apply_lookup_type_3(rec.lookup_index, gids[abs_idx]);
                    if let (Some(p), Some(c)) = (prev, curr) {
                        if let (Some((px, py)), Some((cx, cy))) = (p.exit, c.entry) {
                            out.push(PosRecord {
                                glyph_index: abs_idx,
                                value: PosValue {
                                    x_placement: px.wrapping_sub(cx),
                                    y_placement: py.wrapping_sub(cy),
                                    ..PosValue::default()
                                },
                            });
                        }
                    }
                }
                LOOKUP_MARK_BASE_POS if abs_idx + 1 < gids.len() => {
                    // Mark-to-base under chain context: the base is at
                    // abs_idx and the mark sits at abs_idx + 1. (The
                    // spec leaves the matching to the chain rule; the
                    // base/mark roles are implied by the rule's window
                    // shape.)
                    if let Some((dx, dy)) = self.lookup_mark_to_base_via_lookup(
                        rec.lookup_index,
                        gids[abs_idx],
                        gids[abs_idx + 1],
                    ) {
                        out.push(PosRecord {
                            glyph_index: abs_idx + 1,
                            value: PosValue {
                                x_placement: dx,
                                y_placement: dy,
                                ..PosValue::default()
                            },
                        });
                    }
                }
                LOOKUP_MARK_MARK_POS if abs_idx + 1 < gids.len() => {
                    if let Some((dx, dy)) = self.lookup_mark_to_mark_via_lookup(
                        rec.lookup_index,
                        gids[abs_idx],
                        gids[abs_idx + 1],
                    ) {
                        out.push(PosRecord {
                            glyph_index: abs_idx + 1,
                            value: PosValue {
                                x_placement: dx,
                                y_placement: dy,
                                ..PosValue::default()
                            },
                        });
                    }
                }
                LOOKUP_CHAIN_CONTEXT_POS => {
                    if let Some(mut nested) =
                        self.apply_chain_context_at(rec.lookup_index, gids, abs_idx, depth + 1)
                    {
                        out.append(&mut nested);
                    }
                }
                _ => {
                    // Unsupported nested lookup type — silently skip,
                    // matches our GSUB policy for unknown types.
                }
            }
        }
        out
    }

    /// Walk a single LookupType-4 lookup looking for `(base, mark)`.
    /// Used by the chain-context dispatcher when a `PosLookupRecord`
    /// references a specific mark-to-base lookup index. Mirror of
    /// [`Self::lookup_mark_to_base`] scoped to one lookup.
    fn lookup_mark_to_base_via_lookup(
        &self,
        lookup_index: u16,
        base: u16,
        mark: u16,
    ) -> Option<(i16, i16)> {
        let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, lookup_index)?;
        if lookup.len() < 6 {
            return None;
        }
        let kind = read_u16(lookup, 0).ok()?;
        let sub_count = read_u16(lookup, 4).ok()? as usize;
        for s in 0..sub_count {
            let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
            let sub = lookup.get(sub_off..)?;
            let (effective_kind, effective_sub) = unwrap_extension(kind, sub)?;
            if effective_kind != LOOKUP_MARK_BASE_POS {
                continue;
            }
            if let Some(v) = mark_base_pos_lookup(effective_sub, base, mark) {
                return Some(v);
            }
        }
        None
    }

    /// Walk a single LookupType-6 lookup looking for `(mark1, mark2)`.
    /// Companion of [`Self::lookup_mark_to_base_via_lookup`].
    fn lookup_mark_to_mark_via_lookup(
        &self,
        lookup_index: u16,
        mark1: u16,
        mark2: u16,
    ) -> Option<(i16, i16)> {
        let lookup = lookup_table_slice(self.bytes, self.lookup_list_off, lookup_index)?;
        if lookup.len() < 6 {
            return None;
        }
        let kind = read_u16(lookup, 0).ok()?;
        let sub_count = read_u16(lookup, 4).ok()? as usize;
        for s in 0..sub_count {
            let sub_off = read_u16(lookup, 6 + s * 2).ok()? as usize;
            let sub = lookup.get(sub_off..)?;
            let (effective_kind, effective_sub) = unwrap_extension(kind, sub)?;
            if effective_kind != LOOKUP_MARK_MARK_POS {
                continue;
            }
            if let Some(v) = mark_mark_pos_lookup(effective_sub, mark1, mark2) {
                return Some(v);
            }
        }
        None
    }
}

/// Match a ChainContextPosFormat1 sub-table against `gids[pos..]`.
///
/// Layout (per OpenType §"Chained Sequence Context Format 1: simple
/// glyph contexts" — the GPOS LookupType-8 wire format is identical
/// to GSUB LookupType-6 modulo the record array's name):
/// ```text
///   u16 format = 1
///   Offset16 coverageOffset             (input[0] coverage)
///   u16 chainPosRuleSetCount
///   Offset16 chainPosRuleSetOffsets[chainPosRuleSetCount]
///
///   ChainPosRuleSet { u16 chainPosRuleCount; Offset16 chainPosRuleOffsets[]; }
///   ChainPosRule    { u16 backtrackGlyphCount; u16 backtrackSequence[];
///                     u16 inputGlyphCount;     u16 inputSequence[inputGlyphCount-1];
///                     u16 lookaheadGlyphCount; u16 lookaheadSequence[];
///                     u16 posCount;            PosLookupRecord posRecords[]; }
/// ```
fn chain_context_pos_format1_match(sub: &[u8], gids: &[u16], pos: usize) -> Option<ChainPosMatch> {
    if sub.len() < 6 {
        return None;
    }
    let coverage_off = read_u16(sub, 2).ok()? as usize;
    let coverage = sub.get(coverage_off..)?;
    let cov_idx = coverage_lookup(coverage, gids[pos])? as usize;
    let set_count = read_u16(sub, 4).ok()? as usize;
    if cov_idx >= set_count {
        return None;
    }
    let set_off = read_u16(sub, 6 + cov_idx * 2).ok()? as usize;
    let set = sub.get(set_off..)?;
    if set.len() < 2 {
        return None;
    }
    let rule_count = read_u16(set, 0).ok()? as usize;
    for r in 0..rule_count {
        let rule_off = read_u16(set, 2 + r * 2).ok()? as usize;
        let rule = match set.get(rule_off..) {
            Some(b) => b,
            None => continue,
        };
        if let Some(m) = chain_context_pos_format1_rule_match(rule, gids, pos) {
            return Some(m);
        }
    }
    None
}

fn chain_context_pos_format1_rule_match(
    rule: &[u8],
    gids: &[u16],
    pos: usize,
) -> Option<ChainPosMatch> {
    let mut cur = 0usize;
    if rule.len() < cur + 2 {
        return None;
    }
    let bt_count = read_u16(rule, cur).ok()? as usize;
    cur += 2;
    if rule.len() < cur + bt_count * 2 {
        return None;
    }
    if pos < bt_count {
        return None;
    }
    // Backtrack sequence is reverse-text order (closest first).
    for i in 0..bt_count {
        let want = read_u16(rule, cur + i * 2).ok()?;
        if gids[pos - 1 - i] != want {
            return None;
        }
    }
    cur += bt_count * 2;
    if rule.len() < cur + 2 {
        return None;
    }
    let in_count = read_u16(rule, cur).ok()? as usize;
    if in_count == 0 {
        return None;
    }
    cur += 2;
    let in_extra = in_count - 1;
    if rule.len() < cur + in_extra * 2 {
        return None;
    }
    if pos + in_count > gids.len() {
        return None;
    }
    for i in 0..in_extra {
        let want = read_u16(rule, cur + i * 2).ok()?;
        if gids[pos + 1 + i] != want {
            return None;
        }
    }
    cur += in_extra * 2;
    if rule.len() < cur + 2 {
        return None;
    }
    let la_count = read_u16(rule, cur).ok()? as usize;
    cur += 2;
    if rule.len() < cur + la_count * 2 {
        return None;
    }
    if pos + in_count + la_count > gids.len() {
        return None;
    }
    for i in 0..la_count {
        let want = read_u16(rule, cur + i * 2).ok()?;
        if gids[pos + in_count + i] != want {
            return None;
        }
    }
    cur += la_count * 2;
    if rule.len() < cur + 2 {
        return None;
    }
    let pos_count = read_u16(rule, cur).ok()? as usize;
    cur += 2;
    let records = read_pos_lookup_records(rule, cur, pos_count)?;
    Some(ChainPosMatch {
        input_len: in_count,
        records,
    })
}

/// Match a ChainContextPosFormat2 sub-table against `gids[pos..]`.
///
/// Layout:
/// ```text
///   u16 format = 2
///   Offset16 coverageOffset
///   Offset16 backtrackClassDefOffset
///   Offset16 inputClassDefOffset
///   Offset16 lookaheadClassDefOffset
///   u16 chainPosClassSetCount
///   Offset16 chainPosClassSetOffsets[chainPosClassSetCount]
/// ```
fn chain_context_pos_format2_match(sub: &[u8], gids: &[u16], pos: usize) -> Option<ChainPosMatch> {
    if sub.len() < 12 {
        return None;
    }
    let coverage_off = read_u16(sub, 2).ok()? as usize;
    let bt_cd_off = read_u16(sub, 4).ok()? as usize;
    let in_cd_off = read_u16(sub, 6).ok()? as usize;
    let la_cd_off = read_u16(sub, 8).ok()? as usize;
    let set_count = read_u16(sub, 10).ok()? as usize;
    let coverage = sub.get(coverage_off..)?;
    coverage_lookup(coverage, gids[pos])?;
    let in_cd = sub.get(in_cd_off..)?;
    let in_class0 = class_def_lookup(in_cd, gids[pos]).unwrap_or(0);
    if in_class0 as usize >= set_count {
        return None;
    }
    let set_off = read_u16(sub, 12 + in_class0 as usize * 2).ok()? as usize;
    if set_off == 0 {
        return None;
    }
    let set = sub.get(set_off..)?;
    if set.len() < 2 {
        return None;
    }
    let rule_count = read_u16(set, 0).ok()? as usize;
    let bt_cd = sub.get(bt_cd_off..);
    let la_cd = sub.get(la_cd_off..);
    for r in 0..rule_count {
        let rule_off = read_u16(set, 2 + r * 2).ok()? as usize;
        let rule = match set.get(rule_off..) {
            Some(b) => b,
            None => continue,
        };
        if let Some(m) =
            chain_context_pos_format2_rule_match(rule, gids, pos, bt_cd, in_cd, la_cd, in_class0)
        {
            return Some(m);
        }
    }
    None
}

fn chain_context_pos_format2_rule_match(
    rule: &[u8],
    gids: &[u16],
    pos: usize,
    bt_cd: Option<&[u8]>,
    in_cd: &[u8],
    la_cd: Option<&[u8]>,
    in_class0: u16,
) -> Option<ChainPosMatch> {
    let mut cur = 0usize;
    if rule.len() < cur + 2 {
        return None;
    }
    let bt_count = read_u16(rule, cur).ok()? as usize;
    cur += 2;
    if rule.len() < cur + bt_count * 2 {
        return None;
    }
    if pos < bt_count {
        return None;
    }
    if bt_count > 0 {
        let bt_cd = bt_cd?;
        for i in 0..bt_count {
            let want = read_u16(rule, cur + i * 2).ok()?;
            let got = class_def_lookup(bt_cd, gids[pos - 1 - i]).unwrap_or(0);
            if want != got {
                return None;
            }
        }
    }
    cur += bt_count * 2;
    if rule.len() < cur + 2 {
        return None;
    }
    let in_count = read_u16(rule, cur).ok()? as usize;
    if in_count == 0 {
        return None;
    }
    cur += 2;
    let in_extra = in_count - 1;
    if rule.len() < cur + in_extra * 2 {
        return None;
    }
    if pos + in_count > gids.len() {
        return None;
    }
    for i in 0..in_extra {
        let want = read_u16(rule, cur + i * 2).ok()?;
        let got = class_def_lookup(in_cd, gids[pos + 1 + i]).unwrap_or(0);
        if want != got {
            return None;
        }
    }
    cur += in_extra * 2;
    let _ = in_class0;
    if rule.len() < cur + 2 {
        return None;
    }
    let la_count = read_u16(rule, cur).ok()? as usize;
    cur += 2;
    if rule.len() < cur + la_count * 2 {
        return None;
    }
    if pos + in_count + la_count > gids.len() {
        return None;
    }
    if la_count > 0 {
        let la_cd = la_cd?;
        for i in 0..la_count {
            let want = read_u16(rule, cur + i * 2).ok()?;
            let got = class_def_lookup(la_cd, gids[pos + in_count + i]).unwrap_or(0);
            if want != got {
                return None;
            }
        }
    }
    cur += la_count * 2;
    if rule.len() < cur + 2 {
        return None;
    }
    let pos_count = read_u16(rule, cur).ok()? as usize;
    cur += 2;
    let records = read_pos_lookup_records(rule, cur, pos_count)?;
    Some(ChainPosMatch {
        input_len: in_count,
        records,
    })
}

/// Match a ChainContextPosFormat3 sub-table against `gids[pos..]`.
///
/// Layout:
/// ```text
///   u16 format = 3
///   u16 backtrackGlyphCount
///   Offset16 backtrackCoverageOffsets[backtrackGlyphCount]
///   u16 inputGlyphCount
///   Offset16 inputCoverageOffsets[inputGlyphCount]
///   u16 lookaheadGlyphCount
///   Offset16 lookaheadCoverageOffsets[lookaheadGlyphCount]
///   u16 posCount
///   PosLookupRecord posRecords[posCount]
/// ```
fn chain_context_pos_format3_match(sub: &[u8], gids: &[u16], pos: usize) -> Option<ChainPosMatch> {
    if sub.len() < 4 {
        return None;
    }
    let mut cur = 2usize;
    let bt_count = read_u16(sub, cur).ok()? as usize;
    cur += 2;
    if sub.len() < cur + bt_count * 2 {
        return None;
    }
    if pos < bt_count {
        return None;
    }
    for i in 0..bt_count {
        let cov_off = read_u16(sub, cur + i * 2).ok()? as usize;
        let cov = sub.get(cov_off..)?;
        coverage_lookup(cov, gids[pos - 1 - i])?;
    }
    cur += bt_count * 2;
    if sub.len() < cur + 2 {
        return None;
    }
    let in_count = read_u16(sub, cur).ok()? as usize;
    if in_count == 0 {
        return None;
    }
    cur += 2;
    if sub.len() < cur + in_count * 2 {
        return None;
    }
    if pos + in_count > gids.len() {
        return None;
    }
    for i in 0..in_count {
        let cov_off = read_u16(sub, cur + i * 2).ok()? as usize;
        let cov = sub.get(cov_off..)?;
        coverage_lookup(cov, gids[pos + i])?;
    }
    cur += in_count * 2;
    if sub.len() < cur + 2 {
        return None;
    }
    let la_count = read_u16(sub, cur).ok()? as usize;
    cur += 2;
    if sub.len() < cur + la_count * 2 {
        return None;
    }
    if pos + in_count + la_count > gids.len() {
        return None;
    }
    for i in 0..la_count {
        let cov_off = read_u16(sub, cur + i * 2).ok()? as usize;
        let cov = sub.get(cov_off..)?;
        coverage_lookup(cov, gids[pos + in_count + i])?;
    }
    cur += la_count * 2;
    if sub.len() < cur + 2 {
        return None;
    }
    let pos_count = read_u16(sub, cur).ok()? as usize;
    cur += 2;
    let records = read_pos_lookup_records(sub, cur, pos_count)?;
    Some(ChainPosMatch {
        input_len: in_count,
        records,
    })
}

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

    /// Build a tiny GPOS with one PairPosFormat1 subtable: glyph 50
    /// pairs with glyph 60 → xAdvance=-100.
    fn build_simple_pp1() -> Vec<u8> {
        // PairValueRecord: u16 secondGlyph + value record 1 (xAdv only, 2 bytes).
        let mut pvr = Vec::new();
        pvr.extend_from_slice(&60u16.to_be_bytes());
        pvr.extend_from_slice(&(-100i16).to_be_bytes());

        // PairSet: u16 pairValueCount + pairValueRecords.
        let mut pair_set = Vec::new();
        pair_set.extend_from_slice(&1u16.to_be_bytes());
        pair_set.extend_from_slice(&pvr);

        // Coverage format 1: covers glyph 50.
        let mut cov = Vec::new();
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&50u16.to_be_bytes());

        // PairPosFormat1 header (10 bytes) + pairSetOffsets[1].
        let header = 10;
        let pair_set_offsets_size = 2;
        let cov_off = header + pair_set_offsets_size;
        let pair_set_off = cov_off + cov.len();
        let mut pp1 = Vec::new();
        pp1.extend_from_slice(&1u16.to_be_bytes()); // format
        pp1.extend_from_slice(&(cov_off as u16).to_be_bytes());
        pp1.extend_from_slice(&VF_X_ADVANCE.to_be_bytes()); // value_format1
        pp1.extend_from_slice(&0u16.to_be_bytes()); // value_format2
        pp1.extend_from_slice(&1u16.to_be_bytes()); // pairSetCount
        pp1.extend_from_slice(&(pair_set_off as u16).to_be_bytes());
        pp1.extend_from_slice(&cov);
        pp1.extend_from_slice(&pair_set);

        // Lookup: type=2, flag=0, subCount=1, subOffsets=[8].
        let mut lookup = Vec::new();
        lookup.extend_from_slice(&2u16.to_be_bytes());
        lookup.extend_from_slice(&0u16.to_be_bytes());
        lookup.extend_from_slice(&1u16.to_be_bytes());
        lookup.extend_from_slice(&8u16.to_be_bytes());
        lookup.extend_from_slice(&pp1);

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

        // GPOS header.
        let mut gpos = Vec::new();
        gpos.extend_from_slice(&1u16.to_be_bytes());
        gpos.extend_from_slice(&0u16.to_be_bytes());
        gpos.extend_from_slice(&0u16.to_be_bytes());
        gpos.extend_from_slice(&0u16.to_be_bytes());
        gpos.extend_from_slice(&10u16.to_be_bytes());
        gpos.extend_from_slice(&lookup_list);
        gpos
    }

    #[test]
    fn pair_pos_format1_round_trip() {
        let bytes = build_simple_pp1();
        let g = GposTable::parse(&bytes).unwrap();
        assert_eq!(g.lookup_kerning(50, 60, None), -100);
        assert_eq!(g.lookup_kerning(50, 61, None), 0);
        assert_eq!(g.lookup_kerning(99, 60, None), 0);
    }

    /// Build a tiny GPOS with one MarkBasePosFormat1 subtable: base
    /// glyph 10 (anchor 100, 800) with mark glyph 200 (mark class 0,
    /// anchor 50, 0). Expected delta when attaching mark→base:
    /// `(100 - 50, 800 - 0) = (50, 800)`.
    fn build_simple_mark_base() -> Vec<u8> {
        // ---- Anchor tables (format 1: u16 format + i16 x + i16 y) ----
        let mut base_anchor = Vec::new();
        base_anchor.extend_from_slice(&1u16.to_be_bytes());
        base_anchor.extend_from_slice(&100i16.to_be_bytes());
        base_anchor.extend_from_slice(&800i16.to_be_bytes());

        let mut mark_anchor = Vec::new();
        mark_anchor.extend_from_slice(&1u16.to_be_bytes());
        mark_anchor.extend_from_slice(&50i16.to_be_bytes());
        mark_anchor.extend_from_slice(&0i16.to_be_bytes());

        // ---- MarkArray: 1 mark record ----
        // Header (markCount=1) + 1 markRecord (4 bytes: class + offset)
        // = 6 bytes. mark_anchor placed right after, so offset = 6.
        let mut mark_array = Vec::new();
        mark_array.extend_from_slice(&1u16.to_be_bytes());
        mark_array.extend_from_slice(&0u16.to_be_bytes()); // class 0
        mark_array.extend_from_slice(&6u16.to_be_bytes()); // anchor offset
        mark_array.extend_from_slice(&mark_anchor);

        // ---- BaseArray: 1 base record, 1 mark class ----
        // Header (baseCount=1) + 1 baseRecord (1 anchor offset = 2 bytes)
        // = 4 bytes. base_anchor placed right after, so offset = 4.
        let mut base_array = Vec::new();
        base_array.extend_from_slice(&1u16.to_be_bytes());
        base_array.extend_from_slice(&4u16.to_be_bytes());
        base_array.extend_from_slice(&base_anchor);

        // ---- Coverage tables (format 1) ----
        let mut mark_cov = Vec::new();
        mark_cov.extend_from_slice(&1u16.to_be_bytes());
        mark_cov.extend_from_slice(&1u16.to_be_bytes());
        mark_cov.extend_from_slice(&200u16.to_be_bytes());

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

        // ---- MarkBasePosFormat1 subtable ----
        // Header is 12 bytes:
        //   format (2) + markCovOff (2) + baseCovOff (2)
        //   + markClassCount (2) + markArrayOff (2) + baseArrayOff (2)
        let header = 12usize;
        let mark_cov_off = header;
        let base_cov_off = mark_cov_off + mark_cov.len();
        let mark_array_off = base_cov_off + base_cov.len();
        let base_array_off = mark_array_off + mark_array.len();
        let mut mbp = Vec::new();
        mbp.extend_from_slice(&1u16.to_be_bytes()); // format
        mbp.extend_from_slice(&(mark_cov_off as u16).to_be_bytes());
        mbp.extend_from_slice(&(base_cov_off as u16).to_be_bytes());
        mbp.extend_from_slice(&1u16.to_be_bytes()); // markClassCount
        mbp.extend_from_slice(&(mark_array_off as u16).to_be_bytes());
        mbp.extend_from_slice(&(base_array_off as u16).to_be_bytes());
        mbp.extend_from_slice(&mark_cov);
        mbp.extend_from_slice(&base_cov);
        mbp.extend_from_slice(&mark_array);
        mbp.extend_from_slice(&base_array);

        // ---- Lookup: type=4, flag=0, subCount=1, subOffsets=[8] ----
        let mut lookup = Vec::new();
        lookup.extend_from_slice(&4u16.to_be_bytes());
        lookup.extend_from_slice(&0u16.to_be_bytes());
        lookup.extend_from_slice(&1u16.to_be_bytes());
        lookup.extend_from_slice(&8u16.to_be_bytes());
        lookup.extend_from_slice(&mbp);

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

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

    #[test]
    fn mark_to_base_round_trip() {
        let bytes = build_simple_mark_base();
        let g = GposTable::parse(&bytes).unwrap();
        // Expected: base_anchor (100, 800) - mark_anchor (50, 0) = (50, 800).
        assert_eq!(g.lookup_mark_to_base(10, 200), Some((50, 800)));
        // Pair not in coverage → None.
        assert_eq!(g.lookup_mark_to_base(11, 200), None);
        assert_eq!(g.lookup_mark_to_base(10, 201), None);
    }

    #[test]
    fn mark_to_base_missing_table_returns_none() {
        // Reuse the kerning-only fixture: it has no LookupType 4, so
        // lookup_mark_to_base must return None for any pair.
        let bytes = build_simple_pp1();
        let g = GposTable::parse(&bytes).unwrap();
        assert_eq!(g.lookup_mark_to_base(50, 60), None);
    }

    /// Build a tiny GPOS with one MarkMarkPosFormat1 subtable: previous
    /// mark glyph 30 (anchor 60, 1200) and new mark glyph 40 (mark
    /// class 0, anchor 30, 0). Expected delta when stacking new on
    /// previous: `(60 - 30, 1200 - 0) = (30, 1200)`.
    fn build_simple_mark_mark() -> Vec<u8> {
        // ---- Anchor tables (format 1: u16 format + i16 x + i16 y) ----
        let mut prev_anchor = Vec::new();
        prev_anchor.extend_from_slice(&1u16.to_be_bytes());
        prev_anchor.extend_from_slice(&60i16.to_be_bytes());
        prev_anchor.extend_from_slice(&1200i16.to_be_bytes());

        let mut new_anchor = Vec::new();
        new_anchor.extend_from_slice(&1u16.to_be_bytes());
        new_anchor.extend_from_slice(&30i16.to_be_bytes());
        new_anchor.extend_from_slice(&0i16.to_be_bytes());

        // ---- New-mark MarkArray (sub.mark1_array, the *attaching*
        // mark) — covers mark2 (the new glyph in our shaper API).
        // markCount=1 + record (class=0, off=6) + anchor at offset 6.
        let mut new_mark_array = Vec::new();
        new_mark_array.extend_from_slice(&1u16.to_be_bytes());
        new_mark_array.extend_from_slice(&0u16.to_be_bytes()); // class 0
        new_mark_array.extend_from_slice(&6u16.to_be_bytes()); // anchor offset
        new_mark_array.extend_from_slice(&new_anchor);

        // ---- Previous-mark Mark2Array (sub.mark2_array, the
        // already-placed mark) — covers mark1 in our shaper API.
        // mark2Count=1 + record (1 anchor offset = 2 bytes) + anchor
        // at offset 4.
        let mut prev_array = Vec::new();
        prev_array.extend_from_slice(&1u16.to_be_bytes());
        prev_array.extend_from_slice(&4u16.to_be_bytes()); // anchor off
        prev_array.extend_from_slice(&prev_anchor);

        // ---- Coverage tables (format 1) ----
        // sub.mark1_cov covers the new attaching mark (gid 40).
        let mut new_cov = Vec::new();
        new_cov.extend_from_slice(&1u16.to_be_bytes());
        new_cov.extend_from_slice(&1u16.to_be_bytes());
        new_cov.extend_from_slice(&40u16.to_be_bytes());

        // sub.mark2_cov covers the already-placed mark (gid 30).
        let mut prev_cov = Vec::new();
        prev_cov.extend_from_slice(&1u16.to_be_bytes());
        prev_cov.extend_from_slice(&1u16.to_be_bytes());
        prev_cov.extend_from_slice(&30u16.to_be_bytes());

        // ---- MarkMarkPosFormat1 subtable (12-byte header) ----
        let header = 12usize;
        let new_cov_off = header;
        let prev_cov_off = new_cov_off + new_cov.len();
        let new_mark_array_off = prev_cov_off + prev_cov.len();
        let prev_array_off = new_mark_array_off + new_mark_array.len();
        let mut mmp = Vec::new();
        mmp.extend_from_slice(&1u16.to_be_bytes()); // format
        mmp.extend_from_slice(&(new_cov_off as u16).to_be_bytes()); // mark1Cov
        mmp.extend_from_slice(&(prev_cov_off as u16).to_be_bytes()); // mark2Cov
        mmp.extend_from_slice(&1u16.to_be_bytes()); // markClassCount
        mmp.extend_from_slice(&(new_mark_array_off as u16).to_be_bytes());
        mmp.extend_from_slice(&(prev_array_off as u16).to_be_bytes());
        mmp.extend_from_slice(&new_cov);
        mmp.extend_from_slice(&prev_cov);
        mmp.extend_from_slice(&new_mark_array);
        mmp.extend_from_slice(&prev_array);

        // ---- Lookup: type=6, flag=0, subCount=1, subOffsets=[8] ----
        let mut lookup = Vec::new();
        lookup.extend_from_slice(&6u16.to_be_bytes());
        lookup.extend_from_slice(&0u16.to_be_bytes());
        lookup.extend_from_slice(&1u16.to_be_bytes());
        lookup.extend_from_slice(&8u16.to_be_bytes());
        lookup.extend_from_slice(&mmp);

        let mut lookup_list = Vec::new();
        lookup_list.extend_from_slice(&1u16.to_be_bytes());
        lookup_list.extend_from_slice(&4u16.to_be_bytes());
        lookup_list.extend_from_slice(&lookup);

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

    #[test]
    fn mark_to_mark_round_trip() {
        let bytes = build_simple_mark_mark();
        let g = GposTable::parse(&bytes).unwrap();
        // Expected: prev_anchor (60, 1200) - new_anchor (30, 0) = (30, 1200).
        assert_eq!(g.lookup_mark_to_mark(30, 40), Some((30, 1200)));
        // Pair not in coverage → None.
        assert_eq!(g.lookup_mark_to_mark(31, 40), None);
        assert_eq!(g.lookup_mark_to_mark(30, 41), None);
    }

    #[test]
    fn mark_to_mark_missing_table_returns_none() {
        // Reuse the kerning-only fixture: no LookupType 6, so
        // lookup_mark_to_mark must return None for any pair.
        let bytes = build_simple_pp1();
        let g = GposTable::parse(&bytes).unwrap();
        assert_eq!(g.lookup_mark_to_mark(50, 60), None);
        // Mark-to-base fixture also has no LookupType 6.
        let bytes = build_simple_mark_base();
        let g = GposTable::parse(&bytes).unwrap();
        assert_eq!(g.lookup_mark_to_mark(10, 200), None);
    }

    // ---- LookupType 1 (single positioning) -------------------------

    /// Wrap a sub-table into a Lookup{type, flag=0, subCount=1, subOff=8}.
    fn wrap_lookup(lookup_type: u16, sub: &[u8]) -> Vec<u8> {
        let mut out = Vec::new();
        out.extend_from_slice(&lookup_type.to_be_bytes());
        out.extend_from_slice(&0u16.to_be_bytes());
        out.extend_from_slice(&1u16.to_be_bytes());
        out.extend_from_slice(&8u16.to_be_bytes());
        out.extend_from_slice(sub);
        out
    }

    /// Wrap a single Lookup into a GPOS header (LookupList = [lookup]).
    fn wrap_gpos_single(lookup: &[u8]) -> Vec<u8> {
        // LookupList: count=1, offsets=[4]
        let mut lookup_list = Vec::new();
        lookup_list.extend_from_slice(&1u16.to_be_bytes());
        lookup_list.extend_from_slice(&4u16.to_be_bytes());
        lookup_list.extend_from_slice(lookup);
        // GPOS header: major, minor, scriptList, featureList, lookupList=10.
        let mut gpos = Vec::new();
        gpos.extend_from_slice(&1u16.to_be_bytes());
        gpos.extend_from_slice(&0u16.to_be_bytes());
        gpos.extend_from_slice(&0u16.to_be_bytes());
        gpos.extend_from_slice(&0u16.to_be_bytes());
        gpos.extend_from_slice(&10u16.to_be_bytes());
        gpos.extend_from_slice(&lookup_list);
        gpos
    }

    /// SinglePosFormat1 — coverage covers gid 7 + 9, all share xAdv = -150.
    fn build_single_pos_format1() -> Vec<u8> {
        // Coverage format 1: gids [7, 9].
        let mut cov = Vec::new();
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&2u16.to_be_bytes());
        cov.extend_from_slice(&7u16.to_be_bytes());
        cov.extend_from_slice(&9u16.to_be_bytes());
        // Sub-table: format=1, covOff=8, valueFormat=VF_X_ADVANCE,
        //            ValueRecord{x_adv = -150}
        let mut sub = Vec::new();
        sub.extend_from_slice(&1u16.to_be_bytes()); // format
        sub.extend_from_slice(&8u16.to_be_bytes()); // covOff
        sub.extend_from_slice(&VF_X_ADVANCE.to_be_bytes());
        sub.extend_from_slice(&(-150i16).to_be_bytes()); // x_adv
        sub.extend_from_slice(&cov);
        let lookup = wrap_lookup(LOOKUP_SINGLE_POS, &sub);
        wrap_gpos_single(&lookup)
    }

    /// SinglePosFormat2 — gids [7, 9], per-glyph (xPlace, yPlace, xAdv).
    fn build_single_pos_format2() -> Vec<u8> {
        let mut cov = Vec::new();
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&2u16.to_be_bytes());
        cov.extend_from_slice(&7u16.to_be_bytes());
        cov.extend_from_slice(&9u16.to_be_bytes());
        // valueFormat = X_PLACEMENT | Y_PLACEMENT | X_ADVANCE = 7
        let vf = VF_X_PLACEMENT | VF_Y_PLACEMENT | VF_X_ADVANCE;
        // valueRecord size = 6 bytes; valueCount = 2 → 12 bytes of records.
        // header = 8 bytes (format + cov_off + valueFormat + valueCount)
        // + 12 bytes records + cov.len() at the end → cov_off = 20.
        let cov_off = 20u16;
        let mut sub = Vec::new();
        sub.extend_from_slice(&2u16.to_be_bytes()); // format
        sub.extend_from_slice(&cov_off.to_be_bytes());
        sub.extend_from_slice(&vf.to_be_bytes());
        sub.extend_from_slice(&2u16.to_be_bytes()); // valueCount
                                                    // Record [0] (gid 7): x_pl=10 y_pl=20 x_adv=30
        sub.extend_from_slice(&10i16.to_be_bytes());
        sub.extend_from_slice(&20i16.to_be_bytes());
        sub.extend_from_slice(&30i16.to_be_bytes());
        // Record [1] (gid 9): x_pl=-5 y_pl=-15 x_adv=40
        sub.extend_from_slice(&(-5i16).to_be_bytes());
        sub.extend_from_slice(&(-15i16).to_be_bytes());
        sub.extend_from_slice(&40i16.to_be_bytes());
        sub.extend_from_slice(&cov);
        let lookup = wrap_lookup(LOOKUP_SINGLE_POS, &sub);
        wrap_gpos_single(&lookup)
    }

    #[test]
    fn single_pos_format1_returns_shared_value_for_every_covered_glyph() {
        let bytes = build_single_pos_format1();
        let g = GposTable::parse(&bytes).unwrap();
        let v7 = g.apply_lookup_type_1(0, 7).unwrap();
        let v9 = g.apply_lookup_type_1(0, 9).unwrap();
        assert_eq!(v7, v9);
        assert_eq!(v7.x_advance, -150);
        // Off-coverage glyph → None.
        assert_eq!(g.apply_lookup_type_1(0, 8), None);
    }

    #[test]
    fn single_pos_format2_returns_per_glyph_value() {
        let bytes = build_single_pos_format2();
        let g = GposTable::parse(&bytes).unwrap();
        let v7 = g.apply_lookup_type_1(0, 7).unwrap();
        assert_eq!(v7.x_placement, 10);
        assert_eq!(v7.y_placement, 20);
        assert_eq!(v7.x_advance, 30);
        let v9 = g.apply_lookup_type_1(0, 9).unwrap();
        assert_eq!(v9.x_placement, -5);
        assert_eq!(v9.y_placement, -15);
        assert_eq!(v9.x_advance, 40);
        assert_eq!(g.apply_lookup_type_1(0, 8), None);
    }

    #[test]
    fn single_pos_returns_none_when_lookup_index_out_of_range() {
        let bytes = build_single_pos_format1();
        let g = GposTable::parse(&bytes).unwrap();
        assert_eq!(g.apply_lookup_type_1(99, 7), None);
    }

    #[test]
    fn single_pos_returns_none_when_lookup_is_not_type_1() {
        // Re-use the pair-pos kerning fixture — its single lookup is
        // LookupType 2, not 1, so apply_lookup_type_1 must return None.
        let bytes = build_simple_pp1();
        let g = GposTable::parse(&bytes).unwrap();
        assert_eq!(g.apply_lookup_type_1(0, 50), None);
    }

    // ---- LookupType 8 (chained context positioning) ----------------

    /// Build a GPOS table with two lookups:
    ///   lookup 0: SinglePos Format 1, covers gid 10, x_adv = +50
    ///   lookup 1: ChainContextPos Format 1, matches bt=[1] in=[10]
    ///             la=[99], records=[(seq=0, lookupIndex=0)].
    fn build_chain_context_pos_format1() -> Vec<u8> {
        // ---- lookup 0 sub-table: SinglePos Format 1 ----
        let mut cov0 = Vec::new();
        cov0.extend_from_slice(&1u16.to_be_bytes());
        cov0.extend_from_slice(&1u16.to_be_bytes());
        cov0.extend_from_slice(&10u16.to_be_bytes());
        let mut sub0 = Vec::new();
        sub0.extend_from_slice(&1u16.to_be_bytes()); // format
        sub0.extend_from_slice(&8u16.to_be_bytes()); // covOff
        sub0.extend_from_slice(&VF_X_ADVANCE.to_be_bytes());
        sub0.extend_from_slice(&50i16.to_be_bytes());
        sub0.extend_from_slice(&cov0);
        let lookup0 = wrap_lookup(LOOKUP_SINGLE_POS, &sub0);

        // ---- lookup 1 sub-table: ChainContextPos Format 1 ----
        // Coverage covering input[0] = gid 10.
        let mut cov_in = Vec::new();
        cov_in.extend_from_slice(&1u16.to_be_bytes());
        cov_in.extend_from_slice(&1u16.to_be_bytes());
        cov_in.extend_from_slice(&10u16.to_be_bytes());
        // Rule body
        let mut rule = Vec::new();
        rule.extend_from_slice(&1u16.to_be_bytes()); // bt count
        rule.extend_from_slice(&1u16.to_be_bytes()); // bt[0]
        rule.extend_from_slice(&1u16.to_be_bytes()); // in count
                                                     // (no extra inputs for in_count=1)
        rule.extend_from_slice(&1u16.to_be_bytes()); // la count
        rule.extend_from_slice(&99u16.to_be_bytes()); // la[0]
        rule.extend_from_slice(&1u16.to_be_bytes()); // posCount
        rule.extend_from_slice(&0u16.to_be_bytes()); // seqIndex
        rule.extend_from_slice(&0u16.to_be_bytes()); // lookupIndex → 0
                                                     // RuleSet header: count=1 + offset[0] (after the header)
        let rule_set_header_len = 4u16;
        let mut rule_set = Vec::new();
        rule_set.extend_from_slice(&1u16.to_be_bytes());
        rule_set.extend_from_slice(&rule_set_header_len.to_be_bytes());
        rule_set.extend_from_slice(&rule);
        // Sub-table header: 8 bytes (format + cov_off + setCount + setOff[0])
        let header_len = 8u16;
        let cov_off = header_len;
        let set_off = cov_off + cov_in.len() as u16;
        let mut sub1 = Vec::new();
        sub1.extend_from_slice(&1u16.to_be_bytes()); // format
        sub1.extend_from_slice(&cov_off.to_be_bytes());
        sub1.extend_from_slice(&1u16.to_be_bytes()); // setCount
        sub1.extend_from_slice(&set_off.to_be_bytes());
        sub1.extend_from_slice(&cov_in);
        sub1.extend_from_slice(&rule_set);
        let lookup1 = wrap_lookup(LOOKUP_CHAIN_CONTEXT_POS, &sub1);

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

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

    #[test]
    fn chain_context_pos_format1_dispatches_nested_single_pos() {
        let bytes = build_chain_context_pos_format1();
        let g = GposTable::parse(&bytes).unwrap();
        // Run [1, 10, 99]; chain rule fires at pos=1 → emit one
        // PosRecord at glyph_index=1 with x_advance = +50.
        let recs = g.apply_lookup_type_8(1, &[1, 10, 99], 1).unwrap();
        assert_eq!(recs.len(), 1);
        assert_eq!(recs[0].glyph_index, 1);
        assert_eq!(recs[0].value.x_advance, 50);
    }

    #[test]
    fn chain_context_pos_format1_no_match_when_backtrack_or_lookahead_misses() {
        let bytes = build_chain_context_pos_format1();
        let g = GposTable::parse(&bytes).unwrap();
        // Wrong backtrack glyph.
        assert_eq!(g.apply_lookup_type_8(1, &[2, 10, 99], 1), None);
        // No backtrack room.
        assert_eq!(g.apply_lookup_type_8(1, &[10, 99], 0), None);
        // Wrong lookahead.
        assert_eq!(g.apply_lookup_type_8(1, &[1, 10, 50], 1), None);
        // Out-of-range lookup index.
        assert_eq!(g.apply_lookup_type_8(99, &[1, 10, 99], 1), None);
    }

    /// Build a Format-3 chain-context pos sub-table:
    /// backtrack covers [1], input covers [10], lookahead covers [99],
    /// invokes single-pos lookup 0 (gid 10 → x_adv = +50).
    fn build_chain_context_pos_format3() -> Vec<u8> {
        // lookup 0: SinglePos Format 1, gid 10, x_adv = +50.
        let mut cov_lookup0 = Vec::new();
        cov_lookup0.extend_from_slice(&1u16.to_be_bytes());
        cov_lookup0.extend_from_slice(&1u16.to_be_bytes());
        cov_lookup0.extend_from_slice(&10u16.to_be_bytes());
        let mut sub0 = Vec::new();
        sub0.extend_from_slice(&1u16.to_be_bytes());
        sub0.extend_from_slice(&8u16.to_be_bytes());
        sub0.extend_from_slice(&VF_X_ADVANCE.to_be_bytes());
        sub0.extend_from_slice(&50i16.to_be_bytes());
        sub0.extend_from_slice(&cov_lookup0);
        let lookup0 = wrap_lookup(LOOKUP_SINGLE_POS, &sub0);

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

        // Format-3 sub-table header: 18 bytes (same as GSUB version):
        // 2 (format) + 2 (btCount) + 2 (btOff[0]) + 2 (inCount)
        // + 2 (inOff[0]) + 2 (laCount) + 2 (laOff[0]) + 2 (posCount)
        // + 4 (PosLookupRecord[0]) = 20? Actually let's recount:
        // 2+2+2+2+2+2+2+2+4 = 20. Wait, GSUB says 18. But GSUB had bt+in+la
        // counts each followed by ONE offset, then substCount, then 4-byte
        // record. So 2+2+2+2+2+2+2+2+4 = 20. Hmm let me recount the GSUB
        // build: 2+2+2+2+2+2+2+2+4 = 20 in fact. The comment on GSUB said 18
        // but counted wrong. So header_len = 20.
        let header_len: u16 = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 4;
        let bt_off = header_len;
        let in_off = bt_off + cov_bt.len() as u16;
        let la_off = in_off + cov_in.len() as u16;

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

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

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

    #[test]
    fn chain_context_pos_format3_coverage_based_dispatch() {
        let bytes = build_chain_context_pos_format3();
        let g = GposTable::parse(&bytes).unwrap();
        let recs = g.apply_lookup_type_8(1, &[1, 10, 99], 1).unwrap();
        assert_eq!(recs.len(), 1);
        assert_eq!(recs[0].glyph_index, 1);
        assert_eq!(recs[0].value.x_advance, 50);
    }

    #[test]
    fn chain_context_pos_format3_no_match_when_window_short_or_uncovered() {
        let bytes = build_chain_context_pos_format3();
        let g = GposTable::parse(&bytes).unwrap();
        // Backtrack required, pos=0 leaves none.
        assert_eq!(g.apply_lookup_type_8(1, &[10, 99], 0), None);
        // No lookahead.
        assert_eq!(g.apply_lookup_type_8(1, &[1, 10], 1), None);
        // Lookahead glyph not in coverage.
        assert_eq!(g.apply_lookup_type_8(1, &[1, 10, 12], 1), None);
    }

    // ---- Format-2 (class-based) chain-context positioning ----------

    /// Build ChainContextPos Format-2 sub-table where:
    ///   - input class def: gid 10 → class 1, gid 11 → class 1
    ///   - backtrack class def: gid 1 → class 1, gid 2 → class 1
    ///   - lookahead class def: gid 99 → class 1
    ///   - one rule under input class set 1: bt=[1] in=[1] la=[1] +
    ///     PosLookupRecord (seq=0, lookup=0) → SinglePos lookup 0
    fn build_chain_context_pos_format2() -> Vec<u8> {
        // Classdefs are format 2 ranges.
        // ClassDef format 2: u16 format=2, u16 rangeCount, ClassRangeRecord[].
        // Each record = u16 startGlyph, u16 endGlyph, u16 class.
        let mut in_cd = Vec::new();
        in_cd.extend_from_slice(&2u16.to_be_bytes());
        in_cd.extend_from_slice(&1u16.to_be_bytes()); // 1 range
        in_cd.extend_from_slice(&10u16.to_be_bytes()); // start
        in_cd.extend_from_slice(&11u16.to_be_bytes()); // end
        in_cd.extend_from_slice(&1u16.to_be_bytes()); // class

        let mut bt_cd = Vec::new();
        bt_cd.extend_from_slice(&2u16.to_be_bytes());
        bt_cd.extend_from_slice(&1u16.to_be_bytes());
        bt_cd.extend_from_slice(&1u16.to_be_bytes());
        bt_cd.extend_from_slice(&2u16.to_be_bytes());
        bt_cd.extend_from_slice(&1u16.to_be_bytes());

        let mut la_cd = Vec::new();
        la_cd.extend_from_slice(&2u16.to_be_bytes());
        la_cd.extend_from_slice(&1u16.to_be_bytes());
        la_cd.extend_from_slice(&99u16.to_be_bytes());
        la_cd.extend_from_slice(&99u16.to_be_bytes());
        la_cd.extend_from_slice(&1u16.to_be_bytes());

        // Coverage: covers input[0] candidates (gid 10).
        let mut cov = Vec::new();
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&1u16.to_be_bytes());
        cov.extend_from_slice(&10u16.to_be_bytes());

        // Rule body
        let mut rule = Vec::new();
        rule.extend_from_slice(&1u16.to_be_bytes()); // btCount
        rule.extend_from_slice(&1u16.to_be_bytes()); // bt[0] = class 1
        rule.extend_from_slice(&1u16.to_be_bytes()); // inCount = 1, no extras
        rule.extend_from_slice(&1u16.to_be_bytes()); // laCount
        rule.extend_from_slice(&1u16.to_be_bytes()); // la[0] = class 1
        rule.extend_from_slice(&1u16.to_be_bytes()); // posCount
        rule.extend_from_slice(&0u16.to_be_bytes()); // seq
        rule.extend_from_slice(&0u16.to_be_bytes()); // lookupIndex

        // Set: count=1 + offset to rule (after 4-byte set header)
        let mut set = Vec::new();
        set.extend_from_slice(&1u16.to_be_bytes()); // ruleCount
        set.extend_from_slice(&4u16.to_be_bytes()); // offset
        set.extend_from_slice(&rule);

        // Sub-table header: 12 bytes (format + cov + bt_cd + in_cd + la_cd
        // + setCount) + 2 * setCount = 14 bytes. setCount = 2 because
        // class 0 is implicit before class 1; offsets[0] = 0 (no set),
        // offsets[1] = real.
        // header_len: 14 bytes (12 + 2 set offsets)
        let set_count = 2u16;
        let header_len = 12 + (set_count as usize) * 2;
        let cov_off = header_len as u16;
        let bt_cd_off = cov_off + cov.len() as u16;
        let in_cd_off = bt_cd_off + bt_cd.len() as u16;
        let la_cd_off = in_cd_off + in_cd.len() as u16;
        let set_off = la_cd_off + la_cd.len() as u16;

        let mut sub1 = Vec::new();
        sub1.extend_from_slice(&2u16.to_be_bytes()); // format
        sub1.extend_from_slice(&cov_off.to_be_bytes());
        sub1.extend_from_slice(&bt_cd_off.to_be_bytes());
        sub1.extend_from_slice(&in_cd_off.to_be_bytes());
        sub1.extend_from_slice(&la_cd_off.to_be_bytes());
        sub1.extend_from_slice(&set_count.to_be_bytes());
        // setOffsets[0] (class 0) = 0 (no rules)
        sub1.extend_from_slice(&0u16.to_be_bytes());
        // setOffsets[1] (class 1) = set_off
        sub1.extend_from_slice(&set_off.to_be_bytes());
        sub1.extend_from_slice(&cov);
        sub1.extend_from_slice(&bt_cd);
        sub1.extend_from_slice(&in_cd);
        sub1.extend_from_slice(&la_cd);
        sub1.extend_from_slice(&set);

        // lookup 0: SinglePos Format 1, gid 10, x_adv = +50.
        let mut cov0 = Vec::new();
        cov0.extend_from_slice(&1u16.to_be_bytes());
        cov0.extend_from_slice(&1u16.to_be_bytes());
        cov0.extend_from_slice(&10u16.to_be_bytes());
        let mut sub0 = Vec::new();
        sub0.extend_from_slice(&1u16.to_be_bytes());
        sub0.extend_from_slice(&8u16.to_be_bytes());
        sub0.extend_from_slice(&VF_X_ADVANCE.to_be_bytes());
        sub0.extend_from_slice(&50i16.to_be_bytes());
        sub0.extend_from_slice(&cov0);
        let lookup0 = wrap_lookup(LOOKUP_SINGLE_POS, &sub0);

        let lookup1 = wrap_lookup(LOOKUP_CHAIN_CONTEXT_POS, &sub1);

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

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

    #[test]
    fn chain_context_pos_format2_class_based_dispatch() {
        let bytes = build_chain_context_pos_format2();
        let g = GposTable::parse(&bytes).unwrap();
        let recs = g.apply_lookup_type_8(1, &[1, 10, 99], 1).unwrap();
        assert_eq!(recs.len(), 1);
        assert_eq!(recs[0].glyph_index, 1);
        assert_eq!(recs[0].value.x_advance, 50);
    }

    #[test]
    fn chain_context_pos_format2_no_match_when_class_differs() {
        let bytes = build_chain_context_pos_format2();
        let g = GposTable::parse(&bytes).unwrap();
        // gid 5 isn't in any backtrack class → bt class 0 ≠ rule's class 1.
        assert_eq!(g.apply_lookup_type_8(1, &[5, 10, 99], 1), None);
    }

    // ---- LookupList enumeration ------------------------------------

    #[test]
    fn lookup_list_reports_index_type_and_subtable_count() {
        // pp1 fixture: single LookupType-2 lookup with 1 sub-table.
        let bytes = build_simple_pp1();
        let g = GposTable::parse(&bytes).unwrap();
        let v: Vec<_> = g.lookup_list().collect();
        assert_eq!(v, vec![(0u16, 2u16, 1u16)]);

        // Mark-base fixture: single LookupType-4 lookup with 1 sub-table.
        let bytes = build_simple_mark_base();
        let g = GposTable::parse(&bytes).unwrap();
        let v: Vec<_> = g.lookup_list().collect();
        assert_eq!(v, vec![(0u16, 4u16, 1u16)]);

        // Chain-context-pos fixture has two lookups: 1, 8.
        let bytes = build_chain_context_pos_format1();
        let g = GposTable::parse(&bytes).unwrap();
        let v: Vec<_> = g.lookup_list().collect();
        assert_eq!(v, vec![(0u16, 1u16, 1u16), (1u16, 8u16, 1u16)]);
    }

    #[test]
    fn value_record_size_packs_low_byte_only() {
        // VF_X_PLACEMENT | VF_Y_PLACEMENT = 2 fields × 2 bytes = 4.
        assert_eq!(value_record_size(VF_X_PLACEMENT | VF_Y_PLACEMENT), 4);
        // All four geometric + all four device = 8 fields × 2 bytes = 16.
        let all = VF_X_PLACEMENT
            | VF_Y_PLACEMENT
            | VF_X_ADVANCE
            | VF_Y_ADVANCE
            | VF_X_PLA_DEVICE
            | VF_Y_PLA_DEVICE
            | VF_X_ADV_DEVICE
            | VF_Y_ADV_DEVICE;
        assert_eq!(value_record_size(all), 16);
        // Empty value format → 0 bytes.
        assert_eq!(value_record_size(0), 0);
    }

    // ---- LookupType 3 (cursive attachment) -------------------------

    /// Build a CursivePosFormat1 sub-table covering glyphs 5 (entry only),
    /// 6 (both entry and exit), 7 (exit only). Anchor coords are
    /// (entry.x, entry.y) = (10*gid, 100), (exit.x, exit.y) = (20*gid, 200).
    fn build_cursive_pos_format1() -> Vec<u8> {
        // Anchor table format 1: u16 format + i16 x + i16 y = 6 bytes.
        fn anchor(x: i16, y: i16) -> Vec<u8> {
            let mut v = Vec::new();
            v.extend_from_slice(&1u16.to_be_bytes());
            v.extend_from_slice(&x.to_be_bytes());
            v.extend_from_slice(&y.to_be_bytes());
            v
        }
        // Anchors: 6 bytes each. We need:
        //   gid 5 entry (50, 100)
        //   gid 6 entry (60, 100), exit (120, 200)
        //   gid 7 exit  (140, 200)
        let a5_entry = anchor(50, 100);
        let a6_entry = anchor(60, 100);
        let a6_exit = anchor(120, 200);
        let a7_exit = anchor(140, 200);

        // Coverage format 1 covering [5, 6, 7].
        let mut cov = Vec::new();
        cov.extend_from_slice(&1u16.to_be_bytes()); // format
        cov.extend_from_slice(&3u16.to_be_bytes()); // glyphCount
        cov.extend_from_slice(&5u16.to_be_bytes());
        cov.extend_from_slice(&6u16.to_be_bytes());
        cov.extend_from_slice(&7u16.to_be_bytes());

        // Sub-table header: format(2) + covOff(2) + entryExitCount(2)
        // + 3 EntryExitRecords (4 bytes each) = 6 + 12 = 18 bytes
        let header_len = 6 + 3 * 4;
        let cov_off = (header_len + 4 * 6) as u16; // anchors live before coverage
                                                   // anchor placement: right after header.
        let a5_entry_off = header_len as u16;
        let a6_entry_off = a5_entry_off + 6;
        let a6_exit_off = a6_entry_off + 6;
        let a7_exit_off = a6_exit_off + 6;

        let mut sub = Vec::new();
        sub.extend_from_slice(&1u16.to_be_bytes()); // format
        sub.extend_from_slice(&cov_off.to_be_bytes());
        sub.extend_from_slice(&3u16.to_be_bytes()); // entryExitCount
                                                    // Record [0] gid 5: entry only (exit = 0/null)
        sub.extend_from_slice(&a5_entry_off.to_be_bytes());
        sub.extend_from_slice(&0u16.to_be_bytes());
        // Record [1] gid 6: both
        sub.extend_from_slice(&a6_entry_off.to_be_bytes());
        sub.extend_from_slice(&a6_exit_off.to_be_bytes());
        // Record [2] gid 7: exit only (entry = 0/null)
        sub.extend_from_slice(&0u16.to_be_bytes());
        sub.extend_from_slice(&a7_exit_off.to_be_bytes());
        // Anchors
        sub.extend_from_slice(&a5_entry);
        sub.extend_from_slice(&a6_entry);
        sub.extend_from_slice(&a6_exit);
        sub.extend_from_slice(&a7_exit);
        // Coverage
        sub.extend_from_slice(&cov);

        let lookup = wrap_lookup(LOOKUP_CURSIVE_POS, &sub);
        wrap_gpos_single(&lookup)
    }

    #[test]
    fn cursive_pos_format1_returns_entry_and_exit_anchors() {
        let bytes = build_cursive_pos_format1();
        let g = GposTable::parse(&bytes).unwrap();
        // gid 5 has only entry (50, 100).
        let r5 = g.apply_lookup_type_3(0, 5).unwrap();
        assert_eq!(r5.entry, Some((50, 100)));
        assert_eq!(r5.exit, None);
        // gid 6 has both.
        let r6 = g.apply_lookup_type_3(0, 6).unwrap();
        assert_eq!(r6.entry, Some((60, 100)));
        assert_eq!(r6.exit, Some((120, 200)));
        // gid 7 has only exit.
        let r7 = g.apply_lookup_type_3(0, 7).unwrap();
        assert_eq!(r7.entry, None);
        assert_eq!(r7.exit, Some((140, 200)));
    }

    #[test]
    fn cursive_pos_returns_none_off_coverage() {
        let bytes = build_cursive_pos_format1();
        let g = GposTable::parse(&bytes).unwrap();
        assert_eq!(g.apply_lookup_type_3(0, 4), None);
        assert_eq!(g.apply_lookup_type_3(0, 8), None);
    }

    #[test]
    fn cursive_pos_returns_none_when_lookup_is_not_type_3() {
        // Re-use a kerning-only GPOS — its single lookup is type 2.
        let bytes = build_simple_pp1();
        let g = GposTable::parse(&bytes).unwrap();
        assert_eq!(g.apply_lookup_type_3(0, 5), None);
    }

    #[test]
    fn lookup_cursive_attachment_walks_lookup_list() {
        let bytes = build_cursive_pos_format1();
        let g = GposTable::parse(&bytes).unwrap();
        // The convenience walker should find the same anchors
        // regardless of which lookup index hosts them.
        let r6 = g.lookup_cursive_attachment(6).unwrap();
        assert_eq!(r6.entry, Some((60, 100)));
        assert_eq!(r6.exit, Some((120, 200)));
        // No coverage hit on gid 99 in any cursive lookup.
        assert_eq!(g.lookup_cursive_attachment(99), None);
    }

    // ---- LookupType 5 (mark-to-ligature) ---------------------------

    /// Build a MarkLigPosFormat1 sub-table:
    ///   ligature gid 100 with 2 components,
    ///   mark gid 200 (class 0) with anchor (10, 0).
    ///   Component 0 anchor for class 0: (300, 800)
    ///   Component 1 anchor for class 0: (500, 850)
    /// Expected:
    ///   apply(lig=100, comp=0, mark=200) -> (300-10, 800-0) = (290, 800)
    ///   apply(lig=100, comp=1, mark=200) -> (500-10, 850-0) = (490, 850)
    fn build_mark_ligature_pos_format1() -> Vec<u8> {
        fn anchor(x: i16, y: i16) -> Vec<u8> {
            let mut v = Vec::new();
            v.extend_from_slice(&1u16.to_be_bytes());
            v.extend_from_slice(&x.to_be_bytes());
            v.extend_from_slice(&y.to_be_bytes());
            v
        }

        // ---- mark anchor + MarkArray ----
        let mark_anchor = anchor(10, 0);
        // MarkArray: markCount(2) + markRecord (4 bytes: class + offset)
        // = 6 bytes header. Anchor placed right after at offset 6.
        let mut mark_array = Vec::new();
        mark_array.extend_from_slice(&1u16.to_be_bytes()); // markCount
        mark_array.extend_from_slice(&0u16.to_be_bytes()); // class 0
        mark_array.extend_from_slice(&6u16.to_be_bytes()); // anchor offset
        mark_array.extend_from_slice(&mark_anchor);

        // ---- LigatureAttach for ligature 100, 2 components, 1 mark class ----
        // Header: componentCount(2). Each componentRecord = markClassCount * 2
        // = 2 bytes (1 anchor offset). Total component-record block = 4 bytes.
        // Anchors placed right after at offsets:
        //   header(2) + 2 component records (2 bytes each) = 6
        //   comp0 anchor at 6, comp1 anchor at 12.
        let comp0_anchor = anchor(300, 800);
        let comp1_anchor = anchor(500, 850);
        let mut lig_attach = Vec::new();
        lig_attach.extend_from_slice(&2u16.to_be_bytes()); // componentCount
        lig_attach.extend_from_slice(&6u16.to_be_bytes()); // comp0 → offset 6
        lig_attach.extend_from_slice(&12u16.to_be_bytes()); // comp1 → offset 12
        lig_attach.extend_from_slice(&comp0_anchor);
        lig_attach.extend_from_slice(&comp1_anchor);

        // ---- LigatureArray: ligatureCount(1) + offset to lig_attach
        // Header = 2 + 2 = 4 bytes. lig_attach starts at offset 4.
        let mut lig_array = Vec::new();
        lig_array.extend_from_slice(&1u16.to_be_bytes()); // ligatureCount
        lig_array.extend_from_slice(&4u16.to_be_bytes()); // offset
        lig_array.extend_from_slice(&lig_attach);

        // ---- Coverage tables ----
        let mut mark_cov = Vec::new();
        mark_cov.extend_from_slice(&1u16.to_be_bytes());
        mark_cov.extend_from_slice(&1u16.to_be_bytes());
        mark_cov.extend_from_slice(&200u16.to_be_bytes());

        let mut lig_cov = Vec::new();
        lig_cov.extend_from_slice(&1u16.to_be_bytes());
        lig_cov.extend_from_slice(&1u16.to_be_bytes());
        lig_cov.extend_from_slice(&100u16.to_be_bytes());

        // ---- MarkLigPosFormat1 sub-table (12-byte header) ----
        let header = 12usize;
        let mark_cov_off = header;
        let lig_cov_off = mark_cov_off + mark_cov.len();
        let mark_array_off = lig_cov_off + lig_cov.len();
        let lig_array_off = mark_array_off + mark_array.len();

        let mut mlp = Vec::new();
        mlp.extend_from_slice(&1u16.to_be_bytes()); // format
        mlp.extend_from_slice(&(mark_cov_off as u16).to_be_bytes());
        mlp.extend_from_slice(&(lig_cov_off as u16).to_be_bytes());
        mlp.extend_from_slice(&1u16.to_be_bytes()); // markClassCount
        mlp.extend_from_slice(&(mark_array_off as u16).to_be_bytes());
        mlp.extend_from_slice(&(lig_array_off as u16).to_be_bytes());
        mlp.extend_from_slice(&mark_cov);
        mlp.extend_from_slice(&lig_cov);
        mlp.extend_from_slice(&mark_array);
        mlp.extend_from_slice(&lig_array);

        let lookup = wrap_lookup(LOOKUP_MARK_LIGATURE_POS, &mlp);
        wrap_gpos_single(&lookup)
    }

    #[test]
    fn mark_to_ligature_attaches_to_each_component() {
        let bytes = build_mark_ligature_pos_format1();
        let g = GposTable::parse(&bytes).unwrap();
        // Component 0: (300 - 10, 800 - 0) = (290, 800)
        assert_eq!(g.apply_lookup_type_5(0, 100, 0, 200), Some((290, 800)));
        // Component 1: (500 - 10, 850 - 0) = (490, 850)
        assert_eq!(g.apply_lookup_type_5(0, 100, 1, 200), Some((490, 850)));
    }

    #[test]
    fn mark_to_ligature_returns_none_for_out_of_range_component() {
        let bytes = build_mark_ligature_pos_format1();
        let g = GposTable::parse(&bytes).unwrap();
        // Only 2 components (0, 1); component 2 is out of range.
        assert_eq!(g.apply_lookup_type_5(0, 100, 2, 200), None);
    }

    #[test]
    fn mark_to_ligature_returns_none_for_uncovered_glyphs() {
        let bytes = build_mark_ligature_pos_format1();
        let g = GposTable::parse(&bytes).unwrap();
        // Wrong ligature glyph.
        assert_eq!(g.apply_lookup_type_5(0, 101, 0, 200), None);
        // Wrong mark glyph.
        assert_eq!(g.apply_lookup_type_5(0, 100, 0, 201), None);
    }

    #[test]
    fn lookup_mark_to_ligature_walks_lookup_list() {
        let bytes = build_mark_ligature_pos_format1();
        let g = GposTable::parse(&bytes).unwrap();
        assert_eq!(g.lookup_mark_to_ligature(100, 1, 200), Some((490, 850)));
        assert_eq!(g.lookup_mark_to_ligature(99, 0, 200), None);
    }

    // ---- ExtensionPos (LookupType 9) wrapping at the lookup level --

    /// Wrap an arbitrary sub-table inside an ExtensionPosFormat1 wrapper
    /// so the resulting Lookup carries `lookupType = 9` even though its
    /// effective sub-table is `inner_type`.
    ///
    /// ExtensionPosFormat1 wire layout:
    ///   u16 format = 1
    ///   u16 extensionLookupType
    ///   Offset32 extensionOffset (relative to ExtensionPos sub-table)
    fn build_extension_wrapped_lookup(inner_type: u16, inner_sub: &[u8]) -> Vec<u8> {
        // Build the wrapper sub-table: 8-byte header + inner sub-table.
        let mut ext_sub = Vec::new();
        ext_sub.extend_from_slice(&1u16.to_be_bytes()); // format
        ext_sub.extend_from_slice(&inner_type.to_be_bytes()); // extensionLookupType
        ext_sub.extend_from_slice(&8u32.to_be_bytes()); // extensionOffset = 8
        ext_sub.extend_from_slice(inner_sub);
        // Wrap as an outer LookupType-9 lookup.
        let lookup = wrap_lookup(LOOKUP_EXTENSION_POS, &ext_sub);
        wrap_gpos_single(&lookup)
    }

    #[test]
    fn extension_wrapper_unwraps_for_cursive_pos_lookup() {
        // Build a CursivePosFormat1 sub-table by extracting it from the
        // build_cursive_pos_format1 fixture (which wraps it as a plain
        // type-3 lookup). We re-build the inner sub-table here so we
        // can wrap it with type 9 instead.
        // Easier: inline a tiny cursive sub-table covering only gid 6.
        let mut cursive_sub = Vec::new();
        // format=1, covOff=10, entryExitCount=1, then EntryExitRecord
        // (entryOff=20, exitOff=26), then anchors at 20+26.
        // header = 6 + 4 = 10.
        cursive_sub.extend_from_slice(&1u16.to_be_bytes()); // format
        cursive_sub.extend_from_slice(&22u16.to_be_bytes()); // covOff (after anchors)
        cursive_sub.extend_from_slice(&1u16.to_be_bytes()); // entryExitCount
                                                            // EntryExitRecord
        cursive_sub.extend_from_slice(&10u16.to_be_bytes()); // entryOff
        cursive_sub.extend_from_slice(&16u16.to_be_bytes()); // exitOff
                                                             // Entry anchor (1, x=70, y=110)
        cursive_sub.extend_from_slice(&1u16.to_be_bytes());
        cursive_sub.extend_from_slice(&70i16.to_be_bytes());
        cursive_sub.extend_from_slice(&110i16.to_be_bytes());
        // Exit anchor (1, x=130, y=210)
        cursive_sub.extend_from_slice(&1u16.to_be_bytes());
        cursive_sub.extend_from_slice(&130i16.to_be_bytes());
        cursive_sub.extend_from_slice(&210i16.to_be_bytes());
        // Coverage format 1 covering [6]
        cursive_sub.extend_from_slice(&1u16.to_be_bytes());
        cursive_sub.extend_from_slice(&1u16.to_be_bytes());
        cursive_sub.extend_from_slice(&6u16.to_be_bytes());

        let bytes = build_extension_wrapped_lookup(LOOKUP_CURSIVE_POS, &cursive_sub);
        let g = GposTable::parse(&bytes).unwrap();

        // The lookup is type 9 on disk; lookup_list reports the
        // *effective* type after unwrap.
        let v: Vec<_> = g.lookup_list().collect();
        assert_eq!(v, vec![(0u16, LOOKUP_CURSIVE_POS, 1u16)]);

        // apply_lookup_type_3 must transparently unwrap the LT9 wrapper.
        let r = g.apply_lookup_type_3(0, 6).unwrap();
        assert_eq!(r.entry, Some((70, 110)));
        assert_eq!(r.exit, Some((130, 210)));
    }
}