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
use amethyst_assets::{Asset, Error, Handle, ResultExt, SimpleFormat};
use amethyst_core::specs::prelude::VecStorage;
use rusttype::{Font, FontCollection};
pub struct FontAsset(pub Font<'static>);
pub type FontHandle = Handle<FontAsset>;
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<FontAsset, Error>> for FontData {
fn into(self) -> Result<FontAsset, Error> {
Ok(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> {
FontCollection::from_bytes(bytes)
.into_fonts()
.nth(0)
.map(|f| FontData(f))
.chain_err(|| "Font parsing error")
}
}