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 | |
13 | use std::convert::From; |
14 | use std::error::Error; |
15 | use std::io; |
16 | |
17 | macro_rules! impl_display { |
18 | ($enum:ident, {$($variant:pat => $fmt_string:expr),+$(,)* }) => { |
19 | |
20 | impl ::std::fmt::Display for $enum { |
21 | fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { |
22 | use self::$enum::*; |
23 | match &self { |
24 | $( |
25 | $variant => write!(f, "{}" , $fmt_string), |
26 | )+ |
27 | } |
28 | } |
29 | } |
30 | }; |
31 | } |
32 | |
33 | /// Reasons why a loader might fail to load a font. |
34 | #[derive (Debug)] |
35 | pub enum FontLoadingError { |
36 | /// The data was of a format the loader didn't recognize. |
37 | UnknownFormat, |
38 | /// Attempted to load an invalid index in a TrueType or OpenType font collection. |
39 | /// |
40 | /// For example, if a `.ttc` file has 2 fonts in it, and you ask for the 5th one, you'll get |
41 | /// this error. |
42 | NoSuchFontInCollection, |
43 | /// Attempted to load a malformed or corrupted font. |
44 | Parse, |
45 | /// Attempted to load a font from the filesystem, but there is no filesystem (e.g. in |
46 | /// WebAssembly). |
47 | NoFilesystem, |
48 | /// A disk or similar I/O error occurred while attempting to load the font. |
49 | Io(io::Error), |
50 | } |
51 | |
52 | impl Error for FontLoadingError {} |
53 | |
54 | impl_display! { FontLoadingError, { |
55 | UnknownFormat => "unknown format" , |
56 | NoSuchFontInCollection => "no such font in the collection" , |
57 | Parse => "parse error" , |
58 | NoFilesystem => "no filesystem present" , |
59 | Io(e) => format!("I/O error: {}" , e), |
60 | } |
61 | } |
62 | |
63 | impl From<io::Error> for FontLoadingError { |
64 | fn from(error: io::Error) -> FontLoadingError { |
65 | FontLoadingError::Io(error) |
66 | } |
67 | } |
68 | |
69 | /// Reasons why a font might fail to load a glyph. |
70 | #[derive (Clone, Copy, PartialEq, Debug)] |
71 | pub enum GlyphLoadingError { |
72 | /// The font didn't contain a glyph with that ID. |
73 | NoSuchGlyph, |
74 | /// A platform function returned an error. |
75 | PlatformError, |
76 | } |
77 | |
78 | impl Error for GlyphLoadingError {} |
79 | |
80 | impl_display! { GlyphLoadingError, { |
81 | NoSuchGlyph => "no such glyph" , |
82 | PlatformError => "platform error" , |
83 | } |
84 | } |
85 | |
86 | #[cfg (target_family = "windows" )] |
87 | impl From<winapi::um::winnt::HRESULT> for GlyphLoadingError { |
88 | fn from(_err: winapi::um::winnt::HRESULT) -> GlyphLoadingError { |
89 | GlyphLoadingError::PlatformError |
90 | } |
91 | } |
92 | |
93 | /// Reasons why a source might fail to look up a font or fonts. |
94 | #[derive (Clone, Copy, PartialEq, Debug)] |
95 | pub enum SelectionError { |
96 | /// No font matching the given query was found. |
97 | NotFound, |
98 | /// The source was inaccessible because of an I/O or similar error. |
99 | CannotAccessSource, |
100 | } |
101 | |
102 | impl Error for SelectionError {} |
103 | |
104 | impl_display! { SelectionError, { |
105 | NotFound => "no font found" , |
106 | CannotAccessSource => "failed to access source" , |
107 | } |
108 | } |
109 | |