write-fonts 0.48.1

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
// 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::*;

/// [Compact Font Format](https://learn.microsoft.com/en-us/typography/opentype/spec/cff) table header
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CffHeader {
    /// Header size (bytes).
    pub hdr_size: u8,
    /// Absolute offset size.
    pub off_size: u8,
    /// Padding bytes before the start of the Name INDEX.
    pub _padding: Vec<u8>,
    /// Remaining table data.
    pub trailing_data: Vec<u8>,
}

impl CffHeader {
    /// Construct a new `CffHeader`
    pub fn new(hdr_size: u8, off_size: u8, _padding: Vec<u8>, trailing_data: Vec<u8>) -> Self {
        Self {
            hdr_size,
            off_size,
            _padding,
            trailing_data,
        }
    }
}

impl FontWrite for CffHeader {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        (1 as u8).write_into(writer);
        (0 as u8).write_into(writer);
        self.hdr_size.write_into(writer);
        self.off_size.write_into(writer);
        self._padding.write_into(writer);
        self.trailing_data.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("CffHeader")
    }
}

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

impl<'a> FromObjRef<read_fonts::ps::cff::v1::CffHeader<'a>> for CffHeader {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::CffHeader<'a>, _: FontData) -> Self {
        let offset_data = obj.offset_data();
        CffHeader {
            hdr_size: obj.hdr_size(),
            off_size: obj.off_size(),
            _padding: obj._padding().to_owned_obj(offset_data),
            trailing_data: obj.trailing_data().to_owned_obj(offset_data),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::ps::cff::v1::CffHeader<'a>> for CffHeader {}

impl<'a> FontRead<'a> for CffHeader {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        <read_fonts::ps::cff::v1::CffHeader as FontRead>::read(data).map(|x| x.to_owned_table())
    }
}

/// An array of variable-sized objects in a `CFF` table.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Index {
    /// Number of objects stored in INDEX.
    pub count: u16,
    /// Object array element size.
    pub off_size: u8,
    /// Bytes containing `count + 1` offsets each of `off_size`.
    pub offsets: Vec<u8>,
    /// Array containing the object data.
    pub data: Vec<u8>,
}

impl Index {
    /// Construct a new `Index`
    pub fn new(count: u16, off_size: u8, offsets: Vec<u8>, data: Vec<u8>) -> Self {
        Self {
            count,
            off_size,
            offsets,
            data,
        }
    }
}

impl FontWrite for Index {
    fn write_into(&self, writer: &mut TableWriter) {
        self.count.write_into(writer);
        self.off_size.write_into(writer);
        self.offsets.write_into(writer);
        self.data.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("Index")
    }
}

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

impl<'a> FromObjRef<read_fonts::ps::cff::v1::Index<'a>> for Index {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::Index<'a>, _: FontData) -> Self {
        let offset_data = obj.offset_data();
        Index {
            count: obj.count(),
            off_size: obj.off_size(),
            offsets: obj.offsets().to_owned_obj(offset_data),
            data: obj.data().to_owned_obj(offset_data),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::ps::cff::v1::Index<'a>> for Index {}

impl<'a> FontRead<'a> for Index {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        <read_fonts::ps::cff::v1::Index as FontRead>::read(data).map(|x| x.to_owned_table())
    }
}

/// Associates a glyph identifier with a Font DICT.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FdSelect {
    Format0(FdSelectFormat0),
    Format3(FdSelectFormat3),
    Format4(FdSelectFormat4),
}

impl FdSelect {
    /// Construct a new `FdSelectFormat0` subtable
    pub fn format_0(fds: Vec<u8>) -> Self {
        Self::Format0(FdSelectFormat0::new(fds))
    }

    /// Construct a new `FdSelectFormat3` subtable
    pub fn format_3(ranges: Vec<FdSelectRange3>, sentinel: u16) -> Self {
        Self::Format3(FdSelectFormat3::new(ranges, sentinel))
    }

    /// Construct a new `FdSelectFormat4` subtable
    pub fn format_4(ranges: Vec<FdSelectRange4>, sentinel: u32) -> Self {
        Self::Format4(FdSelectFormat4::new(ranges, sentinel))
    }
}

impl Default for FdSelect {
    fn default() -> Self {
        Self::Format0(Default::default())
    }
}

impl FontWrite for FdSelect {
    fn write_into(&self, writer: &mut TableWriter) {
        match self {
            Self::Format0(item) => item.write_into(writer),
            Self::Format3(item) => item.write_into(writer),
            Self::Format4(item) => item.write_into(writer),
        }
    }
    fn table_type(&self) -> TableType {
        match self {
            Self::Format0(item) => item.table_type(),
            Self::Format3(item) => item.table_type(),
            Self::Format4(item) => item.table_type(),
        }
    }
}

impl Validate for FdSelect {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        match self {
            Self::Format0(item) => item.validate_impl(ctx),
            Self::Format3(item) => item.validate_impl(ctx),
            Self::Format4(item) => item.validate_impl(ctx),
        }
    }
}

impl FromObjRef<read_fonts::ps::cff::v1::FdSelect<'_>> for FdSelect {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::FdSelect, _: FontData) -> Self {
        use read_fonts::ps::cff::v1::FdSelect as ObjRefType;
        match obj {
            ObjRefType::Format0(item) => FdSelect::Format0(item.to_owned_table()),
            ObjRefType::Format3(item) => FdSelect::Format3(item.to_owned_table()),
            ObjRefType::Format4(item) => FdSelect::Format4(item.to_owned_table()),
        }
    }
}

impl FromTableRef<read_fonts::ps::cff::v1::FdSelect<'_>> for FdSelect {}

impl<'a> FontRead<'a> for FdSelect {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        <read_fonts::ps::cff::v1::FdSelect as FontRead>::read(data).map(|x| x.to_owned_table())
    }
}

impl From<FdSelectFormat0> for FdSelect {
    fn from(src: FdSelectFormat0) -> FdSelect {
        FdSelect::Format0(src)
    }
}

impl From<FdSelectFormat3> for FdSelect {
    fn from(src: FdSelectFormat3) -> FdSelect {
        FdSelect::Format3(src)
    }
}

impl From<FdSelectFormat4> for FdSelect {
    fn from(src: FdSelectFormat4) -> FdSelect {
        FdSelect::Format4(src)
    }
}

/// FdSelect format 0.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FdSelectFormat0 {
    /// FD selector array (one entry for each glyph).
    pub fds: Vec<u8>,
}

impl FdSelectFormat0 {
    /// Construct a new `FdSelectFormat0`
    pub fn new(fds: Vec<u8>) -> Self {
        Self { fds }
    }
}

impl FontWrite for FdSelectFormat0 {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        (0 as u8).write_into(writer);
        self.fds.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("FdSelectFormat0")
    }
}

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

impl<'a> FromObjRef<read_fonts::ps::cff::v1::FdSelectFormat0<'a>> for FdSelectFormat0 {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::FdSelectFormat0<'a>, _: FontData) -> Self {
        let offset_data = obj.offset_data();
        FdSelectFormat0 {
            fds: obj.fds().to_owned_obj(offset_data),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::ps::cff::v1::FdSelectFormat0<'a>> for FdSelectFormat0 {}

impl<'a> FontRead<'a> for FdSelectFormat0 {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        <read_fonts::ps::cff::v1::FdSelectFormat0 as FontRead>::read(data)
            .map(|x| x.to_owned_table())
    }
}

/// FdSelect format 3.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FdSelectFormat3 {
    /// Range3 array.
    pub ranges: Vec<FdSelectRange3>,
    /// Sentinel GID. Set equal to the number of glyphs in the font.
    pub sentinel: u16,
}

impl FdSelectFormat3 {
    /// Construct a new `FdSelectFormat3`
    pub fn new(ranges: Vec<FdSelectRange3>, sentinel: u16) -> Self {
        Self { ranges, sentinel }
    }
}

impl FontWrite for FdSelectFormat3 {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        (3 as u8).write_into(writer);
        (u16::try_from(array_len(&self.ranges)).unwrap()).write_into(writer);
        self.ranges.write_into(writer);
        self.sentinel.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("FdSelectFormat3")
    }
}

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

impl<'a> FromObjRef<read_fonts::ps::cff::v1::FdSelectFormat3<'a>> for FdSelectFormat3 {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::FdSelectFormat3<'a>, _: FontData) -> Self {
        let offset_data = obj.offset_data();
        FdSelectFormat3 {
            ranges: obj.ranges().to_owned_obj(offset_data),
            sentinel: obj.sentinel(),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::ps::cff::v1::FdSelectFormat3<'a>> for FdSelectFormat3 {}

impl<'a> FontRead<'a> for FdSelectFormat3 {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        <read_fonts::ps::cff::v1::FdSelectFormat3 as FontRead>::read(data)
            .map(|x| x.to_owned_table())
    }
}

/// Range struct for FdSelect format 3.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FdSelectRange3 {
    /// First glyph index in range.
    pub first: u16,
    /// FD index for all glyphs in range.
    pub fd: u8,
}

impl FdSelectRange3 {
    /// Construct a new `FdSelectRange3`
    pub fn new(first: u16, fd: u8) -> Self {
        Self { first, fd }
    }
}

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

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

impl FromObjRef<read_fonts::ps::cff::v1::FdSelectRange3> for FdSelectRange3 {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::FdSelectRange3, _: FontData) -> Self {
        FdSelectRange3 {
            first: obj.first(),
            fd: obj.fd(),
        }
    }
}

/// FdSelect format 4.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FdSelectFormat4 {
    /// Range4 array.
    pub ranges: Vec<FdSelectRange4>,
    /// Sentinel GID. Set equal to the number of glyphs in the font.
    pub sentinel: u32,
}

impl FdSelectFormat4 {
    /// Construct a new `FdSelectFormat4`
    pub fn new(ranges: Vec<FdSelectRange4>, sentinel: u32) -> Self {
        Self { ranges, sentinel }
    }
}

impl FontWrite for FdSelectFormat4 {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        (4 as u8).write_into(writer);
        (u32::try_from(array_len(&self.ranges)).unwrap()).write_into(writer);
        self.ranges.write_into(writer);
        self.sentinel.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("FdSelectFormat4")
    }
}

impl Validate for FdSelectFormat4 {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        ctx.in_table("FdSelectFormat4", |ctx| {
            ctx.in_field("ranges", |ctx| {
                if self.ranges.len() > (u32::MAX as usize) {
                    ctx.report("array exceeds max length");
                }
                self.ranges.validate_impl(ctx);
            });
        })
    }
}

impl<'a> FromObjRef<read_fonts::ps::cff::v1::FdSelectFormat4<'a>> for FdSelectFormat4 {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::FdSelectFormat4<'a>, _: FontData) -> Self {
        let offset_data = obj.offset_data();
        FdSelectFormat4 {
            ranges: obj.ranges().to_owned_obj(offset_data),
            sentinel: obj.sentinel(),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::ps::cff::v1::FdSelectFormat4<'a>> for FdSelectFormat4 {}

impl<'a> FontRead<'a> for FdSelectFormat4 {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        <read_fonts::ps::cff::v1::FdSelectFormat4 as FontRead>::read(data)
            .map(|x| x.to_owned_table())
    }
}

/// Range struct for FdSelect format 4.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FdSelectRange4 {
    /// First glyph index in range.
    pub first: u32,
    /// FD index for all glyphs in range.
    pub fd: u16,
}

impl FdSelectRange4 {
    /// Construct a new `FdSelectRange4`
    pub fn new(first: u32, fd: u16) -> Self {
        Self { first, fd }
    }
}

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

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

impl FromObjRef<read_fonts::ps::cff::v1::FdSelectRange4> for FdSelectRange4 {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::FdSelectRange4, _: FontData) -> Self {
        FdSelectRange4 {
            first: obj.first(),
            fd: obj.fd(),
        }
    }
}

/// Charset with custom glyph id to string id mappings.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CustomCharset {
    Format0(CharsetFormat0),
    Format1(CharsetFormat1),
    Format2(CharsetFormat2),
}

impl CustomCharset {
    /// Construct a new `CharsetFormat0` subtable
    pub fn format_0(glyph: Vec<u16>) -> Self {
        Self::Format0(CharsetFormat0::new(glyph))
    }

    /// Construct a new `CharsetFormat1` subtable
    pub fn format_1(ranges: Vec<CharsetRange1>) -> Self {
        Self::Format1(CharsetFormat1::new(ranges))
    }

    /// Construct a new `CharsetFormat2` subtable
    pub fn format_2(ranges: Vec<CharsetRange2>) -> Self {
        Self::Format2(CharsetFormat2::new(ranges))
    }
}

impl Default for CustomCharset {
    fn default() -> Self {
        Self::Format0(Default::default())
    }
}

impl FontWrite for CustomCharset {
    fn write_into(&self, writer: &mut TableWriter) {
        match self {
            Self::Format0(item) => item.write_into(writer),
            Self::Format1(item) => item.write_into(writer),
            Self::Format2(item) => item.write_into(writer),
        }
    }
    fn table_type(&self) -> TableType {
        match self {
            Self::Format0(item) => item.table_type(),
            Self::Format1(item) => item.table_type(),
            Self::Format2(item) => item.table_type(),
        }
    }
}

impl Validate for CustomCharset {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        match self {
            Self::Format0(item) => item.validate_impl(ctx),
            Self::Format1(item) => item.validate_impl(ctx),
            Self::Format2(item) => item.validate_impl(ctx),
        }
    }
}

impl FromObjRef<read_fonts::ps::cff::v1::CustomCharset<'_>> for CustomCharset {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::CustomCharset, _: FontData) -> Self {
        use read_fonts::ps::cff::v1::CustomCharset as ObjRefType;
        match obj {
            ObjRefType::Format0(item) => CustomCharset::Format0(item.to_owned_table()),
            ObjRefType::Format1(item) => CustomCharset::Format1(item.to_owned_table()),
            ObjRefType::Format2(item) => CustomCharset::Format2(item.to_owned_table()),
        }
    }
}

impl FromTableRef<read_fonts::ps::cff::v1::CustomCharset<'_>> for CustomCharset {}

impl<'a> FontRead<'a> for CustomCharset {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        <read_fonts::ps::cff::v1::CustomCharset as FontRead>::read(data).map(|x| x.to_owned_table())
    }
}

impl From<CharsetFormat0> for CustomCharset {
    fn from(src: CharsetFormat0) -> CustomCharset {
        CustomCharset::Format0(src)
    }
}

impl From<CharsetFormat1> for CustomCharset {
    fn from(src: CharsetFormat1) -> CustomCharset {
        CustomCharset::Format1(src)
    }
}

impl From<CharsetFormat2> for CustomCharset {
    fn from(src: CharsetFormat2) -> CustomCharset {
        CustomCharset::Format2(src)
    }
}

/// Charset format 0.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CharsetFormat0 {
    /// Glyph name array.
    pub glyph: Vec<u16>,
}

impl CharsetFormat0 {
    /// Construct a new `CharsetFormat0`
    pub fn new(glyph: Vec<u16>) -> Self {
        Self { glyph }
    }
}

impl FontWrite for CharsetFormat0 {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        (0 as u8).write_into(writer);
        self.glyph.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("CharsetFormat0")
    }
}

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

impl<'a> FromObjRef<read_fonts::ps::cff::v1::CharsetFormat0<'a>> for CharsetFormat0 {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::CharsetFormat0<'a>, _: FontData) -> Self {
        let offset_data = obj.offset_data();
        CharsetFormat0 {
            glyph: obj.glyph().to_owned_obj(offset_data),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::ps::cff::v1::CharsetFormat0<'a>> for CharsetFormat0 {}

impl<'a> FontRead<'a> for CharsetFormat0 {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        <read_fonts::ps::cff::v1::CharsetFormat0 as FontRead>::read(data)
            .map(|x| x.to_owned_table())
    }
}

/// Charset format 1.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CharsetFormat1 {
    /// Range1 array.
    pub ranges: Vec<CharsetRange1>,
}

impl CharsetFormat1 {
    /// Construct a new `CharsetFormat1`
    pub fn new(ranges: Vec<CharsetRange1>) -> Self {
        Self { ranges }
    }
}

impl FontWrite for CharsetFormat1 {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        (1 as u8).write_into(writer);
        self.ranges.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("CharsetFormat1")
    }
}

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

impl<'a> FromObjRef<read_fonts::ps::cff::v1::CharsetFormat1<'a>> for CharsetFormat1 {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::CharsetFormat1<'a>, _: FontData) -> Self {
        let offset_data = obj.offset_data();
        CharsetFormat1 {
            ranges: obj.ranges().to_owned_obj(offset_data),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::ps::cff::v1::CharsetFormat1<'a>> for CharsetFormat1 {}

impl<'a> FontRead<'a> for CharsetFormat1 {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        <read_fonts::ps::cff::v1::CharsetFormat1 as FontRead>::read(data)
            .map(|x| x.to_owned_table())
    }
}

/// Range struct for Charset format 1.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CharsetRange1 {
    /// First glyph in range.
    pub first: u16,
    /// Glyphs left in range (excluding first).
    pub n_left: u8,
}

impl CharsetRange1 {
    /// Construct a new `CharsetRange1`
    pub fn new(first: u16, n_left: u8) -> Self {
        Self { first, n_left }
    }
}

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

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

impl FromObjRef<read_fonts::ps::cff::v1::CharsetRange1> for CharsetRange1 {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::CharsetRange1, _: FontData) -> Self {
        CharsetRange1 {
            first: obj.first(),
            n_left: obj.n_left(),
        }
    }
}

/// Charset format 2.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CharsetFormat2 {
    /// Range2 array.
    pub ranges: Vec<CharsetRange2>,
}

impl CharsetFormat2 {
    /// Construct a new `CharsetFormat2`
    pub fn new(ranges: Vec<CharsetRange2>) -> Self {
        Self { ranges }
    }
}

impl FontWrite for CharsetFormat2 {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        (2 as u8).write_into(writer);
        self.ranges.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("CharsetFormat2")
    }
}

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

impl<'a> FromObjRef<read_fonts::ps::cff::v1::CharsetFormat2<'a>> for CharsetFormat2 {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::CharsetFormat2<'a>, _: FontData) -> Self {
        let offset_data = obj.offset_data();
        CharsetFormat2 {
            ranges: obj.ranges().to_owned_obj(offset_data),
        }
    }
}

#[allow(clippy::needless_lifetimes)]
impl<'a> FromTableRef<read_fonts::ps::cff::v1::CharsetFormat2<'a>> for CharsetFormat2 {}

impl<'a> FontRead<'a> for CharsetFormat2 {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        <read_fonts::ps::cff::v1::CharsetFormat2 as FontRead>::read(data)
            .map(|x| x.to_owned_table())
    }
}

/// Range struct for Charset format 2.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CharsetRange2 {
    /// First glyph in range.
    pub first: u16,
    /// Glyphs left in range (excluding first).
    pub n_left: u16,
}

impl CharsetRange2 {
    /// Construct a new `CharsetRange2`
    pub fn new(first: u16, n_left: u16) -> Self {
        Self { first, n_left }
    }
}

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

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

impl FromObjRef<read_fonts::ps::cff::v1::CharsetRange2> for CharsetRange2 {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::CharsetRange2, _: FontData) -> Self {
        CharsetRange2 {
            first: obj.first(),
            n_left: obj.n_left(),
        }
    }
}

/// Range struct for Encoding format 1.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EncodingRange1 {
    /// First code in range.
    pub first: u8,
    /// Codes left in range (excluding first).
    pub n_left: u8,
}

impl EncodingRange1 {
    /// Construct a new `EncodingRange1`
    pub fn new(first: u8, n_left: u8) -> Self {
        Self { first, n_left }
    }
}

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

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

impl FromObjRef<read_fonts::ps::cff::v1::EncodingRange1> for EncodingRange1 {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::EncodingRange1, _: FontData) -> Self {
        EncodingRange1 {
            first: obj.first(),
            n_left: obj.n_left(),
        }
    }
}

/// Supplemental encoding record.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EncodingSupplement {
    /// Encoding.
    pub code: u8,
    /// Name.
    pub glyph: u16,
}

impl EncodingSupplement {
    /// Construct a new `EncodingSupplement`
    pub fn new(code: u8, glyph: u16) -> Self {
        Self { code, glyph }
    }
}

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

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

impl FromObjRef<read_fonts::ps::cff::v1::EncodingSupplement> for EncodingSupplement {
    fn from_obj_ref(obj: &read_fonts::ps::cff::v1::EncodingSupplement, _: FontData) -> Self {
        EncodingSupplement {
            code: obj.code(),
            glyph: obj.glyph(),
        }
    }
}