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
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((background_origin, background_color)) = current_background.as_mut() {
114 cx.paint_quad(
115 Bounds {
116 origin: *background_origin,
117 size: size(glyph_origin.x - background_origin.x, line_height),
118 },
119 Corners::default(),
120 *background_color,
121 Edges::default(),
122 transparent_black(),
123 );
124 background_origin.x = origin.x;
125 background_origin.y += line_height;
126 }
127 if let Some((underline_origin, underline_style)) = current_underline.as_mut() {
128 cx.paint_underline(
129 *underline_origin,
130 glyph_origin.x - underline_origin.x,
131 underline_style,
132 );
133 underline_origin.x = origin.x;
134 underline_origin.y += line_height;
135 }
136
137 glyph_origin.x = origin.x;
138 glyph_origin.y += line_height;
139 }
140 prev_glyph_position = glyph.position;
141
142 let mut finished_background: Option<(Point<Pixels>, Hsla)> = None;
143 let mut finished_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
144 if glyph.index >= run_end {
145 if let Some(style_run) = decoration_runs.next() {
146 if let Some((_, background_color)) = &mut current_background {
147 if style_run.background_color.as_ref() != Some(background_color) {
148 finished_background = current_background.take();
149 }
150 }
151 if let Some(run_background) = style_run.background_color {
152 current_background
153 .get_or_insert((point(glyph_origin.x, glyph_origin.y), run_background));
154 }
155
156 if let Some((_, underline_style)) = &mut current_underline {
157 if style_run.underline.as_ref() != Some(underline_style) {
158 finished_underline = current_underline.take();
159 }
160 }
161 if let Some(run_underline) = style_run.underline.as_ref() {
162 current_underline.get_or_insert((
163 point(
164 glyph_origin.x,
165 glyph_origin.y + baseline_offset.y + (layout.descent * 0.618),
166 ),
167 UnderlineStyle {
168 color: Some(run_underline.color.unwrap_or(style_run.color)),
169 thickness: run_underline.thickness,
170 wavy: run_underline.wavy,
171 },
172 ));
173 }
174
175 run_end += style_run.len as usize;
176 color = style_run.color;
177 } else {
178 run_end = layout.len;
179 finished_background = current_background.take();
180 finished_underline = current_underline.take();
181 }
182 }
183
184 if let Some((background_origin, background_color)) = finished_background {
185 cx.paint_quad(
186 Bounds {
187 origin: background_origin,
188 size: size(glyph_origin.x - background_origin.x, line_height),
189 },
190 Corners::default(),
191 background_color,
192 Edges::default(),
193 transparent_black(),
194 );
195 }
196
197 if let Some((underline_origin, underline_style)) = finished_underline {
198 cx.paint_underline(
199 underline_origin,
200 glyph_origin.x - underline_origin.x,
201 &underline_style,
202 );
203 }
204
205 let max_glyph_bounds = Bounds {
206 origin: glyph_origin,
207 size: max_glyph_size,
208 };
209
210 let content_mask = cx.content_mask();
211 if max_glyph_bounds.intersects(&content_mask.bounds) {
212 if glyph.is_emoji {
213 cx.paint_emoji(
214 glyph_origin + baseline_offset,
215 run.font_id,
216 glyph.id,
217 layout.font_size,
218 )?;
219 } else {
220 cx.paint_glyph(
221 glyph_origin + baseline_offset,
222 run.font_id,
223 glyph.id,
224 layout.font_size,
225 color,
226 )?;
227 }
228 }
229 }
230 }
231
232 let mut last_line_end_x = origin.x + layout.width;
233 if let Some(boundary) = wrap_boundaries.last() {
234 let run = &layout.runs[boundary.run_ix];
235 let glyph = &run.glyphs[boundary.glyph_ix];
236 last_line_end_x -= glyph.position.x;
237 }
238
239 if let Some((background_origin, background_color)) = current_background.take() {
240 cx.paint_quad(
241 Bounds {
242 origin: background_origin,
243 size: size(last_line_end_x - background_origin.x, line_height),
244 },
245 Corners::default(),
246 background_color,
247 Edges::default(),
248 transparent_black(),
249 );
250 }
251
252 if let Some((underline_start, underline_style)) = current_underline.take() {
253 cx.paint_underline(
254 underline_start,
255 last_line_end_x - underline_start.x,
256 &underline_style,
257 );
258 }
259
260 Ok(())
261}