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

@ -58,7 +58,16 @@ impl Cache {
text::to_shaping(key.shaping),
);
let bounds = text::measure(&buffer);
let (bounds, has_rtl) = text::measure(&buffer);
if has_rtl {
buffer.set_size(
font_system,
Some(bounds.width),
Some(bounds.height),
);
}
let _ = entry.insert(Entry {
buffer,
min_bounds: bounds,

View file

@ -450,7 +450,10 @@ impl editor::Editor for Editor {
fn min_bounds(&self) -> Size {
let internal = self.internal();
text::measure(buffer_from_editor(&internal.editor))
let (bounds, _has_rtl) =
text::measure(buffer_from_editor(&internal.editor));
bounds
}
fn update(

View file

@ -18,7 +18,7 @@ struct Internal {
font: Font,
shaping: Shaping,
wrapping: Wrapping,
horizontal_alignment: alignment::Horizontal,
horizontal_alignment: Option<alignment::Horizontal>,
vertical_alignment: alignment::Vertical,
bounds: Size,
min_bounds: Size,
@ -89,7 +89,8 @@ impl core::text::Paragraph for Paragraph {
text::to_shaping(text.shaping),
);
let min_bounds = text::measure(&buffer);
let min_bounds =
align(&mut buffer, &mut font_system, text.horizontal_alignment);
Self(Arc::new(Internal {
buffer,
@ -159,7 +160,8 @@ impl core::text::Paragraph for Paragraph {
None,
);
let min_bounds = text::measure(&buffer);
let min_bounds =
align(&mut buffer, &mut font_system, text.horizontal_alignment);
Self(Arc::new(Internal {
buffer,
@ -186,8 +188,10 @@ impl core::text::Paragraph for Paragraph {
Some(new_bounds.height),
);
let (min_bounds, _has_rtl) = text::measure(&paragraph.buffer);
paragraph.bounds = new_bounds;
paragraph.min_bounds = text::measure(&paragraph.buffer);
paragraph.min_bounds = min_bounds;
}
fn compare(&self, text: Text<()>) -> core::text::Difference {
@ -212,7 +216,7 @@ impl core::text::Paragraph for Paragraph {
}
}
fn horizontal_alignment(&self) -> alignment::Horizontal {
fn horizontal_alignment(&self) -> Option<alignment::Horizontal> {
self.internal().horizontal_alignment
}
@ -354,6 +358,42 @@ impl core::text::Paragraph for Paragraph {
}
}
fn align(
buffer: &mut cosmic_text::Buffer,
font_system: &mut text::FontSystem,
align_x: Option<alignment::Horizontal>,
) -> Size {
let (min_bounds, has_rtl) = text::measure(buffer);
let mut needs_relayout = has_rtl;
if let Some(align_x) = align_x {
let has_multiple_lines = buffer.lines.len() > 1
|| buffer.lines.first().is_some_and(|line| {
line.layout_opt().is_some_and(|layout| layout.len() > 1)
});
if has_multiple_lines {
for line in &mut buffer.lines {
let _ = line.set_align(Some(text::to_align(align_x)));
}
needs_relayout = true;
}
}
if needs_relayout {
log::trace!("Relayouting paragraph...");
buffer.set_size(
font_system.raw(),
Some(min_bounds.width),
Some(min_bounds.height),
);
}
min_bounds
}
impl Default for Paragraph {
fn default() -> Self {
Self(Arc::new(Internal::default()))
@ -397,7 +437,7 @@ impl Default for Internal {
font: Font::default(),
shaping: Shaping::default(),
wrapping: Wrapping::default(),
horizontal_alignment: alignment::Horizontal::Left,
horizontal_alignment: None,
vertical_alignment: alignment::Vertical::Top,
bounds: Size::ZERO,
min_bounds: Size::ZERO,
@ -413,7 +453,7 @@ pub struct Weak {
/// The minimum bounds of the [`Paragraph`].
pub min_bounds: Size,
/// The horizontal alignment of the [`Paragraph`].
pub horizontal_alignment: alignment::Horizontal,
pub horizontal_alignment: Option<alignment::Horizontal>,
/// The vertical alignment of the [`Paragraph`].
pub vertical_alignment: alignment::Vertical,
}