use core::fmt;
use raw::{model::Error, tables::glyf::bytecode::HintError, types::GlyphId};
use crate::outline::DrawError;
#[non_exhaustive]
#[derive(Clone, PartialEq, Debug)]
pub enum ScaleError {
GlyphNotFound(GlyphId),
InsufficientMemory {
required: usize,
supplied: usize,
},
Outline(Error),
Hinting(HintError),
}
impl ScaleError {
pub fn kind(&self) -> Error {
match self {
Self::GlyphNotFound(_) => Error::MissingResource,
Self::InsufficientMemory { .. } => Error::InsufficientMemory,
Self::Outline(error) => *error,
Self::Hinting(_) => Error::InvalidOperation,
}
}
pub(super) fn from_outline(error: Error, required: usize, supplied: usize) -> Self {
match error {
Error::InsufficientMemory => Self::InsufficientMemory { required, supplied },
error => Self::Outline(error),
}
}
pub(super) fn from_draw_error(error: DrawError, required: usize, supplied: usize) -> Self {
match error {
DrawError::GlyphNotFound(glyph) => Self::GlyphNotFound(glyph),
DrawError::HintingFailed(error) => Self::Hinting(error),
DrawError::PostScript(error) => Self::Outline(error),
DrawError::InsufficientMemory => Self::InsufficientMemory { required, supplied },
DrawError::RecursionLimitExceeded(_) | DrawError::TooManyPoints(_) => {
Self::Outline(Error::LimitExceeded)
}
DrawError::NoSources => Self::Outline(Error::MissingResource),
_ => Self::Outline(Error::Malformed),
}
}
}
impl From<Error> for ScaleError {
fn from(error: Error) -> Self {
Self::Outline(error)
}
}
impl From<HintError> for ScaleError {
fn from(error: HintError) -> Self {
Self::Hinting(error)
}
}
impl fmt::Display for ScaleError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::GlyphNotFound(glyph) => write!(f, "glyph {glyph} has no outline"),
Self::InsufficientMemory { required, supplied } => write!(
f,
"the glyph needs {required} bytes of memory but {supplied} were supplied"
),
Self::Outline(error) => write!(f, "{error}"),
Self::Hinting(error) => write!(f, "{error}"),
}
}
}
impl core::error::Error for ScaleError {}
#[cfg(test)]
mod tests {
use super::*;
use raw::tables::glyf::bytecode::{HintErrorKind, Program};
#[test]
fn each_variant_has_a_kind() {
let hinting = HintError {
program: Program::Font,
glyph_id: None,
pc: 0,
opcode: None,
kind: HintErrorKind::ValueStackUnderflow,
};
for (error, kind) in [
(
ScaleError::GlyphNotFound(GlyphId::new(3)),
Error::MissingResource,
),
(
ScaleError::InsufficientMemory {
required: 8,
supplied: 4,
},
Error::InsufficientMemory,
),
(
ScaleError::Outline(Error::LimitExceeded),
Error::LimitExceeded,
),
(ScaleError::Hinting(hinting), Error::InvalidOperation),
] {
assert_eq!(error.kind(), kind, "{error:?}");
}
}
#[test]
fn autohint_memory_errors_gain_the_memory_sizes() {
assert_eq!(
ScaleError::from_draw_error(DrawError::InsufficientMemory, 0, 4),
ScaleError::InsufficientMemory {
required: 0,
supplied: 4
}
);
}
#[test]
fn outline_errors_gain_the_memory_sizes() {
assert_eq!(
ScaleError::from_outline(Error::InsufficientMemory, 64, 16),
ScaleError::InsufficientMemory {
required: 64,
supplied: 16,
}
);
assert_eq!(
ScaleError::from_outline(Error::Malformed, 64, 16),
ScaleError::Outline(Error::Malformed)
);
}
}