Skip to main content

iced_core/text/
paragraph.rs

1//! Draw paragraphs.
2use crate::alignment;
3use crate::text::{
4    Alignment, Difference, Ellipsis, Hit, LineHeight, Shaping, Span, Text, Wrapping,
5};
6use crate::{Pixels, Point, Rectangle, Size};
7
8/// A text paragraph.
9pub trait Paragraph: Sized + Default {
10    /// The font of this [`Paragraph`].
11    type Font: Copy + PartialEq;
12
13    /// Creates a new [`Paragraph`] laid out with the given [`Text`].
14    fn with_text(text: Text<&str, Self::Font>) -> Self;
15
16    /// Creates a new [`Paragraph`] laid out with the given [`Text`].
17    fn with_spans<Link>(text: Text<&[Span<'_, Link, Self::Font>], Self::Font>) -> Self;
18
19    /// Lays out the [`Paragraph`] with some new boundaries.
20    fn resize(&mut self, new_bounds: Size);
21
22    /// Compares the [`Paragraph`] with some desired [`Text`] and returns the
23    /// [`Difference`].
24    fn compare(&self, text: Text<(), Self::Font>) -> Difference;
25
26    /// Returns the text size of the [`Paragraph`] in [`Pixels`].
27    fn size(&self) -> Pixels;
28
29    /// Returns the hint factor of the [`Paragraph`].
30    fn hint_factor(&self) -> Option<f32>;
31
32    /// Returns the font of the [`Paragraph`].
33    fn font(&self) -> Self::Font;
34
35    /// Returns the [`LineHeight`] of the [`Paragraph`].
36    fn line_height(&self) -> LineHeight;
37
38    /// Returns the horizontal alignment of the [`Paragraph`].
39    fn align_x(&self) -> Alignment;
40
41    /// Returns the vertical alignment of the [`Paragraph`].
42    fn align_y(&self) -> alignment::Vertical;
43
44    /// Returns the [`Wrapping`] strategy of the [`Paragraph`]>
45    fn wrapping(&self) -> Wrapping;
46
47    /// Returns the [`Ellipsis`] strategy of the [`Paragraph`]>
48    fn ellipsis(&self) -> Ellipsis;
49
50    /// Returns the [`Shaping`] strategy of the [`Paragraph`]>
51    fn shaping(&self) -> Shaping;
52
53    /// Returns the available bounds used to layout the [`Paragraph`].
54    fn bounds(&self) -> Size;
55
56    /// Returns the minimum boundaries that can fit the contents of the
57    /// [`Paragraph`].
58    fn min_bounds(&self) -> Size;
59
60    /// Tests whether the provided point is within the boundaries of the
61    /// [`Paragraph`], returning information about the nearest character.
62    fn hit_test(&self, point: Point) -> Option<Hit>;
63
64    /// Tests whether the provided point is within the boundaries of a
65    /// [`Span`] in the [`Paragraph`], returning the index of the [`Span`]
66    /// that was hit.
67    fn hit_span(&self, point: Point) -> Option<usize>;
68
69    /// Returns all bounds for the provided [`Span`] index of the [`Paragraph`].
70    /// A [`Span`] can have multiple bounds for each line it's on.
71    fn span_bounds(&self, index: usize) -> Vec<Rectangle>;
72
73    /// Returns the distance to the given grapheme index in the [`Paragraph`].
74    fn grapheme_position(&self, line: usize, index: usize) -> Option<Point>;
75
76    /// Returns the minimum width that can fit the contents of the [`Paragraph`].
77    fn min_width(&self) -> f32 {
78        self.min_bounds().width
79    }
80
81    /// Returns the minimum height that can fit the contents of the [`Paragraph`].
82    fn min_height(&self) -> f32 {
83        self.min_bounds().height
84    }
85}
86
87/// A [`Paragraph`] of plain text.
88#[derive(Debug, Clone, Default)]
89pub struct Plain<P: Paragraph> {
90    raw: P,
91    content: String,
92}
93
94impl<P: Paragraph> Plain<P> {
95    /// Creates a new [`Plain`] paragraph.
96    pub fn new(text: Text<String, P::Font>) -> Self {
97        Self {
98            raw: P::with_text(text.as_ref()),
99            content: text.content,
100        }
101    }
102
103    /// Updates the plain [`Paragraph`] to match the given [`Text`], if needed.
104    ///
105    /// Returns true if the [`Paragraph`] changed.
106    pub fn update(&mut self, text: Text<&str, P::Font>) -> bool {
107        if self.content != text.content {
108            text.content.clone_into(&mut self.content);
109            self.raw = P::with_text(text);
110            return true;
111        }
112
113        match self.raw.compare(text.with_content(())) {
114            Difference::None => false,
115            Difference::Bounds => {
116                self.raw.resize(text.bounds);
117                true
118            }
119            Difference::Shape => {
120                self.raw = P::with_text(text);
121                true
122            }
123        }
124    }
125
126    /// Returns the horizontal alignment of the [`Paragraph`].
127    pub fn align_x(&self) -> Alignment {
128        self.raw.align_x()
129    }
130
131    /// Returns the vertical alignment of the [`Paragraph`].
132    pub fn align_y(&self) -> alignment::Vertical {
133        self.raw.align_y()
134    }
135
136    /// Returns the minimum boundaries that can fit the contents of the
137    /// [`Paragraph`].
138    pub fn min_bounds(&self) -> Size {
139        self.raw.min_bounds()
140    }
141
142    /// Returns the minimum width that can fit the contents of the
143    /// [`Paragraph`].
144    pub fn min_width(&self) -> f32 {
145        self.raw.min_width()
146    }
147
148    /// Returns the minimum height that can fit the contents of the
149    /// [`Paragraph`].
150    pub fn min_height(&self) -> f32 {
151        self.raw.min_height()
152    }
153
154    /// Returns the cached [`Paragraph`].
155    pub fn raw(&self) -> &P {
156        &self.raw
157    }
158
159    /// Returns the current content of the plain [`Paragraph`].
160    pub fn content(&self) -> &str {
161        &self.content
162    }
163
164    /// Returns the [`Paragraph`] as a [`Text`] definition.
165    pub fn as_text(&self) -> Text<&str, P::Font> {
166        Text {
167            content: &self.content,
168            bounds: self.raw.bounds(),
169            size: self.raw.size(),
170            line_height: self.raw.line_height(),
171            font: self.raw.font(),
172            align_x: self.raw.align_x(),
173            align_y: self.raw.align_y(),
174            shaping: self.raw.shaping(),
175            wrapping: self.raw.wrapping(),
176            ellipsis: self.raw.ellipsis(),
177            hint_factor: self.raw.hint_factor(),
178        }
179    }
180}