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 glyph_brush::rusttype::Font;
use serde::{Deserialize, Serialize};
use amethyst_assets::{Asset, Format, Handle, ProcessableAsset, ProcessingState};
use amethyst_core::ecs::prelude::VecStorage;
use amethyst_error::{format_err, Error, ResultExt};
#[derive(Clone)]
#[allow(missing_debug_implementations)]
pub struct FontAsset(pub Font<'static>);
pub type FontHandle = Handle<FontAsset>;
#[derive(Clone)]
#[allow(missing_debug_implementations)]
pub struct FontData(Font<'static>);
amethyst_assets::register_format_type!(FontData);
impl Asset for FontAsset {
const NAME: &'static str = "ui::Font";
type Data = FontData;
type HandleStorage = VecStorage<Handle<Self>>;
}
impl ProcessableAsset for FontAsset {
fn process(data: FontData) -> Result<ProcessingState<FontAsset>, Error> {
Ok(ProcessingState::Loaded(FontAsset(data.0)))
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TtfFormat;
amethyst_assets::register_format!("TTF", TtfFormat as FontData);
impl Format<FontData> for TtfFormat {
fn name(&self) -> &'static str {
"TTF"
}
fn import_simple(&self, bytes: Vec<u8>) -> Result<FontData, Error> {
Font::from_bytes(bytes)
.map(FontData)
.with_context(|_| format_err!("Font parsing error"))
}
}