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
use super::*;
/// Deterministic cross-platform `TextMeasurer` implementation.
impl TextMeasurer for DeterministicTextMeasurer {
/// Measures and lays out `text` identically on every operating system and rendering backend.
///
/// # Arguments
///
/// - `&Self` - Measurer instance.
/// - `&str` - Input UTF-8 string.
/// - `TextStyle` - Active typography style.
/// - `Option<f32>` - Optional maximum available width.
///
/// # Returns
///
/// - `TextLayoutResult` - Measured lines and bounding box.
fn measure(&self, text: &str, style: TextStyle, max_width: Option<f32>) -> TextLayoutResult {
let line_height_px: f32 = (style.get_font_size() * style.get_line_height()).max(1.0);
if text.is_empty() {
return TextLayoutResult::new(
Vec::new(),
Size::new(0.0, line_height_px),
line_height_px,
);
}
let mut lines: Vec<TextLine> = Vec::new();
let wrap_limit: Option<f32> = match style.get_overflow() {
TextOverflow::Wrap => max_width.filter(|w: &f32| *w > 0.0),
TextOverflow::Clip | TextOverflow::Ellipsis => None,
};
for raw_line in text.split('\n') {
if let Some(raw_limit) = wrap_limit {
let limit: f32 = raw_limit.max(1.0);
let mut current: String = String::new();
let mut current_w: f32 = 0.0;
let mut word: String = String::new();
let mut word_w: f32 = 0.0;
let space_w: f32 = measure_char_advance(' ', style);
for ch in raw_line.chars() {
let ch_w: f32 = measure_char_advance(ch, style);
if ch == ' ' {
if !current.is_empty() && current_w + word_w > limit {
while current.ends_with(' ') {
let _: Option<char> = current.pop();
current_w = (current_w - space_w).max(0.0);
}
lines.push(TextLine::new(take(&mut current), current_w));
current_w = 0.0;
}
if !word.is_empty() {
current.push_str(&word);
current_w += word_w;
word.clear();
word_w = 0.0;
}
if !current.is_empty() && current_w + ch_w <= limit {
current.push(' ');
current_w += ch_w;
}
} else if is_wide_unicode(ch) {
if !word.is_empty() {
if !current.is_empty() && current_w + word_w > limit {
while current.ends_with(' ') {
let _: Option<char> = current.pop();
current_w = (current_w - space_w).max(0.0);
}
lines.push(TextLine::new(take(&mut current), current_w));
current_w = 0.0;
}
current.push_str(&word);
current_w += word_w;
word.clear();
word_w = 0.0;
}
if !current.is_empty() && current_w + ch_w > limit {
lines.push(TextLine::new(take(&mut current), current_w));
current_w = 0.0;
}
current.push(ch);
current_w += ch_w;
} else {
if !current.is_empty()
&& current_w + word_w + ch_w > limit
&& word_w + ch_w > limit
{
while current.ends_with(' ') {
let _: Option<char> = current.pop();
current_w = (current_w - space_w).max(0.0);
}
lines.push(TextLine::new(take(&mut current), current_w));
current_w = 0.0;
}
if !word.is_empty() && current.is_empty() && word_w + ch_w > limit {
lines.push(TextLine::new(take(&mut word), word_w));
word_w = 0.0;
}
word.push(ch);
word_w += ch_w;
}
}
if !word.is_empty() {
if !current.is_empty() && current_w + word_w > limit {
while current.ends_with(' ') {
let _: Option<char> = current.pop();
current_w = (current_w - space_w).max(0.0);
}
lines.push(TextLine::new(take(&mut current), current_w));
current_w = 0.0;
}
current.push_str(&word);
current_w += word_w;
}
while current.ends_with(' ') {
let _: Option<char> = current.pop();
current_w = (current_w - space_w).max(0.0);
}
lines.push(TextLine::new(current, current_w));
} else {
let mut width: f32 = 0.0;
for ch in raw_line.chars() {
width += measure_char_advance(ch, style);
}
if style.get_overflow() == TextOverflow::Ellipsis
&& let Some(limit) = max_width
&& width > limit + 0.5
&& limit > 0.0
&& raw_line.chars().count() > 2
{
let ellipsis_w: f32 = measure_char_advance('.', style) * 3.0;
if width <= ellipsis_w + 1.0 {
lines.push(TextLine::new(raw_line.to_string(), width));
} else {
let target_w: f32 = (limit - ellipsis_w).max(0.0);
let mut truncated: String = String::new();
let mut acc: f32 = 0.0;
for ch in raw_line.chars() {
let ch_w: f32 = measure_char_advance(ch, style);
if acc + ch_w > target_w {
break;
}
truncated.push(ch);
acc += ch_w;
}
truncated.push('.');
truncated.push('.');
truncated.push('.');
lines.push(TextLine::new(truncated, acc + ellipsis_w));
}
} else {
lines.push(TextLine::new(raw_line.to_string(), width));
}
}
}
let max_line_w: f32 = lines.iter().fold(0.0_f32, |acc: f32, line: &TextLine| {
acc.max(line.get_width())
});
let total_h: f32 = (lines.len().max(1) as f32) * line_height_px;
TextLayoutResult::new(lines, Size::new(max_line_w, total_h), line_height_px)
}
}