Newer
Older
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use std::collections::HashMap;
#[cfg(feature = "bevy_renderer")]
use bevy::{prelude::Handle, prelude::*, reflect::TypePath, render::texture::Image};
use unicode_segmentation::UnicodeSegmentation;
use crate::utility::{BreakableWord, MISSING, SPACE};
use crate::{
utility, Alignment, Glyph, GlyphRect, Grapheme, Line, Sdf, TextLayout, TextProperties,
};
#[cfg(feature = "bevy_renderer")]
#[derive(Debug, Clone, TypePath, PartialEq, Asset)]
pub struct KayakFont {
pub sdf: Sdf,
pub image: ImageType,
pub missing_glyph: Option<char>,
char_ids: HashMap<char, u32>,
max_glyph_size: (f32, f32),
}
#[cfg(feature = "bevy_renderer")]
#[derive(Debug, Clone, PartialEq)]
pub enum ImageType {
Atlas(Handle<Image>),
Array(Handle<Image>),
}
impl ImageType {
pub fn get(&self) -> &Handle<Image> {
match self {
Self::Atlas(handle) => handle,
Self::Array(handle) => handle,
}
}
}
#[cfg(not(feature = "bevy_renderer"))]
#[derive(Debug, Clone)]
pub struct KayakFont {
pub sdf: Sdf,
pub missing_glyph: Option<char>,
char_ids: HashMap<char, u32>,
max_glyph_size: (f32, f32),
}
impl KayakFont {
pub fn new(sdf: Sdf, #[cfg(feature = "bevy_renderer")] image_type: ImageType) -> Self {
let max_glyph_size = sdf.max_glyph_size();
assert!(
sdf.glyphs.len() < u32::MAX as usize,
"SDF contains too many glyphs"
);
let char_ids: HashMap<char, u32> = sdf
.glyphs
.iter()
.enumerate()
.map(|(idx, glyph)| (glyph.unicode, idx as u32))
.collect();
let missing_glyph = if char_ids.contains_key(&MISSING) {
Some(MISSING)
} else if char_ids.contains_key(&SPACE) {
Some(SPACE)
} else {
None
};
Self {
sdf,
#[cfg(feature = "bevy_renderer")]
image: image_type,
missing_glyph,
char_ids,
max_glyph_size,
}
}
pub fn generate_char_ids(&mut self) {
for (count, glyph) in self.sdf.glyphs.iter().enumerate() {
self.char_ids.insert(glyph.unicode, count as u32);
}
}
pub fn get_char_id(&self, c: char) -> Option<u32> {
self.char_ids.get(&c).copied()
}
pub fn get_word_width(&self, word: &str, properties: TextProperties) -> f32 {
let space_width = self.get_space_width(properties);
let tab_width = self.get_tab_width(properties);
let mut width = 0.0;
for c in word.chars() {
if utility::is_space(c) {
width += space_width;
} else if utility::is_tab(c) {
width += tab_width;
} else if let Some(glyph) = self.get_glyph(c) {
width += glyph.advance * properties.font_size;
}
}
width
}
/// Splits up the provided &str into grapheme clusters.
pub fn get_graphemes<'a>(&'a self, content: &'a str) -> Vec<&'a str> {
UnicodeSegmentation::graphemes(content, true).collect::<Vec<_>>()
}
/// Measures the given text content and calculates an appropriate layout
/// given a set of properties.
///
/// # Arguments
///
/// * `content`: The textual content to measure.
/// * `properties`: The text properties to use.
///
pub fn measure(&self, content: &str, properties: TextProperties) -> TextLayout {
let space_width = self.get_space_width(properties);
let tab_width = self.get_tab_width(properties);
let mut size: (f32, f32) = (0.0, 0.0);
let mut glyph_rects = Vec::new();
let mut lines = Vec::new();
// This is the normalized glyph bounds for all glyphs in the atlas.
// It's needed to ensure all glyphs render proportional to each other.
let norm_glyph_bounds = self.calc_glyph_size(properties.font_size);
// The current line being calculated
let mut line = Line::new(0);
let mut glyph_index = 0;
let mut char_index = 0;
// The word index to break a line before
let mut break_index = None;
// The word index until attempting to find another line break
let mut skip_until_index = None;
// We'll now split up the text content so that we can measure the layout.
// This is the "text pipeline" for this function:
// 1. Split the text by their UAX #29 word boundaries.
// 2. Split each word by its UAX #29 grapheme clusters.
// This step is important since "a̐" is technically two characters (codepoints),
// but rendered as a single glyph.
// 3. Process each character within the grapheme cluster.
//
// FIXME: I think #3 is wrong— we probably need to process the full grapheme cluster
// rather than each character individually,— however, this might take some
// careful thought and consideration, so it should probably be addressed later.
// Once resolved, this comment should be updated accordingly.
let words = utility::split_breakable_words(content).collect::<Vec<_>>();
for (index, word) in words.iter().enumerate() {
// Check if this is the last word of the line.
let mut will_break = break_index.map(|idx| index + 1 == idx).unwrap_or_default();
// === Line Break === //
// If the `break_index` is set, see if it applies.
if let Some(idx) = break_index {
if idx == index {
let next_line = Line::new_after(&line);
lines.push(line);
line = next_line;
break_index = None;
}
}
if break_index.is_none() {
match skip_until_index {
Some(idx) if index < idx => {
// Skip finding a line break since we're guaranteed not to find one until `idx`
}
_ => {
let (next_break, next_skip) =
self.find_next_break(index, line.width(), properties, &words);
break_index = next_break;
skip_until_index = next_skip;
will_break |= break_index.map(|idx| index + 1 == idx).unwrap_or_default();
}
}
}
// === Iterate Grapheme Clusters === //
for grapheme_content in word.content.graphemes(true) {
let mut grapheme = Grapheme {
position: (line.width(), properties.line_height * lines.len() as f32),
glyph_index,
char_index,
..Default::default()
};
for c in grapheme_content.chars() {
char_index += 1;
grapheme.char_total += 1;
if utility::is_newline(c) {
// Newlines (hard breaks) are already accounted for by the line break algorithm
continue;
}
if utility::is_space(c) {
if !will_break {
// Don't add the space if we're about to break the line
grapheme.size.0 += space_width;
}
} else if utility::is_tab(c) {
grapheme.size.0 += tab_width;
} else {
let glyph = self.get_glyph(c).or_else(|| {
if let Some(missing) = self.missing_glyph {
self.get_glyph(missing)
} else {
None
}
});
if let Some(glyph) = glyph {
// Character is valid glyph -> calculate its size and position
let plane_bounds = glyph.plane_bounds.as_ref();
let (left, top, _width, _height) = match plane_bounds {
Some(rect) => (
rect.left,
rect.top,
rect.width() * properties.font_size,
rect.height() * properties.font_size,
),
None => (0.0, 0.0, 0.0, 0.0),
};
// Calculate position relative to line and normalized glyph bounds
let pos_x = (grapheme.position.0 + grapheme.size.0)
+ left * properties.font_size;
let pos_y = (grapheme.position.1 + grapheme.size.1)
- top * properties.font_size;
glyph_rects.push(GlyphRect {
position: (pos_x, pos_y),
size: norm_glyph_bounds,
content: glyph.unicode,
});
glyph_index += 1;
grapheme.glyph_total += 1;
grapheme.size.0 += glyph.advance * properties.font_size;
}
}
}
line.add_grapheme(grapheme);
size.0 = size.0.max(line.width());
}
}
// Push the final line
lines.push(line);
size.1 = properties.line_height * lines.len() as f32;
// === Shift Lines & Glyphs === //
for line in lines.iter() {
let shift_x = match properties.alignment {
Alignment::Start => 0.0,
Alignment::Middle => (properties.max_size.0 - line.width()) / 2.0,
Alignment::End => properties.max_size.0 - line.width(),
};
let start = line.glyph_index();
let end = line.glyph_index() + line.total_glyphs();
#[allow(clippy::needless_range_loop)]
for index in start..end {
let rect = &mut glyph_rects[index];
rect.position.0 += shift_x;
}
}
TextLayout::new(glyph_rects, lines, size, properties)
}
/// Attempts to find the next line break for a given set of [breakable words](BreakableWord).
///
/// Each line break returned is guaranteed to be a _future_ index. That is, a line break will
/// never occur before the given index. This ensures you can always prepare for a line break
/// (e.g. remove extraneous trailing spaces) ahead of time.
///
/// # Returns
///
/// A tuple. The first field of the tuple indicates which word index to break _before_, if any.
/// The second field indicates which word index to wait _until_ before calling this method again,
/// if any. The reason for the second field is that there are cases where the line break behavior
/// can be accounted for ahead of time.
///
/// It's important that the skip index is used. Aside from it being inefficient, it may also result
/// in unexpected behavior.
///
/// # Arguments
///
/// * `curr_index`: The current word index
/// * `line_width`: The current line's current width
/// * `properties`: The associated text properties
/// * `words`: The list of breakable words
///
fn find_next_break(
&self,
curr_index: usize,
line_width: f32,
properties: TextProperties,
words: &[BreakableWord],
) -> (Option<usize>, Option<usize>) {
// Line Break Rules:
//
// Break before Next if...
// 1. Current is hard break.
// 2. Next (end-trimmed) width > Max width.
// 3. Next (end-trimmed) width + Current width > Max width.
// 4. Next (end-trimmed) width + Current width + Line width > Max width.
//
// Break after Next if...
// 5. Next is hard break.
//
// No break if...
// 6. Next ends in whitespace.
//
// Collect joined Chain of words.
//
// No break if...
// 7. Chain width + Current width + Line width <= Max width.
//
// Add Current width to Chain width if Current does not end in whitespace.
//
// Break before Next if...
// 8. Chain width <= Max width.
//
// Otherwise...
// 9. Break after Best point in Chain.
let next_index = curr_index + 1;
let curr = if let Some(curr) = words.get(curr_index) {
curr
} else {
return (None, None);
};
// 1.
if curr.hard_break {
return (Some(next_index), None);
}
let next = if let Some(next) = words.get(next_index) {
next
} else {
return (None, None);
};
let next_trimmed_width = self.get_word_width(next.content.trim_end(), properties);
// 2.
if next_trimmed_width > properties.max_size.0 {
return (Some(next_index), None);
}
let curr_width = self.get_word_width(curr.content, properties);
// 3.
if next_trimmed_width + curr_width > properties.max_size.0 {
return (Some(next_index), None);
}
// 4.
if next_trimmed_width + curr_width + line_width > properties.max_size.0 {
return (Some(next_index), None);
}
// 5.
if next.hard_break {
return (Some(next_index + 1), None);
}
// 6.
if next.content.ends_with(char::is_whitespace) {
return (None, None);
}
let mut peek_index = next_index;
let mut chain_width = 0.0;
let mut best_break_index = next_index;
while let Some(peek) = words.get(peek_index) {
chain_width += self.get_word_width(peek.content, properties);
if peek.content.ends_with(char::is_whitespace) {
// End of joined chain
break;
}
if chain_width + curr_width + line_width < properties.max_size.0 {
// Still within confines of line -> break line after here if needed
best_break_index = peek_index + 1;
}
peek_index += 1;
}
// 7.
if chain_width + curr_width + line_width <= properties.max_size.0 {
return (None, Some(peek_index));
}
if !curr.content.ends_with(char::is_whitespace) {
// Include the current word as part of the chain (if it is a part of it).
// This is only for checking if the entire chain can fit on its own line.
chain_width += curr_width;
}
// 8.
if chain_width <= properties.max_size.0 {
return (Some(next_index), Some(peek_index));
}
// 9.
(Some(best_break_index), Some(best_break_index))
}
/// Returns the pixel width of a space.
fn get_space_width(&self, properties: TextProperties) -> f32 {
if let Some(glyph) = self.get_glyph(SPACE) {
glyph.advance * properties.font_size
} else {
0.0
}
}
/// Returns the pixel width of a tab.
fn get_tab_width(&self, properties: TextProperties) -> f32 {
self.get_space_width(properties) * properties.tab_size as f32
}
/// Attempts to find the glyph corresponding to the given character.
///
/// Returns `None` if no glyph was found.
pub fn get_glyph(&self, c: char) -> Option<&Glyph> {
self.char_ids
.get(&c)
.and_then(|index| self.sdf.glyphs.get(*index as usize))
}
/// Calculates the appropriate glyph size for a desired font size.
///
/// This glyph size can then be used to provide a normalized size across all glyphs
/// in the atlas.
fn calc_glyph_size(&self, font_size: f32) -> (f32, f32) {
let font_scale = font_size / self.sdf.atlas.font_size;
(
self.max_glyph_size.0 * font_scale,
self.max_glyph_size.1 * font_scale,
)
}
}