line.rs

  1use crate::{
  2    black, point, px, size, transparent_black, BorrowWindow, Bounds, Corners, Edges, Hsla,
  3    LineLayout, Pixels, Point, Result, SharedString, UnderlineStyle, WindowContext, WrapBoundary,
  4    WrappedLineLayout,
  5};
  6use derive_more::{Deref, DerefMut};
  7use smallvec::SmallVec;
  8use std::sync::Arc;
  9
 10#[derive(Debug, Clone)]
 11pub struct DecorationRun {
 12    pub len: u32,
 13    pub color: Hsla,
 14    pub background_color: Option<Hsla>,
 15    pub underline: Option<UnderlineStyle>,
 16}
 17
 18#[derive(Clone, Default, Debug, Deref, DerefMut)]
 19pub struct ShapedLine {
 20    #[deref]
 21    #[deref_mut]
 22    pub(crate) layout: Arc<LineLayout>,
 23    pub text: SharedString,
 24    pub(crate) decoration_runs: SmallVec<[DecorationRun; 32]>,
 25}
 26
 27impl ShapedLine {
 28    pub fn len(&self) -> usize {
 29        self.layout.len
 30    }
 31
 32    pub fn paint(
 33        &self,
 34        origin: Point<Pixels>,
 35        line_height: Pixels,
 36        cx: &mut WindowContext,
 37    ) -> Result<()> {
 38        paint_line(
 39            origin,
 40            &self.layout,
 41            line_height,
 42            &self.decoration_runs,
 43            &[],
 44            cx,
 45        )?;
 46
 47        Ok(())
 48    }
 49}
 50
 51#[derive(Clone, Default, Debug, Deref, DerefMut)]
 52pub struct WrappedLine {
 53    #[deref]
 54    #[deref_mut]
 55    pub(crate) layout: Arc<WrappedLineLayout>,
 56    pub text: SharedString,
 57    pub(crate) decoration_runs: SmallVec<[DecorationRun; 32]>,
 58}
 59
 60impl WrappedLine {
 61    pub fn len(&self) -> usize {
 62        self.layout.len()
 63    }
 64
 65    pub fn paint(
 66        &self,
 67        origin: Point<Pixels>,
 68        line_height: Pixels,
 69        cx: &mut WindowContext,
 70    ) -> Result<()> {
 71        paint_line(
 72            origin,
 73            &self.layout.unwrapped_layout,
 74            line_height,
 75            &self.decoration_runs,
 76            &self.wrap_boundaries,
 77            cx,
 78        )?;
 79
 80        Ok(())
 81    }
 82}
 83
 84fn paint_line(
 85    origin: Point<Pixels>,
 86    layout: &LineLayout,
 87    line_height: Pixels,
 88    decoration_runs: &[DecorationRun],
 89    wrap_boundaries: &[WrapBoundary],
 90    cx: &mut WindowContext<'_>,
 91) -> Result<()> {
 92    let padding_top = (line_height - layout.ascent - layout.descent) / 2.;
 93    let baseline_offset = point(px(0.), padding_top + layout.ascent);
 94    let mut decoration_runs = decoration_runs.iter();
 95    let mut wraps = wrap_boundaries.iter().peekable();
 96    let mut run_end = 0;
 97    let mut color = black();
 98    let mut current_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
 99    let mut current_background: Option<(Point<Pixels>, Hsla)> = None;
100    let text_system = cx.text_system().clone();
101    let mut glyph_origin = origin;
102    let mut prev_glyph_position = Point::default();
103    for (run_ix, run) in layout.runs.iter().enumerate() {
104        let max_glyph_size = text_system.bounding_box(run.font_id, layout.font_size).size;
105
106        for (glyph_ix, glyph) in run.glyphs.iter().enumerate() {
107            glyph_origin.x += glyph.position.x - prev_glyph_position.x;
108
109            if wraps.peek() == Some(&&WrapBoundary { run_ix, glyph_ix }) {
110                wraps.next();
111                if let Some((background_origin, background_color)) = current_background.as_mut() {
112                    cx.paint_quad(
113                        Bounds {
114                            origin: *background_origin,
115                            size: size(glyph_origin.x - background_origin.x, line_height),
116                        },
117                        Corners::default(),
118                        *background_color,
119                        Edges::default(),
120                        transparent_black(),
121                    );
122                    background_origin.x = origin.x;
123                    background_origin.y += line_height;
124                }
125                if let Some((underline_origin, underline_style)) = current_underline.as_mut() {
126                    cx.paint_underline(
127                        *underline_origin,
128                        glyph_origin.x - underline_origin.x,
129                        underline_style,
130                    );
131                    underline_origin.x = origin.x;
132                    underline_origin.y += line_height;
133                }
134
135                glyph_origin.x = origin.x;
136                glyph_origin.y += line_height;
137            }
138            prev_glyph_position = glyph.position;
139
140            let mut finished_background: Option<(Point<Pixels>, Hsla)> = None;
141            let mut finished_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
142            if glyph.index >= run_end {
143                if let Some(style_run) = decoration_runs.next() {
144                    if let Some((_, background_color)) = &mut current_background {
145                        if style_run.background_color.as_ref() != Some(background_color) {
146                            finished_background = current_background.take();
147                        }
148                    }
149                    if let Some(run_background) = style_run.background_color {
150                        current_background
151                            .get_or_insert((point(glyph_origin.x, glyph_origin.y), run_background));
152                    }
153
154                    if let Some((_, underline_style)) = &mut current_underline {
155                        if style_run.underline.as_ref() != Some(underline_style) {
156                            finished_underline = current_underline.take();
157                        }
158                    }
159                    if let Some(run_underline) = style_run.underline.as_ref() {
160                        current_underline.get_or_insert((
161                            point(
162                                glyph_origin.x,
163                                glyph_origin.y + baseline_offset.y + (layout.descent * 0.618),
164                            ),
165                            UnderlineStyle {
166                                color: Some(run_underline.color.unwrap_or(style_run.color)),
167                                thickness: run_underline.thickness,
168                                wavy: run_underline.wavy,
169                            },
170                        ));
171                    }
172
173                    run_end += style_run.len as usize;
174                    color = style_run.color;
175                } else {
176                    run_end = layout.len;
177                    finished_background = current_background.take();
178                    finished_underline = current_underline.take();
179                }
180            }
181
182            if let Some((background_origin, background_color)) = finished_background {
183                cx.paint_quad(
184                    Bounds {
185                        origin: background_origin,
186                        size: size(glyph_origin.x - background_origin.x, line_height),
187                    },
188                    Corners::default(),
189                    background_color,
190                    Edges::default(),
191                    transparent_black(),
192                );
193            }
194
195            if let Some((underline_origin, underline_style)) = finished_underline {
196                cx.paint_underline(
197                    underline_origin,
198                    glyph_origin.x - underline_origin.x,
199                    &underline_style,
200                );
201            }
202
203            let max_glyph_bounds = Bounds {
204                origin: glyph_origin,
205                size: max_glyph_size,
206            };
207
208            let content_mask = cx.content_mask();
209            if max_glyph_bounds.intersects(&content_mask.bounds) {
210                if glyph.is_emoji {
211                    cx.paint_emoji(
212                        glyph_origin + baseline_offset,
213                        run.font_id,
214                        glyph.id,
215                        layout.font_size,
216                    )?;
217                } else {
218                    cx.paint_glyph(
219                        glyph_origin + baseline_offset,
220                        run.font_id,
221                        glyph.id,
222                        layout.font_size,
223                        color,
224                    )?;
225                }
226            }
227        }
228    }
229
230    let mut last_line_end_x = origin.x + layout.width;
231    if let Some(boundary) = wrap_boundaries.last() {
232        let run = &layout.runs[boundary.run_ix];
233        let glyph = &run.glyphs[boundary.glyph_ix];
234        last_line_end_x -= glyph.position.x;
235    }
236
237    if let Some((background_origin, background_color)) = current_background.take() {
238        cx.paint_quad(
239            Bounds {
240                origin: background_origin,
241                size: size(last_line_end_x - background_origin.x, line_height),
242            },
243            Corners::default(),
244            background_color,
245            Edges::default(),
246            transparent_black(),
247        );
248    }
249
250    if let Some((underline_start, underline_style)) = current_underline.take() {
251        cx.paint_underline(
252            underline_start,
253            last_line_end_x - underline_start.x,
254            &underline_style,
255        );
256    }
257
258    Ok(())
259}