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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//! Error type.
use fontoxide_misc::Tag;
/// Errors from reading, writing or converting fonts.
///
/// The variants correspond to the exceptions fontTools raises, so that callers can
/// tell them apart the same way.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
/// `TTLibError`: malformed or unsupported font data, or a misuse of the API.
#[error("{0}")]
TtLib(String),
/// `TTLibFileIsCollectionError`: a collection was opened without a valid font
/// number.
#[error("{0}")]
FileIsCollection(String),
/// `KeyError` for a missing table.
#[error("'{0}' table not found")]
TableNotFound(Tag),
/// `KeyError` for an unknown glyph name.
#[error("glyph not found: {0}")]
GlyphNotFound(String),
/// `struct.error`: data too short to unpack, or a value that does not fit its
/// binary field.
#[error("{0}")]
Struct(String),
/// `AssertionError` raised by fontTools on inconsistent data.
#[error("assertion failed: {0}")]
Assertion(String),
/// `ValueError` / `TypeError` for invalid values.
#[error("{0}")]
Value(String),
/// An error while decompiling a table, with the table it occurred in.
#[error("error decompiling '{tag}' table: {source}")]
Decompile {
/// The table being decompiled.
tag: Tag,
/// The underlying error.
#[source]
source: Box<Error>,
},
/// An error while compiling a table, with the table it occurred in.
#[error("error compiling '{tag}' table: {source}")]
Compile {
/// The table being compiled.
tag: Tag,
/// The underlying error.
#[source]
source: Box<Error>,
},
/// `TTXParseError` or an XML syntax error.
#[error("TTX parse error: {0}")]
TtxParse(String),
/// A required table is currently being decompiled, compiled or dumped, so it
/// cannot be accessed (a circular dependency between tables).
#[error("'{0}' table is in use (circular table dependency)")]
TableBusy(Tag),
/// A table exists but is not of the requested type (for example, it could not
/// be decompiled and is kept as raw data).
#[error("'{0}' table has an unexpected type")]
WrongTableType(Tag),
/// `OTLOffsetOverflowError`: a 16-bit offset does not fit while compiling an
/// OpenType Layout table.
#[error("offset overflow: {0:?}")]
OffsetOverflow(Box<crate::ot::writer::OverflowErrorRecord>),
/// An I/O error.
#[error(transparent)]
Io(#[from] std::io::Error),
/// `ImportError`: the functionality needs an optional dependency of
/// fontTools (one of its extras), which the Cargo feature of the same name
/// leaves out; the message is fontTools' (for example
/// `No module named brotli`).
#[error("{0}")]
Import(String),
}
impl From<fontoxide_pens::Error> for Error {
fn from(e: fontoxide_pens::Error) -> Self {
match e {
fontoxide_pens::Error::Glyph(inner) => match inner.downcast::<Error>() {
Ok(e) => *e,
Err(other) => Error::TtLib(other.to_string()),
},
fontoxide_pens::Error::MissingComponent(name) => {
Error::TtLib(format!("MissingComponentError: {name}"))
}
fontoxide_pens::Error::Usage(msg) => Error::TtLib(msg),
fontoxide_pens::Error::Pen(msg) => Error::TtLib(format!("PenError: {msg}")),
}
}
}
/// Shorthand for `Result<T, fontoxide_ttlib::Error>`.
pub type Result<T, E = Error> = std::result::Result<T, E>;
impl Error {
/// A `TTLibError` with the given message.
pub fn ttlib(msg: impl Into<String>) -> Self {
Error::TtLib(msg.into())
}
/// A `ValueError` with the given message.
pub fn value(msg: impl Into<String>) -> Self {
Error::Value(msg.into())
}
/// A TTX parse error with the given message.
pub fn ttx(msg: impl Into<String>) -> Self {
Error::TtxParse(msg.into())
}
/// This error as one of [`fontoxide_pens`], to pass it through a pen.
pub fn into_pen_error(self) -> fontoxide_pens::Error {
fontoxide_pens::Error::Glyph(Box::new(self))
}
/// The innermost error, skipping the `Decompile`/`Compile` wrappers.
pub fn root_cause(&self) -> &Error {
match self {
Error::Decompile { source, .. } | Error::Compile { source, .. } => source.root_cause(),
other => other,
}
}
}