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
//! Per-glyph metrics.

#[cfg(feature = "libm")]
#[allow(unused_imports)]
use core_maths::CoreFloat;

use crate::tables::glyf::outline::{
    Buffers, OutlineContext, OutlinePlan, ScaleF32 as OutlineScaleF32,
};
use crate::{
    mem::with_scratch,
    model::{
        glyph::{
            bitmap::BitmapStrikes,
            color::ColorGlyphs,
            outline::{OutlineGlyphs, OutlineSource},
            GlyphSource, GlyphSourceOrder,
        },
        metrics::{LineExtents, Metrics, Scale, Scale26Dot6, ScaleF32, ScaledGlyphMetrics},
        Font, Kind,
    },
    ps::{cff::CffFontRef, charstring::CommandSink, type1::Type1Instance},
    tables::{glyf::Glyf, hmtx::GlyphMetricRecords, loca::Loca},
};
use types::{BoundingBox, F2Dot14, F48Dot16, Fixed, GlyphId, Point};

/// Where a glyph's ink sits, relative to its origin.
///
/// A bearing and a size rather than two corners, which is the shape the
/// metrics built on it use.
#[derive(Copy, Clone, Default, PartialEq, Eq, Debug)]
pub struct GlyphExtents<T> {
    /// Distance from the origin to the left edge of the ink.
    pub x_bearing: T,
    /// Distance from the origin to the top edge of the ink.
    pub y_bearing: T,
    /// Width of the ink, rightward from `x_bearing`.
    pub width: T,
    /// Height of the ink, downward from `y_bearing`.
    ///
    /// Never negative. A caller whose own extents count upward negates it.
    pub height: T,
}

impl<T> From<BoundingBox<T>> for GlyphExtents<T>
where
    T: Copy + PartialOrd + core::ops::Sub<Output = T>,
{
    /// Converts the corners of a box into a bearing and a size.
    ///
    /// The corners can be in either order. A reversed box still produces a
    /// non-negative size.
    fn from(bounds: BoundingBox<T>) -> Self {
        let (left, right) = if bounds.x_min <= bounds.x_max {
            (bounds.x_min, bounds.x_max)
        } else {
            (bounds.x_max, bounds.x_min)
        };
        let (bottom, top) = if bounds.y_min <= bounds.y_max {
            (bounds.y_min, bounds.y_max)
        } else {
            (bounds.y_max, bounds.y_min)
        };
        Self {
            x_bearing: left,
            y_bearing: top,
            width: right - left,
            height: top - bottom,
        }
    }
}

/// Measurements of individual glyphs at one location.
///
/// Cheap to obtain, so it can be taken per query rather than held.
#[derive(Clone, Copy)]
pub struct GlyphMetrics<'a> {
    h_metrics: &'a GlyphMetricRecords<'a>,
    font: &'a Font,
    global: &'a Metrics,
    coords: &'a [F2Dot14],
    num_glyphs: u32,
    units_per_em: u16,
    order: GlyphSourceOrder,
    bitmap_ppem: Option<f32>,
}

impl<'a> GlyphMetrics<'a> {
    /// Creates metrics for `font` at the location in `coords`.
    #[inline]
    pub(crate) fn new(font: &'a Font, global: &'a Metrics, coords: &'a [F2Dot14]) -> Self {
        Self {
            h_metrics: font.h_metric_records(),
            font,
            global,
            coords,
            num_glyphs: global.num_glyphs,
            units_per_em: global.units_per_em,
            order: GlyphSourceOrder::DEFAULT,
            bitmap_ppem: None,
        }
    }

    /// Returns these metrics in the units `scale` describes.
    #[inline]
    pub fn scaled<S: Scale>(self, scale: S) -> ScaledGlyphMetrics<'a, 'static, S> {
        ScaledGlyphMetrics::new(self, scale)
    }

    /// Returns these metrics in pixels at `ppem`, as `f32`.
    #[inline]
    pub fn scaled_f32(self, ppem: f32) -> ScaledGlyphMetrics<'a, 'static, ScaleF32> {
        let units_per_em = self.units_per_em;
        self.scaled(ScaleF32::from_ppem(ppem, units_per_em))
    }

    /// Returns these metrics in pixels at `ppem`, in 26.6 fixed point.
    #[inline]
    pub fn scaled_26dot6(self, ppem: f32) -> ScaledGlyphMetrics<'a, 'static, Scale26Dot6> {
        let units_per_em = self.units_per_em;
        self.scaled(Scale26Dot6::from_ppem(ppem, units_per_em))
    }

    /// Measures ink using the sources in `order`.
    ///
    /// Each source in the font is tried in turn until one provides extents
    /// for the glyph. [`GlyphSourceOrder::DEFAULT`] tries bitmaps, then color,
    /// then outlines.
    pub fn with_extents_source_order(self, order: GlyphSourceOrder) -> Self {
        Self { order, ..self }
    }

    /// Measures bitmaps using the strike for `ppem`.
    ///
    /// This selects the smallest strike that is at least `ppem`, or the
    /// largest strike if none is that large. `None` (the default) or a size
    /// that is not positive selects the largest strike. A glyph missing from
    /// that strike is measured by the next source in the order, even if
    /// another strike contains it. No other metric depends on the size.
    pub(crate) fn with_bitmap_ppem(self, ppem: Option<f32>) -> Self {
        Self {
            bitmap_ppem: ppem,
            ..self
        }
    }

    /// Returns the order used to measure ink.
    pub fn extents_source_order(&self) -> GlyphSourceOrder {
        self.order
    }

    /// Returns the advance width of `glyph`, in font units.
    ///
    /// See [`h_advance_exact`](Self::h_advance_exact) for the width before
    /// it is narrowed to `f32`.
    #[inline]
    pub fn h_advance(&self, glyph: GlyphId) -> f32 {
        self.h_advance_exact(glyph).to_f32()
    }

    /// Returns the exact advance width of `glyph`, in font units.
    ///
    /// `hmtx` contains whole units and variations can add a fraction, so the
    /// result may not be a whole number. A varied advance below zero is
    /// clamped to zero before scaling. The caller decides how to round it.
    #[inline]
    pub fn h_advance_exact(&self, glyph: GlyphId) -> F48Dot16 {
        let mut width = F48Dot16::ZERO;
        self.h_advance_batched(|value| value, core::iter::once((glyph, &mut width)));
        width
    }

    /// Writes the advance width of each glyph to its slot, in order.
    ///
    /// Each width passes through `convert`, so a caller working in another
    /// number type writes into that type directly.
    #[inline]
    pub fn h_advance_batched<'o, V: 'o>(
        &self,
        convert: impl Fn(F48Dot16) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        let raw = self.h_metrics;
        if raw.is_empty() {
            // A Type 1 font states its widths in the charstrings rather than
            // a table, so it reaches here and is measured another way. The
            // test sits inside this branch so that a font with `hmtx`, which
            // is nearly all of them, never makes it.
            match self.font.kind() {
                Kind::Type1(_) => {
                    if let Some(font) = self.font.type1_instance() {
                        return self.h_advance_batched_type1(font, convert, glyphs);
                    }
                }
                Kind::Cff(cff, _) => {
                    return self.h_advance_batched_standalone_cff(&cff, convert, glyphs)
                }
                _ => {}
            }
            // Otherwise the font states no widths at all, and every glyph
            // gets half an em; no location can move that.
            let half_em = F48Dot16::from_i32(self.units_per_em as i32 / 2);
            for (_, out) in glyphs {
                *out = convert(half_em);
            }
            return;
        }
        let coords = self.coords;
        if coords.is_empty() {
            return raw.run(self.num_glyphs, convert, glyphs);
        }
        self.h_advance_batched_varied(raw, coords, convert, glyphs)
    }

    /// Returns the advance height of `glyph`, in font units.
    ///
    /// See [`v_advance_exact`](Self::v_advance_exact) for the height before
    /// it is narrowed to `f32`.
    #[inline]
    pub fn v_advance(&self, glyph: GlyphId) -> f32 {
        self.v_advance_exact(glyph).to_f32()
    }

    /// Returns the exact advance height of `glyph`, in font units.
    ///
    /// The height is non-negative, as stored in the font. A varied advance
    /// below zero is clamped to zero before scaling. A caller whose y axis
    /// points down must negate it.
    #[inline]
    pub fn v_advance_exact(&self, glyph: GlyphId) -> F48Dot16 {
        let mut height = F48Dot16::ZERO;
        self.v_advance_batched(|value| value, core::iter::once((glyph, &mut height)));
        height
    }

    /// Writes the advance height of each glyph to its slot, in order.
    ///
    /// Each height passes through `convert`, so a caller working in another
    /// number type writes into that type directly.
    #[inline]
    pub fn v_advance_batched<'o, V: 'o>(
        &self,
        convert: impl Fn(F48Dot16) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        let raw = self.font.v_metric_records();
        if raw.is_empty() {
            // Such a font stacks its glyphs by the line it lays horizontal
            // text on, so every glyph advances the same and no location
            // moves it.
            let height = self.line_height();
            for (_, out) in glyphs {
                *out = convert(height);
            }
            return;
        }
        let coords = self.coords;
        if coords.is_empty() {
            return raw.run(self.num_glyphs, convert, glyphs);
        }
        self.v_advance_batched_varied(raw, coords, convert, glyphs)
    }

    /// Returns the y coordinate of `glyph`'s vertical origin, in font units.
    ///
    /// The x coordinate is half its advance width. See
    /// [`v_origin_y_exact`](Self::v_origin_y_exact) for the value before it is
    /// narrowed to `f32`.
    #[inline]
    pub fn v_origin_y(&self, glyph: GlyphId) -> f32 {
        self.v_origin_y_exact(glyph).to_f32()
    }

    /// Returns the exact y coordinate of `glyph`'s vertical origin.
    #[inline]
    pub fn v_origin_y_exact(&self, glyph: GlyphId) -> F48Dot16 {
        let mut origin = F48Dot16::ZERO;
        self.v_origin_y_batched(|value| value, core::iter::once((glyph, &mut origin)));
        origin
    }

    /// Writes the vertical origin of each glyph in font units to its slot.
    ///
    /// `VORG` answers first, followed by the glyf top phantom point. Other
    /// glyphs are centered in the horizontal line by their extents, or
    /// placed on the ascender when extents are unavailable.
    pub fn v_origin_y_batched<'o, V: 'o>(
        &self,
        convert: impl Fn(F48Dot16) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        let mut line = None;
        self.v_origin_y_batched_with(
            |glyph, origin| {
                convert(origin.unwrap_or_else(|| {
                    self.origin_against_ink(glyph, *line.get_or_insert_with(|| self.origin_line()))
                }))
            },
            |glyphs| {
                let line = self.origin_line();
                self.extents_batched(
                    |extents| convert(Self::origin_from_extents(extents, line)),
                    glyphs,
                );
            },
            glyphs,
        );
    }

    /// Returns where `glyph`'s ink sits, in font units.
    ///
    /// An empty glyph has zero extents. `None` means the glyph is unavailable
    /// or its extents could not be read. A bitmap is measured from the font's
    /// largest strike; [`scaled`](Self::scaled) metrics use the strike for
    /// the scale's size.
    ///
    /// See [`extents_exact`](Self::extents_exact) for the values before they
    /// are narrowed to `f32`.
    #[inline]
    pub fn extents(&self, glyph: GlyphId) -> Option<GlyphExtents<f32>> {
        self.extents_exact(glyph).map(|extents| GlyphExtents {
            x_bearing: extents.x_bearing.to_f32(),
            y_bearing: extents.y_bearing.to_f32(),
            width: extents.width.to_f32(),
            height: extents.height.to_f32(),
        })
    }

    /// Returns the exact extents of `glyph` in font units.
    ///
    /// See [`extents_batched`](Self::extents_batched) to compute several
    /// glyphs' extents with one choice of source for the batch.
    #[inline]
    pub fn extents_exact(&self, glyph: GlyphId) -> Option<GlyphExtents<F48Dot16>> {
        let mut extents = None;
        self.extents_batched(|value| value, core::iter::once((glyph, &mut extents)));
        extents
    }

    /// Writes where each glyph's ink sits to its slot, in order.
    ///
    /// Each measurement passes through `convert`, so a caller working in
    /// another number type writes into that type directly.
    ///
    /// The sources are resolved once for the batch, because the available
    /// tables depend on the font rather than on the glyph. Each glyph then
    /// tries them in order until one provides extents.
    pub fn extents_batched<'o, V: 'o>(
        &self,
        convert: impl Fn(Option<GlyphExtents<F48Dot16>>) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        // Nearly every font has one source, its outlines, and a run over
        // them is kept free of the per-glyph dispatch below.
        if let Some(base) = self.only_base_source() {
            return self.extents_batched_one(base, convert, glyphs);
        }
        let sources = ExtentsSources::new(self);
        if let Some(only) = sources.only() {
            return self.extents_batched_one(only, convert, glyphs);
        }
        if sources.is_empty() {
            // A font stating its glyphs in a way none of the order reads.
            for (_, out) in glyphs {
                *out = convert(None);
            }
            return;
        }
        self.extents_batched_mixed(&sources, convert, glyphs);
    }

    /// Returns the base outline source, where it is the only source in the
    /// order that the font has.
    ///
    /// This needs no list of sources: the font's answers about its bitmap,
    /// color and composite tables are kept, and the base is chosen as
    /// [`OutlineGlyphs`] chooses it.
    fn only_base_source(&self) -> Option<ExtentsSource<'a>> {
        let font = self.font;
        if font.has_layered_source_in(self.order) {
            return None;
        }
        if let Some(type1) = font.type1_instance() {
            return self
                .order
                .contains(GlyphSource::Type1)
                .then_some(ExtentsSource::Type1(type1));
        }
        self.order.iter().find_map(|source| match source {
            GlyphSource::Glyf => font
                .selected_glyf_loca()
                .map(|(glyf, loca)| ExtentsSource::Glyf(glyf, loca)),
            GlyphSource::Cff2 => font
                .charstrings(OutlineSource::Cff2)
                .map(|cff| ExtentsSource::Cff(cff, OutlineSource::Cff2)),
            GlyphSource::Cff => font
                .charstrings(OutlineSource::Cff)
                .map(|cff| ExtentsSource::Cff(cff, OutlineSource::Cff)),
            _ => None,
        })
    }

    /// Measures every glyph from the one source that answers for them all.
    fn extents_batched_one<'o, V: 'o>(
        &self,
        source: ExtentsSource<'_>,
        convert: impl Fn(Option<GlyphExtents<F48Dot16>>) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        match source {
            ExtentsSource::Glyf(glyf, loca) => {
                if self.coords.is_empty() {
                    for (glyph, out) in glyphs {
                        *out = convert(self.stated_glyf_extents(glyf, loca, glyph));
                    }
                } else {
                    self.extents_batched_glyf_varied(convert, glyphs);
                }
            }
            ExtentsSource::Cff(cff, source) => {
                self.extents_batched_cff(cff, source, convert, glyphs)
            }
            ExtentsSource::Type1(font) => self.extents_batched_type1(font, convert, glyphs),
            source => {
                for (glyph, out) in glyphs {
                    *out = convert(self.extents_from_source(&source, glyph));
                }
            }
        }
    }

    /// Returns the bounding box in a `glyf` glyph's header, with its bearing
    /// from `hmtx`.
    ///
    /// The glyph is not loaded. The header contains the box at the default
    /// location, so this is used only at that location.
    fn stated_glyf_extents(
        &self,
        glyf: &Glyf<'_>,
        loca: &Loca<'_>,
        glyph: GlyphId,
    ) -> Option<GlyphExtents<F48Dot16>> {
        if glyph.to_u32() >= self.num_glyphs {
            return None;
        }
        let outline = loca.get_glyf(glyph, glyf).ok()?;
        let Some(outline) = outline else {
            return Some(GlyphExtents::default());
        };
        if outline.number_of_contours() == 0 {
            return Some(GlyphExtents::default());
        }
        let mut extents = extents_from_corners(
            F48Dot16::from_i32(outline.x_min() as i32),
            F48Dot16::from_i32(outline.y_min() as i32),
            F48Dot16::from_i32(outline.x_max() as i32),
            F48Dot16::from_i32(outline.y_max() as i32),
        );
        extents.x_bearing = self
            .h_metrics
            .stated_side_bearing(glyph)
            .unwrap_or(F48Dot16::ZERO);
        Some(extents)
    }

    /// Returns the bounding box of a charstring.
    fn cff_extents(
        &self,
        cff: &CffFontRef<'_>,
        source: OutlineSource,
        glyph: GlyphId,
    ) -> Option<GlyphExtents<F48Dot16>> {
        let index = cff.subfont_index(glyph)?;
        let subfont = self.font.cff_subfont(source, index)?;
        let bounds = cff.evaluate_extents(&subfont, glyph, self.coords).ok()?;
        Some(bounds.map(extents_from_bounds).unwrap_or_default())
    }

    /// Returns the bounding box of a Type 1 charstring.
    fn type1_extents(
        &self,
        font: Type1Instance<'_>,
        glyph: GlyphId,
    ) -> Option<GlyphExtents<F48Dot16>> {
        let bounds = font.evaluate_extents(glyph).ok()?;
        Some(bounds.map(extents_from_bounds).unwrap_or_default())
    }

    /// Returns the box that `source` measures for `glyph`, or `None` if the
    /// source does not contain the glyph.
    fn extents_from_source(
        &self,
        source: &ExtentsSource<'_>,
        glyph: GlyphId,
    ) -> Option<GlyphExtents<F48Dot16>> {
        match *source {
            ExtentsSource::Bitmap(strikes) => {
                // As in HarfBuzz, the strike selected for the size answers
                // for every glyph, and a glyph it lacks is measured by the
                // next source.
                let bitmap = strikes.select(self.bitmap_ppem)?.get(glyph)?;
                Some(exact_extents(bitmap.extents()))
            }
            ExtentsSource::Color(colors, outlines) => {
                colors.extents(glyph, outlines).map(exact_extents)
            }
            ExtentsSource::Varc(outlines) => {
                // Only the composites: the outlines underneath them answer
                // for themselves, through their own source.
                if !outlines.varc_covers(glyph) {
                    return None;
                }
                Some(exact_extents(outlines.get(glyph)?.extents().ok()?))
            }
            ExtentsSource::Glyf(glyf, loca) => {
                if self.coords.is_empty() {
                    return self.stated_glyf_extents(glyf, loca, glyph);
                }
                if glyph.to_u32() >= self.num_glyphs {
                    return None;
                }
                let context = self.font;
                varied_control_box(&context, glyph)
            }
            ExtentsSource::Cff(cff, source) => self.cff_extents(cff, source, glyph),
            ExtentsSource::Type1(font) => self.type1_extents(font, glyph),
        }
    }

    /// Runs [`extents_batched`](Self::extents_batched) over more than one
    /// source.
    ///
    /// Marked `#[inline(never)]` because fonts with bitmaps or color are
    /// uncommon, and the loop over outlines alone should not include this
    /// code.
    #[inline(never)]
    fn extents_batched_mixed<'o, V: 'o>(
        &self,
        sources: &ExtentsSources<'_>,
        convert: impl Fn(Option<GlyphExtents<F48Dot16>>) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        for (glyph, out) in glyphs {
            let extents = if glyph.to_u32() < self.num_glyphs {
                sources
                    .iter()
                    .find_map(|source| self.extents_from_source(source, glyph))
            } else {
                None
            };
            *out = convert(extents);
        }
    }

    /// Returns the line this font's glyphs stack by, where it provides one.
    #[inline]
    pub(crate) fn line_extents(&self) -> Option<LineExtents<F48Dot16>> {
        self.global.h_line().map(|line| line.extents())
    }

    /// Returns the vertical origin of `glyph` from `VORG`, in font units.
    ///
    /// Returns `None` if the font has no `VORG`, as for every font with `glyf`
    /// outlines.
    #[cfg(test)]
    pub(crate) fn stated_v_origin(&self, glyph: GlyphId) -> Option<F48Dot16> {
        let vorg = self.font.vorg()?;
        let origin = F48Dot16::from_i32(vorg.vertical_origin_y(glyph) as i32);
        let delta = if self.coords.is_empty() {
            F48Dot16::ZERO
        } else {
            self.font
                .vvar()
                .and_then(|vvar| {
                    vvar.v_origin_y_delta_with_scalars(glyph, self.coords, self.font.vvar_scalars())
                })
                .unwrap_or(F48Dot16::ZERO)
        };
        Some(origin.saturating_add(delta))
    }

    /// The top side bearing `vmtx` states for `glyph`, before any location.
    ///
    /// `None` for a font that states none and past the last glyph one covers.
    #[cfg(test)]
    pub(crate) fn stated_v_bearing(&self, glyph: GlyphId) -> Option<F48Dot16> {
        self.font.v_metric_records().stated_side_bearing(glyph)
    }

    /// The top phantom point of a glyf glyph with its vertical metrics.
    /// Loading the outline also handles composite `USE_MY_METRICS` and gvar.
    #[cfg(test)]
    pub(crate) fn glyf_v_origin(&self, glyph: GlyphId) -> Option<F48Dot16> {
        if self.font.v_metric_records().is_empty() || self.font.selected_glyf_loca().is_none() {
            return None;
        }
        let context = self.font;
        glyf_v_origin_from_context(
            &context,
            &OutlineScaleF32::new(None, self.units_per_em),
            glyph,
        )
    }

    /// Selects the source of vertical origins once for the whole run.
    ///
    /// `fallback` receives the whole run when neither table answers. A
    /// malformed glyf glyph can still reach `convert` with `None`.
    pub(crate) fn v_origin_y_batched_with<'o, V: 'o, I>(
        &self,
        mut convert: impl FnMut(GlyphId, Option<F48Dot16>) -> V,
        fallback: impl FnOnce(I),
        glyphs: I,
    ) where
        I: Iterator<Item = (GlyphId, &'o mut V)>,
    {
        if let Some(vorg) = self.font.vorg() {
            let vvar = if self.coords.is_empty() {
                None
            } else {
                self.font.vvar()
            };
            let scalars = if vvar.is_some() {
                self.font.vvar_scalars()
            } else {
                &[]
            };
            for (glyph, out) in glyphs {
                let origin = F48Dot16::from_i32(vorg.vertical_origin_y(glyph) as i32);
                let delta = vvar
                    .and_then(|vvar| {
                        vvar.v_origin_y_delta_with_scalars(glyph, self.coords, scalars)
                    })
                    .unwrap_or(F48Dot16::ZERO);
                *out = convert(glyph, Some(origin.saturating_add(delta)));
            }
            return;
        }
        if !self.font.v_metric_records().is_empty() && self.font.selected_glyf_loca().is_some() {
            let context = self.font;
            let scale = OutlineScaleF32::new(None, self.units_per_em);
            for (glyph, out) in glyphs {
                *out = convert(glyph, glyf_v_origin_from_context(&context, &scale, glyph));
            }
            return;
        }
        fallback(glyphs);
    }

    /// Returns `true` where the font provides vertical metrics of its own.
    ///
    /// One that does not stacks its glyphs by the line, which is the only
    /// part of a vertical advance a caller can supply.
    #[inline]
    pub(crate) fn states_vertical_advances(&self) -> bool {
        !self.font.v_metric_records().is_empty()
    }

    pub(crate) fn origin_line(&self) -> LineExtents<F48Dot16> {
        self.line_extents().unwrap_or_else(|| {
            let em = self.units_per_em as f64;
            let ascender = F48Dot16::from_f64(em * 0.8);
            LineExtents {
                ascender,
                descender: ascender - F48Dot16::from_i32(self.units_per_em as i32),
            }
        })
    }

    fn origin_against_ink(&self, glyph: GlyphId, line: LineExtents<F48Dot16>) -> F48Dot16 {
        Self::origin_from_extents(self.extents_exact(glyph), line)
    }

    fn origin_from_extents(
        extents: Option<GlyphExtents<F48Dot16>>,
        line: LineExtents<F48Dot16>,
    ) -> F48Dot16 {
        let Some(extents) = extents else {
            return line.ascender;
        };
        let height = line.ascender - line.descender;
        extents.y_bearing + F48Dot16::from_bits((height - extents.height).to_bits() >> 1)
    }

    /// The varied half of [`extents_batched`](Self::extents_batched).
    ///
    /// A location moves the points, and the box a glyph states is the one it
    /// has at the default location, so the answer comes from loading the
    /// outline. Floating-point deltas are kept until the four bounds are
    /// measured; the scale decides how to round them.
    /// `#[inline(never)]` because that pulls in the loader, whose size no
    /// unvaried measurement should pay for.
    #[inline(never)]
    fn extents_batched_glyf_varied<'o, V: 'o>(
        &self,
        convert: impl Fn(Option<GlyphExtents<F48Dot16>>) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        let context = self.font;
        for (glyph, out) in glyphs {
            *out = convert(
                (glyph.to_u32() < self.num_glyphs)
                    .then(|| varied_control_box(&context, glyph))
                    .flatten(),
            );
        }
    }

    /// The charstring half of [`extents_batched`](Self::extents_batched).
    ///
    /// A charstring states no box, so reading one means running it.
    /// `#[inline(never)]` for the same reason as the varied arm.
    #[inline(never)]
    fn extents_batched_cff<'o, V: 'o>(
        &self,
        cff: &CffFontRef<'_>,
        source: OutlineSource,
        convert: impl Fn(Option<GlyphExtents<F48Dot16>>) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        for (glyph, out) in glyphs {
            *out = convert(self.cff_extents(cff, source, glyph));
        }
    }

    /// The Type 1 half of [`extents_batched`](Self::extents_batched).
    ///
    /// `#[inline(never)]` for the same reason as
    /// [`h_advance_batched_type1`](Self::h_advance_batched_type1).
    #[inline(never)]
    fn extents_batched_type1<'o, V: 'o>(
        &self,
        font: Type1Instance<'_>,
        convert: impl Fn(Option<GlyphExtents<F48Dot16>>) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        for (glyph, out) in glyphs {
            *out = convert(self.type1_extents(font, glyph));
        }
    }

    /// The height a glyph advances where the font has no `vmtx`.
    ///
    /// The extent of the line, without the gap after it, as HarfBuzz and
    /// FreeType both take it. A font that states no line at all measures one
    /// em.
    fn line_height(&self) -> F48Dot16 {
        match self.global.h_line() {
            Some(line) => line.ascender - line.descender,
            None => F48Dot16::from_i32(self.units_per_em as i32),
        }
    }

    /// The varied half of [`v_advance_batched`](Self::v_advance_batched).
    ///
    /// `#[inline(never)]` for the same reason as the horizontal one.
    #[inline(never)]
    fn v_advance_batched_varied<'o, V: 'o>(
        &self,
        raw: &GlyphMetricRecords<'_>,
        coords: &[F2Dot14],
        convert: impl Fn(F48Dot16) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        // Ask the table that answers directly, and stop there if it does.
        if let Some(vvar) = self.font.vvar() {
            let scalars = self.font.vvar_scalars();
            return raw.run_varied(
                self.num_glyphs,
                |gid| {
                    vvar.advance_delta_with_scalars(gid, coords, scalars)
                        .unwrap_or(F48Dot16::ZERO)
                },
                convert,
                glyphs,
            );
        }
        // Without `VVAR`, the phantom points answer. The vertical pair runs
        // downward from the origin, so the height is the top less the
        // bottom, the reverse of the width.
        if let (Some(gvar), Some((glyf, loca))) =
            (self.font.selected_gvar(), self.font.selected_glyf_loca())
        {
            let scalars = self.font.gvar_scalars();
            return raw.run_varied(
                self.num_glyphs,
                |gid| {
                    // A glyph the table says nothing readable about does
                    // not move.
                    gvar.phantom_point_deltas_with_scalars(glyf, loca, coords, scalars, gid)
                        .map_or(F48Dot16::ZERO, |deltas| {
                            (deltas[2].y - deltas[3].y).to_f48dot16()
                        })
                },
                convert,
                glyphs,
            );
        }
        // A location, but nothing stating what it changes.
        raw.run(self.num_glyphs, convert, glyphs)
    }

    /// The varied half of [`h_advance_batched`](Self::h_advance_batched).
    ///
    /// `#[inline(never)]` because reading `gvar` pulls in a large amount of
    /// code, whose size would otherwise be charged to every unvaried
    /// measurement.
    #[inline(never)]
    fn h_advance_batched_varied<'o, V: 'o>(
        &self,
        raw: &GlyphMetricRecords<'_>,
        coords: &[F2Dot14],
        convert: impl Fn(F48Dot16) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        // Ask the table that answers directly, and stop there if it does.
        if let Some(hvar) = self.font.hvar() {
            let scalars = self.font.hvar_scalars();
            return raw.run_varied(
                self.num_glyphs,
                |gid| {
                    hvar.advance_delta_with_scalars(gid, coords, scalars)
                        .unwrap_or(F48Dot16::ZERO)
                },
                convert,
                glyphs,
            );
        }
        // Without `HVAR`, the answer comes from the phantom points on the
        // outline: an advance spans the two horizontal ones, so a change in
        // it is a change in that span.
        if let (Some(gvar), Some((glyf, loca))) =
            (self.font.selected_gvar(), self.font.selected_glyf_loca())
        {
            let scalars = self.font.gvar_scalars();
            return raw.run_varied(
                self.num_glyphs,
                |gid| {
                    // A glyph the table says nothing readable about does
                    // not move.
                    gvar.phantom_point_deltas_with_scalars(glyf, loca, coords, scalars, gid)
                        .map_or(F48Dot16::ZERO, |deltas| {
                            (deltas[1].x - deltas[0].x).to_f48dot16()
                        })
                },
                convert,
                glyphs,
            );
        }
        // A location, but nothing stating what it changes.
        raw.run(self.num_glyphs, convert, glyphs)
    }

    /// Reads widths from standalone CFF charstrings.
    #[inline(never)]
    fn h_advance_batched_standalone_cff<'o, V: 'o>(
        &self,
        cff: &CffFontRef<'_>,
        convert: impl Fn(F48Dot16) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        for (glyph, out) in glyphs {
            let width = (glyph.to_u32() < self.num_glyphs)
                .then(|| cff.subfont_index(glyph))
                .flatten()
                .and_then(|index| self.font.cff_subfont(OutlineSource::Cff, index))
                .and_then(|subfont| {
                    cff.evaluate_width(&subfont, glyph, self.coords)
                        .ok()
                        .flatten()
                })
                .map(|width| width.to_f48dot16())
                .unwrap_or(F48Dot16::ZERO)
                .max(F48Dot16::ZERO);
            *out = convert(width);
        }
    }

    /// The Type 1 half of [`h_advance_batched`](Self::h_advance_batched).
    ///
    /// A Type 1 charstring states its own width, so reading one means
    /// running it. `#[inline(never)]` because that has nothing in common with
    /// reading a table, and no `sfnt` should pay for its presence.
    #[inline(never)]
    fn h_advance_batched_type1<'o, V: 'o>(
        &self,
        font: Type1Instance<'_>,
        convert: impl Fn(F48Dot16) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        /// The width is all that is wanted, so the outline is discarded.
        struct WidthOnly;

        impl CommandSink for WidthOnly {
            fn move_to(&mut self, _x: Fixed, _y: Fixed) {}
            fn line_to(&mut self, _x: Fixed, _y: Fixed) {}
            fn curve_to(
                &mut self,
                _cx0: Fixed,
                _cy0: Fixed,
                _cx1: Fixed,
                _cy1: Fixed,
                _x: Fixed,
                _y: Fixed,
            ) {
            }
            fn close(&mut self) {}
        }

        // A charstring states its width in its own space, which the font
        // matrix maps to design units. No size is applied: that is the
        // caller's to do.
        let transform = font.font().transform(None);
        for (gid, out) in glyphs {
            // A charstring states its width before it draws anything, so one
            // that states none is malformed and measures as nothing.
            let width = font
                .evaluate_charstring(gid, &mut WidthOnly)
                .ok()
                .flatten()
                .map(|width| transform.transform_h_metric(width).to_f48dot16())
                .unwrap_or(F48Dot16::ZERO)
                .max(F48Dot16::ZERO);
            *out = convert(width);
        }
    }
}

impl<'a> GlyphMetricRecords<'a> {
    /// Returns `true` if the table states no metrics.
    #[inline]
    pub(crate) fn is_empty(&self) -> bool {
        self.metrics.is_empty()
    }

    /// Writes the table's value for each glyph.
    ///
    /// A stored advance is a `u16`, so this needs neither the saturating sum
    /// nor the lower bound of [`run_varied`](Self::run_varied).
    #[inline]
    fn run<'o, V: 'o>(
        &self,
        num_glyphs: u32,
        convert: impl Fn(F48Dot16) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        for (gid, out) in glyphs {
            *out = convert(self.stated_advance(num_glyphs, gid));
        }
    }

    /// Writes each glyph's advance with `delta` applied.
    ///
    /// `delta` is settled before the run, leaving only the branches that turn
    /// on the glyph. The sum saturates rather than wrapping, and is clamped
    /// to zero before conversion or scaling.
    ///
    /// Glyphs beyond the end of the font measure zero here, as they do
    /// unvaried.
    #[inline]
    fn run_varied<'o, V: 'o>(
        &self,
        num_glyphs: u32,
        delta: impl Fn(GlyphId) -> F48Dot16,
        convert: impl Fn(F48Dot16) -> V,
        glyphs: impl Iterator<Item = (GlyphId, &'o mut V)>,
    ) {
        for (gid, out) in glyphs {
            // Glyphs beyond the end of the font measure zero, and no
            // delta applies to them. The index map clamps an out of range
            // glyph onto its last entry, so asking would answer with some
            // other glyph's delta.
            let advance = if gid.to_u32() < num_glyphs {
                self.stated_advance(num_glyphs, gid)
                    .saturating_add(delta(gid))
                    .max(F48Dot16::ZERO)
            } else {
                F48Dot16::ZERO
            };
            *out = convert(advance);
        }
    }

    /// Returns the advance the table states for `glyph`, before any location.
    ///
    /// A glyph past the end of the font advances by nothing. The last record
    /// covers every glyph after it, and clamping the index rather than
    /// falling back on a failed lookup keeps that bound provable, so a run
    /// compiles without a branch per glyph.
    ///
    /// `self.metrics` is never empty here: a caller settles that font before
    /// the run starts.
    #[inline]
    pub(crate) fn stated_advance(&self, num_glyphs: u32, glyph: GlyphId) -> F48Dot16 {
        if glyph.to_u32() >= num_glyphs {
            return F48Dot16::ZERO;
        }
        let index = (glyph.to_u32() as usize).min(self.metrics.len().saturating_sub(1));
        match self.metrics.get(index) {
            Some(metric) => F48Dot16::from_i32(metric.advance() as i32),
            None => F48Dot16::ZERO,
        }
    }

    /// Returns the side bearing stated for `glyph`, including the bare
    /// bearings following the last long metric.
    pub(crate) fn stated_side_bearing(&self, glyph: GlyphId) -> Option<F48Dot16> {
        let index = glyph.to_u32() as usize;
        if let Some(metric) = self.metrics.get(index) {
            return Some(F48Dot16::from_i32(metric.side_bearing() as i32));
        }
        let tail = index.checked_sub(self.metrics.len())?;
        Some(F48Dot16::from_i32(self.bearings.get(tail)?.get() as i32))
    }
}

/// A table that can measure a glyph's ink.
#[derive(Clone, Copy)]
enum ExtentsSource<'a> {
    /// A bitmap table. The strike selected for the size measures every glyph.
    Bitmap(BitmapStrikes<'a>),
    /// A `COLR` table of either version.
    /// The outlines provide the clip glyphs.
    Color(ColorGlyphs<'a>, OutlineGlyphs<'a>),
    /// A `VARC` table, used for the glyphs it covers.
    Varc(OutlineGlyphs<'a>),
    Glyf(&'a Glyf<'a>, &'a Loca<'a>),
    Cff(&'a CffFontRef<'a>, OutlineSource),
    Type1(Type1Instance<'a>),
}

/// The sources available in a font, in the order they are tried.
///
/// The three bitmap tables, `COLR` and `VARC` take an entry each, and the
/// four outline sources share one, so six entries always suffice.
struct ExtentsSources<'a> {
    items: [Option<ExtentsSource<'a>>; MAX_EXTENTS_SOURCES],
    len: usize,
}

/// The most entries [`ExtentsSources`] holds.
const MAX_EXTENTS_SOURCES: usize = 6;

impl<'a> ExtentsSources<'a> {
    fn new(metrics: &GlyphMetrics<'a>) -> Self {
        let font = metrics.font;
        let mut sources = Self {
            items: [None; MAX_EXTENTS_SOURCES],
            len: 0,
        };
        // `VARC` composites are drawn over the base the order chooses, so
        // both come from one view of the outlines.
        let outlines = font.outline_glyphs_with(metrics.order);
        let mut base_taken = false;
        for format in metrics.order.iter() {
            let source = match format {
                GlyphSource::Sbix | GlyphSource::Cbdt | GlyphSource::Ebdt => {
                    let bitmaps = font.bitmap_strikes_with(GlyphSourceOrder::new(&[format]));
                    (!bitmaps.is_empty()).then_some(ExtentsSource::Bitmap(bitmaps))
                }
                GlyphSource::Colr => {
                    let colors = font.color_glyphs();
                    // Clips are outlines whatever the order says, so an
                    // order with no outline source clips with the font's
                    // default outlines, as `ColorGlyph::extents` does.
                    let clips = if outlines.is_empty() {
                        font.outline_glyphs()
                    } else {
                        outlines
                    };
                    (!colors.is_empty()).then_some(ExtentsSource::Color(colors, clips))
                }
                GlyphSource::Varc => outlines.has_varc().then_some(ExtentsSource::Varc(outlines)),
                GlyphSource::Glyf | GlyphSource::Cff2 | GlyphSource::Cff | GlyphSource::Type1 => {
                    // Only the base the outlines chose answers, at its own
                    // place in the order.
                    let base = outlines
                        .underlying_source()
                        .filter(|base| !base_taken && GlyphSource::from(*base) == format);
                    let Some(base) = base else {
                        continue;
                    };
                    base_taken = true;
                    match base {
                        OutlineSource::Glyf => font
                            .selected_glyf_loca()
                            .map(|(glyf, loca)| ExtentsSource::Glyf(glyf, loca)),
                        // The outlines found the charstrings, in the tables
                        // or standing alone.
                        OutlineSource::Cff2 | OutlineSource::Cff => {
                            outlines.cff_font().map(|cff| ExtentsSource::Cff(cff, base))
                        }
                        OutlineSource::Type1 => match font.kind() {
                            Kind::Type1(_) => font.type1_instance().map(ExtentsSource::Type1),
                            _ => None,
                        },
                        OutlineSource::Varc => None,
                    }
                }
            };
            if let Some(source) = source {
                sources.items[sources.len] = Some(source);
                sources.len += 1;
            }
        }
        sources
    }

    fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Returns the source if there is exactly one.
    fn only(&self) -> Option<ExtentsSource<'a>> {
        if self.len == 1 {
            self.items[0]
        } else {
            None
        }
    }

    fn iter(&self) -> impl Iterator<Item = &ExtentsSource<'a>> {
        self.items[..self.len].iter().flatten()
    }
}

/// Widens floating-point extents to the fixed-point type the metrics report.
fn exact_extents(extents: GlyphExtents<f32>) -> GlyphExtents<F48Dot16> {
    let exact = |value: f32| F48Dot16::from_f64(value as f64);
    GlyphExtents {
        x_bearing: exact(extents.x_bearing),
        y_bearing: exact(extents.y_bearing),
        width: exact(extents.width),
        height: exact(extents.height),
    }
}

/// Returns the metrics of a font that states none.
pub(crate) fn empty() -> &'static GlyphMetricRecords<'static> {
    static EMPTY: GlyphMetricRecords<'static> = GlyphMetricRecords {
        metrics: &[],
        bearings: &[],
    };
    &EMPTY
}

/// Turns the corners of a box into a bearing and a size.
///
/// The corners can be in either order. A reversed box still has a
/// non-negative size.
fn extents_from_corners(
    x_min: F48Dot16,
    y_min: F48Dot16,
    x_max: F48Dot16,
    y_max: F48Dot16,
) -> GlyphExtents<F48Dot16> {
    GlyphExtents::from(BoundingBox {
        x_min,
        y_min,
        x_max,
        y_max,
    })
}

/// Turns a charstring's box into a bearing and a size.
fn extents_from_bounds(bounds: BoundingBox<Fixed>) -> GlyphExtents<F48Dot16> {
    extents_from_corners(
        bounds.x_min.to_f48dot16(),
        bounds.y_min.to_f48dot16(),
        bounds.x_max.to_f48dot16(),
        bounds.y_max.to_f48dot16(),
    )
}

/// The box a loaded outline's points stay within.
///
/// The control points count, so this is what the glyph states as its own box
/// rather than the tighter box its curves keep to. A scale can round the
/// finished bounds after applying floating-point variation deltas.
fn control_box(points: &[Point<f32>]) -> Option<GlyphExtents<F48Dot16>> {
    let first = points.first()?;
    let (mut x_min, mut y_min) = (first.x, first.y);
    let (mut x_max, mut y_max) = (first.x, first.y);
    for point in &points[1..] {
        x_min = x_min.min(point.x);
        y_min = y_min.min(point.y);
        x_max = x_max.max(point.x);
        y_max = y_max.max(point.y);
    }
    let as_fixed = |value: f32| F48Dot16::from_f64(value as f64);
    Some(extents_from_corners(
        as_fixed(x_min),
        as_fixed(y_min),
        as_fixed(x_max),
        as_fixed(y_max),
    ))
}

/// The box a glyph's points stay within, at the location `tables` states.
///
/// How much scratch space a glyph takes is known only once its plan is read,
/// and it is finished with by the time this returns, so the space comes from
/// [`with_scratch`] rather than an allocation per glyph.
fn varied_control_box<'a>(
    context: &'a dyn OutlineContext<'a>,
    glyph: GlyphId,
) -> Option<GlyphExtents<F48Dot16>> {
    let plan = OutlinePlan::new(context, glyph).ok()?;
    let scale = OutlineScaleF32::new(None, context.units_per_em());
    let lengths = plan.buffer_lengths::<OutlineScaleF32>();
    with_scratch(lengths.packed_len::<OutlineScaleF32>(), |block| {
        let buffers = Buffers::from_bytes(block, &lengths).ok()?;
        let outline = plan.load(context, &scale, buffers, None).ok()?;
        Some(control_box(outline.points()).unwrap_or_default())
    })
}

/// The loaded top phantom point, retaining fractional gvar deltas.
fn glyf_v_origin_from_context<'a>(
    context: &'a dyn OutlineContext<'a>,
    scale: &OutlineScaleF32,
    glyph: GlyphId,
) -> Option<F48Dot16> {
    let plan = OutlinePlan::new(context, glyph).ok()?;
    let lengths = plan.buffer_lengths::<OutlineScaleF32>();
    with_scratch(lengths.packed_len::<OutlineScaleF32>(), |block| {
        let buffers = Buffers::from_bytes(block, &lengths).ok()?;
        let outline = plan.load(context, scale, buffers, None).ok()?;
        Some(F48Dot16::from_f64(outline.phantom_points()[2].y as f64))
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        model::{
            glyph::outline::{ControlBoundsSink, NullSink},
            Blob, NormalizedCoord,
        },
        tables::glyf::outline::{Outline, OutlineTables, PathContourStart, Unscaled},
        FontRef, TableProvider,
    };
    use alloc::{sync::Arc, vec, vec::Vec};
    use std::sync::Mutex;
    use types::Tag;

    const STATIC: &[u8] = font_test_data::TINOS_SUBSET;

    #[test]
    fn extended_metrics_in_the_cached_font_model() {
        use font_test_data::extended::metrics_font;
        for outline in [None, Some(*b"glyf"), Some(*b"GLYF")] {
            for legacy in [false, true] {
                let data = metrics_font(true, legacy, outline);
                let font = Font::new(data, 0).unwrap();
                assert_eq!(font.num_glyphs(), 70002);
                assert_eq!(
                    font.tables().maxp_extended().unwrap().num_glyphs().to_u32(),
                    70002
                );
                assert_eq!(
                    font.tables().hhea_extended().unwrap().number_of_h_metrics(),
                    70000
                );
                assert_eq!(
                    font.tables()
                        .vhea_extended()
                        .unwrap()
                        .number_of_long_ver_metrics(),
                    70000
                );
                let metrics = font.glyph_metrics();
                for gid in [0, 65535, 65536, 69999] {
                    assert_eq!(
                        metrics.h_advance_exact(GlyphId::new(gid)),
                        F48Dot16::from_i32((1000 + gid % 100) as i32)
                    );
                    assert_eq!(
                        metrics.v_advance_exact(GlyphId::new(gid)),
                        F48Dot16::from_i32((1200 + gid % 100) as i32)
                    );
                }
                assert_eq!(
                    metrics.h_advance_exact(GlyphId::new(70001)),
                    F48Dot16::from_i32(1099)
                );
                assert_eq!(
                    metrics.v_advance_exact(GlyphId::new(70001)),
                    F48Dot16::from_i32(1299)
                );
                assert_eq!(metrics.h_advance_exact(GlyphId::new(70002)), F48Dot16::ZERO);
            }
        }
    }

    #[test]
    fn extended_metrics_from_callback_tables_are_cached() {
        let data = font_test_data::extended::metrics_font(true, true, None);
        let asked = Arc::new(Mutex::new(Vec::new()));
        let requests = asked.clone();
        let source: Arc<dyn Fn(Tag) -> Option<Blob> + Send + Sync> = Arc::new(move |tag| {
            requests.lock().unwrap().push(tag);
            let font = FontRef::new(&data).ok()?;
            Some(Blob::from(font.table_data(tag)?.as_bytes().to_vec()))
        });
        let font = Font::new(source, 0).unwrap();
        for _ in 0..2 {
            let metrics = font.glyph_metrics();
            assert_eq!(font.num_glyphs(), 70002);
            assert_eq!(
                metrics.h_advance_exact(GlyphId::new(65536)),
                F48Dot16::from_i32(1036)
            );
            assert_eq!(
                metrics.v_advance_exact(GlyphId::new(65536)),
                F48Dot16::from_i32(1236)
            );
        }
        let asked = asked.lock().unwrap();
        for tag in [b"MAXP", b"HHEA", b"HMTX", b"VHEA", b"VMTX"] {
            assert_eq!(
                asked.iter().filter(|&&seen| seen == Tag::new(tag)).count(),
                1
            );
        }
        for tag in [b"maxp", b"hhea", b"hmtx", b"vhea", b"vmtx"] {
            assert!(!asked.contains(&Tag::new(tag)));
        }
    }

    #[test]
    fn extended_outline_extents_in_the_cached_font_model() {
        for long_loca in [false, true] {
            let data = font_test_data::extended::outlines_font(true, true, true, long_loca);
            let font = Font::new(data, 0).unwrap();
            let metrics = font.glyph_metrics();
            for gid in [1, 65536] {
                let extents = metrics.extents_exact(GlyphId::new(gid)).unwrap();
                assert_eq!(extents.width, F48Dot16::from_i32(80));
                assert_eq!(extents.height, F48Dot16::from_i32(80));
            }
        }
    }

    #[test]
    fn extended_gvar_metrics_and_extents_in_the_cached_font_model() {
        for long_offsets in [false, true] {
            for legacy in [false, true] {
                let data =
                    font_test_data::extended::variable_outlines_font(true, legacy, long_offsets);
                let font = Font::new(data, 0).unwrap();
                assert_eq!(font.tables().selected_gvar().unwrap().glyph_count(), 65537);
                for coord in [0.0, 0.5, 1.0] {
                    let instance = at(&font, coord);
                    let metrics = instance.glyph_metrics();
                    for (gid, delta, shift) in [(65536, 100.0, 40.0), (1, 60.0, 60.0)] {
                        let id = GlyphId::new(gid);
                        assert_eq!(
                            metrics.h_advance_exact(id),
                            F48Dot16::from_f64(1000.0 + delta * coord as f64)
                        );
                        let v_delta = if gid == 65536 { 60.0 } else { 0.0 };
                        assert_eq!(
                            metrics.v_advance_exact(id),
                            F48Dot16::from_f64(1200.0 + v_delta * coord as f64)
                        );
                        let extents = metrics.extents_exact(id).unwrap();
                        assert_eq!(
                            extents.x_bearing,
                            F48Dot16::from_f64(shift * coord as f64),
                            "gid {gid}, coord {coord}"
                        );
                        assert_eq!(extents.width, F48Dot16::from_i32(80));
                        assert_eq!(extents.height, F48Dot16::from_i32(80));
                    }
                }
            }
        }
    }

    /// Has both `HVAR` and `gvar`, so it can answer either way.
    const VAR: &[u8] = font_test_data::VAZIRMATN_VAR;
    /// Eleven glyphs but one long metric, so ten of them are in the tail.
    const TAIL: &[u8] = font_test_data::MATERIAL_SYMBOLS_SUBSET;
    /// Charstring outlines, with no `glyf` to answer ahead of them.
    const CFF: &[u8] = font_test_data::NOTO_SANS_JP_CFF;
    const CFF2: &[u8] = font_test_data::ift::CFF2_FONT;

    #[test]
    fn vertical_origins_in_design_units_match_their_sources_and_batch() {
        for data in [
            font_test_data::VORG,
            font_test_data::MPLUS1CODE_VERTICAL_SUBSET,
            STATIC,
        ] {
            let font = Font::new(data, 0).unwrap();
            let metrics = font.glyph_metrics();
            let glyphs: Vec<_> = (0..font.num_glyphs()).map(GlyphId::new).collect();
            let mut batched = vec![F48Dot16::ZERO; glyphs.len()];
            metrics.v_origin_y_batched(
                |origin| origin,
                glyphs.iter().copied().zip(batched.iter_mut()),
            );
            for (glyph, origin) in glyphs.into_iter().zip(batched) {
                assert_eq!(origin, metrics.v_origin_y_exact(glyph), "glyph {glyph}");
                assert_eq!(origin.to_f32(), metrics.v_origin_y(glyph));
                if let Some(stated) = metrics.stated_v_origin(glyph) {
                    assert_eq!(origin, stated);
                } else if let Some(phantom) = metrics.glyf_v_origin(glyph) {
                    assert_eq!(origin, phantom);
                }
            }
        }
    }

    #[test]
    fn a_width_is_what_hmtx_stores() {
        // A font with a tail, so this covers both halves of `hmtx`.
        let font = Font::new(TAIL, 0).unwrap();
        let direct = FontRef::new(TAIL).unwrap();
        let hmtx = direct.hmtx().unwrap();
        let num_glyphs = direct.maxp().unwrap().num_glyphs();
        assert!(num_glyphs > 1);
        for gid in 0..num_glyphs as u32 {
            let expected = hmtx.advance(GlyphId::new(gid)).unwrap();
            assert_eq!(
                font.glyph_metrics().h_advance_exact(GlyphId::new(gid)),
                F48Dot16::from_i32(expected as i32)
            );
        }
    }

    #[test]
    fn a_glyph_past_the_end_advances_by_nothing() {
        // Deliberately unlike `Hmtx::advance`, which clamps to the last long
        // metric and so reports a width for a glyph the font does not have.
        // HarfBuzz returns zero once a font has metrics at all, and a shaper
        // handed a bad glyph id is better served by nothing than by whatever
        // the last real glyph happened to measure.
        let font = Font::new(STATIC, 0).unwrap();
        let direct = FontRef::new(STATIC).unwrap();
        let past = GlyphId::new(direct.maxp().unwrap().num_glyphs() as u32);

        assert_eq!(font.glyph_metrics().h_advance_exact(past), F48Dot16::ZERO);
        assert!(direct.hmtx().unwrap().advance(past).unwrap() > 0);
        assert_eq!(
            font.glyph_metrics().h_advance_exact(GlyphId::new(60000)),
            F48Dot16::ZERO
        );
    }

    #[test]
    fn a_font_with_no_widths_gives_every_glyph_half_an_em() {
        let font = Font::new(font_test_data::NAMES_ONLY, 0).unwrap();
        // That font has no `head` either, so the em is 1000.
        assert_eq!(
            font.glyph_metrics().h_advance_exact(GlyphId::new(1)),
            F48Dot16::from_i32(500)
        );
    }

    #[test]
    fn glyphs_past_the_long_metrics_share_the_last_advance() {
        // `hmtx` stores a full metric for the first `numberOfHMetrics`
        // glyphs and a bare side bearing for the rest, which all advance by
        // the last stated width. That tail is how a font records a run of
        // glyphs of equal width, and it is most of this font.
        let font = Font::new(TAIL, 0).unwrap();
        let direct = FontRef::new(TAIL).unwrap();
        let num_glyphs = direct.maxp().unwrap().num_glyphs() as u32;
        let num_long = direct.hhea().unwrap().number_of_h_metrics() as u32;
        assert!(num_long < num_glyphs, "this font has no tail to test");

        let last = font
            .glyph_metrics()
            .h_advance_exact(GlyphId::new(num_long - 1));
        assert!(last > F48Dot16::ZERO);
        for gid in num_long..num_glyphs {
            assert_eq!(
                font.glyph_metrics().h_advance_exact(GlyphId::new(gid)),
                last,
                "glyph {gid} is in the tail and should share the last advance"
            );
        }
        // And the tail stops at the end of the font rather than running on.
        assert_eq!(
            font.glyph_metrics()
                .h_advance_exact(GlyphId::new(num_glyphs)),
            F48Dot16::ZERO
        );
    }

    #[test]
    fn glyphs_beyond_the_font_measure_zero_at_any_location() {
        // The delta set index map clamps an out of range glyph onto its
        // last entry, so without a check these would take the last real
        // glyph's delta and report an advance, and a negative one at that.
        let font = Font::new(VAR, 0).unwrap();
        let num_glyphs = font.num_glyphs();
        let instance = at(&font, -1.0);
        assert!(
            instance
                .glyph_metrics()
                .h_advance_exact(GlyphId::new(num_glyphs - 1))
                != font
                    .glyph_metrics()
                    .h_advance_exact(GlyphId::new(num_glyphs - 1)),
            "the last real glyph should move, or this proves nothing"
        );
        for gid in [num_glyphs, num_glyphs + 1, u32::MAX] {
            let gid = GlyphId::new(gid);
            assert_eq!(
                instance.glyph_metrics().h_advance_exact(gid),
                F48Dot16::ZERO,
                "glyph {gid} is beyond the font"
            );
            assert_eq!(font.glyph_metrics().h_advance_exact(gid), F48Dot16::ZERO);
        }
    }

    /// Has `vmtx`, `VVAR` and one axis, so it can vary vertically.
    const VERT_VAR: &[u8] = font_test_data::ift::CFF2_FONT;
    /// Has `vmtx`, `VVAR` and `gvar`, which disagree about the advance.
    const VERT: &[u8] = font_test_data::MPLUS1CODE_VERTICAL_SUBSET;

    #[test]
    fn a_type1_glyph_stacks_by_the_line() {
        // Type 1 has no `vmtx`, so its glyphs take the line, which such a
        // font derives from its bounding box. Every glyph gets the same
        // height while their widths differ, and no location moves either.
        //
        // FreeType instead gives each Type 1 glyph 12/10 of its own ink
        // height. Nothing here follows it: one rule for every font without
        // vertical metrics is easier to predict than a per-glyph heuristic,
        // and HarfBuzz has no opinion because it does not read Type 1.
        let font = Font::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA, 0).unwrap();
        let line = font.metrics().h_line().unwrap();
        let height = line.ascender - line.descender;
        assert!(height > F48Dot16::ZERO);
        let metrics = font.glyph_metrics();
        let mut widths = std::collections::HashSet::new();
        for gid in (0..font.num_glyphs()).map(GlyphId::new) {
            assert_eq!(metrics.v_advance_exact(gid), height, "glyph {gid}");
            widths.insert(metrics.h_advance_exact(gid).to_bits());
        }
        assert!(
            widths.len() > 1,
            "every glyph is the same width, so this proves nothing"
        );
    }

    #[test]
    fn vertical_advances_come_from_vmtx() {
        let font = Font::new(VERT_VAR, 0).unwrap();
        let direct = FontRef::new(VERT_VAR).unwrap();
        let vmtx = direct.vmtx().unwrap();
        let metrics = font.glyph_metrics();
        let mut checked = 0;
        for gid in (0..font.num_glyphs()).map(GlyphId::new) {
            let Some(stated) = vmtx.advance(gid) else {
                continue;
            };
            assert_eq!(
                metrics.v_advance_exact(gid),
                F48Dot16::from_i32(stated as i32),
                "glyph {gid}"
            );
            checked += 1;
        }
        assert!(checked > 1, "this font states no vertical advances");
    }

    #[test]
    fn vorg_reads_the_vvar_origin_delta_at_a_location() {
        // Pair the VORG fixture with a variable CFF2 font so both tables
        // answer. This VVAR fixture's origin deltas are zero, but requesting
        // the table proves the varied branch is taken.
        let asked = Arc::new(Mutex::new(Vec::new()));
        let calls = asked.clone();
        let source: Arc<dyn Fn(Tag) -> Option<Blob> + Send + Sync> = Arc::new(move |tag| {
            calls.lock().unwrap().push(tag);
            let data = if tag == Tag::new(b"VORG") {
                font_test_data::VORG
            } else {
                VERT_VAR
            };
            let font = FontRef::new(data).ok()?;
            Some(Blob::from(font.table_data(tag)?.as_bytes().to_vec()))
        });
        let font = Font::new(source, 0).unwrap();
        let instance = at(&font, -1.0);
        let gid = GlyphId::new(1);
        let vorg = FontRef::new(font_test_data::VORG).unwrap().vorg().unwrap();
        assert_eq!(
            instance.glyph_metrics().stated_v_origin(gid),
            Some(F48Dot16::from_i32(vorg.vertical_origin_y(gid) as i32))
        );
        assert!(asked_for(&asked, b"VVAR"));
    }

    #[test]
    fn without_vmtx_glyphs_advance_by_the_line() {
        // A horizontal font stacks by the line it lays text on, so every
        // glyph gets the same height whatever its width.
        let font = Font::new(STATIC, 0).unwrap();
        let line = font.metrics().h_line().unwrap();
        let expected = line.ascender - line.descender;
        assert!(expected > F48Dot16::ZERO);
        let metrics = font.glyph_metrics();
        for gid in (0..font.num_glyphs().min(16)).map(GlyphId::new) {
            assert_eq!(metrics.v_advance_exact(gid), expected, "glyph {gid}");
        }
    }

    #[test]
    fn a_location_moves_a_vertical_advance() {
        let font = Font::new(VERT, 0).unwrap();
        let instance = at(&font, -1.0);
        let (varied, plain) = (instance.glyph_metrics(), font.glyph_metrics());
        let moved = (0..font.num_glyphs())
            .map(GlyphId::new)
            .filter(|gid| varied.v_advance_exact(*gid) != plain.v_advance_exact(*gid))
            .count();
        assert!(moved > 0, "no glyph moved at the light end of the axis");
    }

    #[test]
    fn phantom_points_measure_a_glyph_the_same_as_vvar() {
        // Both describe the same font, so where the font is written
        // consistently they agree. Withholding `VVAR` drives the phantom
        // point path rather than reimplementing it alongside.
        let with = Font::new(VERT, 0).unwrap();
        let asked = Arc::new(Mutex::new(Vec::new()));
        let without = callback_font(VERT, asked.clone(), &[b"VVAR"]);
        let (a, b) = (at(&with, -1.0), at(&without, -1.0));
        let (ma, mb) = (a.glyph_metrics(), b.glyph_metrics());
        // The composite is the exception: it carries its component's metrics
        // through `USE_MY_METRICS`, and this font states a delta there that
        // its own `vmtx` has already counted, so the two differ by it.
        let composite = GlyphId::new(2);
        for gid in (0..with.num_glyphs()).map(GlyphId::new) {
            if gid == composite {
                continue;
            }
            assert_eq!(
                ma.v_advance_exact(gid),
                mb.v_advance_exact(gid),
                "glyph {gid}"
            );
        }
        assert_eq!(
            mb.v_advance_exact(composite) - ma.v_advance_exact(composite),
            F48Dot16::from_i32(235)
        );
    }

    #[test]
    fn vvar_answering_keeps_gvar_and_the_outlines_unread() {
        let asked = Arc::new(Mutex::new(Vec::new()));
        let font = callback_font(VERT, asked.clone(), &[]);
        let instance = at(&font, -1.0);
        let metrics = instance.glyph_metrics();
        for gid in (0..font.num_glyphs()).map(GlyphId::new) {
            assert!(metrics.v_advance_exact(gid) > F48Dot16::ZERO);
        }
        assert!(asked_for(&asked, b"VVAR"));
        for unread in [b"gvar", b"glyf", b"loca"] {
            assert!(
                !asked_for(&asked, unread),
                "{:?} was read",
                Tag::new(unread)
            );
        }
    }

    #[test]
    fn a_vertical_batch_agrees_with_one_at_a_time() {
        for (data, coord) in [(VERT_VAR, 0.0), (VERT_VAR, -1.0), (STATIC, 0.0)] {
            let font = Font::new(data, 0).unwrap();
            let instance = at(&font, coord);
            let metrics = instance.glyph_metrics();
            let gids: Vec<_> = (0..16u32).map(GlyphId::new).collect();
            let mut batch = vec![F48Dot16::ZERO; 16];
            metrics.v_advance_batched(|v| v, gids.iter().copied().zip(batch.iter_mut()));
            for (gid, expected) in gids.iter().zip(&batch) {
                assert_eq!(metrics.v_advance_exact(*gid), *expected, "glyph {gid}");
            }
        }
    }

    #[test]
    fn the_tables_are_parsed_once_for_the_font() {
        let font = Font::new(VAR, 0).unwrap();
        let first = font.h_metric_records() as *const GlyphMetricRecords<'_>;
        for _ in 0..8 {
            assert!(core::ptr::eq(
                font.h_metric_records() as *const GlyphMetricRecords<'_>,
                first
            ));
        }
        // And clones share it, since they share one `Arc<FontRepr>`.
        assert!(core::ptr::eq(
            font.clone().h_metric_records() as *const GlyphMetricRecords<'_>,
            first
        ));
    }

    #[test]
    fn a_batch_agrees_with_one_at_a_time() {
        let font = Font::new(STATIC, 0).unwrap();
        let gids: Vec<_> = (0..16u32).map(GlyphId::new).collect();
        let mut batch = vec![F48Dot16::ZERO; 16];
        font.glyph_metrics()
            .h_advance_batched(|v| v, gids.iter().copied().zip(batch.iter_mut()));
        for (gid, expected) in gids.iter().zip(&batch) {
            assert_eq!(font.glyph_metrics().h_advance_exact(*gid), *expected);
        }
    }

    #[test]
    fn a_type1_glyph_is_measured_by_its_charstring() {
        // Type 1 states no metrics table, so a width that is neither zero
        // nor half an em can only have come from running the charstring.
        let font = Font::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFB, 0).unwrap();
        let metrics = font.glyph_metrics();
        let half_em = F48Dot16::from_i32(font.units_per_em() as i32 / 2);
        assert!(font.num_glyphs() > 1);

        let widths: Vec<_> = (0..font.num_glyphs())
            .map(|gid| metrics.h_advance_exact(GlyphId::new(gid)))
            .collect();
        assert!(widths.iter().any(|width| *width > F48Dot16::ZERO));
        assert!(
            widths.iter().any(|width| *width != half_em),
            "every glyph reported the fallback, so no charstring was run"
        );
    }

    /// The fixture with its font matrix replaced, same length so the rest of
    /// the header is untouched. `xx` becomes 2, so every advance doubles.
    fn type1_with_a_stretched_matrix() -> Vec<u8> {
        const FROM: &[u8] = b"/FontMatrix [0.001 0 0 0.001 0 0 ]";
        const TO: &[u8] = b"/FontMatrix [0.002 0 0 0.001 0 0 ]";
        let base = font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA;
        let at = base
            .windows(FROM.len())
            .position(|window| window == FROM)
            .expect("the fixture states the matrix this test rewrites");
        let mut data = base.to_vec();
        data[at..at + TO.len()].copy_from_slice(TO);
        data
    }

    #[test]
    fn a_type1_width_is_mapped_by_the_font_matrix() {
        // The charstring states a width in its own space, which the matrix
        // maps to design units. The fixture states the usual matrix, where
        // that mapping is the identity and a missing one would go unnoticed,
        // so this stretches it: every advance must double.
        let plain = Font::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA, 0).unwrap();
        let stretched = Font::new(type1_with_a_stretched_matrix(), 0).unwrap();
        let (a, b) = (plain.glyph_metrics(), stretched.glyph_metrics());
        assert_eq!(plain.num_glyphs(), stretched.num_glyphs());

        let mut stretched_any = false;
        for gid in (0..plain.num_glyphs()).map(GlyphId::new) {
            let width = a.h_advance_exact(gid);
            assert_eq!(
                b.h_advance_exact(gid),
                width.saturating_add(width),
                "glyph {gid}"
            );
            stretched_any |= width > F48Dot16::ZERO;
        }
        assert!(stretched_any, "no glyph had a width to stretch");
    }

    #[test]
    fn a_type1_advance_is_what_drawing_the_glyph_reports() {
        // `draw` is the reference: it maps the charstring's own space to
        // design units through the font matrix. Note that this font states
        // the usual matrix for an em of 1000, where that mapping is the
        // identity, so this pins agreement with `draw` rather than proving
        // the matrix is applied.
        let data = font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA;
        let font = Font::new(data, 0).unwrap();
        let direct = crate::ps::type1::Type1Font::new(data).unwrap();
        let metrics = font.glyph_metrics();
        for gid in (0..font.num_glyphs()).map(GlyphId::new) {
            let expected = direct.draw(gid, None, &mut NullSink).ok().flatten();
            assert_eq!(
                metrics.h_advance(gid),
                expected.unwrap_or(0.0),
                "glyph {gid}"
            );
        }
    }

    #[test]
    fn the_narrowed_advance_agrees_with_the_exact_one() {
        for data in [STATIC, TAIL, VAR] {
            let font = Font::new(data, 0).unwrap();
            let metrics = font.glyph_metrics();
            for gid in (0..font.num_glyphs()).map(GlyphId::new) {
                assert_eq!(
                    metrics.h_advance(gid),
                    metrics.h_advance_exact(gid).to_f32()
                );
            }
        }
    }

    #[test]
    fn the_conversion_decides_what_a_caller_gets_back() {
        let font = Font::new(STATIC, 0).unwrap();
        let gids = [GlyphId::new(1), GlyphId::new(2)];
        let mut exact = [F48Dot16::ZERO; 2];
        let mut floats = [0.0f32; 2];
        font.glyph_metrics()
            .h_advance_batched(|v| v, gids.iter().copied().zip(exact.iter_mut()));
        font.glyph_metrics()
            .h_advance_batched(|v| v.to_f32(), gids.iter().copied().zip(floats.iter_mut()));
        for i in 0..2 {
            assert_eq!(floats[i], exact[i].to_f32());
        }
    }

    #[test]
    fn a_box_is_the_one_the_glyph_states() {
        // Nothing has to be loaded at the default location: the glyph header
        // carries the box.
        let font = Font::new(STATIC, 0).unwrap();
        let metrics = font.glyph_metrics();
        let direct = FontRef::new(STATIC).unwrap();
        let (glyf, loca, hmtx) = (
            direct.glyf().unwrap(),
            direct.loca(None).unwrap(),
            direct.hmtx().unwrap(),
        );
        let mut drawn = 0;
        let mut empty = 0;
        for gid in (0..font.num_glyphs()).map(GlyphId::new) {
            match loca.get_glyf(gid, &glyf).unwrap() {
                Some(glyph) => {
                    if glyph.number_of_contours() == 0 {
                        assert_eq!(
                            metrics.extents_exact(gid),
                            Some(GlyphExtents::default()),
                            "glyph {gid}"
                        );
                        empty += 1;
                        continue;
                    }
                    let unit = |value: i16| F48Dot16::from_i32(value as i32);
                    assert_eq!(
                        metrics.extents_exact(gid),
                        Some(GlyphExtents {
                            x_bearing: hmtx.side_bearing(gid).map(unit).unwrap_or(F48Dot16::ZERO),
                            y_bearing: unit(glyph.y_max()),
                            width: unit(glyph.x_max()) - unit(glyph.x_min()),
                            height: unit(glyph.y_max()) - unit(glyph.y_min()),
                        }),
                        "glyph {gid}"
                    );
                    drawn += 1;
                }
                // A valid empty glyph has a zero box.
                None => {
                    assert_eq!(
                        metrics.extents_exact(gid),
                        Some(GlyphExtents::default()),
                        "glyph {gid}"
                    );
                    empty += 1;
                }
            }
        }
        assert!(drawn > 0 && empty > 0, "{drawn} drawn, {empty} empty");
    }

    #[test]
    fn glyf_ink_starts_at_the_stated_left_side_bearing() {
        let direct = FontRef::new(STATIC).unwrap();
        let loca = direct.loca(None).unwrap();
        let glyf = direct.glyf().unwrap();
        let gid = (0..direct.hmtx().unwrap().h_metrics().len() as u32)
            .map(GlyphId::new)
            .find(|gid| {
                loca.get_glyf(*gid, &glyf)
                    .ok()
                    .flatten()
                    .is_some_and(|glyph| glyph.number_of_contours() > 0)
            })
            .unwrap();
        let outline = loca.get_glyf(gid, &glyf).unwrap().unwrap();
        let bearing = outline.x_min() + 17;
        let source: Arc<dyn Fn(Tag) -> Option<Blob> + Send + Sync> = Arc::new(move |tag| {
            let font = FontRef::new(STATIC).ok()?;
            let mut data = font.table_data(tag)?.as_bytes().to_vec();
            if tag == Tag::new(b"hmtx") {
                let offset = gid.to_u32() as usize * 4 + 2;
                data[offset..offset + 2].copy_from_slice(&bearing.to_be_bytes());
            }
            Some(Blob::from(data))
        });
        let font = Font::new(source, 0).unwrap();
        assert_ne!(bearing, outline.x_min());
        let tables = OutlineTables::new(&font.tables()).unwrap();
        let mut outline = Outline::<Unscaled>::new();
        let loaded = outline.load(&tables, gid).unwrap();
        let points = loaded.points();
        let x_min = points.iter().map(|point| point.x).min().unwrap();
        let x_max = points.iter().map(|point| point.x).max().unwrap();
        let y_min = points.iter().map(|point| point.y).min().unwrap();
        let y_max = points.iter().map(|point| point.y).max().unwrap();
        let unit = F48Dot16::from_i32;
        assert_eq!(
            font.glyph_metrics().extents_exact(gid),
            Some(GlyphExtents {
                x_bearing: unit(x_min),
                y_bearing: unit(y_max),
                width: unit(x_max - x_min),
                height: unit(y_max - y_min),
            })
        );
    }

    #[test]
    fn glyf_bearing_uses_hmtx_tail_then_falls_back_to_zero() {
        let direct = FontRef::new(TAIL).unwrap();
        let loca = direct.loca(None).unwrap();
        let glyf = direct.glyf().unwrap();
        let hmtx = direct.hmtx().unwrap();
        let long_count = hmtx.h_metrics().len();
        let gid = (long_count as u32..direct.maxp().unwrap().num_glyphs() as u32)
            .map(GlyphId::new)
            .find(|gid| loca.get_glyf(*gid, &glyf).ok().flatten().is_some())
            .unwrap();
        let font = Font::new(TAIL, 0).unwrap();
        assert_eq!(
            font.glyph_metrics().extents_exact(gid).unwrap().x_bearing,
            F48Dot16::from_i32(hmtx.side_bearing(gid).unwrap() as i32)
        );

        let source: Arc<dyn Fn(Tag) -> Option<Blob> + Send + Sync> = Arc::new(move |tag| {
            let font = FontRef::new(TAIL).ok()?;
            let mut data = font.table_data(tag)?.as_bytes().to_vec();
            if tag == Tag::new(b"hmtx") {
                data.truncate(long_count * 4);
            }
            Some(Blob::from(data))
        });
        let shortened = Font::new(source, 0).unwrap();
        assert_eq!(
            shortened
                .glyph_metrics()
                .extents_exact(gid)
                .unwrap()
                .x_bearing,
            F48Dot16::ZERO
        );
        let tables = OutlineTables::new(&shortened.tables()).unwrap();
        let mut outline = Outline::<Unscaled>::new();
        let loaded = outline.load(&tables, gid).unwrap();
        assert_eq!(loaded.points().iter().map(|point| point.x).min(), Some(0));
    }

    #[test]
    fn a_varied_box_holds_the_outline_it_describes() {
        // The stated box is the one the glyph has at the default location, so
        // a location away from it is measured from the loaded points. Drawing
        // those points reaches the same box by another route, which is what
        // catches a phantom point counted as ink.
        let font = Font::new(VAR, 0).unwrap();
        let instance = at(&font, -1.0);
        let metrics = instance.glyph_metrics();
        let direct = FontRef::new(VAR).unwrap();
        let tables = OutlineTables::new(&direct)
            .unwrap()
            .at(instance.normalized_coords(), &[]);
        let mut outline = Outline::<OutlineScaleF32>::new();
        let mut checked = 0;
        for gid in (0..font.num_glyphs()).map(GlyphId::new) {
            let Ok(loaded) = outline.load(&tables, gid, None) else {
                continue;
            };
            let mut pen = ControlBoundsSink::new();
            let _ = loaded.to_path(PathContourStart::ScanBackward, &mut pen);
            let expected = pen
                .bounding_box()
                .map(|b| {
                    extents_from_corners(
                        F48Dot16::from_f64(b.x_min as f64),
                        F48Dot16::from_f64(b.y_min as f64),
                        F48Dot16::from_f64(b.x_max as f64),
                        F48Dot16::from_f64(b.y_max as f64),
                    )
                })
                .or(Some(GlyphExtents::default()));
            assert_eq!(metrics.extents_exact(gid), expected, "glyph {gid}");
            checked += expected.is_some() as u32;
        }
        assert!(checked > 0);
    }

    #[test]
    fn a_location_moves_the_box() {
        let font = Font::new(VAR, 0).unwrap();
        let instance = at(&font, -1.0);
        let (varied, plain) = (instance.glyph_metrics(), font.glyph_metrics());
        let moved = (0..font.num_glyphs())
            .map(GlyphId::new)
            .filter(|gid| varied.extents_exact(*gid) != plain.extents_exact(*gid))
            .count();
        assert!(moved > 0, "no glyph ink moved at the end of the axis");
    }

    #[test]
    fn a_charstring_box_is_the_box_it_draws_within() {
        // A charstring states no box, so the two have to agree by running it
        // the same way twice: once collecting bounds, once drawing.
        for data in [CFF, CFF2] {
            let font = Font::new(data, 0).unwrap();
            let metrics = font.glyph_metrics();
            let direct = FontRef::new(data).unwrap();
            let cff = font
                .charstrings(OutlineSource::Cff2)
                .or_else(|| font.charstrings(OutlineSource::Cff))
                .expect("no charstrings");
            assert!(direct.glyf().is_err(), "glyf would answer first");
            let mut checked = 0;
            for gid in (0..font.num_glyphs()).map(GlyphId::new) {
                let subfont = cff
                    .subfont(cff.subfont_index(gid).unwrap(), &[])
                    .expect("no subfont");
                let mut pen = ControlBoundsSink::new();
                cff.draw(&subfont, gid, &[], None, &mut pen).unwrap();
                let expected = pen
                    .bounding_box()
                    .map(|b| GlyphExtents {
                        x_bearing: F48Dot16::from_f64(b.x_min as f64),
                        y_bearing: F48Dot16::from_f64(b.y_max as f64),
                        width: F48Dot16::from_f64((b.x_max - b.x_min) as f64),
                        height: F48Dot16::from_f64((b.y_max - b.y_min) as f64),
                    })
                    .or(Some(GlyphExtents::default()));
                assert_eq!(metrics.extents_exact(gid), expected, "glyph {gid}");
                checked += expected.is_some() as u32;
            }
            assert!(checked > 0);
        }
    }

    #[test]
    fn a_type1_box_is_the_box_it_draws_within() {
        let data = font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA;
        let font = Font::new(data, 0).unwrap();
        let metrics = font.glyph_metrics();
        let direct = crate::ps::type1::Type1Font::new(data).unwrap();
        let mut checked = 0;
        for gid in (0..font.num_glyphs()).map(GlyphId::new) {
            let mut pen = ControlBoundsSink::new();
            direct.draw(gid, None, &mut pen).unwrap();
            let expected = pen
                .bounding_box()
                .map(|b| GlyphExtents {
                    x_bearing: F48Dot16::from_f64(b.x_min as f64),
                    y_bearing: F48Dot16::from_f64(b.y_max as f64),
                    width: F48Dot16::from_f64((b.x_max - b.x_min) as f64),
                    height: F48Dot16::from_f64((b.y_max - b.y_min) as f64),
                })
                .or(Some(GlyphExtents::default()));
            assert_eq!(metrics.extents_exact(gid), expected, "glyph {gid}");
            checked += expected.is_some() as u32;
        }
        assert!(checked > 0);
    }

    #[test]
    fn every_source_measures_a_run_as_it_measures_one() {
        // Each source keeps something across a run: buffers, a subfont, a
        // table read once. None of it may change an answer.
        for (data, coord) in [(STATIC, 0.0), (VAR, -1.0), (CFF, 0.0), (CFF2, 0.0)] {
            let font = Font::new(data, 0).unwrap();
            let instance = if coord == 0.0 {
                font.clone()
            } else {
                at(&font, coord)
            };
            let metrics = instance.glyph_metrics();
            let glyphs: Vec<_> = (0..font.num_glyphs()).map(GlyphId::new).collect();
            let mut batched = vec![None; glyphs.len()];
            metrics.extents_batched(
                |extents| extents,
                glyphs.iter().copied().zip(batched.iter_mut()),
            );
            for (gid, batched) in glyphs.iter().copied().zip(batched) {
                assert_eq!(metrics.extents_exact(gid), batched, "glyph {gid}");
            }
        }
    }

    /// Converts a box in font units the same way the sources do.
    fn extents_of(bounds: BoundingBox<f32>) -> GlyphExtents<F48Dot16> {
        exact_extents(bounds.into())
    }

    #[test]
    fn bitmaps_answer_before_outlines_in_the_default_order() {
        let font = Font::new(font_test_data::CBDT, 0).unwrap();
        let gid = GlyphId::new(2);
        let bitmaps = font.bitmap_strikes();
        let from_strike = |ppem| exact_extents(bitmaps.select_bitmap(gid, ppem).unwrap().extents());
        // No size picks the largest strike, as HarfBuzz does for a font
        // with no size set.
        let metrics = font.glyph_metrics();
        assert_eq!(metrics.extents_exact(gid), Some(from_strike(None)));
        // A size picks the smallest strike that covers it.
        let at_16 = metrics.with_bitmap_ppem(Some(12.0));
        assert_eq!(at_16.extents_exact(gid), Some(from_strike(Some(12.0))));
        assert_ne!(from_strike(None), from_strike(Some(12.0)));
        // Leaving the bitmaps out of the order measures the outlines.
        let outlines_only = metrics.with_extents_source_order(GlyphSourceOrder::OUTLINES);
        let expected = font
            .outline_glyphs()
            .get(gid)
            .map(|glyph| exact_extents(glyph.extents().unwrap()));
        assert_eq!(outlines_only.extents_exact(gid), expected);
        assert_ne!(outlines_only.extents_exact(gid), metrics.extents_exact(gid));
        // And a glyph the strike lacks falls through to the next source.
        let past = GlyphId::new(font.num_glyphs());
        assert_eq!(metrics.extents_exact(past), None);
    }

    /// Returns the `CBDT` test font with glyph 2 left out of its 16 ppem
    /// strike, so that only the larger strikes have it.
    fn cbdt_without_glyph_2_at_16() -> alloc::vec::Vec<u8> {
        let source = crate::FontRef::new(font_test_data::CBDT).unwrap();
        let cblc_start = source
            .table_directory()
            .table_records()
            .iter()
            .find(|record| record.tag() == crate::types::Tag::new(b"CBLC"))
            .unwrap()
            .offset() as usize;
        let cblc = source.cblc().unwrap();
        let size = cblc
            .bitmap_sizes()
            .iter()
            .find(|size| size.ppem_x() == 16)
            .unwrap();
        let mut data = font_test_data::CBDT.to_vec();
        let list = cblc_start + size.index_subtable_list_offset() as usize;
        for index in 0..size.number_of_index_subtables() as usize {
            let record = list + index * 8;
            let read = |at: usize| u16::from_be_bytes([data[at], data[at + 1]]);
            let (first, last) = (read(record), read(record + 2));
            if (first..=last).contains(&2) {
                // End the range before the glyph, or start it after.
                let (at, value) = if first < 2 {
                    (record + 2, 1u16)
                } else {
                    (record, 3u16)
                };
                data[at..at + 2].copy_from_slice(&value.to_be_bytes());
            }
        }
        data
    }

    // A glyph missing from the strike for the size is measured by the next
    // source, as in HarfBuzz, though drawing still finds a larger strike.
    #[test]
    fn a_bitmap_the_selected_strike_lacks_is_measured_by_the_next_source() {
        let font = Font::new(cbdt_without_glyph_2_at_16(), 0).unwrap();
        let gid = GlyphId::new(2);
        let strikes = font.bitmap_strikes();
        let selected = strikes.select(Some(12.0)).unwrap();
        assert_eq!(selected.ppem(), 16);
        assert!(selected.get(gid).is_none());
        let larger = strikes.select_bitmap(gid, Some(12.0)).unwrap();
        assert_eq!(larger.ppem(), 64);
        let larger = Some(exact_extents(larger.extents()));
        // The font has no outline for the glyph, so nothing measures it.
        let outline = font
            .outline_glyphs()
            .get(gid)
            .and_then(|glyph| glyph.extents().ok().map(exact_extents));
        let at_12 = font.glyph_metrics().with_bitmap_ppem(Some(12.0));
        assert_eq!(at_12.extents_exact(gid), outline);
        assert_ne!(at_12.extents_exact(gid), larger);
        // At 64 ppem the selected strike has it.
        let at_64 = font.glyph_metrics().with_bitmap_ppem(Some(64.0));
        assert_eq!(at_64.extents_exact(gid), larger);
    }

    #[test]
    fn a_color_glyph_measures_by_its_clip_box_or_its_paint() {
        use crate::model::glyph::color::test_glyph_defs::{CLIPBOX, COLORED_CIRCLES_V0};
        let font = Font::new(font_test_data::COLRV0V1_VARIABLE, 0).unwrap();
        let metrics = font.glyph_metrics();
        let colors = font.color_glyphs();
        let map = |ch: char| font.charmap().map_unicode(ch).unwrap();
        // A glyph with a clip box is measured by it.
        let clipped = map(CLIPBOX[0]);
        let clip_box = colors.get(clipped).unwrap().clip_box().unwrap();
        assert_eq!(metrics.extents_exact(clipped), Some(extents_of(clip_box)));
        // One without is measured by what it paints, which here is a set of
        // layered outlines and so the union of their boxes.
        let painted = map(COLORED_CIRCLES_V0[0]);
        let glyph = colors.get(painted).unwrap();
        assert!(glyph.clip_box().is_none());
        assert_eq!(
            metrics.extents_exact(painted),
            Some(exact_extents(glyph.extents().unwrap()))
        );
        // The base glyph of a color glyph is usually empty, so taking color
        // out of the order measures something else.
        let outlines_only = metrics.with_extents_source_order(GlyphSourceOrder::OUTLINES);
        assert_ne!(
            outlines_only.extents_exact(clipped),
            metrics.extents_exact(clipped)
        );
        // A glyph the table does not paint is measured by its outline.
        let plain = GlyphId::new(1);
        assert!(colors.get(plain).is_none());
        assert_eq!(
            metrics.extents_exact(plain),
            outlines_only.extents_exact(plain)
        );
    }

    // HarfBuzz measures a version 0 table's layered glyphs too.
    // A color-only order still clips with outlines, so a painted glyph
    // measures as it does on its own.
    #[test]
    fn a_color_only_order_keeps_the_clips() {
        let font = Font::new(font_test_data::COLRV0V1_VARIABLE, 0).unwrap();
        let metrics = font
            .glyph_metrics()
            .with_extents_source_order(GlyphSourceOrder::COLOR);
        let mut measured = 0;
        for gid in (0..font.num_glyphs()).map(GlyphId::new) {
            let Some(color) = font.color_glyphs().get(gid) else {
                continue;
            };
            let Ok(direct) = color.extents() else {
                continue;
            };
            if color.clip_box().is_some() || direct.width == 0.0 || direct.height == 0.0 {
                continue;
            }
            assert_eq!(metrics.extents(gid), Some(direct), "glyph {gid}");
            measured += 1;
        }
        assert!(measured > 0);
    }

    #[test]
    fn a_version_0_color_glyph_measures_by_its_layers() {
        use crate::model::glyph::color::test_glyph_defs::COLORED_CIRCLES_V0;
        let mut data = font_test_data::COLRV0V1.to_vec();
        let colr = crate::FontRef::new(&data)
            .unwrap()
            .table_directory()
            .table_records()
            .iter()
            .find(|record| record.tag() == Tag::new(b"COLR"))
            .unwrap()
            .offset() as usize;
        // A version 0 reader stops before the version 1 fields, so this is a
        // valid version 0 table holding the same layer records.
        data[colr..colr + 2].copy_from_slice(&0u16.to_be_bytes());
        let font = Font::new(data, 0).unwrap();
        let colors = font.color_glyphs();
        assert_eq!(colors.version(), Some(0));
        let metrics = font.glyph_metrics();
        let outlines_only = metrics.with_extents_source_order(GlyphSourceOrder::OUTLINES);
        let glyph = font.charmap().map_unicode(COLORED_CIRCLES_V0[0]).unwrap();
        let layers = exact_extents(colors.get(glyph).unwrap().extents().unwrap());
        assert_eq!(metrics.extents_exact(glyph), Some(layers));
        assert_ne!(outlines_only.extents_exact(glyph), Some(layers));
        // A glyph without layers is measured by its outline.
        let plain = GlyphId::new(1);
        assert!(colors.get(plain).is_none());
        assert_eq!(
            metrics.extents_exact(plain),
            outlines_only.extents_exact(plain)
        );
    }

    #[test]
    fn a_color_glyph_at_a_location_moves_its_clip_box() {
        use crate::model::glyph::color::test_glyph_defs::CLIPBOX;
        let font = Font::new(font_test_data::COLRV0V1_VARIABLE, 0).unwrap();
        let gid = font.charmap().map_unicode(CLIPBOX[0]).unwrap();
        let moved = font
            .instance_builder()
            .variations([("CLXI", 200.0)])
            .build();
        let (at_default, at_moved) = (
            font.glyph_metrics().extents_exact(gid).unwrap(),
            moved.glyph_metrics().extents_exact(gid).unwrap(),
        );
        assert_ne!(at_default.x_bearing, at_moved.x_bearing);
        // The clip box moves by a fraction of a unit, and the extents by the
        // whole units HarfBuzz rounds each delta to.
        let clip_box = moved.color_glyphs().get(gid).unwrap().clip_box().unwrap();
        assert_ne!(clip_box.x_min, clip_box.x_min.round());
        let rounded = BoundingBox {
            x_min: clip_box.x_min.round(),
            y_min: clip_box.y_min.round(),
            x_max: clip_box.x_max.round(),
            y_max: clip_box.y_max.round(),
        };
        assert_eq!(at_moved, extents_of(rounded));
    }

    #[test]
    fn a_varc_composite_measures_by_what_it_draws() {
        let font = Font::new(font_test_data::varc::CJK_6868, 0).unwrap();
        let outlines = font.outline_glyphs();
        assert!(outlines.has_varc(), "the fixture has VARC");
        let metrics = font.glyph_metrics();
        let base_only = metrics.with_extents_source_order(GlyphSourceOrder::BASE_OUTLINES);
        let mut composites = 0;
        for gid in (0..font.num_glyphs()).map(GlyphId::new) {
            let glyph = outlines.get(gid).unwrap();
            let expected = exact_extents(glyph.extents().unwrap());
            if outlines.varc_covers(gid) {
                assert!(matches!(
                    glyph,
                    crate::model::glyph::outline::OutlineGlyph::Varc(_)
                ));
                assert_eq!(metrics.extents_exact(gid), Some(expected), "glyph {gid}");
                composites += 1;
            } else {
                // The base measures it either way.
                assert_eq!(
                    metrics.extents_exact(gid),
                    base_only.extents_exact(gid),
                    "glyph {gid}"
                );
            }
        }
        assert!(composites > 0);
        // Without VARC in the order, a composite is measured by the empty
        // base glyph beneath it.
        let composite = (0..font.num_glyphs())
            .map(GlyphId::new)
            .find(|gid| outlines.varc_covers(*gid))
            .unwrap();
        assert_ne!(
            base_only.extents_exact(composite),
            metrics.extents_exact(composite)
        );
    }

    #[test]
    fn mixed_sources_measure_a_run_as_they_measure_one() {
        for data in [
            font_test_data::CBDT,
            font_test_data::COLRV0V1_VARIABLE,
            font_test_data::varc::CJK_6868,
        ] {
            let font = Font::new(data, 0).unwrap();
            let metrics = font.glyph_metrics();
            let glyphs: Vec<_> = (0..font.num_glyphs()).map(GlyphId::new).collect();
            let mut batched = vec![None; glyphs.len()];
            metrics.extents_batched(
                |extents| extents,
                glyphs.iter().copied().zip(batched.iter_mut()),
            );
            for (gid, batched) in glyphs.iter().copied().zip(batched) {
                assert_eq!(metrics.extents_exact(gid), batched, "glyph {gid}");
            }
        }
    }

    /// Measures `glyph` by trying every source the order finds, as the
    /// general path does.
    fn extents_through_every_source(
        metrics: &GlyphMetrics<'_>,
        glyph: GlyphId,
    ) -> Option<GlyphExtents<F48Dot16>> {
        let sources = ExtentsSources::new(metrics);
        let extents = sources
            .iter()
            .find_map(|source| metrics.extents_from_source(source, glyph));
        extents
    }

    // Skipping the list of sources when only a base outline can answer gives
    // the same extents as trying every source.
    #[test]
    fn the_base_outline_shortcut_matches_every_source() {
        let mut fonts = alloc::vec![
            Font::new(STATIC, 0).unwrap(),
            Font::new(font_test_data::NOTO_SERIF_DISPLAY_TRIMMED, 0).unwrap(),
            Font::new(font_test_data::CBDT, 0).unwrap(),
            Font::new(font_test_data::COLRV0V1_VARIABLE, 0).unwrap(),
            Font::new(font_test_data::varc::CJK_6868, 0).unwrap(),
            Font::new(font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA, 0).unwrap(),
        ];
        let cantarell = Font::new(font_test_data::CANTARELL_VF_TRIMMED, 0).unwrap();
        fonts.push(
            cantarell
                .instance_builder()
                .variations([("wght", 650.0)])
                .build(),
        );
        fonts.push(cantarell);
        let orders = [
            GlyphSourceOrder::DEFAULT,
            GlyphSourceOrder::OUTLINES,
            GlyphSourceOrder::BASE_OUTLINES,
            GlyphSourceOrder::BITMAPS,
            GlyphSourceOrder::COLOR,
            GlyphSourceOrder::new(&[GlyphSource::Cff, GlyphSource::Glyf, GlyphSource::Colr]),
            GlyphSourceOrder::EMPTY,
        ];
        for (index, font) in fonts.iter().enumerate() {
            for order in orders {
                let metrics = font.glyph_metrics().with_extents_source_order(order);
                for glyph in (0..font.num_glyphs() + 1).map(GlyphId::new) {
                    assert_eq!(
                        metrics.extents_exact(glyph),
                        extents_through_every_source(&metrics, glyph),
                        "font {index} order {order:?} glyph {glyph}"
                    );
                }
            }
        }
    }

    // An order without bitmap, color or composite sources probes none of
    // those tables.
    #[test]
    fn an_outline_order_probes_no_other_tables() {
        let font = Font::new(font_test_data::CBDT, 0).unwrap();
        let probed = |font: &Font| font.probed_glyph_sources();
        let base = font
            .glyph_metrics()
            .with_extents_source_order(GlyphSourceOrder::BASE_OUTLINES);
        base.extents_exact(GlyphId::new(1));
        assert_eq!(probed(&font), 0);
        // The default order asks for the bitmaps, and the answer is kept.
        font.glyph_metrics().extents_exact(GlyphId::new(1));
        assert_ne!(probed(&font), 0);
        assert!(font.has_layered_source(GlyphSource::Cbdt));
        assert!(!font.has_layered_source(GlyphSource::Colr));
    }

    #[test]
    fn an_empty_order_measures_nothing() {
        let font = Font::new(STATIC, 0).unwrap();
        let metrics = font
            .glyph_metrics()
            .with_extents_source_order(GlyphSourceOrder::EMPTY);
        assert_eq!(metrics.extents_exact(GlyphId::new(1)), None);
        assert_eq!(metrics.extents_source_order(), GlyphSourceOrder::EMPTY);
    }

    #[test]
    fn a_font_stating_no_outlines_measures_nothing() {
        let font = Font::new(font_test_data::NAMES_ONLY, 0).unwrap();
        let metrics = font.glyph_metrics();
        for gid in (0..8).map(GlyphId::new) {
            assert_eq!(metrics.extents_exact(gid), None);
        }
    }

    #[test]
    fn a_glyph_past_the_font_has_no_extents() {
        for data in [
            STATIC,
            VAR,
            CFF,
            CFF2,
            font_test_data::type1::NOTO_SERIF_REGULAR_SUBSET_PFA,
        ] {
            let font = Font::new(data, 0).unwrap();
            let past = GlyphId::new(font.num_glyphs());
            assert_eq!(font.glyph_metrics().extents_exact(past), None);
            if data == VAR {
                assert_eq!(at(&font, -1.0).glyph_metrics().extents_exact(past), None);
            }
        }
    }

    #[test]
    fn a_reversed_box_still_measures_a_size() {
        // A size is never negative, whichever way round a font states its
        // corners.
        let unit = F48Dot16::from_i32;
        assert_eq!(
            extents_from_corners(unit(70), unit(50), unit(10), unit(-30)),
            GlyphExtents {
                x_bearing: unit(10),
                y_bearing: unit(50),
                width: unit(60),
                height: unit(80),
            }
        );
    }

    fn at(font: &Font, coord: f32) -> Font {
        font.instance_builder()
            .normalized_coords([NormalizedCoord::from_f32(coord)])
            .build()
    }

    /// A font whose tables arrive one at a time, recording what was asked
    /// for and withholding any tag in `hide`.
    fn callback_font(
        data: &'static [u8],
        asked: Arc<Mutex<Vec<Tag>>>,
        hide: &'static [&[u8; 4]],
    ) -> Font {
        let source: Arc<dyn Fn(Tag) -> Option<Blob> + Send + Sync> = Arc::new(move |tag: Tag| {
            asked.lock().unwrap().push(tag);
            if hide.iter().any(|hidden| Tag::new(hidden) == tag) {
                return None;
            }
            let font = FontRef::new(data).ok()?;
            Some(Blob::from(font.table_data(tag)?.as_bytes().to_vec()))
        });
        Font::new(source, 0).unwrap()
    }

    fn asked_for(asked: &Arc<Mutex<Vec<Tag>>>, tag: &[u8; 4]) -> bool {
        asked.lock().unwrap().contains(&Tag::new(tag))
    }

    #[test]
    fn an_instance_varies_what_the_font_does_not() {
        let font = Font::new(VAR, 0).unwrap();
        let instance = at(&font, -1.0);
        let (varied, plain) = (instance.glyph_metrics(), font.glyph_metrics());
        let moved = (0..font.num_glyphs())
            .map(GlyphId::new)
            .any(|gid| varied.h_advance_exact(gid) != plain.h_advance_exact(gid));
        assert!(moved, "no glyph moved at the far end of the axis");
    }

    #[test]
    fn an_advance_keeps_the_fraction_a_location_adds() {
        // The stated width is whole and the delta is not, so a varied advance
        // should carry a fraction that rounding would have thrown away. If
        // nothing here is fractional the exactness is untested.
        let font = Font::new(VAR, 0).unwrap();
        let metrics = at(&font, -0.4);
        let metrics = metrics.glyph_metrics();
        let fractional = (0..font.num_glyphs())
            .map(GlyphId::new)
            .any(|gid| metrics.h_advance_exact(gid).to_bits() & 0xFFFF != 0);
        assert!(fractional, "no advance carried a fraction");
    }

    #[test]
    fn negative_varied_advances_are_clamped_before_scaling() {
        const DATA: &[u8] = font_test_data::NEGATIVE_VAR_ADVANCES;
        for hide in [&[][..], &[b"HVAR", b"VVAR"][..]] {
            let asked = Arc::new(Mutex::new(Vec::new()));
            let font = callback_font(DATA, asked.clone(), hide);
            // A crosses zero; B remains positive, with fractional advances
            // at intermediate locations. The two metric sources agree.
            for (coord, widths, heights) in [
                (0.0, [500.0, 500.0], [1000.0, 1000.0]),
                (0.5, [199.5, 550.5], [399.5, 1100.5]),
                (1.0, [0.0, 601.0], [0.0, 1201.0]),
            ] {
                let instance = at(&font, coord);
                let metrics = instance.glyph_metrics();
                let gids = [GlyphId::new(1), GlyphId::new(2), GlyphId::new(3)];
                let widths = [widths[0], widths[1], 0.0];
                let heights = [heights[0], heights[1], 0.0];
                let mut h_batch = [F48Dot16::ZERO; 3];
                let mut v_batch = [F48Dot16::ZERO; 3];
                metrics.h_advance_batched(|v| v, gids.into_iter().zip(h_batch.iter_mut()));
                metrics.v_advance_batched(|v| v, gids.into_iter().zip(v_batch.iter_mut()));
                for (i, gid) in gids.into_iter().enumerate() {
                    assert_eq!(metrics.h_advance_exact(gid), F48Dot16::from_f64(widths[i]));
                    assert_eq!(metrics.v_advance_exact(gid), F48Dot16::from_f64(heights[i]));
                    assert_eq!(h_batch[i], metrics.h_advance_exact(gid));
                    assert_eq!(v_batch[i], metrics.v_advance_exact(gid));
                    assert_eq!(metrics.h_advance(gid), widths[i] as f32);
                    assert_eq!(metrics.v_advance(gid), heights[i] as f32);
                }
                for factor in [-2.0, -1.0, 0.0, 1.0, 2.0] {
                    let scaled =
                        metrics.scaled(ScaleF32::new(factor * 1000.0, -factor * 1000.0, 1000));
                    let mut h_batch = [0.0; 3];
                    let mut v_batch = [0.0; 3];
                    scaled.h_advance_batched(|v| v, gids.into_iter().zip(h_batch.iter_mut()));
                    scaled.v_advance_batched(|v| v, gids.into_iter().zip(v_batch.iter_mut()));
                    for (i, gid) in gids.into_iter().enumerate() {
                        assert_eq!(scaled.h_advance(gid), widths[i] as f32 * factor);
                        assert_eq!(scaled.v_advance(gid), heights[i] as f32 * -factor);
                        assert_eq!(h_batch[i], scaled.h_advance(gid));
                        assert_eq!(v_batch[i], scaled.v_advance(gid));
                    }
                }
            }
            if !hide.is_empty() {
                assert!(asked_for(&asked, b"gvar"));
            }
        }
    }

    #[test]
    fn a_batch_agrees_with_one_at_a_time_at_a_location() {
        let font = Font::new(VAR, 0).unwrap();
        let instance = at(&font, -0.6);
        let metrics = instance.glyph_metrics();
        let gids: Vec<_> = (0..16u32).map(GlyphId::new).collect();
        let mut batch = vec![F48Dot16::ZERO; 16];
        metrics.h_advance_batched(|v| v, gids.iter().copied().zip(batch.iter_mut()));
        for (gid, expected) in gids.iter().zip(&batch) {
            assert_eq!(metrics.h_advance_exact(*gid), *expected);
        }
    }

    #[test]
    fn gvar_answers_the_same_as_hvar() {
        // The two rungs of the ladder describe the same font, so a font that
        // has both must measure the same either way. Withholding `HVAR`
        // forces the second rung, which exercises the real fallback rather
        // than a reimplementation of it.
        let font = Font::new(VAR, 0).unwrap();
        let asked = Arc::new(Mutex::new(Vec::new()));
        let no_hvar = callback_font(VAR, asked.clone(), &[b"HVAR"]);
        for coord in [-1.0, -0.8, -0.25, 0.75, 1.0] {
            let (with, without) = (at(&font, coord), at(&no_hvar, coord));
            let (with, without) = (with.glyph_metrics(), without.glyph_metrics());
            for gid in (0..font.num_glyphs()).map(GlyphId::new) {
                assert_eq!(
                    with.h_advance_exact(gid),
                    without.h_advance_exact(gid),
                    "glyph {gid} disagrees at {coord}"
                );
            }
        }
        // And it really did take the other path.
        assert!(asked_for(&asked, b"gvar"));
    }

    #[test]
    fn a_default_location_reads_no_variation_table() {
        // The point of the whole arrangement: `gvar` is typically about half
        // a variable font, and on a platform that hands tables over one at a
        // time, asking for it means copying it. Static text must not.
        let asked = Arc::new(Mutex::new(Vec::new()));
        let font = callback_font(VAR, asked.clone(), &[]);
        let metrics = font.glyph_metrics();
        for gid in (0..8).map(GlyphId::new) {
            let _ = metrics.h_advance_exact(gid);
        }
        assert!(!asked_for(&asked, b"gvar"));
        assert!(!asked_for(&asked, b"HVAR"));
        assert!(asked_for(&asked, b"hmtx"), "but it should read hmtx");
    }

    #[test]
    fn an_instance_at_the_default_location_reads_no_variation_table() {
        let asked = Arc::new(Mutex::new(Vec::new()));
        let font = callback_font(VAR, asked.clone(), &[]);
        // All-zero coordinates are the default location, and an instance
        // collapses them to none.
        let instance = at(&font, 0.0);
        assert!(instance.normalized_coords().is_empty());
        let _ = instance.glyph_metrics().h_advance_exact(GlyphId::new(1));
        assert!(!asked_for(&asked, b"gvar"));
        assert!(!asked_for(&asked, b"HVAR"));
    }

    #[test]
    fn hvar_answering_keeps_gvar_and_the_outlines_unread() {
        let asked = Arc::new(Mutex::new(Vec::new()));
        let font = callback_font(VAR, asked.clone(), &[]);
        let _ = at(&font, -0.75)
            .glyph_metrics()
            .h_advance_exact(GlyphId::new(1));
        assert!(asked_for(&asked, b"HVAR"));
        for cold in [b"gvar", b"glyf", b"loca"] {
            assert!(
                !asked_for(&asked, cold),
                "HVAR answered, so {} should never have been copied",
                Tag::new(cold)
            );
        }
    }

    #[test]
    fn taking_the_metrics_reads_hmtx_but_nothing_that_varies() {
        // Every measurement needs `hmtx`, so it is read up front. What a
        // location changes is not: that waits until something asks.
        let asked = Arc::new(Mutex::new(Vec::new()));
        let font = callback_font(VAR, asked.clone(), &[]);
        let instance = at(&font, -0.75);
        let metrics = instance.glyph_metrics();
        assert!(asked_for(&asked, b"hmtx"));
        for cold in [b"HVAR", b"gvar", b"glyf", b"loca"] {
            assert!(!asked_for(&asked, cold), "read {}", Tag::new(cold));
        }
        // And measuring one glyph then reads only what answers.
        let _ = metrics.h_advance_exact(GlyphId::new(1));
        assert!(asked_for(&asked, b"HVAR"));
        assert!(!asked_for(&asked, b"gvar"));
    }
}