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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use super::*;
use std::fmt;
use std::mem;
use std::sync::{Mutex, MutexGuard};
pub trait GlyphCruncher<'font> {
fn pixel_bounds_custom_layout<'a, S, L>(
&mut self,
section: S,
custom_layout: &L,
) -> Option<Rect<i32>>
where
L: GlyphPositioner + Hash,
S: Into<Cow<'a, VariedSection<'a>>>;
fn pixel_bounds<'a, S>(&mut self, section: S) -> Option<Rect<i32>>
where
S: Into<Cow<'a, VariedSection<'a>>>,
{
let section = section.into();
let layout = section.layout;
self.pixel_bounds_custom_layout(section, &layout)
}
fn glyphs_custom_layout<'a, 'b, S, L>(
&'b mut self,
section: S,
custom_layout: &L,
) -> PositionedGlyphIter<'b, 'font>
where
L: GlyphPositioner + Hash,
S: Into<Cow<'a, VariedSection<'a>>>;
fn glyphs<'a, 'b, S>(&'b mut self, section: S) -> PositionedGlyphIter<'b, 'font>
where
S: Into<Cow<'a, VariedSection<'a>>>,
{
let section = section.into();
let layout = section.layout;
self.glyphs_custom_layout(section, &layout)
}
}
pub struct GlyphCalculator<'font> {
fonts: HashMap<FontId, rusttype::Font<'font>>,
calculate_glyph_cache: Mutex<HashMap<u64, GlyphedSection<'font>>>,
}
impl<'font> fmt::Debug for GlyphCalculator<'font> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "GlyphCalculator")
}
}
impl<'font> GlyphCalculator<'font> {
pub fn cache_scope<'a>(&'a self) -> GlyphCalculatorGuard<'a, 'font> {
GlyphCalculatorGuard {
fonts: &self.fonts,
glyph_cache: self.calculate_glyph_cache.lock().unwrap(),
cached: HashSet::new(),
}
}
}
pub struct GlyphCalculatorGuard<'brush, 'font: 'brush> {
fonts: &'brush HashMap<FontId, rusttype::Font<'font>>,
glyph_cache: MutexGuard<'brush, HashMap<u64, GlyphedSection<'font>>>,
cached: HashSet<u64>,
}
impl<'brush, 'font> GlyphCalculatorGuard<'brush, 'font> {
fn cache_glyphs<L>(&mut self, section: &VariedSection, layout: &L) -> u64
where
L: GlyphPositioner,
{
let section_hash = xxhash(&(section, layout));
if let Entry::Vacant(entry) = self.glyph_cache.entry(section_hash) {
entry.insert(GlyphedSection {
bounds: layout.bounds_rect(section),
glyphs: layout.calculate_glyphs(self.fonts, section),
z: section.z,
});
}
section_hash
}
}
impl<'brush, 'font> fmt::Debug for GlyphCalculatorGuard<'brush, 'font> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "GlyphCalculatorGuard")
}
}
impl<'brush, 'font> GlyphCruncher<'font> for GlyphCalculatorGuard<'brush, 'font> {
fn pixel_bounds_custom_layout<'a, S, L>(
&mut self,
section: S,
custom_layout: &L,
) -> Option<Rect<i32>>
where
L: GlyphPositioner + Hash,
S: Into<Cow<'a, VariedSection<'a>>>,
{
let section = section.into();
let mut x = (0, 0);
let mut y = (0, 0);
let mut no_match = true;
let section_hash = self.cache_glyphs(section.borrow(), custom_layout);
self.cached.insert(section_hash);
for g in self.glyph_cache[§ion_hash]
.glyphs
.iter()
.flat_map(|&GlyphedSectionText(ref g, ..)| g.iter())
{
if let Some(Rect { min, max }) = g.pixel_bounding_box() {
if no_match || min.x < x.0 {
x.0 = min.x;
}
if no_match || min.y < y.0 {
y.0 = min.y;
}
if no_match || max.x > x.1 {
x.1 = max.x;
}
if no_match || max.y > y.1 {
y.1 = max.y;
}
no_match = false;
}
}
if no_match {
None
}
else {
Some(Rect {
min: Point { x: x.0, y: y.0 },
max: Point { x: x.1, y: y.1 },
})
}
}
fn glyphs_custom_layout<'a, 'b, S, L>(
&'b mut self,
section: S,
custom_layout: &L,
) -> PositionedGlyphIter<'b, 'font>
where
L: GlyphPositioner + Hash,
S: Into<Cow<'a, VariedSection<'a>>>,
{
let section = section.into();
let section_hash = self.cache_glyphs(section.borrow(), custom_layout);
self.cached.insert(section_hash);
self.glyph_cache[§ion_hash]
.glyphs
.iter()
.flat_map(|&GlyphedSectionText(ref g, ..)| g.iter())
}
}
impl<'a, 'b> Drop for GlyphCalculatorGuard<'a, 'b> {
fn drop(&mut self) {
let mut cached = HashSet::new();
mem::swap(&mut cached, &mut self.cached);
self.glyph_cache.retain(|key, _| cached.contains(key));
}
}
pub struct GlyphCalculatorBuilder<'a> {
font_data: Vec<Font<'a>>,
}
impl<'a> GlyphCalculatorBuilder<'a> {
pub fn using_font_bytes<B: Into<SharedBytes<'a>>>(font_0_data: B) -> Self {
Self::using_font(Font::from_bytes(font_0_data).unwrap())
}
pub fn using_fonts_bytes<B, V>(font_data: V) -> Self
where
B: Into<SharedBytes<'a>>,
V: Into<Vec<B>>,
{
Self::using_fonts(
font_data
.into()
.into_iter()
.map(|data| Font::from_bytes(data).unwrap())
.collect::<Vec<_>>(),
)
}
pub fn using_font(font_0_data: Font<'a>) -> Self {
Self {
font_data: vec![font_0_data],
}
}
pub fn using_fonts<V: Into<Vec<Font<'a>>>>(fonts: V) -> Self {
Self {
font_data: fonts.into(),
}
}
pub fn add_font_bytes<B: Into<SharedBytes<'a>>>(&mut self, font_data: B) -> FontId {
self.font_data
.push(Font::from_bytes(font_data.into()).unwrap());
FontId(self.font_data.len() - 1)
}
pub fn add_font(&mut self, font_data: Font<'a>) -> FontId {
self.font_data.push(font_data);
FontId(self.font_data.len() - 1)
}
pub fn build(self) -> GlyphCalculator<'a> {
let fonts = self.font_data
.into_iter()
.enumerate()
.map(|(idx, data)| (FontId(idx), data))
.collect();
GlyphCalculator {
fonts,
calculate_glyph_cache: Mutex::new(HashMap::new()),
}
}
}