truetype/tables/
postscript.rs

1//! The [PostScript table][1].
2//!
3//! [1]: https://learn.microsoft.com/en-us/typography/opentype/spec/post
4
5use crate::{q32, Result};
6
7/// A PostScript table.
8#[derive(Clone, Debug)]
9pub enum PostScript {
10    /// Version 1.
11    Version1(PostScript1),
12    /// Version 2.
13    Version2(PostScript2),
14    /// Version 3.
15    Version3(PostScript3),
16}
17
18table! {
19    /// A PostScript table of version 1.
20    #[derive(Copy)]
21    pub PostScript1 {
22        version             (q32), // version
23        italic_angle        (q32), // italicAngle
24        underline_position  (i16), // underlinePosition
25        underline_thickness (i16), // underlineThickness
26        is_fixed_pitch      (u32), // isFixedPitch
27        min_memory_type42   (u32), // minMemType42
28        max_memory_type42   (u32), // maxMemType42
29        min_memory_type1    (u32), // minMemType1
30        max_memory_type1    (u32), // maxMemType1
31    }
32}
33
34table! {
35    /// A PostScript table of version 2.
36    pub PostScript2 {
37        version             (q32), // version
38        italic_angle        (q32), // italicAngle
39        underline_position  (i16), // underlinePosition
40        underline_thickness (i16), // underlineThickness
41        is_fixed_pitch      (u32), // isFixedPitch
42        min_memory_type42   (u32), // minMemType42
43        max_memory_type42   (u32), // maxMemType42
44        min_memory_type1    (u32), // minMemType1
45        max_memory_type1    (u32), // maxMemType1
46        glyph_count         (u16), // numberOfGlyphs
47
48        glyph_name_indices (Vec<u16>) |this, tape| { // glyphNameIndex
49            tape.take_given(this.glyph_count as usize)
50        },
51
52        glyph_names (Vec<String>) |this, tape| { // names
53            read_pascal_strings(tape, &this.glyph_name_indices)
54        },
55    }
56}
57
58/// A PostScript table of version 3.
59pub type PostScript3 = PostScript1;
60
61impl crate::value::Read for PostScript {
62    fn read<T: crate::tape::Read>(tape: &mut T) -> Result<Self> {
63        Ok(match tape.peek::<q32>()? {
64            q32(0x00010000) => PostScript::Version1(tape.take()?),
65            q32(0x00020000) => PostScript::Version2(tape.take()?),
66            q32(0x00030000) => PostScript::Version3(tape.take()?),
67            _ => raise!("found an unknown version of the PostScript table"),
68        })
69    }
70}
71
72fn read_pascal_strings<T: crate::tape::Read>(tape: &mut T, indices: &[u16]) -> Result<Vec<String>> {
73    let count = indices.iter().fold(
74        0,
75        |n, &i| if (258..=32767).contains(&i) { n + 1 } else { n },
76    );
77    let mut names = Vec::with_capacity(count);
78    for _ in 0..count {
79        let size = tape.take::<u8>()? as usize;
80        match String::from_utf8(tape.take_bytes(size)?) {
81            Ok(name) => names.push(name),
82            _ => names.push("<malformed>".into()),
83        }
84    }
85    Ok(names)
86}