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
mod fira_mono;
use std::collections::HashMap;
use crate::device::Device;
use crate::error::Result;
use crate::image::Format;
use crate::image::Mips;
use crate::image::Texture;
use crate::math::Vec4;
use crate::pipeline::Uniforms;
pub struct Font {
char_data: HashMap<char, CharData>,
texture: Texture,
}
#[derive(Debug, Copy, Clone)]
pub(crate) struct CharData {
pub(crate) width: f32,
pub(crate) height: f32,
pub(crate) uvs: Vec4,
pub(crate) x_offset: f32,
pub(crate) y_offset: f32,
pub(crate) advance: f32,
}
impl Font {
pub(crate) fn fira_mono(device: &Device, uniforms: &mut Uniforms) -> Result<Self> {
let atlas_width = fira_mono::ATLAS_WIDTH;
let atlas_height = fira_mono::ATLAS_HEIGHT;
let line_height = fira_mono::LINE_HEIGHT;
let texture = Texture::new(
device,
uniforms,
fira_mono::DATA.to_vec(),
atlas_width,
atlas_height,
Format::Gray,
Mips::Zero,
)?;
let mut char_data = HashMap::new();
for (c, metrics) in fira_mono::metrics() {
let u_min = metrics.x as f32 / atlas_width as f32;
let v_min = metrics.y as f32 / atlas_height as f32;
let u_max = u_min + (metrics.width as f32 / atlas_width as f32);
let v_max = v_min + (metrics.height as f32 / atlas_height as f32);
let uvs = Vec4::new(u_min, v_min, u_max, v_max);
let width = metrics.width as f32 / line_height as f32;
let height = metrics.height as f32 / line_height as f32;
let x_offset = metrics.xo as f32 / line_height as f32;
let y_offset = metrics.yo as f32 / line_height as f32;
let advance = metrics.advance as f32 / line_height as f32;
char_data.insert(
c,
CharData {
width,
height,
uvs,
x_offset,
y_offset,
advance,
},
);
}
Ok(Self { char_data, texture })
}
pub(crate) const fn texture(&self) -> &Texture {
&self.texture
}
pub(crate) fn char_data(&self, c: char) -> CharData {
match self.char_data.get(&c) {
Some(data) => *data,
None => *self.char_data.get(&'?').expect("bad default"),
}
}
pub(crate) fn destroy(&self, device: &Device, uniforms: &mut Uniforms) {
self.texture.destroy(device, uniforms);
}
}