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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use std::ops::Deref;
use std::sync::Arc;
use figures::{Displayable, Figure, Round};
use crate::color::Color;
use crate::math::{Pixels, Point};
use crate::scene::{Element, Target};
use crate::text::Font;
#[derive(Copy, Clone, Debug)]
pub struct VMetrics {
pub ascent: Figure<f32, Pixels>,
pub descent: Figure<f32, Pixels>,
pub line_gap: Figure<f32, Pixels>,
}
impl From<rusttype::VMetrics> for VMetrics {
fn from(value: rusttype::VMetrics) -> Self {
Self {
ascent: Figure::new(value.ascent),
descent: Figure::new(value.descent),
line_gap: Figure::new(value.line_gap),
}
}
}
impl VMetrics {
#[must_use]
pub fn line_height(&self) -> Figure<f32, Pixels> {
self.height() + self.line_gap
}
#[must_use]
pub fn height(&self) -> Figure<f32, Pixels> {
self.ascent - self.descent
}
}
#[derive(Clone, Debug)]
pub struct PreparedSpan {
pub location: Point<f32, Pixels>,
data: Arc<PreparedSpanData>,
}
impl PreparedSpan {
#[must_use]
pub(crate) fn new(
font: Font,
size: Figure<f32, Pixels>,
color: Color,
width: Figure<f32, Pixels>,
characters: Vec<char>,
glyphs: Vec<GlyphInfo>,
metrics: rusttype::VMetrics,
) -> Self {
Self {
location: Point::default(),
data: Arc::new(PreparedSpanData {
font,
size,
color,
width,
characters,
glyphs,
metrics,
}),
}
}
#[must_use]
pub(crate) fn translate(&self, location: Point<f32, Pixels>) -> Self {
Self {
location: location.round(),
data: self.data.clone(),
}
}
#[allow(clippy::needless_pass_by_value)]
pub fn render_baseline_at(
&self,
scene: &Target,
location: impl Displayable<f32, Pixels = Point<f32, Pixels>>,
) -> crate::Result<()> {
let effective_scale_factor = scene.scale();
let location =
scene.offset_point_raw(location.to_pixels(effective_scale_factor) + self.location);
scene.push_element(Element::Text {
span: self.translate(location),
clip: scene.clip,
});
Ok(())
}
}
impl Deref for PreparedSpan {
type Target = PreparedSpanData;
fn deref(&self) -> &Self::Target {
self.data.as_ref()
}
}
#[derive(Debug)]
pub struct PreparedSpanData {
pub font: Font,
pub size: Figure<f32, Pixels>,
pub color: Color,
pub width: Figure<f32, Pixels>,
pub characters: Vec<char>,
pub glyphs: Vec<GlyphInfo>,
pub metrics: rusttype::VMetrics,
}
#[derive(Debug)]
pub struct GlyphInfo {
pub source_offset: usize,
pub source: char,
pub glyph: rusttype::PositionedGlyph<'static>,
}
impl GlyphInfo {
#[must_use]
pub fn width(&self) -> Figure<f32, Pixels> {
Figure::new(self.glyph.unpositioned().h_metrics().advance_width)
}
#[must_use]
pub fn location(&self) -> Point<f32, Pixels> {
Point::new(self.glyph.position().x, self.glyph.position().y)
}
}