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
use std::fs;
use font_kit::handle::Handle as FontKitHandle;
use log::{error, warn};
use amethyst_assets::{AssetStorage, Format, Loader};
use crate::{
font::systemfont::default_system_font,
format::{FontAsset, FontHandle, TtfFormat},
};
pub fn get_default_font(loader: &Loader, storage: &AssetStorage<FontAsset>) -> FontHandle {
let system_font = default_system_font();
match system_font {
Ok(handle) => match handle {
FontKitHandle::Path { path, .. } => {
if let Some(file_extension) = path.extension() {
let format = match file_extension.to_str() {
Some(ext) => {
if ext.eq_ignore_ascii_case("ttf") || ext.eq_ignore_ascii_case("otf") {
Some(TtfFormat)
} else {
None
}
}
None => None,
};
if let Some(format) = format {
match fs::read(&path) {
Ok(bytes) => match format.import_simple(bytes) {
Ok(data) => return loader.load_from_data(data, (), storage),
Err(err) => warn!("System font at '{}' cannot be loaded. Fallback to default. Error: {}", path.display(), err),
},
Err(err) => warn!("System font at '{}' is not available for use. Fallback to default. Error: {}", path.display(), err)
}
} else {
error!("System font '{}' has unknown format", path.display());
}
} else {
warn!("System font has no file extension!");
}
}
FontKitHandle::Memory { bytes, .. } => {
let font_data = TtfFormat.import_simple(bytes.to_vec());
match font_data {
Ok(data) => return loader.load_from_data(data, (), storage),
Err(e) => warn!("Failed to load default system font from bytes. Falling back to built-in.\nError: {:?}", e),
}
}
},
Err(e) => warn!(
"Failed to find suitable default system font. Falling back to built-in.\nError: {:?}",
e
),
}
loader.load_from_data(
TtfFormat
.import_simple(include_bytes!("./square.ttf").to_vec())
.expect("Unable to import fallback font './square.ttf'"),
(),
storage,
)
}