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
use super::*;

#[derive(Debug, PartialEq, Eq)]
pub enum DeserializationError {
    /// The TFM file is empty (i.e., 0 bytes).
    ///
    /// Knuth's TFToPL doesn't handle this case explicitly.
    /// Providing an empty file to the TFtoPL always returns an error.
    /// However, the error message is non-deterministic
    ///     and depends on the initial value of a specific byte of memory (`tfm[0]`).
    /// If that byte is greater than 127 the message for [`DeserializationError::InternalFileLengthIsNegative`]
    ///     is printed; otherwise the message for [`DeserializationError::FileHasOneByte`] is printed.
    FileIsEmpty,
    /// The TFM file consists of a single byte.
    FileHasOneByte(u8),
    /// The file length specified inside the TFM file is invalid because it is zero.
    InternalFileLengthIsZero,
    /// The file length specified inside the TFM file is invalid because it is negative.
    ///
    /// The variant payload is the invalid length in this file.
    InternalFileLengthIsNegative(i16),
    /// The file length specified inside the TFM file is invalid because the file is smaller than it claims.
    ///
    /// The variant payload is the invalid length of this file (in words) and the actual file size (in bytes).
    /// One word is 4 bytes.
    InternalFileLengthIsTooBig(i16, usize),
    /// The file length specified inside the TFM file is invalid because it is too small.
    ///
    /// TFM files must contain at least 24 bytes of data: the 16-bit file size and the 11
    /// 16-bit numbers in the sub file sizes section.
    ///
    /// Knuth's TFToPL doesn't handle this case explicitly.
    /// When this buggy input is provided,
    ///     a [DeserializationError::SubFileSizeIsNegative] error is thrown in some cases,
    ///     and a [DeserializationError::HeaderLengthIsTooSmall] in other cases.
    /// Although the result is deterministic, there seems to be undefined behavior here
    ///     because it seems to depend on the value of uninitialized memory.
    InternalFileLengthIsTooSmall(i16, usize),
    /// One of the sub file sizes is negative.
    SubFileSizeIsNegative(SubFileSizes),
    /// The header length is too small (either 0 or 1).
    HeaderLengthIsTooSmall(i16),
    /// The character range is invalid.
    ///
    /// This means either that the lower bound of the range (the smallest character)
    /// is not smaller than the upper bound of the range (the largest character),
    /// or the upper bound is bigger than 255.
    InvalidCharacterRange(i16, i16),
    /// The character dimension sub-files are incomplete.
    ///
    /// This means the sub-file for either widths, heights, depths or italic corrections is empty.
    IncompleteSubFiles(SubFileSizes),
    /// There are more than 256 extensible characters.
    TooManyExtensibleCharacters(i16),
    /// The sub-file sizes are inconsistent.
    InconsistentSubFileSizes(SubFileSizes),
}

impl DeserializationError {
    /// Returns the error message the TFtoPL program prints for this kind of error.
    pub fn tftopl_message(&self) -> String {
        use DeserializationError::*;
        let first_line = match self {
            FileHasOneByte(0..=127) => "The input file is only one byte long!".into(),
            InternalFileLengthIsZero => {
                "The file claims to have length zero, but that's impossible!".into()
            }
            FileHasOneByte(128..=255) | InternalFileLengthIsNegative(_) | FileIsEmpty => {
                // See documentation on [DeserializationError::FileIsEmpty] for why we return this string in the case
                // when the file is empty. While two error messages are possible, this is the one I
                // observed on my machine today.
                "The first byte of the input file exceeds 127!".into()
            }
            InternalFileLengthIsTooBig(_, _) => "The file has fewer bytes than it claims!".into(),
            InternalFileLengthIsTooSmall(_, _) | SubFileSizeIsNegative(_) => {
                "One of the subfile sizes is negative!".into()
            }
            HeaderLengthIsTooSmall(lh) => format!["The header length is only {lh}!"],
            InvalidCharacterRange(l, u) => {
                format!("The character code range {l}..{u} is illegal!")
            }
            IncompleteSubFiles(_) => "Incomplete subfiles for character dimensions!".into(),
            TooManyExtensibleCharacters(n) => format!["There are {n} extensible recipes!"],
            InconsistentSubFileSizes(_) => "Subfile sizes don't add up to the stated total!".into(),
        };
        format!(
            "{}\nSorry, but I can't go on; are you sure this is a TFM?",
            first_line
        )
    }

    /// Returns the section in Knuth's TFtoPL (version 2014) in which this error occurs.
    pub fn tftopl_section(&self) -> usize {
        use DeserializationError::*;
        match self {
            FileIsEmpty
            | FileHasOneByte(_)
            | InternalFileLengthIsZero
            | InternalFileLengthIsNegative(_)
            | InternalFileLengthIsTooBig(_, _) => 20,
            InternalFileLengthIsTooSmall(_, _)
            | SubFileSizeIsNegative(_)
            | HeaderLengthIsTooSmall(_)
            | InvalidCharacterRange(_, _)
            | IncompleteSubFiles(_)
            | TooManyExtensibleCharacters(_)
            | InconsistentSubFileSizes(_) => 21,
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
pub enum DeserializationWarning {
    /// The file length specified inside the TFM file is smaller than the actual file size.
    ///
    /// Additional data after the file length is ignored.
    ///
    /// The first element is the number of words specified in the TFM header.
    /// The second element is the number of bytes in the file.
    InternalFileLengthIsSmall(i16, usize),
}

impl DeserializationWarning {
    /// Returns the warning message the TFtoPL program prints for this kind of error.
    pub fn tftopl_message(&self) -> String {
        use DeserializationWarning::*;
        match self {
            InternalFileLengthIsSmall(_, _) => {
                "There's some extra junk at the end of the TFM file,\nbut I'll proceed as if it weren't there.".into()
            },
        }
    }

    /// Returns the section in Knuth's TFtoPL (version 2014) in which this warning occurs.
    pub fn tftopl_section(&self) -> usize {
        use DeserializationWarning::*;
        match self {
            InternalFileLengthIsSmall(_, _) => 20,
        }
    }

    /// Returns true if this warning means the .tfm file was modified.
    pub fn tfm_file_modified(&self) -> bool {
        use DeserializationWarning::*;
        match self {
            InternalFileLengthIsSmall(_, _) => false,
        }
    }
}

/// Deserialize a TeX font metric (.tfm) file.
pub(super) fn deserialize(
    b: &[u8],
) -> (
    Result<File, DeserializationError>,
    Vec<DeserializationWarning>,
) {
    match RawFile::deserialize(b) {
        (Ok(raw_file), warnings) => {
            let file = from_raw_file(&raw_file);
            (Ok(file), warnings)
        }
        (Err(err), warnings) => (Err(err), warnings),
    }
}

pub(super) fn from_raw_file(raw_file: &RawFile) -> File {
    let char_infos: Vec<(Option<CharDimensions>, Option<CharTag>)> =
        deserialize_array(raw_file.char_infos);
    File {
        header: Header::deserialize(raw_file.header),
        smallest_char: raw_file.begin_char,
        char_dimens: (raw_file.begin_char.0..=raw_file.end_char.0)
            .zip(char_infos.iter())
            .filter_map(|(c, (d, _))| (d.clone().map(|d| (Char(c), d))))
            .collect(),
        char_tags: (raw_file.begin_char.0..=raw_file.end_char.0)
            .zip(char_infos.iter())
            .filter_map(|(c, (_, d))| (d.clone().map(|d| (Char(c), d))))
            .collect(),
        unset_char_tags: Default::default(),
        widths: deserialize_array(raw_file.widths),
        heights: deserialize_array(raw_file.heights),
        depths: deserialize_array(raw_file.depths),
        italic_corrections: deserialize_array(raw_file.italic_corrections),
        lig_kern_program: deserialize_lig_kern_program(raw_file.lig_kern_instructions),
        kerns: deserialize_array(raw_file.kerns),
        extensible_chars: deserialize_array(raw_file.extensible_recipes),
        params: deserialize_array(raw_file.params),
    }
}

/// Raw .tfm file.
pub struct RawFile<'a> {
    pub sub_file_sizes: SubFileSizes,
    pub raw_sub_file_sizes: &'a [u8],
    pub header: &'a [u8],
    pub begin_char: Char,
    pub end_char: Char,
    pub char_infos: &'a [u8],
    pub widths: &'a [u8],
    pub heights: &'a [u8],
    pub depths: &'a [u8],
    pub italic_corrections: &'a [u8],
    pub lig_kern_instructions: &'a [u8],
    pub kerns: &'a [u8],
    pub extensible_recipes: &'a [u8],
    pub params: &'a [u8],
}

/// Sub-file sizes in a .tfm file.
#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub struct SubFileSizes {
    /// Length of the file, in words.
    pub lf: i16,
    /// Length of the header data, in words.
    pub lh: i16,
    /// Smallest character code in the font.
    pub bc: i16,
    /// Largest character code in the font.
    pub ec: i16,
    /// Number of words in the width table.
    pub nw: i16,
    /// Number of words in the height table.
    pub nh: i16,
    /// Number of words in the depth table.
    pub nd: i16,
    /// Number of words in the italic correction table.
    pub ni: i16,
    /// Number of words in the lig/kern table.
    pub nl: i16,
    /// Number of words in the kern table.
    pub nk: i16,
    /// Number of words in the extensible character table.
    pub ne: i16,
    /// Number of font parameter words.
    pub np: i16,
}

impl SubFileSizes {
    /// Calculate the valid value of lf, given all of the other fields.
    pub fn valid_lf(&self) -> i16 {
        let s = self;
        6 + s.lh + (s.ec - s.bc + 1) + s.nw + s.nh + s.nd + s.ni + s.nl + s.nk + s.ne + s.np
    }
}

impl From<[u8; 24]> for SubFileSizes {
    fn from(value: [u8; 24]) -> Self {
        let d = |u: usize| -> i16 { i16::from_be_bytes([value[u], value[u + 1]]) };
        Self {
            lf: d(0),
            lh: d(2),
            bc: d(4),
            ec: d(6),
            nw: d(8),
            nh: d(10),
            nd: d(12),
            ni: d(14),
            nl: d(16),
            nk: d(18),
            ne: d(20),
            np: d(22),
        }
    }
}

impl From<SubFileSizes> for [u8; 24] {
    fn from(v: SubFileSizes) -> Self {
        let a = |u: i16| u.to_be_bytes()[0];
        let b = |u: i16| u.to_be_bytes()[1];
        [
            a(v.lf),
            b(v.lf),
            a(v.lh),
            b(v.lh),
            a(v.bc),
            b(v.bc),
            a(v.ec),
            b(v.ec),
            a(v.nw),
            b(v.nw),
            a(v.nh),
            b(v.nh),
            a(v.nd),
            b(v.nd),
            a(v.ni),
            b(v.ni),
            a(v.nl),
            b(v.nl),
            a(v.nk),
            b(v.nk),
            a(v.ne),
            b(v.ne),
            a(v.np),
            b(v.np),
        ]
    }
}

impl<'a> RawFile<'a> {
    pub fn from_debug_output(
        s: &str,
        keep_sub_file_sizes: bool,
    ) -> Result<Vec<u8>, (String, usize)> {
        debug::parse(s, keep_sub_file_sizes)
    }

    pub fn deserialize(
        b: &'a [u8],
    ) -> (
        Result<Self, DeserializationError>,
        Vec<DeserializationWarning>,
    ) {
        let lf = {
            let b0 = match b.first() {
                Some(b0) => *b0,
                None => return (Err(DeserializationError::FileIsEmpty), vec![]),
            };
            let b1 = match b.get(1) {
                Some(b1) => *b1,
                None => return (Err(DeserializationError::FileHasOneByte(b0)), vec![]),
            };
            i16::from_be_bytes([b0, b1])
        };
        let mut warnings = vec![];
        match lf {
            ..=-1 => {
                return (
                    Err(DeserializationError::InternalFileLengthIsNegative(lf)),
                    warnings,
                )
            }
            0 => {
                return (
                    Err(DeserializationError::InternalFileLengthIsZero),
                    warnings,
                )
            }
            1.. => {
                let actual_file_length = b.len();
                let claimed_file_length = (lf as usize) * 4;
                match actual_file_length.cmp(&claimed_file_length) {
                    std::cmp::Ordering::Less => {
                        return (
                            Err(DeserializationError::InternalFileLengthIsTooBig(
                                lf,
                                actual_file_length,
                            )),
                            warnings,
                        )
                    }
                    std::cmp::Ordering::Equal => (),
                    std::cmp::Ordering::Greater => warnings.push(
                        DeserializationWarning::InternalFileLengthIsSmall(lf, actual_file_length),
                    ),
                }
                if lf <= 3 {
                    return (
                        Err(DeserializationError::InternalFileLengthIsTooSmall(
                            lf,
                            actual_file_length,
                        )),
                        // TFtoPL doesn't output a warning here.
                        // Which makes sense because the error already encompasses the warning.
                        vec![],
                    );
                }
            }
        }
        let s: SubFileSizes = {
            let sb: [u8; 24] = b
                .get(0..24)
                .expect("3 < lf <= b.len()")
                .try_into()
                .expect("slice has 24 elements so fits in 24 length const array");
            sb.into()
        };

        if s.lh < 0
            || s.bc < 0
            || s.ec < 0
            || s.nw < 0
            || s.nh < 0
            || s.nd < 0
            || s.ni < 0
            || s.nl < 0
            || s.nk < 0
            || s.ne < 0
            || s.np < 0
        {
            return (
                Err(DeserializationError::SubFileSizeIsNegative(s.clone())),
                warnings,
            );
        }
        if s.lh < 2 {
            return (
                Err(DeserializationError::HeaderLengthIsTooSmall(s.lh)),
                warnings,
            );
        }
        let (bc, ec) = match s.bc.cmp(&s.ec.saturating_add(1)) {
            std::cmp::Ordering::Less => {
                let ec: u8 = match s.ec.try_into() {
                    Err(_) => {
                        return (
                            Err(DeserializationError::InvalidCharacterRange(s.bc, s.ec)),
                            warnings,
                        )
                    }
                    Ok(ec) => ec,
                };
                (
                    Char(s.bc.try_into().expect("bc<ec<=u8::MAX, so bc<=u8::MAX")),
                    Char(ec),
                )
            }
            std::cmp::Ordering::Equal => (Char(1), Char(0)),
            std::cmp::Ordering::Greater => {
                return (
                    Err(DeserializationError::InvalidCharacterRange(s.bc, s.ec)),
                    warnings,
                )
            }
        };
        if s.nw == 0 || s.nh == 0 || s.nd == 0 || s.ni == 0 {
            return (
                Err(DeserializationError::IncompleteSubFiles(s.clone())),
                warnings,
            );
        }
        if s.ne > 255 {
            return (
                Err(DeserializationError::TooManyExtensibleCharacters(s.ne)),
                warnings,
            );
        }
        if s.lf != s.valid_lf() {
            return (
                Err(DeserializationError::InconsistentSubFileSizes(s.clone())),
                warnings,
            );
        }

        (Ok(Self::finish_deserialization(b, s, bc, ec)), warnings)
    }

    pub(crate) fn finish_deserialization(b: &[u8], s: SubFileSizes, bc: Char, ec: Char) -> RawFile {
        let mut b = b;
        let mut get = |u: i16| {
            let u = (u as usize) * 4;
            let a = &b[..u];
            b = &b[u..];
            a
        };
        RawFile {
            raw_sub_file_sizes: get(6),
            header: get(s.lh),
            begin_char: bc,
            end_char: ec,
            char_infos: get(s.ec - s.bc + 1),
            widths: get(s.nw),
            heights: get(s.nh),
            depths: get(s.nd),
            italic_corrections: get(s.ni),
            lig_kern_instructions: get(s.nl),
            kerns: get(s.nk),
            extensible_recipes: get(s.ne),
            params: get(s.np),
            sub_file_sizes: s,
        }
    }
}

fn deserialize_string(b: &[u8]) -> Option<String> {
    b.first().map(|tfm_len| {
        match b.get(1..(*tfm_len as usize) + 1) {
            Some(b) => {
                b.iter().map(|u| *u as char).collect()
            },
            None => {
                let first_char = *b.get(1)
                    .expect("from the calling sites, b.len()E{0,20,40}, and b.len()>=1 because b.first().is_some()");
                // The string is truncated later in the validate function.
                format!("{}{}",first_char as char, " ".repeat((*tfm_len as usize)-2))
            }
        }
    })
}

impl Header {
    fn deserialize(mut b: &[u8]) -> Self {
        let checksum = Some(u32::deserialize(b));
        b = &b[4..];
        let design_size = Number::deserialize(b);
        b = b.get(4..).unwrap_or(&[0; 0]);
        let character_coding_scheme = deserialize_string(b.get(0..40).unwrap_or(&[0; 0]));
        b = b.get(40..).unwrap_or(&[0; 0]);
        let font_family = deserialize_string(b.get(0..20).unwrap_or(&[0; 0]));
        b = b.get(20..).unwrap_or(&[0; 0]);
        let seven_bit_safe = b.first().map(|b| *b > 127);
        let face = b.get(3).map(|b| (*b).into());
        let b = b.get(4..).unwrap_or(&[0; 0]);
        Self {
            checksum,
            design_size,
            design_size_valid: true,
            character_coding_scheme,
            font_family,
            seven_bit_safe,
            face,
            additional_data: deserialize_array(b),
        }
    }
}

fn deserialize_lig_kern_program(b: &[u8]) -> ligkern::lang::Program {
    let instructions: Vec<ligkern::lang::Instruction> = deserialize_array(b);
    let mut passthrough: HashSet<u16> = Default::default();
    // Boundary chars are deserialized in TFtoTPL.2014.69
    let boundary_char = match b.get(..2) {
        None => None,
        Some(b) => {
            if b[0] == 255 {
                passthrough.insert(0);
                Some(Char(b[1]))
            } else {
                None
            }
        }
    };
    let boundary_char_entrypoint = match b.len().checked_sub(4) {
        None => None,
        Some(r) => {
            let b = &b[r..];
            if b[0] == 255 {
                passthrough.insert(
                    (instructions.len() - 1)
                        .try_into()
                        .expect("cannot be more than u16::MAX instructions"),
                );
                let r = u16::from_be_bytes([b[2], b[3]]);
                Some(r)
            } else {
                None
            }
        }
    };
    ligkern::lang::Program {
        instructions,
        right_boundary_char: boundary_char,
        left_boundary_char_entrypoint: boundary_char_entrypoint,
        passthrough,
    }
}

fn deserialize_array<T: Deserializable>(mut b: &[u8]) -> Vec<T> {
    let mut r = vec![];
    while !b.is_empty() {
        r.push(T::deserialize(b));
        b = &b[4..]
    }
    r
}

/// Implementations of this trait can be deserialized from a 4-byte word.
trait Deserializable: Sized {
    fn deserialize(b: &[u8]) -> Self;
}

impl Deserializable for u32 {
    #[inline]
    fn deserialize(b: &[u8]) -> Self {
        u32::from_be_bytes([b[0], b[1], b[2], b[3]])
    }
}

impl Deserializable for Number {
    #[inline]
    fn deserialize(b: &[u8]) -> Self {
        Number(u32::deserialize(b) as i32)
    }
}

impl Deserializable for (Option<CharDimensions>, Option<CharTag>) {
    fn deserialize(b: &[u8]) -> Self {
        let info = match b[0].try_into() {
            Ok(n) => Some(CharDimensions {
                width_index: WidthIndex::Valid(n),
                height_index: b[1] / (1 << 4),
                depth_index: b[1] % (1 << 4),
                italic_index: b[2] / (1 << 2),
            }),
            Err(_) => None,
        };
        let tag = match b[2] % (1 << 2) {
            0 => None,
            1 => Some(CharTag::Ligature(b[3])),
            2 => Some(CharTag::List(Char(b[3]))),
            _ => Some(CharTag::Extension(b[3])),
        };
        (info, tag)
    }
}

impl Deserializable for ligkern::lang::Instruction {
    fn deserialize(b: &[u8]) -> Self {
        let (skip_byte, right_char, op_byte, remainder) = (b[0], Char(b[1]), b[2], b[3]);
        if skip_byte > 128 {
            return ligkern::lang::Instruction {
                next_instruction: None,
                right_char,
                operation: ligkern::lang::Operation::EntrypointRedirect(
                    u16::from_be_bytes([b[2], b[3]]),
                    true,
                ),
            };
        }
        ligkern::lang::Instruction {
            next_instruction: if skip_byte < 128 {
                Some(skip_byte)
            } else {
                None
            },
            right_char,
            operation: ligkern::lang::Operation::lig_kern_operation_from_bytes(op_byte, remainder),
        }
    }
}

impl Deserializable for ExtensibleRecipe {
    fn deserialize(b: &[u8]) -> Self {
        let char_or = |b: u8| {
            if b == 0 {
                None
            } else {
                Some(Char(b))
            }
        };
        ExtensibleRecipe {
            top: char_or(b[0]),
            middle: char_or(b[1]),
            bottom: char_or(b[2]),
            rep: Char(b[3]),
        }
    }
}

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

    macro_rules! deserialize_tests {
        ( $( ($name: ident, $input: expr, $want: expr $( , $warning: expr )? ), )+ ) => {
            $(
                mod $name {
                    use super::*;
                    #[test]
                    fn deserialize_test() {
                        let input = $input;
                        let want = $want;
                        let got = deserialize(&input);
                        let warnings = vec![ $( $warning )? ];
                        assert_eq!(got, (want, warnings));
                    }
                }
            )+
        };
    }

    macro_rules! serde_tests {
        ( $( ($name: ident, $bytes: expr, $file: expr $(,)? ), )+ ) => {
            $(
                mod $name {
                    use super::*;
                    #[test]
                    fn deserialize_test() {
                        let input = $bytes;
                        let want = $file;
                        let got = deserialize(&input);
                        assert_eq!(got, (Ok(want), vec![]));
                    }
                    #[test]
                    fn serialize_test() {
                        let input = $file;
                        let want = canonical_tfm_bytes($bytes);
                        let got = input.serialize();
                        assert_eq!(got, want);
                    }
                }
            )+
        };
    }

    deserialize_tests!(
        (empty_file, [], Err(DeserializationError::FileIsEmpty)),
        (
            single_byte_1,
            [2],
            Err(DeserializationError::FileHasOneByte(2))
        ),
        (
            single_byte_2,
            [255],
            Err(DeserializationError::FileHasOneByte(255))
        ),
        (
            internal_file_length_is_negative,
            [255, 0],
            Err(DeserializationError::InternalFileLengthIsNegative(-256))
        ),
        (
            internal_file_length_is_zero,
            [0, 0, 1, 1],
            Err(DeserializationError::InternalFileLengthIsZero)
        ),
        (
            internal_file_length_is_too_big,
            [0, 2, 1, 1],
            Err(DeserializationError::InternalFileLengthIsTooBig(2, 4))
        ),
        (
            internal_file_length_is_too_small,
            extend(&[0, 2, 255, 0], 24),
            Err(DeserializationError::InternalFileLengthIsTooSmall(2, 24))
        ),
        (
            tfm_file_contains_only_sub_file_sizes,
            extend(&[0, 3, 0, 0], 12),
            Err(DeserializationError::InternalFileLengthIsTooSmall(3, 12))
        ),
        (
            sub_file_size_too_small,
            extend(&[0, 6, 255, 0], 24),
            Err(DeserializationError::SubFileSizeIsNegative(SubFileSizes {
                lf: 6,
                lh: -256,
                ..Default::default()
            }))
        ),
        (
            header_length_too_small_0,
            extend(&[0, 6, 0, 0], 24),
            Err(DeserializationError::HeaderLengthIsTooSmall(0))
        ),
        (
            header_length_too_small_1,
            extend(&[0, 6, 0, 1], 24),
            Err(DeserializationError::HeaderLengthIsTooSmall(1))
        ),
        (
            invalid_character_range_1,
            extend(&[0, 6, 0, 2, 0, 2, 0, 0], 24),
            Err(DeserializationError::InvalidCharacterRange(2, 0))
        ),
        (
            invalid_character_range_2,
            extend(&[0, 6, 0, 2, 0, 2, 1, 0], 24),
            Err(DeserializationError::InvalidCharacterRange(2, 256))
        ),
        (
            incomplete_sub_files,
            extend(
                &[
                    /* lf */ 0, 6, /* lh */ 0, 2, /* bc */ 0, 1, /* ec */ 0, 2,
                    /* nw */ 0, 3, /* nh */ 0, 4, /* nd */ 0, 5, /* ni */ 0, 0,
                ],
                24
            ),
            Err(DeserializationError::IncompleteSubFiles(SubFileSizes {
                lf: 6,
                lh: 2,
                bc: 1,
                ec: 2,
                nw: 3,
                nh: 4,
                nd: 5,
                ..Default::default()
            }))
        ),
        (
            too_many_extensible_characters,
            extend(
                &[
                    /* lf */ 0, 6, /* lh */ 0, 2, /* bc */ 0, 1, /* ec */ 0, 2,
                    /* nw */ 0, 3, /* nh */ 0, 4, /* nd */ 0, 5, /* ni */ 0, 6,
                    /* nl */ 0, 0, /* nk */ 0, 0, /* ne */ 1, 1, /* np */ 0, 0,
                ],
                24
            ),
            Err(DeserializationError::TooManyExtensibleCharacters(257))
        ),
        (
            inconsistent_sub_file_sizes,
            extend(
                &[
                    /* lf */ 0, 6, /* lh */ 0, 2, /* bc */ 0, 3, /* ec */ 0, 4,
                    /* nw */ 0, 5, /* nh */ 0, 6, /* nd */ 0, 7, /* ni */ 0, 8,
                    /* nl */ 0, 9, /* nk */ 0, 10, /* ne */ 0, 11, /* np */ 0,
                    12,
                ],
                24
            ),
            Err(DeserializationError::InconsistentSubFileSizes(
                SubFileSizes {
                    lf: 6,
                    lh: 2,
                    bc: 3,
                    ec: 4,
                    nw: 5,
                    nh: 6,
                    nd: 7,
                    ni: 8,
                    nl: 9,
                    nk: 10,
                    ne: 11,
                    np: 12,
                }
            ))
        ),
        (
            corrupt_strings_in_header,
            build_from_header(&[
                /* checksum */ 0, 0, 0, 7, /* design_size */ 0, 0, 0, 11,
                /* character_coding_scheme */ 240, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
                65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
                65, 65, 65, 65, 65, 65, 65, /* font_family */ 100, 66, 66, 66, 66, 66, 66, 66,
                66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, /* seven_bit_safe */ 0, 0, 0,
                /* face */ 61,
            ]),
            Ok(File {
                header: Header {
                    checksum: Some(7),
                    design_size: Number(11),
                    design_size_valid: true,
                    character_coding_scheme: Some(format!["A{}", " ".repeat(238)]),
                    font_family: Some(format!["B{}", " ".repeat(98)]),
                    seven_bit_safe: Some(false),
                    face: Some(Face::Other(61)),
                    additional_data: vec![],
                },
                ..Default::default()
            },)
        ),
        (
            file_longer_than_expected,
            extend(
                &vec![
                    /* lf */ 0, 12, /* lh */ 0, 2, /* bc */ 0, 1, /* ec */ 0,
                    0, /* nw */ 0, 1, /* nh */ 0, 1, /* nd */ 0, 1, /* ni */ 0,
                    1, /* nl */ 0, 0, /* nk */ 0, 0, /* ne */ 0, 0, /* np */ 0,
                    0, /* header.checksum */ 0, 0, 0, 0, /* header.design_size */ 0, 0,
                    0, 0,
                ],
                40 * 4
            ),
            Ok(File {
                header: Header::tfm_default(),
                ..Default::default()
            }),
            DeserializationWarning::InternalFileLengthIsSmall(12, 40 * 4)
        ),
        (
            lig_kern_invalid_lig_tag,
            tfm_bytes_with_one_lig_kern_command([128, 8, 70, 23]),
            Ok(tfm_file_with_one_lig_kern_instruction(
                ligkern::lang::Instruction {
                    next_instruction: None,
                    right_char: Char(8),
                    operation: ligkern::lang::Operation::Ligature {
                        char_to_insert: Char(23),
                        post_lig_operation:
                            ligkern::lang::PostLigOperation::RetainNeitherMoveToInserted,
                        post_lig_tag_invalid: true,
                    },
                }
            ))
        ),
    );

    serde_tests!(
        (
            minimal_header,
            build_from_header(&[/* checksum */ 0, 0, 0, 7, /* design_size */ 0, 0, 0, 11,]),
            File {
                header: Header {
                    checksum: Some(7),
                    design_size: Number(11),
                    design_size_valid: true,
                    character_coding_scheme: None,
                    font_family: None,
                    seven_bit_safe: None,
                    face: None,
                    additional_data: vec![],
                },
                ..Default::default()
            },
        ),
        (
            full_header,
            build_from_header(&[
                /* checksum */ 0, 0, 0, 7, /* design_size */ 0, 0, 0, 11,
                /* character_coding_scheme */ 3, 65, 66, 67, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                /* font_family */ 3, 68, 69, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, /* seven_bit_safe */ 128, 0, 0, /* face */ 9,
                /* additional_data */ 0, 0, 0, 13,
            ]),
            File {
                header: Header {
                    checksum: Some(7),
                    design_size: Number(11),
                    design_size_valid: true,
                    character_coding_scheme: Some("ABC".into()),
                    font_family: Some("DEF".into()),
                    seven_bit_safe: Some(true),
                    face: Some(Face::Valid(
                        FaceWeight::Bold,
                        FaceSlope::Italic,
                        FaceExpansion::Condensed
                    )),
                    additional_data: vec![13],
                },
                ..Default::default()
            },
        ),
        (
            char_infos,
            extend(
                &vec![
                    /* lf */ 0, 29, /* lh */ 0, 2, /* bc */ 0, 70, /* ec */ 0,
                    72, /* nw */ 0, 6, /* nh */ 0, 3, /* nd */ 0, 4,
                    /* ni */ 0, 5, /* nl */ 0, 0, /* nk */ 0, 0, /* ne */ 0, 0,
                    /* np */ 0, 0, /* header.checksum */ 0, 0, 0, 0,
                    /* header.design_size */ 0, 0, 0, 0, /* char_infos */ 5, 35, 16, 0,
                    0, 0, 0, 0, 1, 0, 1, 23, /* widths */
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4,
                    /* heights */
                    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, /* depths */
                    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, /* italic_corrections */
                    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4,
                ],
                15 * 4
            ),
            File {
                header: Header::tfm_default(),
                smallest_char: Char(70),
                char_dimens: BTreeMap::from([
                    (
                        Char(70),
                        CharDimensions {
                            width_index: WidthIndex::Valid(5.try_into().unwrap()),
                            height_index: 2,
                            depth_index: 3,
                            italic_index: 4,
                        }
                    ),
                    (
                        Char(72),
                        CharDimensions {
                            width_index: WidthIndex::Valid(1.try_into().unwrap()),
                            height_index: 0,
                            depth_index: 0,
                            italic_index: 0,
                        }
                    ),
                ]),
                char_tags: BTreeMap::from([(Char(72), CharTag::Ligature(23)),]),
                widths: vec![
                    Number(0),
                    Number(0),
                    Number(1),
                    Number(2),
                    Number(3),
                    Number(4)
                ],
                heights: vec![Number(0), Number(1), Number(2)],
                depths: vec![Number(0), Number(1), Number(2), Number(3)],
                italic_corrections: vec![Number(0), Number(1), Number(2), Number(3), Number(4)],
                ..Default::default()
            },
        ),
        (
            char_infos_large_char,
            extend(
                &vec![
                    /* lf */ 0, 27, /* lh */ 0, 2, /* bc */ 0, 255, /* ec */ 0,
                    255, /* nw */ 0, 6, /* nh */ 0, 3, /* nd */ 0, 4,
                    /* ni */ 0, 5, /* nl */ 0, 0, /* nk */ 0, 0, /* ne */ 0, 0,
                    /* np */ 0, 0, /* header.checksum */ 0, 0, 0, 0,
                    /* header.design_size */ 0, 0, 0, 0, /* char_infos */ 5, 35, 16, 0,
                    /* widths */
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4,
                    /* heights */
                    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, /* depths */
                    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, /* italic_corrections */
                    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4,
                ],
                13 * 4
            ),
            File {
                header: Header::tfm_default(),
                smallest_char: Char(255),
                char_dimens: BTreeMap::from([(
                    Char(255),
                    CharDimensions {
                        width_index: WidthIndex::Valid(5.try_into().unwrap()),
                        height_index: 2,
                        depth_index: 3,
                        italic_index: 4,
                    }
                ),]),
                widths: vec![
                    Number(0),
                    Number(0),
                    Number(1),
                    Number(2),
                    Number(3),
                    Number(4)
                ],
                heights: vec![Number(0), Number(1), Number(2)],
                depths: vec![Number(0), Number(1), Number(2), Number(3)],
                italic_corrections: vec![Number(0), Number(1), Number(2), Number(3), Number(4)],
                ..Default::default()
            },
        ),
        (
            widths_heights_depths_italic_corrections_kerns,
            vec![
                /* lf */ 0, 17, /* lh */ 0, 2, /* bc */ 0, 1, /* ec */ 0, 0,
                /* nw */ 0, 2, /* nh */ 0, 2, /* nd */ 0, 2, /* ni */ 0, 2,
                /* nl */ 0, 0, /* nk */ 0, 1, /* ne */ 0, 0, /* np */ 0, 0,
                /* header.checksum */ 0, 0, 0, 0, /* header.design_size */ 0, 0, 0, 0,
                /* widths */ 0, 0, 0, 0, 0, 0, 0, 23, /* heights */ 0, 0, 0, 0, 0, 0, 0,
                29, /* depths */ 0, 0, 0, 0, 0, 0, 0, 31, /* italic_corrections */ 0, 0,
                0, 0, 0, 0, 0, 37, /* lig_kern_commands */
                /* kerns */ 0, 0, 0, 37
            ],
            File {
                header: Header::tfm_default(),
                widths: vec![Number::ZERO, Number(23)],
                heights: vec![Number::ZERO, Number(29)],
                depths: vec![Number::ZERO, Number(31)],
                italic_corrections: vec![Number::ZERO, Number(37)],
                kerns: vec![Number(37)],
                ..Default::default()
            },
        ),
        (
            lig_kern_command_1,
            tfm_bytes_with_one_lig_kern_command([0, 5, 130, 13]),
            tfm_file_with_one_lig_kern_instruction(ligkern::lang::Instruction {
                next_instruction: Some(0),
                right_char: Char(5),
                operation: ligkern::lang::Operation::KernAtIndex(256 * 2 + 13)
            }),
        ),
        (
            lig_kern_command_2,
            tfm_bytes_with_one_lig_kern_command([0, 6, 3, 17]),
            tfm_file_with_one_lig_kern_instruction(ligkern::lang::Instruction {
                next_instruction: Some(0),
                right_char: Char(6),
                operation: ligkern::lang::Operation::Ligature {
                    char_to_insert: Char(17),
                    post_lig_operation: ligkern::lang::PostLigOperation::RetainBothMoveNowhere,
                    post_lig_tag_invalid: false,
                },
            }),
        ),
        (
            lig_kern_command_3,
            tfm_bytes_with_one_lig_kern_command([0, 7, 3 + 4, 19]),
            tfm_file_with_one_lig_kern_instruction(ligkern::lang::Instruction {
                next_instruction: Some(0),
                right_char: Char(7),
                operation: ligkern::lang::Operation::Ligature {
                    char_to_insert: Char(19),
                    post_lig_operation: ligkern::lang::PostLigOperation::RetainBothMoveToInserted,
                    post_lig_tag_invalid: false,
                },
            }),
        ),
        (
            lig_kern_command_4,
            tfm_bytes_with_one_lig_kern_command([128, 8, 3 + 8, 23]),
            tfm_file_with_one_lig_kern_instruction(ligkern::lang::Instruction {
                next_instruction: None,
                right_char: Char(8),
                operation: ligkern::lang::Operation::Ligature {
                    char_to_insert: Char(23),
                    post_lig_operation: ligkern::lang::PostLigOperation::RetainBothMoveToRight,
                    post_lig_tag_invalid: false,
                },
            }),
        ),
        (
            lig_kern_command_5,
            tfm_bytes_with_one_lig_kern_command([128, 8, 1, 23]),
            tfm_file_with_one_lig_kern_instruction(ligkern::lang::Instruction {
                next_instruction: None,
                right_char: Char(8),
                operation: ligkern::lang::Operation::Ligature {
                    char_to_insert: Char(23),
                    post_lig_operation: ligkern::lang::PostLigOperation::RetainRightMoveToInserted,
                    post_lig_tag_invalid: false,
                },
            }),
        ),
        (
            lig_kern_command_6,
            tfm_bytes_with_one_lig_kern_command([128, 8, 1 + 4, 23]),
            tfm_file_with_one_lig_kern_instruction(ligkern::lang::Instruction {
                next_instruction: None,
                right_char: Char(8),
                operation: ligkern::lang::Operation::Ligature {
                    char_to_insert: Char(23),
                    post_lig_operation: ligkern::lang::PostLigOperation::RetainRightMoveToRight,
                    post_lig_tag_invalid: false,
                },
            }),
        ),
        (
            lig_kern_command_7,
            tfm_bytes_with_one_lig_kern_command([128, 8, 2, 23]),
            tfm_file_with_one_lig_kern_instruction(ligkern::lang::Instruction {
                next_instruction: None,
                right_char: Char(8),
                operation: ligkern::lang::Operation::Ligature {
                    char_to_insert: Char(23),
                    post_lig_operation: ligkern::lang::PostLigOperation::RetainLeftMoveNowhere,
                    post_lig_tag_invalid: false,
                },
            }),
        ),
        (
            lig_kern_command_8,
            tfm_bytes_with_one_lig_kern_command([128, 8, 2 + 4, 23]),
            tfm_file_with_one_lig_kern_instruction(ligkern::lang::Instruction {
                next_instruction: None,
                right_char: Char(8),
                operation: ligkern::lang::Operation::Ligature {
                    char_to_insert: Char(23),
                    post_lig_operation: ligkern::lang::PostLigOperation::RetainLeftMoveToInserted,
                    post_lig_tag_invalid: false,
                },
            }),
        ),
        (
            lig_kern_command_9,
            tfm_bytes_with_one_lig_kern_command([128, 8, 0, 23]),
            tfm_file_with_one_lig_kern_instruction(ligkern::lang::Instruction {
                next_instruction: None,
                right_char: Char(8),
                operation: ligkern::lang::Operation::Ligature {
                    char_to_insert: Char(23),
                    post_lig_operation:
                        ligkern::lang::PostLigOperation::RetainNeitherMoveToInserted,
                    post_lig_tag_invalid: false,
                },
            }),
        ),
        (
            lig_kern_command_special,
            tfm_bytes_with_one_lig_kern_command([254, 0, 1, 2]),
            tfm_file_with_one_lig_kern_instruction(ligkern::lang::Instruction {
                next_instruction: None,
                right_char: Char(0),
                operation: ligkern::lang::Operation::EntrypointRedirect(
                    u16::from_be_bytes([1, 2]),
                    true,
                ),
            }),
        ),
        (
            extensible_chars,
            vec![
                /* lf */ 0, 13, /* lh */ 0, 2, /* bc */ 0, 1, /* ec */ 0, 0,
                /* nw */ 0, 1, /* nh */ 0, 1, /* nd */ 0, 1, /* ni */ 0, 1,
                /* nl */ 0, 0, /* nk */ 0, 0, /* ne */ 0, 1, /* np */ 0, 0,
                /* header.checksum */ 0, 0, 0, 0, /* header.design_size */ 0, 0, 0, 0,
                /* widths */ 0, 0, 0, 0, /* heights */ 0, 0, 0, 0, /* depths */ 0,
                0, 0, 0, /* italic_corrections */ 0, 0, 0, 0, /* lig_kern_commands */
                /* kerns */ /* extensible_chars */ 17, 19, 23, 27
            ],
            File {
                header: Header::tfm_default(),
                extensible_chars: vec![ExtensibleRecipe {
                    top: Some(Char(17)),
                    middle: Some(Char(19)),
                    bottom: Some(Char(23)),
                    rep: Char(27),
                }],
                ..Default::default()
            },
        ),
        (
            params,
            vec![
                /* lf */ 0, 22, /* lh */ 0, 2, /* bc */ 0, 1, /* ec */ 0, 0,
                /* nw */ 0, 1, /* nh */ 0, 1, /* nd */ 0, 1, /* ni */ 0, 1,
                /* nl */ 0, 0, /* nk */ 0, 0, /* ne */ 0, 0, /* np */ 0, 10,
                /* header.checksum */ 0, 0, 0, 0, /* header.design_size */ 0, 0, 0, 0,
                /* widths */ 0, 0, 0, 0, /* heights */ 0, 0, 0, 0, /* depths */ 0,
                0, 0, 0, /* italic_corrections */ 0, 0, 0, 0, /* lig_kern_commands */
                /* kerns */ /* extensible_chars */ /* params */
                0, 0, 0, 11, 0, 0, 0, 13, 0, 0, 0, 17, 0, 0, 0, 19, 0, 0, 0, 23, 0, 0, 0, 29, 0, 0,
                0, 31, 0, 0, 0, 37, 0, 0, 0, 41, 0, 0, 0, 43,
            ],
            File {
                header: Header::tfm_default(),
                params: vec![
                    Number(11),
                    Number(13),
                    Number(17),
                    Number(19),
                    Number(23),
                    Number(29),
                    Number(31),
                    Number(37),
                    Number(41),
                    Number(43),
                ],
                ..Default::default()
            },
        ),
    );

    fn extend(input: &[u8], size: usize) -> Vec<u8> {
        let mut v: Vec<u8> = input.into();
        for _ in input.len()..size {
            v.push(0)
        }
        v
    }

    fn tfm_file_with_one_lig_kern_instruction(instruction: ligkern::lang::Instruction) -> File {
        File {
            header: Header::tfm_default(),
            lig_kern_program: ligkern::lang::Program {
                instructions: vec![
                    instruction,
                    ligkern::lang::Instruction {
                        next_instruction: None,
                        right_char: Char(0),
                        operation: ligkern::lang::Operation::KernAtIndex(0),
                    },
                ],
                right_boundary_char: None,
                left_boundary_char_entrypoint: None,
                passthrough: Default::default(),
            },
            ..Default::default()
        }
    }

    fn tfm_bytes_with_one_lig_kern_command(lig_kern_command: [u8; 4]) -> Vec<u8> {
        let mut v = vec![
            /* lf */ 0, 14, /* lh */ 0, 2, /* bc */ 0, 1, /* ec */ 0, 0,
            /* nw */ 0, 1, /* nh */ 0, 1, /* nd */ 0, 1, /* ni */ 0, 1,
            /* nl */ 0, 2, /* nk */ 0, 0, /* ne */ 0, 0, /* np */ 0, 0,
            /* header.checksum */ 0, 0, 0, 0, /* header.design_size */ 0, 0, 0, 0,
            /* widths */ 0, 0, 0, 0, /* heights */ 0, 0, 0, 0, /* depths */ 0, 0, 0,
            0, /* italic_corrections */ 0, 0, 0, 0,
        ];
        /* lig_kern_commands */
        v.extend(lig_kern_command);
        v.extend([128, 0, 128, 0]);
        v
    }

    fn build_from_header(header: &[u8]) -> Vec<u8> {
        assert_eq!(header.len() % 4, 0);
        let num_words: i16 = (header.len() / 4).try_into().unwrap();
        let lf: u8 = (6 + num_words + 4).try_into().unwrap();
        let lh: u8 = num_words.try_into().unwrap();
        let mut v: Vec<u8> = vec![
            /* lf */ 0, lf, /* lh */ 0, lh, /* bc */ 0, 1, /* ec */ 0, 0,
            /* nw */ 0, 1, /* nh */ 0, 1, /* nd */ 0, 1, /* ni */ 0, 1,
            /* nl */ 0, 0, /* nk */ 0, 0, /* ne */ 0, 0, /* np */ 0, 0,
        ];
        v.extend(header);
        v.extend(&[0_u8; 16]); // the widths etc.
        v
    }

    // This function pads out the header with 0s so that it always contains at least 18 words.
    fn canonical_tfm_bytes(b: Vec<u8>) -> Vec<u8> {
        let lh = u16::from_be_bytes([b[2], b[3]]);
        if lh >= 18 {
            return b;
        }
        let mut lf = u16::from_be_bytes([b[0], b[1]]);
        lf += 18 - lh;
        let boundary = 24 + (lh as usize) * 4;
        let mut v: Vec<u8> = lf.to_be_bytes().into();
        v.extend(&[0, 18]);
        v.extend(&b[4..boundary]);
        v.resize(24 + 18 * 4, 0);
        v.extend(&b[boundary..]);
        v
    }
}