iced_graphics/text/
paragraph.rs

1//! Draw paragraphs.
2use crate::core;
3use crate::core::alignment;
4use crate::core::text::{Alignment, Hit, Shaping, Span, Text, Wrapping};
5use crate::core::{Font, Point, Rectangle, Size};
6use crate::text;
7
8use std::fmt;
9use std::sync::{self, Arc};
10
11/// A bunch of text.
12#[derive(Clone, PartialEq)]
13pub struct Paragraph(Arc<Internal>);
14
15#[derive(Clone)]
16struct Internal {
17    buffer: cosmic_text::Buffer,
18    font: Font,
19    shaping: Shaping,
20    wrapping: Wrapping,
21    align_x: Alignment,
22    align_y: alignment::Vertical,
23    bounds: Size,
24    min_bounds: Size,
25    version: text::Version,
26}
27
28impl Paragraph {
29    /// Creates a new empty [`Paragraph`].
30    pub fn new() -> Self {
31        Self::default()
32    }
33
34    /// Returns the buffer of the [`Paragraph`].
35    pub fn buffer(&self) -> &cosmic_text::Buffer {
36        &self.internal().buffer
37    }
38
39    /// Creates a [`Weak`] reference to the [`Paragraph`].
40    ///
41    /// This is useful to avoid cloning the [`Paragraph`] when
42    /// referential guarantees are unnecessary. For instance,
43    /// when creating a rendering tree.
44    pub fn downgrade(&self) -> Weak {
45        let paragraph = self.internal();
46
47        Weak {
48            raw: Arc::downgrade(paragraph),
49            min_bounds: paragraph.min_bounds,
50            align_x: paragraph.align_x,
51            align_y: paragraph.align_y,
52        }
53    }
54
55    fn internal(&self) -> &Arc<Internal> {
56        &self.0
57    }
58}
59
60impl core::text::Paragraph for Paragraph {
61    type Font = Font;
62
63    fn with_text(text: Text<&str>) -> Self {
64        log::trace!("Allocating plain paragraph: {}", text.content);
65
66        let mut font_system =
67            text::font_system().write().expect("Write font system");
68
69        let mut buffer = cosmic_text::Buffer::new(
70            font_system.raw(),
71            cosmic_text::Metrics::new(
72                text.size.into(),
73                text.line_height.to_absolute(text.size).into(),
74            ),
75        );
76
77        buffer.set_size(
78            font_system.raw(),
79            Some(text.bounds.width),
80            Some(text.bounds.height),
81        );
82
83        buffer.set_wrap(font_system.raw(), text::to_wrap(text.wrapping));
84
85        buffer.set_text(
86            font_system.raw(),
87            text.content,
88            text::to_attributes(text.font),
89            text::to_shaping(text.shaping),
90        );
91
92        let min_bounds = align(&mut buffer, &mut font_system, text.align_x);
93
94        Self(Arc::new(Internal {
95            buffer,
96            font: text.font,
97            align_x: text.align_x,
98            align_y: text.align_y,
99            shaping: text.shaping,
100            wrapping: text.wrapping,
101            bounds: text.bounds,
102            min_bounds,
103            version: font_system.version(),
104        }))
105    }
106
107    fn with_spans<Link>(text: Text<&[Span<'_, Link>]>) -> Self {
108        log::trace!("Allocating rich paragraph: {} spans", text.content.len());
109
110        let mut font_system =
111            text::font_system().write().expect("Write font system");
112
113        let mut buffer = cosmic_text::Buffer::new(
114            font_system.raw(),
115            cosmic_text::Metrics::new(
116                text.size.into(),
117                text.line_height.to_absolute(text.size).into(),
118            ),
119        );
120
121        buffer.set_size(
122            font_system.raw(),
123            Some(text.bounds.width),
124            Some(text.bounds.height),
125        );
126
127        buffer.set_wrap(font_system.raw(), text::to_wrap(text.wrapping));
128
129        buffer.set_rich_text(
130            font_system.raw(),
131            text.content.iter().enumerate().map(|(i, span)| {
132                let attrs = text::to_attributes(span.font.unwrap_or(text.font));
133
134                let attrs = match (span.size, span.line_height) {
135                    (None, None) => attrs,
136                    _ => {
137                        let size = span.size.unwrap_or(text.size);
138
139                        attrs.metrics(cosmic_text::Metrics::new(
140                            size.into(),
141                            span.line_height
142                                .unwrap_or(text.line_height)
143                                .to_absolute(size)
144                                .into(),
145                        ))
146                    }
147                };
148
149                let attrs = if let Some(color) = span.color {
150                    attrs.color(text::to_color(color))
151                } else {
152                    attrs
153                };
154
155                (span.text.as_ref(), attrs.metadata(i))
156            }),
157            text::to_attributes(text.font),
158            text::to_shaping(text.shaping),
159            None,
160        );
161
162        let min_bounds = align(&mut buffer, &mut font_system, text.align_x);
163
164        Self(Arc::new(Internal {
165            buffer,
166            font: text.font,
167            align_x: text.align_x,
168            align_y: text.align_y,
169            shaping: text.shaping,
170            wrapping: text.wrapping,
171            bounds: text.bounds,
172            min_bounds,
173            version: font_system.version(),
174        }))
175    }
176
177    fn resize(&mut self, new_bounds: Size) {
178        let paragraph = Arc::make_mut(&mut self.0);
179
180        let mut font_system =
181            text::font_system().write().expect("Write font system");
182
183        paragraph.buffer.set_size(
184            font_system.raw(),
185            Some(new_bounds.width),
186            Some(new_bounds.height),
187        );
188
189        let (min_bounds, _has_rtl) = text::measure(&paragraph.buffer);
190
191        paragraph.bounds = new_bounds;
192        paragraph.min_bounds = min_bounds;
193    }
194
195    fn compare(&self, text: Text<()>) -> core::text::Difference {
196        let font_system = text::font_system().read().expect("Read font system");
197        let paragraph = self.internal();
198        let metrics = paragraph.buffer.metrics();
199
200        if paragraph.version != font_system.version
201            || metrics.font_size != text.size.0
202            || metrics.line_height != text.line_height.to_absolute(text.size).0
203            || paragraph.font != text.font
204            || paragraph.shaping != text.shaping
205            || paragraph.wrapping != text.wrapping
206            || paragraph.align_x != text.align_x
207            || paragraph.align_y != text.align_y
208        {
209            core::text::Difference::Shape
210        } else if paragraph.bounds != text.bounds {
211            core::text::Difference::Bounds
212        } else {
213            core::text::Difference::None
214        }
215    }
216
217    fn align_x(&self) -> Alignment {
218        self.internal().align_x
219    }
220
221    fn align_y(&self) -> alignment::Vertical {
222        self.internal().align_y
223    }
224
225    fn min_bounds(&self) -> Size {
226        self.internal().min_bounds
227    }
228
229    fn hit_test(&self, point: Point) -> Option<Hit> {
230        let cursor = self.internal().buffer.hit(point.x, point.y)?;
231
232        Some(Hit::CharOffset(cursor.index))
233    }
234
235    fn hit_span(&self, point: Point) -> Option<usize> {
236        let internal = self.internal();
237
238        let cursor = internal.buffer.hit(point.x, point.y)?;
239        let line = internal.buffer.lines.get(cursor.line)?;
240
241        let mut last_glyph = None;
242        let mut glyphs = line
243            .layout_opt()
244            .as_ref()?
245            .iter()
246            .flat_map(|line| line.glyphs.iter())
247            .peekable();
248
249        while let Some(glyph) = glyphs.peek() {
250            if glyph.start <= cursor.index && cursor.index < glyph.end {
251                break;
252            }
253
254            last_glyph = glyphs.next();
255        }
256
257        let glyph = match cursor.affinity {
258            cosmic_text::Affinity::Before => last_glyph,
259            cosmic_text::Affinity::After => glyphs.next(),
260        }?;
261
262        Some(glyph.metadata)
263    }
264
265    fn span_bounds(&self, index: usize) -> Vec<Rectangle> {
266        let internal = self.internal();
267
268        let mut bounds = Vec::new();
269        let mut current_bounds = None;
270
271        let glyphs = internal
272            .buffer
273            .layout_runs()
274            .flat_map(|run| {
275                let line_top = run.line_top;
276                let line_height = run.line_height;
277
278                run.glyphs
279                    .iter()
280                    .map(move |glyph| (line_top, line_height, glyph))
281            })
282            .skip_while(|(_, _, glyph)| glyph.metadata != index)
283            .take_while(|(_, _, glyph)| glyph.metadata == index);
284
285        for (line_top, line_height, glyph) in glyphs {
286            let y = line_top + glyph.y;
287
288            let new_bounds = || {
289                Rectangle::new(
290                    Point::new(glyph.x, y),
291                    Size::new(
292                        glyph.w,
293                        glyph.line_height_opt.unwrap_or(line_height),
294                    ),
295                )
296            };
297
298            match current_bounds.as_mut() {
299                None => {
300                    current_bounds = Some(new_bounds());
301                }
302                Some(current_bounds) if y != current_bounds.y => {
303                    bounds.push(*current_bounds);
304                    *current_bounds = new_bounds();
305                }
306                Some(current_bounds) => {
307                    current_bounds.width += glyph.w;
308                }
309            }
310        }
311
312        bounds.extend(current_bounds);
313        bounds
314    }
315
316    fn grapheme_position(&self, line: usize, index: usize) -> Option<Point> {
317        use unicode_segmentation::UnicodeSegmentation;
318
319        let run = self.internal().buffer.layout_runs().nth(line)?;
320
321        // index represents a grapheme, not a glyph
322        // Let's find the first glyph for the given grapheme cluster
323        let mut last_start = None;
324        let mut last_grapheme_count = 0;
325        let mut graphemes_seen = 0;
326
327        let glyph = run
328            .glyphs
329            .iter()
330            .find(|glyph| {
331                if Some(glyph.start) != last_start {
332                    last_grapheme_count = run.text[glyph.start..glyph.end]
333                        .graphemes(false)
334                        .count();
335                    last_start = Some(glyph.start);
336                    graphemes_seen += last_grapheme_count;
337                }
338
339                graphemes_seen >= index
340            })
341            .or_else(|| run.glyphs.last())?;
342
343        let advance = if index == 0 {
344            0.0
345        } else {
346            glyph.w
347                * (1.0
348                    - graphemes_seen.saturating_sub(index) as f32
349                        / last_grapheme_count.max(1) as f32)
350        };
351
352        Some(Point::new(
353            glyph.x + glyph.x_offset * glyph.font_size + advance,
354            glyph.y - glyph.y_offset * glyph.font_size,
355        ))
356    }
357}
358
359fn align(
360    buffer: &mut cosmic_text::Buffer,
361    font_system: &mut text::FontSystem,
362    alignment: Alignment,
363) -> Size {
364    let (min_bounds, has_rtl) = text::measure(buffer);
365    let mut needs_relayout = has_rtl;
366
367    if let Some(align) = text::to_align(alignment) {
368        let has_multiple_lines = buffer.lines.len() > 1
369            || buffer.lines.first().is_some_and(|line| {
370                line.layout_opt().is_some_and(|layout| layout.len() > 1)
371            });
372
373        if has_multiple_lines {
374            for line in &mut buffer.lines {
375                let _ = line.set_align(Some(align));
376            }
377
378            needs_relayout = true;
379        }
380    }
381
382    // TODO: Avoid relayout with some changes to `cosmic-text` (?)
383    if needs_relayout {
384        log::trace!("Relayouting paragraph...");
385
386        buffer.set_size(
387            font_system.raw(),
388            Some(min_bounds.width),
389            Some(min_bounds.height),
390        );
391    }
392
393    min_bounds
394}
395
396impl Default for Paragraph {
397    fn default() -> Self {
398        Self(Arc::new(Internal::default()))
399    }
400}
401
402impl fmt::Debug for Paragraph {
403    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
404        let paragraph = self.internal();
405
406        f.debug_struct("Paragraph")
407            .field("font", &paragraph.font)
408            .field("shaping", &paragraph.shaping)
409            .field("horizontal_alignment", &paragraph.align_x)
410            .field("vertical_alignment", &paragraph.align_y)
411            .field("bounds", &paragraph.bounds)
412            .field("min_bounds", &paragraph.min_bounds)
413            .finish()
414    }
415}
416
417impl PartialEq for Internal {
418    fn eq(&self, other: &Self) -> bool {
419        self.font == other.font
420            && self.shaping == other.shaping
421            && self.align_x == other.align_x
422            && self.align_y == other.align_y
423            && self.bounds == other.bounds
424            && self.min_bounds == other.min_bounds
425            && self.buffer.metrics() == other.buffer.metrics()
426    }
427}
428
429impl Default for Internal {
430    fn default() -> Self {
431        Self {
432            buffer: cosmic_text::Buffer::new_empty(cosmic_text::Metrics {
433                font_size: 1.0,
434                line_height: 1.0,
435            }),
436            font: Font::default(),
437            shaping: Shaping::default(),
438            wrapping: Wrapping::default(),
439            align_x: Alignment::Default,
440            align_y: alignment::Vertical::Top,
441            bounds: Size::ZERO,
442            min_bounds: Size::ZERO,
443            version: text::Version::default(),
444        }
445    }
446}
447
448/// A weak reference to a [`Paragraph`].
449#[derive(Debug, Clone)]
450pub struct Weak {
451    raw: sync::Weak<Internal>,
452    /// The minimum bounds of the [`Paragraph`].
453    pub min_bounds: Size,
454    /// The horizontal alignment of the [`Paragraph`].
455    pub align_x: Alignment,
456    /// The vertical alignment of the [`Paragraph`].
457    pub align_y: alignment::Vertical,
458}
459
460impl Weak {
461    /// Tries to update the reference into a [`Paragraph`].
462    pub fn upgrade(&self) -> Option<Paragraph> {
463        self.raw.upgrade().map(Paragraph)
464    }
465}
466
467impl PartialEq for Weak {
468    fn eq(&self, other: &Self) -> bool {
469        match (self.raw.upgrade(), other.raw.upgrade()) {
470            (Some(p1), Some(p2)) => p1 == p2,
471            _ => false,
472        }
473    }
474}