write-fonts 0.55.0

Writing font files.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
// THIS FILE IS AUTOGENERATED.
// Any changes to this file will be overwritten.
// For more information about how codegen works, see font-codegen/README.md

#[allow(unused_imports)]
use crate::codegen_prelude::*;

pub use read_fonts::tables::math::PartFlags;

/// The [MATH](https://learn.microsoft.com/en-us/typography/opentype/spec/math) table
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Math {
    /// Offset to MathConstants table, from beginning of MATH table.
    pub math_constants: OffsetMarker<MathConstants>,
    /// Offset to MathGlyphInfo table, from beginning of MATH table.
    pub math_glyph_info: OffsetMarker<MathGlyphInfo>,
    /// Offset to MathVariants table, from beginning of MATH table.
    pub math_variants: OffsetMarker<MathVariants>,
}

impl Math {
    /// Construct a new `Math`
    pub fn new(
        math_constants: MathConstants,
        math_glyph_info: MathGlyphInfo,
        math_variants: MathVariants,
    ) -> Self {
        Self {
            math_constants: math_constants.into(),
            math_glyph_info: math_glyph_info.into(),
            math_variants: math_variants.into(),
        }
    }
}

impl FontWrite for Math {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        let version = MajorMinor::VERSION_1_0 as MajorMinor;
        version.write_into(writer);
        self.math_constants.write_into(writer);
        self.math_glyph_info.write_into(writer);
        self.math_variants.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::TopLevel(Math::TAG)
    }
}

impl Validate for Math {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        ctx.in_table("Math", |ctx| {
            ctx.in_field("math_constants", |ctx| {
                self.math_constants.validate_impl(ctx);
            });
            ctx.in_field("math_glyph_info", |ctx| {
                self.math_glyph_info.validate_impl(ctx);
            });
            ctx.in_field("math_variants", |ctx| {
                self.math_variants.validate_impl(ctx);
            });
        })
    }
}

impl TopLevelTable for Math {
    const TAG: Tag = Tag::new(b"MATH");
}

impl<'a> FromObjRef<read_fonts::tables::math::Math<'a>> for Math {
    fn from_obj_ref(obj: &read_fonts::tables::math::Math<'a>, _: FontData) -> Self {
        Math {
            math_constants: obj.math_constants().to_owned_table(),
            math_glyph_info: obj.math_glyph_info().to_owned_table(),
            math_variants: obj.math_variants().to_owned_table(),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::tables::math::Math<'a>> for Math {}

impl ReadArgs for Math {
    type Args = ();
}

impl<'a> FontRead<'a> for Math {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        <read_fonts::tables::math::Math as FontRead>::read(data).map(|x| x.to_owned_table())
    }
}

/// [MathValueRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/math#mathvaluerecord)
///
/// The device offset is measured from the beginning of the table that contains
/// the record, not from the record itself.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MathValueRecord {
    /// The X or Y value in design units.
    pub value: FWord,
    /// Offset to the Device table (non-variable font) / VariationIndex table
    /// (variable font) for this value, from the beginning of the parent table.
    /// May be NULL.
    pub device: NullableOffsetMarker<DeviceOrVariationIndex>,
}

impl MathValueRecord {
    /// Construct a new `MathValueRecord`
    pub fn new(value: FWord, device: Option<DeviceOrVariationIndex>) -> Self {
        Self {
            value,
            device: device.into(),
        }
    }
}

impl FontWrite for MathValueRecord {
    fn write_into(&self, writer: &mut TableWriter) {
        self.value.write_into(writer);
        self.device.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("MathValueRecord")
    }
}

impl Validate for MathValueRecord {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        ctx.in_table("MathValueRecord", |ctx| {
            ctx.in_field("device", |ctx| {
                self.device.validate_impl(ctx);
            });
        })
    }
}

impl FromObjRef<read_fonts::tables::math::MathValueRecord> for MathValueRecord {
    fn from_obj_ref(
        obj: &read_fonts::tables::math::MathValueRecord,
        offset_data: FontData,
    ) -> Self {
        MathValueRecord {
            value: obj.value(),
            device: obj.device(offset_data).to_owned_table(),
        }
    }
}

/// [MathConstants](https://learn.microsoft.com/en-us/typography/opentype/spec/math#mathconstants-table)
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MathConstants {
    /// Percentage of scaling down for level 1 superscripts and subscripts.
    pub script_percent_scale_down: i16,
    /// Percentage of scaling down for level 2 (scriptScript) superscripts and
    /// subscripts.
    pub script_script_percent_scale_down: i16,
    /// Minimum height required for a delimited expression (contained within
    /// parentheses, etc.) to be treated as a sub-formula.
    pub delimited_sub_formula_min_height: UfWord,
    /// Minimum height of n-ary operators (such as integral and summation) for
    /// formulas in display mode (that is, appearing as standalone page
    /// elements, not embedded inline within text).
    pub display_operator_min_height: UfWord,
    /// White space to be left between math formulas to ensure proper line
    /// spacing.
    pub math_leading: MathValueRecord,
    /// Axis height of the font.
    pub axis_height: MathValueRecord,
    /// Maximum (ink) height of accent base that does not require raising the
    /// accents.
    pub accent_base_height: MathValueRecord,
    /// Maximum (ink) height of accent base that does not require flattening the
    /// accents.
    pub flattened_accent_base_height: MathValueRecord,
    /// The standard shift down applied to subscript elements.
    pub subscript_shift_down: MathValueRecord,
    /// Maximum allowed height of the (ink) top of subscripts that does not
    /// require moving subscripts further down.
    pub subscript_top_max: MathValueRecord,
    /// Minimum allowed drop of the baseline of subscripts relative to the (ink)
    /// bottom of the base.
    pub subscript_baseline_drop_min: MathValueRecord,
    /// Standard shift up applied to superscript elements.
    pub superscript_shift_up: MathValueRecord,
    /// Standard shift of superscripts relative to the base, in cramped style.
    pub superscript_shift_up_cramped: MathValueRecord,
    /// Minimum allowed height of the (ink) bottom of superscripts that does not
    /// require moving subscripts further up.
    pub superscript_bottom_min: MathValueRecord,
    /// Maximum allowed drop of the baseline of superscripts relative to the
    /// (ink) top of the base.
    pub superscript_baseline_drop_max: MathValueRecord,
    /// Minimum gap between the superscript and subscript ink.
    pub sub_superscript_gap_min: MathValueRecord,
    /// The maximum level to which the (ink) bottom of superscript can be
    /// pushed to increase the gap between superscript and subscript, before
    /// subscript starts being moved down.
    pub superscript_bottom_max_with_subscript: MathValueRecord,
    /// Extra white space to be added after each subscript and superscript.
    pub space_after_script: MathValueRecord,
    /// Minimum gap between the (ink) bottom of the upper limit, and the (ink)
    /// top of the base operator.
    pub upper_limit_gap_min: MathValueRecord,
    /// Minimum distance between baseline of upper limit and (ink) top of the
    /// base operator.
    pub upper_limit_baseline_rise_min: MathValueRecord,
    /// Minimum gap between (ink) top of the lower limit, and (ink) bottom of
    /// the base operator.
    pub lower_limit_gap_min: MathValueRecord,
    /// Minimum distance between baseline of the lower limit and (ink) bottom of
    /// the base operator.
    pub lower_limit_baseline_drop_min: MathValueRecord,
    /// Standard shift up applied to the top element of a stack.
    pub stack_top_shift_up: MathValueRecord,
    /// Standard shift up applied to the top element of a stack in display
    /// style.
    pub stack_top_display_style_shift_up: MathValueRecord,
    /// Standard shift down applied to the bottom element of a stack.
    pub stack_bottom_shift_down: MathValueRecord,
    /// Standard shift down applied to the bottom element of a stack in display
    /// style.
    pub stack_bottom_display_style_shift_down: MathValueRecord,
    /// Minimum gap between (ink) bottom of the top element of a stack, and the
    /// (ink) top of the bottom element.
    pub stack_gap_min: MathValueRecord,
    /// Minimum gap between (ink) bottom of the top element of a stack, and the
    /// (ink) top of the bottom element in display style.
    pub stack_display_style_gap_min: MathValueRecord,
    /// Standard shift up applied to the top element of the stretch stack.
    pub stretch_stack_top_shift_up: MathValueRecord,
    /// Standard shift down applied to the bottom element of the stretch stack.
    pub stretch_stack_bottom_shift_down: MathValueRecord,
    /// Minimum gap between the ink of the stretched element, and the (ink)
    /// bottom of the element above.
    pub stretch_stack_gap_above_min: MathValueRecord,
    /// Minimum gap between the ink of the stretched element, and the (ink) top
    /// of the element below.
    pub stretch_stack_gap_below_min: MathValueRecord,
    /// Standard shift up applied to the numerator.
    pub fraction_numerator_shift_up: MathValueRecord,
    /// Standard shift up applied to the numerator in display style.
    pub fraction_numerator_display_style_shift_up: MathValueRecord,
    /// Standard shift down applied to the denominator.
    pub fraction_denominator_shift_down: MathValueRecord,
    /// Standard shift down applied to the denominator in display style.
    pub fraction_denominator_display_style_shift_down: MathValueRecord,
    /// Minimum tolerated gap between the (ink) bottom of the numerator and the
    /// ink of the fraction bar.
    pub fraction_numerator_gap_min: MathValueRecord,
    /// Minimum tolerated gap between the (ink) bottom of the numerator and the
    /// ink of the fraction bar in display style.
    pub fraction_num_display_style_gap_min: MathValueRecord,
    /// Thickness of the fraction bar.
    pub fraction_rule_thickness: MathValueRecord,
    /// Minimum tolerated gap between the ink of the fraction bar and the (ink)
    /// top of the denominator.
    pub fraction_denominator_gap_min: MathValueRecord,
    /// Minimum tolerated gap between the ink of the fraction bar and the (ink)
    /// top of the denominator in display style.
    pub fraction_denom_display_style_gap_min: MathValueRecord,
    /// Horizontal distance between the top and bottom elements of a skewed
    /// fraction.
    pub skewed_fraction_horizontal_gap: MathValueRecord,
    /// Vertical distance between the ink of the top and bottom elements of a
    /// skewed fraction.
    pub skewed_fraction_vertical_gap: MathValueRecord,
    /// Distance between the overbar and the (ink) top of he base.
    pub overbar_vertical_gap: MathValueRecord,
    /// Thickness of overbar.
    pub overbar_rule_thickness: MathValueRecord,
    /// Extra white space reserved above the overbar.
    pub overbar_extra_ascender: MathValueRecord,
    /// Distance between underbar and (ink) bottom of the base.
    pub underbar_vertical_gap: MathValueRecord,
    /// Thickness of underbar.
    pub underbar_rule_thickness: MathValueRecord,
    /// Extra white space reserved below the underbar.
    pub underbar_extra_descender: MathValueRecord,
    /// Space between the (ink) top of the expression and the bar over it.
    pub radical_vertical_gap: MathValueRecord,
    /// Space between the (ink) top of the expression and the bar over it in
    /// display style.
    pub radical_display_style_vertical_gap: MathValueRecord,
    /// Thickness of the radical rule.
    pub radical_rule_thickness: MathValueRecord,
    /// Extra white space reserved above the radical.
    pub radical_extra_ascender: MathValueRecord,
    /// Extra horizontal kern before the degree of a radical, if such is
    /// present.
    pub radical_kern_before_degree: MathValueRecord,
    /// Negative kern after the degree of a radical, if such is present.
    pub radical_kern_after_degree: MathValueRecord,
    /// Height of the bottom of the radical degree, if such is present, in
    /// proportion to the ascender of the radical sign.
    pub radical_degree_bottom_raise_percent: i16,
}

impl MathConstants {
    /// Construct a new `MathConstants`
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        script_percent_scale_down: i16,
        script_script_percent_scale_down: i16,
        delimited_sub_formula_min_height: UfWord,
        display_operator_min_height: UfWord,
        math_leading: MathValueRecord,
        axis_height: MathValueRecord,
        accent_base_height: MathValueRecord,
        flattened_accent_base_height: MathValueRecord,
        subscript_shift_down: MathValueRecord,
        subscript_top_max: MathValueRecord,
        subscript_baseline_drop_min: MathValueRecord,
        superscript_shift_up: MathValueRecord,
        superscript_shift_up_cramped: MathValueRecord,
        superscript_bottom_min: MathValueRecord,
        superscript_baseline_drop_max: MathValueRecord,
        sub_superscript_gap_min: MathValueRecord,
        superscript_bottom_max_with_subscript: MathValueRecord,
        space_after_script: MathValueRecord,
        upper_limit_gap_min: MathValueRecord,
        upper_limit_baseline_rise_min: MathValueRecord,
        lower_limit_gap_min: MathValueRecord,
        lower_limit_baseline_drop_min: MathValueRecord,
        stack_top_shift_up: MathValueRecord,
        stack_top_display_style_shift_up: MathValueRecord,
        stack_bottom_shift_down: MathValueRecord,
        stack_bottom_display_style_shift_down: MathValueRecord,
        stack_gap_min: MathValueRecord,
        stack_display_style_gap_min: MathValueRecord,
        stretch_stack_top_shift_up: MathValueRecord,
        stretch_stack_bottom_shift_down: MathValueRecord,
        stretch_stack_gap_above_min: MathValueRecord,
        stretch_stack_gap_below_min: MathValueRecord,
        fraction_numerator_shift_up: MathValueRecord,
        fraction_numerator_display_style_shift_up: MathValueRecord,
        fraction_denominator_shift_down: MathValueRecord,
        fraction_denominator_display_style_shift_down: MathValueRecord,
        fraction_numerator_gap_min: MathValueRecord,
        fraction_num_display_style_gap_min: MathValueRecord,
        fraction_rule_thickness: MathValueRecord,
        fraction_denominator_gap_min: MathValueRecord,
        fraction_denom_display_style_gap_min: MathValueRecord,
        skewed_fraction_horizontal_gap: MathValueRecord,
        skewed_fraction_vertical_gap: MathValueRecord,
        overbar_vertical_gap: MathValueRecord,
        overbar_rule_thickness: MathValueRecord,
        overbar_extra_ascender: MathValueRecord,
        underbar_vertical_gap: MathValueRecord,
        underbar_rule_thickness: MathValueRecord,
        underbar_extra_descender: MathValueRecord,
        radical_vertical_gap: MathValueRecord,
        radical_display_style_vertical_gap: MathValueRecord,
        radical_rule_thickness: MathValueRecord,
        radical_extra_ascender: MathValueRecord,
        radical_kern_before_degree: MathValueRecord,
        radical_kern_after_degree: MathValueRecord,
        radical_degree_bottom_raise_percent: i16,
    ) -> Self {
        Self {
            script_percent_scale_down,
            script_script_percent_scale_down,
            delimited_sub_formula_min_height,
            display_operator_min_height,
            math_leading,
            axis_height,
            accent_base_height,
            flattened_accent_base_height,
            subscript_shift_down,
            subscript_top_max,
            subscript_baseline_drop_min,
            superscript_shift_up,
            superscript_shift_up_cramped,
            superscript_bottom_min,
            superscript_baseline_drop_max,
            sub_superscript_gap_min,
            superscript_bottom_max_with_subscript,
            space_after_script,
            upper_limit_gap_min,
            upper_limit_baseline_rise_min,
            lower_limit_gap_min,
            lower_limit_baseline_drop_min,
            stack_top_shift_up,
            stack_top_display_style_shift_up,
            stack_bottom_shift_down,
            stack_bottom_display_style_shift_down,
            stack_gap_min,
            stack_display_style_gap_min,
            stretch_stack_top_shift_up,
            stretch_stack_bottom_shift_down,
            stretch_stack_gap_above_min,
            stretch_stack_gap_below_min,
            fraction_numerator_shift_up,
            fraction_numerator_display_style_shift_up,
            fraction_denominator_shift_down,
            fraction_denominator_display_style_shift_down,
            fraction_numerator_gap_min,
            fraction_num_display_style_gap_min,
            fraction_rule_thickness,
            fraction_denominator_gap_min,
            fraction_denom_display_style_gap_min,
            skewed_fraction_horizontal_gap,
            skewed_fraction_vertical_gap,
            overbar_vertical_gap,
            overbar_rule_thickness,
            overbar_extra_ascender,
            underbar_vertical_gap,
            underbar_rule_thickness,
            underbar_extra_descender,
            radical_vertical_gap,
            radical_display_style_vertical_gap,
            radical_rule_thickness,
            radical_extra_ascender,
            radical_kern_before_degree,
            radical_kern_after_degree,
            radical_degree_bottom_raise_percent,
        }
    }
}

impl FontWrite for MathConstants {
    fn write_into(&self, writer: &mut TableWriter) {
        self.script_percent_scale_down.write_into(writer);
        self.script_script_percent_scale_down.write_into(writer);
        self.delimited_sub_formula_min_height.write_into(writer);
        self.display_operator_min_height.write_into(writer);
        self.math_leading.write_into(writer);
        self.axis_height.write_into(writer);
        self.accent_base_height.write_into(writer);
        self.flattened_accent_base_height.write_into(writer);
        self.subscript_shift_down.write_into(writer);
        self.subscript_top_max.write_into(writer);
        self.subscript_baseline_drop_min.write_into(writer);
        self.superscript_shift_up.write_into(writer);
        self.superscript_shift_up_cramped.write_into(writer);
        self.superscript_bottom_min.write_into(writer);
        self.superscript_baseline_drop_max.write_into(writer);
        self.sub_superscript_gap_min.write_into(writer);
        self.superscript_bottom_max_with_subscript
            .write_into(writer);
        self.space_after_script.write_into(writer);
        self.upper_limit_gap_min.write_into(writer);
        self.upper_limit_baseline_rise_min.write_into(writer);
        self.lower_limit_gap_min.write_into(writer);
        self.lower_limit_baseline_drop_min.write_into(writer);
        self.stack_top_shift_up.write_into(writer);
        self.stack_top_display_style_shift_up.write_into(writer);
        self.stack_bottom_shift_down.write_into(writer);
        self.stack_bottom_display_style_shift_down
            .write_into(writer);
        self.stack_gap_min.write_into(writer);
        self.stack_display_style_gap_min.write_into(writer);
        self.stretch_stack_top_shift_up.write_into(writer);
        self.stretch_stack_bottom_shift_down.write_into(writer);
        self.stretch_stack_gap_above_min.write_into(writer);
        self.stretch_stack_gap_below_min.write_into(writer);
        self.fraction_numerator_shift_up.write_into(writer);
        self.fraction_numerator_display_style_shift_up
            .write_into(writer);
        self.fraction_denominator_shift_down.write_into(writer);
        self.fraction_denominator_display_style_shift_down
            .write_into(writer);
        self.fraction_numerator_gap_min.write_into(writer);
        self.fraction_num_display_style_gap_min.write_into(writer);
        self.fraction_rule_thickness.write_into(writer);
        self.fraction_denominator_gap_min.write_into(writer);
        self.fraction_denom_display_style_gap_min.write_into(writer);
        self.skewed_fraction_horizontal_gap.write_into(writer);
        self.skewed_fraction_vertical_gap.write_into(writer);
        self.overbar_vertical_gap.write_into(writer);
        self.overbar_rule_thickness.write_into(writer);
        self.overbar_extra_ascender.write_into(writer);
        self.underbar_vertical_gap.write_into(writer);
        self.underbar_rule_thickness.write_into(writer);
        self.underbar_extra_descender.write_into(writer);
        self.radical_vertical_gap.write_into(writer);
        self.radical_display_style_vertical_gap.write_into(writer);
        self.radical_rule_thickness.write_into(writer);
        self.radical_extra_ascender.write_into(writer);
        self.radical_kern_before_degree.write_into(writer);
        self.radical_kern_after_degree.write_into(writer);
        self.radical_degree_bottom_raise_percent.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("MathConstants")
    }
}

impl Validate for MathConstants {
    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
}

impl<'a> FromObjRef<read_fonts::tables::math::MathConstants<'a>> for MathConstants {
    fn from_obj_ref(obj: &read_fonts::tables::math::MathConstants<'a>, _: FontData) -> Self {
        let offset_data = obj.offset_data();
        MathConstants {
            script_percent_scale_down: obj.script_percent_scale_down(),
            script_script_percent_scale_down: obj.script_script_percent_scale_down(),
            delimited_sub_formula_min_height: obj.delimited_sub_formula_min_height(),
            display_operator_min_height: obj.display_operator_min_height(),
            math_leading: obj.math_leading().to_owned_obj(offset_data),
            axis_height: obj.axis_height().to_owned_obj(offset_data),
            accent_base_height: obj.accent_base_height().to_owned_obj(offset_data),
            flattened_accent_base_height: obj
                .flattened_accent_base_height()
                .to_owned_obj(offset_data),
            subscript_shift_down: obj.subscript_shift_down().to_owned_obj(offset_data),
            subscript_top_max: obj.subscript_top_max().to_owned_obj(offset_data),
            subscript_baseline_drop_min: obj
                .subscript_baseline_drop_min()
                .to_owned_obj(offset_data),
            superscript_shift_up: obj.superscript_shift_up().to_owned_obj(offset_data),
            superscript_shift_up_cramped: obj
                .superscript_shift_up_cramped()
                .to_owned_obj(offset_data),
            superscript_bottom_min: obj.superscript_bottom_min().to_owned_obj(offset_data),
            superscript_baseline_drop_max: obj
                .superscript_baseline_drop_max()
                .to_owned_obj(offset_data),
            sub_superscript_gap_min: obj.sub_superscript_gap_min().to_owned_obj(offset_data),
            superscript_bottom_max_with_subscript: obj
                .superscript_bottom_max_with_subscript()
                .to_owned_obj(offset_data),
            space_after_script: obj.space_after_script().to_owned_obj(offset_data),
            upper_limit_gap_min: obj.upper_limit_gap_min().to_owned_obj(offset_data),
            upper_limit_baseline_rise_min: obj
                .upper_limit_baseline_rise_min()
                .to_owned_obj(offset_data),
            lower_limit_gap_min: obj.lower_limit_gap_min().to_owned_obj(offset_data),
            lower_limit_baseline_drop_min: obj
                .lower_limit_baseline_drop_min()
                .to_owned_obj(offset_data),
            stack_top_shift_up: obj.stack_top_shift_up().to_owned_obj(offset_data),
            stack_top_display_style_shift_up: obj
                .stack_top_display_style_shift_up()
                .to_owned_obj(offset_data),
            stack_bottom_shift_down: obj.stack_bottom_shift_down().to_owned_obj(offset_data),
            stack_bottom_display_style_shift_down: obj
                .stack_bottom_display_style_shift_down()
                .to_owned_obj(offset_data),
            stack_gap_min: obj.stack_gap_min().to_owned_obj(offset_data),
            stack_display_style_gap_min: obj
                .stack_display_style_gap_min()
                .to_owned_obj(offset_data),
            stretch_stack_top_shift_up: obj.stretch_stack_top_shift_up().to_owned_obj(offset_data),
            stretch_stack_bottom_shift_down: obj
                .stretch_stack_bottom_shift_down()
                .to_owned_obj(offset_data),
            stretch_stack_gap_above_min: obj
                .stretch_stack_gap_above_min()
                .to_owned_obj(offset_data),
            stretch_stack_gap_below_min: obj
                .stretch_stack_gap_below_min()
                .to_owned_obj(offset_data),
            fraction_numerator_shift_up: obj
                .fraction_numerator_shift_up()
                .to_owned_obj(offset_data),
            fraction_numerator_display_style_shift_up: obj
                .fraction_numerator_display_style_shift_up()
                .to_owned_obj(offset_data),
            fraction_denominator_shift_down: obj
                .fraction_denominator_shift_down()
                .to_owned_obj(offset_data),
            fraction_denominator_display_style_shift_down: obj
                .fraction_denominator_display_style_shift_down()
                .to_owned_obj(offset_data),
            fraction_numerator_gap_min: obj.fraction_numerator_gap_min().to_owned_obj(offset_data),
            fraction_num_display_style_gap_min: obj
                .fraction_num_display_style_gap_min()
                .to_owned_obj(offset_data),
            fraction_rule_thickness: obj.fraction_rule_thickness().to_owned_obj(offset_data),
            fraction_denominator_gap_min: obj
                .fraction_denominator_gap_min()
                .to_owned_obj(offset_data),
            fraction_denom_display_style_gap_min: obj
                .fraction_denom_display_style_gap_min()
                .to_owned_obj(offset_data),
            skewed_fraction_horizontal_gap: obj
                .skewed_fraction_horizontal_gap()
                .to_owned_obj(offset_data),
            skewed_fraction_vertical_gap: obj
                .skewed_fraction_vertical_gap()
                .to_owned_obj(offset_data),
            overbar_vertical_gap: obj.overbar_vertical_gap().to_owned_obj(offset_data),
            overbar_rule_thickness: obj.overbar_rule_thickness().to_owned_obj(offset_data),
            overbar_extra_ascender: obj.overbar_extra_ascender().to_owned_obj(offset_data),
            underbar_vertical_gap: obj.underbar_vertical_gap().to_owned_obj(offset_data),
            underbar_rule_thickness: obj.underbar_rule_thickness().to_owned_obj(offset_data),
            underbar_extra_descender: obj.underbar_extra_descender().to_owned_obj(offset_data),
            radical_vertical_gap: obj.radical_vertical_gap().to_owned_obj(offset_data),
            radical_display_style_vertical_gap: obj
                .radical_display_style_vertical_gap()
                .to_owned_obj(offset_data),
            radical_rule_thickness: obj.radical_rule_thickness().to_owned_obj(offset_data),
            radical_extra_ascender: obj.radical_extra_ascender().to_owned_obj(offset_data),
            radical_kern_before_degree: obj.radical_kern_before_degree().to_owned_obj(offset_data),
            radical_kern_after_degree: obj.radical_kern_after_degree().to_owned_obj(offset_data),
            radical_degree_bottom_raise_percent: obj.radical_degree_bottom_raise_percent(),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::tables::math::MathConstants<'a>> for MathConstants {}

impl ReadArgs for MathConstants {
    type Args = ();
}

impl<'a> FontRead<'a> for MathConstants {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        <read_fonts::tables::math::MathConstants as FontRead>::read(data)
            .map(|x| x.to_owned_table())
    }
}

/// [MathGlyphInfo](https://learn.microsoft.com/en-us/typography/opentype/spec/math#mathglyphinfo-table)
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MathGlyphInfo {
    /// Offset to MathItalicsCorrectionInfo table, from the beginning of the
    /// MathGlyphInfo table. May be NULL.
    pub math_italics_correction_info: NullableOffsetMarker<MathItalicsCorrectionInfo>,
    /// Offset to MathTopAccentAttachment table, from the beginning of the
    /// MathGlyphInfo table. May be NULL.
    pub math_top_accent_attachment: NullableOffsetMarker<MathTopAccentAttachment>,
    /// Offset to the Coverage table for glyphs with extended shapes, from the
    /// beginning of the MathGlyphInfo table. May be NULL.
    pub extended_shape_coverage: NullableOffsetMarker<CoverageTable>,
    /// Offset to the MathKernInfo table, from the beginning of the
    /// MathGlyphInfo table. May be NULL.
    pub math_kern_info: NullableOffsetMarker<MathKernInfo>,
}

impl MathGlyphInfo {
    /// Construct a new `MathGlyphInfo`
    pub fn new(
        math_italics_correction_info: Option<MathItalicsCorrectionInfo>,
        math_top_accent_attachment: Option<MathTopAccentAttachment>,
        extended_shape_coverage: Option<CoverageTable>,
        math_kern_info: Option<MathKernInfo>,
    ) -> Self {
        Self {
            math_italics_correction_info: math_italics_correction_info.into(),
            math_top_accent_attachment: math_top_accent_attachment.into(),
            extended_shape_coverage: extended_shape_coverage.into(),
            math_kern_info: math_kern_info.into(),
        }
    }
}

impl FontWrite for MathGlyphInfo {
    fn write_into(&self, writer: &mut TableWriter) {
        self.math_italics_correction_info.write_into(writer);
        self.math_top_accent_attachment.write_into(writer);
        self.extended_shape_coverage.write_into(writer);
        self.math_kern_info.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("MathGlyphInfo")
    }
}

impl Validate for MathGlyphInfo {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        ctx.in_table("MathGlyphInfo", |ctx| {
            ctx.in_field("math_italics_correction_info", |ctx| {
                self.math_italics_correction_info.validate_impl(ctx);
            });
            ctx.in_field("math_top_accent_attachment", |ctx| {
                self.math_top_accent_attachment.validate_impl(ctx);
            });
            ctx.in_field("extended_shape_coverage", |ctx| {
                self.extended_shape_coverage.validate_impl(ctx);
            });
            ctx.in_field("math_kern_info", |ctx| {
                self.math_kern_info.validate_impl(ctx);
            });
        })
    }
}

impl<'a> FromObjRef<read_fonts::tables::math::MathGlyphInfo<'a>> for MathGlyphInfo {
    fn from_obj_ref(obj: &read_fonts::tables::math::MathGlyphInfo<'a>, _: FontData) -> Self {
        MathGlyphInfo {
            math_italics_correction_info: obj.math_italics_correction_info().to_owned_table(),
            math_top_accent_attachment: obj.math_top_accent_attachment().to_owned_table(),
            extended_shape_coverage: obj.extended_shape_coverage().to_owned_table(),
            math_kern_info: obj.math_kern_info().to_owned_table(),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::tables::math::MathGlyphInfo<'a>> for MathGlyphInfo {}

impl ReadArgs for MathGlyphInfo {
    type Args = ();
}

impl<'a> FontRead<'a> for MathGlyphInfo {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        <read_fonts::tables::math::MathGlyphInfo as FontRead>::read(data)
            .map(|x| x.to_owned_table())
    }
}

/// [MathItalicsCorrectionInfo](https://learn.microsoft.com/en-us/typography/opentype/spec/math#mathitalicscorrectioninfo-table)
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MathItalicsCorrectionInfo {
    /// Offset to the Coverage table, from the beginning of the
    /// MathItalicsCorrectionInfo table.
    pub coverage: OffsetMarker<CoverageTable>,
    /// Array of MathValueRecords defining italics correction values for each
    /// covered glyph.
    pub italics_correction: Vec<MathValueRecord>,
}

impl MathItalicsCorrectionInfo {
    /// Construct a new `MathItalicsCorrectionInfo`
    pub fn new(coverage: CoverageTable, italics_correction: Vec<MathValueRecord>) -> Self {
        Self {
            coverage: coverage.into(),
            italics_correction,
        }
    }
}

impl FontWrite for MathItalicsCorrectionInfo {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        self.coverage.write_into(writer);
        (u16::try_from(array_len(&self.italics_correction)).unwrap()).write_into(writer);
        self.italics_correction.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("MathItalicsCorrectionInfo")
    }
}

impl Validate for MathItalicsCorrectionInfo {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        ctx.in_table("MathItalicsCorrectionInfo", |ctx| {
            ctx.in_field("coverage", |ctx| {
                self.coverage.validate_impl(ctx);
            });
            ctx.in_field("italics_correction", |ctx| {
                if self.italics_correction.len() > to_usize(u16::MAX) {
                    ctx.report("array exceeds max length");
                }
                self.italics_correction.validate_impl(ctx);
            });
        })
    }
}

impl<'a> FromObjRef<read_fonts::tables::math::MathItalicsCorrectionInfo<'a>>
    for MathItalicsCorrectionInfo
{
    fn from_obj_ref(
        obj: &read_fonts::tables::math::MathItalicsCorrectionInfo<'a>,
        _: FontData,
    ) -> Self {
        let offset_data = obj.offset_data();
        MathItalicsCorrectionInfo {
            coverage: obj.coverage().to_owned_table(),
            italics_correction: obj.italics_correction().to_owned_obj(offset_data),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::tables::math::MathItalicsCorrectionInfo<'a>>
    for MathItalicsCorrectionInfo
{
}

impl ReadArgs for MathItalicsCorrectionInfo {
    type Args = ();
}

impl<'a> FontRead<'a> for MathItalicsCorrectionInfo {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        <read_fonts::tables::math::MathItalicsCorrectionInfo as FontRead>::read(data)
            .map(|x| x.to_owned_table())
    }
}

/// [MathTopAccentAttachment](https://learn.microsoft.com/en-us/typography/opentype/spec/math#mathtopaccentattachment-table)
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MathTopAccentAttachment {
    /// Offset to the Coverage table, from the beginning of the
    /// MathTopAccentAttachment table.
    pub top_accent_coverage: OffsetMarker<CoverageTable>,
    /// Array of MathValueRecords defining top accent attachment points for each
    /// covered glyph.
    pub top_accent_attachment: Vec<MathValueRecord>,
}

impl MathTopAccentAttachment {
    /// Construct a new `MathTopAccentAttachment`
    pub fn new(
        top_accent_coverage: CoverageTable,
        top_accent_attachment: Vec<MathValueRecord>,
    ) -> Self {
        Self {
            top_accent_coverage: top_accent_coverage.into(),
            top_accent_attachment,
        }
    }
}

impl FontWrite for MathTopAccentAttachment {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        self.top_accent_coverage.write_into(writer);
        (u16::try_from(array_len(&self.top_accent_attachment)).unwrap()).write_into(writer);
        self.top_accent_attachment.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("MathTopAccentAttachment")
    }
}

impl Validate for MathTopAccentAttachment {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        ctx.in_table("MathTopAccentAttachment", |ctx| {
            ctx.in_field("top_accent_coverage", |ctx| {
                self.top_accent_coverage.validate_impl(ctx);
            });
            ctx.in_field("top_accent_attachment", |ctx| {
                if self.top_accent_attachment.len() > to_usize(u16::MAX) {
                    ctx.report("array exceeds max length");
                }
                self.top_accent_attachment.validate_impl(ctx);
            });
        })
    }
}

impl<'a> FromObjRef<read_fonts::tables::math::MathTopAccentAttachment<'a>>
    for MathTopAccentAttachment
{
    fn from_obj_ref(
        obj: &read_fonts::tables::math::MathTopAccentAttachment<'a>,
        _: FontData,
    ) -> Self {
        let offset_data = obj.offset_data();
        MathTopAccentAttachment {
            top_accent_coverage: obj.top_accent_coverage().to_owned_table(),
            top_accent_attachment: obj.top_accent_attachment().to_owned_obj(offset_data),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::tables::math::MathTopAccentAttachment<'a>>
    for MathTopAccentAttachment
{
}

impl ReadArgs for MathTopAccentAttachment {
    type Args = ();
}

impl<'a> FontRead<'a> for MathTopAccentAttachment {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        <read_fonts::tables::math::MathTopAccentAttachment as FontRead>::read(data)
            .map(|x| x.to_owned_table())
    }
}

/// [MathKernInfo](https://learn.microsoft.com/en-us/typography/opentype/spec/math#mathkerninfo-table)
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MathKernInfo {
    /// Offset to the Coverage table, from the beginning of the MathKernInfo
    /// table.
    pub math_kern_coverage: OffsetMarker<CoverageTable>,
    /// Array of MathKernInfoRecords, one for each covered glyph.
    pub math_kern_info_records: Vec<MathKernInfoRecord>,
}

impl MathKernInfo {
    /// Construct a new `MathKernInfo`
    pub fn new(
        math_kern_coverage: CoverageTable,
        math_kern_info_records: Vec<MathKernInfoRecord>,
    ) -> Self {
        Self {
            math_kern_coverage: math_kern_coverage.into(),
            math_kern_info_records,
        }
    }
}

impl FontWrite for MathKernInfo {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        self.math_kern_coverage.write_into(writer);
        (u16::try_from(array_len(&self.math_kern_info_records)).unwrap()).write_into(writer);
        self.math_kern_info_records.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("MathKernInfo")
    }
}

impl Validate for MathKernInfo {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        ctx.in_table("MathKernInfo", |ctx| {
            ctx.in_field("math_kern_coverage", |ctx| {
                self.math_kern_coverage.validate_impl(ctx);
            });
            ctx.in_field("math_kern_info_records", |ctx| {
                if self.math_kern_info_records.len() > to_usize(u16::MAX) {
                    ctx.report("array exceeds max length");
                }
                self.math_kern_info_records.validate_impl(ctx);
            });
        })
    }
}

impl<'a> FromObjRef<read_fonts::tables::math::MathKernInfo<'a>> for MathKernInfo {
    fn from_obj_ref(obj: &read_fonts::tables::math::MathKernInfo<'a>, _: FontData) -> Self {
        let offset_data = obj.offset_data();
        MathKernInfo {
            math_kern_coverage: obj.math_kern_coverage().to_owned_table(),
            math_kern_info_records: obj.math_kern_info_records().to_owned_obj(offset_data),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::tables::math::MathKernInfo<'a>> for MathKernInfo {}

impl ReadArgs for MathKernInfo {
    type Args = ();
}

impl<'a> FontRead<'a> for MathKernInfo {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        <read_fonts::tables::math::MathKernInfo as FontRead>::read(data).map(|x| x.to_owned_table())
    }
}

/// [MathKernInfoRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/math#mathkerninforecord)
///
/// The offsets are measured from the beginning of the MathKernInfo table.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MathKernInfoRecord {
    /// Offset to the MathKern table for the top right corner, from the
    /// beginning of the MathKernInfo table. May be NULL.
    pub top_right_math_kern: NullableOffsetMarker<MathKern>,
    /// Offset to the MathKern table for the top left corner, from the beginning
    /// of the MathKernInfo table. May be NULL.
    pub top_left_math_kern: NullableOffsetMarker<MathKern>,
    /// Offset to the MathKern table for the bottom right corner, from the
    /// beginning of the MathKernInfo table. May be NULL.
    pub bottom_right_math_kern: NullableOffsetMarker<MathKern>,
    /// Offset to the MathKern table for the bottom left corner, from the
    /// beginning of the MathKernInfo table. May be NULL.
    pub bottom_left_math_kern: NullableOffsetMarker<MathKern>,
}

impl MathKernInfoRecord {
    /// Construct a new `MathKernInfoRecord`
    pub fn new(
        top_right_math_kern: Option<MathKern>,
        top_left_math_kern: Option<MathKern>,
        bottom_right_math_kern: Option<MathKern>,
        bottom_left_math_kern: Option<MathKern>,
    ) -> Self {
        Self {
            top_right_math_kern: top_right_math_kern.into(),
            top_left_math_kern: top_left_math_kern.into(),
            bottom_right_math_kern: bottom_right_math_kern.into(),
            bottom_left_math_kern: bottom_left_math_kern.into(),
        }
    }
}

impl FontWrite for MathKernInfoRecord {
    fn write_into(&self, writer: &mut TableWriter) {
        self.top_right_math_kern.write_into(writer);
        self.top_left_math_kern.write_into(writer);
        self.bottom_right_math_kern.write_into(writer);
        self.bottom_left_math_kern.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("MathKernInfoRecord")
    }
}

impl Validate for MathKernInfoRecord {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        ctx.in_table("MathKernInfoRecord", |ctx| {
            ctx.in_field("top_right_math_kern", |ctx| {
                self.top_right_math_kern.validate_impl(ctx);
            });
            ctx.in_field("top_left_math_kern", |ctx| {
                self.top_left_math_kern.validate_impl(ctx);
            });
            ctx.in_field("bottom_right_math_kern", |ctx| {
                self.bottom_right_math_kern.validate_impl(ctx);
            });
            ctx.in_field("bottom_left_math_kern", |ctx| {
                self.bottom_left_math_kern.validate_impl(ctx);
            });
        })
    }
}

impl FromObjRef<read_fonts::tables::math::MathKernInfoRecord> for MathKernInfoRecord {
    fn from_obj_ref(
        obj: &read_fonts::tables::math::MathKernInfoRecord,
        offset_data: FontData,
    ) -> Self {
        MathKernInfoRecord {
            top_right_math_kern: obj.top_right_math_kern(offset_data).to_owned_table(),
            top_left_math_kern: obj.top_left_math_kern(offset_data).to_owned_table(),
            bottom_right_math_kern: obj.bottom_right_math_kern(offset_data).to_owned_table(),
            bottom_left_math_kern: obj.bottom_left_math_kern(offset_data).to_owned_table(),
        }
    }
}

/// [MathKern](https://learn.microsoft.com/en-us/typography/opentype/spec/math#mathkern-table)
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MathKern {
    /// Array of correction heights, in design units, sorted from lowest to
    /// highest.
    pub correction_height: Vec<MathValueRecord>,
    /// Array of kerning values for different height ranges, in design units.
    ///
    /// There is one more of these than there are correction heights: the first
    /// applies below the first correction height, and the last above the last.
    pub kern_values: Vec<MathValueRecord>,
}

impl MathKern {
    /// Construct a new `MathKern`
    pub fn new(correction_height: Vec<MathValueRecord>, kern_values: Vec<MathValueRecord>) -> Self {
        Self {
            correction_height,
            kern_values,
        }
    }
}

impl FontWrite for MathKern {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        (u16::try_from(array_len(&self.correction_height)).unwrap()).write_into(writer);
        self.correction_height.write_into(writer);
        self.kern_values.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("MathKern")
    }
}

impl Validate for MathKern {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        ctx.in_table("MathKern", |ctx| {
            ctx.in_field("correction_height", |ctx| {
                if self.correction_height.len() > to_usize(u16::MAX) {
                    ctx.report("array exceeds max length");
                }
                self.correction_height.validate_impl(ctx);
            });
            ctx.in_field("kern_values", |ctx| {
                self.kern_values.validate_impl(ctx);
            });
        })
    }
}

impl<'a> FromObjRef<read_fonts::tables::math::MathKern<'a>> for MathKern {
    fn from_obj_ref(obj: &read_fonts::tables::math::MathKern<'a>, _: FontData) -> Self {
        let offset_data = obj.offset_data();
        MathKern {
            correction_height: obj.correction_height().to_owned_obj(offset_data),
            kern_values: obj.kern_values().to_owned_obj(offset_data),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::tables::math::MathKern<'a>> for MathKern {}

impl ReadArgs for MathKern {
    type Args = ();
}

impl<'a> FontRead<'a> for MathKern {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        <read_fonts::tables::math::MathKern as FontRead>::read(data).map(|x| x.to_owned_table())
    }
}

/// [MathVariants](https://learn.microsoft.com/en-us/typography/opentype/spec/math#mathvariants-table)
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MathVariants {
    /// Minimum overlap of connecting glyphs during glyph construction, in
    /// design units.
    pub min_connector_overlap: UfWord,
    /// Offset to the Coverage table for shapes growing in the vertical
    /// direction, from the beginning of the MathVariants table. May be NULL.
    pub vert_glyph_coverage: NullableOffsetMarker<CoverageTable>,
    /// Offset to the Coverage table for shapes growing in the horizontal
    /// direction, from the beginning of the MathVariants table. May be NULL.
    pub horiz_glyph_coverage: NullableOffsetMarker<CoverageTable>,
    /// Array of offsets to MathGlyphConstruction tables, from the beginning of
    /// the MathVariants table, for shapes growing in the vertical direction.
    pub vert_glyph_constructions: Vec<OffsetMarker<MathGlyphConstruction>>,
    /// Array of offsets to MathGlyphConstruction tables, from the beginning of
    /// the MathVariants table, for shapes growing in the horizontal direction.
    pub horiz_glyph_constructions: Vec<OffsetMarker<MathGlyphConstruction>>,
}

impl MathVariants {
    /// Construct a new `MathVariants`
    pub fn new(
        min_connector_overlap: UfWord,
        vert_glyph_coverage: Option<CoverageTable>,
        horiz_glyph_coverage: Option<CoverageTable>,
        vert_glyph_constructions: Vec<MathGlyphConstruction>,
        horiz_glyph_constructions: Vec<MathGlyphConstruction>,
    ) -> Self {
        Self {
            min_connector_overlap,
            vert_glyph_coverage: vert_glyph_coverage.into(),
            horiz_glyph_coverage: horiz_glyph_coverage.into(),
            vert_glyph_constructions: vert_glyph_constructions
                .into_iter()
                .map(Into::into)
                .collect(),
            horiz_glyph_constructions: horiz_glyph_constructions
                .into_iter()
                .map(Into::into)
                .collect(),
        }
    }
}

impl FontWrite for MathVariants {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        self.min_connector_overlap.write_into(writer);
        self.vert_glyph_coverage.write_into(writer);
        self.horiz_glyph_coverage.write_into(writer);
        (u16::try_from(array_len(&self.vert_glyph_constructions)).unwrap()).write_into(writer);
        (u16::try_from(array_len(&self.horiz_glyph_constructions)).unwrap()).write_into(writer);
        self.vert_glyph_constructions.write_into(writer);
        self.horiz_glyph_constructions.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("MathVariants")
    }
}

impl Validate for MathVariants {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        ctx.in_table("MathVariants", |ctx| {
            ctx.in_field("vert_glyph_coverage", |ctx| {
                self.vert_glyph_coverage.validate_impl(ctx);
            });
            ctx.in_field("horiz_glyph_coverage", |ctx| {
                self.horiz_glyph_coverage.validate_impl(ctx);
            });
            ctx.in_field("vert_glyph_constructions", |ctx| {
                if self.vert_glyph_constructions.len() > to_usize(u16::MAX) {
                    ctx.report("array exceeds max length");
                }
                self.vert_glyph_constructions.validate_impl(ctx);
            });
            ctx.in_field("horiz_glyph_constructions", |ctx| {
                if self.horiz_glyph_constructions.len() > to_usize(u16::MAX) {
                    ctx.report("array exceeds max length");
                }
                self.horiz_glyph_constructions.validate_impl(ctx);
            });
        })
    }
}

impl<'a> FromObjRef<read_fonts::tables::math::MathVariants<'a>> for MathVariants {
    fn from_obj_ref(obj: &read_fonts::tables::math::MathVariants<'a>, _: FontData) -> Self {
        MathVariants {
            min_connector_overlap: obj.min_connector_overlap(),
            vert_glyph_coverage: obj.vert_glyph_coverage().to_owned_table(),
            horiz_glyph_coverage: obj.horiz_glyph_coverage().to_owned_table(),
            vert_glyph_constructions: obj.vert_glyph_constructions().to_owned_table(),
            horiz_glyph_constructions: obj.horiz_glyph_constructions().to_owned_table(),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::tables::math::MathVariants<'a>> for MathVariants {}

impl ReadArgs for MathVariants {
    type Args = ();
}

impl<'a> FontRead<'a> for MathVariants {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        <read_fonts::tables::math::MathVariants as FontRead>::read(data).map(|x| x.to_owned_table())
    }
}

/// [MathGlyphConstruction](https://learn.microsoft.com/en-us/typography/opentype/spec/math#mathglyphconstruction-table)
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MathGlyphConstruction {
    /// Offset to the GlyphAssembly table for this shape, from the beginning of
    /// the MathGlyphConstruction table. May be NULL.
    pub glyph_assembly: NullableOffsetMarker<GlyphAssembly>,
    /// MathGlyphVariantRecords for alternative variants of the base glyph.
    pub math_glyph_variant_records: Vec<MathGlyphVariantRecord>,
}

impl MathGlyphConstruction {
    /// Construct a new `MathGlyphConstruction`
    pub fn new(
        glyph_assembly: Option<GlyphAssembly>,
        math_glyph_variant_records: Vec<MathGlyphVariantRecord>,
    ) -> Self {
        Self {
            glyph_assembly: glyph_assembly.into(),
            math_glyph_variant_records,
        }
    }
}

impl FontWrite for MathGlyphConstruction {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        self.glyph_assembly.write_into(writer);
        (u16::try_from(array_len(&self.math_glyph_variant_records)).unwrap()).write_into(writer);
        self.math_glyph_variant_records.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("MathGlyphConstruction")
    }
}

impl Validate for MathGlyphConstruction {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        ctx.in_table("MathGlyphConstruction", |ctx| {
            ctx.in_field("glyph_assembly", |ctx| {
                self.glyph_assembly.validate_impl(ctx);
            });
            ctx.in_field("math_glyph_variant_records", |ctx| {
                if self.math_glyph_variant_records.len() > to_usize(u16::MAX) {
                    ctx.report("array exceeds max length");
                }
                self.math_glyph_variant_records.validate_impl(ctx);
            });
        })
    }
}

impl<'a> FromObjRef<read_fonts::tables::math::MathGlyphConstruction<'a>> for MathGlyphConstruction {
    fn from_obj_ref(
        obj: &read_fonts::tables::math::MathGlyphConstruction<'a>,
        _: FontData,
    ) -> Self {
        let offset_data = obj.offset_data();
        MathGlyphConstruction {
            glyph_assembly: obj.glyph_assembly().to_owned_table(),
            math_glyph_variant_records: obj.math_glyph_variant_records().to_owned_obj(offset_data),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::tables::math::MathGlyphConstruction<'a>>
    for MathGlyphConstruction
{
}

impl ReadArgs for MathGlyphConstruction {
    type Args = ();
}

impl<'a> FontRead<'a> for MathGlyphConstruction {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        <read_fonts::tables::math::MathGlyphConstruction as FontRead>::read(data)
            .map(|x| x.to_owned_table())
    }
}

/// [MathGlyphVariantRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/math#mathglyphvariantrecord)
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MathGlyphVariantRecord {
    /// Glyph ID for the variant.
    pub variant_glyph: GlyphId16,
    /// Advance width/height, in design units, of the variant, in the direction
    /// of requested glyph extension.
    pub advance_measurement: UfWord,
}

impl MathGlyphVariantRecord {
    /// Construct a new `MathGlyphVariantRecord`
    pub fn new(variant_glyph: GlyphId16, advance_measurement: UfWord) -> Self {
        Self {
            variant_glyph,
            advance_measurement,
        }
    }
}

impl FontWrite for MathGlyphVariantRecord {
    fn write_into(&self, writer: &mut TableWriter) {
        self.variant_glyph.write_into(writer);
        self.advance_measurement.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("MathGlyphVariantRecord")
    }
}

impl Validate for MathGlyphVariantRecord {
    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
}

impl FromObjRef<read_fonts::tables::math::MathGlyphVariantRecord> for MathGlyphVariantRecord {
    fn from_obj_ref(obj: &read_fonts::tables::math::MathGlyphVariantRecord, _: FontData) -> Self {
        MathGlyphVariantRecord {
            variant_glyph: obj.variant_glyph(),
            advance_measurement: obj.advance_measurement(),
        }
    }
}

/// [GlyphAssembly](https://learn.microsoft.com/en-us/typography/opentype/spec/math#glyphassembly-table)
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GlyphAssembly {
    /// Italics correction of this GlyphAssembly. Should not depend on the
    /// assembly size.
    pub italics_correction: MathValueRecord,
    /// Array of part records, from left to right (for assemblies that extend
    /// horizontally) or bottom to top (for assemblies that extend vertically).
    pub part_records: Vec<GlyphPartRecord>,
}

impl GlyphAssembly {
    /// Construct a new `GlyphAssembly`
    pub fn new(italics_correction: MathValueRecord, part_records: Vec<GlyphPartRecord>) -> Self {
        Self {
            italics_correction,
            part_records,
        }
    }
}

impl FontWrite for GlyphAssembly {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        self.italics_correction.write_into(writer);
        (u16::try_from(array_len(&self.part_records)).unwrap()).write_into(writer);
        self.part_records.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("GlyphAssembly")
    }
}

impl Validate for GlyphAssembly {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        ctx.in_table("GlyphAssembly", |ctx| {
            ctx.in_field("part_records", |ctx| {
                if self.part_records.len() > to_usize(u16::MAX) {
                    ctx.report("array exceeds max length");
                }
                self.part_records.validate_impl(ctx);
            });
        })
    }
}

impl<'a> FromObjRef<read_fonts::tables::math::GlyphAssembly<'a>> for GlyphAssembly {
    fn from_obj_ref(obj: &read_fonts::tables::math::GlyphAssembly<'a>, _: FontData) -> Self {
        let offset_data = obj.offset_data();
        GlyphAssembly {
            italics_correction: obj.italics_correction().to_owned_obj(offset_data),
            part_records: obj.part_records().to_owned_obj(offset_data),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::tables::math::GlyphAssembly<'a>> for GlyphAssembly {}

impl ReadArgs for GlyphAssembly {
    type Args = ();
}

impl<'a> FontRead<'a> for GlyphAssembly {
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
        <read_fonts::tables::math::GlyphAssembly as FontRead>::read(data)
            .map(|x| x.to_owned_table())
    }
}

/// [GlyphPartRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/math#glyphpartrecord)
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GlyphPartRecord {
    /// Glyph ID for the part.
    pub glyph_id: GlyphId16,
    /// Advance width/ height, in design units, of the straight bar connector
    /// material at the start of the glyph in the direction of the extension.
    pub start_connector_length: UfWord,
    /// Advance width/ height, in design units, of the straight bar connector
    /// material at the end of the glyph in the direction of the extension.
    pub end_connector_length: UfWord,
    /// Full advance width/height for this part in the direction of the
    /// extension, in design units.
    pub full_advance: UfWord,
    /// Part qualifiers.
    pub part_flags: PartFlags,
}

impl GlyphPartRecord {
    /// Construct a new `GlyphPartRecord`
    pub fn new(
        glyph_id: GlyphId16,
        start_connector_length: UfWord,
        end_connector_length: UfWord,
        full_advance: UfWord,
        part_flags: PartFlags,
    ) -> Self {
        Self {
            glyph_id,
            start_connector_length,
            end_connector_length,
            full_advance,
            part_flags,
        }
    }
}

impl FontWrite for GlyphPartRecord {
    fn write_into(&self, writer: &mut TableWriter) {
        self.glyph_id.write_into(writer);
        self.start_connector_length.write_into(writer);
        self.end_connector_length.write_into(writer);
        self.full_advance.write_into(writer);
        self.part_flags.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("GlyphPartRecord")
    }
}

impl Validate for GlyphPartRecord {
    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
}

impl FromObjRef<read_fonts::tables::math::GlyphPartRecord> for GlyphPartRecord {
    fn from_obj_ref(obj: &read_fonts::tables::math::GlyphPartRecord, _: FontData) -> Self {
        GlyphPartRecord {
            glyph_id: obj.glyph_id(),
            start_connector_length: obj.start_connector_length(),
            end_connector_length: obj.end_connector_length(),
            full_advance: obj.full_advance(),
            part_flags: obj.part_flags(),
        }
    }
}

impl FontWrite for PartFlags {
    fn write_into(&self, writer: &mut TableWriter) {
        writer.write_slice(&self.bits().to_be_bytes())
    }
}