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

/// [post (PostScript)](https://docs.microsoft.com/en-us/typography/opentype/spec/post#header) table
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct PostMarker {
    num_glyphs_byte_start: Option<usize>,
    glyph_name_index_byte_start: Option<usize>,
    glyph_name_index_byte_len: Option<usize>,
    string_data_byte_start: Option<usize>,
    string_data_byte_len: Option<usize>,
}

impl PostMarker {
    fn version_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + Version16Dot16::RAW_BYTE_LEN
    }
    fn italic_angle_byte_range(&self) -> Range<usize> {
        let start = self.version_byte_range().end;
        start..start + Fixed::RAW_BYTE_LEN
    }
    fn underline_position_byte_range(&self) -> Range<usize> {
        let start = self.italic_angle_byte_range().end;
        start..start + FWord::RAW_BYTE_LEN
    }
    fn underline_thickness_byte_range(&self) -> Range<usize> {
        let start = self.underline_position_byte_range().end;
        start..start + FWord::RAW_BYTE_LEN
    }
    fn is_fixed_pitch_byte_range(&self) -> Range<usize> {
        let start = self.underline_thickness_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }
    fn min_mem_type42_byte_range(&self) -> Range<usize> {
        let start = self.is_fixed_pitch_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }
    fn max_mem_type42_byte_range(&self) -> Range<usize> {
        let start = self.min_mem_type42_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }
    fn min_mem_type1_byte_range(&self) -> Range<usize> {
        let start = self.max_mem_type42_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }
    fn max_mem_type1_byte_range(&self) -> Range<usize> {
        let start = self.min_mem_type1_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }
    fn num_glyphs_byte_range(&self) -> Option<Range<usize>> {
        let start = self.num_glyphs_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn glyph_name_index_byte_range(&self) -> Option<Range<usize>> {
        let start = self.glyph_name_index_byte_start?;
        Some(start..start + self.glyph_name_index_byte_len?)
    }
    fn string_data_byte_range(&self) -> Option<Range<usize>> {
        let start = self.string_data_byte_start?;
        Some(start..start + self.string_data_byte_len?)
    }
}

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

impl<'a> FontRead<'a> for Post<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        let version: Version16Dot16 = cursor.read()?;
        cursor.advance::<Fixed>();
        cursor.advance::<FWord>();
        cursor.advance::<FWord>();
        cursor.advance::<u32>();
        cursor.advance::<u32>();
        cursor.advance::<u32>();
        cursor.advance::<u32>();
        cursor.advance::<u32>();
        let num_glyphs_byte_start = version
            .compatible((2u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        let num_glyphs = version
            .compatible((2u16, 0u16))
            .then(|| cursor.read::<u16>())
            .transpose()?
            .unwrap_or(0);
        let glyph_name_index_byte_start = version
            .compatible((2u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        let glyph_name_index_byte_len = version.compatible((2u16, 0u16)).then_some(
            (num_glyphs as usize)
                .checked_mul(u16::RAW_BYTE_LEN)
                .ok_or(ReadError::OutOfBounds)?,
        );
        if let Some(value) = glyph_name_index_byte_len {
            cursor.advance_by(value);
        }
        let string_data_byte_start = version
            .compatible((2u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        let string_data_byte_len = version
            .compatible((2u16, 0u16))
            .then_some(cursor.remaining_bytes());
        if let Some(value) = string_data_byte_len {
            cursor.advance_by(value);
        }
        cursor.finish(PostMarker {
            num_glyphs_byte_start,
            glyph_name_index_byte_start,
            glyph_name_index_byte_len,
            string_data_byte_start,
            string_data_byte_len,
        })
    }
}

/// [post (PostScript)](https://docs.microsoft.com/en-us/typography/opentype/spec/post#header) table
pub type Post<'a> = TableRef<'a, PostMarker>;

impl<'a> Post<'a> {
    /// 0x00010000 for version 1.0 0x00020000 for version 2.0
    /// 0x00025000 for version 2.5 (deprecated) 0x00030000 for version
    /// 3.0
    pub fn version(&self) -> Version16Dot16 {
        let range = self.shape.version_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Italic angle in counter-clockwise degrees from the vertical.
    /// Zero for upright text, negative for text that leans to the
    /// right (forward).
    pub fn italic_angle(&self) -> Fixed {
        let range = self.shape.italic_angle_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// This is the suggested distance of the top of the underline from
    /// the baseline (negative values indicate below baseline). The
    /// PostScript definition of this FontInfo dictionary key (the y
    /// coordinate of the center of the stroke) is not used for
    /// historical reasons. The value of the PostScript key may be
    /// calculated by subtracting half the underlineThickness from the
    /// value of this field.
    pub fn underline_position(&self) -> FWord {
        let range = self.shape.underline_position_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Suggested values for the underline thickness. In general, the
    /// underline thickness should match the thickness of the
    /// underscore character (U+005F LOW LINE), and should also match
    /// the strikeout thickness, which is specified in the OS/2 table.
    pub fn underline_thickness(&self) -> FWord {
        let range = self.shape.underline_thickness_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Set to 0 if the font is proportionally spaced, non-zero if the
    /// font is not proportionally spaced (i.e. monospaced).
    pub fn is_fixed_pitch(&self) -> u32 {
        let range = self.shape.is_fixed_pitch_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Minimum memory usage when an OpenType font is downloaded.
    pub fn min_mem_type42(&self) -> u32 {
        let range = self.shape.min_mem_type42_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Maximum memory usage when an OpenType font is downloaded.
    pub fn max_mem_type42(&self) -> u32 {
        let range = self.shape.max_mem_type42_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Minimum memory usage when an OpenType font is downloaded as a
    /// Type 1 font.
    pub fn min_mem_type1(&self) -> u32 {
        let range = self.shape.min_mem_type1_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Maximum memory usage when an OpenType font is downloaded as a
    /// Type 1 font.
    pub fn max_mem_type1(&self) -> u32 {
        let range = self.shape.max_mem_type1_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Number of glyphs (this should be the same as numGlyphs in
    /// 'maxp' table).
    pub fn num_glyphs(&self) -> Option<u16> {
        let range = self.shape.num_glyphs_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// Array of indices into the string data. See below for details.
    pub fn glyph_name_index(&self) -> Option<&'a [BigEndian<u16>]> {
        let range = self.shape.glyph_name_index_byte_range()?;
        Some(self.data.read_array(range).unwrap())
    }

    /// Storage for the string data.
    pub fn string_data(&self) -> Option<VarLenArray<'a, PString<'a>>> {
        let range = self.shape.string_data_byte_range()?;
        Some(VarLenArray::read(self.data.split_off(range.start).unwrap()).unwrap())
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Post<'a> {
    fn type_name(&self) -> &str {
        "Post"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        let version = self.version();
        match idx {
            0usize => Some(Field::new("version", self.version())),
            1usize => Some(Field::new("italic_angle", self.italic_angle())),
            2usize => Some(Field::new("underline_position", self.underline_position())),
            3usize => Some(Field::new(
                "underline_thickness",
                self.underline_thickness(),
            )),
            4usize => Some(Field::new("is_fixed_pitch", self.is_fixed_pitch())),
            5usize => Some(Field::new("min_mem_type42", self.min_mem_type42())),
            6usize => Some(Field::new("max_mem_type42", self.max_mem_type42())),
            7usize => Some(Field::new("min_mem_type1", self.min_mem_type1())),
            8usize => Some(Field::new("max_mem_type1", self.max_mem_type1())),
            9usize if version.compatible((2u16, 0u16)) => {
                Some(Field::new("num_glyphs", self.num_glyphs().unwrap()))
            }
            10usize if version.compatible((2u16, 0u16)) => Some(Field::new(
                "glyph_name_index",
                self.glyph_name_index().unwrap(),
            )),
            11usize if version.compatible((2u16, 0u16)) => {
                Some(Field::new("string_data", self.traverse_string_data()))
            }
            _ => None,
        }
    }
}

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