Skip to main content

read_fonts/tables/glyf/outline/
error.rs

1//! What can go wrong while building an outline.
2
3use super::super::bytecode::HintError;
4use crate::ReadError;
5
6/// An error from inspecting or building an outline.
7///
8/// The variants carry no payload. The caller already named the glyph, and
9/// anything finer would need a path through the composite tree to be
10/// actionable.
11#[derive(Clone, PartialEq, Debug)]
12pub enum OutlineError {
13    /// A requested glyph was not found in the font.
14    MissingGlyph,
15    /// A composite glyph nests more deeply than
16    /// [`MAX_RECURSION_DEPTH`](crate::limits::MAX_RECURSION_DEPTH).
17    RecursionLimitExceeded,
18    /// A composite glyph references more components than
19    /// [`MAX_COMPOSITE_EDGES`](crate::limits::MAX_COMPOSITE_EDGES).
20    TooManyComponents,
21    /// An outline has more points than
22    /// [`MAX_OUTLINE_POINTS`](crate::limits::MAX_OUTLINE_POINTS).
23    TooManyPoints,
24    /// A component referenced a point that does not exist.
25    InvalidAnchorPoint,
26    /// A caller supplied buffer was too small.
27    InsufficientMemory,
28    /// The [`Hinter`](super::Hinter) reported a failure.
29    Hinting(HintError),
30    /// The font data was malformed.
31    ///
32    /// Covers every failure to read the underlying tables.
33    Malformed,
34}
35
36impl From<ReadError> for OutlineError {
37    fn from(_: ReadError) -> Self {
38        Self::Malformed
39    }
40}
41
42impl From<HintError> for OutlineError {
43    fn from(value: HintError) -> Self {
44        Self::Hinting(value)
45    }
46}
47
48impl core::fmt::Display for OutlineError {
49    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
50        match self {
51            Self::MissingGlyph => f.write_str("requested glyph was not found in the font"),
52            Self::RecursionLimitExceeded => {
53                f.write_str("glyph exceeded the composite recursion limit")
54            }
55            Self::TooManyComponents => f.write_str("glyph referenced too many components"),
56            Self::TooManyPoints => f.write_str("glyph has too many points"),
57            Self::InvalidAnchorPoint => f.write_str("glyph referenced a nonexistent anchor point"),
58            Self::InsufficientMemory => f.write_str("outline buffers were too small"),
59            Self::Hinting(err) => write!(f, "{err}"),
60            Self::Malformed => f.write_str("font data was malformed"),
61        }
62    }
63}
64
65#[cfg(feature = "std")]
66impl std::error::Error for OutlineError {}