1// font-kit/src/error.rs
2//
3// Copyright © 2018 The Pathfinder Project Developers.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! Various types of errors that `font-kit` can return.
12
13use std::borrow::Cow;
14use std::convert::From;
15use std::error::Error;
16use std::io;
17
18macro_rules! impl_display {
19 ($enum:ident, {$($variant:pat => $fmt_string:expr),+$(,)* }) => {
20
21 impl ::std::fmt::Display for $enum {
22 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
23 use self::$enum::*;
24 match &self {
25 $(
26 $variant => write!(f, "{}", $fmt_string),
27 )+
28 }
29 }
30 }
31 };
32}
33
34/// Reasons why a loader might fail to load a font.
35#[derive(Debug)]
36pub enum FontLoadingError {
37 /// The data was of a format the loader didn't recognize.
38 UnknownFormat,
39 /// Attempted to load an invalid index in a TrueType or OpenType font collection.
40 ///
41 /// For example, if a `.ttc` file has 2 fonts in it, and you ask for the 5th one, you'll get
42 /// this error.
43 NoSuchFontInCollection,
44 /// Attempted to load a malformed or corrupted font.
45 Parse,
46 /// Attempted to load a font from the filesystem, but there is no filesystem (e.g. in
47 /// WebAssembly).
48 NoFilesystem,
49 /// A disk or similar I/O error occurred while attempting to load the font.
50 Io(io::Error),
51}
52
53impl Error for FontLoadingError {}
54
55impl_display! { FontLoadingError, {
56 UnknownFormat => "unknown format",
57 NoSuchFontInCollection => "no such font in the collection",
58 Parse => "parse error",
59 NoFilesystem => "no filesystem present",
60 Io(e) => format!("I/O error: {}", e),
61 }
62}
63
64impl From<io::Error> for FontLoadingError {
65 fn from(error: io::Error) -> FontLoadingError {
66 FontLoadingError::Io(error)
67 }
68}
69
70/// Reasons why a font might fail to load a glyph.
71#[derive(Clone, Copy, PartialEq, Debug)]
72pub enum GlyphLoadingError {
73 /// The font didn't contain a glyph with that ID.
74 NoSuchGlyph,
75 /// A platform function returned an error.
76 PlatformError,
77}
78
79impl Error for GlyphLoadingError {}
80
81impl_display! { GlyphLoadingError, {
82 NoSuchGlyph => "no such glyph",
83 PlatformError => "platform error",
84 }
85}
86
87#[cfg(target_family = "windows")]
88impl From<winapi::um::winnt::HRESULT> for GlyphLoadingError {
89 fn from(_err: winapi::um::winnt::HRESULT) -> GlyphLoadingError {
90 GlyphLoadingError::PlatformError
91 }
92}
93
94/// Reasons why a source might fail to look up a font or fonts.
95#[derive(Clone, PartialEq, Debug)]
96pub enum SelectionError {
97 /// No font matching the given query was found.
98 NotFound,
99 /// The source was inaccessible because of an I/O or similar error.
100 CannotAccessSource {
101 /// Additional diagnostic information may include file name
102 reason: Option<Cow<'static, str>>,
103 },
104}
105
106impl Error for SelectionError {}
107
108impl_display! { SelectionError, {
109 NotFound => "no font found",
110 CannotAccessSource { reason: ref maybe_cow } => maybe_cow.as_deref().unwrap_or("failed to access source")
111 }
112}
113