Fix horizontal text alignment

This commit is contained in:
Héctor Ramón Jiménez 2025-03-11 00:43:51 +01:00
parent 35c0e14452
commit 179a34d37b
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
23 changed files with 141 additions and 67 deletions

View file

@ -54,7 +54,7 @@ pub enum Text {
/// The font of the text.
font: Font,
/// The horizontal alignment of the text.
horizontal_alignment: alignment::Horizontal,
horizontal_alignment: Option<alignment::Horizontal>,
/// The vertical alignment of the text.
vertical_alignment: alignment::Vertical,
/// The shaping strategy of the text.
@ -84,7 +84,7 @@ impl Text {
Rectangle::new(*position, paragraph.min_bounds)
.intersection(clip_bounds)
.map(|bounds| bounds * *transformation),
Some(paragraph.horizontal_alignment),
paragraph.horizontal_alignment,
Some(paragraph.vertical_alignment),
),
Text::Editor {
@ -108,7 +108,7 @@ impl Text {
..
} => (
bounds.intersection(clip_bounds),
Some(*horizontal_alignment),
*horizontal_alignment,
Some(*vertical_alignment),
),
Text::Raw { raw, .. } => (Some(raw.clip_bounds), None, None),
@ -242,15 +242,19 @@ impl PartialEq for Raw {
}
/// Measures the dimensions of the given [`cosmic_text::Buffer`].
pub fn measure(buffer: &cosmic_text::Buffer) -> Size {
let (width, height) =
buffer
.layout_runs()
.fold((0.0, 0.0), |(width, height), run| {
(run.line_w.max(width), height + run.line_height)
});
pub fn measure(buffer: &cosmic_text::Buffer) -> (Size, bool) {
let (width, height, has_rtl) = buffer.layout_runs().fold(
(0.0, 0.0, false),
|(width, height, has_rtl), run| {
(
run.line_w.max(width),
height + run.line_height,
has_rtl || run.rtl,
)
},
);
Size::new(width, height)
(Size::new(width, height), has_rtl)
}
/// Returns the attributes of the given [`Font`].
@ -309,6 +313,14 @@ fn to_style(style: font::Style) -> cosmic_text::Style {
}
}
fn to_align(alignment: alignment::Horizontal) -> cosmic_text::Align {
match alignment {
alignment::Horizontal::Left => cosmic_text::Align::Left,
alignment::Horizontal::Center => cosmic_text::Align::Center,
alignment::Horizontal::Right => cosmic_text::Align::Right,
}
}
/// Converts some [`Shaping`] strategy to a [`cosmic_text::Shaping`] strategy.
pub fn to_shaping(shaping: Shaping) -> cosmic_text::Shaping {
match shaping {