1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use crate::{
    black, point, px, BorrowWindow, Bounds, Hsla, LineLayout, Pixels, Point, Result, SharedString,
    UnderlineStyle, WindowContext, WrapBoundary, WrappedLineLayout,
};
use derive_more::{Deref, DerefMut};
use smallvec::SmallVec;
use std::sync::Arc;

#[derive(Debug, Clone)]
pub struct DecorationRun {
    pub len: u32,
    pub color: Hsla,
    pub underline: Option<UnderlineStyle>,
}

#[derive(Clone, Default, Debug, Deref, DerefMut)]
pub struct ShapedLine {
    #[deref]
    #[deref_mut]
    pub(crate) layout: Arc<LineLayout>,
    pub text: SharedString,
    pub(crate) decoration_runs: SmallVec<[DecorationRun; 32]>,
}

impl ShapedLine {
    pub fn len(&self) -> usize {
        self.layout.len
    }

    pub fn paint(
        &self,
        origin: Point<Pixels>,
        line_height: Pixels,
        cx: &mut WindowContext,
    ) -> Result<()> {
        paint_line(
            origin,
            &self.layout,
            line_height,
            &self.decoration_runs,
            None,
            &[],
            cx,
        )?;

        Ok(())
    }
}

#[derive(Clone, Default, Debug, Deref, DerefMut)]
pub struct WrappedLine {
    #[deref]
    #[deref_mut]
    pub(crate) layout: Arc<WrappedLineLayout>,
    pub text: SharedString,
    pub(crate) decoration_runs: SmallVec<[DecorationRun; 32]>,
}

impl WrappedLine {
    pub fn len(&self) -> usize {
        self.layout.len()
    }

    pub fn paint(
        &self,
        origin: Point<Pixels>,
        line_height: Pixels,
        cx: &mut WindowContext,
    ) -> Result<()> {
        paint_line(
            origin,
            &self.layout.unwrapped_layout,
            line_height,
            &self.decoration_runs,
            self.wrap_width,
            &self.wrap_boundaries,
            cx,
        )?;

        Ok(())
    }
}

fn paint_line(
    origin: Point<Pixels>,
    layout: &LineLayout,
    line_height: Pixels,
    decoration_runs: &[DecorationRun],
    wrap_width: Option<Pixels>,
    wrap_boundaries: &[WrapBoundary],
    cx: &mut WindowContext<'_>,
) -> Result<()> {
    let padding_top = (line_height - layout.ascent - layout.descent) / 2.;
    let baseline_offset = point(px(0.), padding_top + layout.ascent);
    let mut decoration_runs = decoration_runs.iter();
    let mut wraps = wrap_boundaries.iter().peekable();
    let mut run_end = 0;
    let mut color = black();
    let mut current_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
    let text_system = cx.text_system().clone();
    let mut glyph_origin = origin;
    let mut prev_glyph_position = Point::default();
    for (run_ix, run) in layout.runs.iter().enumerate() {
        let max_glyph_size = text_system
            .bounding_box(run.font_id, layout.font_size)?
            .size;

        for (glyph_ix, glyph) in run.glyphs.iter().enumerate() {
            glyph_origin.x += glyph.position.x - prev_glyph_position.x;

            if wraps.peek() == Some(&&WrapBoundary { run_ix, glyph_ix }) {
                wraps.next();
                if let Some((underline_origin, underline_style)) = current_underline.take() {
                    cx.paint_underline(
                        underline_origin,
                        glyph_origin.x - underline_origin.x,
                        &underline_style,
                    )?;
                }

                glyph_origin.x = origin.x;
                glyph_origin.y += line_height;
            }
            prev_glyph_position = glyph.position;

            let mut finished_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
            if glyph.index >= run_end {
                if let Some(style_run) = decoration_runs.next() {
                    if let Some((_, underline_style)) = &mut current_underline {
                        if style_run.underline.as_ref() != Some(underline_style) {
                            finished_underline = current_underline.take();
                        }
                    }
                    if let Some(run_underline) = style_run.underline.as_ref() {
                        current_underline.get_or_insert((
                            point(
                                glyph_origin.x,
                                origin.y + baseline_offset.y + (layout.descent * 0.618),
                            ),
                            UnderlineStyle {
                                color: Some(run_underline.color.unwrap_or(style_run.color)),
                                thickness: run_underline.thickness,
                                wavy: run_underline.wavy,
                            },
                        ));
                    }

                    run_end += style_run.len as usize;
                    color = style_run.color;
                } else {
                    run_end = layout.len;
                    finished_underline = current_underline.take();
                }
            }

            if let Some((underline_origin, underline_style)) = finished_underline {
                cx.paint_underline(
                    underline_origin,
                    glyph_origin.x - underline_origin.x,
                    &underline_style,
                )?;
            }

            let max_glyph_bounds = Bounds {
                origin: glyph_origin,
                size: max_glyph_size,
            };

            let content_mask = cx.content_mask();
            if max_glyph_bounds.intersects(&content_mask.bounds) {
                if glyph.is_emoji {
                    cx.paint_emoji(
                        glyph_origin + baseline_offset,
                        run.font_id,
                        glyph.id,
                        layout.font_size,
                    )?;
                } else {
                    cx.paint_glyph(
                        glyph_origin + baseline_offset,
                        run.font_id,
                        glyph.id,
                        layout.font_size,
                        color,
                    )?;
                }
            }
        }
    }

    if let Some((underline_start, underline_style)) = current_underline.take() {
        let line_end_x = origin.x + wrap_width.unwrap_or(Pixels::MAX).min(layout.width);
        cx.paint_underline(
            underline_start,
            line_end_x - underline_start.x,
            &underline_style,
        )?;
    }

    Ok(())
}