ttf_parser/tables/
maxp.rs

1// https://docs.microsoft.com/en-us/typography/opentype/spec/maxp
2
3use core::num::NonZeroU16;
4
5use crate::parser::Stream;
6
7// We care only about `numGlyphs`.
8pub fn parse(data: &[u8]) -> Option<NonZeroU16> {
9    let mut s = Stream::new(data);
10    let version: u32 = s.read()?;
11    if !(version == 0x00005000 || version == 0x00010000) {
12        return None;
13    }
14
15    let n: u16 = s.read()?;
16    NonZeroU16::new(n)
17}
18
19
20#[cfg(test)]
21mod tests {
22    #[test]
23    fn version_05() {
24        let num_glyphs = super::parse(&[
25            0x00, 0x00, 0x50, 0x00, // version: 0.3125
26            0x00, 0x01, // number of glyphs: 1
27        ]).map(|n| n.get());
28        assert_eq!(num_glyphs, Some(1));
29    }
30
31    #[test]
32    fn version_1_full() {
33        let num_glyphs = super::parse(&[
34            0x00, 0x01, 0x00, 0x00, // version: 1
35            0x00, 0x01, // number of glyphs: 1
36            0x00, 0x00, // maximum points in a non-composite glyph: 0
37            0x00, 0x00, // maximum contours in a non-composite glyph: 0
38            0x00, 0x00, // maximum points in a composite glyph: 0
39            0x00, 0x00, // maximum contours in a composite glyph: 0
40            0x00, 0x00, // maximum zones: 0
41            0x00, 0x00, // maximum twilight points: 0
42            0x00, 0x00, // number of Storage Area locations: 0
43            0x00, 0x00, // number of FDEFs: 0
44            0x00, 0x00, // number of IDEFs: 0
45            0x00, 0x00, // maximum stack depth: 0
46            0x00, 0x00, // maximum byte count for glyph instructions: 0
47            0x00, 0x00, // maximum number of components: 0
48            0x00, 0x00, // maximum levels of recursion: 0
49        ]).map(|n| n.get());
50        assert_eq!(num_glyphs, Some(1));
51    }
52
53    #[test]
54    fn version_1_trimmed() {
55        // We don't really care about the data after the number of glyphs.
56        let num_glyphs = super::parse(&[
57            0x00, 0x01, 0x00, 0x00, // version: 1
58            0x00, 0x01, // number of glyphs: 1
59        ]).map(|n| n.get());
60        assert_eq!(num_glyphs, Some(1));
61    }
62
63    #[test]
64    fn unknown_version() {
65        let num_glyphs = super::parse(&[
66            0x00, 0x00, 0x00, 0x00, // version: 0
67            0x00, 0x01, // number of glyphs: 1
68        ]).map(|n| n.get());
69        assert_eq!(num_glyphs, None);
70    }
71
72    #[test]
73    fn zero_glyphs() {
74        let num_glyphs = super::parse(&[
75            0x00, 0x00, 0x50, 0x00, // version: 0.3125
76            0x00, 0x00, // number of glyphs: 0
77        ]).map(|n| n.get());
78        assert_eq!(num_glyphs, None);
79    }
80
81    // TODO: what to do when the number of glyphs is 0xFFFF?
82    //       we're actually checking this in loca
83}