Make anchoring explicit and improve reusability of text pipelines

This commit is contained in:
Héctor Ramón Jiménez 2025-05-04 03:54:42 +02:00
parent d643bd5ba2
commit 6bf709e03e
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
13 changed files with 423 additions and 325 deletions

View file

@ -1,7 +1,9 @@
//! Draw paragraphs.
use crate::alignment;
use crate::text::{Alignment, Difference, Hit, Span, Text};
use crate::{Point, Rectangle, Size};
use crate::text::{
Alignment, Difference, Hit, LineHeight, Shaping, Span, Text, Wrapping,
};
use crate::{Pixels, Point, Rectangle, Size};
/// A text paragraph.
pub trait Paragraph: Sized + Default {
@ -23,12 +25,30 @@ pub trait Paragraph: Sized + Default {
/// [`Difference`].
fn compare(&self, text: Text<(), Self::Font>) -> Difference;
/// Returns the text size of the [`Paragraph`] in [`Pixels`].
fn size(&self) -> Pixels;
/// Returns the font of the [`Paragraph`].
fn font(&self) -> Self::Font;
/// Returns the [`LineHeight`] of the [`Paragraph`].
fn line_height(&self) -> LineHeight;
/// Returns the horizontal alignment of the [`Paragraph`].
fn align_x(&self) -> Alignment;
/// Returns the vertical alignment of the [`Paragraph`].
fn align_y(&self) -> alignment::Vertical;
/// Returns the [`Wrapping`] strategy of the [`Paragraph`]>
fn wrapping(&self) -> Wrapping;
/// Returns the [`Shaping`] strategy of the [`Paragraph`]>
fn shaping(&self) -> Shaping;
/// Returns the availalbe bounds used to layout the [`Paragraph`].
fn bounds(&self) -> Size;
/// Returns the minimum boundaries that can fit the contents of the
/// [`Paragraph`].
fn min_bounds(&self) -> Size;
@ -69,12 +89,10 @@ pub struct Plain<P: Paragraph> {
impl<P: Paragraph> Plain<P> {
/// Creates a new [`Plain`] paragraph.
pub fn new(text: Text<&str, P::Font>) -> Self {
let content = text.content.to_owned();
pub fn new(text: Text<String, P::Font>) -> Self {
Self {
raw: P::with_text(text),
content,
raw: P::with_text(text.as_ref()),
content: text.content,
}
}
@ -88,17 +106,7 @@ impl<P: Paragraph> Plain<P> {
return true;
}
match self.raw.compare(Text {
content: (),
bounds: text.bounds,
size: text.size,
line_height: text.line_height,
font: text.font,
align_x: text.align_x,
align_y: text.align_y,
shaping: text.shaping,
wrapping: text.wrapping,
}) {
match self.raw.compare(text.with_content(())) {
Difference::None => false,
Difference::Bounds => {
self.raw.resize(text.bounds);
@ -148,4 +156,19 @@ impl<P: Paragraph> Plain<P> {
pub fn content(&self) -> &str {
&self.content
}
/// Returns the [`Paragraph`] as a [`Text`] definition.
pub fn as_text(&self) -> Text<&str, P::Font> {
Text {
content: &self.content,
bounds: self.raw.bounds(),
size: self.raw.size(),
line_height: self.raw.line_height(),
font: self.raw.font(),
align_x: self.raw.align_x(),
align_y: self.raw.align_y(),
shaping: self.raw.shaping(),
wrapping: self.raw.wrapping(),
}
}
}