1use crate::{
2 black, fill, point, px, size, App, Bounds, Half, Hsla, LineLayout, Pixels, Point, Result,
3 SharedString, StrikethroughStyle, TextAlign, UnderlineStyle, Window, WrapBoundary,
4 WrappedLineLayout,
5};
6use derive_more::{Deref, DerefMut};
7use smallvec::SmallVec;
8use std::sync::Arc;
9
10/// Set the text decoration for a run of text.
11#[derive(Debug, Clone)]
12pub struct DecorationRun {
13 /// The length of the run in utf-8 bytes.
14 pub len: u32,
15
16 /// The color for this run
17 pub color: Hsla,
18
19 /// The background color for this run
20 pub background_color: Option<Hsla>,
21
22 /// The underline style for this run
23 pub underline: Option<UnderlineStyle>,
24
25 /// The strikethrough style for this run
26 pub strikethrough: Option<StrikethroughStyle>,
27}
28
29/// A line of text that has been shaped and decorated.
30#[derive(Clone, Default, Debug, Deref, DerefMut)]
31pub struct ShapedLine {
32 #[deref]
33 #[deref_mut]
34 pub(crate) layout: Arc<LineLayout>,
35 /// The text that was shaped for this line.
36 pub text: SharedString,
37 pub(crate) decoration_runs: SmallVec<[DecorationRun; 32]>,
38}
39
40impl ShapedLine {
41 /// The length of the line in utf-8 bytes.
42 #[allow(clippy::len_without_is_empty)]
43 pub fn len(&self) -> usize {
44 self.layout.len
45 }
46
47 /// Override the len, useful if you're rendering text a
48 /// as text b (e.g. rendering invisibles).
49 pub fn with_len(mut self, len: usize) -> Self {
50 let layout = self.layout.as_ref();
51 self.layout = Arc::new(LineLayout {
52 font_size: layout.font_size,
53 width: layout.width,
54 ascent: layout.ascent,
55 descent: layout.descent,
56 runs: layout.runs.clone(),
57 len,
58 });
59 self
60 }
61
62 /// Paint the line of text to the window.
63 pub fn paint(
64 &self,
65 origin: Point<Pixels>,
66 line_height: Pixels,
67 window: &mut Window,
68 cx: &mut App,
69 ) -> Result<()> {
70 paint_line(
71 origin,
72 &self.layout,
73 line_height,
74 TextAlign::default(),
75 None,
76 &self.decoration_runs,
77 &[],
78 window,
79 cx,
80 )?;
81
82 Ok(())
83 }
84}
85
86/// A line of text that has been shaped, decorated, and wrapped by the text layout system.
87#[derive(Clone, Default, Debug, Deref, DerefMut)]
88pub struct WrappedLine {
89 #[deref]
90 #[deref_mut]
91 pub(crate) layout: Arc<WrappedLineLayout>,
92 /// The text that was shaped for this line.
93 pub text: SharedString,
94 pub(crate) decoration_runs: SmallVec<[DecorationRun; 32]>,
95}
96
97impl WrappedLine {
98 /// The length of the underlying, unwrapped layout, in utf-8 bytes.
99 #[allow(clippy::len_without_is_empty)]
100 pub fn len(&self) -> usize {
101 self.layout.len()
102 }
103
104 /// Paint this line of text to the window.
105 pub fn paint(
106 &self,
107 origin: Point<Pixels>,
108 line_height: Pixels,
109 align: TextAlign,
110 window: &mut Window,
111 cx: &mut App,
112 ) -> Result<()> {
113 paint_line(
114 origin,
115 &self.layout.unwrapped_layout,
116 line_height,
117 align,
118 self.layout.wrap_width,
119 &self.decoration_runs,
120 &self.wrap_boundaries,
121 window,
122 cx,
123 )?;
124
125 Ok(())
126 }
127}
128
129#[allow(clippy::too_many_arguments)]
130fn paint_line(
131 origin: Point<Pixels>,
132 layout: &LineLayout,
133 line_height: Pixels,
134 align: TextAlign,
135 align_width: Option<Pixels>,
136 decoration_runs: &[DecorationRun],
137 wrap_boundaries: &[WrapBoundary],
138 window: &mut Window,
139 cx: &mut App,
140) -> Result<()> {
141 let line_bounds = Bounds::new(
142 origin,
143 size(
144 layout.width,
145 line_height * (wrap_boundaries.len() as f32 + 1.),
146 ),
147 );
148 window.paint_layer(line_bounds, |window| {
149 let padding_top = (line_height - layout.ascent - layout.descent) / 2.;
150 let baseline_offset = point(px(0.), padding_top + layout.ascent);
151 let mut decoration_runs = decoration_runs.iter();
152 let mut wraps = wrap_boundaries.iter().peekable();
153 let mut run_end = 0;
154 let mut color = black();
155 let mut current_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
156 let mut current_strikethrough: Option<(Point<Pixels>, StrikethroughStyle)> = None;
157 let mut current_background: Option<(Point<Pixels>, Hsla)> = None;
158 let text_system = cx.text_system().clone();
159 let mut glyph_origin = point(
160 aligned_origin_x(
161 origin,
162 align_width.unwrap_or(layout.width),
163 px(0.0),
164 &align,
165 layout,
166 wraps.peek(),
167 ),
168 origin.y,
169 );
170 let mut prev_glyph_position = Point::default();
171 let mut max_glyph_size = size(px(0.), px(0.));
172 for (run_ix, run) in layout.runs.iter().enumerate() {
173 max_glyph_size = text_system.bounding_box(run.font_id, layout.font_size).size;
174
175 for (glyph_ix, glyph) in run.glyphs.iter().enumerate() {
176 glyph_origin.x += glyph.position.x - prev_glyph_position.x;
177
178 if wraps.peek() == Some(&&WrapBoundary { run_ix, glyph_ix }) {
179 wraps.next();
180 if let Some((background_origin, background_color)) = current_background.as_mut()
181 {
182 if glyph_origin.x == background_origin.x {
183 background_origin.x -= max_glyph_size.width.half()
184 }
185 window.paint_quad(fill(
186 Bounds {
187 origin: *background_origin,
188 size: size(glyph_origin.x - background_origin.x, line_height),
189 },
190 *background_color,
191 ));
192 background_origin.x = origin.x;
193 background_origin.y += line_height;
194 }
195 if let Some((underline_origin, underline_style)) = current_underline.as_mut() {
196 if glyph_origin.x == underline_origin.x {
197 underline_origin.x -= max_glyph_size.width.half();
198 };
199 window.paint_underline(
200 *underline_origin,
201 glyph_origin.x - underline_origin.x,
202 underline_style,
203 );
204 underline_origin.x = origin.x;
205 underline_origin.y += line_height;
206 }
207 if let Some((strikethrough_origin, strikethrough_style)) =
208 current_strikethrough.as_mut()
209 {
210 if glyph_origin.x == strikethrough_origin.x {
211 strikethrough_origin.x -= max_glyph_size.width.half();
212 };
213 window.paint_strikethrough(
214 *strikethrough_origin,
215 glyph_origin.x - strikethrough_origin.x,
216 strikethrough_style,
217 );
218 strikethrough_origin.x = origin.x;
219 strikethrough_origin.y += line_height;
220 }
221
222 glyph_origin.x = aligned_origin_x(
223 origin,
224 align_width.unwrap_or(layout.width),
225 prev_glyph_position.x,
226 &align,
227 layout,
228 wraps.peek(),
229 );
230 glyph_origin.y += line_height;
231 }
232 prev_glyph_position = glyph.position;
233
234 let mut finished_background: Option<(Point<Pixels>, Hsla)> = None;
235 let mut finished_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
236 let mut finished_strikethrough: Option<(Point<Pixels>, StrikethroughStyle)> = None;
237 if glyph.index >= run_end {
238 let mut style_run = decoration_runs.next();
239
240 // ignore style runs that apply to a partial glyph
241 while let Some(run) = style_run {
242 if glyph.index < run_end + (run.len as usize) {
243 break;
244 }
245 run_end += run.len as usize;
246 style_run = decoration_runs.next();
247 }
248
249 if let Some(style_run) = style_run {
250 if let Some((_, background_color)) = &mut current_background {
251 if style_run.background_color.as_ref() != Some(background_color) {
252 finished_background = current_background.take();
253 }
254 }
255 if let Some(run_background) = style_run.background_color {
256 current_background.get_or_insert((
257 point(glyph_origin.x, glyph_origin.y),
258 run_background,
259 ));
260 }
261
262 if let Some((_, underline_style)) = &mut current_underline {
263 if style_run.underline.as_ref() != Some(underline_style) {
264 finished_underline = current_underline.take();
265 }
266 }
267 if let Some(run_underline) = style_run.underline.as_ref() {
268 current_underline.get_or_insert((
269 point(
270 glyph_origin.x,
271 glyph_origin.y + baseline_offset.y + (layout.descent * 0.618),
272 ),
273 UnderlineStyle {
274 color: Some(run_underline.color.unwrap_or(style_run.color)),
275 thickness: run_underline.thickness,
276 wavy: run_underline.wavy,
277 },
278 ));
279 }
280 if let Some((_, strikethrough_style)) = &mut current_strikethrough {
281 if style_run.strikethrough.as_ref() != Some(strikethrough_style) {
282 finished_strikethrough = current_strikethrough.take();
283 }
284 }
285 if let Some(run_strikethrough) = style_run.strikethrough.as_ref() {
286 current_strikethrough.get_or_insert((
287 point(
288 glyph_origin.x,
289 glyph_origin.y
290 + (((layout.ascent * 0.5) + baseline_offset.y) * 0.5),
291 ),
292 StrikethroughStyle {
293 color: Some(run_strikethrough.color.unwrap_or(style_run.color)),
294 thickness: run_strikethrough.thickness,
295 },
296 ));
297 }
298
299 run_end += style_run.len as usize;
300 color = style_run.color;
301 } else {
302 run_end = layout.len;
303 finished_background = current_background.take();
304 finished_underline = current_underline.take();
305 finished_strikethrough = current_strikethrough.take();
306 }
307 }
308
309 if let Some((mut background_origin, background_color)) = finished_background {
310 let mut width = glyph_origin.x - background_origin.x;
311 if background_origin.x == glyph_origin.x {
312 background_origin.x -= max_glyph_size.width.half();
313 };
314 window.paint_quad(fill(
315 Bounds {
316 origin: background_origin,
317 size: size(width, line_height),
318 },
319 background_color,
320 ));
321 }
322
323 if let Some((mut underline_origin, underline_style)) = finished_underline {
324 if underline_origin.x == glyph_origin.x {
325 underline_origin.x -= max_glyph_size.width.half();
326 };
327 window.paint_underline(
328 underline_origin,
329 glyph_origin.x - underline_origin.x,
330 &underline_style,
331 );
332 }
333
334 if let Some((mut strikethrough_origin, strikethrough_style)) =
335 finished_strikethrough
336 {
337 if strikethrough_origin.x == glyph_origin.x {
338 strikethrough_origin.x -= max_glyph_size.width.half();
339 };
340 window.paint_strikethrough(
341 strikethrough_origin,
342 glyph_origin.x - strikethrough_origin.x,
343 &strikethrough_style,
344 );
345 }
346
347 let max_glyph_bounds = Bounds {
348 origin: glyph_origin,
349 size: max_glyph_size,
350 };
351
352 let content_mask = window.content_mask();
353 if max_glyph_bounds.intersects(&content_mask.bounds) {
354 if glyph.is_emoji {
355 window.paint_emoji(
356 glyph_origin + baseline_offset,
357 run.font_id,
358 glyph.id,
359 layout.font_size,
360 )?;
361 } else {
362 window.paint_glyph(
363 glyph_origin + baseline_offset,
364 run.font_id,
365 glyph.id,
366 layout.font_size,
367 color,
368 )?;
369 }
370 }
371 }
372 }
373
374 let mut last_line_end_x = origin.x + layout.width;
375 if let Some(boundary) = wrap_boundaries.last() {
376 let run = &layout.runs[boundary.run_ix];
377 let glyph = &run.glyphs[boundary.glyph_ix];
378 last_line_end_x -= glyph.position.x;
379 }
380
381 if let Some((mut background_origin, background_color)) = current_background.take() {
382 if last_line_end_x == background_origin.x {
383 background_origin.x -= max_glyph_size.width.half()
384 };
385 window.paint_quad(fill(
386 Bounds {
387 origin: background_origin,
388 size: size(last_line_end_x - background_origin.x, line_height),
389 },
390 background_color,
391 ));
392 }
393
394 if let Some((mut underline_start, underline_style)) = current_underline.take() {
395 if last_line_end_x == underline_start.x {
396 underline_start.x -= max_glyph_size.width.half()
397 };
398 window.paint_underline(
399 underline_start,
400 last_line_end_x - underline_start.x,
401 &underline_style,
402 );
403 }
404
405 if let Some((mut strikethrough_start, strikethrough_style)) = current_strikethrough.take() {
406 if last_line_end_x == strikethrough_start.x {
407 strikethrough_start.x -= max_glyph_size.width.half()
408 };
409 window.paint_strikethrough(
410 strikethrough_start,
411 last_line_end_x - strikethrough_start.x,
412 &strikethrough_style,
413 );
414 }
415
416 Ok(())
417 })
418}
419
420fn aligned_origin_x(
421 origin: Point<Pixels>,
422 align_width: Pixels,
423 last_glyph_x: Pixels,
424 align: &TextAlign,
425 layout: &LineLayout,
426 wrap_boundary: Option<&&WrapBoundary>,
427) -> Pixels {
428 let end_of_line = if let Some(WrapBoundary { run_ix, glyph_ix }) = wrap_boundary {
429 if layout.runs[*run_ix].glyphs.len() == glyph_ix + 1 {
430 // Next glyph is in next run
431 layout
432 .runs
433 .get(run_ix + 1)
434 .and_then(|run| run.glyphs.first())
435 .map_or(layout.width, |glyph| glyph.position.x)
436 } else {
437 // Get next glyph
438 layout.runs[*run_ix].glyphs[*glyph_ix + 1].position.x
439 }
440 } else {
441 layout.width
442 };
443
444 let line_width = end_of_line - last_glyph_x;
445
446 match align {
447 TextAlign::Left => origin.x,
448 TextAlign::Center => (2.0 * origin.x + align_width - line_width) / 2.0,
449 TextAlign::Right => origin.x + align_width - line_width,
450 }
451}