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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use std::{
fmt::Debug,
sync::{
atomic::{AtomicU64, Ordering},
Arc,
},
};
use easygpu::prelude::*;
use figures::Figure;
use lazy_static::lazy_static;
use rusttype::{gpu_cache, Scale};
use crate::math::Pixels;
lazy_static! {
static ref GLOBAL_ID_CELL: AtomicU64 = AtomicU64::new(0);
}
#[macro_export]
macro_rules! include_font {
($path:expr) => {{
let bytes = std::include_bytes!($path);
Font::try_from_bytes(bytes as &[u8]).expect("Error loading bundled font")
}};
}
#[derive(Clone, Debug)]
pub struct Font {
pub(crate) handle: Arc<FontData>,
}
impl Font {
#[must_use]
pub fn try_from_bytes(bytes: &'static [u8]) -> Option<Self> {
let font = rusttype::Font::try_from_bytes(bytes)?;
let id = GLOBAL_ID_CELL.fetch_add(1, Ordering::SeqCst);
Some(Self {
handle: Arc::new(FontData { id, font }),
})
}
#[must_use]
pub fn id(&self) -> u64 {
self.handle.id
}
#[must_use]
pub fn metrics(&self, size: Figure<f32, Pixels>) -> rusttype::VMetrics {
self.handle
.font
.v_metrics(rusttype::Scale::uniform(size.get()))
}
#[must_use]
pub fn family(&self) -> Option<String> {
match &self.handle.font {
rusttype::Font::Ref(f) => f.family_name(),
rusttype::Font::Owned(_) => None,
}
}
#[must_use]
pub(crate) fn glyph(&self, c: char) -> rusttype::Glyph<'static> {
self.handle.font.glyph(c)
}
#[must_use]
pub(crate) fn pair_kerning(
&self,
size: f32,
a: rusttype::GlyphId,
b: rusttype::GlyphId,
) -> f32 {
self.handle.font.pair_kerning(Scale::uniform(size), a, b)
}
}
#[derive(Debug)]
pub struct FontData {
pub(crate) id: u64,
pub(crate) font: rusttype::Font<'static>,
}
pub struct LoadedFont {
pub font: Font,
pub cache: gpu_cache::Cache<'static>,
pub(crate) binding: Option<BindingGroup>,
pub(crate) texture: Option<Texture>,
}
impl Debug for LoadedFont {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LoadedFont")
.field("font", &self.font)
.field("binding", &self.binding)
.field("texture", &self.texture)
.finish()
}
}
impl LoadedFont {
pub fn new(font: &Font) -> Self {
Self {
font: font.clone(),
cache: gpu_cache::Cache::builder().dimensions(512, 512).build(),
binding: None,
texture: None,
}
}
}