read-fonts 0.46.0

Reading OpenType font files.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
//! Type1 fonts.

use super::{
    charmap::UnicodeCharmap,
    charstring::{
        self, CharstringContext, CharstringKind, CommandSink, NopFilterSink, TransformSink,
    },
    encoding::PredefinedEncoding,
    transform::{self, FontMatrix, ScaledFontMatrix, Transform},
};
use crate::model::Error;
use crate::{
    model::glyph::outline::PathSink,
    types::{BoundingBox, Fixed, GlyphId},
};
use alloc::{borrow::Cow, string::String, vec::Vec};
use core::ops::Range;

/// A Type1 font.
pub struct Type1Font {
    name: Option<String>,
    full_name: Option<String>,
    family_name: Option<String>,
    weight: Option<String>,
    version: Option<String>,
    notice: Option<String>,
    bbox: BoundingBox<Fixed>,
    italic_angle: i32,
    is_fixed_pitch: bool,
    underline_position: i32,
    underline_thickness: i32,
    matrix: ScaledFontMatrix,
    charstrings: Charstrings,
    subrs: Subrs,
    encoding: Option<RawEncoding>,
    /// The default blend of a multiple master font, empty for any other.
    weight_vector: Vec<Fixed>,
    multiple_master: Option<MultipleMaster>,
    /// The number of elements in the `BuildCharArray` that othersubrs
    /// read and write.
    build_char_len: usize,
    unicode_charmap: UnicodeCharmap,
}

impl Type1Font {
    /// Creates a new Type1 font from the given data.
    pub fn new(data: &[u8]) -> Result<Self, Error> {
        // any failure to parse is simply malformed data
        Self::new_impl(data).ok_or(Error::Malformed)
    }

    fn new_impl(data: &[u8]) -> Option<Self> {
        let raw_dicts = RawDicts::new(data)?;
        Self::from_dicts(raw_dicts.base, &raw_dicts.private)
    }

    fn empty() -> Self {
        Self {
            name: None,
            full_name: None,
            family_name: None,
            weight: None,
            version: None,
            notice: None,
            italic_angle: 0,
            is_fixed_pitch: false,
            underline_position: 0,
            underline_thickness: 0,
            matrix: ScaledFontMatrix {
                matrix: FontMatrix::IDENTITY,
                scale: 1000,
            },
            bbox: BoundingBox::default(),
            charstrings: Charstrings::default(),
            subrs: Subrs::default(),
            encoding: None,
            weight_vector: Vec::new(),
            multiple_master: None,
            build_char_len: 0,
            unicode_charmap: UnicodeCharmap::default(),
        }
    }

    fn from_dicts(base: &[u8], private: &[u8]) -> Option<Self> {
        let mut font = Self::empty();
        // Read base dict entries
        let mut encoding_offset = None;
        let mut blend = RawBlend::default();
        let mut parser = Parser::new(base);
        while let Some(token) = parser.next() {
            match token {
                Token::Name(b"FontName") => font.name = parser.read_string(),
                Token::Name(b"FullName") => font.full_name = parser.read_string(),
                Token::Name(b"FamilyName") => font.family_name = parser.read_string(),
                Token::Name(b"Weight") => {
                    // The /Weight token can appear elsewhere in MM fonts so take
                    // the first one
                    if font.weight.is_none() {
                        font.weight = parser.read_string();
                    }
                }
                Token::Name(b"version") => font.version = parser.read_string(),
                Token::Name(b"Notice") => font.notice = parser.read_string(),
                // In the /Blend dictionary of a multiple master font these
                // hold an array with a value for each master. FreeType keeps
                // the font's own value then, so only a number replaces it.
                Token::Name(b"ItalicAngle") => {
                    if let Some(angle) = parser.read_num_as_int() {
                        font.italic_angle = angle;
                    }
                }
                Token::Name(b"isFixedPitch") => match parser.next() {
                    Some(Token::Raw(b"[") | Token::Proc(_)) => {}
                    token => font.is_fixed_pitch = token == Some(Token::Raw(b"true")),
                },
                Token::Name(b"UnderlinePosition") => {
                    if let Some(position) = parser.read_num_as_int() {
                        font.underline_position = position;
                    }
                }
                Token::Name(b"UnderlineThickness") => {
                    if let Some(thickness) = parser.read_num_as_int() {
                        font.underline_thickness = thickness;
                    }
                }
                Token::Name(b"FontBBox") => {
                    if let Some([x_min, y_min, x_max, y_max]) = parser.read_font_bbox() {
                        font.bbox = BoundingBox {
                            x_min,
                            y_min,
                            x_max,
                            y_max,
                        };
                    }
                }
                Token::Name(b"FontMatrix") => font.matrix = parser.read_font_matrix()?,
                // Simply save the encoding offset. We'll parse it after
                // we have read charstrings so we have an accurate mapping
                // if we've synthesized or remapped a notdef glyph
                Token::Name(b"Encoding") => encoding_offset = Some(parser.pos),
                Token::Name(b"WeightVector") => {
                    // Gated on a successful read because some fonts reference
                    // the /WeightVector name outside of a proc, leading to
                    // spurious field reads
                    if let Some(weights) = parser.read_weight_vector() {
                        blend.add_weights(weights)?;
                    }
                }
                // The blend dictionaries are each read only where they hold
                // an array, as FreeType reads them.
                Token::Name(b"BlendAxisTypes") => {
                    if let Some(names) = parser.read_blend_axis_types() {
                        blend.add_axis_types(names)?;
                    }
                }
                Token::Name(b"BlendDesignPositions") => {
                    if let Some(positions) = parser.read_blend_design_positions() {
                        blend.add_design_positions(positions)?;
                    }
                }
                Token::Name(b"BlendDesignMap") => {
                    if let Some(maps) = parser.read_blend_design_map() {
                        blend.add_design_maps(maps)?;
                    }
                }
                Token::Name(b"BuildCharArray") => {
                    if let Some(len) = parser.read_array_len() {
                        blend.build_char_len = len;
                    }
                }
                _ => {}
            }
        }
        if let Some((multiple_master, weights)) = blend.finish() {
            font.multiple_master = Some(multiple_master);
            font.weight_vector = weights;
            font.build_char_len = blend.build_char_len;
        }
        // Read private dict entries
        let mut parser = Parser::new(private);
        // Default value if not present
        let mut len_iv = 4;
        while let Some(token) = parser.next() {
            match token {
                Token::Name(b"lenIV") => len_iv = parser.read_int()?,
                Token::Name(b"Subrs") => {
                    // With synthetic fonts, it's possible to read subroutines
                    // twice. FreeType ignores the second copy.
                    // <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L1855>
                    if font.subrs.index.is_empty() {
                        font.subrs = parser.read_subrs(len_iv)?;
                    }
                }
                Token::Name(b"CharStrings") => {
                    // Some non-standard fonts provide multiple copies of
                    // outlines for different resolutions and FreeType only
                    // retains the first copy, so skip parsing if we've
                    // already read some charstrings.
                    // <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L2058>
                    if font.charstrings.index.is_empty() {
                        font.charstrings = parser.read_charstrings(len_iv)?;
                    }
                }
                _ => {}
            }
        }
        // Reject fonts that are missing a /CharStrings array
        // <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L2665>
        if font.charstrings.index.is_empty() {
            return None;
        }
        if let Some(encoding_offset) = encoding_offset {
            let mut parser = Parser::new(base.get(encoding_offset..)?);
            font.encoding = Some(parser.read_encoding(&font.charstrings)?);
        }
        // We can only generate a Unicode cmap if we have the AGL available
        #[cfg(feature = "agl")]
        {
            font.unicode_charmap = UnicodeCharmap::from_glyph_names(font.glyph_names());
        }
        Some(font)
    }

    /// Returns the PostScript name.
    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    /// Returns the full font name.
    pub fn full_name(&self) -> Option<&str> {
        self.full_name.as_deref()
    }

    /// Returns the font family name.
    pub fn family_name(&self) -> Option<&str> {
        self.family_name.as_deref()
    }

    /// Returns the weight or style name.
    pub fn weight(&self) -> Option<&str> {
        self.weight.as_deref()
    }

    /// Returns the font version string.
    pub fn version(&self) -> Option<&str> {
        self.version.as_deref()
    }

    /// Returns the font's copyright or trademark notice.
    pub fn notice(&self) -> Option<&str> {
        self.notice.as_deref()
    }

    /// Returns the italic angle.
    pub fn italic_angle(&self) -> i32 {
        self.italic_angle
    }

    /// Returns true if the glyphs in this font have the same width.
    pub fn is_fixed_pitch(&self) -> bool {
        self.is_fixed_pitch
    }

    /// Returns the position of the center of an underline decoration.
    pub fn underline_position(&self) -> i32 {
        self.underline_position
    }

    /// Returns the suggested size for an underline decoration.
    pub fn underline_thickness(&self) -> i32 {
        self.underline_thickness
    }

    /// Returns the font bounding box.
    pub fn bbox(&self) -> BoundingBox<Fixed> {
        self.bbox
    }

    /// Returns the number of glyphs in the Type1 font.
    pub fn num_glyphs(&self) -> u32 {
        self.charstrings.num_glyphs()
    }

    /// Returns the units per em.
    pub fn upem(&self) -> i32 {
        self.matrix.scale
    }

    /// Returns the top level font matrix.
    pub fn matrix(&self) -> FontMatrix {
        self.matrix.matrix
    }

    /// Returns the appropriate transform for adjusting points and metrics.
    pub fn transform(&self, ppem: Option<f32>) -> Transform {
        let scale = ppem.map(|ppem| Transform::compute_scale(ppem, self.upem()));
        Transform {
            matrix: self.matrix(),
            scale,
        }
    }

    /// Returns the character encoding.
    pub fn encoding(&self) -> Option<Encoding<'_>> {
        self.encoding.as_ref().map(|enc| Encoding {
            encoding: enc,
            charstrings: &self.charstrings,
        })
    }

    /// Returns the Unicode charmap for this font.
    ///
    /// Note that this is an empty mapping if the `agl` feature is not enabled.
    pub fn unicode_charmap(&self) -> &UnicodeCharmap {
        &self.unicode_charmap
    }

    /// Returns the glyph name for the given id.
    pub fn glyph_name(&self, gid: GlyphId) -> Option<&str> {
        self.charstrings.name(gid.to_u32())
    }

    /// Returns an iterator over the pairs of glyph ids and associated names in
    /// the Type1 font.
    pub fn glyph_names(&self) -> impl Iterator<Item = (GlyphId, &str)> {
        (0..self.num_glyphs())
            .filter_map(|idx| Some((GlyphId::new(idx), self.charstrings.name(idx)?)))
    }

    /// Given a glyph identifier in the original glyph order, returns the
    /// possibly remapped identifier.
    ///
    /// This occurs if we remap or synthesize a `.notdef` glyph.
    pub fn remapped_gid(&self, original_gid: GlyphId) -> GlyphId {
        if let Some(orig_notdef) = self.charstrings.orig_notdef_index {
            if original_gid == GlyphId::NOTDEF {
                GlyphId::new(orig_notdef as u32)
            } else if orig_notdef == original_gid.to_u32() as usize {
                GlyphId::NOTDEF
            } else {
                original_gid
            }
        } else {
            original_gid
        }
    }

    /// Returns the design space of a multiple master font.
    ///
    /// `None` for a font with no masters, or with blend dictionaries that
    /// do not agree.
    pub fn multiple_master(&self) -> Option<&MultipleMaster> {
        self.multiple_master.as_ref()
    }

    /// Returns the font drawn with the blend that `weights` give, one per
    /// master.
    ///
    /// [`MultipleMaster::blend_to_weights`] computes the weights for a
    /// position in the blend space.
    pub fn instance<'a>(&'a self, weights: &'a [Fixed]) -> Type1Instance<'a> {
        Type1Instance {
            font: self,
            weights,
        }
    }

    /// Returns the font drawn with its own default blend.
    pub fn default_instance(&self) -> Type1Instance<'_> {
        self.instance(&self.weight_vector)
    }

    /// Evaluates the charstring for the requested glyph far enough to read
    /// its advance width, without drawing it.
    ///
    /// A Type 1 charstring states its width in the `hsbw` or `sbw` that must
    /// open it, so evaluation stops there.
    ///
    /// The font matrix is applied and no size is, so the width is exact and
    /// in the units the rest of the font is measured in.
    /// [`advance`](Self::advance) applies a size as well and reports what
    /// [`draw`](Self::draw) does.
    ///
    /// Unlike [`evaluate_charstring`], the matrix is applied here rather than
    /// left to a caller. That one withholds it so a hinter can be slotted in
    /// ahead of it; nothing hints a width.
    ///
    /// [`evaluate_charstring`]: Self::evaluate_charstring
    pub fn evaluate_width(&self, gid: GlyphId) -> Result<Option<Fixed>, Error> {
        self.default_instance().evaluate_width(gid)
    }

    /// Evaluates the charstring for the requested glyph and sends the results
    /// to the given sink.
    ///
    /// Returns the width the charstring states, in the charstring's own
    /// space: neither the font matrix nor a size is applied, to the width or
    /// to the commands. [`draw`](Self::draw) applies both.
    pub fn evaluate_charstring(
        &self,
        gid: GlyphId,
        sink: &mut impl CommandSink,
    ) -> Result<Option<Fixed>, Error> {
        self.default_instance().evaluate_charstring(gid, sink)
    }

    /// Returns the advance width of the glyph with an optional size in
    /// ppem, without drawing it.
    ///
    /// This is the width [`draw`](Self::draw) reports, arrived at without
    /// the drawing.
    pub fn advance(&self, gid: GlyphId, ppem: Option<f32>) -> Result<Option<f32>, Error> {
        self.default_instance().advance(gid, ppem)
    }

    /// Measures the box the glyph's drawing stays within, in design units.
    ///
    /// A control box: a curve counts through its control points rather than
    /// the path they describe, so one bulging inside its hull measures larger
    /// than it draws. HarfBuzz reports a charstring's extents the same way.
    ///
    /// The font matrix is applied and no size is, so the box is exact and in
    /// the units the rest of the font is measured in.
    /// [`extents`](Self::extents) applies a size as well and answers in what
    /// a caller laying out text works in.
    ///
    /// Unlike [`evaluate_charstring`], the matrix is applied here rather than
    /// left to a caller. That one withholds it so a hinter can be slotted in
    /// before it; nothing hints a box, and a box cannot be brought through a
    /// matrix afterwards anyway, because turning a box turns its corners and
    /// a matrix that shears tilts the drawing inside a box that has to grow
    /// to hold them.
    ///
    /// `None` where the glyph draws nothing.
    ///
    /// [`evaluate_charstring`]: Self::evaluate_charstring
    pub fn evaluate_extents(&self, gid: GlyphId) -> Result<Option<BoundingBox<Fixed>>, Error> {
        self.default_instance().evaluate_extents(gid)
    }

    /// Returns the box the glyph's drawing stays within, with an optional
    /// size in ppem.
    ///
    /// The box [`draw`](Self::draw) would stay within, measured as
    /// [`evaluate_extents`](Self::evaluate_extents) measures it and scaled by
    /// the size as well.
    ///
    /// A size can be applied to a finished box, being one scale in both
    /// directions; it is applied here so the answer matches `draw` at that
    /// size without a caller doing the arithmetic. The matrix cannot, which
    /// is why neither of these leaves it out.
    ///
    /// `None` where the glyph draws nothing.
    pub fn extents(
        &self,
        gid: GlyphId,
        ppem: Option<f32>,
    ) -> Result<Option<BoundingBox<f32>>, Error> {
        self.default_instance().extents(gid, ppem)
    }

    /// Draws the glyph with an optional size in ppem to the given pen.
    ///
    /// Returns the advance width of the glyph if the charstring provides
    /// one.
    pub fn draw(
        &self,
        gid: GlyphId,
        ppem: Option<f32>,
        sink: &mut impl PathSink,
    ) -> Result<Option<f32>, Error> {
        self.default_instance().draw(gid, ppem, sink)
    }
}

impl<'a> Type1Instance<'a> {
    /// Returns the font.
    pub fn font(&self) -> &'a Type1Font {
        self.font
    }

    /// Returns the weight of each master.
    pub fn weights(&self) -> &'a [Fixed] {
        self.weights
    }

    /// As [`Type1Font::evaluate_width`], with this instance's blend.
    pub fn evaluate_width(&self, gid: GlyphId) -> Result<Option<Fixed>, Error> {
        let charstring_data = self
            .font
            .charstrings
            .get(gid.to_u32())
            .ok_or(Error::Malformed)?;
        let transform = self.font.transform(None);
        Ok(charstring::evaluate_width(self, None, charstring_data)?
            .map(|width| transform.transform_h_metric(width)))
    }

    /// As [`Type1Font::evaluate_charstring`], with this instance's blend.
    pub fn evaluate_charstring(
        &self,
        gid: GlyphId,
        sink: &mut impl CommandSink,
    ) -> Result<Option<Fixed>, Error> {
        let charstring_data = self
            .font
            .charstrings
            .get(gid.to_u32())
            .ok_or(Error::Malformed)?;
        charstring::evaluate(self, None, charstring_data, sink)
    }

    /// As [`Type1Font::advance`], with this instance's blend.
    pub fn advance(&self, gid: GlyphId, ppem: Option<f32>) -> Result<Option<f32>, Error> {
        let charstring_data = self
            .font
            .charstrings
            .get(gid.to_u32())
            .ok_or(Error::Malformed)?;
        let transform = self.font.transform(ppem);
        Ok(charstring::evaluate_width(self, None, charstring_data)?
            .map(|width| transform.transform_h_metric(width).to_f32().max(0.0)))
    }

    /// As [`Type1Font::evaluate_extents`], with this instance's blend.
    pub fn evaluate_extents(&self, gid: GlyphId) -> Result<Option<BoundingBox<Fixed>>, Error> {
        let mut bounds = charstring::ControlBoundsSink::new();
        let mut nop_filter = charstring::NopFilterSink::new(&mut bounds);
        let transform = self.font.transform(None);
        let mut transformer = charstring::TransformSink::new(&mut nop_filter, transform);
        self.evaluate_charstring(gid, &mut transformer)?;
        Ok(bounds.bounding_box())
    }

    /// As [`Type1Font::extents`], with this instance's blend.
    pub fn extents(
        &self,
        gid: GlyphId,
        ppem: Option<f32>,
    ) -> Result<Option<BoundingBox<f32>>, Error> {
        let mut bounds = charstring::ControlBoundsSink::new();
        let mut nop_filter = charstring::NopFilterSink::new(&mut bounds);
        let transform = self.font.transform(ppem);
        let mut transformer = charstring::TransformSink::new(&mut nop_filter, transform);
        self.evaluate_charstring(gid, &mut transformer)?;
        Ok(bounds.bounding_box().map(|b| BoundingBox {
            x_min: b.x_min.to_f32(),
            y_min: b.y_min.to_f32(),
            x_max: b.x_max.to_f32(),
            y_max: b.y_max.to_f32(),
        }))
    }

    /// As [`Type1Font::draw`], with this instance's blend.
    pub fn draw(
        &self,
        gid: GlyphId,
        ppem: Option<f32>,
        sink: &mut impl PathSink,
    ) -> Result<Option<f32>, Error> {
        let mut nop_filter = NopFilterSink::new(sink);
        let transform = self.font.transform(ppem);
        let mut transformer = TransformSink::new(&mut nop_filter, transform);
        let width = self.evaluate_charstring(gid, &mut transformer)?;
        Ok(width.map(|w| transform.transform_h_metric(w).to_f32().max(0.0)))
    }
}

impl CharstringContext for Type1Font {
    fn kind(&self) -> CharstringKind {
        CharstringKind::Type1
    }

    fn seac_components(&self, base_code: i32, accent_code: i32) -> Result<[&[u8]; 2], Error> {
        let decode = |code: i32| {
            let name = PredefinedEncoding::Standard
                .name(code.try_into().map_err(|_| Error::InvalidOperation)?);
            self.charstrings
                .index_for_name(name)
                .and_then(|idx| self.charstrings.get(idx))
                .ok_or(Error::InvalidOperation)
        };
        let base = decode(base_code)?;
        let accent = decode(accent_code)?;
        Ok([base, accent])
    }

    fn subr(&self, index: i32) -> Result<&[u8], Error> {
        self.subrs.get(index as u32).ok_or(Error::Malformed)
    }

    fn global_subr(&self, _index: i32) -> Result<&[u8], Error> {
        // Type1 fonts don't have global subroutines
        Err(Error::MissingResource)
    }

    fn weight_vector(&self) -> &[Fixed] {
        &self.weight_vector
    }

    fn build_char_len(&self) -> usize {
        self.build_char_len
    }
}

impl CharstringContext for Type1Instance<'_> {
    fn kind(&self) -> CharstringKind {
        CharstringKind::Type1
    }

    fn seac_components(&self, base_code: i32, accent_code: i32) -> Result<[&[u8]; 2], Error> {
        self.font.seac_components(base_code, accent_code)
    }

    fn subr(&self, index: i32) -> Result<&[u8], Error> {
        self.font.subr(index)
    }

    fn global_subr(&self, index: i32) -> Result<&[u8], Error> {
        self.font.global_subr(index)
    }

    fn weight_vector(&self) -> &[Fixed] {
        self.weights
    }

    fn build_char_len(&self) -> usize {
        self.font.build_char_len
    }
}

/// The most axes a multiple master font can have.
const MAX_MASTER_AXES: usize = 4;

/// The most masters a multiple master font can have.
const MAX_MASTERS: usize = 16;

/// The most points in the design map of one axis.
const MAX_MAP_POINTS: usize = 20;

/// The blend dictionaries of a font as they are read, before they are
/// checked against each other.
///
/// Each `add` fails where FreeType refuses the whole font: a count out of
/// range, a count that disagrees with one already read, or a second design
/// map. A later axis type or position list replaces an earlier one, as in
/// FreeType.
///
/// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L764>
#[derive(Default)]
struct RawBlend {
    num_axes: usize,
    num_masters: usize,
    axis_names: Vec<String>,
    positions: bool,
    maps: Vec<Vec<(i32, Fixed)>>,
    weights: Vec<Fixed>,
    build_char_len: usize,
}

impl RawBlend {
    fn set_num_axes(&mut self, num_axes: usize) -> Option<()> {
        if num_axes == 0 || num_axes > MAX_MASTER_AXES {
            return None;
        }
        if self.num_axes != 0 && self.num_axes != num_axes {
            return None;
        }
        self.num_axes = num_axes;
        Some(())
    }

    fn set_num_masters(&mut self, num_masters: usize) -> Option<()> {
        if num_masters == 0 || num_masters > MAX_MASTERS {
            return None;
        }
        if self.num_masters != 0 && self.num_masters != num_masters {
            return None;
        }
        self.num_masters = num_masters;
        Some(())
    }

    fn add_axis_types(&mut self, names: Vec<String>) -> Option<()> {
        self.set_num_axes(names.len())?;
        self.axis_names = names;
        Some(())
    }

    fn add_design_positions(&mut self, positions: Vec<Vec<Fixed>>) -> Option<()> {
        self.set_num_masters(positions.len())?;
        let num_axes = positions.first()?.len();
        if positions.iter().any(|position| position.len() != num_axes) {
            return None;
        }
        self.set_num_axes(num_axes)?;
        // Only their presence matters: a font's masters sit at the corners
        // of the blend space, which the weights are computed for.
        self.positions = true;
        Some(())
    }

    fn add_design_maps(&mut self, maps: Vec<Vec<(i32, Fixed)>>) -> Option<()> {
        if !self.maps.is_empty() {
            return None;
        }
        self.set_num_axes(maps.len())?;
        if maps
            .iter()
            .any(|map| map.is_empty() || map.len() > MAX_MAP_POINTS)
        {
            return None;
        }
        self.maps = maps;
        Some(())
    }

    fn add_weights(&mut self, weights: Vec<Fixed>) -> Option<()> {
        self.set_num_masters(weights.len())?;
        self.weights = weights;
        Some(())
    }

    /// Returns the design space and the default weights, or `None` where
    /// FreeType sets the blend aside and treats the font as an ordinary one.
    ///
    /// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L2567>
    fn finish(&self) -> Option<(MultipleMaster, Vec<Fixed>)> {
        // Only masters at the corners of the blend space are supported.
        if self.num_axes == 0 || self.num_masters != 1 << self.num_axes {
            return None;
        }
        if self.weights.is_empty() || !self.positions || self.maps.len() != self.num_axes {
            return None;
        }
        let axes = self
            .maps
            .iter()
            .enumerate()
            .map(|(index, map)| MasterAxis {
                name: self.axis_names.get(index).cloned(),
                design_points: map.iter().map(|point| point.0).collect(),
                blend_points: map.iter().map(|point| point.1).collect(),
            })
            .collect();
        let multiple_master = MultipleMaster {
            axes,
            num_masters: self.num_masters,
            default_weights: self.weights.clone(),
        };
        Some((multiple_master, self.weights.clone()))
    }
}

/// The design space of a multiple master font.
///
/// A multiple master font holds two to sixteen masters at the corners of a
/// blend space with one to four axes. An instance is a weighted blend of
/// the masters, and its weights come from a position in that space, given
/// as blend coordinates from 0 to 1 on each axis or as design coordinates,
/// which each axis maps to blend coordinates.
///
/// The conversions follow FreeType's, including where its arithmetic
/// departs from the specification, so that an instance draws as it does
/// in FreeType.
#[derive(Clone, Debug)]
pub struct MultipleMaster {
    axes: Vec<MasterAxis>,
    num_masters: usize,
    default_weights: Vec<Fixed>,
}

/// One axis of a multiple master font.
#[derive(Clone, Debug)]
pub struct MasterAxis {
    name: Option<String>,
    design_points: Vec<i32>,
    blend_points: Vec<Fixed>,
}

impl MasterAxis {
    /// Returns the name the font gives the axis, such as `Weight`.
    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    /// Returns the lowest design coordinate.
    pub fn min_design(&self) -> i32 {
        self.design_points.first().copied().unwrap_or_default()
    }

    /// Returns the highest design coordinate.
    pub fn max_design(&self) -> i32 {
        self.design_points.last().copied().unwrap_or_default()
    }

    /// Returns the map from design coordinates to blend coordinates, as
    /// pairs in the font's order.
    pub fn design_map(&self) -> impl Iterator<Item = (i32, Fixed)> + '_ {
        self.design_points
            .iter()
            .copied()
            .zip(self.blend_points.iter().copied())
    }

    /// Returns the blend coordinate for a design coordinate.
    ///
    /// This follows FreeType's `T1_Set_MM_Design`, which interpolates
    /// between two points as though the first mapped to a blend of zero.
    /// The result is exact for a map whose interpolated points start at
    /// zero, as two point maps do.
    ///
    /// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L555>
    pub fn design_to_blend(&self, design: i32) -> Fixed {
        let designs = &self.design_points;
        let blends = &self.blend_points;
        let mut before = None;
        let mut after = None;
        for (p, &p_design) in designs.iter().enumerate() {
            if design == p_design {
                return blends[p];
            }
            if design < p_design {
                after = Some(p);
                break;
            }
            before = Some(p);
        }
        match (before, after) {
            (None, _) => blends.first().copied().unwrap_or_default(),
            (Some(_), None) => blends.last().copied().unwrap_or_default(),
            (Some(before), Some(after)) => {
                // FreeType leaves out `+ blends[before]`.
                Fixed::from_bits(saturate(mul_div_wide(
                    i64::from(design) - i64::from(designs[before]),
                    i64::from(blends[after].to_bits()) - i64::from(blends[before].to_bits()),
                    i64::from(designs[after]) - i64::from(designs[before]),
                )))
            }
        }
    }

    /// Returns the design coordinate for a blend coordinate, a whole number.
    ///
    /// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L218>
    pub fn blend_to_design(&self, blend: Fixed) -> i32 {
        let designs = &self.design_points;
        let blends = &self.blend_points;
        if designs.is_empty() || blend <= blends[0] {
            return self.min_design();
        }
        for j in 1..designs.len() {
            if blend <= blends[j] {
                let offset = mul_div_wide(
                    i64::from(blend.to_bits()) - i64::from(blends[j - 1].to_bits()),
                    i64::from(designs[j]) - i64::from(designs[j - 1]),
                    i64::from(blends[j].to_bits()) - i64::from(blends[j - 1].to_bits()),
                );
                return saturate(i64::from(designs[j - 1]) + offset);
            }
        }
        self.max_design()
    }
}

/// Returns `a * b / c`, rounded as [`Fixed::mul_div`] rounds, without
/// overflowing on the way.
///
/// Design maps hold any `i32`, so the differences between their points
/// don't fit in one.
fn mul_div_wide(a: i64, b: i64, c: i64) -> i64 {
    let (a_abs, b_abs, c_abs) = (
        u128::from(a.unsigned_abs()),
        u128::from(b.unsigned_abs()),
        u128::from(c.unsigned_abs()),
    );
    let result = if c_abs > 0 {
        (a_abs * b_abs + (c_abs >> 1)) / c_abs
    } else {
        0x7FFFFFFF
    };
    let result = result.min(i64::MAX as u128) as i64;
    if (a < 0) ^ (b < 0) ^ (c < 0) {
        -result
    } else {
        result
    }
}

/// Clamps `value` to the range of an `i32`.
fn saturate(value: i64) -> i32 {
    value.clamp(i32::MIN.into(), i32::MAX.into()) as i32
}

impl MultipleMaster {
    /// Returns the axes.
    pub fn axes(&self) -> &[MasterAxis] {
        &self.axes
    }

    /// Returns the number of masters, which is two to the power of the
    /// number of axes.
    pub fn num_masters(&self) -> usize {
        self.num_masters
    }

    /// Returns the weights of the font's default instance, one per master.
    pub fn default_weights(&self) -> &[Fixed] {
        &self.default_weights
    }

    /// Writes the blend coordinates of the font's default instance to
    /// `blend`, one per axis.
    ///
    /// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L248>
    pub fn default_blend(&self, blend: &mut [Fixed]) {
        self.weights_to_blend(&self.default_weights, blend);
    }

    /// Writes the blend coordinates that give rise to `weights` to `blend`,
    /// one per axis.
    ///
    /// The blend on an axis is the sum of the weights of the masters at its
    /// far end.
    pub fn weights_to_blend(&self, weights: &[Fixed], blend: &mut [Fixed]) {
        for (axis, value) in blend.iter_mut().enumerate().take(self.axes.len()) {
            *value = Fixed::from_bits(
                (0..self.num_masters)
                    .filter(|master| master & (1 << axis) != 0)
                    .filter_map(|master| weights.get(master))
                    .fold(0i32, |sum, weight| sum.wrapping_add(weight.to_bits())),
            );
        }
    }

    /// Writes the weights for the blend coordinates `blend` to `weights`, one
    /// per master.
    ///
    /// An axis without a coordinate takes the middle of its range.
    ///
    /// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L383>
    pub fn blend_to_weights(&self, blend: &[Fixed], weights: &mut [Fixed]) {
        for (master, weight) in weights.iter_mut().enumerate().take(self.num_masters) {
            let mut result = Fixed::ONE;
            for axis in 0..self.axes.len() {
                let Some(&coord) = blend.get(axis) else {
                    result = Fixed::from_bits(result.to_bits() >> 1);
                    continue;
                };
                let factor = if master & (1 << axis) == 0 {
                    Fixed::ONE - coord
                } else {
                    coord
                };
                if factor <= Fixed::ZERO {
                    result = Fixed::ZERO;
                    break;
                } else if factor >= Fixed::ONE {
                    continue;
                }
                result *= factor;
            }
            *weight = result;
        }
    }

    /// Writes the blend coordinates for the design coordinates `design` to
    /// `blend`, one per axis.
    ///
    /// Each design coordinate is rounded to a whole number first. An axis
    /// without a coordinate takes half its range above zero, `(max - min) /
    /// 2`, which is the middle only of a range that starts at zero. Both
    /// follow FreeType's `T1_Set_Var_Design`, as does
    /// [`MasterAxis::design_to_blend`].
    ///
    /// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L653>
    pub fn design_to_blend(&self, design: &[Fixed], blend: &mut [Fixed]) {
        for (index, (axis, value)) in self.axes.iter().zip(blend.iter_mut()).enumerate() {
            let design = match design.get(index) {
                // FreeType's `FIXED_TO_INT`, which rounds a half away from
                // zero.
                Some(design) => {
                    let bits = design.to_bits();
                    bits.wrapping_add(0x8000 - i32::from(bits < 0)) >> 16
                }
                None => saturate((i64::from(axis.max_design()) - i64::from(axis.min_design())) / 2),
            };
            *value = axis.design_to_blend(design);
        }
    }
}

/// A Type 1 font drawn with a given blend of its masters.
///
/// For a multiple master font, the weights give the blend, one per master.
/// [`MultipleMaster::blend_to_weights`] computes them for a position in the
/// blend space. A font with no masters takes empty weights.
#[derive(Clone, Copy)]
pub struct Type1Instance<'a> {
    font: &'a Type1Font,
    weights: &'a [Fixed],
}

/// Associates character codes with glyph names and ids.
#[derive(Clone)]
pub struct Encoding<'a> {
    encoding: &'a RawEncoding,
    charstrings: &'a Charstrings,
}

impl<'a> Encoding<'a> {
    /// Returns the predefined encoding, if any.
    pub fn predefined(&self) -> Option<PredefinedEncoding> {
        if let RawEncoding::Predefined(pre) = self.encoding {
            Some(*pre)
        } else {
            None
        }
    }

    /// Returns the glyph name for the given character code.
    pub fn glyph_name(&self, code: u8) -> Option<&'a str> {
        match self.encoding {
            RawEncoding::Predefined(pre) => Some(pre.name(code)),
            RawEncoding::Custom(custom) => {
                self.charstrings.name(custom.get(code as usize)?.to_u32())
            }
        }
    }

    /// Maps a character code to a glyph identifier.
    pub fn map(&self, code: u8) -> Option<GlyphId> {
        match self.encoding {
            RawEncoding::Predefined(pre) => self
                .charstrings
                .index_for_name(pre.name(code))
                .map(GlyphId::new),
            RawEncoding::Custom(custom) => custom.get(code as usize).copied(),
        }
    }
}

/// Raw dictionary data for a Type1 font.
struct RawDicts<'a> {
    /// Data containing the base dicitionary.
    base: &'a [u8],
    /// Data containing the decrypted private dictionary.
    private: Vec<u8>,
}

impl<'a> RawDicts<'a> {
    fn new(data: &'a [u8]) -> Option<Self> {
        if let Some((PFB_TEXT_SEGMENT_TAG, base_size)) = decode_pfb_tag(data, 0) {
            // We have a PFB; skip the tag
            let data = data.get(6..)?;
            verify_header(data)?;
            let (base_dict, raw_private_dict) = data.split_at_checked(base_size as usize)?;
            // Decrypt private dict segments
            let private_dict = decrypt(
                decode_pfb_binary_segments(raw_private_dict)
                    .flat_map(|segment| segment.iter().copied()),
                EEXEC_SEED,
            )
            // First four bytes are random garbage
            .skip(4)
            .collect::<Vec<_>>();
            Some(Self {
                base: base_dict,
                private: private_dict,
            })
        } else {
            // We have a PFA
            verify_header(data)?;
            // Now find the start of the private dictionary
            let start = find_eexec_data(data)?;
            let (base_dict, raw_private_dict) = data.split_at_checked(start)?;
            let private_dict = if raw_private_dict.len() > 3
                && raw_private_dict[..4].iter().all(|b| b.is_ascii_hexdigit())
            {
                // Hex decode and then decrypt
                decrypt(decode_hex(raw_private_dict.iter().copied()), EEXEC_SEED)
                    .skip(4)
                    .collect::<Vec<_>>()
            } else {
                // Just decrypt
                decrypt(raw_private_dict.iter().copied(), EEXEC_SEED)
                    .skip(4)
                    .collect::<Vec<_>>()
            };
            Some(Self {
                base: base_dict,
                private: private_dict,
            })
        }
    }
}

fn verify_header(data: &[u8]) -> Option<()> {
    (data.starts_with(b"%!PS-AdobeFont") || data.starts_with(b"%!FontType")).then_some(())
}

const PFB_TEXT_SEGMENT_TAG: u16 = 0x8001;
const PFB_BINARY_SEGMENT_TAG: u16 = 0x8002;

/// Returns the PFB tag and segment size.
///
/// See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1parse.c#L69>
fn decode_pfb_tag(data: &[u8], start: usize) -> Option<(u16, u32)> {
    let header: [u8; 6] = data.get(start..start + 6)?.try_into().ok()?;
    let tag = ((header[0] as u16) << 8) | header[1] as u16;
    if matches!(tag, PFB_BINARY_SEGMENT_TAG | PFB_TEXT_SEGMENT_TAG) {
        let size = u32::from_le_bytes(header[2..].try_into().unwrap());
        Some((tag, size))
    } else {
        None
    }
}

/// Returns an iterator over the sequence of PFB binary segments.
fn decode_pfb_binary_segments(data: &[u8]) -> impl Iterator<Item = &[u8]> + '_ {
    let mut pos = 0usize;
    core::iter::from_fn(move || {
        let (tag, len) = decode_pfb_tag(data, pos)?;
        // FT only decodes the sequence of binary segments here
        // <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1parse.c#L286>
        if tag != PFB_BINARY_SEGMENT_TAG {
            return None;
        }
        // Skip tag and size bytes
        let start = pos + 6;
        let end = start + len as usize;
        let segment = data.get(start..end)?;
        pos = end;
        Some(segment)
    })
}

/// Helper to find the position of the data following the 'eexec' token.
///
/// Unsurprisingly, more complicated than it should be.
fn find_eexec_data(data: &[u8]) -> Option<usize> {
    // Use a parser to avoid catching "eexec" in a comment or string
    // which apparently occurs in some fonts.
    let mut parser = Parser::new(data);
    while let Some(token) = parser.next() {
        if token != Token::Raw(b"eexec") {
            continue;
        }
        let mut start = parser.pos;
        // FreeType has some unfun logic for skipping whitespace
        // after the eexec token
        // <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1parse.c#L382>
        let mut linefeed_pos = None;
        while start < data.len() {
            match data[start] {
                b' ' | b'\t' => {}
                b'\n' => linefeed_pos = Some(start),
                b'\r' => {
                    // If we've already seen \n or there is not a \n later
                    // in the data, then stop at this \r
                    if *linefeed_pos.get_or_insert_with(|| {
                        data[start..]
                            .iter()
                            .position(|b| *b == b'\n')
                            .map(|pos| pos + start)
                            .unwrap_or(0)
                    }) < start
                    {
                        break;
                    }
                }
                _ => break,
            }
            start += 1;
        }
        if start == data.len() {
            // eexec not properly terminated
            return None;
        }
        return Some(start);
    }
    None
}

/// Converts hex formatted data to associated bytes.
///
/// See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/psaux/psconv.c#L464>
fn decode_hex(mut bytes: impl Iterator<Item = u8>) -> impl Iterator<Item = u8> {
    /// Converts digits (as ASCII characters) into integer values.
    const DIGIT_TO_NUM: [i8; 128] = [
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1,
        -1, -1, -1, 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, -1, -1, -1, -1, -1,
    ];
    let mut pad = 0x1_u32;
    core::iter::from_fn(move || {
        loop {
            let Some(c) = bytes.next() else {
                break;
            };
            if is_whitespace(c) {
                continue;
            }
            if c >= 0x80 {
                break;
            }
            let c = DIGIT_TO_NUM[(c & 0x7F) as usize] as u32;
            if c >= 16 {
                break;
            }
            pad = (pad << 4) | c;
            if pad & 0x100 != 0 {
                let res = pad as u8;
                pad = 0x1;
                return Some(res);
            } else {
                continue;
            }
        }
        if pad != 0x1 {
            let res = (pad << 4) as u8;
            pad = 0x1;
            return Some(res);
        }
        None
    })
}

/// Decryption seed for eexec segment.
const EEXEC_SEED: u32 = 55665;

/// Decryption seed for charstring (and subroutine) data.
const CHARSTRING_SEED: u32 = 4330;

/// Returns an iterator yielding the decrypted bytes.
///
/// See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/psaux/psconv.c#L557>
fn decrypt(bytes: impl Iterator<Item = u8>, mut seed: u32) -> impl Iterator<Item = u8> {
    bytes.map(move |b| {
        let b = b as u32;
        let plain = b ^ (seed >> 8);
        seed = b.wrapping_add(seed).wrapping_mul(52845).wrapping_add(22719) & 0xFFFF;
        plain as u8
    })
}

fn is_whitespace(c: u8) -> bool {
    if c <= 32 {
        return matches!(c, b' ' | b'\n' | b'\r' | b'\t' | b'\0' | 0x0C);
    }
    false
}

/// Characters that always delimit tokens.
///
/// See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/include/freetype/internal/psaux.h#L1398>
fn is_special(c: u8) -> bool {
    matches!(
        c,
        b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'{' | b'}' | b'/' | b'%'
    )
}

fn is_special_or_whitespace(c: u8) -> bool {
    is_special(c) || is_whitespace(c)
}

/// Preserve valid UTF-8 names; otherwise decode each source byte as Latin-1.
fn decode_name(bytes: &[u8]) -> Cow<'_, str> {
    match core::str::from_utf8(bytes) {
        Ok(value) => Cow::Borrowed(value),
        Err(_) => Cow::Owned(bytes.iter().copied().map(char::from).collect()),
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
enum Token<'a> {
    /// Integers
    Int(i64),
    /// Literal strings, delimited by ()
    LitString(&'a [u8]),
    /// Hex strings, delimited by <>
    HexString(&'a [u8]),
    /// Procedures, delimited by {}
    Proc(&'a [u8]),
    /// Binary blobs
    Binary(&'a [u8]),
    /// Names, preceded by /
    Name(&'a [u8]),
    /// All other raw tokens (identifiers and self-delimiting punctuation)
    Raw(&'a [u8]),
}

/// Collection of subroutines.
#[derive(Default)]
struct Subrs {
    /// Packed data for all subroutines.
    data: Vec<u8>,
    /// Index mapping subroutine number to range in the packed data. Sorted
    /// by subroutine number.
    index: Vec<(u32, Range<usize>)>,
    /// If true, subroutine number == index so we don't need to
    /// bsearch.
    is_dense: bool,
}

impl Subrs {
    fn get(&self, index: u32) -> Option<&[u8]> {
        let entry_idx = if self.is_dense {
            index as usize
        } else {
            self.index.binary_search_by_key(&index, |e| e.0).ok()?
        };
        self.data.get(self.index.get(entry_idx)?.1.clone())
    }
}

struct CharstringEntry {
    name: Range<usize>,
    data: Range<usize>,
}

/// Collection of charstrings.
#[derive(Default)]
struct Charstrings {
    /// Packed data for all charstrings.
    data: Vec<u8>,
    /// Packed data for all glyph names.
    names: Vec<u8>,
    /// Index containing all charstrings.
    index: Vec<CharstringEntry>,
    /// If notdef was remapped, holds the original index of the notdef
    /// charstring.
    orig_notdef_index: Option<usize>,
}

impl Charstrings {
    fn num_glyphs(&self) -> u32 {
        self.index.len() as u32
    }

    fn get(&self, index: u32) -> Option<&[u8]> {
        self.data.get(self.index.get(index as usize)?.data.clone())
    }

    fn name(&self, index: u32) -> Option<&str> {
        core::str::from_utf8(
            self.names
                .get(self.index.get(index as usize)?.name.clone())?,
        )
        .ok()
    }

    fn index_for_name(&self, name: &str) -> Option<u32> {
        let name = name.as_bytes();
        for (idx, entry) in self.index.iter().enumerate() {
            if self.names.get(entry.name.clone()) == Some(name) {
                return Some(idx as u32);
            }
        }
        None
    }

    fn push(&mut self, name: &[u8], data: &[u8], len_iv: i64) {
        let start = self.data.len();
        if len_iv >= 0 {
            // use decryption; skip first len_iv bytes
            self.data
                .extend(decrypt(data.iter().copied(), CHARSTRING_SEED).skip(len_iv as usize));
        } else {
            // just add the data
            self.data.extend_from_slice(data);
        }
        let end = self.data.len();
        let name_start = self.names.len();
        self.names.extend_from_slice(decode_name(name).as_bytes());
        let name_end = self.names.len();
        self.index.push(CharstringEntry {
            name: name_start..name_end,
            data: start..end,
        });
    }
}

/// Encoding that maps characters to glyph identifiers.
#[derive(PartialEq, Debug)]
enum RawEncoding {
    Predefined(PredefinedEncoding),
    Custom(Vec<GlyphId>),
}

/// Simulated .notdef glyph, same as FreeType:
///
/// 0 333 hsbw endchar
///
/// See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L2192>
const NOTDEF_GLYPH: &[u8] = &[0x8B, 0xF7, 0xE1, 0x0D, 0x0E];

#[derive(Clone)]
struct Parser<'a> {
    data: &'a [u8],
    pos: usize,
}

impl<'a> Parser<'a> {
    fn new(data: &'a [u8]) -> Self {
        Self { data, pos: 0 }
    }

    fn next(&mut self) -> Option<Token<'a>> {
        // Roughly follows the logic of ps_parser_skip_PS_token
        // <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/psaux/psobjs.c#L482>
        loop {
            self.skip_whitespace()?;
            let start = self.pos;
            let c = self.next_byte()?;
            match c {
                // Line comment
                b'%' => self.skip_line(),
                // Procedures
                b'{' => return self.read_proc(start),
                // Literal strings
                b'(' => return self.read_lit_string(start),
                b'<' => {
                    if self.peek_byte() == Some(b'<') {
                        // Just ignore these
                        self.pos += 1;
                        continue;
                    }
                    // Hex string: hex digits and whitespace
                    return self.read_hex_string(start);
                }
                b'>' => {
                    // We consume single '>' when parsing hex strings so a
                    // double >> is expected here
                    if self.next_byte()? != b'>' {
                        return None;
                    }
                }
                // Name
                b'/' => {
                    if let Some(c) = self.peek_byte() {
                        if is_whitespace(c) || is_special(c) {
                            if !is_special(c) {
                                self.pos += 1;
                            }
                            return Some(Token::Name(&[]));
                        } else {
                            let count = self.skip_until(|c| is_whitespace(c) || is_special(c));
                            return self.data.get(start + 1..start + count).map(Token::Name);
                        }
                    }
                }
                // Brackets
                b'[' | b']' => {
                    let data = self.data.get(start..start + 1)?;
                    return Some(Token::Raw(data));
                }
                _ => {
                    let count = self.skip_until(is_special_or_whitespace);
                    let content = self.data.get(start..start + count)?;
                    // Look for numbers but don't try to parse fractional
                    // values since we want to handle those with special
                    // precision
                    if (c.is_ascii_digit() || c == b'-') && !content.contains(&b'.') {
                        if let Some(int) = decode_int(content) {
                            // HACK: if we have an int followed by RD or -|
                            // then is a binary blob in Type1. Hack because
                            // this is not actually how PostScript works
                            // but Type1 fonts define /RD procs and this
                            // pattern is used by FreeType.
                            // <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L1351>
                            if self.accept_blob_start() {
                                // skip a single space
                                self.pos += 1;
                                // read the internal data
                                let data = self.read_bytes(int as usize)?;
                                // there's often some form of terminator here
                                // but let the calling code handle it because
                                // some buggy fonts may omit it
                                return Some(Token::Binary(data));
                            }
                            return Some(Token::Int(int));
                        }
                    }
                    return Some(Token::Raw(content));
                }
            }
        }
    }

    /// Special lookahead case for RD or -| which indicates a binary blob in
    /// Type1 fonts.
    fn accept_blob_start(&mut self) -> bool {
        let mut p = self.clone();
        p.skip_whitespace();
        let start = p.pos;
        p.skip_until(is_special_or_whitespace);
        let end = p.pos;
        if matches!(self.data.get(start..end), Some(b"RD") | Some(b"-|")) {
            self.pos = end;
            true
        } else {
            false
        }
    }

    fn accept(&mut self, token: Token) -> bool {
        let mut p = self.clone();
        if p.next() == Some(token) {
            self.pos = p.pos;
            true
        } else {
            false
        }
    }

    fn expect(&mut self, token: Token) -> Option<()> {
        (self.next()? == token).then_some(())
    }

    fn next_byte(&mut self) -> Option<u8> {
        let byte = self.peek_byte()?;
        self.pos += 1;
        Some(byte)
    }

    fn peek_byte(&self) -> Option<u8> {
        self.data.get(self.pos).copied()
    }

    fn read_bytes(&mut self, len: usize) -> Option<&'a [u8]> {
        let end = self.pos.checked_add(len)?;
        let content = self.data.get(self.pos..end)?;
        self.pos = end;
        Some(content)
    }

    fn skip_whitespace(&mut self) -> Option<()> {
        while is_whitespace(*self.data.get(self.pos)?) {
            self.pos += 1;
        }
        Some(())
    }

    fn skip_line(&mut self) {
        while let Some(c) = self.next_byte() {
            if c == b'\n' || c == b'\r' {
                break;
            }
        }
    }

    fn skip_until(&mut self, f: impl Fn(u8) -> bool) -> usize {
        let mut count = 0;
        while let Some(byte) = self.peek_byte() {
            if f(byte) {
                break;
            }
            self.pos += 1;
            count += 1;
        }
        count + 1
    }

    fn read_proc(&mut self, start: usize) -> Option<Token<'a>> {
        let mut nest_depth = 1i32;
        while let Some(c) = self.next_byte() {
            match c {
                b'{' => nest_depth = nest_depth.checked_add(1)?,
                b'}' => {
                    nest_depth -= 1;
                    if nest_depth == 0 {
                        break;
                    }
                }
                // Skip over comments
                b'%' => self.skip_line(),
                // Skip over literal strings since they can contain braces
                b'(' => {
                    self.read_lit_string(self.pos - 1)?;
                }
                _ => {}
            }
        }
        if nest_depth != 0 {
            // unterminated procedure
            return None;
        }
        let end = self.pos;
        Some(Token::Proc(self.data.get(start + 1..end - 1)?))
    }

    fn read_lit_string(&mut self, start: usize) -> Option<Token<'a>> {
        let mut nest_depth = 1i32;
        while let Some(c) = self.next_byte() {
            match c {
                b'(' => nest_depth = nest_depth.checked_add(1)?,
                b')' => {
                    nest_depth -= 1;
                    if nest_depth == 0 {
                        break;
                    }
                }
                // Escape sequence
                b'\\' => {
                    // Just eat the next byte. We only care
                    // about avoiding \( and \) anyway.
                    self.next_byte()?;
                }
                _ => {}
            }
        }
        if nest_depth != 0 {
            // unterminated string
            return None;
        }
        let end = self.pos;
        Some(Token::LitString(self.data.get(start + 1..end - 1)?))
    }

    fn read_hex_string(&mut self, start: usize) -> Option<Token<'a>> {
        while let Some(c) = self.next_byte() {
            if !is_whitespace(c) && !c.is_ascii_hexdigit() {
                break;
            }
        }
        let end = self.pos;
        if self.data.get(end - 1) != Some(&b'>') {
            // unterminated hex string
            return None;
        }
        Some(Token::HexString(self.data.get(start + 1..end - 1)?))
    }

    fn read_int(&mut self) -> Option<i64> {
        self.next().and_then(|t| match t {
            Token::Int(n) => Some(n),
            _ => None,
        })
    }

    fn read_num_as_int(&mut self) -> Option<i32> {
        match self.next()? {
            Token::Int(n) => Some(n as i32),
            // Note, FT calls PS_Conv_ToInt for fields that might contain
            // fractional bits but just ignores everything after the
            // initial integer, so we do the same
            Token::Raw(bytes) => decode_int_prefix(bytes, 0).map(|n| n.0 as i32),
            _ => None,
        }
    }

    fn read_string(&mut self) -> Option<String> {
        let bytes = match self.next()? {
            // FreeType accepts a name or a string here
            // <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/psaux/psobjs.c#L1123>
            Token::Name(bytes) | Token::LitString(bytes) => bytes,
            _ => return None,
        };
        Some(decode_name(bytes).into_owned())
    }
}

impl Parser<'_> {
    /// Parse a font matrix.
    ///
    /// Like FreeType, this is designed assuming a upem of 1000 and produces
    /// an identity matrix in that case. This is, the result is scaled such
    /// that 0.001 yields a value of 1.0.
    ///
    /// See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L1403>
    fn read_font_matrix(&mut self) -> Option<ScaledFontMatrix> {
        let mut components = [Fixed::ZERO; 6];
        // accept [ or { to match FreeType
        if !self.accept(Token::Raw(b"[")) {
            self.expect(Token::Raw(b"{"))?;
        }
        // read all components
        for component in &mut components {
            *component = match self.next()? {
                Token::Int(int) => Fixed::from_i32((int as i32).checked_mul(1000)?),
                Token::Raw(bytes) => decode_fixed(bytes, 3)?,
                _ => return None,
            }
        }
        // FreeType doesn't validate the closing delimiter, so just skip
        self.next()?;
        let temp_scale = components[3].abs();
        if temp_scale == Fixed::ZERO {
            return None;
        }
        let mut upem = 1000;
        if temp_scale != Fixed::ONE {
            upem = (Fixed::from_bits(1000) / temp_scale).to_bits();
            components[0] /= temp_scale;
            components[1] /= temp_scale;
            components[2] /= temp_scale;
            // don't scale components[3]
            components[4] /= temp_scale;
            components[5] /= temp_scale;
            if components[3] < Fixed::ZERO {
                components[3] = -Fixed::ONE;
            } else {
                components[3] = Fixed::ONE;
            }
        }
        // offsets must be expressed in integer font units
        for offset in components.iter_mut().skip(4) {
            *offset = Fixed::from_bits(offset.to_bits() >> 16);
        }
        let matrix = FontMatrix::from_elements(components);
        if transform::is_degenerate(&matrix) {
            return None;
        }
        Some(ScaledFontMatrix {
            matrix,
            scale: upem,
        })
    }

    /// Parse the set of subroutines.
    ///
    /// The `len_iv` parameter defines the number of prefix padding bytes for
    /// encrypted data. If < 0, then the data is not encrypted.
    ///
    /// See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L1720>
    fn read_subrs(&mut self, len_iv: i64) -> Option<Subrs> {
        let mut subrs = Subrs::default();
        let _: usize = match self.next()? {
            Token::Raw(b"[") => {
                // Just an empty array
                self.expect(Token::Raw(b"]"))?;
                return Some(subrs);
            }
            Token::Int(n) => n.try_into().ok()?,
            _ => return None,
        };
        self.expect(Token::Raw(b"array"))?;
        let mut is_dense = true;
        // The pattern for each subroutine is `dup <subr_num> <data>`
        while self.accept(Token::Raw(b"dup")) {
            let (Token::Int(n), Token::Binary(data)) = (self.next()?, self.next()?) else {
                return None;
            };
            // Skip the NP, | or noaccess. FreeType just skips whatever happens
            // to be here
            self.next();
            // There might be an additional put token following the binary data
            self.accept(Token::Raw(b"put"));
            let subr_num: u32 = n.try_into().ok()?;
            if subr_num as usize != subrs.index.len() {
                is_dense = false;
            }
            let start = subrs.data.len();
            if len_iv >= 0 {
                // use decryption; skip first len_iv bytes
                subrs
                    .data
                    .extend(decrypt(data.iter().copied(), CHARSTRING_SEED).skip(len_iv as usize));
            } else {
                // just add the data
                subrs.data.extend_from_slice(data);
            }
            let end = subrs.data.len();
            subrs.index.push((subr_num, start..end));
        }
        // If we don't have a dense set, sort the index by number
        if !is_dense {
            subrs.index.sort_unstable_by_key(|(n, ..)| *n);
        }
        subrs.is_dense = is_dense;
        subrs.data.shrink_to_fit();
        subrs.index.shrink_to_fit();
        Some(subrs)
    }

    /// Parse the set of charstrings.
    ///
    /// The `len_iv` parameter defines the number of prefix padding bytes for
    /// encrypted data. If < 0, then the data is not encrypted.
    ///
    /// See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L1919>
    fn read_charstrings(&mut self, len_iv: i64) -> Option<Charstrings> {
        let mut charstrings = Charstrings::default();
        let _: usize = match self.next()? {
            Token::Int(n) => n.try_into().ok()?,
            _ => return None,
        };
        let mut notdef_idx = None;
        while let Some(token) = self.next() {
            let name = match token {
                // Stop when we find a `def` or `end` keyword.
                // The ugliness matches the FT logic to handle some malformed
                // fonts:
                // <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L2006>
                Token::Raw(b"end") => {
                    if self
                        .peek_byte()
                        .map(is_special_or_whitespace)
                        .unwrap_or_default()
                    {
                        break;
                    } else {
                        continue;
                    }
                }
                Token::Raw(b"def") => {
                    // but ignore `def` if no charstring has been seen
                    if self
                        .peek_byte()
                        .map(is_special_or_whitespace)
                        .unwrap_or_default()
                        && !charstrings.index.is_empty()
                    {
                        break;
                    } else {
                        continue;
                    }
                }
                Token::Name(name) => name,
                _ => continue,
            };
            if name == b".notdef" {
                notdef_idx = Some(charstrings.index.len());
            }
            let Token::Binary(data) = self.next()? else {
                return None;
            };
            charstrings.push(name, data, len_iv);
        }
        match notdef_idx {
            Some(0) => {
                // .notdef found and at correct location
            }
            Some(idx) => {
                // .notdef found but at incorrect location. Swap with the
                // glyph at 0
                charstrings.index.swap(0, idx);
                charstrings.orig_notdef_index = Some(idx);
            }
            None => {
                // .notdef not found. Add it to the end and then swap with
                // the glyph at 0
                let idx = charstrings.index.len();
                charstrings.push(b".notdef", NOTDEF_GLYPH, -1);
                charstrings.index.swap(0, idx);
                charstrings.orig_notdef_index = Some(idx);
            }
        }
        charstrings.data.shrink_to_fit();
        charstrings.names.shrink_to_fit();
        charstrings.index.shrink_to_fit();
        Some(charstrings)
    }

    /// Reads a `/FontBBox`, each component rounded to a whole unit as
    /// FreeType rounds it.
    ///
    /// In the `/Blend` dictionary of a multiple master font, each component
    /// is an array with a value for each master. FreeType stores master 0's
    /// box over the font's own, so this returns master 0's values.
    fn read_font_bbox(&mut self) -> Option<[Fixed; 4]> {
        let mut bbox = [Fixed::ZERO; 4];
        // accept [ or { to match FreeType
        // Note that we parse { as a procedure so this needs some special
        // handling
        let mut parser;
        let parser = if self.accept(Token::Raw(b"[")) {
            self
        } else if let Token::Proc(proc) = self.next()? {
            parser = Parser::new(proc);
            &mut parser
        } else {
            return None;
        };
        // read all components
        for component in &mut bbox {
            let value = match parser.next()? {
                Token::Proc(masters) => Parser::new(masters).read_fixed()?,
                Token::Raw(b"[") => {
                    let first = parser.read_fixed()?;
                    while parser.next()? != Token::Raw(b"]") {}
                    first
                }
                Token::Int(int) => Fixed::from_i32(int as i32),
                Token::Raw(bytes) => decode_fixed(bytes, 0)?,
                _ => return None,
            };
            *component = round_fixed(value);
        }
        Some(bbox)
    }

    fn read_weight_vector(&mut self) -> Option<Vec<Fixed>> {
        self.accept(Token::Raw(b"["));
        let mut weights = Vec::new();
        while let Some(token) = self.next() {
            match token {
                Token::Raw(b"]") => break,
                Token::Int(val) => weights.push(Fixed::from_i32(val as _)),
                Token::Raw(raw) => weights.push(decode_fixed(raw, 0)?),
                _ => return None,
            }
        }
        Some(weights)
    }

    /// Reads a number as a fixed point value.
    fn read_fixed(&mut self) -> Option<Fixed> {
        match self.next()? {
            Token::Int(int) => Some(Fixed::from_i32(int as i32)),
            Token::Raw(raw) => decode_fixed(raw, 0),
            _ => None,
        }
    }

    /// Reads the names of the blend axes, as in `[/Weight /Width]`.
    fn read_blend_axis_types(&mut self) -> Option<Vec<String>> {
        self.expect(Token::Raw(b"["))?;
        let mut names = Vec::new();
        loop {
            match self.next()? {
                Token::Raw(b"]") => return Some(names),
                Token::Name(name) if !name.is_empty() => {
                    names.push(decode_name(name).into_owned());
                }
                _ => return None,
            }
        }
    }

    /// Reads the position of each master in the blend space, as in
    /// `[[0 0] [1 0] [0 1] [1 1]]`.
    fn read_blend_design_positions(&mut self) -> Option<Vec<Vec<Fixed>>> {
        self.expect(Token::Raw(b"["))?;
        let mut positions = Vec::new();
        loop {
            match self.next()? {
                Token::Raw(b"]") => return Some(positions),
                Token::Raw(b"[") => {
                    let mut position = Vec::new();
                    loop {
                        let mut peek = self.clone();
                        if peek.next()? == Token::Raw(b"]") {
                            *self = peek;
                            break;
                        }
                        position.push(self.read_fixed()?);
                    }
                    positions.push(position);
                }
                _ => return None,
            }
        }
    }

    /// Reads the map from design coordinates to blend coordinates for each
    /// axis, as in `[[[50 0] [1450 1]] [[50 0] [1450 1]]]`.
    fn read_blend_design_map(&mut self) -> Option<Vec<Vec<(i32, Fixed)>>> {
        self.expect(Token::Raw(b"["))?;
        let mut maps = Vec::new();
        loop {
            match self.next()? {
                Token::Raw(b"]") => return Some(maps),
                Token::Raw(b"[") => {
                    let mut map = Vec::new();
                    loop {
                        match self.next()? {
                            Token::Raw(b"]") => break,
                            Token::Raw(b"[") => {
                                // The design point is read as an integer,
                                // ignoring any fraction, as FreeType reads
                                // it.
                                let design = self.read_num_as_int()?;
                                let blend = self.read_fixed()?;
                                self.expect(Token::Raw(b"]"))?;
                                map.push((design, blend));
                            }
                            _ => return None,
                        }
                    }
                    maps.push(map);
                }
                _ => return None,
            }
        }
    }

    /// Reads an array and returns the number of elements in it.
    fn read_array_len(&mut self) -> Option<usize> {
        self.expect(Token::Raw(b"["))?;
        let mut len = 0;
        loop {
            match self.next()? {
                Token::Raw(b"]") => return Some(len),
                _ => len += 1,
            }
        }
    }

    /// Parse the encoding.
    ///
    /// See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L1474>
    fn read_encoding(&mut self, charstrings: &Charstrings) -> Option<RawEncoding> {
        match self.next()? {
            // Array of names where index == character code
            Token::Raw(b"[") => {
                let mut map = Vec::new();
                // Should always be 256 entries but preset values to notdef
                map.resize(256, GlyphId::NOTDEF);
                self.read_dense_encoding(|idx, name| {
                    if let Some((slot, gid)) = map
                        .get_mut(idx as usize)
                        .zip(charstrings.index_for_name(name))
                    {
                        *slot = gid.into();
                    }
                });
                Some(RawEncoding::Custom(map))
            }
            // Map of index to glyph name
            Token::Int(count) => {
                // We're limited to 256 character codes
                // <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L1518>
                let count: usize = count.clamp(0, 256) as usize;
                let mut map = Vec::new();
                // Start with all glyphs mapped to notdef
                map.resize(count, GlyphId::NOTDEF);
                self.read_sparse_encoding(|idx, name| {
                    if let Some((slot, gid)) = map
                        .get_mut(idx as usize)
                        .zip(charstrings.index_for_name(name))
                    {
                        *slot = gid.into();
                    }
                });
                Some(RawEncoding::Custom(map))
            }
            Token::Raw(b"StandardEncoding") => {
                Some(RawEncoding::Predefined(PredefinedEncoding::Standard))
            }
            Token::Raw(b"ExpertEncoding") => {
                Some(RawEncoding::Predefined(PredefinedEncoding::Expert))
            }
            Token::Raw(b"ISOLatin1Encoding") => {
                Some(RawEncoding::Predefined(PredefinedEncoding::IsoLatin1))
            }
            _ => None,
        }
    }

    /// Returns a custom encoding defined by an array of `/<name>`, where
    /// the index represents the character code, and invokes the given
    /// callback for each.
    fn read_dense_encoding(&mut self, mut f: impl FnMut(i64, &str)) -> Option<()> {
        // Eat the opening brace if present
        self.accept(Token::Raw(b"["));
        // Always expect 256 entries
        // <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/type1/t1load.c#L1510>
        let mut idx = 0;
        while let Some(token) = self.next() {
            match token {
                Token::Raw(b"]") => break,
                Token::Name(name) => {
                    let code = idx;
                    idx += 1;
                    f(code, &decode_name(name));
                }
                _ => {
                    // FreeType fails if missing a literal name here
                    return None;
                }
            }
        }
        Some(())
    }

    /// Reads a custom encoding defined by a map of `<charcode> /<name>`
    /// and invokes the given callback for each.
    fn read_sparse_encoding(&mut self, mut f: impl FnMut(i64, &str)) -> Option<()> {
        while let Some(token) = self.next() {
            match token {
                // The 'def' keyword ends the mapping
                Token::Raw(b"def") => break,
                Token::Int(code) => {
                    // read the name
                    let Some(Token::Name(name)) = self.next() else {
                        continue;
                    };
                    f(code, &decode_name(name));
                }
                _ => {}
            }
        }
        Some(())
    }
}

/// Decode an integer, optionally with a base.
///
/// See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/psaux/psconv.c#L161>
fn decode_int(bytes: &[u8]) -> Option<i64> {
    let s = std::str::from_utf8(bytes).ok()?;
    if let Some(hash_idx) = s.find('#') {
        if hash_idx == 1 || hash_idx == 2 {
            // It's a radix number, like 8#40.
            let radix_str = s.get(0..hash_idx)?;
            let number_str = s.get(hash_idx + 1..)?;
            let radix = radix_str
                .parse::<u32>()
                .ok()
                .filter(|n| (2..=36).contains(n))?;
            i64::from_str_radix(number_str, radix).ok()
        } else {
            s.parse::<i64>().ok()
        }
    } else {
        s.parse::<i64>().ok()
    }
}

/// Decode an integer at the given position, returning the value and the
/// index of the position following the decoded integer.
fn decode_int_prefix(bytes: &[u8], start: usize) -> Option<(i64, usize)> {
    let tail = bytes.get(start..)?;
    let end = tail
        .iter()
        .position(|c| *c != b'-' && !c.is_ascii_digit())
        .unwrap_or(tail.len());
    let int = decode_int(tail.get(..end)?)?;
    Some((int, start + end))
}

/// Rounds to a whole unit, halves away from zero, as FreeType's
/// `FT_RoundFix` does.
///
/// A value from 32767.5 up rounds to 32768, which `Fixed` can't hold, so
/// it saturates to `Fixed::MAX`. FreeType keeps 32768 in a 64-bit value;
/// both give 32768 once rounded up to whole units.
fn round_fixed(value: Fixed) -> Fixed {
    let bits = value.to_bits() as i64;
    let rounded = (bits + 0x8000 - (bits < 0) as i64) & !0xFFFF;
    Fixed::from_bits(rounded.min(i32::MAX as i64) as i32)
}

/// Decode a fixed point value, scaling to a specific power of
/// ten.
///
/// See <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/psaux/psconv.c#L195>
fn decode_fixed(bytes: &[u8], mut power_ten: i32) -> Option<Fixed> {
    const LIMIT: i32 = 0xCCCCCCC;
    let mut idx = 0;
    let &first = bytes.get(idx)?;
    let sign = if first == b'-' || first == b'+' {
        idx += 1;
        if first == b'-' {
            -1
        } else {
            1
        }
    } else {
        1
    };
    let overflow = || Some(Fixed::from_bits(0x7FFFFFFF * sign));
    let mut integral = 0;
    if *bytes.get(idx)? != b'.' {
        let (int, end_idx) = decode_int_prefix(bytes, idx)?;
        if int > 0x7FFF {
            return overflow();
        }
        integral = (int << 16) as i32;
        idx = end_idx;
    }
    let mut decimal = 0;
    let mut divider = 1;
    if bytes.get(idx) == Some(&b'.') {
        idx += 1;
        while let Some(byte) = bytes.get(idx).copied() {
            if !byte.is_ascii_digit() {
                break;
            }
            let digit = (byte - b'0') as i32;
            if divider < LIMIT && decimal < LIMIT {
                decimal = decimal * 10 + digit;
                if integral == 0 && power_ten > 0 {
                    power_ten -= 1;
                } else {
                    divider *= 10;
                }
            }
            idx += 1;
        }
    }
    if bytes.get(idx).map(|b| b.to_ascii_lowercase()) == Some(b'e') {
        idx += 1;
        let (exponent, _) = decode_int_prefix(bytes, idx)?;
        if exponent > 1000 {
            return overflow();
        } else if exponent < -1000 {
            // underflow
            return Some(Fixed::ZERO);
        } else {
            power_ten = power_ten.checked_add(exponent as i32)?;
        }
    }
    if integral == 0 && decimal == 0 {
        return Some(Fixed::ZERO);
    }
    while power_ten > 0 {
        if integral >= LIMIT {
            return overflow();
        }
        integral *= 10;
        if decimal >= LIMIT {
            if divider == 1 {
                return overflow();
            }
            divider /= 10;
        } else {
            decimal *= 10;
        }
        power_ten -= 1;
    }
    while power_ten < 0 {
        integral /= 10;
        if divider < LIMIT {
            divider *= 10;
        } else {
            decimal /= 10;
        }
        if integral == 0 && decimal == 0 {
            return Some(Fixed::ZERO);
        }
        power_ten += 1;
    }
    if decimal != 0 {
        decimal = (Fixed::from_bits(decimal) / Fixed::from_bits(divider)).to_bits();
        integral += decimal;
    }
    Some(Fixed::from_bits(integral * sign))
}

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

    #[test]
    fn pfb_tags() {
        // Text segment tag
        let data = [0x80, 0x01, 0x01, 0x02, 0x00, 0x00];
        let (tag, len) = decode_pfb_tag(&data, 0).unwrap();
        assert_eq!(tag, PFB_TEXT_SEGMENT_TAG);
        assert_eq!(len, 513);
        // Binary segment tag
        let data = [0x80, 0x02, 0x01, 0x03, 0x00, 0x00];
        let (tag, len) = decode_pfb_tag(&data, 0).unwrap();
        assert_eq!(tag, PFB_BINARY_SEGMENT_TAG);
        assert_eq!(len, 769);
        // Invalid tag
        let data = [0x00; 6];
        assert!(decode_pfb_tag(&data, 0).is_none());
        // Not enough data
        let data = [0x00; 5];
        assert!(decode_pfb_tag(&data, 0).is_none());
    }

    #[test]
    fn pfb_segments() {
        let segments = [
            vec![0x01; 8],
            vec![0x02; 10],
            vec![0x03; 4],
            vec![0x04; 255],
        ];
        // Write each segment to a buffer
        let mut buf = vec![];
        for segment in &segments {
            buf.push(0x80);
            buf.push(0x02);
            buf.push(segment.len() as u8);
            buf.extend_from_slice(&[0; 3]);
            for byte in segment {
                buf.push(*byte);
            }
        }
        // Now parse and compare
        let mut parsed_count = 0;
        for (parsed, expected) in decode_pfb_binary_segments(&buf).zip(&segments) {
            assert_eq!(parsed, expected);
            parsed_count += 1;
        }
        assert_eq!(parsed_count, segments.len());
    }

    #[test]
    fn hex_decode() {
        check_hex_decode(
            b"743F8413F3636CA85A9FFEFB50B4BB27",
            &[
                116, 63, 132, 19, 243, 99, 108, 168, 90, 159, 254, 251, 80, 180, 187, 39,
            ],
        );
    }

    #[test]
    fn hex_decode_ignores_whitespace() {
        check_hex_decode(
            b"743F 8413F3636C\nA85A9FFEF\tB50B     4BB27",
            &[
                116, 63, 132, 19, 243, 99, 108, 168, 90, 159, 254, 251, 80, 180, 187, 39,
            ],
        );
    }

    #[test]
    fn hex_decode_truncate() {
        check_hex_decode(b"743F.8413F3636CA85A9FFEFB50B4BB27", &[116, 63]);
    }

    #[test]
    fn hex_decode_odd_chars() {
        check_hex_decode(b"743", &[116, 48]);
    }

    #[track_caller]
    fn check_hex_decode(hex: &[u8], expected: &[u8]) {
        let decoded = decode_hex(hex.iter().copied()).collect::<Vec<_>>();
        assert_eq!(decoded, expected);
    }

    #[test]
    fn decrypt_bytes() {
        let cipher = [
            0x74, 0x3f, 0x84, 0x13, 0xf3, 0x63, 0x6c, 0xa8, 0x5a, 0x9f, 0xfe, 0xfb, 0x50, 0xb4,
            0xbb, 0x27,
        ];
        let plain = decrypt(cipher.iter().copied(), EEXEC_SEED).collect::<Vec<_>>();
        // First 4 bytes are random garbage
        assert_eq!(&plain[4..], b"dup\n/Private");
    }

    #[test]
    fn find_eexec() {
        // Just a space
        assert_eq!(
            find_eexec_data(b"dup\n/Private\ncurrentfile eexec *&&FW"),
            Some(31)
        );
        // Multiple spaces
        assert_eq!(
            find_eexec_data(b"dup\n/Private\ncurrentfile eexec     *&&FW"),
            Some(35)
        );
        // New lines
        assert_eq!(
            find_eexec_data(b"dup\n/Private\ncurrentfile eexec\n\n*&&FW"),
            Some(32)
        );
        // Only skip \r when it precedes \n
        assert_eq!(
            find_eexec_data(b"dup\n/Private\ncurrentfile eexec\r\n\r*&&FW"),
            Some(32)
        );
        // Skip eexec in comments and strings
        assert_eq!(
            find_eexec_data(b"% eexec in comment\n(eexec in string) currentfile eexec $$$$"),
            Some(55)
        );
        // No eexec
        assert!(find_eexec_data(b"% eexec in comment\n(eexec in string) currentfile").is_none());
    }

    #[test]
    fn read_pfb_raw_dicts() {
        let dicts = RawDicts::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFB).unwrap();
        check_noto_serif_base(dicts.base);
        check_noto_serif_private(&dicts.private);
    }

    #[test]
    fn read_pfa_raw_dicts() {
        let dicts = RawDicts::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA).unwrap();
        check_noto_serif_base(dicts.base);
        check_noto_serif_private(&dicts.private);
    }

    fn check_noto_serif_base(base: &[u8]) {
        const EXPECTED_PREFIX: &str = r#"%!PS-AdobeFont-1.0: NotoSerif-Regular 2.007; ttfautohint (v1.8) -l 8 -r 50 -G 200 -x 14 -D latn -f none -a qsq -X ""
%%Title: NotoSerif-Regular
%Version: 2.007; ttfautohint (v1.8) -l 8 -r 50 -G 200 -x 14 -D latn -f none -a qsq -X ""
%%CreationDate: Tue Feb 10 16:07:25 2026
%%Creator: www-data
%Copyright: Copyright 2015-2021 Google LLC. All Rights Reserved.
% Generated by FontForge 20190801 (http://fontforge.sf.net/)
%%EndComments

10 dict begin
/FontType 1 def
/FontMatrix [0.001 0 0 0.001 0 0 ]readonly def
/FontName /NotoSerif-Regular def
/FontBBox {5 0 989 775 }readonly def
"#;
        // git will replace \r\n with \n in pfa files on windows so strip
        // \r out to ensure a robust comparison
        let mut base = base.to_vec();
        base.retain(|&b| b != b'\r');
        assert!(base.starts_with(EXPECTED_PREFIX.as_bytes()));
    }

    fn check_noto_serif_private(private: &[u8]) {
        const EXPECTED_PREFIX: &str = r#"dup
/Private 8 dict dup begin
/RD{string currentfile exch readstring pop}executeonly def
/ND{noaccess def}executeonly def
/NP{noaccess put}executeonly def
/MinFeature{16 16}ND
/password 5839 def
/BlueValues [0 0 536 536 714 714 770 770 ]ND
/OtherSubrs"#;
        assert!(private.starts_with(EXPECTED_PREFIX.as_bytes()))
    }

    #[test]
    fn parse_ints() {
        check_tokens(
            "% a comment\n20 -30 2#1011 10#-5 %another!\r 16#fC",
            &[
                Token::Int(20),
                Token::Int(-30),
                Token::Int(11),
                Token::Int(-5),
                Token::Int(252),
            ],
        );
    }

    #[test]
    fn parse_num_to_int() {
        let mut parser =
            Parser::new(b"102 102.1 102.4 102.5 102.9 -102.1 -102.5 -102.9 8#146 16#66");
        for _ in 0..10 {
            assert_eq!(parser.read_num_as_int().unwrap().abs(), 102);
        }
        assert!(parser.next().is_none());
    }

    #[test]
    fn parse_strings() {
        check_tokens(
            "(string (nested) 1) % and a hex string:\n <DEAD BEEF>",
            &[
                Token::LitString(b"string (nested) 1"),
                Token::HexString(b"DEAD BEEF"),
            ],
        );
    }

    #[test]
    fn font_info_strings_use_utf8_then_latin1() {
        let mut parser = Parser::new(b"(Caf\xc3\xa9) (Caf\xe9) /Caf\xe9");
        assert_eq!(parser.read_string().as_deref(), Some("Café"));
        assert_eq!(parser.read_string().as_deref(), Some("Café"));
        assert_eq!(parser.read_string().as_deref(), Some("Café"));
    }

    #[test]
    fn latin1_glyph_and_encoding_names_match() {
        let mut charstrings = Charstrings::default();
        charstrings.push(b"Caf\xe9", b"", -1);
        assert_eq!(charstrings.name(0), Some("Café"));
        assert_eq!(charstrings.index_for_name("Café"), Some(0));

        let mut dense = Parser::new(b"[/Caf\xe9]");
        let mut dense_names = Vec::new();
        dense
            .read_dense_encoding(|code, name| dense_names.push((code, name.to_owned())))
            .unwrap();
        assert_eq!(dense_names, [(0, "Café".into())]);

        let mut sparse = Parser::new(b"65 /Caf\xe9 def");
        let mut sparse_names = Vec::new();
        sparse
            .read_sparse_encoding(|code, name| sparse_names.push((code, name.to_owned())))
            .unwrap();
        assert_eq!(sparse_names, [(65, "Café".into())]);
    }

    #[test]
    fn parse_unterminated_strings() {
        check_tokens("(string (nested) 1", &[]);
        check_tokens("<DEAD BEEF", &[]);
    }

    #[test]
    fn parse_procs() {
        check_tokens(
            "{a {nested 20 % comment\n} proc } % and a\n {simple proc}",
            &[
                Token::Proc(b"a {nested 20 % comment\n} proc "),
                Token::Proc(b"simple proc"),
            ],
        );
    }

    #[test]
    fn parse_procs_with_string_containing_unbalanced_braces() {
        check_tokens(
            "{a proc with (string {with braces}} {) }",
            &[Token::Proc(b"a proc with (string {with braces}} {) ")],
        );
    }

    #[test]
    fn parse_proc_with_single_int() {
        check_tokens(
            "dup 3 {3} executeonly put",
            &[
                Token::Raw(b"dup"),
                Token::Int(3),
                Token::Proc(b"3"),
                Token::Raw(b"executeonly"),
                Token::Raw(b"put"),
            ],
        );
    }

    #[test]
    fn parse_unterminated_procs() {
        check_tokens("{a {nested 20} proc", &[]);
    }

    #[test]
    fn parse_aggregate_tokens_without_whitespace() {
        check_tokens(
            "{(string)}3(string1)(string2)",
            &[
                Token::Proc(b"(string)"),
                Token::Int(3),
                Token::LitString(b"string1"),
                Token::LitString(b"string2"),
            ],
        );
    }

    #[test]
    fn parse_names() {
        check_tokens(
            "/FontMatrix\r %comment\n /CharStrings",
            &[Token::Name(b"FontMatrix"), Token::Name(b"CharStrings")],
        );
    }

    #[test]
    fn parse_binary_blobs() {
        check_tokens(
            "/.notdef 4 RD abcd \n5 11\n \t-| a83jnshf7 3 ",
            &[
                // simulates a charstring: name followed by data
                Token::Name(b".notdef"),
                Token::Binary(b"abcd"),
                // simulates a subr: index followed by data
                Token::Int(5),
                Token::Binary(b"a83jnshf7 3"),
            ],
        )
    }

    #[test]
    fn parse_base_dict_prefix() {
        let dicts = RawDicts::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA).unwrap();
        let ts = parse_to_tokens(dicts.base);
        assert_eq!(
            &ts[..19],
            &[
                Token::Int(10),
                Token::Raw(b"dict"),
                Token::Raw(b"begin"),
                Token::Name(b"FontType"),
                Token::Int(1),
                Token::Raw(b"def"),
                Token::Name(b"FontMatrix"),
                Token::Raw(b"["),
                Token::Raw(b"0.001"),
                Token::Int(0),
                Token::Int(0),
                Token::Raw(b"0.001"),
                Token::Int(0),
                Token::Int(0),
                Token::Raw(b"]"),
                Token::Raw(b"readonly"),
                Token::Raw(b"def"),
                Token::Name(b"FontName"),
                Token::Name(b"NotoSerif-Regular"),
            ]
        );
    }

    #[track_caller]
    fn check_tokens(source: &str, expected: &[Token]) {
        let ts = parse_to_tokens(source.as_bytes());
        assert_eq!(ts, expected);
    }

    fn parse_to_tokens(data: &'_ [u8]) -> Vec<Token<'_>> {
        let mut tokens = vec![];
        let mut parser = Parser::new(data);
        while let Some(token) = parser.next() {
            tokens.push(token);
        }
        tokens
    }

    #[test]
    fn parse_fixed() {
        // Direct conversions (power_ten = 0)
        assert_eq!(decode_fixed(b"42.5", 0).unwrap(), Fixed::from_f64(42.5));
        assert_eq!(
            decode_fixed(b"0.0015", 0).unwrap(),
            Fixed::from_f64(0.001495361328125)
        );
        assert_eq!(
            decode_fixed(b"425.000e-1", 0).unwrap(),
            Fixed::from_f64(42.5)
        );
        assert_eq!(
            decode_fixed(b"1.5e-3", 0).unwrap(),
            Fixed::from_f64(0.001495361328125)
        );
        // Scaled by 1000 (power_ten = 3)
        assert_eq!(decode_fixed(b"1.5", 3).unwrap(), Fixed::from_f64(1500.0));
        assert_eq!(decode_fixed(b"0.001", 3).unwrap(), Fixed::from_f64(1.0));
        assert_eq!(
            decode_fixed(b"15000e-4", 3).unwrap(),
            Fixed::from_f64(1500.0)
        );
        assert_eq!(decode_fixed(b"1.000e-3", 3).unwrap(), Fixed::from_f64(1.0));
    }

    #[test]
    fn parse_font_matrix() {
        // Standard matrix for 1000 upem
        assert_eq!(
            Parser::new(b"[0.001 0 0 0.001 0 0]")
                .read_font_matrix()
                .unwrap()
                .matrix,
            FontMatrix::IDENTITY,
        );
        // Matrix with a stretch along the x axis and a small
        // offset
        assert_eq!(
            Parser::new(b"[0.002 0 0 0.001 1 2e1]")
                .read_font_matrix()
                .unwrap()
                .matrix,
            FontMatrix::from_elements([
                Fixed::from_i32(2),
                Fixed::ZERO,
                Fixed::ZERO,
                Fixed::ONE,
                Fixed::from_bits(1000),
                Fixed::from_bits(20000)
            ])
        );
        // Matrix with modified upem
        assert_eq!(
            Parser::new(b"[0.001 0 0 0.0005 0.0 0.0]")
                .read_font_matrix()
                .unwrap(),
            ScaledFontMatrix {
                matrix: FontMatrix::from_elements([
                    Fixed::from_i32(2),
                    Fixed::ZERO,
                    Fixed::ZERO,
                    Fixed::ONE,
                    Fixed::from_i32(0),
                    Fixed::from_i32(0)
                ]),
                scale: 2000,
            }
        );
    }

    #[test]
    fn parse_subrs() {
        let dicts = RawDicts::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA).unwrap();
        let mut parser = Parser::new(&dicts.private);
        let mut subrs = None;
        while let Some(token) = parser.next() {
            if let Token::Name(b"Subrs") = token {
                subrs = parser.read_subrs(4);
                break;
            }
        }
        let mut subrs = subrs.unwrap();
        // The decrypted subroutines extracted from FreeType
        let expected_subrs: [&[u8]; 5] = [
            &[142, 139, 12, 16, 12, 17, 12, 17, 12, 33, 11],
            &[139, 140, 12, 16, 11],
            &[139, 141, 12, 16, 11],
            &[11],
            &[140, 142, 12, 16, 12, 17, 10, 11],
        ];
        assert_eq!(subrs.index.len(), expected_subrs.len());
        assert!(subrs.is_dense);
        // These subrs are densely allocated but check binary search mode
        // as well
        for is_dense in [true, false] {
            subrs.is_dense = is_dense;
            for (idx, &expected) in expected_subrs.iter().enumerate() {
                let subr = subrs.get(idx as u32).unwrap();
                assert_eq!(subr, expected);
            }
        }
    }

    #[test]
    fn parse_empty_array_subrs() {
        let subrs = Parser::new(b"[ ]").read_subrs(4).unwrap();
        assert!(subrs.data.is_empty());
        assert!(subrs.index.is_empty());
    }

    #[test]
    fn parse_empty_subrs() {
        let subrs = Parser::new(b" 0 array\nND\n").read_subrs(4).unwrap();
        assert!(subrs.data.is_empty());
        assert!(subrs.index.is_empty());
    }

    #[test]
    fn parse_malformed_subrs() {
        assert!(Parser::new(b" 20 \nND\n").read_subrs(4).is_none());
    }

    #[test]
    fn parse_subrs_duplicate_def() {
        // Two definitions of subrs.. we want to keep the first
        // one which has two entries at 5 and 42
        let private = b"/Subrs 2 array dup 5 2 RD nd NP dup 42 2 RD ab NP ND\n/Subrs 1 array dup 0 2 RD xy NP ND /CharStrings 0";
        let font = Type1Font::from_dicts(b"", private).unwrap();
        assert_eq!(font.subrs.index.len(), 2);
        assert_eq!(font.subrs.index[0].0, 5);
        assert_eq!(font.subrs.index[1].0, 42);
    }

    #[test]
    fn parse_charstrings() {
        let dicts = RawDicts::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA).unwrap();
        let mut parser = Parser::new(&dicts.private);
        let mut charstrings = None;
        while let Some(token) = parser.next() {
            if let Token::Name(b"CharStrings") = token {
                charstrings = parser.read_charstrings(4);
                break;
            }
        }
        let charstrings = charstrings.unwrap();
        assert_eq!(charstrings.num_glyphs(), 9);
        assert!(charstrings.orig_notdef_index.is_none());
        let expected_names = [
            ".notdef",
            "H",
            "f",
            "i",
            "x",
            "f_f.liga",
            "f_f_i.liga",
            "f_i.liga",
            "H.c2sc",
        ];
        let names = (0..charstrings.num_glyphs())
            .map(|idx| charstrings.name(idx).unwrap())
            .collect::<Vec<_>>();
        assert_eq!(names, expected_names);
        // Prefix (up to 8 bytes), extracted from FreeType
        let expected_charstrings_prefix: [&[u8]; 9] = [
            &[139, 248, 236, 13, 14],
            &[177, 249, 173, 13, 139, 4, 247, 183],
            &[166, 248, 5, 13, 139, 4, 247, 201],
            &[162, 247, 212, 13, 247, 30, 249, 16],
            &[144, 248, 214, 13, 139, 4, 247, 130],
            &[166, 249, 88, 13, 139, 4, 247, 181],
            &[166, 250, 126, 13, 139, 4, 247, 181],
            &[166, 249, 43, 13, 139, 4, 247, 181],
            &[180, 249, 60, 13, 139, 4, 247, 141],
        ];
        for (idx, &expected) in expected_charstrings_prefix.iter().enumerate() {
            let charstring = charstrings.get(idx as u32).unwrap();
            assert_eq!(&charstring[..expected.len()], expected);
        }
    }

    #[test]
    fn parse_charstrings_duplicate_def() {
        // Two definitions of charstrings.. we want to keep the first
        // one which has three glyphs: .notdef, H and I
        let private = b"/CharStrings 2 /.notdef 2 RD nd ND /H 2 RD ab ND /I 2 RD cd ND def\n/CharStrings 1 /B 2 RD xy ND def";
        let font = Type1Font::from_dicts(b"", private).unwrap();
        assert_eq!(font.num_glyphs(), 3);
        assert_eq!(font.charstrings.name(0).unwrap(), ".notdef");
        assert_eq!(font.charstrings.name(1).unwrap(), "H");
        assert_eq!(font.charstrings.name(2).unwrap(), "I");
    }

    #[test]
    fn parse_charstrings_missing_notdef() {
        let mut parser = Parser::new(b"1 /H 2 RD ab ND /B 2 RD xy ND");
        let charstrings = parser.read_charstrings(-1).unwrap();
        assert_eq!(charstrings.num_glyphs(), 3);
        assert_eq!(charstrings.orig_notdef_index, Some(2));
        let expected_glyphs: &[(&str, &[u8])] =
            &[(".notdef", NOTDEF_GLYPH), ("B", b"xy"), ("H", b"ab")];
        check_charstrings(&charstrings, expected_glyphs);
        let mut font = Type1Font::empty();
        font.charstrings = charstrings;
        assert_eq!(font.remapped_gid(GlyphId::new(0)), GlyphId::new(2));
        assert_eq!(font.remapped_gid(GlyphId::new(1)), GlyphId::new(1));
        assert_eq!(font.remapped_gid(GlyphId::new(2)), GlyphId::new(0));
    }

    #[test]
    fn parse_charstrings_notdef_moved() {
        let mut parser = Parser::new(b"1 /H 2 RD ab ND /.notdef 2 RD nd ND /B 2 RD xy ND");
        let charstrings = parser.read_charstrings(-1).unwrap();
        assert_eq!(charstrings.num_glyphs(), 3);
        assert_eq!(charstrings.orig_notdef_index, Some(1));
        let expected_glyphs: &[(&str, &[u8])] = &[(".notdef", b"nd"), ("H", b"ab"), ("B", b"xy")];
        check_charstrings(&charstrings, expected_glyphs);
        let mut font = Type1Font::empty();
        font.charstrings = charstrings;
        assert_eq!(font.remapped_gid(GlyphId::new(0)), GlyphId::new(1));
        assert_eq!(font.remapped_gid(GlyphId::new(1)), GlyphId::new(0));
        assert_eq!(font.remapped_gid(GlyphId::new(2)), GlyphId::new(2));
    }

    #[track_caller]
    fn check_charstrings(charstrings: &Charstrings, expected_glyphs: &[(&str, &[u8])]) {
        for (idx, expected) in expected_glyphs.iter().enumerate() {
            let idx = idx as u32;
            let name = charstrings.name(idx).unwrap();
            let data = charstrings.get(idx).unwrap();
            assert_eq!((name, data), *expected);
        }
    }

    #[test]
    fn parse_weight_vector() {
        let mut parser = Parser::new(b"[0 0.125, 1.25 -0.87]");
        let weights = parser
            .read_weight_vector()
            .unwrap()
            .drain(..)
            .map(|w| w.to_f32())
            .collect::<Vec<_>>();
        assert_eq!(weights, &[0.0, 0.125, 1.25, -0.8699951]);
    }

    #[test]
    fn parse_type1_font_pfb() {
        check_type1_font(
            &Type1Font::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFB).unwrap(),
        );
    }

    #[test]
    fn parse_type1_font_pfa() {
        check_type1_font(
            &Type1Font::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA).unwrap(),
        );
    }

    #[track_caller]
    fn check_type1_font(font: &Type1Font) {
        assert_eq!(font.name(), Some("NotoSerif-Regular"));
        assert_eq!(
            font.version(),
            Some(
                "2.007; ttfautohint (v1.8) -l 8 -r 50 -G 200 -x 14 -D latn -f none -a qsq -X \"\""
            )
        );
        assert_eq!(
            font.notice(),
            Some("Copyright 2015-2021 Google LLC. All Rights Reserved.")
        );
        assert_eq!(font.full_name(), Some("Noto Serif Regular"));
        assert_eq!(font.family_name(), Some("Noto Serif"));
        assert_eq!(font.weight(), Some("Book"));
        assert_eq!(font.italic_angle(), 0);
        assert!(!font.is_fixed_pitch());
        assert_eq!(font.underline_position(), -125);
        assert_eq!(font.underline_thickness(), 50);
        assert_eq!(
            font.bbox(),
            BoundingBox {
                x_min: Fixed::from_i32(5),
                y_min: Fixed::ZERO,
                x_max: Fixed::from_i32(989),
                y_max: Fixed::from_i32(775)
            }
        );
        assert_eq!(font.num_glyphs(), 9);
        assert_eq!(font.subrs.index.len(), 5);
        assert_eq!(
            font.matrix,
            ScaledFontMatrix {
                matrix: FontMatrix::IDENTITY,
                scale: 1000
            }
        );
        assert!(font
            .glyph_names()
            .map(|(_, name)| name)
            .take(4)
            .eq([".notdef", "H", "f", "i"].into_iter()))
    }

    #[test]
    fn parses_lowercase_is_fixed_pitch() {
        let dicts = RawDicts::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA).unwrap();
        let mut base = dicts.base.to_vec();
        base.extend_from_slice(b"\n/isFixedPitch true def\n");
        let font = Type1Font::from_dicts(&base, &dicts.private).unwrap();
        assert!(font.is_fixed_pitch());
    }

    #[test]
    fn parse_encoding() {
        assert!(matches!(
            Type1Font::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA)
                .unwrap()
                .encoding,
            Some(RawEncoding::Predefined(PredefinedEncoding::Standard)),
        ));
    }

    #[test]
    fn parse_known_encodings() {
        for (blob, encoding) in [
            (
                "StandardEncoding",
                RawEncoding::Predefined(PredefinedEncoding::Standard),
            ),
            (
                "ExpertEncoding",
                RawEncoding::Predefined(PredefinedEncoding::Expert),
            ),
            (
                "ISOLatin1Encoding",
                RawEncoding::Predefined(PredefinedEncoding::IsoLatin1),
            ),
        ] {
            assert_eq!(
                Parser::new(blob.as_bytes())
                    .read_encoding(&Charstrings::default())
                    .unwrap(),
                encoding
            );
        }
    }

    #[test]
    fn parse_custom_dense_encoding() {
        let mut map = Vec::new();
        map.resize(256, ".notdef".to_string());
        let mut parser = Parser::new(b"[/.notdef /A /b /.notdef /comma /at]");
        parser.read_dense_encoding(|idx, name| {
            map[idx as usize] = name.to_string();
        });
        for (ch, entry) in map.iter().enumerate() {
            let expected = match ch {
                1 => "A",
                2 => "b",
                4 => "comma",
                5 => "at",
                _ => ".notdef",
            };
            assert_eq!(entry, expected);
        }
    }

    #[test]
    fn parse_custom_sparse_encoding() {
        let mut map = Vec::new();
        map.resize(256, ".notdef".to_string());
        let mut parser = Parser::new(CUSTOM_SPARSE_ENCODING.as_bytes());
        parser.read_sparse_encoding(|idx, name| {
            map[idx as usize] = name.to_string();
        });
        for (ch, entry) in map.iter().enumerate() {
            let expected = match ch {
                66 => "B",
                97 => "a",
                64 => "at",
                44 => "comma",
                56 => "eight",
                _ => ".notdef",
            };
            assert_eq!(entry, expected);
        }
    }

    const CUSTOM_SPARSE_ENCODING: &str = r#"
        array
        0 1 255 {1 index exch /.notdef put} for
        dup 66 /B put
        dup 97 /a put
        dup 64 /at put
        dup 44 /comma put
        dup 56 /eight put
        readonly def    
    "#;

    #[test]
    fn eval_charstrings() {
        let font = Type1Font::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA).unwrap();
        let expected_eval_prefix = [
            "M38,0 L329,0 L329,42 L316,42 C293,42 274,46 258,54 C242,63 234,83 234,114",
            "M27,0 L336,0 L336,42 L298,42 C275,42 256,46 240,54 C224,63 216,83 216,114",
            "M161,636 C176,636 190,641 201,650 C212,659 218,675 218,698 C218,721 212,738 201,746",
            "M5,0 L243,0 L243,42 L240,42 C218,42 202,44 192,50 C183,54 178,62 178,73",
            "M27,0 L316,0 L316,42 L298,42 C275,42 256,46 240,54 C224,63 216,83 216,114",
            "M27,0 L316,0 L316,42 L298,42 C275,42 256,46 240,54 C224,63 216,83 216,114",
            "M27,0 L316,0 L316,42 L298,42 C275,42 256,46 240,54 C224,63 216,83 216,114",
            "M41,0 L290,0 L290,42 L269,42 C254,42 240,45 229,52 C218,58 212,73 212,98",
        ];
        // -1 to ignore the .notdef glyph
        assert_eq!(font.num_glyphs() as usize - 1, expected_eval_prefix.len());
        let mut commands = CaptureCommandSink::default();
        for (gid, expected_prefix) in (1..font.num_glyphs()).zip(&expected_eval_prefix) {
            commands.0.clear();
            font.evaluate_charstring(gid.into(), &mut commands).unwrap();
            assert!(commands.to_svg().starts_with(expected_prefix));
        }
    }

    #[test]
    fn eval_charstring_widths() {
        let font = Type1Font::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA).unwrap();
        let expected_widths = [
            600.0, 793.0, 369.0, 320.0, 578.0, 708.0, 1002.0, 663.0, 680.0,
        ];
        let mut commands = CaptureCommandSink::default();
        let widths = (0..font.num_glyphs())
            .map(|gid| {
                commands.0.clear();
                font.evaluate_charstring(gid.into(), &mut commands)
                    .unwrap()
                    .unwrap()
                    .to_f32()
            })
            .collect::<Vec<_>>();
        assert_eq!(widths, expected_widths);
    }

    #[test]
    fn csctx_seac_components() {
        let font = Type1Font::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA).unwrap();
        // Standard encoding for 'x' and 'i'
        let x_code = 120;
        let i_code = 105;
        let [x_data, i_data] = font.seac_components(x_code, i_code).unwrap();
        let name_to_gid = |name| {
            font.glyph_names()
                .find_map(|(gid, gname)| (name == gname).then_some(gid.to_u32()))
                .unwrap()
        };
        assert_eq!(x_data, font.charstrings.get(name_to_gid("x")).unwrap());
        assert_eq!(i_data, font.charstrings.get(name_to_gid("i")).unwrap());
    }

    #[test]
    fn csctx_subrs() {
        let font = Type1Font::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA).unwrap();
        assert!(!font.subrs.index.is_empty());
        for subr_idx in 0..font.subrs.index.len() {
            assert_eq!(
                font.subrs.get(subr_idx as u32).unwrap(),
                font.subr(subr_idx as _).unwrap()
            )
        }
    }

    #[test]
    fn encoding_mapping() {
        let font = Type1Font::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA).unwrap();
        let encoding = font.encoding().unwrap();
        let expected = [
            // code, gid, name
            (0, 0, ".notdef"),
            (72, 1, "H"),
            (102, 2, "f"),
            (105, 3, "i"),
            (120, 4, "x"),
        ];
        for (code, gid, name) in expected {
            assert_eq!(encoding.glyph_name(code).unwrap(), name);
            assert_eq!(encoding.map(code).unwrap().to_u32(), gid);
        }
    }
}

#[cfg(test)]
mod width_only_tests {
    use super::*;
    use crate::model::glyph::outline::NullSink;

    const FONTS: [&[u8]; 2] = [
        font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA,
        font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFB,
    ];

    #[test]
    fn the_quick_width_is_what_a_full_run_reports() {
        for data in FONTS {
            let font = Type1Font::new(data).unwrap();
            assert!(font.num_glyphs() > 1);
            // `evaluate_charstring` answers in the charstring's own space and
            // `evaluate_width` in design units, so the matrix goes on the
            // full run before the two can be compared. It maps one onto the
            // other unchanged in these fonts, so what this pins is the width
            // rather than the mapping; the CFF side has a font where the two
            // spaces differ.
            let transform = font.transform(None);
            for gid in (0..font.num_glyphs()).map(GlyphId::new) {
                let full = font
                    .evaluate_charstring(gid, &mut charstring::NullSink)
                    .unwrap()
                    .map(|width| transform.transform_h_metric(width));
                let quick = font.evaluate_width(gid).unwrap();
                assert_eq!(full, quick, "glyph {gid}");
            }
        }
    }

    #[test]
    fn the_quick_advance_is_what_drawing_reports() {
        // Drawing scales the width by the font matrix, and by ppem where one
        // is given, so this is what catches an advance that skipped either.
        for data in FONTS {
            let font = Type1Font::new(data).unwrap();
            for ppem in [None, Some(16.0), Some(72.0)] {
                for gid in (0..font.num_glyphs()).map(GlyphId::new) {
                    let drawn = font.draw(gid, ppem, &mut NullSink).unwrap();
                    let quick = font.advance(gid, ppem).unwrap();
                    assert_eq!(drawn, quick, "glyph {gid} at {ppem:?}");
                }
            }
        }
    }

    #[test]
    fn every_glyph_states_a_width() {
        // A Type 1 charstring must open with hsbw or sbw, so one that
        // reports nothing is malformed rather than merely silent.
        let font = Type1Font::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA).unwrap();
        for gid in (0..font.num_glyphs()).map(GlyphId::new) {
            assert!(font.evaluate_width(gid).unwrap().is_some(), "glyph {gid}");
        }
    }
}

#[cfg(test)]
mod extents_tests {
    use super::*;
    use crate::model::glyph::outline::PathSink;

    const FONTS: [&[u8]; 2] = [
        font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA,
        font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFB,
    ];

    /// Collects the same box from the other side, through a pen.
    #[derive(Default)]
    struct BoundsPen(Option<[f32; 4]>);

    impl BoundsPen {
        fn add(&mut self, x: f32, y: f32) {
            self.0 = Some(match self.0 {
                Some([a, b, c, d]) => [a.min(x), b.min(y), c.max(x), d.max(y)],
                None => [x, y, x, y],
            });
        }
    }

    impl PathSink for BoundsPen {
        fn move_to(&mut self, x: f32, y: f32) {
            self.add(x, y);
        }
        fn line_to(&mut self, x: f32, y: f32) {
            self.add(x, y);
        }
        fn quad_to(&mut self, cx: f32, cy: f32, x: f32, y: f32) {
            self.add(cx, cy);
            self.add(x, y);
        }
        fn curve_to(&mut self, cx0: f32, cy0: f32, cx1: f32, cy1: f32, x: f32, y: f32) {
            self.add(cx0, cy0);
            self.add(cx1, cy1);
            self.add(x, y);
        }
        fn close(&mut self) {}
    }

    fn corners(b: BoundingBox<f32>) -> [f32; 4] {
        [b.x_min, b.y_min, b.x_max, b.y_max]
    }

    #[test]
    fn the_extents_are_the_box_drawing_stays_within() {
        // The same pipeline either way, so the two agree exactly. A
        // difference would mean the box was measured somewhere the drawing
        // was not, which is what transforming a finished box would do.
        for data in FONTS {
            let font = Type1Font::new(data).unwrap();
            for ppem in [None, Some(16.0), Some(72.0)] {
                let mut drew = 0;
                for gid in (0..font.num_glyphs()).map(GlyphId::new) {
                    let mut pen = BoundsPen::default();
                    font.draw(gid, ppem, &mut pen).unwrap();
                    let measured = font.extents(gid, ppem).unwrap();
                    assert_eq!(measured.map(corners), pen.0, "glyph {gid} at {ppem:?}");
                    drew += pen.0.is_some() as u32;
                }
                assert!(drew > 1, "almost nothing drew, so this proves little");
            }
        }
    }

    #[test]
    fn the_exact_box_and_the_sized_one_agree_at_no_size() {
        // Both apply the matrix; only the size and the number type differ.
        let font = Type1Font::new(FONTS[0]).unwrap();
        let mut checked = 0;
        for gid in (0..font.num_glyphs()).map(GlyphId::new) {
            let exact = font.evaluate_extents(gid).unwrap();
            let sized = font.extents(gid, None).unwrap();
            assert_eq!(
                exact.map(|b| [
                    b.x_min.to_f32(),
                    b.y_min.to_f32(),
                    b.x_max.to_f32(),
                    b.y_max.to_f32()
                ]),
                sized.map(corners),
                "glyph {gid}"
            );
            checked += exact.is_some() as u32;
        }
        assert!(checked > 1, "almost nothing drew, so this proves little");
    }

    #[test]
    fn a_size_scales_the_box() {
        // `evaluate_extents` measures before the matrix and any size;
        // `extents` measures after both. This font's matrix maps its
        // charstring space onto design units unchanged, so what separates
        // the two here is the size. A matrix that does more than that is
        // covered where one exists, on the CFF side.
        let font = Type1Font::new(FONTS[0]).unwrap();
        let mut checked = 0;
        for gid in (0..font.num_glyphs()).map(GlyphId::new) {
            let Some(unscaled) = font.extents(gid, None).unwrap() else {
                continue;
            };
            let scaled = font.extents(gid, Some(16.0)).unwrap().unwrap();
            let factor = 16.0 / font.upem() as f32;
            for (a, b) in corners(scaled).iter().zip(corners(unscaled)) {
                assert!(
                    (a - b * factor).abs() < 0.05,
                    "glyph {gid}: {a} against {}",
                    b * factor
                );
            }
            checked += 1;
        }
        assert!(checked > 1, "almost nothing drew, so this proves little");
    }

    #[test]
    fn a_glyph_that_draws_nothing_has_no_extents() {
        // A box of no size at the origin would say something different from
        // drawing nothing at all.
        let font = Type1Font::new(FONTS[0]).unwrap();
        let empty = (0..font.num_glyphs())
            .map(GlyphId::new)
            .find(|gid| font.extents(*gid, None).unwrap().is_none());
        let gid = empty.expect("this font has no empty glyph to check");
        assert!(font.evaluate_extents(gid).unwrap().is_none());
    }
}

#[cfg(test)]
pub(crate) mod multiple_master_tests {
    use super::*;
    use crate::model::glyph::outline::PathElement;
    use alloc::{format, vec};

    /// Encodes `value` as a Type 1 charstring number.
    fn num(value: i32) -> Vec<u8> {
        match value {
            -107..=107 => vec![(value + 139) as u8],
            108..=1131 => {
                let v = value - 108;
                vec![(v / 256 + 247) as u8, (v % 256) as u8]
            }
            -1131..=-108 => {
                let v = -value - 108;
                vec![(v / 256 + 251) as u8, (v % 256) as u8]
            }
            _ => {
                let mut bytes = vec![255];
                bytes.extend_from_slice(&value.to_be_bytes());
                bytes
            }
        }
    }

    /// Builds a charstring from numbers and operators, where an operator is
    /// given as its bytes.
    fn charstring(parts: &[&[i32]]) -> Vec<u8> {
        let mut bytes = Vec::new();
        for part in parts {
            match part {
                [op] if *op < 0 => bytes.push((-op) as u8),
                [12, op] if *op < 0 => bytes.extend_from_slice(&[12, (-op) as u8]),
                values => values.iter().for_each(|&v| bytes.extend(num(v))),
            }
        }
        bytes
    }

    const HSBW: &[i32] = &[-13];
    const RMOVETO: &[i32] = &[-21];
    const RLINETO: &[i32] = &[-5];
    const CLOSEPATH: &[i32] = &[-9];
    const ENDCHAR: &[i32] = &[-14];
    const CALLOTHERSUBR: &[i32] = &[12, -16];
    const POP: &[i32] = &[12, -17];

    /// Reads the font that [`pfa_with_blend`] makes.
    fn font_with_blend(blend: &str) -> Option<Type1Font> {
        Type1Font::new(&pfa_with_blend(blend)).ok()
    }

    /// Returns a PFA font with `blend` in place of the blend entries of its
    /// font dictionary.
    ///
    /// Glyph 1 moves to x = 100 + 200 * w1, where w1 is the weight of the
    /// second master, then draws a triangle.
    pub(crate) fn pfa_with_blend(blend: &str) -> Vec<u8> {
        let base = format!(
            "%!PS-AdobeFont-1.0: Test\n/FontName /Test def \
             /FontMatrix [0.001 0 0 0.001 0 0] readonly def {blend}\n\
             currentfile eexec\n"
        );
        let notdef = charstring(&[&[0, 500], HSBW, ENDCHAR]);
        let glyph = charstring(&[
            &[0, 500],
            HSBW,
            &[100, 50, 200, 0, 4, 15],
            CALLOTHERSUBR,
            POP,
            POP,
            RMOVETO,
            &[300, 0],
            RLINETO,
            &[0, 300],
            RLINETO,
            CLOSEPATH,
            ENDCHAR,
        ]);
        let mut private = b"/lenIV -1 def /CharStrings 2 dict dup begin\n".to_vec();
        for (name, bytes) in [("/.notdef", notdef), ("/a", glyph)] {
            private.extend_from_slice(format!("{name} {} RD ", bytes.len()).as_bytes());
            private.extend_from_slice(&bytes);
            private.extend_from_slice(b" ND\n");
        }
        private.extend_from_slice(b"end");
        // Encrypted as eexec data, after four bytes of padding, and written
        // as hex.
        let mut seed = EEXEC_SEED;
        let mut pfa = base.into_bytes();
        for plain in [0u8; 4].iter().chain(&private) {
            let cipher = (*plain as u32 ^ (seed >> 8)) as u8;
            seed = (cipher as u32)
                .wrapping_add(seed)
                .wrapping_mul(52845)
                .wrapping_add(22719)
                & 0xFFFF;
            pfa.extend_from_slice(format!("{cipher:02x}").as_bytes());
        }
        pfa
    }

    pub(crate) const ONE_AXIS: &str = "/BlendAxisTypes [/Weight] def \
        /BlendDesignPositions [[0] [1]] def \
        /BlendDesignMap [[[100 0] [900 1]]] def \
        /WeightVector [0.75 0.25] def";

    fn bbox(font: &Type1Font) -> [f32; 4] {
        let b = font.bbox();
        [b.x_min, b.y_min, b.x_max, b.y_max].map(|v| v.to_f32())
    }

    // Every box is rounded to whole units, halves away from zero.
    #[test]
    fn boxes_are_rounded() {
        let font = font_with_blend("/FontBBox {-10.4 -20.5 1000.5 900.25} readonly def").unwrap();
        assert_eq!(bbox(&font), [-10.0, -21.0, 1001.0, 900.0]);
    }

    // A bound that rounds past the largest fixed value saturates instead of
    // overflowing.
    #[test]
    fn box_rounding_saturates() {
        let font = font_with_blend("/FontBBox [-32768 0 32767.5 900] def").unwrap();
        assert_eq!(font.bbox().x_max, Fixed::MAX);
        assert_eq!(font.bbox().x_min, Fixed::from_i32(-32768));
    }

    // The /Blend dictionary holds a box for each master, and master 0's
    // replaces the font's own.
    #[test]
    fn box_is_master_zeros() {
        let font = font_with_blend(&format!(
            "{ONE_AXIS} /FontBBox {{-157 -257 1194 872}} readonly def \
             /Blend 3 dict dup begin \
             /FontBBox {{{{-55 -112}} {{-261 -262}} {{424.4 1156}} {{811 970}}}} def \
             end def"
        ))
        .unwrap();
        assert_eq!(bbox(&font), [-55.0, -261.0, 424.0, 811.0]);
        let bracketed = font_with_blend(&format!(
            "{ONE_AXIS} /Blend 3 dict dup begin \
             /FontBBox [[-55 -112] [-261 -262] [424 1156] [811 970]] def end def"
        ))
        .unwrap();
        assert_eq!(bbox(&bracketed), [-55.0, -261.0, 424.0, 811.0]);
    }

    // The /Blend dictionary's font info holds a value for each master, and
    // the font keeps its own.
    #[test]
    fn font_info_keeps_its_own_values() {
        let font = font_with_blend(&format!(
            "{ONE_AXIS} /ItalicAngle -12 def /isFixedPitch true def \
             /UnderlinePosition -100 def /UnderlineThickness 50 def \
             /Blend 3 dict dup begin /FontInfo 4 dict dup begin \
             /ItalicAngle [-12 0] def /isFixedPitch [false false] def \
             /UnderlinePosition [-80 -120] def /UnderlineThickness [40 60] def \
             end def end def"
        ))
        .unwrap();
        assert_eq!(font.italic_angle(), -12);
        assert!(font.is_fixed_pitch());
        assert_eq!(font.underline_position(), -100);
        assert_eq!(font.underline_thickness(), 50);
    }

    fn first_move(instance: Type1Instance) -> f32 {
        let mut path = Vec::<PathElement>::new();
        instance.draw(GlyphId::new(1), None, &mut path).unwrap();
        match path[0] {
            PathElement::MoveTo { x, .. } => x,
            _ => panic!("expected a move"),
        }
    }

    #[test]
    fn reads_the_design_space() {
        let font = font_with_blend(ONE_AXIS).unwrap();
        let mm = font.multiple_master().unwrap();
        assert_eq!(mm.num_masters(), 2);
        let [axis] = mm.axes() else {
            panic!("expected one axis")
        };
        assert_eq!(axis.name(), Some("Weight"));
        assert_eq!((axis.min_design(), axis.max_design()), (100, 900));
        let mut blend = [Fixed::ZERO];
        mm.default_blend(&mut blend);
        assert_eq!(blend, [Fixed::from_f64(0.25)]);
        assert_eq!(axis.blend_to_design(blend[0]), 300);
    }

    #[test]
    fn draws_with_a_blend_of_the_masters() {
        let font = font_with_blend(ONE_AXIS).unwrap();
        let mm = font.multiple_master().unwrap();
        // The font's own weights.
        assert_eq!(first_move(font.default_instance()), 150.0);
        let mut blend = [Fixed::ZERO];
        let mut weights = [Fixed::ZERO; 2];
        for (design, x) in [(100.0, 100.0), (500.0, 200.0), (900.0, 300.0)] {
            mm.design_to_blend(&[Fixed::from_f64(design)], &mut blend);
            mm.blend_to_weights(&blend, &mut weights);
            assert_eq!(first_move(font.instance(&weights)), x, "{design}");
        }
    }

    // Blends that FreeType sets aside leave an ordinary font, whose glyphs
    // fail where they blend.
    #[test]
    fn inconsistent_blends_are_set_aside() {
        for blend in [
            // Three masters cannot sit at the corners of one axis.
            "/BlendDesignPositions [[0] [1] [0.5]] def \
             /BlendDesignMap [[[100 0] [900 1]]] def \
             /WeightVector [0.5 0.25 0.25] def",
            // No design map.
            "/BlendDesignPositions [[0] [1]] def /WeightVector [0.75 0.25] def",
            // Only weights, as in a font made from one instance.
            "/WeightVector [0.75 0.25] def",
        ] {
            let font = font_with_blend(blend).unwrap();
            assert!(font.multiple_master().is_none(), "{blend}");
            let mut path = Vec::<PathElement>::new();
            assert!(font.draw(GlyphId::new(1), None, &mut path).is_err());
        }
    }

    // Counts that disagree make FreeType refuse the font.
    #[test]
    fn conflicting_counts_refuse_the_font() {
        for blend in [
            "/BlendDesignPositions [[0] [1]] def /WeightVector [0.5 0.25 0.25] def",
            "/BlendAxisTypes [/Weight /Width] def /BlendDesignPositions [[0] [1]] def",
            "/BlendDesignMap [[[100 0] [900 1]]] def /BlendDesignMap [[[100 0] [900 1]]] def",
            "/BlendAxisTypes [/A /B /C /D /E] def",
        ] {
            assert!(font_with_blend(blend).is_none(), "{blend}");
        }
    }

    /// The design space of a font with two axes, each mapping 50 to 1450,
    /// whose default is the last master.
    fn two_axes() -> MultipleMaster {
        let axis = |name: &str| MasterAxis {
            name: Some(name.into()),
            design_points: vec![50, 1450],
            blend_points: vec![Fixed::ZERO, Fixed::ONE],
        };
        MultipleMaster {
            axes: vec![axis("Weight"), axis("Width")],
            num_masters: 4,
            default_weights: vec![Fixed::ZERO, Fixed::ZERO, Fixed::ZERO, Fixed::ONE],
        }
    }

    // The blends and weights FreeType computes for these design coordinates
    // in a font with this design space.
    #[test]
    fn design_coordinates_convert_as_in_freetype() {
        let mm = two_axes();
        for (design, blend, weights) in [
            (
                &[471.5, 819.4][..],
                [19754, 35998],
                [20635, 8903, 25147, 10851],
            ),
            (
                &[470.49, 820.51],
                [19661, 36092],
                [20611, 8833, 25264, 10828],
            ),
            (&[1000.0, 333.0], [44471, 13248], [16807, 35481, 4258, 8990]),
            (&[-100.0, 2000.0], [0, 65536], [0, 0, 65536, 0]),
            // The missing axis takes (1450 - 50) / 2 = 700.
            (&[300.0], [11703, 30427], [28839, 6270, 24994, 5433]),
        ] {
            let design: Vec<_> = design
                .iter()
                .map(|v| Fixed::from_bits((v * 65536.0) as i32))
                .collect();
            let mut actual_blend = [Fixed::ZERO; 2];
            mm.design_to_blend(&design, &mut actual_blend);
            assert_eq!(actual_blend.map(Fixed::to_bits), blend, "{design:?}");
            let mut actual_weights = [Fixed::ZERO; 4];
            mm.blend_to_weights(&actual_blend, &mut actual_weights);
            assert_eq!(actual_weights.map(Fixed::to_bits), weights, "{design:?}");
        }
        let mut default = [Fixed::ZERO; 2];
        mm.default_blend(&mut default);
        assert_eq!(default, [Fixed::ONE; 2]);
        assert!(mm
            .axes()
            .iter()
            .all(|axis| axis.blend_to_design(Fixed::ONE) == 1450));
    }

    // Between two points that are not the first, FreeType's interpolation
    // leaves out the blend at the lower point.
    #[test]
    fn three_point_maps_interpolate_as_in_freetype() {
        let axis = MasterAxis {
            name: None,
            design_points: vec![0, 500, 1000],
            blend_points: vec![Fixed::ZERO, Fixed::from_f64(0.6), Fixed::ONE],
        };
        assert_eq!(axis.design_to_blend(250), Fixed::from_f64(0.3));
        assert_eq!(axis.design_to_blend(500), Fixed::from_f64(0.6));
        // The specification gives 0.8.
        assert_eq!(axis.design_to_blend(750), Fixed::from_f64(0.2));
        assert_eq!(axis.design_to_blend(2000), Fixed::ONE);
    }

    // Design coordinates round to whole numbers, a half away from zero.
    #[test]
    fn design_coordinates_round_away_from_zero() {
        let axis = MasterAxis {
            name: None,
            design_points: vec![-10, 10],
            blend_points: vec![Fixed::ZERO, Fixed::ONE],
        };
        let mm = MultipleMaster {
            axes: vec![axis],
            num_masters: 2,
            default_weights: vec![Fixed::ONE, Fixed::ZERO],
        };
        let mut blend = [Fixed::ZERO];
        for (design, rounded) in [(0.5, 1), (-0.5, -1), (0.49, 0), (-1.5, -2)] {
            mm.design_to_blend(&[Fixed::from_f64(design)], &mut blend);
            assert_eq!(blend[0], mm.axes()[0].design_to_blend(rounded), "{design}");
        }
    }

    // A map can span every design coordinate without overflowing.
    #[test]
    fn widest_design_maps_convert() {
        let axis = MasterAxis {
            name: None,
            design_points: vec![i32::MIN, i32::MAX],
            blend_points: vec![Fixed::ZERO, Fixed::ONE],
        };
        assert_eq!(axis.design_to_blend(0), Fixed::from_f64(0.5));
        assert_eq!(axis.blend_to_design(Fixed::from_f64(0.5)), 0);
        assert_eq!(axis.blend_to_design(Fixed::ONE), i32::MAX);
        assert_eq!(axis.blend_to_design(Fixed::ZERO), i32::MIN);
        let mm = MultipleMaster {
            axes: vec![axis],
            num_masters: 2,
            default_weights: vec![Fixed::from_f64(0.5), Fixed::from_f64(0.5)],
        };
        // A missing coordinate is half the range, which is beyond the map.
        let mut blend = [Fixed::ZERO];
        mm.design_to_blend(&[], &mut blend);
        assert_eq!(blend[0], Fixed::ONE);
        let font = font_with_blend(
            "/BlendAxisTypes [/Weight] def /BlendDesignPositions [[0] [1]] def              /BlendDesignMap [[[-2147483648 0] [2147483647 1]]] def              /WeightVector [0.5 0.5] def",
        )
        .unwrap();
        assert!(font.multiple_master().is_some());
    }
}