Skip to content

Commit

Permalink
Make alignment when free space is negative configurable (#241)
Browse files Browse the repository at this point in the history
Untested, but probably fixes
#240

---------

Signed-off-by: Nico Burns <[email protected]>
  • Loading branch information
nicoburns authored Jan 22, 2025
1 parent e8bbdcf commit 0f90765
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 12 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 @@ -168,7 +168,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 @@ -99,7 +99,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
11 changes: 9 additions & 2 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,8 +28,7 @@ pub(crate) fn align<B: Brush>(
// Compute free space.
let free_space = alignment_width - line.metrics.advance + line.metrics.trailing_whitespace;

// Alignment only applies if free_space > 0
if free_space <= 0. {
if !align_when_overflowing && free_space <= 0.0 {
continue;
}

Expand All @@ -43,6 +43,13 @@ pub(crate) fn align<B: Brush>(
line.metrics.offset = free_space * 0.5;
}
Alignment::Justified => {
// Justified alignment doesn't have any effect if free_space is negative or zero
if free_space <= 0.0 {
continue;
}

// Justified alignment doesn't apply to the last line of a paragraph (`BreakReason::None`)
// or if there are no whitespace gaps to adjust
if line.break_reason == BreakReason::None || line.num_spaces == 0 {
continue;
}
Expand Down
6 changes: 5 additions & 1 deletion parley/src/layout/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,11 @@ mod tests {
fn cluster_from_position_with_alignment(alignment: Alignment) {
let mut layout = create_unaligned_layout();
let width = layout.full_width();
layout.align(Some(width + 100.), alignment);
layout.align(
Some(width + 100.),
alignment,
false, /* align_when_overflowing */
);
assert_eq!(
layout.len(),
1,
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
2 changes: 1 addition & 1 deletion parley/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
//! // Run line-breaking and alignment on the Layout
//! const MAX_WIDTH : Option<f32> = Some(100.0);
//! layout.break_all_lines(MAX_WIDTH);
//! layout.align(MAX_WIDTH, Alignment::Start);
//! layout.align(MAX_WIDTH, Alignment::Start, false /* align_when_overflowing */);
//!
//! // Inspect computed layout (see examples for more details)
//! let width = layout.width();
Expand Down
18 changes: 15 additions & 3 deletions parley/src/tests/test_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ fn plain_multiline_text() {
let mut builder = env.builder(text);
let mut layout = builder.build(text);
layout.break_all_lines(None);
layout.align(None, Alignment::Start);
layout.align(
None,
Alignment::Start,
false, /* align_when_overflowing */
);

env.check_layout_snapshot(&layout);
}
Expand All @@ -36,7 +40,11 @@ fn placing_inboxes() {
});
let mut layout = builder.build(text);
layout.break_all_lines(None);
layout.align(None, Alignment::Start);
layout.align(
None,
Alignment::Start,
false, /* align_when_overflowing */
);
env.with_name(test_case_name).check_layout_snapshot(&layout);
}
}
Expand All @@ -57,7 +65,11 @@ fn only_inboxes_wrap() {
}
let mut layout = builder.build(text);
layout.break_all_lines(Some(40.0));
layout.align(None, Alignment::Middle);
layout.align(
None,
Alignment::Middle,
false, /* align_when_overflowing */
);

env.check_layout_snapshot(&layout);
}

0 comments on commit 0f90765

Please sign in to comment.