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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#![cfg_attr(not(feature = "font_ttf"), allow(unused))]
mod cache;
mod packer;
#[cfg(feature = "font_ttf")]
mod vector;
use std::cell::{RefCell, RefMut};
use std::fmt::{self, Debug, Formatter};
use std::path::Path;
use std::rc::Rc;
use crate::error::Result;
use crate::graphics::text::cache::{FontCache, TextGeometry};
use crate::graphics::{self, DrawParams, Drawable, Rectangle};
use crate::Context;
#[cfg(feature = "font_ttf")]
pub use crate::graphics::text::vector::VectorFontBuilder;
#[derive(Clone)]
pub struct Font {
data: Rc<RefCell<FontCache>>,
}
impl Font {
#[cfg(feature = "font_ttf")]
pub fn vector<P>(ctx: &mut Context, path: P, size: f32) -> Result<Font>
where
P: AsRef<Path>,
{
VectorFontBuilder::new(path)?.with_size(ctx, size)
}
}
impl Debug for Font {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Font").finish()
}
}
#[derive(Debug, Clone)]
pub struct Text {
content: String,
font: Font,
geometry: RefCell<Option<TextGeometry>>,
}
impl Text {
pub fn new<C>(content: C, font: Font) -> Text
where
C: Into<String>,
{
Text {
content: content.into(),
font,
geometry: RefCell::new(None),
}
}
pub fn content(&self) -> &str {
&self.content
}
pub fn set_content<C>(&mut self, content: C)
where
C: Into<String>,
{
self.geometry.replace(None);
self.content = content.into();
}
pub fn font(&self) -> &Font {
&self.font
}
pub fn set_font(&mut self, font: Font) {
self.geometry.replace(None);
self.font = font;
}
pub fn push(&mut self, ch: char) {
self.geometry.replace(None);
self.content.push(ch);
}
pub fn push_str(&mut self, string: &str) {
self.geometry.replace(None);
self.content.push_str(string);
}
pub fn pop(&mut self) -> Option<char> {
self.geometry.replace(None);
self.content.pop()
}
pub fn get_bounds(&self, ctx: &mut Context) -> Option<Rectangle> {
let geometry = self.get_latest_geometry(ctx);
geometry.bounds
}
fn get_latest_geometry(&self, ctx: &mut Context) -> RefMut<'_, TextGeometry> {
let mut data = self.font.data.borrow_mut();
let mut geometry = self.geometry.borrow_mut();
let needs_render = match &*geometry {
None => true,
Some(g) => g.resize_count != data.resize_count(),
};
if needs_render {
let new_geometry = data.render(&mut ctx.device, &self.content);
geometry.replace(new_geometry);
}
RefMut::map(geometry, |g| {
g.as_mut()
.expect("Geometry should have already been generated")
})
}
}
impl Drawable for Text {
fn draw<P>(&self, ctx: &mut Context, params: P)
where
P: Into<DrawParams>,
{
let params = params.into();
let geometry = self.get_latest_geometry(ctx);
let data = self.font.data.borrow();
graphics::set_texture(ctx, data.texture());
for quad in &geometry.quads {
graphics::push_quad(
ctx,
quad.position.x,
quad.position.y,
quad.position.right(),
quad.position.bottom(),
quad.uv.x,
quad.uv.y,
quad.uv.right(),
quad.uv.bottom(),
¶ms,
);
}
}
}