euv_core/font/measurer/
impl.rs1use super::*;
2
3impl TextMeasurer for DeterministicTextMeasurer {
5 fn measure(&self, text: &str, style: TextStyle, max_width: Option<f32>) -> TextLayoutResult {
18 let line_height_px: f32 = (style.get_font_size() * style.get_line_height()).max(1.0);
19 if text.is_empty() {
20 return TextLayoutResult::new(
21 Vec::new(),
22 Size::new(0.0, line_height_px),
23 line_height_px,
24 );
25 }
26 let mut lines: Vec<TextLine> = Vec::new();
27 let wrap_limit: Option<f32> = match style.get_overflow() {
28 TextOverflow::Wrap => max_width.filter(|w: &f32| *w > 0.0),
29 TextOverflow::Clip | TextOverflow::Ellipsis => None,
30 };
31 for raw_line in text.split('\n') {
32 if let Some(raw_limit) = wrap_limit {
33 let limit: f32 = raw_limit + 0.5;
34 let mut current: String = String::new();
35 let mut current_w: f32 = 0.0;
36 let mut word: String = String::new();
37 let mut word_w: f32 = 0.0;
38 let space_w: f32 = measure_char_advance(' ', style);
39 for ch in raw_line.chars() {
40 let ch_w: f32 = measure_char_advance(ch, style);
41 if ch == ' ' {
42 if !current.is_empty() && current_w + word_w > limit {
43 while current.ends_with(' ') {
44 let _: Option<char> = current.pop();
45 current_w = (current_w - space_w).max(0.0);
46 }
47 lines.push(TextLine::new(take(&mut current), current_w));
48 current_w = 0.0;
49 }
50 if !word.is_empty() {
51 current.push_str(&word);
52 current_w += word_w;
53 word.clear();
54 word_w = 0.0;
55 }
56 if !current.is_empty() && current_w + ch_w <= limit {
57 current.push(' ');
58 current_w += ch_w;
59 }
60 } else if is_wide_unicode(ch) {
61 if !word.is_empty() {
62 if !current.is_empty() && current_w + word_w > limit {
63 while current.ends_with(' ') {
64 let _: Option<char> = current.pop();
65 current_w = (current_w - space_w).max(0.0);
66 }
67 lines.push(TextLine::new(take(&mut current), current_w));
68 current_w = 0.0;
69 }
70 current.push_str(&word);
71 current_w += word_w;
72 word.clear();
73 word_w = 0.0;
74 }
75 if !current.is_empty() && current_w + ch_w > limit {
76 lines.push(TextLine::new(take(&mut current), current_w));
77 current_w = 0.0;
78 }
79 current.push(ch);
80 current_w += ch_w;
81 } else {
82 if !current.is_empty()
83 && current_w + word_w + ch_w > limit
84 && word_w + ch_w > limit
85 {
86 while current.ends_with(' ') {
87 let _: Option<char> = current.pop();
88 current_w = (current_w - space_w).max(0.0);
89 }
90 lines.push(TextLine::new(take(&mut current), current_w));
91 current_w = 0.0;
92 }
93 if !word.is_empty() && current.is_empty() && word_w + ch_w > limit {
94 lines.push(TextLine::new(take(&mut word), word_w));
95 word_w = 0.0;
96 }
97 word.push(ch);
98 word_w += ch_w;
99 }
100 }
101 if !word.is_empty() {
102 if !current.is_empty() && current_w + word_w > limit {
103 while current.ends_with(' ') {
104 let _: Option<char> = current.pop();
105 current_w = (current_w - space_w).max(0.0);
106 }
107 lines.push(TextLine::new(take(&mut current), current_w));
108 current_w = 0.0;
109 }
110 current.push_str(&word);
111 current_w += word_w;
112 }
113 while current.ends_with(' ') {
114 let _: Option<char> = current.pop();
115 current_w = (current_w - space_w).max(0.0);
116 }
117 lines.push(TextLine::new(current, current_w));
118 } else {
119 let mut width: f32 = 0.0;
120 for ch in raw_line.chars() {
121 width += measure_char_advance(ch, style);
122 }
123 if style.get_overflow() == TextOverflow::Ellipsis
124 && let Some(limit) = max_width
125 && width > limit + 0.5
126 && limit > 0.0
127 && raw_line.chars().count() > 2
128 {
129 let ellipsis_w: f32 = measure_char_advance('.', style) * 3.0;
130 if width <= ellipsis_w + 1.0 {
131 lines.push(TextLine::new(raw_line.to_string(), width));
132 } else {
133 let target_w: f32 = (limit - ellipsis_w).max(0.0);
134 let mut truncated: String = String::new();
135 let mut acc: f32 = 0.0;
136 for ch in raw_line.chars() {
137 let ch_w: f32 = measure_char_advance(ch, style);
138 if acc + ch_w > target_w {
139 break;
140 }
141 truncated.push(ch);
142 acc += ch_w;
143 }
144 truncated.push('.');
145 truncated.push('.');
146 truncated.push('.');
147 lines.push(TextLine::new(truncated, acc + ellipsis_w));
148 }
149 } else {
150 lines.push(TextLine::new(raw_line.to_string(), width));
151 }
152 }
153 }
154 let max_line_w: f32 = lines.iter().fold(0.0_f32, |acc: f32, line: &TextLine| {
155 acc.max(line.get_width())
156 });
157 let total_h: f32 = (lines.len().max(1) as f32) * line_height_px;
158 TextLayoutResult::new(lines, Size::new(max_line_w, total_h), line_height_px)
159 }
160}