ttf_parser/tables/
maxp.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
// https://docs.microsoft.com/en-us/typography/opentype/spec/maxp

use core::num::NonZeroU16;

use crate::parser::Stream;

// We care only about `numGlyphs`.
pub fn parse(data: &[u8]) -> Option<NonZeroU16> {
    let mut s = Stream::new(data);
    let version: u32 = s.read()?;
    if !(version == 0x00005000 || version == 0x00010000) {
        return None;
    }

    let n: u16 = s.read()?;
    NonZeroU16::new(n)
}


#[cfg(test)]
mod tests {
    #[test]
    fn version_05() {
        let num_glyphs = super::parse(&[
            0x00, 0x00, 0x50, 0x00, // version: 0.3125
            0x00, 0x01, // number of glyphs: 1
        ]).map(|n| n.get());
        assert_eq!(num_glyphs, Some(1));
    }

    #[test]
    fn version_1_full() {
        let num_glyphs = super::parse(&[
            0x00, 0x01, 0x00, 0x00, // version: 1
            0x00, 0x01, // number of glyphs: 1
            0x00, 0x00, // maximum points in a non-composite glyph: 0
            0x00, 0x00, // maximum contours in a non-composite glyph: 0
            0x00, 0x00, // maximum points in a composite glyph: 0
            0x00, 0x00, // maximum contours in a composite glyph: 0
            0x00, 0x00, // maximum zones: 0
            0x00, 0x00, // maximum twilight points: 0
            0x00, 0x00, // number of Storage Area locations: 0
            0x00, 0x00, // number of FDEFs: 0
            0x00, 0x00, // number of IDEFs: 0
            0x00, 0x00, // maximum stack depth: 0
            0x00, 0x00, // maximum byte count for glyph instructions: 0
            0x00, 0x00, // maximum number of components: 0
            0x00, 0x00, // maximum levels of recursion: 0
        ]).map(|n| n.get());
        assert_eq!(num_glyphs, Some(1));
    }

    #[test]
    fn version_1_trimmed() {
        // We don't really care about the data after the number of glyphs.
        let num_glyphs = super::parse(&[
            0x00, 0x01, 0x00, 0x00, // version: 1
            0x00, 0x01, // number of glyphs: 1
        ]).map(|n| n.get());
        assert_eq!(num_glyphs, Some(1));
    }

    #[test]
    fn unknown_version() {
        let num_glyphs = super::parse(&[
            0x00, 0x00, 0x00, 0x00, // version: 0
            0x00, 0x01, // number of glyphs: 1
        ]).map(|n| n.get());
        assert_eq!(num_glyphs, None);
    }

    #[test]
    fn zero_glyphs() {
        let num_glyphs = super::parse(&[
            0x00, 0x00, 0x50, 0x00, // version: 0.3125
            0x00, 0x00, // number of glyphs: 0
        ]).map(|n| n.get());
        assert_eq!(num_glyphs, None);
    }

    // TODO: what to do when the number of glyphs is 0xFFFF?
    //       we're actually checking this in loca
}