write_fonts/generated/
generated_hmtx.rs

1// THIS FILE IS AUTOGENERATED.
2// Any changes to this file will be overwritten.
3// For more information about how codegen works, see font-codegen/README.md
4
5#[allow(unused_imports)]
6use crate::codegen_prelude::*;
7
8/// The [hmtx (Horizontal Metrics)](https://docs.microsoft.com/en-us/typography/opentype/spec/hmtx) table
9#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct Hmtx {
12    /// Paired advance width/height and left/top side bearing values for each
13    /// glyph. Records are indexed by glyph ID.
14    pub h_metrics: Vec<LongMetric>,
15    /// Leading (left/top) side bearings for glyph IDs greater than or equal to
16    /// numberOfLongMetrics.
17    pub left_side_bearings: Vec<i16>,
18}
19
20impl Hmtx {
21    /// Construct a new `Hmtx`
22    pub fn new(h_metrics: Vec<LongMetric>, left_side_bearings: Vec<i16>) -> Self {
23        Self {
24            h_metrics,
25            left_side_bearings,
26        }
27    }
28}
29
30impl FontWrite for Hmtx {
31    fn write_into(&self, writer: &mut TableWriter) {
32        self.h_metrics.write_into(writer);
33        self.left_side_bearings.write_into(writer);
34    }
35    fn table_type(&self) -> TableType {
36        TableType::TopLevel(Hmtx::TAG)
37    }
38}
39
40impl Validate for Hmtx {
41    fn validate_impl(&self, ctx: &mut ValidationCtx) {
42        ctx.in_table("Hmtx", |ctx| {
43            ctx.in_field("h_metrics", |ctx| {
44                if self.h_metrics.len() > (u16::MAX as usize) {
45                    ctx.report("array exceeds max length");
46                }
47                self.h_metrics.validate_impl(ctx);
48            });
49        })
50    }
51}
52
53impl TopLevelTable for Hmtx {
54    const TAG: Tag = Tag::new(b"hmtx");
55}
56
57impl<'a> FromObjRef<read_fonts::tables::hmtx::Hmtx<'a>> for Hmtx {
58    fn from_obj_ref(obj: &read_fonts::tables::hmtx::Hmtx<'a>, _: FontData) -> Self {
59        let offset_data = obj.offset_data();
60        Hmtx {
61            h_metrics: obj.h_metrics().to_owned_obj(offset_data),
62            left_side_bearings: obj.left_side_bearings().to_owned_obj(offset_data),
63        }
64    }
65}
66
67#[allow(clippy::needless_lifetimes)]
68impl<'a> FromTableRef<read_fonts::tables::hmtx::Hmtx<'a>> for Hmtx {}
69
70#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
71#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
72pub struct LongMetric {
73    /// Advance width/height, in font design units.
74    pub advance: u16,
75    /// Glyph leading (left/top) side bearing, in font design units.
76    pub side_bearing: i16,
77}
78
79impl LongMetric {
80    /// Construct a new `LongMetric`
81    pub fn new(advance: u16, side_bearing: i16) -> Self {
82        Self {
83            advance,
84            side_bearing,
85        }
86    }
87}
88
89impl FontWrite for LongMetric {
90    fn write_into(&self, writer: &mut TableWriter) {
91        self.advance.write_into(writer);
92        self.side_bearing.write_into(writer);
93    }
94    fn table_type(&self) -> TableType {
95        TableType::Named("LongMetric")
96    }
97}
98
99impl Validate for LongMetric {
100    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
101}
102
103impl FromObjRef<read_fonts::tables::hmtx::LongMetric> for LongMetric {
104    fn from_obj_ref(obj: &read_fonts::tables::hmtx::LongMetric, _: FontData) -> Self {
105        LongMetric {
106            advance: obj.advance(),
107            side_bearing: obj.side_bearing(),
108        }
109    }
110}