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,3 +1,4 @@
use crate::alignment;
use crate::{Padding, Point, Radians, Size, Vector}; use crate::{Padding, Point, Radians, Size, Vector};
/// An axis-aligned rectangle. /// An axis-aligned rectangle.
@ -303,6 +304,34 @@ impl Rectangle<f32> {
height: self.height * zoom, height: self.height * zoom,
} }
} }
/// Returns the top-left position to render an object of the given [`Size`].
/// inside the [`Rectangle`] that is anchored to the edge or corner
/// defined by the alignment arguments.
pub fn anchor(
&self,
size: Size,
align_x: impl Into<alignment::Horizontal>,
align_y: impl Into<alignment::Vertical>,
) -> Point {
let x = match align_x.into() {
alignment::Horizontal::Left => self.x,
alignment::Horizontal::Center => {
self.x + (self.width - size.width) / 2.0
}
alignment::Horizontal::Right => self.x + self.width - size.width,
};
let y = match align_y.into() {
alignment::Vertical::Top => self.y,
alignment::Vertical::Center => {
self.y + (self.height - size.height) / 2.0
}
alignment::Vertical::Bottom => self.y + self.height - size.height,
};
Point::new(x, y)
}
} }
impl std::ops::Mul<f32> for Rectangle<f32> { impl std::ops::Mul<f32> for Rectangle<f32> {

View file

@ -88,6 +88,18 @@ impl text::Paragraph for () {
text::Difference::None text::Difference::None
} }
fn size(&self) -> Pixels {
Pixels(16.0)
}
fn font(&self) -> Font {
Font::DEFAULT
}
fn line_height(&self) -> text::LineHeight {
text::LineHeight::default()
}
fn align_x(&self) -> text::Alignment { fn align_x(&self) -> text::Alignment {
text::Alignment::Default text::Alignment::Default
} }
@ -96,10 +108,22 @@ impl text::Paragraph for () {
alignment::Vertical::Top alignment::Vertical::Top
} }
fn wrapping(&self) -> text::Wrapping {
text::Wrapping::default()
}
fn shaping(&self) -> text::Shaping {
text::Shaping::default()
}
fn grapheme_position(&self, _line: usize, _index: usize) -> Option<Point> { fn grapheme_position(&self, _line: usize, _index: usize) -> Option<Point> {
None None
} }
fn bounds(&self) -> Size {
Size::ZERO
}
fn min_bounds(&self) -> Size { fn min_bounds(&self) -> Size {
Size::ZERO Size::ZERO
} }

View file

@ -46,6 +46,38 @@ pub struct Text<Content = String, Font = crate::Font> {
pub wrapping: Wrapping, pub wrapping: Wrapping,
} }
impl<Content, Font> Text<Content, Font>
where
Font: Copy,
{
/// Returns a new [`Text`] replacing only the content with the
/// given value.
pub fn with_content<T>(&self, content: T) -> Text<T, Font> {
Text {
content,
bounds: self.bounds,
size: self.size,
line_height: self.line_height,
font: self.font,
align_x: self.align_x,
align_y: self.align_y,
shaping: self.shaping,
wrapping: self.wrapping,
}
}
}
impl<Content, Font> Text<Content, Font>
where
Content: AsRef<str>,
Font: Copy,
{
/// Returns a borrowed version of [`Text`].
pub fn as_ref(&self) -> Text<&str, Font> {
self.with_content(self.content.as_ref())
}
}
/// The alignment of some text. /// The alignment of some text.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Alignment { pub enum Alignment {
@ -85,6 +117,18 @@ impl From<crate::Alignment> for Alignment {
} }
} }
impl From<Alignment> for alignment::Horizontal {
fn from(alignment: Alignment) -> Self {
match alignment {
Alignment::Default | Alignment::Left | Alignment::Justified => {
alignment::Horizontal::Left
}
Alignment::Center => alignment::Horizontal::Center,
Alignment::Right => alignment::Horizontal::Right,
}
}
}
/// The shaping strategy of some text. /// The shaping strategy of some text.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Shaping { pub enum Shaping {

View file

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

View file

@ -28,8 +28,7 @@ use crate::text;
use crate::text::paragraph::{self, Paragraph}; use crate::text::paragraph::{self, Paragraph};
use crate::widget::tree::{self, Tree}; use crate::widget::tree::{self, Tree};
use crate::{ use crate::{
Color, Element, Layout, Length, Pixels, Point, Rectangle, Size, Theme, Color, Element, Layout, Length, Pixels, Rectangle, Size, Theme, Widget,
Widget,
}; };
pub use text::{Alignment, LineHeight, Shaping, Wrapping}; pub use text::{Alignment, LineHeight, Shaping, Wrapping};
@ -63,15 +62,7 @@ where
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
fragment: text::Fragment<'a>, fragment: text::Fragment<'a>,
size: Option<Pixels>, format: Format<Renderer::Font>,
line_height: LineHeight,
width: Length,
height: Length,
align_x: text::Alignment,
align_y: alignment::Vertical,
font: Option<Renderer::Font>,
shaping: Shaping,
wrapping: Wrapping,
class: Theme::Class<'a>, class: Theme::Class<'a>,
} }
@ -84,28 +75,20 @@ where
pub fn new(fragment: impl text::IntoFragment<'a>) -> Self { pub fn new(fragment: impl text::IntoFragment<'a>) -> Self {
Text { Text {
fragment: fragment.into_fragment(), fragment: fragment.into_fragment(),
size: None, format: Format::default(),
line_height: LineHeight::default(),
font: None,
width: Length::Shrink,
height: Length::Shrink,
align_x: text::Alignment::Default,
align_y: alignment::Vertical::Top,
shaping: Shaping::default(),
wrapping: Wrapping::default(),
class: Theme::default(), class: Theme::default(),
} }
} }
/// Sets the size of the [`Text`]. /// Sets the size of the [`Text`].
pub fn size(mut self, size: impl Into<Pixels>) -> Self { pub fn size(mut self, size: impl Into<Pixels>) -> Self {
self.size = Some(size.into()); self.format.size = Some(size.into());
self self
} }
/// Sets the [`LineHeight`] of the [`Text`]. /// Sets the [`LineHeight`] of the [`Text`].
pub fn line_height(mut self, line_height: impl Into<LineHeight>) -> Self { pub fn line_height(mut self, line_height: impl Into<LineHeight>) -> Self {
self.line_height = line_height.into(); self.format.line_height = line_height.into();
self self
} }
@ -113,19 +96,19 @@ where
/// ///
/// [`Font`]: crate::text::Renderer::Font /// [`Font`]: crate::text::Renderer::Font
pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self { pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
self.font = Some(font.into()); self.format.font = Some(font.into());
self self
} }
/// Sets the width of the [`Text`] boundaries. /// Sets the width of the [`Text`] boundaries.
pub fn width(mut self, width: impl Into<Length>) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width.into(); self.format.width = width.into();
self self
} }
/// Sets the height of the [`Text`] boundaries. /// Sets the height of the [`Text`] boundaries.
pub fn height(mut self, height: impl Into<Length>) -> Self { pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height.into(); self.format.height = height.into();
self self
} }
@ -137,7 +120,7 @@ where
/// Sets the [`alignment::Horizontal`] of the [`Text`]. /// Sets the [`alignment::Horizontal`] of the [`Text`].
pub fn align_x(mut self, alignment: impl Into<text::Alignment>) -> Self { pub fn align_x(mut self, alignment: impl Into<text::Alignment>) -> Self {
self.align_x = alignment.into(); self.format.align_x = alignment.into();
self self
} }
@ -146,19 +129,19 @@ where
mut self, mut self,
alignment: impl Into<alignment::Vertical>, alignment: impl Into<alignment::Vertical>,
) -> Self { ) -> Self {
self.align_y = alignment.into(); self.format.align_y = alignment.into();
self self
} }
/// Sets the [`Shaping`] strategy of the [`Text`]. /// Sets the [`Shaping`] strategy of the [`Text`].
pub fn shaping(mut self, shaping: Shaping) -> Self { pub fn shaping(mut self, shaping: Shaping) -> Self {
self.shaping = shaping; self.format.shaping = shaping;
self self
} }
/// Sets the [`Wrapping`] strategy of the [`Text`]. /// Sets the [`Wrapping`] strategy of the [`Text`].
pub fn wrapping(mut self, wrapping: Wrapping) -> Self { pub fn wrapping(mut self, wrapping: Wrapping) -> Self {
self.wrapping = wrapping; self.format.wrapping = wrapping;
self self
} }
@ -200,8 +183,7 @@ where
} }
/// The internal state of a [`Text`] widget. /// The internal state of a [`Text`] widget.
#[derive(Debug, Default)] pub type State<P> = paragraph::Plain<P>;
pub struct State<P: Paragraph>(pub paragraph::Plain<P>);
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for Text<'_, Theme, Renderer> for Text<'_, Theme, Renderer>
@ -214,15 +196,13 @@ where
} }
fn state(&self) -> tree::State { fn state(&self) -> tree::State {
tree::State::new(State::<Renderer::Paragraph>( tree::State::new(paragraph::Plain::<Renderer::Paragraph>::default())
paragraph::Plain::default(),
))
} }
fn size(&self) -> Size<Length> { fn size(&self) -> Size<Length> {
Size { Size {
width: self.width, width: self.format.width,
height: self.height, height: self.format.height,
} }
} }
@ -236,16 +216,8 @@ where
tree.state.downcast_mut::<State<Renderer::Paragraph>>(), tree.state.downcast_mut::<State<Renderer::Paragraph>>(),
renderer, renderer,
limits, limits,
self.width,
self.height,
&self.fragment, &self.fragment,
self.line_height, self.format,
self.size,
self.font,
self.align_x,
self.align_y,
self.shaping,
self.wrapping,
) )
} }
@ -262,7 +234,14 @@ where
let state = tree.state.downcast_ref::<State<Renderer::Paragraph>>(); let state = tree.state.downcast_ref::<State<Renderer::Paragraph>>();
let style = theme.style(&self.class); let style = theme.style(&self.class);
draw(renderer, defaults, layout, state.0.raw(), style, viewport); draw(
renderer,
defaults,
layout.bounds(),
state.raw(),
style,
viewport,
);
} }
fn operate( fn operate(
@ -276,43 +255,67 @@ where
} }
} }
/// The format of some [`Text`].
///
/// Check out the methods of the [`Text`] widget
/// to learn more about each field.
#[derive(Debug, Clone, Copy)]
#[allow(missing_docs)]
pub struct Format<Font> {
pub width: Length,
pub height: Length,
pub size: Option<Pixels>,
pub font: Option<Font>,
pub line_height: LineHeight,
pub align_x: text::Alignment,
pub align_y: alignment::Vertical,
pub shaping: Shaping,
pub wrapping: Wrapping,
}
impl<Font> Default for Format<Font> {
fn default() -> Self {
Self {
size: None,
line_height: LineHeight::default(),
font: None,
width: Length::Shrink,
height: Length::Shrink,
align_x: text::Alignment::Default,
align_y: alignment::Vertical::Top,
shaping: Shaping::default(),
wrapping: Wrapping::default(),
}
}
}
/// Produces the [`layout::Node`] of a [`Text`] widget. /// Produces the [`layout::Node`] of a [`Text`] widget.
pub fn layout<Renderer>( pub fn layout<Renderer>(
state: &mut State<Renderer::Paragraph>, paragraph: &mut paragraph::Plain<Renderer::Paragraph>,
renderer: &Renderer, renderer: &Renderer,
limits: &layout::Limits, limits: &layout::Limits,
width: Length,
height: Length,
content: &str, content: &str,
line_height: LineHeight, format: Format<Renderer::Font>,
size: Option<Pixels>,
font: Option<Renderer::Font>,
align_x: text::Alignment,
align_y: alignment::Vertical,
shaping: Shaping,
wrapping: Wrapping,
) -> layout::Node ) -> layout::Node
where where
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
layout::sized(limits, width, height, |limits| { layout::sized(limits, format.width, format.height, |limits| {
let bounds = limits.max(); let bounds = limits.max();
let size = size.unwrap_or_else(|| renderer.default_size()); let size = format.size.unwrap_or_else(|| renderer.default_size());
let font = font.unwrap_or_else(|| renderer.default_font()); let font = format.font.unwrap_or_else(|| renderer.default_font());
let State(paragraph) = state;
let _ = paragraph.update(text::Text { let _ = paragraph.update(text::Text {
content, content,
bounds, bounds,
size, size,
line_height, line_height: format.line_height,
font, font,
align_x, align_x: format.align_x,
align_y, align_y: format.align_y,
shaping, shaping: format.shaping,
wrapping, wrapping: format.wrapping,
}); });
paragraph.min_bounds() paragraph.min_bounds()
@ -320,27 +323,21 @@ where
} }
/// Draws text using the same logic as the [`Text`] widget. /// Draws text using the same logic as the [`Text`] widget.
///
/// Specifically:
///
/// * If no `size` is provided, the default text size of the `Renderer` will be
/// used.
/// * If no `color` is provided, the [`renderer::Style::text_color`] will be
/// used.
/// * The alignment attributes do not affect the position of the bounds of the
/// [`Layout`].
pub fn draw<Renderer>( pub fn draw<Renderer>(
renderer: &mut Renderer, renderer: &mut Renderer,
style: &renderer::Style, style: &renderer::Style,
layout: Layout<'_>, bounds: Rectangle,
paragraph: &Renderer::Paragraph, paragraph: &Renderer::Paragraph,
appearance: Style, appearance: Style,
viewport: &Rectangle, viewport: &Rectangle,
) where ) where
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
let bounds = layout.bounds(); let anchor = bounds.anchor(
let anchor = anchor(bounds, paragraph.align_x(), paragraph.align_y()); paragraph.min_bounds(),
paragraph.align_x(),
paragraph.align_y(),
);
renderer.fill_paragraph( renderer.fill_paragraph(
paragraph, paragraph,
@ -350,27 +347,6 @@ pub fn draw<Renderer>(
); );
} }
/// Returns the anchor position of a [`Text`] widget.
pub fn anchor(
bounds: Rectangle,
align_x: text::Alignment,
align_y: alignment::Vertical,
) -> Point {
let x = match align_x {
Alignment::Default | Alignment::Left | Alignment::Justified => bounds.x,
Alignment::Center => bounds.center_x(),
Alignment::Right => bounds.x + bounds.width,
};
let y = match align_y {
alignment::Vertical::Top => bounds.y,
alignment::Vertical::Center => bounds.center_y(),
alignment::Vertical::Bottom => bounds.y + bounds.height,
};
Point::new(x, y)
}
impl<'a, Message, Theme, Renderer> From<Text<'a, Theme, Renderer>> impl<'a, Message, Theme, Renderer> From<Text<'a, Theme, Renderer>>
for Element<'a, Message, Theme, Renderer> for Element<'a, Message, Theme, Renderer>
where where

View file

@ -1,8 +1,10 @@
//! Draw paragraphs. //! Draw paragraphs.
use crate::core; use crate::core;
use crate::core::alignment; use crate::core::alignment;
use crate::core::text::{Alignment, Hit, Shaping, Span, Text, Wrapping}; use crate::core::text::{
use crate::core::{Font, Point, Rectangle, Size}; Alignment, Hit, LineHeight, Shaping, Span, Text, Wrapping,
};
use crate::core::{Font, Pixels, Point, Rectangle, Size};
use crate::text; use crate::text;
use std::fmt; use std::fmt;
@ -220,6 +222,18 @@ impl core::text::Paragraph for Paragraph {
} }
} }
fn size(&self) -> Pixels {
Pixels(self.0.buffer.metrics().font_size)
}
fn font(&self) -> Font {
self.0.font
}
fn line_height(&self) -> LineHeight {
LineHeight::Absolute(Pixels(self.0.buffer.metrics().line_height))
}
fn align_x(&self) -> Alignment { fn align_x(&self) -> Alignment {
self.internal().align_x self.internal().align_x
} }
@ -228,6 +242,18 @@ impl core::text::Paragraph for Paragraph {
self.internal().align_y self.internal().align_y
} }
fn wrapping(&self) -> Wrapping {
self.0.wrapping
}
fn shaping(&self) -> Shaping {
self.0.shaping
}
fn bounds(&self) -> Size {
self.0.bounds
}
fn min_bounds(&self) -> Size { fn min_bounds(&self) -> Size {
self.internal().min_bounds self.internal().min_bounds
} }

View file

@ -1,8 +1,6 @@
use crate::core::alignment; use crate::core::alignment;
use crate::core::text::{Alignment, Shaping}; use crate::core::text::{Alignment, Shaping};
use crate::core::{ use crate::core::{Color, Font, Pixels, Point, Rectangle, Transformation};
Color, Font, Pixels, Point, Rectangle, Size, Transformation,
};
use crate::graphics::text::cache::{self, Cache}; use crate::graphics::text::cache::{self, Cache};
use crate::graphics::text::editor; use crate::graphics::text::editor;
use crate::graphics::text::font_system; use crate::graphics::text::font_system;
@ -47,8 +45,6 @@ impl Pipeline {
clip_mask: Option<&tiny_skia::Mask>, clip_mask: Option<&tiny_skia::Mask>,
transformation: Transformation, transformation: Transformation,
) { ) {
use crate::core::text::Paragraph as _;
let Some(paragraph) = paragraph.upgrade() else { let Some(paragraph) = paragraph.upgrade() else {
return; return;
}; };
@ -59,10 +55,8 @@ impl Pipeline {
font_system.raw(), font_system.raw(),
&mut self.glyph_cache, &mut self.glyph_cache,
paragraph.buffer(), paragraph.buffer(),
Rectangle::new(position, paragraph.min_bounds()), position,
color, color,
paragraph.align_x(),
paragraph.align_y(),
pixels, pixels,
clip_mask, clip_mask,
transformation, transformation,
@ -78,8 +72,6 @@ impl Pipeline {
clip_mask: Option<&tiny_skia::Mask>, clip_mask: Option<&tiny_skia::Mask>,
transformation: Transformation, transformation: Transformation,
) { ) {
use crate::core::text::Editor as _;
let Some(editor) = editor.upgrade() else { let Some(editor) = editor.upgrade() else {
return; return;
}; };
@ -90,10 +82,8 @@ impl Pipeline {
font_system.raw(), font_system.raw(),
&mut self.glyph_cache, &mut self.glyph_cache,
editor.buffer(), editor.buffer(),
Rectangle::new(position, editor.bounds()), position,
color, color,
Alignment::Default,
alignment::Vertical::Top,
pixels, pixels,
clip_mask, clip_mask,
transformation, transformation,
@ -135,18 +125,26 @@ impl Pipeline {
let width = entry.min_bounds.width; let width = entry.min_bounds.width;
let height = entry.min_bounds.height; let height = entry.min_bounds.height;
let x = match align_x {
Alignment::Default | Alignment::Left | Alignment::Justified => {
bounds.x
}
Alignment::Center => bounds.x - width / 2.0,
Alignment::Right => bounds.x - width,
};
let y = match align_y {
alignment::Vertical::Top => bounds.y,
alignment::Vertical::Center => bounds.y - height / 2.0,
alignment::Vertical::Bottom => bounds.y - height,
};
draw( draw(
font_system, font_system,
&mut self.glyph_cache, &mut self.glyph_cache,
&entry.buffer, &entry.buffer,
Rectangle { Point::new(x, y),
width,
height,
..bounds
},
color, color,
align_x,
align_y,
pixels, pixels,
clip_mask, clip_mask,
transformation, transformation,
@ -164,22 +162,12 @@ impl Pipeline {
) { ) {
let mut font_system = font_system().write().expect("Write font system"); let mut font_system = font_system().write().expect("Write font system");
let (width, height) = buffer.size();
draw( draw(
font_system.raw(), font_system.raw(),
&mut self.glyph_cache, &mut self.glyph_cache,
buffer, buffer,
Rectangle::new( position,
position,
Size::new(
width.unwrap_or(pixels.width() as f32),
height.unwrap_or(pixels.height() as f32),
),
),
color, color,
Alignment::Default,
alignment::Vertical::Top,
pixels, pixels,
clip_mask, clip_mask,
transformation, transformation,
@ -196,34 +184,22 @@ fn draw(
font_system: &mut cosmic_text::FontSystem, font_system: &mut cosmic_text::FontSystem,
glyph_cache: &mut GlyphCache, glyph_cache: &mut GlyphCache,
buffer: &cosmic_text::Buffer, buffer: &cosmic_text::Buffer,
bounds: Rectangle, position: Point,
color: Color, color: Color,
align_x: Alignment,
align_y: alignment::Vertical,
pixels: &mut tiny_skia::PixmapMut<'_>, pixels: &mut tiny_skia::PixmapMut<'_>,
clip_mask: Option<&tiny_skia::Mask>, clip_mask: Option<&tiny_skia::Mask>,
transformation: Transformation, transformation: Transformation,
) { ) {
let bounds = bounds * transformation; let position = position * transformation;
let x = match align_x {
Alignment::Default | Alignment::Left | Alignment::Justified => bounds.x,
Alignment::Center => bounds.x - bounds.width / 2.0,
Alignment::Right => bounds.x - bounds.width,
};
let y = match align_y {
alignment::Vertical::Top => bounds.y,
alignment::Vertical::Center => bounds.y - bounds.height / 2.0,
alignment::Vertical::Bottom => bounds.y - bounds.height,
};
let mut swash = cosmic_text::SwashCache::new(); let mut swash = cosmic_text::SwashCache::new();
for run in buffer.layout_runs() { for run in buffer.layout_runs() {
for glyph in run.glyphs { for glyph in run.glyphs {
let physical_glyph = let physical_glyph = glyph.physical(
glyph.physical((x, y), transformation.scale_factor()); (position.x, position.y),
transformation.scale_factor(),
);
if let Some((buffer, placement)) = glyph_cache.allocate( if let Some((buffer, placement)) = glyph_cache.allocate(
physical_glyph.cache_key, physical_glyph.cache_key,

View file

@ -506,130 +506,114 @@ fn prepare(
let text_areas = sections.iter().zip(allocations.iter()).filter_map( let text_areas = sections.iter().zip(allocations.iter()).filter_map(
|(section, allocation)| { |(section, allocation)| {
let ( let (buffer, position, color, clip_bounds, transformation) =
buffer, match section {
bounds, Text::Paragraph {
align_x, position,
align_y, color,
color, clip_bounds,
clip_bounds, transformation,
transformation, ..
) = match section { } => {
Text::Paragraph { let Some(Allocation::Paragraph(paragraph)) = allocation
position, else {
color, return None;
clip_bounds, };
transformation,
..
} => {
use crate::core::text::Paragraph as _;
let Some(Allocation::Paragraph(paragraph)) = allocation (
else { paragraph.buffer(),
return None; *position,
}; *color,
*clip_bounds,
*transformation,
)
}
Text::Editor {
position,
color,
clip_bounds,
transformation,
..
} => {
let Some(Allocation::Editor(editor)) = allocation
else {
return None;
};
( (
paragraph.buffer(), editor.buffer(),
Rectangle::new(*position, paragraph.min_bounds()), *position,
paragraph.align_x(), *color,
paragraph.align_y(), *clip_bounds,
*color, *transformation,
*clip_bounds, )
*transformation, }
) Text::Cached {
} bounds,
Text::Editor { align_x,
position, align_y,
color, color,
clip_bounds, clip_bounds,
transformation, ..
.. } => {
} => { let Some(Allocation::Cache(key)) = allocation else {
use crate::core::text::Editor as _; return None;
};
let Some(Allocation::Editor(editor)) = allocation else { let entry =
return None; buffer_cache.get(key).expect("Get cached buffer");
};
( let mut position = bounds.position();
editor.buffer(),
Rectangle::new(*position, editor.bounds()),
Alignment::Default,
alignment::Vertical::Top,
*color,
*clip_bounds,
*transformation,
)
}
Text::Cached {
bounds,
align_x,
align_y,
color,
clip_bounds,
..
} => {
let Some(Allocation::Cache(key)) = allocation else {
return None;
};
let entry = position.x = match align_x {
buffer_cache.get(key).expect("Get cached buffer"); Alignment::Default
| Alignment::Left
| Alignment::Justified => position.x,
Alignment::Center => {
position.x - entry.min_bounds.width / 2.0
}
Alignment::Right => {
position.x - entry.min_bounds.width
}
};
( position.y = match align_y {
&entry.buffer, alignment::Vertical::Top => position.y,
Rectangle::new(bounds.position(), entry.min_bounds), alignment::Vertical::Center => {
*align_x, position.y - entry.min_bounds.height / 2.0
*align_y, }
*color, alignment::Vertical::Bottom => {
*clip_bounds, position.y - entry.min_bounds.height
Transformation::IDENTITY, }
) };
}
Text::Raw {
raw,
transformation,
} => {
let Some(Allocation::Raw(buffer)) = allocation else {
return None;
};
let (width, height) = buffer.size(); (
&entry.buffer,
position,
*color,
*clip_bounds,
Transformation::IDENTITY,
)
}
Text::Raw {
raw,
transformation,
} => {
let Some(Allocation::Raw(buffer)) = allocation else {
return None;
};
( (
buffer.as_ref(), buffer.as_ref(),
Rectangle::new(
raw.position, raw.position,
Size::new( raw.color,
width.unwrap_or(layer_bounds.width), raw.clip_bounds,
height.unwrap_or(layer_bounds.height), *transformation,
), )
), }
Alignment::Default, };
alignment::Vertical::Top,
raw.color,
raw.clip_bounds,
*transformation,
)
}
};
let bounds = bounds * transformation * layer_transformation; let position = position * transformation * layer_transformation;
let left = match align_x {
Alignment::Default | Alignment::Left | Alignment::Justified => {
bounds.x
}
Alignment::Center => bounds.x - bounds.width / 2.0,
Alignment::Right => bounds.x - bounds.width,
};
let top = match align_y {
alignment::Vertical::Top => bounds.y,
alignment::Vertical::Center => bounds.y - bounds.height / 2.0,
alignment::Vertical::Bottom => bounds.y - bounds.height,
};
let clip_bounds = layer_bounds.intersection( let clip_bounds = layer_bounds.intersection(
&(clip_bounds * transformation * layer_transformation), &(clip_bounds * transformation * layer_transformation),
@ -637,8 +621,8 @@ fn prepare(
Some(cryoglyph::TextArea { Some(cryoglyph::TextArea {
buffer, buffer,
left, left: position.x,
top, top: position.y,
scale: transformation.scale_factor() scale: transformation.scale_factor()
* layer_transformation.scale_factor(), * layer_transformation.scale_factor(),
bounds: cryoglyph::TextBounds { bounds: cryoglyph::TextBounds {

View file

@ -287,16 +287,18 @@ where
state, state,
renderer, renderer,
limits, limits,
self.width,
Length::Shrink,
&self.label, &self.label,
self.text_line_height, widget::text::Format {
self.text_size, width: self.width,
self.font, height: Length::Shrink,
text::Alignment::Default, line_height: self.text_line_height,
alignment::Vertical::Top, size: self.text_size,
self.text_shaping, font: self.font,
self.text_wrapping, align_x: text::Alignment::Default,
align_y: alignment::Vertical::Top,
shaping: self.text_shaping,
wrapping: self.text_wrapping,
},
) )
}, },
) )
@ -436,8 +438,8 @@ where
crate::text::draw( crate::text::draw(
renderer, renderer,
defaults, defaults,
label_layout, label_layout.bounds(),
state.0.raw(), state.raw(),
crate::text::Style { crate::text::Style {
color: style.text_color, color: style.text_color,
}, },

View file

@ -308,16 +308,18 @@ where
state, state,
renderer, renderer,
limits, limits,
self.width,
Length::Shrink,
&self.label, &self.label,
self.text_line_height, widget::text::Format {
self.text_size, width: self.width,
self.font, height: Length::Shrink,
text::Alignment::Default, line_height: self.text_line_height,
alignment::Vertical::Top, size: self.text_size,
self.text_shaping, font: self.font,
self.text_wrapping, align_x: text::Alignment::Default,
align_y: alignment::Vertical::Top,
shaping: self.text_shaping,
wrapping: self.text_wrapping,
},
) )
}, },
) )
@ -445,8 +447,8 @@ where
crate::text::draw( crate::text::draw(
renderer, renderer,
defaults, defaults,
label_layout, label_layout.bounds(),
state.0.raw(), state.raw(),
crate::text::Style { crate::text::Style {
color: style.text_color, color: style.text_color,
}, },

View file

@ -365,7 +365,7 @@ where
text::draw( text::draw(
renderer, renderer,
defaults, defaults,
layout, layout.bounds(),
&state.paragraph, &state.paragraph,
style, style,
viewport, viewport,

View file

@ -57,8 +57,9 @@ use crate::core::widget::operation::{self, Operation};
use crate::core::widget::tree::{self, Tree}; use crate::core::widget::tree::{self, Tree};
use crate::core::window; use crate::core::window;
use crate::core::{ use crate::core::{
Background, Border, Color, Element, Event, InputMethod, Layout, Length, Alignment, Background, Border, Color, Element, Event, InputMethod, Layout,
Padding, Pixels, Point, Rectangle, Shell, Size, Theme, Vector, Widget, Length, Padding, Pixels, Point, Rectangle, Shell, Size, Theme, Vector,
Widget,
}; };
use crate::runtime::Action; use crate::runtime::Action;
use crate::runtime::task::{self, Task}; use crate::runtime::task::{self, Task};
@ -481,9 +482,15 @@ where
if self.icon.is_some() { if self.icon.is_some() {
let icon_layout = children_layout.next().unwrap(); let icon_layout = children_layout.next().unwrap();
let icon = state.icon.raw();
renderer.fill_paragraph( renderer.fill_paragraph(
state.icon.raw(), icon,
icon_layout.bounds().center(), icon_layout.bounds().anchor(
icon.min_bounds(),
Alignment::Center,
Alignment::Center,
),
style.icon, style.icon,
*viewport, *viewport,
); );
@ -609,8 +616,11 @@ where
renderer.fill_paragraph( renderer.fill_paragraph(
paragraph, paragraph,
Point::new(text_bounds.x, text_bounds.center_y()) text_bounds.anchor(
+ Vector::new(alignment_offset - offset, 0.0), paragraph.min_bounds(),
Alignment::Start,
Alignment::Center,
) + Vector::new(alignment_offset - offset, 0.0),
if text.is_empty() { if text.is_empty() {
style.placeholder style.placeholder
} else { } else {
@ -1724,7 +1734,7 @@ fn replace_paragraph<Renderer>(
state.value = paragraph::Plain::new(Text { state.value = paragraph::Plain::new(Text {
font, font,
line_height, line_height,
content: &value.to_string(), content: value.to_string(),
bounds: Size::new(f32::INFINITY, text_bounds.height), bounds: Size::new(f32::INFINITY, text_bounds.height),
size: text_size, size: text_size,
align_x: text::Alignment::Default, align_x: text::Alignment::Default,

View file

@ -291,16 +291,18 @@ where
state, state,
renderer, renderer,
limits, limits,
self.width,
Length::Shrink,
label, label,
self.text_line_height, widget::text::Format {
self.text_size, width: self.width,
self.font, height: Length::Shrink,
self.text_alignment, line_height: self.text_line_height,
alignment::Vertical::Top, size: self.text_size,
self.text_shaping, font: self.font,
self.text_wrapping, align_x: self.text_alignment,
align_y: alignment::Vertical::Top,
shaping: self.text_shaping,
wrapping: self.text_wrapping,
},
) )
} else { } else {
layout::Node::new(Size::ZERO) layout::Node::new(Size::ZERO)
@ -406,8 +408,8 @@ where
crate::text::draw( crate::text::draw(
renderer, renderer,
style, style,
label_layout, label_layout.bounds(),
state.0.raw(), state.raw(),
crate::text::Style::default(), crate::text::Style::default(),
viewport, viewport,
); );