1use crate::{
2 black, fill, point, px, size, Bounds, ElementContext, Hsla, LineLayout, Pixels, Point, Result,
3 SharedString, UnderlineStyle, 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 background_color: Option<Hsla>,
14 pub underline: Option<UnderlineStyle>,
15}
16
17#[derive(Clone, Default, Debug, Deref, DerefMut)]
18pub struct ShapedLine {
19 #[deref]
20 #[deref_mut]
21 pub(crate) layout: Arc<LineLayout>,
22 pub text: SharedString,
23 pub(crate) decoration_runs: SmallVec<[DecorationRun; 32]>,
24}
25
26impl ShapedLine {
27 /// The length of the line in utf-8 bytes.
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 ElementContext,
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 ElementContext,
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 ElementContext<'_>,
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(fill(
113 Bounds {
114 origin: *background_origin,
115 size: size(glyph_origin.x - background_origin.x, line_height),
116 },
117 *background_color,
118 ));
119 background_origin.x = origin.x;
120 background_origin.y += line_height;
121 }
122 if let Some((underline_origin, underline_style)) = current_underline.as_mut() {
123 cx.paint_underline(
124 *underline_origin,
125 glyph_origin.x - underline_origin.x,
126 underline_style,
127 );
128 underline_origin.x = origin.x;
129 underline_origin.y += line_height;
130 }
131
132 glyph_origin.x = origin.x;
133 glyph_origin.y += line_height;
134 }
135 prev_glyph_position = glyph.position;
136
137 let mut finished_background: Option<(Point<Pixels>, Hsla)> = None;
138 let mut finished_underline: Option<(Point<Pixels>, UnderlineStyle)> = None;
139 if glyph.index >= run_end {
140 if let Some(style_run) = decoration_runs.next() {
141 if let Some((_, background_color)) = &mut current_background {
142 if style_run.background_color.as_ref() != Some(background_color) {
143 finished_background = current_background.take();
144 }
145 }
146 if let Some(run_background) = style_run.background_color {
147 current_background
148 .get_or_insert((point(glyph_origin.x, glyph_origin.y), run_background));
149 }
150
151 if let Some((_, underline_style)) = &mut current_underline {
152 if style_run.underline.as_ref() != Some(underline_style) {
153 finished_underline = current_underline.take();
154 }
155 }
156 if let Some(run_underline) = style_run.underline.as_ref() {
157 current_underline.get_or_insert((
158 point(
159 glyph_origin.x,
160 glyph_origin.y + baseline_offset.y + (layout.descent * 0.618),
161 ),
162 UnderlineStyle {
163 color: Some(run_underline.color.unwrap_or(style_run.color)),
164 thickness: run_underline.thickness,
165 wavy: run_underline.wavy,
166 },
167 ));
168 }
169
170 run_end += style_run.len as usize;
171 color = style_run.color;
172 } else {
173 run_end = layout.len;
174 finished_background = current_background.take();
175 finished_underline = current_underline.take();
176 }
177 }
178
179 if let Some((background_origin, background_color)) = finished_background {
180 cx.paint_quad(fill(
181 Bounds {
182 origin: background_origin,
183 size: size(glyph_origin.x - background_origin.x, line_height),
184 },
185 background_color,
186 ));
187 }
188
189 if let Some((underline_origin, underline_style)) = finished_underline {
190 cx.paint_underline(
191 underline_origin,
192 glyph_origin.x - underline_origin.x,
193 &underline_style,
194 );
195 }
196
197 let max_glyph_bounds = Bounds {
198 origin: glyph_origin,
199 size: max_glyph_size,
200 };
201
202 let content_mask = cx.content_mask();
203 if max_glyph_bounds.intersects(&content_mask.bounds) {
204 if glyph.is_emoji {
205 cx.paint_emoji(
206 glyph_origin + baseline_offset,
207 run.font_id,
208 glyph.id,
209 layout.font_size,
210 )?;
211 } else {
212 cx.paint_glyph(
213 glyph_origin + baseline_offset,
214 run.font_id,
215 glyph.id,
216 layout.font_size,
217 color,
218 )?;
219 }
220 }
221 }
222 }
223
224 let mut last_line_end_x = origin.x + layout.width;
225 if let Some(boundary) = wrap_boundaries.last() {
226 let run = &layout.runs[boundary.run_ix];
227 let glyph = &run.glyphs[boundary.glyph_ix];
228 last_line_end_x -= glyph.position.x;
229 }
230
231 if let Some((background_origin, background_color)) = current_background.take() {
232 cx.paint_quad(fill(
233 Bounds {
234 origin: background_origin,
235 size: size(last_line_end_x - background_origin.x, line_height),
236 },
237 background_color,
238 ));
239 }
240
241 if let Some((underline_start, underline_style)) = current_underline.take() {
242 cx.paint_underline(
243 underline_start,
244 last_line_end_x - underline_start.x,
245 &underline_style,
246 );
247 }
248
249 Ok(())
250}