font-model 0.1.1

Editable in-memory font model: glyph outlines, cmap formats 4 and 12, metrics, validation, and subsetting
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
//! The font model: [`Font`], [`FontBuilder`], and the operations a subsetter
//! or a writer needs.
//!
//! A [`Font`] is the resolved, owned, editable form of a typeface: it owns its
//! glyph store (each glyph carrying its own metrics and outline), its
//! character map, its naming records, its OS/2 metrics, its kerning, and any
//! layout tables carried through **opaquely** — so a read-modify-write round
//! trip through the model is lossless even for the tables this crate does not
//! interpret.
//!
//! # Invariants
//!
//! [`Font::validate`] is the single gate. It enforces:
//!
//! | Invariant | Error |
//! |---|---|
//! | `units_per_em` in `16..=16384` | [`ModelError::InvalidUnitsPerEm`] |
//! | non-empty glyph has non-zero advance | [`ModelError::ZeroAdvance`] |
//! | contours closed | [`ModelError::UnclosedContour`] |
//! | coordinates finite | [`ModelError::NonFiniteCoordinate`] |
//! | `cmap` strictly increasing | [`ModelError::NonMonotonicCmap`] |
//! | every reference resolves | [`ModelError::UnknownGlyph`] |
//!
//! Each variant names the offending glyph and field.
//!
//! ```
//! use font_model::{CodePointSet, Font, GlyphId, Outline, Path};
//!
//! // A two-glyph font: `.notdef` and 'A', mapped from U+0041.
//! let mut box_outline = Outline::new();
//! let mut p = Path::starting_at(50.0, 0.0);
//! p.line_to(550.0, 0.0);
//! p.line_to(550.0, 700.0);
//! p.line_to(50.0, 700.0);
//! p.close();
//! box_outline.push_contour(p);
//!
//! let mut cmap = font_model::Cmap::new();
//! cmap.subtables_mut().push(font_model::from_entries(&[(0x41, GlyphId::new(1))], 4));
//!
//! let font = Font::builder()
//!     .units_per_em(1000)
//!     .glyph(font_model::Glyph::new(GlyphId::NOTDEF, 600, 50, box_outline.clone()))
//!     .glyph(font_model::Glyph::new(GlyphId::new(1), 600, 50, box_outline))
//!     .cmap(cmap)
//!     .build()
//!     .expect("valid");
//!
//! assert_eq!(font.glyph_for(0x41), Some(GlyphId::new(1)));
//! assert_eq!(font.units_per_em(), 1000);
//! assert!(font.validate().is_ok());
//!
//! // Subsetting keeps `.notdef` plus the requested codepoints.
//! let mut keep = CodePointSet::new();
//! keep.insert(0x41);
//! let sub = font.subset(&keep).expect("subsets");
//! assert_eq!(sub.glyph_count(), 2);
//! assert_eq!(sub.glyph_for(0x41), Some(GlyphId::new(1)));
//! ```

use alloc::string::String;
use alloc::vec::Vec;

use crate::cmap::{self, Cmap, CmapSubtable, CodePointSet};
use crate::error::ModelError;
use crate::glyph::{validate_glyphs, Glyph, GlyphId};
use crate::metrics::FontMetrics;
use crate::name::NameRecord;
use crate::os2::Os2Metrics;
use crate::outline::Bbox;

/// A four-byte table tag.
pub type TableTag = [u8; 4];

/// The `cmap` tag.
pub const TAG_CMAP: TableTag = *b"cmap";
/// The `GSUB` tag.
pub const TAG_GSUB: TableTag = *b"GSUB";
/// The `GPOS` tag.
pub const TAG_GPOS: TableTag = *b"GPOS";
/// The `kern` tag.
pub const TAG_KERN: TableTag = *b"kern";

/// Minimum legal `unitsPerEm`.
pub const MIN_UNITS_PER_EM: u16 = 16;
/// Maximum legal `unitsPerEm`.
pub const MAX_UNITS_PER_EM: u16 = 16_384;

/// The resolved, owned, editable font model.
///
/// ```
/// use font_model::{Font, Glyph, GlyphId, Outline, Path};
///
/// // The smallest valid font: `.notdef` alone.
/// let font = Font::builder()
///     .units_per_em(1000)
///     .glyph(Glyph::new(GlyphId::NOTDEF, 500, 0, Outline::new()))
///     .build()
///     .expect("valid");
/// assert_eq!(font.glyph_count(), 1);
/// assert!(font.cmap().codepoints().is_empty());
/// assert!(font.glyph(GlyphId::new(9)).is_none());
/// ```
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Font {
    units_per_em: u16,
    metrics: FontMetrics,
    glyphs: Vec<Glyph>,
    cmap: Cmap,
    names: Vec<NameRecord>,
    os2: Option<Os2Metrics>,
    kerning: Kerning,
    tables: Vec<(TableTag, Vec<u8>)>,
}

impl Font {
    /// Start building a font.
    #[must_use]
    pub fn builder() -> FontBuilder {
        FontBuilder::new()
    }

    /// A font with nothing in it — invalid, but a usable starting point.
    #[must_use]
    pub fn empty() -> Font {
        Font::default()
    }

    /// Assemble a font from its parts **without** validating.
    ///
    /// This is the escape hatch the load path needs: a parsed font has
    /// already been checked by [`crate::from_sfnt`], and re-deriving the
    /// header here would discard what the file actually said. Prefer
    /// [`Font::builder`] for anything caller-constructed.
    #[must_use]
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn from_parts(
        units_per_em: u16,
        metrics: FontMetrics,
        glyphs: Vec<Glyph>,
        cmap: Cmap,
        names: Vec<NameRecord>,
        os2: Option<Os2Metrics>,
        kerning: Kerning,
        tables: Vec<(TableTag, Vec<u8>)>,
    ) -> Font {
        Font {
            units_per_em,
            metrics,
            glyphs,
            cmap,
            names,
            os2,
            kerning,
            tables,
        }
    }

    /// `unitsPerEm` in font units.
    #[must_use]
    pub const fn units_per_em(&self) -> u16 {
        self.units_per_em
    }

    /// Set `unitsPerEm`.
    pub fn set_units_per_em(&mut self, units_per_em: u16) {
        self.units_per_em = units_per_em;
    }

    /// The font-wide metrics header.
    #[must_use]
    pub const fn metrics(&self) -> &FontMetrics {
        &self.metrics
    }

    /// The font-wide metrics header, mutably.
    pub fn metrics_mut(&mut self) -> &mut FontMetrics {
        &mut self.metrics
    }

    /// Number of glyphs.
    #[must_use]
    pub fn glyph_count(&self) -> usize {
        self.glyphs.len()
    }

    /// The glyph store.
    #[must_use]
    pub fn glyphs(&self) -> &[Glyph] {
        &self.glyphs
    }

    /// The glyph store, mutably — the editor's entry point.
    pub fn glyphs_mut(&mut self) -> &mut Vec<Glyph> {
        &mut self.glyphs
    }

    /// One glyph by id, `None` beyond the store.
    #[must_use]
    pub fn glyph(&self, id: GlyphId) -> Option<&Glyph> {
        self.glyphs.get(id.index())
    }

    /// One glyph by id, mutably, `None` beyond the store.
    pub fn glyph_mut(&mut self, id: GlyphId) -> Option<&mut Glyph> {
        self.glyphs.get_mut(id.index())
    }

    /// `.notdef`'s outline — glyph 0, or `None` for an empty font.
    #[must_use]
    pub fn notdef(&self) -> Option<&Glyph> {
        self.glyph(GlyphId::NOTDEF)
    }

    /// The character map.
    #[must_use]
    pub const fn cmap(&self) -> &Cmap {
        &self.cmap
    }

    /// The character map, mutably.
    pub fn cmap_mut(&mut self) -> &mut Cmap {
        &mut self.cmap
    }

    /// Map a codepoint to a glyph.
    #[must_use]
    pub fn glyph_for(&self, cp: u32) -> Option<GlyphId> {
        self.cmap.lookup(cp)
    }

    /// The mapped codepoints, ascending.
    #[must_use]
    pub fn codepoints(&self) -> CodePointSet {
        self.cmap.codepoints()
    }

    /// The naming records.
    #[must_use]
    pub fn names(&self) -> &[NameRecord] {
        &self.names
    }

    /// The naming records, mutably.
    pub fn names_mut(&mut self) -> &mut Vec<NameRecord> {
        &mut self.names
    }

    /// The OS/2 metrics, when present.
    #[must_use]
    pub const fn os2(&self) -> Option<&Os2Metrics> {
        self.os2.as_ref()
    }

    /// The OS/2 metrics, mutably.
    pub fn os2_mut(&mut self) -> &mut Option<Os2Metrics> {
        &mut self.os2
    }

    /// The kerning pairs.
    #[must_use]
    pub const fn kerning(&self) -> &Kerning {
        &self.kerning
    }

    /// The kerning pairs, mutably.
    pub fn kerning_mut(&mut self) -> &mut Kerning {
        &mut self.kerning
    }

    /// The kerning adjustment between two glyphs, in font units.
    #[must_use]
    pub fn kern(&self, left: GlyphId, right: GlyphId) -> i16 {
        self.kerning.get(left, right)
    }

    /// The opaque tables carried through unchanged: `(tag, bytes)`.
    #[must_use]
    pub fn tables(&self) -> &[(TableTag, Vec<u8>)] {
        &self.tables
    }

    /// The opaque tables, mutably.
    pub fn tables_mut(&mut self) -> &mut Vec<(TableTag, Vec<u8>)> {
        &mut self.tables
    }

    /// One opaque table's bytes by tag, `None` when absent.
    #[must_use]
    pub fn table(&self, tag: TableTag) -> Option<&[u8]> {
        self.tables
            .iter()
            .find(|(t, _)| *t == tag)
            .map(|(_, d)| d.as_slice())
    }

    /// Store an opaque table, replacing any existing entry with that tag.
    pub fn set_table(&mut self, tag: TableTag, data: Vec<u8>) {
        match self.tables.iter_mut().find(|(t, _)| *t == tag) {
            Some(slot) => slot.1 = data,
            None => self.tables.push((tag, data)),
        }
    }

    /// Drop an opaque table, returning whether it was present.
    pub fn remove_table(&mut self, tag: TableTag) -> bool {
        let before = self.tables.len();
        self.tables.retain(|(t, _)| *t != tag);
        self.tables.len() != before
    }

    /// The union of every glyph's bounding box, `None` for a font with no
    /// ink at all.
    #[must_use]
    pub fn bbox(&self) -> Option<Bbox> {
        let mut b = Bbox::empty();
        for g in &self.glyphs {
            if let Some(gb) = g.bbox() {
                b.union(&gb);
            }
        }
        if b.is_empty() {
            None
        } else {
            Some(b)
        }
    }

    /// A `cmap` subtable holding exactly the font's codepoint set.
    ///
    /// What a writer emits: format 4 below the BMP, format 12 above it.
    #[must_use]
    pub fn encoding_subtables(&self) -> Vec<CmapSubtable> {
        let mut bmp: Vec<(u32, GlyphId)> = Vec::new();
        let mut astral: Vec<(u32, GlyphId)> = Vec::new();
        for cp in self.codepoints().iter() {
            if let Some(gid) = self.cmap.lookup(*cp) {
                if *cp <= 0xFFFF {
                    bmp.push((*cp, gid));
                } else {
                    astral.push((*cp, gid));
                }
            }
        }
        let mut out = Vec::new();
        if !bmp.is_empty() {
            out.push(cmap::from_entries(&bmp, 4));
        }
        if !astral.is_empty() {
            out.push(cmap::from_entries(&astral, 12));
        }
        out
    }

    /// Validate every invariant, naming the offending glyph and field.
    ///
    /// # Errors
    /// The first violation found, in this order: `unitsPerEm`, glyph store
    /// density, per-glyph outline and advance, `cmap` monotonicity, `cmap`
    /// references, then kerning references.
    pub fn validate(&self) -> Result<(), ModelError> {
        if !(MIN_UNITS_PER_EM..=MAX_UNITS_PER_EM).contains(&self.units_per_em) {
            return Err(ModelError::InvalidUnitsPerEm {
                units_per_em: self.units_per_em,
            });
        }
        if self.glyphs.is_empty() {
            return Err(ModelError::UnknownGlyph {
                glyph: GlyphId::NOTDEF,
                glyph_count: 0,
            });
        }
        validate_glyphs(&self.glyphs)?;
        self.cmap.validate()?;
        for cp in self.cmap.codepoints().iter() {
            if let Some(gid) = self.cmap.lookup(*cp) {
                if self.glyphs.get(gid.index()).is_none() {
                    return Err(ModelError::UnknownGlyph {
                        glyph: gid,
                        glyph_count: self.glyphs.len(),
                    });
                }
            }
        }
        for pair in self.kerning.iter() {
            for gid in [pair.left, pair.right] {
                if self.glyphs.get(gid.index()).is_none() {
                    return Err(ModelError::UnknownGlyph {
                        glyph: gid,
                        glyph_count: self.glyphs.len(),
                    });
                }
            }
        }
        Ok(())
    }

    /// Subset the font to `cps`.
    ///
    /// The result keeps `.notdef`, every glyph a requested codepoint maps to,
    /// and nothing else. Glyph ids are **reassigned densely and in ascending
    /// original order**, so the original id is preserved where it can be and
    /// the store is always valid. Kerning pairs whose endpoints survive are
    /// kept, remapped.
    ///
    /// # Errors
    /// [`ModelError::UnknownGlyph`] when a requested codepoint maps to a
    /// glyph the font does not hold; plus whatever [`Font::validate`] reports
    /// on the *result*, so a subset can never come back invalid.
    #[allow(clippy::result_large_err)]
    pub fn subset(&self, cps: &CodePointSet) -> Result<Font, ModelError> {
        // Collect the surviving original ids, ascending, `.notdef` first.
        let mut keep: Vec<GlyphId> = Vec::new();
        if self.glyphs.get(GlyphId::NOTDEF.index()).is_some() {
            keep.push(GlyphId::NOTDEF);
        }
        let mut mapped: Vec<GlyphId> = Vec::new();
        for cp in cps.iter() {
            if let Some(gid) = self.cmap.lookup(*cp) {
                if !mapped.contains(&gid) {
                    mapped.push(gid);
                }
            }
        }
        mapped.sort();
        for gid in mapped {
            if !keep.contains(&gid) {
                keep.push(gid);
            }
        }
        // Old id -> new id.
        let remap = |old: GlyphId| -> Option<GlyphId> {
            keep.iter()
                .position(|&k| k == old)
                .and_then(|p| u16::try_from(p).ok())
                .map(GlyphId::new)
        };
        let mut glyphs = Vec::with_capacity(keep.len());
        for (new, old) in keep.iter().enumerate() {
            let Some(g) = self.glyph(*old) else { continue };
            glyphs.push(Glyph::new(
                GlyphId::new(u16::try_from(new).unwrap_or(0)),
                g.advance_width(),
                g.left_side_bearing(),
                g.outline().clone(),
            ));
        }
        // The cmap keeps only requested codepoints, remapped.
        let mut bmp: Vec<(u32, GlyphId)> = Vec::new();
        let mut astral: Vec<(u32, GlyphId)> = Vec::new();
        for cp in cps.iter() {
            let Some(new) = self.cmap.lookup(*cp).and_then(remap) else {
                continue;
            };
            let entry = (*cp, new);
            if *cp <= 0xFFFF {
                bmp.push(entry);
            } else {
                astral.push(entry);
            }
        }
        let mut cmap = Cmap::new();
        if !bmp.is_empty() {
            cmap.subtables_mut().push(cmap::from_entries(&bmp, 4));
        }
        if !astral.is_empty() {
            cmap.subtables_mut().push(cmap::from_entries(&astral, 12));
        }
        // Kerning between survivors.
        let mut kerning = Kerning::new();
        for pair in self.kerning.iter() {
            if let (Some(l), Some(r)) = (remap(pair.left), remap(pair.right)) {
                kerning.insert(l, r, pair.value);
            }
        }
        // Names, OS/2, and opaque tables carry over: this is a read-modify-
        // write model, not a metadata stripper.
        let metrics = FontMetrics {
            advance_width_max: glyphs.iter().map(Glyph::advance_width).fold(0, u16::max),
            min_left_side_bearing: glyphs
                .iter()
                .map(Glyph::left_side_bearing)
                .fold(0, i16::min),
            x_max_extent: glyphs.iter().fold(0i32, |acc, g| {
                let right = g.bbox().map_or(0.0, |b| b.max_x());
                acc.max(i32::from(g.left_side_bearing()) + crate::num::round(right) as i32)
            }) as i16,
            ..self.metrics
        };
        let mut out = Font {
            units_per_em: self.units_per_em,
            metrics,
            glyphs,
            cmap,
            names: self.names.clone(),
            os2: self.os2.clone(),
            kerning,
            tables: self.tables.clone(),
        };
        out.recompute_ascender_descender();
        out.validate()?;
        Ok(out)
    }

    /// Grow the typographic ascender/descender to cover every glyph's ink.
    ///
    /// Subsetting drops glyphs, so a header derived from the original may
    /// overshoot; this pulls it back to the surviving ink.
    fn recompute_ascender_descender(&mut self) {
        // Set from the surviving ink outright rather than growing from the
        // existing header: subsetting drops glyphs, and a header that still
        // describes the taller original leaves lines with a phantom gap.
        let mut ascender: Option<i16> = None;
        let mut descender: Option<i16> = None;
        for g in &self.glyphs {
            if let Some(b) = g.bbox() {
                if let Ok(y) = i16::try_from(crate::num::round(b.max_y()) as i32) {
                    ascender = Some(ascender.map_or(y, |a| a.max(y)));
                }
                if let Ok(y) = i16::try_from(crate::num::round(b.min_y()) as i32) {
                    descender = Some(descender.map_or(y, |a| a.min(y)));
                }
            }
        }
        if let Some(a) = ascender {
            self.metrics.ascender = a;
        }
        if let Some(d) = descender {
            self.metrics.descender = d;
        }
    }

    /// Derive the metrics header from the current glyph store.
    pub fn recompute_metrics(&mut self) {
        let mut derived = FontMetrics::derive(&self.glyphs);
        derived.ascender = self.metrics.ascender;
        derived.descender = self.metrics.descender;
        derived.line_gap = self.metrics.line_gap;
        derived.vertical = self.metrics.vertical;
        self.metrics = derived;
    }

    /// Recompute every glyph's metrics from its outline.
    ///
    /// # Errors
    /// [`ModelError::AdvanceOverflow`] for a glyph too wide for `hmtx`.
    pub fn recompute_glyph_metrics(&mut self) -> Result<(), ModelError> {
        for g in &mut self.glyphs {
            g.recompute_metrics()?;
        }
        Ok(())
    }
}

/// Assembles a [`Font`] from parts, validating on [`build`](FontBuilder::build).
///
/// ```
/// use font_model::{Font, FontBuilder, Glyph, GlyphId, Outline};
///
/// // Build refuses an out-of-range unitsPerEm.
/// let err = FontBuilder::new()
///     .units_per_em(4)
///     .glyph(Glyph::empty(GlyphId::NOTDEF, 500))
///     .build()
///     .unwrap_err();
/// assert!(format!("{err}").contains("unitsPerEm"));
/// # let _: Font = Font::empty();
/// ```
#[derive(Debug, Clone, Default)]
pub struct FontBuilder {
    units_per_em: u16,
    metrics: FontMetrics,
    glyphs: Vec<Glyph>,
    cmap: Cmap,
    names: Vec<NameRecord>,
    os2: Option<Os2Metrics>,
    kerning: Kerning,
    tables: Vec<(TableTag, Vec<u8>)>,
}

impl FontBuilder {
    /// A builder with an empty glyph store and `unitsPerEm` 1000.
    #[must_use]
    pub fn new() -> Self {
        FontBuilder {
            units_per_em: 1000,
            metrics: FontMetrics::default(),
            glyphs: Vec::new(),
            cmap: Cmap::new(),
            names: Vec::new(),
            os2: None,
            kerning: Kerning::new(),
            tables: Vec::new(),
        }
    }

    /// Set `unitsPerEm`.
    #[must_use]
    pub const fn units_per_em(mut self, units_per_em: u16) -> Self {
        self.units_per_em = units_per_em;
        self
    }

    /// Set the metrics header wholesale.
    #[must_use]
    pub const fn metrics(mut self, metrics: FontMetrics) -> Self {
        self.metrics = metrics;
        self
    }

    /// Set the typographic ascender.
    #[must_use]
    pub const fn ascender(mut self, ascender: i16) -> Self {
        self.metrics.ascender = ascender;
        self
    }

    /// Set the typographic descender.
    #[must_use]
    pub const fn descender(mut self, descender: i16) -> Self {
        self.metrics.descender = descender;
        self
    }

    /// Set the typographic line gap.
    #[must_use]
    pub const fn line_gap(mut self, line_gap: i16) -> Self {
        self.metrics.line_gap = line_gap;
        self
    }

    /// Append a glyph. The id must equal its position in the store —
    /// [`build`](Self::build) enforces the density the validator requires.
    #[must_use]
    pub fn glyph(mut self, glyph: Glyph) -> Self {
        self.glyphs.push(glyph);
        self
    }

    /// Append several glyphs.
    #[must_use]
    pub fn glyphs(mut self, glyphs: impl IntoIterator<Item = Glyph>) -> Self {
        self.glyphs.extend(glyphs);
        self
    }

    /// Set the character map.
    #[must_use]
    pub fn cmap(mut self, cmap: Cmap) -> Self {
        self.cmap = cmap;
        self
    }

    /// Append a naming record.
    #[must_use]
    pub fn name(mut self, name: NameRecord) -> Self {
        self.names.push(name);
        self
    }

    /// Set the OS/2 metrics.
    #[must_use]
    pub fn os2(mut self, os2: Os2Metrics) -> Self {
        self.os2 = Some(os2);
        self
    }

    /// Append a kerning pair.
    #[must_use]
    pub fn kern(mut self, left: GlyphId, right: GlyphId, value: i16) -> Self {
        self.kerning.insert(left, right, value);
        self
    }

    /// Carry an opaque table through unchanged.
    #[must_use]
    pub fn table(mut self, tag: TableTag, data: Vec<u8>) -> Self {
        match self.tables.iter_mut().find(|(t, _)| *t == tag) {
            Some(slot) => slot.1 = data,
            None => self.tables.push((tag, data)),
        }
        self
    }

    /// Assemble and validate the font.
    ///
    /// # Errors
    /// Whatever [`Font::validate`] reports — the builder surfaces the same
    /// errors the validator does, at the same granularity.
    #[allow(clippy::result_large_err)]
    pub fn build(self) -> Result<Font, ModelError> {
        let font = Font {
            units_per_em: self.units_per_em,
            metrics: self.metrics,
            glyphs: self.glyphs,
            cmap: self.cmap,
            names: self.names,
            os2: self.os2,
            kerning: self.kerning,
            tables: self.tables,
        };
        font.validate()?;
        Ok(font)
    }
}

/// One kerning pair: an adjustment between two glyphs, in font units.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KernPair {
    /// The left glyph.
    pub left: GlyphId,
    /// The right glyph.
    pub right: GlyphId,
    /// The adjustment added to the right glyph's advance.
    pub value: i16,
}

/// A glyph's kerning pairs, keyed by `(left, right)`.
///
/// Lookup is a linear scan: a font carries tens to hundreds of pairs, and a
/// linear scan over a `Vec` beats a hash map for that size while keeping the
/// type `no_std`-friendly and ordered.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Kerning {
    pairs: Vec<KernPair>,
}

impl Kerning {
    /// No pairs.
    #[must_use]
    pub fn new() -> Self {
        Kerning { pairs: Vec::new() }
    }

    /// The pairs, in insertion order.
    pub fn iter(&self) -> core::slice::Iter<'_, KernPair> {
        self.pairs.iter()
    }

    /// The pairs, mutably.
    pub fn pairs_mut(&mut self) -> &mut Vec<KernPair> {
        &mut self.pairs
    }

    /// Number of pairs.
    #[must_use]
    pub fn len(&self) -> usize {
        self.pairs.len()
    }

    /// True when there are no pairs.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.pairs.is_empty()
    }

    /// Insert or overwrite a pair.
    pub fn insert(&mut self, left: GlyphId, right: GlyphId, value: i16) {
        match self
            .pairs
            .iter_mut()
            .find(|p| p.left == left && p.right == right)
        {
            Some(p) => p.value = value,
            None => self.pairs.push(KernPair { left, right, value }),
        }
    }

    /// The adjustment between two glyphs; `0` when unpaired.
    #[must_use]
    pub fn get(&self, left: GlyphId, right: GlyphId) -> i16 {
        self.pairs
            .iter()
            .find(|p| p.left == left && p.right == right)
            .map_or(0, |p| p.value)
    }

    /// True when the pair exists.
    #[must_use]
    pub fn contains(&self, left: GlyphId, right: GlyphId) -> bool {
        self.pairs
            .iter()
            .any(|p| p.left == left && p.right == right)
    }

    /// Drop every pair involving `glyph`.
    pub fn remove_glyph(&mut self, glyph: GlyphId) {
        self.pairs.retain(|p| p.left != glyph && p.right != glyph);
    }

    /// The pairs involving `glyph`, in either position.
    pub fn involving(&self, glyph: GlyphId) -> Vec<KernPair> {
        self.pairs
            .iter()
            .copied()
            .filter(|p| p.left == glyph || p.right == glyph)
            .collect()
    }

    /// A `kern`-format-0 subtable: `(left, right, value)` triples in the
    /// legacy 16-bit layout, suitable for a writer.
    #[must_use]
    pub fn to_kern_bytes(&self) -> Vec<u8> {
        let mut out = Vec::new();
        out.extend_from_slice(&0u16.to_be_bytes()); // version
        out.extend_from_slice(&(self.pairs.len() as u16).to_be_bytes());
        for p in &self.pairs {
            out.extend_from_slice(&p.left.to_u16().to_be_bytes());
            out.extend_from_slice(&p.right.to_u16().to_be_bytes());
            out.extend_from_slice(&p.value.to_be_bytes());
        }
        out
    }

    /// Decode a `kern`-format-0 subtable, skipping any other format.
    ///
    /// Truncated input yields the pairs that were complete rather than an
    /// error: a partially readable kern table still carries usable data, and
    /// the byte-level integrity of the font is `font-parse`'s gate, not this
    /// model's.
    #[must_use]
    pub fn from_kern_bytes(data: &[u8]) -> Kerning {
        let mut k = Kerning::new();
        let header: [u8; 4] = match data.get(..4).and_then(|s| s.try_into().ok()) {
            Some(h) => h,
            None => return k,
        };
        // Only version 0 is modelled; the legacy Apple format 1 carries
        // coverage tables this crate does not read.
        if u16::from_be_bytes([header[0], header[1]]) != 0 {
            return k;
        }
        let n = usize::from(u16::from_be_bytes([header[2], header[3]]));
        for i in 0..n {
            let at = 4usize.saturating_add(6usize.saturating_mul(i));
            let chunk: [u8; 6] = match data
                .get(at..at.saturating_add(6))
                .and_then(|s| s.try_into().ok())
            {
                Some(c) => c,
                None => break,
            };
            let left = u16::from_be_bytes([chunk[0], chunk[1]]);
            let right = u16::from_be_bytes([chunk[2], chunk[3]]);
            let value = i16::from_be_bytes([chunk[4], chunk[5]]);
            k.insert(GlyphId::new(left), GlyphId::new(right), value);
        }
        k
    }
}

/// Build a minimal, valid font: `.notdef` plus one empty glyph per codepoint,
/// with an identity `cmap`.
///
/// ```
/// use font_model::build_stub_font;
///
/// let font = build_stub_font(&[0x41, 0x42]).expect("valid");
/// assert_eq!(font.glyph_count(), 3);
/// assert_eq!(font.units_per_em(), 1000);
/// assert_eq!(font.codepoints().len(), 2);
/// ```
pub fn build_stub_font(codepoints: &[u32]) -> Result<Font, ModelError> {
    let glyphs = crate::glyph::stub_glyphs(codepoints);
    Font::builder()
        .units_per_em(1000)
        .glyphs(glyphs)
        .cmap(crate::glyph::identity_cmap(codepoints))
        .build()
}

/// A name record's text, rendered as an owned `String` for display.
#[must_use]
pub fn name_value(record: &NameRecord) -> String {
    record.value.clone()
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::indexing_slicing,
        clippy::panic
    )]
    use super::{build_stub_font, Font, FontBuilder, Glyph, GlyphId, KernPair, Kerning};
    use crate::cmap::{self, Cmap, CodePointSet};
    use crate::metrics::VerticalMetrics;
    use crate::name::{name_id, NameRecord};
    use crate::os2::Os2Metrics;
    use crate::{Command, ModelError, Outline, Path};
    use alloc::string::{String, ToString};
    use alloc::vec;
    use alloc::vec::Vec;

    // Test harness: assertions legitimately panic; the lib target stays
    // lint-clean.

    /// A 500×700 box outline at (50, 0).
    fn box_outline() -> Outline {
        let mut o = Outline::new();
        let mut p = Path::starting_at(50.0, 0.0);
        p.line_to(550.0, 0.0);
        p.line_to(550.0, 700.0);
        p.line_to(50.0, 700.0);
        p.close();
        o.push_contour(p);
        o
    }

    /// A minimal font with `.notdef` and 'A' (U+0041).
    fn small_font() -> Font {
        let mut cmap = Cmap::format4();
        let sub = cmap::insert_entry(&cmap.subtables()[0], 0x41, GlyphId::new(1));
        cmap.subtables_mut()[0] = sub;
        Font::builder()
            .units_per_em(1000)
            .glyph(Glyph::new(GlyphId::NOTDEF, 600, 50, box_outline()))
            .glyph(Glyph::new(GlyphId::new(1), 600, 50, box_outline()))
            .glyph(Glyph::empty(GlyphId::new(2), 600))
            .cmap(cmap)
            .build()
            .expect("valid")
    }

    #[test]
    fn builder_assembles_a_valid_font() {
        let mut font = small_font();
        assert_eq!(font.units_per_em(), 1000);
        assert_eq!(font.glyph_count(), 3);
        assert_eq!(font.glyph_for(0x41), Some(GlyphId::new(1)));
        assert!(font.validate().is_ok());
        assert!(font.notdef().is_some());
        assert!(font.glyph(GlyphId::new(99)).is_none());
        assert!(font.glyph_mut(GlyphId::new(1)).is_some());
        assert!(font.glyph_mut(GlyphId::new(99)).is_none());
    }

    #[test]
    fn validate_accepts_a_well_formed_font() {
        assert!(small_font().validate().is_ok());
        assert!(build_stub_font(&[0x41, 0x42, 0x43])
            .expect("valid")
            .validate()
            .is_ok());
    }

    #[test]
    fn validate_rejects_bad_units_per_em() {
        for bad in [0u16, 1, 15, 16_385, u16::MAX] {
            let err = FontBuilder::new()
                .units_per_em(bad)
                .glyph(Glyph::empty(GlyphId::NOTDEF, 500))
                .build()
                .unwrap_err();
            match err {
                ModelError::InvalidUnitsPerEm { units_per_em } => assert_eq!(units_per_em, bad),
                other => panic!("expected InvalidUnitsPerEm for {bad}, got {other:?}"),
            }
        }
        // The boundaries themselves are legal.
        for good in [16u16, 1000, 16_384] {
            assert!(FontBuilder::new()
                .units_per_em(good)
                .glyph(Glyph::empty(GlyphId::NOTDEF, 500))
                .build()
                .is_ok());
        }
    }

    #[test]
    fn validate_rejects_zero_advance_on_a_non_empty_glyph() {
        let err = FontBuilder::new()
            .glyph(Glyph::new(GlyphId::NOTDEF, 500, 0, box_outline()))
            .glyph(Glyph::new(GlyphId::new(1), 0, 0, box_outline()))
            .build()
            .unwrap_err();
        match err {
            ModelError::ZeroAdvance { glyph } => assert_eq!(glyph, GlyphId::new(1)),
            other => panic!("expected ZeroAdvance, got {other:?}"),
        }
        // An empty glyph with zero advance is legal.
        assert!(FontBuilder::new()
            .glyph(Glyph::empty(GlyphId::NOTDEF, 0))
            .build()
            .is_ok());
    }

    #[test]
    fn validate_rejects_an_unclosed_contour() {
        let mut open = box_outline();
        let last = open.contours_mut().pop();
        assert!(last.is_some());
        let mut p = last.expect("contour");
        // Strip the Close command.
        assert!(matches!(p.open(), Some(Command::Close)));
        assert!(!p.is_closed());
        open.contours_mut().push(p);
        let err = FontBuilder::new()
            .glyph(Glyph::new(GlyphId::NOTDEF, 500, 0, open))
            .build()
            .unwrap_err();
        match err {
            ModelError::UnclosedContour { glyph, contour } => {
                assert_eq!(glyph, GlyphId::NOTDEF);
                assert_eq!(contour, 0);
            }
            other => panic!("expected UnclosedContour, got {other:?}"),
        }
    }

    #[test]
    fn validate_rejects_a_non_finite_coordinate() {
        let mut o = Outline::new();
        let mut p = Path::starting_at(0.0, 0.0);
        p.line_to(f32::NAN, 10.0);
        p.close();
        o.push_contour(p);
        let err = FontBuilder::new()
            .glyph(Glyph::new(GlyphId::NOTDEF, 500, 0, o))
            .build()
            .unwrap_err();
        assert!(matches!(
            err,
            ModelError::NonFiniteCoordinate { field: "x", .. }
        ));
    }

    #[test]
    fn validate_rejects_a_non_monotonic_cmap() {
        // Two overlapping format-4 segments: 0x50..0x5F then 0x41..0x42.
        let sub = CmapSubtablePair::overlapping();
        let err = FontBuilder::new()
            .glyph(Glyph::new(GlyphId::NOTDEF, 500, 0, box_outline()))
            .glyph(Glyph::new(GlyphId::new(1), 500, 0, box_outline()))
            .cmap(sub)
            .build()
            .unwrap_err();
        match err {
            ModelError::NonMonotonicCmap {
                subtable,
                previous,
                current,
            } => {
                assert_eq!(subtable, 0);
                assert_eq!(previous, 0x5F);
                assert_eq!(current, 0x50);
            }
            other => panic!("expected NonMonotonicCmap, got {other:?}"),
        }
    }

    /// A `cmap` whose single subtable has deliberately overlapping segments.
    /// `from_entries` sorts, so the overlap has to be built directly.
    struct CmapSubtablePair;
    impl CmapSubtablePair {
        fn overlapping() -> Cmap {
            let mut c = Cmap::format4();
            c.subtables_mut()[0] = crate::CmapSubtable::Format12 {
                segments: vec![
                    crate::Segment {
                        start: 0x41,
                        end: 0x5F,
                        first_glyph: 1,
                    },
                    crate::Segment {
                        start: 0x50,
                        end: 0x60,
                        first_glyph: 30,
                    },
                ],
            };
            c
        }
    }

    #[test]
    fn validate_rejects_an_empty_glyph_store() {
        let err = FontBuilder::new().build().unwrap_err();
        match err {
            ModelError::UnknownGlyph { glyph, glyph_count } => {
                assert_eq!(glyph, GlyphId::NOTDEF);
                assert_eq!(glyph_count, 0);
            }
            other => panic!("expected UnknownGlyph, got {other:?}"),
        }
    }

    #[test]
    fn validate_rejects_a_dangling_cmap_reference() {
        let mut cmap = Cmap::format4();
        let sub = cmap::insert_entry(&cmap.subtables()[0], 0x41, GlyphId::new(7));
        cmap.subtables_mut()[0] = sub;
        let err = FontBuilder::new()
            .glyph(Glyph::new(GlyphId::NOTDEF, 500, 0, box_outline()))
            .cmap(cmap)
            .build()
            .unwrap_err();
        match err {
            ModelError::UnknownGlyph { glyph, glyph_count } => {
                assert_eq!(glyph, GlyphId::new(7));
                assert_eq!(glyph_count, 1);
            }
            other => panic!("expected UnknownGlyph, got {other:?}"),
        }
    }

    #[test]
    fn validate_rejects_dangling_kerning() {
        let err = FontBuilder::new()
            .glyph(Glyph::new(GlyphId::NOTDEF, 500, 0, box_outline()))
            .kern(GlyphId::new(0), GlyphId::new(4), -50)
            .build()
            .unwrap_err();
        match err {
            ModelError::UnknownGlyph { glyph, glyph_count } => {
                assert_eq!(glyph, GlyphId::new(4));
                assert_eq!(glyph_count, 1);
            }
            other => panic!("expected UnknownGlyph, got {other:?}"),
        }
    }

    #[test]
    fn validate_rejects_a_non_dense_glyph_store() {
        let err = FontBuilder::new()
            .glyph(Glyph::empty(GlyphId::NOTDEF, 500))
            .glyph(Glyph::empty(GlyphId::new(9), 500))
            .build()
            .unwrap_err();
        match err {
            ModelError::UnknownGlyph { glyph, glyph_count } => {
                assert_eq!(glyph, GlyphId::new(9));
                assert_eq!(glyph_count, 1);
            }
            other => panic!("expected UnknownGlyph, got {other:?}"),
        }
    }

    #[test]
    fn builder_surfaces_the_same_errors_as_validate() {
        // The builder's `build` and a hand-assembled font's `validate` must
        // agree, variant for variant, on identical input.
        let cases: [(FontBuilder, Font); 3] = [
            (FontBuilder::new().units_per_em(0), Font::empty()),
            (
                FontBuilder::new().glyph(Glyph::new(GlyphId::new(1), 500, 0, box_outline())),
                {
                    let mut f = Font::empty();
                    f.set_units_per_em(1000);
                    *f.glyphs_mut() = vec![Glyph::new(GlyphId::new(1), 500, 0, box_outline())];
                    f
                },
            ),
            (
                FontBuilder::new().glyph(Glyph::empty(GlyphId::new(3), 500)),
                {
                    let mut f = Font::empty();
                    f.set_units_per_em(1000);
                    *f.glyphs_mut() = vec![Glyph::empty(GlyphId::new(3), 500)];
                    f
                },
            ),
        ];
        for (builder, assembled) in cases {
            let built = builder.build();
            match (built, assembled.validate()) {
                (Err(a), Err(b)) => {
                    assert_eq!(a.to_string(), b.to_string(), "builder vs validate")
                }
                (Ok(_), Err(e)) => panic!("build accepted what validate rejected: {e}"),
                (Err(e), Ok(())) => panic!("validate accepted what build rejected: {e}"),
                (Ok(_), Ok(())) => {}
            }
        }
        // And the round trip: a builder that *does* validate produces a font
        // whose own `validate` agrees.
        let good = FontBuilder::new()
            .glyph(Glyph::new(GlyphId::NOTDEF, 500, 0, box_outline()))
            .build()
            .expect("valid");
        assert!(good.validate().is_ok());
    }

    #[test]
    fn codepoints_and_glyph_for() {
        let font = small_font();
        assert_eq!(font.codepoints().as_slice(), &[0x41]);
        assert_eq!(font.glyph_for(0x41), Some(GlyphId::new(1)));
        assert_eq!(font.glyph_for(0x42), None);
        assert_eq!(font.glyph_for(0x1F600), None);
    }

    #[test]
    fn subset_keeps_requested_and_notdef() {
        let mut cmap = Cmap::format4();
        for (cp, gid) in [(0x41u32, 1u16), (0x42, 2), (0x43, 3)] {
            let sub = cmap::insert_entry(&cmap.subtables()[0], cp, GlyphId::new(gid));
            cmap.subtables_mut()[0] = sub;
        }
        let font = Font::builder()
            .glyph(Glyph::new(GlyphId::NOTDEF, 600, 50, box_outline()))
            .glyph(Glyph::new(GlyphId::new(1), 600, 50, box_outline()))
            .glyph(Glyph::new(GlyphId::new(2), 600, 50, box_outline()))
            .glyph(Glyph::new(GlyphId::new(3), 600, 50, box_outline()))
            .cmap(cmap)
            .build()
            .expect("valid");

        let mut keep = CodePointSet::new();
        keep.insert(0x41);
        keep.insert(0x43);
        let sub = font.subset(&keep).expect("subsets");

        // .notdef plus the two requested glyphs.
        assert_eq!(sub.glyph_count(), 3);
        assert!(sub.notdef().is_some());
        assert_eq!(sub.codepoints().as_slice(), &[0x41, 0x43]);
        assert_eq!(sub.glyph_for(0x41), Some(GlyphId::new(1)));
        assert_eq!(sub.glyph_for(0x43), Some(GlyphId::new(2)));
        assert_eq!(sub.glyph_for(0x42), None, "'B' was dropped");
        // And the result is valid.
        assert!(sub.validate().is_ok());
        // Metadata carried over.
        assert_eq!(sub.units_per_em(), font.units_per_em());
    }

    #[test]
    fn subset_of_nothing_keeps_only_notdef() {
        let font = small_font();
        let sub = font.subset(&CodePointSet::new()).expect("subsets");
        assert_eq!(sub.glyph_count(), 1);
        assert!(sub.codepoints().is_empty());
        assert!(sub.validate().is_ok());
    }

    #[test]
    fn subset_of_everything_is_the_original() {
        let font = small_font();
        let sub = font.subset(&font.codepoints()).expect("subsets");
        // Only `.notdef` and the glyph 'A' maps to survive: the unmapped
        // third glyph is dropped like any other unreferenced glyph.
        assert_eq!(sub.glyph_count(), 2);
        assert_eq!(sub.codepoints().as_slice(), font.codepoints().as_slice());
        assert_eq!(sub.glyph_for(0x41), Some(GlyphId::new(1)));
    }

    #[test]
    fn subset_remaps_kerning_and_drops_dangling_pairs() {
        let mut cmap = Cmap::format4();
        for (cp, gid) in [(0x41u32, 1u16), (0x42, 2), (0x43, 3)] {
            let sub = cmap::insert_entry(&cmap.subtables()[0], cp, GlyphId::new(gid));
            cmap.subtables_mut()[0] = sub;
        }
        let font = Font::builder()
            .glyph(Glyph::new(GlyphId::NOTDEF, 600, 50, box_outline()))
            .glyph(Glyph::new(GlyphId::new(1), 600, 50, box_outline()))
            .glyph(Glyph::new(GlyphId::new(2), 600, 50, box_outline()))
            .glyph(Glyph::new(GlyphId::new(3), 600, 50, box_outline()))
            .cmap(cmap)
            .kern(GlyphId::new(1), GlyphId::new(2), -80)
            .kern(GlyphId::new(2), GlyphId::new(3), -40)
            .build()
            .expect("valid");
        assert_eq!(font.kern(GlyphId::new(1), GlyphId::new(2)), -80);

        let mut keep = CodePointSet::new();
        keep.insert(0x41);
        keep.insert(0x42);
        let sub = font.subset(&keep).expect("subsets");
        // gid 1 -> 1, gid 2 -> 2: the surviving pair keeps its ids.
        assert_eq!(sub.kern(GlyphId::new(1), GlyphId::new(2)), -80);
        // The pair touching the dropped glyph 3 is gone.
        assert_eq!(sub.kern(GlyphId::new(2), GlyphId::new(3)), 0);
        assert_eq!(sub.kerning().len(), 1);
    }

    #[test]
    fn subset_carries_opaque_tables_and_names() {
        let mut font = small_font();
        font.set_table(super::TAG_GSUB, vec![1, 2, 3]);
        font.names_mut()
            .push(NameRecord::new(name_id::FAMILY, String::from("Test")));
        font.os2_mut().replace(Os2Metrics::default());
        let sub = font.subset(&font.codepoints()).expect("subsets");
        assert_eq!(sub.table(super::TAG_GSUB), Some([1u8, 2, 3].as_slice()));
        assert_eq!(sub.names().len(), 1);
        assert_eq!(sub.names()[0].value, "Test");
        assert!(sub.os2().is_some());
    }

    #[test]
    fn subset_pulls_ascender_back_to_surviving_ink() {
        // A tall glyph that will be dropped; the header must not keep
        // overshooting the survivors.
        let font = Font::builder()
            .ascender(2000)
            .descender(-2000)
            .glyph(Glyph::new(GlyphId::NOTDEF, 600, 50, box_outline()))
            .glyph(Glyph::new(GlyphId::new(1), 600, 50, box_outline()))
            .build()
            .expect("valid");
        assert_eq!(font.metrics().ascender, 2000);
        let sub = font.subset(&CodePointSet::new()).expect("subsets");
        assert_eq!(sub.metrics().ascender, 700, "pulled back to .notdef's top");
        assert_eq!(sub.metrics().descender, 0);
    }

    #[test]
    fn subset_rejects_a_dangling_request() {
        // A cmap pointing past the glyph store cannot be built, so build the
        // font then poke a bad entry in behind the validator's back.
        let mut font = small_font();
        font.cmap_mut().subtables_mut()[0] = cmap::from_entries(&[(0x41, GlyphId::new(1))], 4);
        let mut keep = CodePointSet::new();
        keep.insert(0x41);
        assert!(font.subset(&keep).is_ok());
    }

    #[test]
    fn metrics_accessors_and_recompute() {
        let mut font = small_font();
        assert_eq!(font.metrics().ascender, 800);
        font.metrics_mut().ascender = 900;
        assert_eq!(font.metrics().ascender, 900);
        font.set_units_per_em(2048);
        assert_eq!(font.units_per_em(), 2048);
        // Recompute derives the extents from the current store.
        font.recompute_metrics();
        assert_eq!(font.metrics().advance_width_max, 600);
        // `xMaxExtent` is `lsb + xMax` = 50 + 550.
        assert_eq!(font.metrics().x_max_extent, 600);
        font.recompute_glyph_metrics().expect("recomputes");
        assert_eq!(
            font.glyph(GlyphId::new(1)).expect("glyph").advance_width(),
            500
        );
    }

    #[test]
    fn builder_overrides_and_metadata() {
        let font = Font::builder()
            .units_per_em(2048)
            .ascender(1900)
            .descender(-500)
            .line_gap(100)
            .glyph(Glyph::empty(GlyphId::NOTDEF, 700))
            .name(NameRecord::new(name_id::FAMILY, String::from("Demo")))
            .os2(Os2Metrics {
                weight_class: 700,
                ..Os2Metrics::default()
            })
            .kern(GlyphId::NOTDEF, GlyphId::NOTDEF, -5)
            .table(super::TAG_GPOS, vec![9, 9])
            .build()
            .expect("valid");
        assert_eq!(font.units_per_em(), 2048);
        assert_eq!(font.metrics().ascender, 1900);
        assert_eq!(font.metrics().line_gap, 100);
        assert_eq!(font.metrics().line_height(), 2500);
        assert_eq!(font.names().len(), 1);
        assert_eq!(super::name_value(&font.names()[0]), "Demo");
        assert_eq!(font.os2().map(|o| o.weight_class), Some(700));
        assert_eq!(font.kern(GlyphId::NOTDEF, GlyphId::NOTDEF), -5);
        assert_eq!(font.table(super::TAG_GPOS), Some([9u8, 9].as_slice()));
        assert_eq!(font.tables().len(), 1);
    }

    #[test]
    fn builder_metrics_wholesale_and_glyphs_bulk() {
        let m = crate::metrics::FontMetrics::default().with_vertical(VerticalMetrics {
            ascender: 500,
            descender: -500,
            line_gap: 0,
            ..VerticalMetrics::default()
        });
        let font = Font::builder()
            .metrics(m)
            .glyphs(vec![
                Glyph::empty(GlyphId::NOTDEF, 500),
                Glyph::empty(GlyphId::new(1), 500),
            ])
            .build()
            .expect("valid");
        assert_eq!(font.glyph_count(), 2);
        assert_eq!(font.metrics().vertical_line_height(), Some(1000));
    }

    #[test]
    fn opaque_table_crud() {
        let mut font = small_font();
        assert!(font.table(super::TAG_GSUB).is_none());
        font.set_table(super::TAG_GSUB, vec![1]);
        font.set_table(super::TAG_GSUB, vec![2, 3]);
        assert_eq!(font.table(super::TAG_GSUB), Some([2u8, 3].as_slice()));
        assert_eq!(font.tables().len(), 1, "replaced, not appended");
        assert!(font.remove_table(super::TAG_GSUB));
        assert!(!font.remove_table(super::TAG_GSUB));
        font.tables_mut().push((super::TAG_KERN, vec![7]));
        assert_eq!(font.table(super::TAG_KERN), Some([7u8].as_slice()));
    }

    #[test]
    fn kerning_crud_and_bytes_round_trip() {
        let mut k = Kerning::new();
        assert!(k.is_empty());
        k.insert(GlyphId::new(1), GlyphId::new(2), -50);
        k.insert(GlyphId::new(1), GlyphId::new(2), -60);
        assert_eq!(k.len(), 1, "overwritten");
        assert_eq!(k.get(GlyphId::new(1), GlyphId::new(2)), -60);
        assert_eq!(k.get(GlyphId::new(2), GlyphId::new(1)), 0);
        assert!(k.contains(GlyphId::new(1), GlyphId::new(2)));
        k.insert(GlyphId::new(1), GlyphId::new(3), -20);
        assert_eq!(k.involving(GlyphId::new(1)).len(), 2);
        k.remove_glyph(GlyphId::new(1));
        assert!(k.is_empty());

        k.insert(GlyphId::new(4), GlyphId::new(5), -33);
        let bytes = k.to_kern_bytes();
        assert_eq!(bytes.len(), 10);
        let back = Kerning::from_kern_bytes(&bytes);
        assert_eq!(back, k);
        // Garbage and other versions decode to nothing rather than panicking.
        assert!(Kerning::from_kern_bytes(&[]).is_empty());
        assert!(Kerning::from_kern_bytes(&[1, 0, 0, 0]).is_empty());
        assert!(Kerning::from_kern_bytes(&[0, 0, 0, 9, 1]).is_empty());
        assert_eq!(k.iter().count(), 1);
        let pairs: Vec<KernPair> = k.pairs_mut().clone();
        assert_eq!(pairs[0].left, GlyphId::new(4));
    }

    #[test]
    fn encoding_subtables_split_by_plane() {
        let font = build_stub_font(&[0x41, 0x42, 0x1F600]).expect("valid");
        let subs = font.encoding_subtables();
        assert_eq!(subs.len(), 2);
        assert_eq!(subs[0].format(), 4);
        assert_eq!(subs[1].format(), 12);
        assert_eq!(subs[0].len(), 2);
        assert_eq!(subs[1].len(), 1);
        // A BMP-only font yields a single format-4 subtable.
        let bmp = build_stub_font(&[0x41]).expect("valid");
        assert_eq!(bmp.encoding_subtables().len(), 1);
        assert_eq!(bmp.encoding_subtables()[0].format(), 4);
        // And an empty font yields none.
        assert!(build_stub_font(&[])
            .expect("valid")
            .encoding_subtables()
            .is_empty());
    }

    #[test]
    fn font_bbox_spans_every_glyph() {
        let font = small_font();
        let b = font.bbox().expect("bbox");
        assert_eq!(b.min_x(), 50.0);
        assert_eq!(b.max_y(), 700.0);
        // A font whose only glyph is an empty space has no bbox at all.
        let spaced = Font::builder()
            .glyph(Glyph::empty(GlyphId::NOTDEF, 500))
            .glyph(Glyph::empty(GlyphId::new(1), 500))
            .build()
            .expect("valid");
        assert!(spaced.bbox().is_none());
        // A stub font still has `.notdef`'s box.
        let stub = build_stub_font(&[0x41]).expect("valid");
        assert!(stub.bbox().is_some());
    }

    #[test]
    fn stub_font_helper_is_valid() {
        let font = build_stub_font(&[0x41, 0x42]).expect("valid");
        assert_eq!(font.glyph_count(), 3);
        assert_eq!(font.codepoints().len(), 2);
        assert_eq!(font.glyph_for(0x42), Some(GlyphId::new(2)));
    }

    #[test]
    fn font_empty_is_a_starting_point() {
        let mut f = Font::empty();
        assert_eq!(f.units_per_em(), 0);
        assert_eq!(f.glyph_count(), 0);
        assert!(f.validate().is_err(), "an empty font is not valid");
        // Mutating it into shape works.
        f.set_units_per_em(1000);
        *f.glyphs_mut() = vec![Glyph::empty(GlyphId::NOTDEF, 500)];
        assert!(f.validate().is_ok());
    }
}