1//! Draw paragraphs.
2use crate::alignment;
3use crate::text::{Difference, Hit, Span, Text};
4use crate::{Point, Rectangle, Size};
56/// A text paragraph.
7pub trait Paragraph: Sized + Default {
8/// The font of this [`Paragraph`].
9type Font: Copy + PartialEq;
1011/// Creates a new [`Paragraph`] laid out with the given [`Text`].
12fn with_text(text: Text<&str, Self::Font>) -> Self;
1314/// Creates a new [`Paragraph`] laid out with the given [`Text`].
15fn with_spans<Link>(
16 text: Text<&[Span<'_, Link, Self::Font>], Self::Font>,
17 ) -> Self;
1819/// Lays out the [`Paragraph`] with some new boundaries.
20fn resize(&mut self, new_bounds: Size);
2122/// Compares the [`Paragraph`] with some desired [`Text`] and returns the
23 /// [`Difference`].
24fn compare(&self, text: Text<(), Self::Font>) -> Difference;
2526/// Returns the horizontal alignment of the [`Paragraph`].
27fn horizontal_alignment(&self) -> alignment::Horizontal;
2829/// Returns the vertical alignment of the [`Paragraph`].
30fn vertical_alignment(&self) -> alignment::Vertical;
3132/// Returns the minimum boundaries that can fit the contents of the
33 /// [`Paragraph`].
34fn min_bounds(&self) -> Size;
3536/// Tests whether the provided point is within the boundaries of the
37 /// [`Paragraph`], returning information about the nearest character.
38fn hit_test(&self, point: Point) -> Option<Hit>;
3940/// Tests whether the provided point is within the boundaries of a
41 /// [`Span`] in the [`Paragraph`], returning the index of the [`Span`]
42 /// that was hit.
43fn hit_span(&self, point: Point) -> Option<usize>;
4445/// Returns all bounds for the provided [`Span`] index of the [`Paragraph`].
46 /// A [`Span`] can have multiple bounds for each line it's on.
47fn span_bounds(&self, index: usize) -> Vec<Rectangle>;
4849/// Returns the distance to the given grapheme index in the [`Paragraph`].
50fn grapheme_position(&self, line: usize, index: usize) -> Option<Point>;
5152/// Returns the minimum width that can fit the contents of the [`Paragraph`].
53fn min_width(&self) -> f32 {
54self.min_bounds().width
55 }
5657/// Returns the minimum height that can fit the contents of the [`Paragraph`].
58fn min_height(&self) -> f32 {
59self.min_bounds().height
60 }
61}
6263/// A [`Paragraph`] of plain text.
64#[derive(Debug, Clone, Default)]
65pub struct Plain<P: Paragraph> {
66 raw: P,
67 content: String,
68}
6970impl<P: Paragraph> Plain<P> {
71/// Creates a new [`Plain`] paragraph.
72pub fn new(text: Text<&str, P::Font>) -> Self {
73let content = text.content.to_owned();
7475Self {
76 raw: P::with_text(text),
77 content,
78 }
79 }
8081/// Updates the plain [`Paragraph`] to match the given [`Text`], if needed.
82pub fn update(&mut self, text: Text<&str, P::Font>) {
83if self.content != text.content {
84 text.content.clone_into(&mut self.content);
85self.raw = P::with_text(text);
86return;
87 }
8889match self.raw.compare(Text {
90 content: (),
91 bounds: text.bounds,
92 size: text.size,
93 line_height: text.line_height,
94 font: text.font,
95 horizontal_alignment: text.horizontal_alignment,
96 vertical_alignment: text.vertical_alignment,
97 shaping: text.shaping,
98 wrapping: text.wrapping,
99 }) {
100 Difference::None => {}
101 Difference::Bounds => {
102self.raw.resize(text.bounds);
103 }
104 Difference::Shape => {
105self.raw = P::with_text(text);
106 }
107 }
108 }
109110/// Returns the horizontal alignment of the [`Paragraph`].
111pub fn horizontal_alignment(&self) -> alignment::Horizontal {
112self.raw.horizontal_alignment()
113 }
114115/// Returns the vertical alignment of the [`Paragraph`].
116pub fn vertical_alignment(&self) -> alignment::Vertical {
117self.raw.vertical_alignment()
118 }
119120/// Returns the minimum boundaries that can fit the contents of the
121 /// [`Paragraph`].
122pub fn min_bounds(&self) -> Size {
123self.raw.min_bounds()
124 }
125126/// Returns the minimum width that can fit the contents of the
127 /// [`Paragraph`].
128pub fn min_width(&self) -> f32 {
129self.raw.min_width()
130 }
131132/// Returns the cached [`Paragraph`].
133pub fn raw(&self) -> &P {
134&self.raw
135 }
136}