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
use amethyst_assets::{Asset, Error, Handle, ProcessingState, ResultExt, SimpleFormat};
use amethyst_core::specs::prelude::VecStorage;
use gfx_glyph::Font;
pub struct FontAsset(pub Font<'static>);
pub type FontHandle = Handle<FontAsset>;
#[derive(Clone)]
pub struct FontData(Font<'static>);
impl Asset for FontAsset {
const NAME: &'static str = "ui::Font";
type Data = FontData;
type HandleStorage = VecStorage<Handle<Self>>;
}
impl Into<Result<ProcessingState<FontAsset>, Error>> for FontData {
fn into(self) -> Result<ProcessingState<FontAsset>, Error> {
Ok(ProcessingState::Loaded(FontAsset(self.0)))
}
}
pub type OtfFormat = TtfFormat;
#[derive(Clone)]
pub struct TtfFormat;
impl SimpleFormat<FontAsset> for TtfFormat {
const NAME: &'static str = "TTF/OTF";
type Options = ();
fn import(&self, bytes: Vec<u8>, _: ()) -> Result<FontData, Error> {
Font::from_bytes(bytes)
.map(|f| FontData(f))
.chain_err(|| "Font parsing error")
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum FontFormat {
Ttf,
Otf,
}
impl SimpleFormat<FontAsset> for FontFormat {
const NAME: &'static str = "FontFormat";
type Options = ();
fn import(&self, bytes: Vec<u8>, _: ()) -> Result<FontData, Error> {
match *self {
FontFormat::Ttf | FontFormat::Otf => TtfFormat.import(bytes, ()),
}
}
}