skrifa/outline/glyf/hint/engine/
definition.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! Defining and using functions and instructions.
//!
//! Implements 5 instructions.
//!
//! See <https://learn.microsoft.com/en-us/typography/opentype/spec/tt_instructions#defining-and-using-functions-and-instructions>

use read_fonts::tables::glyf::bytecode::Opcode;

use super::{
    super::{definition::Definition, program::Program},
    Engine, HintErrorKind, OpResult,
};

/// [Functions|Instructions] may not exceed 64K in size.
/// See <https://learn.microsoft.com/en-us/typography/opentype/spec/tt_instructions#function-definition>
/// See <https://learn.microsoft.com/en-us/typography/opentype/spec/tt_instructions#instruction-definition>
const MAX_DEFINITION_SIZE: usize = u16::MAX as usize;

impl<'a> Engine<'a> {
    /// Function definition.
    ///
    /// FDEF[] (0x2C)
    ///
    /// Pops: f: function identifier number
    ///
    /// Marks the start of a function definition. The argument f is a number
    /// that uniquely identifies this function. A function definition can
    /// appear only in the Font Program or the CVT program; attempts to invoke
    /// the FDEF instruction within a glyph program will result in an error.
    /// Functions may not exceed 64K in size.
    ///
    /// See <https://learn.microsoft.com/en-us/typography/opentype/spec/tt_instructions#function-definition>
    /// and <https://gitlab.freedesktop.org/freetype/freetype/-/blob/57617782464411201ce7bbc93b086c1b4d7d84a5/src/truetype/ttinterp.c#L3496>
    pub(super) fn op_fdef(&mut self) -> OpResult {
        let f = self.value_stack.pop()?;
        self.do_def(DefKind::Function, f)
    }

    /// End function definition.
    ///
    /// ENDF[] (0x2D)
    ///
    /// Marks the end of a function definition or an instruction definition.
    ///
    /// See <https://learn.microsoft.com/en-us/typography/opentype/spec/tt_instructions#end-function-definition>
    /// and <https://gitlab.freedesktop.org/freetype/freetype/-/blob/57617782464411201ce7bbc93b086c1b4d7d84a5/src/truetype/ttinterp.c#L3578>
    pub(super) fn op_endf(&mut self) -> OpResult {
        self.program.leave()
    }

    /// Call function.
    ///
    /// CALL[] (0x2B)
    ///
    /// Pops: f: function identifier number
    ///
    /// Calls the function identified by the number f.
    ///
    /// See <https://learn.microsoft.com/en-us/typography/opentype/spec/tt_instructions#call-function>
    /// and <https://gitlab.freedesktop.org/freetype/freetype/-/blob/57617782464411201ce7bbc93b086c1b4d7d84a5/src/truetype/ttinterp.c#L3623>
    pub(super) fn op_call(&mut self) -> OpResult {
        let f = self.value_stack.pop()?;
        self.do_call(DefKind::Function, 1, f)
    }

    /// Loop and call function.
    ///
    /// LOOPCALL[] (0x2a)
    ///
    /// Pops: f: function identifier number
    ///       count: number of times to call the function
    ///
    /// Calls the function f, count number of times.
    ///
    /// See <https://learn.microsoft.com/en-us/typography/opentype/spec/tt_instructions#loop-and-call-function>
    /// and <https://gitlab.freedesktop.org/freetype/freetype/-/blob/57617782464411201ce7bbc93b086c1b4d7d84a5/src/truetype/ttinterp.c#L3704>
    pub(super) fn op_loopcall(&mut self) -> OpResult {
        let f = self.value_stack.pop()?;
        let count = self.value_stack.pop()?;
        if count > 0 {
            self.loop_budget.doing_loop_call(count as usize)?;
            self.do_call(DefKind::Function, count as u32, f)
        } else {
            Ok(())
        }
    }

    /// Instruction definition.
    ///
    /// IDEF[] (0x89)
    ///
    /// Pops: opcode
    ///
    /// Begins the definition of an instruction. The instruction definition
    /// terminates when at ENDF, which is encountered in the instruction
    /// stream. Subsequent executions of the opcode popped will be directed
    /// to the contents of this instruction definition (IDEF). IDEFs must be
    /// defined in the Font Program or the CVT Program; attempts to invoke the
    /// IDEF instruction within a glyph program will result in an error. An
    /// IDEF affects only undefined opcodes. If the opcode in question is
    /// already defined, the interpreter will ignore the IDEF. This is to be
    /// used as a patching mechanism for future instructions. Instructions
    /// may not exceed 64K in size.
    ///
    /// See <https://learn.microsoft.com/en-us/typography/opentype/spec/tt_instructions#instruction-definition>
    /// and <https://gitlab.freedesktop.org/freetype/freetype/-/blob/57617782464411201ce7bbc93b086c1b4d7d84a5/src/truetype/ttinterp.c#L3788>
    pub(super) fn op_idef(&mut self) -> OpResult {
        let opcode = self.value_stack.pop()?;
        self.do_def(DefKind::Instruction, opcode)
    }

    /// Catch all for unhandled opcodes which will attempt to dispatch to a
    /// user defined instruction.
    pub(super) fn op_unknown(&mut self, opcode: u8) -> OpResult {
        self.do_call(DefKind::Instruction, 1, opcode as i32)
    }

    /// Common code for FDEF and IDEF.
    fn do_def(&mut self, kind: DefKind, key: i32) -> OpResult {
        if self.program.initial == Program::Glyph {
            return Err(HintErrorKind::DefinitionInGlyphProgram);
        }
        let defs = match kind {
            DefKind::Function => &mut self.definitions.functions,
            DefKind::Instruction => &mut self.definitions.instructions,
        };
        let def = defs.allocate(key)?;
        let start = self.program.decoder.pc;
        while let Some(ins) = self.program.decoder.decode() {
            let ins = ins?;
            match ins.opcode {
                Opcode::FDEF | Opcode::IDEF => return Err(HintErrorKind::NestedDefinition),
                Opcode::ENDF => {
                    let range = start..ins.pc + 1;
                    if self.graphics.is_pedantic && range.len() > MAX_DEFINITION_SIZE {
                        *def = Default::default();
                        return Err(HintErrorKind::DefinitionTooLarge);
                    }
                    *def = Definition::new(self.program.current, range, key);
                    return Ok(());
                }
                _ => {}
            }
        }
        Err(HintErrorKind::UnexpectedEndOfBytecode)
    }

    /// Common code for CALL, LOOPCALL and unknown opcode handling.
    fn do_call(&mut self, kind: DefKind, count: u32, key: i32) -> OpResult {
        if count == 0 {
            return Ok(());
        }
        let def = match kind {
            DefKind::Function => self.definitions.functions.get(key),
            DefKind::Instruction => match self.definitions.instructions.get(key) {
                // Remap an invalid definition error to unhandled opcode
                Err(HintErrorKind::InvalidDefinition(opcode)) => Err(
                    HintErrorKind::UnhandledOpcode(Opcode::from_byte(opcode as u8)),
                ),
                result => result,
            },
        };
        self.program.enter(*def?, count)
    }
}

enum DefKind {
    Function,
    Instruction,
}

#[cfg(test)]
mod tests {
    use super::{
        super::{
            super::program::{Program, ProgramState},
            Engine, MockEngine,
        },
        HintErrorKind, Opcode, MAX_DEFINITION_SIZE,
    };

    /// Define two functions, one of which calls the other with
    /// both CALL and LOOPCALL.
    #[test]
    fn define_function_call_loopcall() {
        use Opcode::*;
        let mut mock = MockEngine::new();
        let mut engine = mock.engine();
        #[rustfmt::skip]
        let font_code = [
            op(PUSHB001), 1, 0,
            // FDEF 0: adds 2 to top stack value
            op(FDEF),
                op(PUSHB000), 2,
                op(ADD),
            op(ENDF),
            // FDEF 1: calls FDEF 0 once, loop calls 5 times, then
            // negates the result
            op(FDEF),
                op(PUSHB000), 0,
                op(CALL),
                op(PUSHB001), 5, 0,
                op(LOOPCALL),
                op(NEG),
            op(ENDF),
        ];
        // Execute this code to define our functions
        engine.set_font_code(&font_code);
        engine.run().unwrap();
        // Call FDEF 1 with value of 10 on the stack:
        // * calls FDEF 0 which adds 2
        // * loop calls FDEF 0 an additional 5 times which adds a total of 10
        // * then negates the result
        // leaving -22 on the stack
        engine.value_stack.push(10).unwrap();
        engine.value_stack.push(1).unwrap();
        engine.op_call().unwrap();
        engine.run().unwrap();
        assert_eq!(engine.value_stack.pop().ok(), Some(-22));
    }

    /// Control value programs can override functions defined in the font
    /// program based on instance state.
    #[test]
    fn override_function() {
        use Opcode::*;
        let mut mock = MockEngine::new();
        let mut engine = mock.engine();
        #[rustfmt::skip]
        let font_code = [
            op(PUSHB001), 0, 0,
            // FDEF 0: adds 2 to top stack value
            op(FDEF),
                op(PUSHB000), 2,
                op(ADD),
            op(ENDF),
            // Redefine FDEF 0: subtract 2 instead
            op(FDEF),
                op(PUSHB000), 2,
                op(SUB),                
            op(ENDF),
        ];
        // Execute this code to define our functions
        engine.set_font_code(&font_code);
        engine.run().unwrap();
        // Call FDEF 0 with value of 10 on the stack:
        // * should subtract 2 rather than add
        // leaving 8 on the stack
        engine.value_stack.push(10).unwrap();
        engine.value_stack.push(0).unwrap();
        engine.op_call().unwrap();
        engine.run().unwrap();
        assert_eq!(engine.value_stack.pop().ok(), Some(8));
    }

    /// Executes a call from a CV program into a font program.
    ///
    /// Tests ProgramState bytecode/decoder management.
    #[test]
    fn call_different_program() {
        use Opcode::*;
        let mut mock = MockEngine::new();
        let mut engine = mock.engine();
        #[rustfmt::skip]
        let font_code = [
            op(PUSHB000), 0,
            // FDEF 0: adds 2 to top stack value
            op(FDEF),
                op(PUSHB000), 2,
                op(ADD),
            op(ENDF),
        ];
        #[rustfmt::skip]
        let cv_code = [
            // Call function defined in font program and negate result
            op(PUSHB001), 40, 0,
            op(CALL),
            op(NEG)
        ];
        let glyph_code = &[];
        // Run font program first to define the function
        engine.program = ProgramState::new(&font_code, &cv_code, glyph_code, Program::Font);
        engine.run().unwrap();
        // Now run CV program which calls into the font program
        engine.program = ProgramState::new(&font_code, &cv_code, glyph_code, Program::ControlValue);
        engine.run().unwrap();
        // Executing CV program:
        // * pushes 40 to the stack
        // * calls FDEF 0 in font program which adds 2
        // * returns to CV program
        // * negates the value
        // leaving -42 on the stack
        assert_eq!(engine.value_stack.pop().ok(), Some(-42));
    }

    /// Fail when we exceed loop call budget.
    #[test]
    fn loopcall_budget() {
        use Opcode::*;
        let mut mock = MockEngine::new();
        let mut engine = mock.engine();
        let limit = engine.loop_budget.limit;
        #[rustfmt::skip]
        let font_code = [
            op(PUSHB001), 1, 0,
            // FDEF 0: does nothing
            op(FDEF),
            op(ENDF),
            // FDEF 1: loop calls FDEF 0 twice, exceeding the budget on the
            // second attempt
            op(FDEF),
                op(PUSHB001), limit as u8, 0,
                op(LOOPCALL),
                op(PUSHB001), 1, 0,
                op(LOOPCALL), // pc = 13
            op(ENDF),
        ];
        // Execute this code to define our functions
        engine.set_font_code(&font_code);
        engine.run().unwrap();
        // Call FDEF 1 which attempts to loop call FDEF 0 (limit + 1) times
        engine.value_stack.push(10).unwrap();
        engine.value_stack.push(1).unwrap();
        engine.op_call().unwrap();
        let err = engine.run().unwrap_err();
        assert!(matches!(err.kind, HintErrorKind::ExceededExecutionBudget));
        assert_eq!(err.pc, 13);
    }

    /// Defines an instruction using an available opcode and executes it.
    #[test]
    fn define_instruction_and_use() {
        use Opcode::*;
        let mut mock = MockEngine::new();
        let mut engine = mock.engine();
        #[rustfmt::skip]
        let font_code = [
            // IDEF 0x93: adds 2 to top stack value
            op(PUSHB000), op(INS93),
            op(IDEF),
                op(PUSHB000), 2,
                op(ADD),
            op(ENDF),
            // FDEF 0: uses defined instruction 0x93 and negates the result 
            op(PUSHB000), 0,
            op(FDEF),
                op(INS93),
                op(NEG),
            op(ENDF),
        ];
        // Execute this code to define our functions
        engine.set_font_code(&font_code);
        engine.run().unwrap();
        // Call FDEF 0 with value of 10 on the stack:
        // * executes defined instruction 0x93
        // * then negates the result
        // leaving -12 on the stack
        engine.value_stack.push(10).unwrap();
        engine.value_stack.push(0).unwrap();
        engine.op_call().unwrap();
        engine.run().unwrap();
        assert_eq!(engine.value_stack.pop().ok(), Some(-12));
    }

    // Invalid to nest definitions.
    #[test]
    fn nested_definition() {
        use Opcode::*;
        let mut mock = MockEngine::new();
        let mut engine = mock.engine();
        #[rustfmt::skip]
        let font_code = [
            op(PUSHB001), 1, 0,
            op(FDEF), // pc = 3
                op(FDEF),
                op(ENDF),
            op(ENDF),
        ];
        // Execute this code to define our functions
        engine.set_font_code(&font_code);
        let err = engine.run().unwrap_err();
        assert!(matches!(err.kind, HintErrorKind::NestedDefinition));
        assert_eq!(err.pc, 3);
    }

    // Invalid to modify definitions from the glyph program.
    #[test]
    fn definition_in_glyph_program() {
        use Opcode::*;
        let mut mock = MockEngine::new();
        let mut engine = mock.engine();
        #[rustfmt::skip]
        let font_code = [
            op(PUSHB000), 0,
            op(FDEF), // pc = 2
            op(ENDF),
        ];
        engine.set_font_code(&font_code);
        engine.program.initial = Program::Glyph;
        let err = engine.run().unwrap_err();
        assert!(matches!(err.kind, HintErrorKind::DefinitionInGlyphProgram));
        assert_eq!(err.pc, 2);
    }

    #[test]
    fn undefined_function() {
        let mut mock = MockEngine::new();
        let mut engine = mock.engine();
        engine.value_stack.push(111).unwrap();
        assert!(matches!(
            engine.op_call(),
            Err(HintErrorKind::InvalidDefinition(111))
        ));
    }

    /// Fun function that just calls itself :)
    #[test]
    fn infinite_recursion() {
        use Opcode::*;
        let mut mock = MockEngine::new();
        let mut engine = mock.engine();
        #[rustfmt::skip]
        let font_code = [
            // FDEF 0: call FDEF 0
            op(PUSHB000), 0,
            op(FDEF),
                op(PUSHB000), 0,
                op(CALL), // pc = 5
            op(ENDF),
        ];
        engine.set_font_code(&font_code);
        engine.run().unwrap();
        // Call stack overflow
        engine.value_stack.push(0).unwrap();
        engine.op_call().unwrap();
        let err = engine.run().unwrap_err();
        assert!(matches!(err.kind, HintErrorKind::CallStackOverflow));
        assert_eq!(err.pc, 5);
    }

    #[test]
    fn call_stack_underflow() {
        use Opcode::*;
        let mut mock = MockEngine::new();
        let mut engine = mock.engine();
        #[rustfmt::skip]
        let font_code = [
            op(ENDF)
        ];
        engine.set_font_code(&font_code);
        let err = engine.run().unwrap_err();
        assert!(matches!(err.kind, HintErrorKind::CallStackUnderflow));
        assert_eq!(err.pc, 0);
    }

    #[test]
    fn unhandled_opcode() {
        let mut mock = MockEngine::new();
        let mut engine = mock.engine();
        #[rustfmt::skip]
        let font_code = [
            op(Opcode::INS28),
        ];
        engine.set_font_code(&font_code);
        let err = engine.run().unwrap_err();
        assert!(matches!(
            err.kind,
            HintErrorKind::UnhandledOpcode(Opcode::INS28)
        ));
        assert_eq!(err.pc, 0);
    }

    #[test]
    fn too_many_definitions() {
        use Opcode::*;
        let mut mock = MockEngine::new();
        let mut engine = mock.engine();
        #[rustfmt::skip]
        let font_code = [
            op(PUSHB101), 0, 1, 2, 3, 4, 5,
            op(FDEF), op(ENDF),
            op(FDEF), op(ENDF),
            op(FDEF), op(ENDF),
            op(FDEF), op(ENDF),
            op(FDEF), op(ENDF),
            op(FDEF), op(ENDF),
        ];
        engine.set_font_code(&font_code);
        let err = engine.run().unwrap_err();
        assert!(matches!(err.kind, HintErrorKind::TooManyDefinitions));
        assert_eq!(err.pc, 17);
    }

    #[test]
    fn big_definition() {
        use Opcode::*;
        let mut mock = MockEngine::new();
        let mut engine = mock.engine();
        let mut font_code = vec![];
        font_code.extend_from_slice(&[op(PUSHB000), 0, op(FDEF)]);
        font_code.extend(core::iter::repeat(op(NEG)).take(MAX_DEFINITION_SIZE + 1));
        font_code.push(op(ENDF));
        engine.set_font_code(&font_code);
        engine.graphics.is_pedantic = true;
        engine.value_stack.push(1).unwrap();
        let err = engine.run().unwrap_err();
        assert!(matches!(err.kind, HintErrorKind::DefinitionTooLarge));
        assert_eq!(err.pc, 2);
    }

    fn op(opcode: Opcode) -> u8 {
        opcode as u8
    }

    impl<'a> Engine<'a> {
        fn set_font_code(&mut self, code: &'a [u8]) {
            self.program.bytecode[0] = code;
            self.program.decoder.bytecode = code;
            self.program.current = Program::Font;
        }
    }
}