skrifa/outline/glyf/hint/
error.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
//! Hinting error definitions.

use read_fonts::tables::glyf::bytecode::{DecodeError, Opcode};

use super::program::Program;
use crate::GlyphId;

/// Errors that may occur when interpreting TrueType bytecode.
#[derive(Clone, PartialEq, Debug)]
pub enum HintErrorKind {
    UnexpectedEndOfBytecode,
    UnhandledOpcode(Opcode),
    DefinitionInGlyphProgram,
    NestedDefinition,
    DefinitionTooLarge,
    TooManyDefinitions,
    InvalidDefinition(usize),
    ValueStackOverflow,
    ValueStackUnderflow,
    CallStackOverflow,
    CallStackUnderflow,
    InvalidStackValue(i32),
    InvalidPointIndex(usize),
    InvalidPointRange(usize, usize),
    InvalidContourIndex(usize),
    InvalidCvtIndex(usize),
    InvalidStorageIndex(usize),
    DivideByZero,
    InvalidZoneIndex(i32),
    NegativeLoopCounter,
    InvalidJump,
    ExceededExecutionBudget,
}

impl core::fmt::Display for HintErrorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UnexpectedEndOfBytecode => write!(f, "unexpected end of bytecode"),
            Self::UnhandledOpcode(opcode) => write!(f, "unhandled instruction opcode {opcode}"),
            Self::DefinitionInGlyphProgram => {
                write!(
                    f,
                    "function or instruction definition present in glyph program"
                )
            }
            Self::NestedDefinition => write!(f, "nested function or instruction definition"),
            Self::DefinitionTooLarge => write!(
                f,
                "function or instruction definition exceeded the maximum size of 64k"
            ),
            Self::TooManyDefinitions => write!(f, "too many function or instruction definitions"),
            Self::InvalidDefinition(key) => {
                write!(f, "function or instruction definition {key} not found")
            }
            Self::ValueStackOverflow => write!(f, "value stack overflow"),
            Self::ValueStackUnderflow => write!(f, "value stack underflow"),
            Self::CallStackOverflow => write!(f, "call stack overflow"),
            Self::CallStackUnderflow => write!(f, "call stack underflow"),
            Self::InvalidStackValue(value) => write!(
                f,
                "stack value {value} was invalid for the current operation"
            ),
            Self::InvalidPointIndex(index) => write!(f, "point index {index} was out of bounds"),
            Self::InvalidPointRange(start, end) => {
                write!(f, "point range {start}..{end} was out of bounds")
            }
            Self::InvalidContourIndex(index) => {
                write!(f, "contour index {index} was out of bounds")
            }
            Self::InvalidCvtIndex(index) => write!(f, "cvt index {index} was out of bounds"),
            Self::InvalidStorageIndex(index) => {
                write!(f, "storage area index {index} was out of bounds")
            }
            Self::DivideByZero => write!(f, "attempt to divide by 0"),
            Self::InvalidZoneIndex(index) => write!(
                f,
                "zone index {index} was invalid (only 0 or 1 are permitted)"
            ),
            Self::NegativeLoopCounter => {
                write!(f, "attempt to set the loop counter to a negative value")
            }
            Self::InvalidJump => write!(f, "the target of a jump instruction was invalid"),
            Self::ExceededExecutionBudget => write!(f, "too many instructions executed"),
        }
    }
}

impl From<DecodeError> for HintErrorKind {
    fn from(_: DecodeError) -> Self {
        Self::UnexpectedEndOfBytecode
    }
}

/// Hinting error with additional context.
#[derive(Clone, Debug)]
pub struct HintError {
    pub program: Program,
    pub glyph_id: Option<GlyphId>,
    pub pc: usize,
    pub opcode: Option<Opcode>,
    pub kind: HintErrorKind,
}

impl core::fmt::Display for HintError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.program {
            Program::ControlValue => write!(f, "prep")?,
            Program::Font => write!(f, "fpgm")?,
            Program::Glyph => write!(f, "glyf")?,
        }
        if let Some(glyph_id) = self.glyph_id {
            write!(f, "[{}]", glyph_id.to_u32())?;
        }
        let (opcode, colon) = match self.opcode {
            Some(opcode) => (opcode.name(), ":"),
            _ => ("", ""),
        };
        write!(f, "@{}:{opcode}{colon} {}", self.pc, self.kind)
    }
}