Skip to main content

read_fonts/model/
error.rs

1//! The error type for reading glyphs and evaluating font programs.
2
3use crate::ReadError;
4use core::fmt;
5
6/// An error from reading a glyph or evaluating a program in the font.
7///
8/// It identifies only the kind of failure. The caller already knows which
9/// glyph or table was requested.
10#[non_exhaustive]
11#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
12pub enum Error {
13    /// The font data is too short, inconsistent, or refers past the end of
14    /// something that is present.
15    Malformed,
16    /// A required part of the font is absent, such as a glyph, a table, or a
17    /// CFF dictionary, subroutine index or charset.
18    MissingResource,
19    /// A program in the font, a charstring or TrueType instructions, did
20    /// something that is not valid, such as popping an empty stack.
21    InvalidOperation,
22    /// The font exceeds a limit this crate sets, such as a nesting depth or a
23    /// count of points, components or paints. A cycle in a color glyph's
24    /// paint graph exceeds the nesting depth.
25    ///
26    /// The limits are in [`limits`](crate::limits).
27    LimitExceeded,
28    /// The memory supplied by the caller is too small.
29    InsufficientMemory,
30}
31
32impl From<ReadError> for Error {
33    fn from(_: ReadError) -> Self {
34        Self::Malformed
35    }
36}
37
38impl fmt::Display for Error {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        f.write_str(match self {
41            Self::Malformed => "font data was malformed",
42            Self::MissingResource => "a required part of the font was missing",
43            Self::InvalidOperation => "a program in the font did something invalid",
44            Self::LimitExceeded => "the font exceeded a processing limit",
45            Self::InsufficientMemory => "the supplied memory was too small",
46        })
47    }
48}
49
50impl core::error::Error for Error {}