Introduce text::Alignment with Justified support

This commit is contained in:
Héctor Ramón Jiménez 2025-03-11 02:25:44 +01:00
parent e45d4b5cb6
commit 0e4a392731
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
30 changed files with 227 additions and 195 deletions

View file

@ -11,7 +11,7 @@ pub use cosmic_text;
use crate::core::alignment;
use crate::core::font::{self, Font};
use crate::core::text::{Shaping, Wrapping};
use crate::core::text::{Alignment, Shaping, Wrapping};
use crate::core::{Color, Pixels, Point, Rectangle, Size, Transformation};
use std::borrow::Cow;
@ -54,9 +54,9 @@ pub enum Text {
/// The font of the text.
font: Font,
/// The horizontal alignment of the text.
horizontal_alignment: Option<alignment::Horizontal>,
align_x: Alignment,
/// The vertical alignment of the text.
vertical_alignment: alignment::Vertical,
align_y: alignment::Vertical,
/// The shaping strategy of the text.
shaping: Shaping,
/// The clip bounds of the text.
@ -73,7 +73,7 @@ pub enum Text {
impl Text {
/// Returns the visible bounds of the [`Text`].
pub fn visible_bounds(&self) -> Option<Rectangle> {
let (bounds, horizontal_alignment, vertical_alignment) = match self {
let (bounds, align_x, align_y) = match self {
Text::Paragraph {
position,
paragraph,
@ -84,8 +84,8 @@ impl Text {
Rectangle::new(*position, paragraph.min_bounds)
.intersection(clip_bounds)
.map(|bounds| bounds * *transformation),
paragraph.horizontal_alignment,
Some(paragraph.vertical_alignment),
paragraph.align_x,
Some(paragraph.align_y),
),
Text::Editor {
editor,
@ -97,38 +97,38 @@ impl Text {
Rectangle::new(*position, editor.bounds)
.intersection(clip_bounds)
.map(|bounds| bounds * *transformation),
None,
Alignment::Default,
None,
),
Text::Cached {
bounds,
clip_bounds,
horizontal_alignment,
vertical_alignment,
align_x: horizontal_alignment,
align_y: vertical_alignment,
..
} => (
bounds.intersection(clip_bounds),
*horizontal_alignment,
Some(*vertical_alignment),
),
Text::Raw { raw, .. } => (Some(raw.clip_bounds), None, None),
Text::Raw { raw, .. } => {
(Some(raw.clip_bounds), Alignment::Default, None)
}
};
let mut bounds = bounds?;
if let Some(alignment) = horizontal_alignment {
match alignment {
alignment::Horizontal::Left => {}
alignment::Horizontal::Center => {
bounds.x -= bounds.width / 2.0;
}
alignment::Horizontal::Right => {
bounds.x -= bounds.width;
}
match align_x {
Alignment::Default | Alignment::Left | Alignment::Justified => {}
Alignment::Center => {
bounds.x -= bounds.width / 2.0;
}
Alignment::Right => {
bounds.x -= bounds.width;
}
}
if let Some(alignment) = vertical_alignment {
if let Some(alignment) = align_y {
match alignment {
alignment::Vertical::Top => {}
alignment::Vertical::Center => {
@ -313,11 +313,13 @@ fn to_style(style: font::Style) -> cosmic_text::Style {
}
}
fn to_align(alignment: alignment::Horizontal) -> cosmic_text::Align {
fn to_align(alignment: Alignment) -> Option<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,
Alignment::Default => None,
Alignment::Left => Some(cosmic_text::Align::Left),
Alignment::Center => Some(cosmic_text::Align::Center),
Alignment::Right => Some(cosmic_text::Align::Right),
Alignment::Justified => Some(cosmic_text::Align::Justified),
}
}