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
|
use ab_glyph::{point, Font, Glyph, Point, ScaleFont};
use crate::wayland::ansi::{StyledChar, StyledString};
/// Simple paragraph layout for glyphs into `target`.
/// Starts at position `(0, ascent)`.
///
/// This is for testing and examples.
pub(super) fn layout_paragraph<F, SF, BF, BSF>(
font: SF,
bold_font: BSF,
position: Point,
max_width: f32,
text: &StyledString,
target: &mut Vec<(Glyph, StyledChar)>,
) where
F: Font,
SF: ScaleFont<F>,
BF: Font,
BSF: ScaleFont<BF>,
{
let v_advance = font.height() + font.line_gap();
let mut caret = position + point(0.0, font.ascent());
let mut last_glyph: Option<Glyph> = None;
for c in text.chars() {
if c.as_char().is_control() {
if c.as_char() == '\n' {
caret = point(position.x, caret.y + v_advance);
last_glyph = None;
}
continue;
}
let mut glyph = if c.is_bold() {
bold_font.scaled_glyph(c.as_char())
} else {
font.scaled_glyph(c.as_char())
};
if let Some(previous) = last_glyph.take() {
caret.x += font.kern(previous.id, glyph.id);
}
glyph.position = caret;
last_glyph = Some(glyph.clone());
caret.x += font.h_advance(glyph.id);
if !c.as_char().is_whitespace() && caret.x > position.x + max_width {
caret = point(position.x, caret.y + v_advance);
glyph.position = caret;
last_glyph = None;
}
target.push((glyph, c));
}
}
|