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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! The operations.

use std::collections::HashMap;

use crate::compact1::number::Number;
use crate::{Result, Tape, Value};

/// An operand.
pub type Operand = Number;

/// A collection of operations.
#[derive(Clone, Debug, Default)]
pub struct Operations(pub HashMap<Operator, Vec<Operand>>);

struct Operation(Operator, Vec<Operand>);

impl Operations {
    /// Return the operands of an operation.
    #[inline]
    pub fn get(&self, operator: Operator) -> Option<&[Operand]> {
        match self.0.get(&operator) {
            Some(operands) => Some(operands),
            _ => operator.default(),
        }
    }

    #[doc(hidden)]
    #[inline]
    pub fn get_single(&self, operator: Operator) -> Option<Operand> {
        self.get(operator).and_then(|operands| {
            if operands.len() > 0 {
                Some(operands[0])
            } else {
                None
            }
        })
    }

    #[doc(hidden)]
    #[inline]
    pub fn get_double(&self, operator: Operator) -> Option<(Operand, Operand)> {
        self.get(operator).and_then(|operands| {
            if operands.len() > 1 {
                Some((operands[0], operands[1]))
            } else {
                None
            }
        })
    }
}

impl Value for Operations {
    fn read<T: Tape>(tape: &mut T) -> Result<Self> {
        use std::io::ErrorKind;

        let mut operations = HashMap::new();
        loop {
            match tape.take() {
                Ok(Operation(operator, operands)) => {
                    operations.insert(operator, operands);
                }
                Err(error) => {
                    if error.kind() == ErrorKind::UnexpectedEof {
                        return Ok(Operations(operations));
                    } else {
                        return Err(error);
                    }
                }
            }
        }
    }
}

dereference! { Operations::0 => HashMap<Operator, Vec<Operand>> }

impl Value for Operation {
    fn read<T: Tape>(tape: &mut T) -> Result<Self> {
        let mut operands = vec![];
        loop {
            match tape.peek::<u8>()? {
                0x1c | 0x1d | 0x1e | 0x20..=0xfe => operands.push(tape.take()?),
                code => {
                    let code = if code == 0x0c {
                        tape.take::<u16>()?
                    } else {
                        tape.take::<u8>()? as u16
                    };
                    return Ok(Self(Operator::from(code)?, operands));
                }
            }
        }
    }
}

macro_rules! default(
    ([$($operand:expr),+ $(,)?]) => ({
        const OPERANDS: &'static [Operand] = &[$($operand),+];
        Some(OPERANDS)
    });
    ([]) => (None);
);

macro_rules! operator {
    (pub $name:ident { $($code:pat => $variant:ident $default:tt,)+ }) => (
        operator! { @define pub $name { $($variant,)+ } }
        operator! { @implement pub $name { $($code => $variant $default,)+ } }
    );
    (@define pub $name:ident { $($variant:ident,)* }) => (
        /// An operator.
        #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
        pub enum $name { $($variant,)* }
    );
    (@implement pub $name:ident { $($code:pat => $variant:ident $default:tt,)* }) => (impl $name {
        #[doc(hidden)]
        pub fn from(code: u16) -> Result<Self> {
            use self::$name::*;

            Ok(match code {
                $($code => $variant,)+
                code => raise!("found an unsupported operator ({})", code),
            })
        }

        /// Return the default operands.
        pub fn default(&self) -> Option<&'static [Operand]> {
            use self::$name::*;

            match *self {
                $($variant => default!($default),)+
            }
        }
    });
}

operator! {
    pub Operator {
        0x00 => Version [],
        0x01 => Notice [],
        0x02 => FullName [],
        0x03 => FamilyName [],
        0x04 => Weight [],
        0x05 => FontBBox [
            Number::Integer(0),
            Number::Integer(0),
            Number::Integer(0),
            Number::Integer(0),
        ],
        0x06 => BlueValues [],
        0x07 => OtherBlues [],
        0x08 => FamilyBlues [],
        0x09 => FamilyOtherBlues [],
        0x0a => StdHW [],
        0x0b => StdVW [],
        // 0x0c => Escape,
        0x0d => UniqueID [],
        0x0e => XUID [],
        0x0f => CharSet [Number::Integer(0)],
        0x10 => Encoding [Number::Integer(0)],
        0x11 => CharStrings [],
        0x12 => Private [],
        0x13 => Subrs [],
        0x14 => DefaultWidthX [Number::Integer(0)],
        0x15 => NominalWidthX [Number::Integer(0)],
        // 0x16..=0x1b => Reserved,
        // 0x1c => ShortInt,
        // 0x1d => LongInt,
        // 0x1e => BCD,
        // 0x1f => Reserved,
        // 0x20..=0xf6 => <numbers>,
        // 0xf7..=0xfe => <numbers>,
        // 0xff => Reserved,
        0x0c00 => Copyright [],
        0x0c01 => IsFixedPitch [Number::Integer(false as i32)],
        0x0c02 => ItalicAngle [Number::Integer(0)],
        0x0c03 => UnderlinePosition [Number::Integer(-100)],
        0x0c04 => UnderlineThickness [Number::Integer(50)],
        0x0c05 => PaintType [Number::Integer(0)],
        0x0c06 => CharStringType [Number::Integer(2)],
        0x0c07 => FontMatrix [
            Number::Real(0.001),
            Number::Real(0.0),
            Number::Real(0.0),
            Number::Real(0.001),
            Number::Real(0.0),
            Number::Real(0.0),
        ],
        0x0c08 => StrokeWidth [Number::Integer(0)],
        0x0c09 => BlueScale [Number::Real(0.039625)],
        0x0c0a => BlueShift [Number::Integer(7)],
        0x0c0b => BlueFuzz [Number::Integer(1)],
        0x0c0c => StemSnapH [],
        0x0c0d => StemSnapV [],
        0x0c0e => ForceBold [Number::Integer(false as i32)],
        // 0x0c0f..=0x0c10 => Reserved,
        0x0c11 => LanguageGroup [Number::Integer(0)],
        0x0c12 => ExpansionFactor [Number::Real(0.06)],
        0x0c13 => InitialRandomSeed [Number::Integer(0)],
        0x0c14 => SyntheticBase [],
        0x0c15 => PostScript [],
        0x0c16 => BaseFontName [],
        0x0c17 => BaseFontBlend [],
        // 0x0c18..=0x0c1d => Reserved,
        0x0c1e => ROS [],
        0x0c1f => CIDFontVersion [Number::Integer(0)],
        0x0c20 => CIDFontRevision [Number::Integer(0)],
        0x0c21 => CIDFontType [Number::Integer(0)],
        0x0c22 => CIDCount [Number::Integer(8720)],
        0x0c23 => UIDBase [],
        0x0c24 => FDArray [],
        0x0c25 => FDSelect [],
        0x0c26 => FontName [],
        // 0x0c27..=0x0cff => Reserved,
    }
}