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
//! Error types associated with outlines.

use core::fmt;
use read_fonts::types::GlyphId;

pub use read_fonts::{tables::postscript::Error as CffError, ReadError};

pub use super::glyf::HintError;
pub use super::path::ToPathError;

/// Errors that may occur when drawing glyphs.
#[derive(Clone, Debug)]
pub enum DrawError {
    /// No viable sources were available.
    NoSources,
    /// The requested glyph was not present in the font.
    GlyphNotFound(GlyphId),
    /// Exceeded memory limits when loading a glyph.
    InsufficientMemory,
    /// Exceeded a recursion limit when loading a glyph.
    RecursionLimitExceeded(GlyphId),
    /// Error occurred during hinting.
    HintingFailed(HintError),
    /// An anchor point had invalid indices.
    InvalidAnchorPoint(GlyphId, u16),
    /// Error occurred while loading a PostScript (CFF/CFF2) glyph.
    PostScript(CffError),
    /// Conversion from outline to path failed.
    ToPath(ToPathError),
    /// Error occurred when reading font data.
    Read(ReadError),
    /// HarfBuzz style drawing with hints is not supported
    // Error rather than silently returning unhinted per f2f discussion.
    HarfBuzzHintingUnsupported,
}

impl From<HintError> for DrawError {
    fn from(value: HintError) -> Self {
        Self::HintingFailed(value)
    }
}

impl From<ToPathError> for DrawError {
    fn from(e: ToPathError) -> Self {
        Self::ToPath(e)
    }
}

impl From<ReadError> for DrawError {
    fn from(e: ReadError) -> Self {
        Self::Read(e)
    }
}

impl From<CffError> for DrawError {
    fn from(value: CffError) -> Self {
        Self::PostScript(value)
    }
}

impl fmt::Display for DrawError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::NoSources => write!(f, "No glyph sources are available for the given font"),
            Self::GlyphNotFound(gid) => write!(f, "Glyph {gid} was not found in the given font"),
            Self::InsufficientMemory => write!(f, "exceeded memory limits"),
            Self::RecursionLimitExceeded(gid) => write!(
                f,
                "Recursion limit ({}) exceeded when loading composite component {gid}",
                super::GLYF_COMPOSITE_RECURSION_LIMIT,
            ),
            Self::HintingFailed(e) => write!(f, "{e}"),
            Self::InvalidAnchorPoint(gid, index) => write!(
                f,
                "Invalid anchor point index ({index}) for composite glyph {gid}",
            ),
            Self::PostScript(e) => write!(f, "{e}"),
            Self::ToPath(e) => write!(f, "{e}"),
            Self::Read(e) => write!(f, "{e}"),
            Self::HarfBuzzHintingUnsupported => write!(
                f,
                "HarfBuzz style paths with hinting is not (yet?) supported"
            ),
        }
    }
}