use std::path::PathBuf;
use pbf_font_tools::PbfFontError;
use crate::resources::fonts::CP_RANGE_SIZE;
#[derive(thiserror::Error, Debug)]
pub enum FontError {
#[error("Font {0} not found")]
FontNotFound(String),
#[error("Requested {requested} font ids, but at most {max} are allowed per request")]
TooManyFontIds {
requested: usize,
max: usize,
},
#[error("Font alias {0:?} is invalid: alias names must be non-empty and must not contain commas")]
InvalidAliasName(String),
#[error("Font alias {0:?} does not reference any fonts")]
EmptyAlias(String),
#[error("Font alias {alias:?} references {requested} fonts, but at most {max} are allowed")]
TooManyFontsInAlias {
alias: String,
requested: usize,
max: usize,
},
#[error("Font alias {alias:?} references {font:?}, which is itself an alias; aliases may only reference fonts")]
AliasWithinAlias {
alias: String,
font: String,
},
#[error("Font alias {alias:?} references unknown font {font:?}")]
AliasFontNotFound {
alias: String,
font: String,
},
#[error("Font range start ({start}) must be <= end ({end})")]
InvalidFontRangeStartEnd {
start: u32,
end: u32,
},
#[error("Font range start ({0}) must be multiple of {CP_RANGE_SIZE} (e.g. 0, 256, 512, ...)")]
InvalidFontRangeStart(u32),
#[error("Font range end ({0}) must be multiple of {CP_RANGE_SIZE} - 1 (e.g. 255, 511, 767, ...)")]
InvalidFontRangeEnd(u32),
#[error(
"Given font range {0}-{1} is invalid. It must be {CP_RANGE_SIZE} characters long (e.g. 0-255, 256-511, ...)"
)]
InvalidFontRange(u32, u32),
#[error(transparent)]
FreeType(#[from] pbf_font_tools::freetype::Error),
#[error("IO error accessing {1}: {0}")]
IoError(#[source] std::io::Error, PathBuf),
#[error("Invalid font file {0}")]
InvalidFontFilePath(PathBuf),
#[error("No font files found in {0}")]
NoFontFilesFound(PathBuf),
#[error("Font {0} is missing a family name")]
MissingFamilyName(PathBuf),
#[error(transparent)]
PbfFontError(#[from] PbfFontError),
#[error(transparent)]
ErrorSerializingProtobuf(#[from] pbf_font_tools::prost::DecodeError),
}
impl crate::Classify for FontError {
fn kind(&self) -> crate::ErrorKind {
use crate::ErrorKind::{Internal, InvalidInput, NotFound};
match self {
Self::FontNotFound(_) => NotFound,
Self::TooManyFontIds { .. }
| Self::InvalidFontRangeStartEnd { .. }
| Self::InvalidFontRangeStart(_)
| Self::InvalidFontRangeEnd(_)
| Self::InvalidFontRange(_, _)
| Self::InvalidAliasName(_)
| Self::EmptyAlias(_)
| Self::TooManyFontsInAlias { .. }
| Self::AliasWithinAlias { .. }
| Self::AliasFontNotFound { .. } => InvalidInput,
Self::FreeType(_)
| Self::IoError(..)
| Self::InvalidFontFilePath(_)
| Self::NoFontFilesFound(_)
| Self::MissingFamilyName(_)
| Self::PbfFontError(_)
| Self::ErrorSerializingProtobuf(_) => Internal,
}
}
}