Skip to content

Commit

Permalink
Make fallback alignment behaviour configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Burns <[email protected]>
  • Loading branch information
nicoburns committed Jan 21, 2025
1 parent f3a7c26 commit 4ec40e5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
6 changes: 5 additions & 1 deletion examples/swash_render/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ fn main() {

// Perform layout (including bidi resolution and shaping) with start alignment
layout.break_all_lines(max_advance);
layout.align(max_advance, Alignment::Start);
layout.align(
max_advance,
Alignment::Start,
false, /* align_when_overflowing */
);

// Create image to render into
let width = layout.width().ceil() as u32 + (padding * 2);
Expand Down
6 changes: 5 additions & 1 deletion examples/tiny_skia_render/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ fn main() {

// Perform layout (including bidi resolution and shaping) with start alignment
layout.break_all_lines(max_advance);
layout.align(max_advance, Alignment::Start);
layout.align(
max_advance,
Alignment::Start,
false, /* align_when_overflowing */
);
let width = layout.width().ceil() as u32;
let height = layout.height().ceil() as u32;
let padded_width = width + padding * 2;
Expand Down
5 changes: 5 additions & 0 deletions parley/src/layout/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub(crate) fn align<B: Brush>(
layout: &mut LayoutData<B>,
alignment_width: Option<f32>,
alignment: Alignment,
align_when_overflowing: bool,
) {
let alignment_width = alignment_width.unwrap_or_else(|| {
let max_line_length = layout
Expand All @@ -27,6 +28,10 @@ pub(crate) fn align<B: Brush>(
// Compute free space.
let free_space = alignment_width - line.metrics.advance + line.metrics.trailing_whitespace;

if !align_when_overflowing && free_space <= 0.0 {
continue;
}

match alignment {
Alignment::Start => {
// Do nothing
Expand Down
6 changes: 5 additions & 1 deletion parley/src/layout/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,11 @@ where
}
self.layout = builder.build(&self.buffer);
self.layout.break_all_lines(self.width);
self.layout.align(self.width, self.alignment);
self.layout.align(
self.width,
self.alignment,
false, /* align_when_overflowing */
);
self.selection = self.selection.refresh(&self.layout);
self.layout_dirty = false;
self.generation.nudge();
Expand Down
18 changes: 16 additions & 2 deletions parley/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,22 @@ impl<B: Brush> Layout<B> {

// Apply to alignment to layout relative to the specified container width. If container_width is not
// specified then the max line length is used.
pub fn align(&mut self, container_width: Option<f32>, alignment: Alignment) {
align(&mut self.data, container_width, alignment);
//
// If `align_when_overflowing` is set to `true` then `Center` and `End` alignment will apply even if
// the line contents are wider than the `container_width`. If it is set to `false` then all overflowing
// lines will be `Start` aligned.
pub fn align(
&mut self,
container_width: Option<f32>,
alignment: Alignment,
align_when_overflowing: bool,
) {
align(
&mut self.data,
container_width,
alignment,
align_when_overflowing,
);
}

/// Returns the index and `Line` object for the line containing the
Expand Down

0 comments on commit 4ec40e5

Please sign in to comment.