truetype/tables/
postscript.rs1use crate::{q32, Result};
6
7#[derive(Clone, Debug)]
9pub enum PostScript {
10 Version1(PostScript1),
12 Version2(PostScript2),
14 Version3(PostScript3),
16}
17
18table! {
19 #[derive(Copy)]
21 pub PostScript1 {
22 version (q32), italic_angle (q32), underline_position (i16), underline_thickness (i16), is_fixed_pitch (u32), min_memory_type42 (u32), max_memory_type42 (u32), min_memory_type1 (u32), max_memory_type1 (u32), }
32}
33
34table! {
35 pub PostScript2 {
37 version (q32), italic_angle (q32), underline_position (i16), underline_thickness (i16), is_fixed_pitch (u32), min_memory_type42 (u32), max_memory_type42 (u32), min_memory_type1 (u32), max_memory_type1 (u32), glyph_count (u16), glyph_name_indices (Vec<u16>) |this, tape| { tape.take_given(this.glyph_count as usize)
50 },
51
52 glyph_names (Vec<String>) |this, tape| { read_pascal_strings(tape, &this.glyph_name_indices)
54 },
55 }
56}
57
58pub 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}