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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use std::cell::RefCell;
use std::fs;
use std::path::Path;
use glyph_brush::rusttype::{Rect, Scale};
use glyph_brush::{BrushAction, BrushError, FontId, GlyphCruncher, GlyphVertex, Section};
use crate::error::Result;
use crate::graphics::opengl::GLDevice;
use crate::graphics::{self, ActiveTexture, DrawParams, Drawable, Rectangle, Texture};
use crate::Context;
#[derive(Clone)]
pub(crate) struct FontQuad {
x1: f32,
y1: f32,
x2: f32,
y2: f32,
u1: f32,
v1: f32,
u2: f32,
v2: f32,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Font {
id: FontId,
}
impl Font {
pub fn new<P>(ctx: &mut Context, path: P) -> Result<Font>
where
P: AsRef<Path>,
{
let font_bytes = fs::read(path)?;
let id = ctx.graphics.font_cache.add_font_bytes(font_bytes);
Ok(Font { id })
}
pub fn from_file_data(ctx: &mut Context, data: &'static [u8]) -> Font {
let id = ctx.graphics.font_cache.add_font_bytes(data);
Font { id }
}
#[deprecated(
since = "0.2.14",
note = "Renamed to `from_file_data` for consistency with Texture and Sound."
)]
#[allow(missing_docs)]
#[inline]
pub fn from_data(ctx: &mut Context, data: &'static [u8]) -> Font {
Font::from_file_data(ctx, data)
}
}
impl Default for Font {
fn default() -> Font {
Font {
id: FontId::default(),
}
}
}
pub struct Text {
content: String,
font: Font,
size: Scale,
quads: RefCell<Vec<FontQuad>>,
}
impl Text {
pub fn new<S>(content: S, font: Font, size: f32) -> Text
where
S: Into<String>,
{
let content = content.into();
Text {
content,
font,
size: Scale::uniform(size),
quads: RefCell::new(Vec::new()),
}
}
pub fn content(&self) -> &str {
&self.content
}
pub fn set_content<S>(&mut self, content: S)
where
S: Into<String>,
{
self.content = content.into();
}
pub fn get_bounds(&self, ctx: &mut Context) -> Option<Rectangle> {
ctx.graphics
.font_cache
.pixel_bounds(self.build_section())
.map(|r| {
let x = r.min.x as f32;
let y = r.min.y as f32;
let width = r.width() as f32;
let height = r.height() as f32;
Rectangle::new(x, y, width, height)
})
}
pub fn font(&self) -> &Font {
&self.font
}
pub fn set_font(&mut self, font: Font) {
self.font = font;
}
pub fn size(&self) -> f32 {
self.size.x
}
pub fn set_size(&mut self, size: f32) {
self.size = Scale::uniform(size);
}
fn build_section(&self) -> Section<'_> {
Section {
text: &self.content,
scale: self.size,
font_id: self.font.id,
..Section::default()
}
}
fn check_for_update(&self, ctx: &mut Context) {
ctx.graphics.font_cache.queue(self.build_section());
let texture_ref = &mut ctx.graphics.font_cache_texture;
let device_ref = &mut ctx.gl;
let action = loop {
let attempted_action = ctx.graphics.font_cache.process_queued(
|rect, data| update_texture(device_ref, texture_ref, rect, data),
|v| glyph_to_quad(&v),
);
match attempted_action {
Ok(action) => break action,
Err(BrushError::TextureTooSmall { suggested, .. }) => {
let (width, height) = suggested;
*texture_ref = device_ref
.new_texture_empty(width as i32, height as i32)
.expect("Could not recreate font cache texture");
ctx.graphics.font_cache.resize_texture(width, height);
}
}
};
if let BrushAction::Draw(new_quads) = action {
*self.quads.borrow_mut() = new_quads;
}
}
}
impl Drawable for Text {
fn draw<P>(&self, ctx: &mut Context, params: P)
where
P: Into<DrawParams>,
{
let params = params.into();
self.check_for_update(ctx);
graphics::set_texture_ex(ctx, ActiveTexture::FontCache);
for quad in self.quads.borrow().iter() {
graphics::push_quad(
ctx, quad.x1, quad.y1, quad.x2, quad.y2, quad.u1, quad.v1, quad.u2, quad.v2,
¶ms,
);
}
}
}
fn update_texture(gl: &mut GLDevice, texture: &Texture, rect: Rect<u32>, data: &[u8]) {
let mut padded_data = Vec::with_capacity(data.len() * 4);
for a in data {
padded_data.push(255);
padded_data.push(255);
padded_data.push(255);
padded_data.push(*a);
}
gl.set_texture_data(
&texture,
&padded_data,
rect.min.x as i32,
rect.min.y as i32,
rect.width() as i32,
rect.height() as i32,
);
}
fn glyph_to_quad(v: &GlyphVertex) -> FontQuad {
FontQuad {
x1: v.pixel_coords.min.x as f32,
y1: v.pixel_coords.min.y as f32,
x2: v.pixel_coords.max.x as f32,
y2: v.pixel_coords.max.y as f32,
u1: v.tex_coords.min.x as f32,
v1: v.tex_coords.min.y as f32,
u2: v.tex_coords.max.x as f32,
v2: v.tex_coords.max.y as f32,
}
}