Add text last-line alignment modes for "Justify Center", "Justify Right", and "Justify All" (#4024)

* feat: Add justify proper alignments(right/left/centre/all)

* chore: fmt

* chore: refactor

* Cleanup

* Fix crash when changing selected layer's alignment from the Text tool control bar

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Jatin Bharti
2026-05-07 07:46:19 +05:30
committed by GitHub
parent 1c2ac19b16
commit c0a8241f50
8 changed files with 142 additions and 32 deletions

View File

@@ -31,21 +31,37 @@ pub use vector_types;
#[widget(Radio)]
pub enum TextAlign {
#[default]
Left,
Center,
Right,
#[label("Justify")]
AlignLeft,
AlignCenter,
AlignRight,
JustifyLeft,
// TODO: JustifyCenter, JustifyRight, JustifyAll
JustifyCenter,
JustifyRight,
JustifyAll,
}
impl From<TextAlign> for parley::Alignment {
fn from(val: TextAlign) -> Self {
match val {
TextAlign::Left => parley::Alignment::Left,
TextAlign::Center => parley::Alignment::Center,
TextAlign::Right => parley::Alignment::Right,
TextAlign::JustifyLeft => parley::Alignment::Justify,
TextAlign::AlignLeft => parley::Alignment::Left,
TextAlign::AlignCenter => parley::Alignment::Center,
TextAlign::AlignRight => parley::Alignment::Right,
_ => parley::Alignment::Justify,
}
}
}
impl TextAlign {
/// What `parley::Alignment` to apply as a post-correction to the last line of a paragraph, or `None` if parley's default already handles it.
///
/// `JustifyLeft` returns `None` because parley already left-aligns the last line of a `Justify` layout. The other justify modes need
/// the last line shifted (`Center`/`Right`) or its inter-word spaces redistributed (`Justify` / `JustifyAll`).
pub fn last_line_correction(self) -> Option<parley::Alignment> {
match self {
Self::JustifyCenter => Some(parley::Alignment::Center),
Self::JustifyRight => Some(parley::Alignment::Right),
Self::JustifyAll => Some(parley::Alignment::Justify),
_ => None,
}
}
}

View File

@@ -52,10 +52,20 @@ impl PathBuilder {
}
#[allow(clippy::too_many_arguments)]
fn draw_glyph(&mut self, glyph: &OutlineGlyph<'_>, size: f32, normalized_coords: &[NormalizedCoord], glyph_offset: DVec2, style_skew: Option<DAffine2>, skew: DAffine2, per_glyph_items: bool) {
fn draw_glyph(
&mut self,
glyph: &OutlineGlyph<'_>,
size: f32,
normalized_coords: &[NormalizedCoord],
glyph_offset: DVec2,
style_skew: Option<DAffine2>,
skew: DAffine2,
per_glyph_items: bool,
) -> bool {
let location_ref = LocationRef::new(normalized_coords);
let settings = DrawSettings::unhinted(Size::new(size), location_ref);
glyph.draw(settings, self).unwrap();
let has_geometry = !self.glyph_subpaths.is_empty();
// Apply transforms in correct order: style-based skew first, then user-requested skew
// This ensures font synthesis (italic) is applied before user transformations
@@ -91,10 +101,12 @@ impl PathBuilder {
self.merged_click_target_baselines.push(glyph_offset.y);
}
}
has_geometry
}
pub fn render_glyph_run(&mut self, glyph_run: &GlyphRun<'_, ()>, tilt: f64, per_glyph_items: bool) {
let mut run_x = glyph_run.offset();
pub fn render_glyph_run(&mut self, glyph_run: &GlyphRun<'_, ()>, tilt: f64, per_glyph_items: bool, x_offset: f32, space_extra: f32) {
let mut run_x = glyph_run.offset() + x_offset;
let run_y = glyph_run.baseline();
let run = glyph_run.run();
@@ -142,7 +154,11 @@ impl PathBuilder {
if !per_glyph_items {
self.origin = glyph_offset;
}
self.draw_glyph(&glyph_outline, font_size, &normalized_coords, glyph_offset, style_skew, skew, per_glyph_items);
let drew_geometry = self.draw_glyph(&glyph_outline, font_size, &normalized_coords, glyph_offset, style_skew, skew, per_glyph_items);
if !drew_geometry && space_extra != 0. && glyph.advance > 0. {
run_x += space_extra;
}
}
}
}

View File

@@ -108,14 +108,50 @@ impl TextContext {
})
.unwrap_or_default();
let alignment_width = typesetting.max_width.map(|w| w as f32).unwrap_or_else(|| layout.full_width());
let last_line_correction = typesetting.align.last_line_correction();
let mut path_builder = PathBuilder::new(per_glyph_items, layout.scale() as f64, text_frame_size, first_glyph_offset);
for line in layout.lines() {
let range = line.text_range();
// Parley always includes a hard-break `\n` as the last byte of the preceding line's range, so the line
// is at the end of a paragraph if it's the very last line of the buffer or its text ends with `\n`.
let is_last_para_line = range.end == text.len() || text.get(range.clone()).is_some_and(|s| s.ends_with('\n'));
let (x_offset, space_extra) = if let (true, Some(correction)) = (is_last_para_line, last_line_correction) {
let metrics = line.metrics();
let content_advance = metrics.advance - metrics.trailing_whitespace;
let free_space = alignment_width - content_advance;
match correction {
parley::Alignment::Center => (free_space * 0.5, 0.),
parley::Alignment::Right => (free_space, 0.),
parley::Alignment::Justify => {
// Exclude trailing-whitespace clusters from the divisor so the redistribution stretches only the internal spaces.
// Parley's `trailing_whitespace` is in advance units, not bytes, so we re-derive the byte boundary here to filter cluster ranges.
let line_text = text.get(range.clone()).unwrap_or("");
let trailing_len = line_text.len() - line_text.trim_end().len();
let visible_end_index = range.end - trailing_len;
let space_count: usize = line
.runs()
.map(|run| run.clusters().filter(|c| c.is_space_or_nbsp() && c.text_range().start < visible_end_index).count())
.sum();
let extra = if space_count > 0 { free_space / space_count as f32 } else { 0. };
(0., extra)
}
_ => (0., 0.),
}
} else {
(0., 0.)
};
for item in line.items() {
if let PositionedLayoutItem::GlyphRun(glyph_run) = item
&& typesetting.max_height.filter(|&max_height| glyph_run.baseline() > max_height as f32).is_none()
{
path_builder.render_glyph_run(&glyph_run, typesetting.tilt, per_glyph_items);
path_builder.render_glyph_run(&glyph_run, typesetting.tilt, per_glyph_items, x_offset, space_extra);
}
}
}