1use crate::{
2 black, fill, point, px, size, Bounds, ElementContext, Hsla, LineLayout, Pixels, Point, Result,
3 SharedString, StrikethroughStyle, UnderlineStyle, 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 ElementContext,
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 ElementContext,
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 ElementContext<'_>,
111) -> Result<()> {
112 let padding_top = (line_height - layout.ascent - layout.descent) / 2.;
113 let baseline_offset = point(px(0.), padding_top + layout.ascent);
114 let mut decoration_runs = decoration_runs.iter();
115 let mut wraps = wrap_boundaries.iter().peekable();
116 let mut run_end = 0;
117 let mut color = black();
118 let mut current_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
119 let mut current_strikethrough: Option<(Point<Pixels>, StrikethroughStyle)> = None;
120 let mut current_background: Option<(Point<Pixels>, Hsla)> = None;
121 let text_system = cx.text_system().clone();
122 let mut glyph_origin = origin;
123 let mut prev_glyph_position = Point::default();
124 for (run_ix, run) in layout.runs.iter().enumerate() {
125 let max_glyph_size = text_system.bounding_box(run.font_id, layout.font_size).size;
126
127 for (glyph_ix, glyph) in run.glyphs.iter().enumerate() {
128 glyph_origin.x += glyph.position.x - prev_glyph_position.x;
129
130 if wraps.peek() == Some(&&WrapBoundary { run_ix, glyph_ix }) {
131 wraps.next();
132 if let Some((background_origin, background_color)) = current_background.as_mut() {
133 cx.paint_quad(fill(
134 Bounds {
135 origin: *background_origin,
136 size: size(glyph_origin.x - background_origin.x, line_height),
137 },
138 *background_color,
139 ));
140 background_origin.x = origin.x;
141 background_origin.y += line_height;
142 }
143 if let Some((underline_origin, underline_style)) = current_underline.as_mut() {
144 cx.paint_underline(
145 *underline_origin,
146 glyph_origin.x - underline_origin.x,
147 underline_style,
148 );
149 underline_origin.x = origin.x;
150 underline_origin.y += line_height;
151 }
152 if let Some((strikethrough_origin, strikethrough_style)) =
153 current_strikethrough.as_mut()
154 {
155 cx.paint_strikethrough(
156 *strikethrough_origin,
157 glyph_origin.x - strikethrough_origin.x,
158 strikethrough_style,
159 );
160 strikethrough_origin.x = origin.x;
161 strikethrough_origin.y += line_height;
162 }
163
164 glyph_origin.x = origin.x;
165 glyph_origin.y += line_height;
166 }
167 prev_glyph_position = glyph.position;
168
169 let mut finished_background: Option<(Point<Pixels>, Hsla)> = None;
170 let mut finished_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
171 let mut finished_strikethrough: Option<(Point<Pixels>, StrikethroughStyle)> = None;
172 if glyph.index >= run_end {
173 if let Some(style_run) = decoration_runs.next() {
174 if let Some((_, background_color)) = &mut current_background {
175 if style_run.background_color.as_ref() != Some(background_color) {
176 finished_background = current_background.take();
177 }
178 }
179 if let Some(run_background) = style_run.background_color {
180 current_background
181 .get_or_insert((point(glyph_origin.x, glyph_origin.y), run_background));
182 }
183
184 if let Some((_, underline_style)) = &mut current_underline {
185 if style_run.underline.as_ref() != Some(underline_style) {
186 finished_underline = current_underline.take();
187 }
188 }
189 if let Some(run_underline) = style_run.underline.as_ref() {
190 current_underline.get_or_insert((
191 point(
192 glyph_origin.x,
193 glyph_origin.y + baseline_offset.y + (layout.descent * 0.618),
194 ),
195 UnderlineStyle {
196 color: Some(run_underline.color.unwrap_or(style_run.color)),
197 thickness: run_underline.thickness,
198 wavy: run_underline.wavy,
199 },
200 ));
201 }
202 if let Some((_, strikethrough_style)) = &mut current_strikethrough {
203 if style_run.strikethrough.as_ref() != Some(strikethrough_style) {
204 finished_strikethrough = current_strikethrough.take();
205 }
206 }
207 if let Some(run_strikethrough) = style_run.strikethrough.as_ref() {
208 current_strikethrough.get_or_insert((
209 point(
210 glyph_origin.x,
211 glyph_origin.y
212 + (((layout.ascent * 0.5) + baseline_offset.y) * 0.5),
213 ),
214 StrikethroughStyle {
215 color: Some(run_strikethrough.color.unwrap_or(style_run.color)),
216 thickness: run_strikethrough.thickness,
217 },
218 ));
219 }
220
221 run_end += style_run.len as usize;
222 color = style_run.color;
223 } else {
224 run_end = layout.len;
225 finished_background = current_background.take();
226 finished_underline = current_underline.take();
227 finished_strikethrough = current_strikethrough.take();
228 }
229 }
230
231 if let Some((background_origin, background_color)) = finished_background {
232 cx.paint_quad(fill(
233 Bounds {
234 origin: background_origin,
235 size: size(glyph_origin.x - background_origin.x, line_height),
236 },
237 background_color,
238 ));
239 }
240
241 if let Some((underline_origin, underline_style)) = finished_underline {
242 cx.paint_underline(
243 underline_origin,
244 glyph_origin.x - underline_origin.x,
245 &underline_style,
246 );
247 }
248
249 if let Some((strikethrough_origin, strikethrough_style)) = finished_strikethrough {
250 cx.paint_strikethrough(
251 strikethrough_origin,
252 glyph_origin.x - strikethrough_origin.x,
253 &strikethrough_style,
254 );
255 }
256
257 let max_glyph_bounds = Bounds {
258 origin: glyph_origin,
259 size: max_glyph_size,
260 };
261
262 let content_mask = cx.content_mask();
263 if max_glyph_bounds.intersects(&content_mask.bounds) {
264 if glyph.is_emoji {
265 cx.paint_emoji(
266 glyph_origin + baseline_offset,
267 run.font_id,
268 glyph.id,
269 layout.font_size,
270 )?;
271 } else {
272 cx.paint_glyph(
273 glyph_origin + baseline_offset,
274 run.font_id,
275 glyph.id,
276 layout.font_size,
277 color,
278 )?;
279 }
280 }
281 }
282 }
283
284 let mut last_line_end_x = origin.x + layout.width;
285 if let Some(boundary) = wrap_boundaries.last() {
286 let run = &layout.runs[boundary.run_ix];
287 let glyph = &run.glyphs[boundary.glyph_ix];
288 last_line_end_x -= glyph.position.x;
289 }
290
291 if let Some((background_origin, background_color)) = current_background.take() {
292 cx.paint_quad(fill(
293 Bounds {
294 origin: background_origin,
295 size: size(last_line_end_x - background_origin.x, line_height),
296 },
297 background_color,
298 ));
299 }
300
301 if let Some((underline_start, underline_style)) = current_underline.take() {
302 cx.paint_underline(
303 underline_start,
304 last_line_end_x - underline_start.x,
305 &underline_style,
306 );
307 }
308
309 if let Some((strikethrough_start, strikethrough_style)) = current_strikethrough.take() {
310 cx.paint_strikethrough(
311 strikethrough_start,
312 last_line_end_x - strikethrough_start.x,
313 &strikethrough_style,
314 );
315 }
316
317 Ok(())
318}