Skip to main content

read_fonts/generated/
generated_post.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
8impl<'a> MinByteRange<'a> for Post<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.max_mem_type1_byte_range().end
11    }
12    fn min_table_bytes(&self) -> &'a [u8] {
13        let range = self.min_byte_range();
14        self.data.as_bytes().get(range).unwrap_or_default()
15    }
16}
17
18impl TopLevelTable for Post<'_> {
19    /// `post`
20    const TAG: Tag = Tag::new(b"post");
21}
22
23impl ReadArgs for Post<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Post<'a> {
28    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
29        #[allow(clippy::absurd_extreme_comparisons)]
30        if data.len() < Self::MIN_SIZE {
31            return Err(ReadError::OutOfBounds);
32        }
33        Ok(Self { data })
34    }
35}
36
37/// [post (PostScript)](https://docs.microsoft.com/en-us/typography/opentype/spec/post#header) table
38#[derive(Clone)]
39pub struct Post<'a> {
40    data: FontData<'a>,
41}
42
43#[allow(clippy::needless_lifetimes)]
44impl<'a> Post<'a> {
45    pub const MIN_SIZE: usize = (Version16Dot16::RAW_BYTE_LEN
46        + Fixed::RAW_BYTE_LEN
47        + FWord::RAW_BYTE_LEN
48        + FWord::RAW_BYTE_LEN
49        + u32::RAW_BYTE_LEN
50        + u32::RAW_BYTE_LEN
51        + u32::RAW_BYTE_LEN
52        + u32::RAW_BYTE_LEN
53        + u32::RAW_BYTE_LEN);
54    basic_table_impls!(impl_the_methods);
55
56    /// 0x00010000 for version 1.0 0x00020000 for version 2.0
57    /// 0x00025000 for version 2.5 (deprecated) 0x00030000 for version
58    /// 3.0
59    pub fn version(&self) -> Version16Dot16 {
60        let range = self.version_byte_range();
61        self.data.read_at(range.start).ok().unwrap()
62    }
63
64    /// Italic angle in counter-clockwise degrees from the vertical.
65    /// Zero for upright text, negative for text that leans to the
66    /// right (forward).
67    pub fn italic_angle(&self) -> Fixed {
68        let range = self.italic_angle_byte_range();
69        self.data.read_at(range.start).ok().unwrap()
70    }
71
72    /// This is the suggested distance of the top of the underline from
73    /// the baseline (negative values indicate below baseline). The
74    /// PostScript definition of this FontInfo dictionary key (the y
75    /// coordinate of the center of the stroke) is not used for
76    /// historical reasons. The value of the PostScript key may be
77    /// calculated by subtracting half the underlineThickness from the
78    /// value of this field.
79    pub fn underline_position(&self) -> FWord {
80        let range = self.underline_position_byte_range();
81        self.data.read_at(range.start).ok().unwrap()
82    }
83
84    /// Suggested values for the underline thickness. In general, the
85    /// underline thickness should match the thickness of the
86    /// underscore character (U+005F LOW LINE), and should also match
87    /// the strikeout thickness, which is specified in the OS/2 table.
88    pub fn underline_thickness(&self) -> FWord {
89        let range = self.underline_thickness_byte_range();
90        self.data.read_at(range.start).ok().unwrap()
91    }
92
93    /// Set to 0 if the font is proportionally spaced, non-zero if the
94    /// font is not proportionally spaced (i.e. monospaced).
95    pub fn is_fixed_pitch(&self) -> u32 {
96        let range = self.is_fixed_pitch_byte_range();
97        self.data.read_at(range.start).ok().unwrap()
98    }
99
100    /// Minimum memory usage when an OpenType font is downloaded.
101    pub fn min_mem_type42(&self) -> u32 {
102        let range = self.min_mem_type42_byte_range();
103        self.data.read_at(range.start).ok().unwrap()
104    }
105
106    /// Maximum memory usage when an OpenType font is downloaded.
107    pub fn max_mem_type42(&self) -> u32 {
108        let range = self.max_mem_type42_byte_range();
109        self.data.read_at(range.start).ok().unwrap()
110    }
111
112    /// Minimum memory usage when an OpenType font is downloaded as a
113    /// Type 1 font.
114    pub fn min_mem_type1(&self) -> u32 {
115        let range = self.min_mem_type1_byte_range();
116        self.data.read_at(range.start).ok().unwrap()
117    }
118
119    /// Maximum memory usage when an OpenType font is downloaded as a
120    /// Type 1 font.
121    pub fn max_mem_type1(&self) -> u32 {
122        let range = self.max_mem_type1_byte_range();
123        self.data.read_at(range.start).ok().unwrap()
124    }
125
126    /// Number of glyphs (this should be the same as numGlyphs in
127    /// 'maxp' table).
128    pub fn num_glyphs(&self) -> Option<u16> {
129        let range = self.num_glyphs_byte_range();
130        (!range.is_empty())
131            .then(|| self.data.read_at(range.start).ok())
132            .flatten()
133    }
134
135    /// Array of indices into the string data. See below for details.
136    pub fn glyph_name_index(&self) -> Option<&'a [BigEndian<u16>]> {
137        let range = self.glyph_name_index_byte_range();
138        (!range.is_empty())
139            .then(|| self.data.read_array(range).ok())
140            .flatten()
141    }
142
143    /// Storage for the string data.
144    pub fn string_data(&self) -> Option<VarLenArray<'a, PString<'a>>> {
145        let range = self.string_data_byte_range();
146        (!range.is_empty())
147            .then(|| {
148                self.data
149                    .split_off(range.start)
150                    .and_then(|d| VarLenArray::read(d).ok())
151            })
152            .flatten()
153    }
154
155    pub fn version_byte_range(&self) -> Range<usize> {
156        let start = 0;
157        let end = start + Version16Dot16::RAW_BYTE_LEN;
158        start..end
159    }
160
161    pub fn italic_angle_byte_range(&self) -> Range<usize> {
162        let start = self.version_byte_range().end;
163        let end = start + Fixed::RAW_BYTE_LEN;
164        start..end
165    }
166
167    pub fn underline_position_byte_range(&self) -> Range<usize> {
168        let start = self.italic_angle_byte_range().end;
169        let end = start + FWord::RAW_BYTE_LEN;
170        start..end
171    }
172
173    pub fn underline_thickness_byte_range(&self) -> Range<usize> {
174        let start = self.underline_position_byte_range().end;
175        let end = start + FWord::RAW_BYTE_LEN;
176        start..end
177    }
178
179    pub fn is_fixed_pitch_byte_range(&self) -> Range<usize> {
180        let start = self.underline_thickness_byte_range().end;
181        let end = start + u32::RAW_BYTE_LEN;
182        start..end
183    }
184
185    pub fn min_mem_type42_byte_range(&self) -> Range<usize> {
186        let start = self.is_fixed_pitch_byte_range().end;
187        let end = start + u32::RAW_BYTE_LEN;
188        start..end
189    }
190
191    pub fn max_mem_type42_byte_range(&self) -> Range<usize> {
192        let start = self.min_mem_type42_byte_range().end;
193        let end = start + u32::RAW_BYTE_LEN;
194        start..end
195    }
196
197    pub fn min_mem_type1_byte_range(&self) -> Range<usize> {
198        let start = self.max_mem_type42_byte_range().end;
199        let end = start + u32::RAW_BYTE_LEN;
200        start..end
201    }
202
203    pub fn max_mem_type1_byte_range(&self) -> Range<usize> {
204        let start = self.min_mem_type1_byte_range().end;
205        let end = start + u32::RAW_BYTE_LEN;
206        start..end
207    }
208
209    pub fn num_glyphs_byte_range(&self) -> Range<usize> {
210        let start = self.max_mem_type1_byte_range().end;
211        let end = if self.version().compatible((2u16, 0u16)) {
212            start + u16::RAW_BYTE_LEN
213        } else {
214            start
215        };
216        start..end
217    }
218
219    pub fn glyph_name_index_byte_range(&self) -> Range<usize> {
220        let num_glyphs = self.num_glyphs().unwrap_or_default();
221        let start = self.num_glyphs_byte_range().end;
222        let end = if self.version().compatible((2u16, 0u16)) {
223            start + (transforms::to_usize(num_glyphs)).saturating_mul(u16::RAW_BYTE_LEN)
224        } else {
225            start
226        };
227        start..end
228    }
229
230    pub fn string_data_byte_range(&self) -> Range<usize> {
231        let start = self.glyph_name_index_byte_range().end;
232        let end = if self.version().compatible((2u16, 0u16)) {
233            start + self.data.len().saturating_sub(start)
234        } else {
235            start
236        };
237        start..end
238    }
239}
240
241const _: () = assert!(FontData::default_data_long_enough(Post::MIN_SIZE));
242
243impl Default for Post<'_> {
244    fn default() -> Self {
245        Self {
246            data: FontData::default_table_data(),
247        }
248    }
249}
250
251#[cfg(feature = "experimental_traverse")]
252impl<'a> SomeTable<'a> for Post<'a> {
253    fn type_name(&self) -> &str {
254        "Post"
255    }
256    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
257        match idx {
258            0usize => Some(Field::new("version", self.version())),
259            1usize => Some(Field::new("italic_angle", self.italic_angle())),
260            2usize => Some(Field::new("underline_position", self.underline_position())),
261            3usize => Some(Field::new(
262                "underline_thickness",
263                self.underline_thickness(),
264            )),
265            4usize => Some(Field::new("is_fixed_pitch", self.is_fixed_pitch())),
266            5usize => Some(Field::new("min_mem_type42", self.min_mem_type42())),
267            6usize => Some(Field::new("max_mem_type42", self.max_mem_type42())),
268            7usize => Some(Field::new("min_mem_type1", self.min_mem_type1())),
269            8usize => Some(Field::new("max_mem_type1", self.max_mem_type1())),
270            9usize if self.version().compatible((2u16, 0u16)) => {
271                Some(Field::new("num_glyphs", self.num_glyphs().unwrap()))
272            }
273            10usize if self.version().compatible((2u16, 0u16)) => Some(Field::new(
274                "glyph_name_index",
275                self.glyph_name_index().unwrap(),
276            )),
277            11usize if self.version().compatible((2u16, 0u16)) => {
278                Some(Field::new("string_data", self.traverse_string_data()))
279            }
280            _ => None,
281        }
282    }
283}
284
285#[cfg(feature = "experimental_traverse")]
286#[allow(clippy::needless_lifetimes)]
287impl<'a> std::fmt::Debug for Post<'a> {
288    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
289        (self as &dyn SomeTable<'a>).fmt(f)
290    }
291}