line.rs

  1use crate::{
  2    black, point, px, BorrowWindow, Bounds, Hsla, LineLayout, Pixels, Point, Result, SharedString,
  3    UnderlineStyle, WindowContext, WrapBoundary, WrappedLineLayout,
  4};
  5use derive_more::{Deref, DerefMut};
  6use smallvec::SmallVec;
  7use std::sync::Arc;
  8
  9#[derive(Debug, Clone)]
 10pub struct DecorationRun {
 11    pub len: u32,
 12    pub color: Hsla,
 13    pub underline: Option<UnderlineStyle>,
 14}
 15
 16#[derive(Clone, Default, Debug, Deref, DerefMut)]
 17pub struct ShapedLine {
 18    #[deref]
 19    #[deref_mut]
 20    pub(crate) layout: Arc<LineLayout>,
 21    pub text: SharedString,
 22    pub(crate) decoration_runs: SmallVec<[DecorationRun; 32]>,
 23}
 24
 25impl ShapedLine {
 26    pub fn len(&self) -> usize {
 27        self.layout.len
 28    }
 29
 30    pub fn paint(
 31        &self,
 32        origin: Point<Pixels>,
 33        line_height: Pixels,
 34        cx: &mut WindowContext,
 35    ) -> Result<()> {
 36        paint_line(
 37            origin,
 38            &self.layout,
 39            line_height,
 40            &self.decoration_runs,
 41            None,
 42            &[],
 43            cx,
 44        )?;
 45
 46        Ok(())
 47    }
 48}
 49
 50#[derive(Clone, Default, Debug, Deref, DerefMut)]
 51pub struct WrappedLine {
 52    #[deref]
 53    #[deref_mut]
 54    pub(crate) layout: Arc<WrappedLineLayout>,
 55    pub text: SharedString,
 56    pub(crate) decoration_runs: SmallVec<[DecorationRun; 32]>,
 57}
 58
 59impl WrappedLine {
 60    pub fn len(&self) -> usize {
 61        self.layout.len()
 62    }
 63
 64    pub fn paint(
 65        &self,
 66        origin: Point<Pixels>,
 67        line_height: Pixels,
 68        cx: &mut WindowContext,
 69    ) -> Result<()> {
 70        paint_line(
 71            origin,
 72            &self.layout.unwrapped_layout,
 73            line_height,
 74            &self.decoration_runs,
 75            self.wrap_width,
 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_width: Option<Pixels>,
 90    wrap_boundaries: &[WrapBoundary],
 91    cx: &mut WindowContext<'_>,
 92) -> Result<()> {
 93    let padding_top = (line_height - layout.ascent - layout.descent) / 2.;
 94    let baseline_offset = point(px(0.), padding_top + layout.ascent);
 95    let mut decoration_runs = decoration_runs.iter();
 96    let mut wraps = wrap_boundaries.iter().peekable();
 97    let mut run_end = 0;
 98    let mut color = black();
 99    let mut current_underline: Option<(Point<Pixels>, UnderlineStyle)> = 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
105            .bounding_box(run.font_id, layout.font_size)?
106            .size;
107
108        for (glyph_ix, glyph) in run.glyphs.iter().enumerate() {
109            glyph_origin.x += glyph.position.x - prev_glyph_position.x;
110
111            if wraps.peek() == Some(&&WrapBoundary { run_ix, glyph_ix }) {
112                wraps.next();
113                if let Some((underline_origin, underline_style)) = current_underline.take() {
114                    cx.paint_underline(
115                        underline_origin,
116                        glyph_origin.x - underline_origin.x,
117                        &underline_style,
118                    )?;
119                }
120
121                glyph_origin.x = origin.x;
122                glyph_origin.y += line_height;
123            }
124            prev_glyph_position = glyph.position;
125
126            let mut finished_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
127            if glyph.index >= run_end {
128                if let Some(style_run) = decoration_runs.next() {
129                    if let Some((_, underline_style)) = &mut current_underline {
130                        if style_run.underline.as_ref() != Some(underline_style) {
131                            finished_underline = current_underline.take();
132                        }
133                    }
134                    if let Some(run_underline) = style_run.underline.as_ref() {
135                        current_underline.get_or_insert((
136                            point(
137                                glyph_origin.x,
138                                origin.y + baseline_offset.y + (layout.descent * 0.618),
139                            ),
140                            UnderlineStyle {
141                                color: Some(run_underline.color.unwrap_or(style_run.color)),
142                                thickness: run_underline.thickness,
143                                wavy: run_underline.wavy,
144                            },
145                        ));
146                    }
147
148                    run_end += style_run.len as usize;
149                    color = style_run.color;
150                } else {
151                    run_end = layout.len;
152                    finished_underline = current_underline.take();
153                }
154            }
155
156            if let Some((underline_origin, underline_style)) = finished_underline {
157                cx.paint_underline(
158                    underline_origin,
159                    glyph_origin.x - underline_origin.x,
160                    &underline_style,
161                )?;
162            }
163
164            let max_glyph_bounds = Bounds {
165                origin: glyph_origin,
166                size: max_glyph_size,
167            };
168
169            let content_mask = cx.content_mask();
170            if max_glyph_bounds.intersects(&content_mask.bounds) {
171                if glyph.is_emoji {
172                    cx.paint_emoji(
173                        glyph_origin + baseline_offset,
174                        run.font_id,
175                        glyph.id,
176                        layout.font_size,
177                    )?;
178                } else {
179                    cx.paint_glyph(
180                        glyph_origin + baseline_offset,
181                        run.font_id,
182                        glyph.id,
183                        layout.font_size,
184                        color,
185                    )?;
186                }
187            }
188        }
189    }
190
191    if let Some((underline_start, underline_style)) = current_underline.take() {
192        let line_end_x = origin.x + wrap_width.unwrap_or(Pixels::MAX).min(layout.width);
193        cx.paint_underline(
194            underline_start,
195            line_end_x - underline_start.x,
196            &underline_style,
197        )?;
198    }
199
200    Ok(())
201}