line.rs

  1use crate::{
  2    black, fill, point, px, size, Bounds, Hsla, LineLayout, Pixels, Point, Result, SharedString,
  3    StrikethroughStyle, UnderlineStyle, WindowContext, WrapBoundary, WrappedLineLayout,
  4};
  5use derive_more::{Deref, DerefMut};
  6use smallvec::SmallVec;
  7use std::sync::Arc;
  8
  9/// Set the text decoration for a run of text.
 10#[derive(Debug, Clone)]
 11pub struct DecorationRun {
 12    /// The length of the run in utf-8 bytes.
 13    pub len: u32,
 14
 15    /// The color for this run
 16    pub color: Hsla,
 17
 18    /// The background color for this run
 19    pub background_color: Option<Hsla>,
 20
 21    /// The underline style for this run
 22    pub underline: Option<UnderlineStyle>,
 23
 24    /// The strikethrough style for this run
 25    pub strikethrough: Option<StrikethroughStyle>,
 26}
 27
 28/// A line of text that has been shaped and decorated.
 29#[derive(Clone, Default, Debug, Deref, DerefMut)]
 30pub struct ShapedLine {
 31    #[deref]
 32    #[deref_mut]
 33    pub(crate) layout: Arc<LineLayout>,
 34    /// The text that was shaped for this line.
 35    pub text: SharedString,
 36    pub(crate) decoration_runs: SmallVec<[DecorationRun; 32]>,
 37}
 38
 39impl ShapedLine {
 40    /// The length of the line in utf-8 bytes.
 41    #[allow(clippy::len_without_is_empty)]
 42    pub fn len(&self) -> usize {
 43        self.layout.len
 44    }
 45
 46    /// Paint the line of text to the window.
 47    pub fn paint(
 48        &self,
 49        origin: Point<Pixels>,
 50        line_height: Pixels,
 51        cx: &mut WindowContext,
 52    ) -> Result<()> {
 53        paint_line(
 54            origin,
 55            &self.layout,
 56            line_height,
 57            &self.decoration_runs,
 58            &[],
 59            cx,
 60        )?;
 61
 62        Ok(())
 63    }
 64}
 65
 66/// A line of text that has been shaped, decorated, and wrapped by the text layout system.
 67#[derive(Clone, Default, Debug, Deref, DerefMut)]
 68pub struct WrappedLine {
 69    #[deref]
 70    #[deref_mut]
 71    pub(crate) layout: Arc<WrappedLineLayout>,
 72    /// The text that was shaped for this line.
 73    pub text: SharedString,
 74    pub(crate) decoration_runs: SmallVec<[DecorationRun; 32]>,
 75}
 76
 77impl WrappedLine {
 78    /// The length of the underlying, unwrapped layout, in utf-8 bytes.
 79    #[allow(clippy::len_without_is_empty)]
 80    pub fn len(&self) -> usize {
 81        self.layout.len()
 82    }
 83
 84    /// Paint this line of text to the window.
 85    pub fn paint(
 86        &self,
 87        origin: Point<Pixels>,
 88        line_height: Pixels,
 89        cx: &mut WindowContext,
 90    ) -> Result<()> {
 91        paint_line(
 92            origin,
 93            &self.layout.unwrapped_layout,
 94            line_height,
 95            &self.decoration_runs,
 96            &self.wrap_boundaries,
 97            cx,
 98        )?;
 99
100        Ok(())
101    }
102}
103
104fn paint_line(
105    origin: Point<Pixels>,
106    layout: &LineLayout,
107    line_height: Pixels,
108    decoration_runs: &[DecorationRun],
109    wrap_boundaries: &[WrapBoundary],
110    cx: &mut WindowContext,
111) -> Result<()> {
112    let line_bounds = Bounds::new(
113        origin,
114        size(
115            layout.width,
116            line_height * (wrap_boundaries.len() as f32 + 1.),
117        ),
118    );
119    cx.paint_layer(line_bounds, |cx| {
120        let padding_top = (line_height - layout.ascent - layout.descent) / 2.;
121        let baseline_offset = point(px(0.), padding_top + layout.ascent);
122        let mut decoration_runs = decoration_runs.iter();
123        let mut wraps = wrap_boundaries.iter().peekable();
124        let mut run_end = 0;
125        let mut color = black();
126        let mut current_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
127        let mut current_strikethrough: Option<(Point<Pixels>, StrikethroughStyle)> = None;
128        let mut current_background: Option<(Point<Pixels>, Hsla)> = None;
129        let text_system = cx.text_system().clone();
130        let mut glyph_origin = origin;
131        let mut prev_glyph_position = Point::default();
132        for (run_ix, run) in layout.runs.iter().enumerate() {
133            let max_glyph_size = text_system.bounding_box(run.font_id, layout.font_size).size;
134
135            for (glyph_ix, glyph) in run.glyphs.iter().enumerate() {
136                glyph_origin.x += glyph.position.x - prev_glyph_position.x;
137
138                if wraps.peek() == Some(&&WrapBoundary { run_ix, glyph_ix }) {
139                    wraps.next();
140                    if let Some((background_origin, background_color)) = current_background.as_mut()
141                    {
142                        cx.paint_quad(fill(
143                            Bounds {
144                                origin: *background_origin,
145                                size: size(glyph_origin.x - background_origin.x, line_height),
146                            },
147                            *background_color,
148                        ));
149                        background_origin.x = origin.x;
150                        background_origin.y += line_height;
151                    }
152                    if let Some((underline_origin, underline_style)) = current_underline.as_mut() {
153                        cx.paint_underline(
154                            *underline_origin,
155                            glyph_origin.x - underline_origin.x,
156                            underline_style,
157                        );
158                        underline_origin.x = origin.x;
159                        underline_origin.y += line_height;
160                    }
161                    if let Some((strikethrough_origin, strikethrough_style)) =
162                        current_strikethrough.as_mut()
163                    {
164                        cx.paint_strikethrough(
165                            *strikethrough_origin,
166                            glyph_origin.x - strikethrough_origin.x,
167                            strikethrough_style,
168                        );
169                        strikethrough_origin.x = origin.x;
170                        strikethrough_origin.y += line_height;
171                    }
172
173                    glyph_origin.x = origin.x;
174                    glyph_origin.y += line_height;
175                }
176                prev_glyph_position = glyph.position;
177
178                let mut finished_background: Option<(Point<Pixels>, Hsla)> = None;
179                let mut finished_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
180                let mut finished_strikethrough: Option<(Point<Pixels>, StrikethroughStyle)> = None;
181                if glyph.index >= run_end {
182                    if let Some(style_run) = decoration_runs.next() {
183                        if let Some((_, background_color)) = &mut current_background {
184                            if style_run.background_color.as_ref() != Some(background_color) {
185                                finished_background = current_background.take();
186                            }
187                        }
188                        if let Some(run_background) = style_run.background_color {
189                            current_background.get_or_insert((
190                                point(glyph_origin.x, glyph_origin.y),
191                                run_background,
192                            ));
193                        }
194
195                        if let Some((_, underline_style)) = &mut current_underline {
196                            if style_run.underline.as_ref() != Some(underline_style) {
197                                finished_underline = current_underline.take();
198                            }
199                        }
200                        if let Some(run_underline) = style_run.underline.as_ref() {
201                            current_underline.get_or_insert((
202                                point(
203                                    glyph_origin.x,
204                                    glyph_origin.y + baseline_offset.y + (layout.descent * 0.618),
205                                ),
206                                UnderlineStyle {
207                                    color: Some(run_underline.color.unwrap_or(style_run.color)),
208                                    thickness: run_underline.thickness,
209                                    wavy: run_underline.wavy,
210                                },
211                            ));
212                        }
213                        if let Some((_, strikethrough_style)) = &mut current_strikethrough {
214                            if style_run.strikethrough.as_ref() != Some(strikethrough_style) {
215                                finished_strikethrough = current_strikethrough.take();
216                            }
217                        }
218                        if let Some(run_strikethrough) = style_run.strikethrough.as_ref() {
219                            current_strikethrough.get_or_insert((
220                                point(
221                                    glyph_origin.x,
222                                    glyph_origin.y
223                                        + (((layout.ascent * 0.5) + baseline_offset.y) * 0.5),
224                                ),
225                                StrikethroughStyle {
226                                    color: Some(run_strikethrough.color.unwrap_or(style_run.color)),
227                                    thickness: run_strikethrough.thickness,
228                                },
229                            ));
230                        }
231
232                        run_end += style_run.len as usize;
233                        color = style_run.color;
234                    } else {
235                        run_end = layout.len;
236                        finished_background = current_background.take();
237                        finished_underline = current_underline.take();
238                        finished_strikethrough = current_strikethrough.take();
239                    }
240                }
241
242                if let Some((background_origin, background_color)) = finished_background {
243                    cx.paint_quad(fill(
244                        Bounds {
245                            origin: background_origin,
246                            size: size(glyph_origin.x - background_origin.x, line_height),
247                        },
248                        background_color,
249                    ));
250                }
251
252                if let Some((underline_origin, underline_style)) = finished_underline {
253                    cx.paint_underline(
254                        underline_origin,
255                        glyph_origin.x - underline_origin.x,
256                        &underline_style,
257                    );
258                }
259
260                if let Some((strikethrough_origin, strikethrough_style)) = finished_strikethrough {
261                    cx.paint_strikethrough(
262                        strikethrough_origin,
263                        glyph_origin.x - strikethrough_origin.x,
264                        &strikethrough_style,
265                    );
266                }
267
268                let max_glyph_bounds = Bounds {
269                    origin: glyph_origin,
270                    size: max_glyph_size,
271                };
272
273                let content_mask = cx.content_mask();
274                if max_glyph_bounds.intersects(&content_mask.bounds) {
275                    if glyph.is_emoji {
276                        cx.paint_emoji(
277                            glyph_origin + baseline_offset,
278                            run.font_id,
279                            glyph.id,
280                            layout.font_size,
281                        )?;
282                    } else {
283                        cx.paint_glyph(
284                            glyph_origin + baseline_offset,
285                            run.font_id,
286                            glyph.id,
287                            layout.font_size,
288                            color,
289                        )?;
290                    }
291                }
292            }
293        }
294
295        let mut last_line_end_x = origin.x + layout.width;
296        if let Some(boundary) = wrap_boundaries.last() {
297            let run = &layout.runs[boundary.run_ix];
298            let glyph = &run.glyphs[boundary.glyph_ix];
299            last_line_end_x -= glyph.position.x;
300        }
301
302        if let Some((background_origin, background_color)) = current_background.take() {
303            cx.paint_quad(fill(
304                Bounds {
305                    origin: background_origin,
306                    size: size(last_line_end_x - background_origin.x, line_height),
307                },
308                background_color,
309            ));
310        }
311
312        if let Some((underline_start, underline_style)) = current_underline.take() {
313            cx.paint_underline(
314                underline_start,
315                last_line_end_x - underline_start.x,
316                &underline_style,
317            );
318        }
319
320        if let Some((strikethrough_start, strikethrough_style)) = current_strikethrough.take() {
321            cx.paint_strikethrough(
322                strikethrough_start,
323                last_line_end_x - strikethrough_start.x,
324                &strikethrough_style,
325            );
326        }
327
328        Ok(())
329    })
330}