read_fonts/generated/
generated_hmtx.rs

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

/// The [hmtx (Horizontal Metrics)](https://docs.microsoft.com/en-us/typography/opentype/spec/hmtx) table
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct HmtxMarker {
    h_metrics_byte_len: usize,
    left_side_bearings_byte_len: usize,
}

impl HmtxMarker {
    fn h_metrics_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + self.h_metrics_byte_len
    }
    fn left_side_bearings_byte_range(&self) -> Range<usize> {
        let start = self.h_metrics_byte_range().end;
        start..start + self.left_side_bearings_byte_len
    }
}

impl TopLevelTable for Hmtx<'_> {
    /// `hmtx`
    const TAG: Tag = Tag::new(b"hmtx");
}

impl ReadArgs for Hmtx<'_> {
    type Args = (u16, u16);
}

impl<'a> FontReadWithArgs<'a> for Hmtx<'a> {
    fn read_with_args(data: FontData<'a>, args: &(u16, u16)) -> Result<Self, ReadError> {
        let (number_of_h_metrics, num_glyphs) = *args;
        let mut cursor = data.cursor();
        let h_metrics_byte_len = (number_of_h_metrics as usize)
            .checked_mul(LongMetric::RAW_BYTE_LEN)
            .ok_or(ReadError::OutOfBounds)?;
        cursor.advance_by(h_metrics_byte_len);
        let left_side_bearings_byte_len = (transforms::subtract(num_glyphs, number_of_h_metrics))
            .checked_mul(i16::RAW_BYTE_LEN)
            .ok_or(ReadError::OutOfBounds)?;
        cursor.advance_by(left_side_bearings_byte_len);
        cursor.finish(HmtxMarker {
            h_metrics_byte_len,
            left_side_bearings_byte_len,
        })
    }
}

impl<'a> Hmtx<'a> {
    /// A constructor that requires additional arguments.
    ///
    /// This type requires some external state in order to be
    /// parsed.
    pub fn read(
        data: FontData<'a>,
        number_of_h_metrics: u16,
        num_glyphs: u16,
    ) -> Result<Self, ReadError> {
        let args = (number_of_h_metrics, num_glyphs);
        Self::read_with_args(data, &args)
    }
}

/// The [hmtx (Horizontal Metrics)](https://docs.microsoft.com/en-us/typography/opentype/spec/hmtx) table
pub type Hmtx<'a> = TableRef<'a, HmtxMarker>;

impl<'a> Hmtx<'a> {
    /// Paired advance width/height and left/top side bearing values for each
    /// glyph. Records are indexed by glyph ID.
    pub fn h_metrics(&self) -> &'a [LongMetric] {
        let range = self.shape.h_metrics_byte_range();
        self.data.read_array(range).unwrap()
    }

    /// Leading (left/top) side bearings for glyph IDs greater than or equal to
    /// numberOfLongMetrics.
    pub fn left_side_bearings(&self) -> &'a [BigEndian<i16>] {
        let range = self.shape.left_side_bearings_byte_range();
        self.data.read_array(range).unwrap()
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Hmtx<'a> {
    fn type_name(&self) -> &str {
        "Hmtx"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new(
                "h_metrics",
                traversal::FieldType::array_of_records(
                    stringify!(LongMetric),
                    self.h_metrics(),
                    self.offset_data(),
                ),
            )),
            1usize => Some(Field::new("left_side_bearings", self.left_side_bearings())),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> std::fmt::Debug for Hmtx<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct LongMetric {
    /// Advance width/height, in font design units.
    pub advance: BigEndian<u16>,
    /// Glyph leading (left/top) side bearing, in font design units.
    pub side_bearing: BigEndian<i16>,
}

impl LongMetric {
    /// Advance width/height, in font design units.
    pub fn advance(&self) -> u16 {
        self.advance.get()
    }

    /// Glyph leading (left/top) side bearing, in font design units.
    pub fn side_bearing(&self) -> i16 {
        self.side_bearing.get()
    }
}

impl FixedSize for LongMetric {
    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + i16::RAW_BYTE_LEN;
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeRecord<'a> for LongMetric {
    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
        RecordResolver {
            name: "LongMetric",
            get_field: Box::new(move |idx, _data| match idx {
                0usize => Some(Field::new("advance", self.advance())),
                1usize => Some(Field::new("side_bearing", self.side_bearing())),
                _ => None,
            }),
            data,
        }
    }
}