1 //! Error types associated with outlines. 2 3 use read_fonts::types::GlyphId; 4 5 pub use read_fonts::{ 6 tables::{glyf::ToPathError, postscript::Error as CffError}, 7 ReadError, 8 }; 9 10 use core::fmt; 11 12 /// Errors that may occur when drawing glyphs. 13 #[derive(Clone, Debug)] 14 pub enum DrawError { 15 /// No viable sources were available. 16 NoSources, 17 /// The requested glyph was not present in the font. 18 GlyphNotFound(GlyphId), 19 /// Exceeded memory limits when loading a glyph. 20 InsufficientMemory, 21 /// Exceeded a recursion limit when loading a glyph. 22 RecursionLimitExceeded(GlyphId), 23 /// Error occured during hinting. 24 HintingFailed(GlyphId), 25 /// An anchor point had invalid indices. 26 InvalidAnchorPoint(GlyphId, u16), 27 /// Error occurred while loading a PostScript (CFF/CFF2) glyph. 28 PostScript(CffError), 29 /// Conversion from outline to path failed. 30 ToPath(ToPathError), 31 /// Error occured when reading font data. 32 Read(ReadError), 33 } 34 35 impl From<ToPathError> for DrawError { from(e: ToPathError) -> Self36 fn from(e: ToPathError) -> Self { 37 Self::ToPath(e) 38 } 39 } 40 41 impl From<ReadError> for DrawError { from(e: ReadError) -> Self42 fn from(e: ReadError) -> Self { 43 Self::Read(e) 44 } 45 } 46 47 impl From<CffError> for DrawError { from(value: CffError) -> Self48 fn from(value: CffError) -> Self { 49 Self::PostScript(value) 50 } 51 } 52 53 impl fmt::Display for DrawError { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result54 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 55 match self { 56 Self::NoSources => write!(f, "No glyph sources are available for the given font"), 57 Self::GlyphNotFound(gid) => write!(f, "Glyph {gid} was not found in the given font"), 58 Self::InsufficientMemory => write!(f, "exceeded memory limits"), 59 Self::RecursionLimitExceeded(gid) => write!( 60 f, 61 "Recursion limit ({}) exceeded when loading composite component {gid}", 62 super::GLYF_COMPOSITE_RECURSION_LIMIT, 63 ), 64 Self::HintingFailed(gid) => write!(f, "Bad hinting bytecode for glyph {gid}"), 65 Self::InvalidAnchorPoint(gid, index) => write!( 66 f, 67 "Invalid anchor point index ({index}) for composite glyph {gid}", 68 ), 69 Self::PostScript(e) => write!(f, "{e}"), 70 Self::ToPath(e) => write!(f, "{e}"), 71 Self::Read(e) => write!(f, "{e}"), 72 } 73 } 74 } 75 76 impl std::error::Error for DrawError {} 77