Skip to content

Commit 681d5a1

Browse files
committed
Use Layout::full_width instead of calculating for alignment
This does require line breaking to have happened, but that is already a requirement, as otherwise `Layout::lines` is empty.
1 parent ab01c9d commit 681d5a1

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

parley/src/layout/alignment.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,7 @@ pub(crate) fn align<B: Brush>(
1515
) {
1616
// Whether the text base direction is right-to-left.
1717
let is_rtl = layout.base_level & 1 == 1;
18-
let alignment_width = alignment_width.unwrap_or_else(|| {
19-
let max_line_length = layout
20-
.lines
21-
.iter()
22-
.map(|line| line.metrics.advance)
23-
.max_by(f32::total_cmp)
24-
.unwrap_or(0.0);
25-
max_line_length.min(max_line_length)
26-
});
18+
let alignment_width = alignment_width.unwrap_or_else(|| layout.full_width);
2719

2820
// Apply alignment to line items
2921
for line in &mut layout.lines {

parley/src/layout/mod.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,16 @@ impl<B: Brush> Layout<B> {
156156
.break_remaining(max_advance.unwrap_or(f32::MAX));
157157
}
158158

159-
// Apply to alignment to layout relative to the specified container width. If container_width is not
160-
// specified then the max line length is used.
161-
//
162-
// If `align_when_overflowing` is set to `true` then `Center` and `End` alignment will apply even if
163-
// the line contents are wider than the `container_width`. If it is set to `false` then all overflowing
164-
// lines will be `Start` aligned.
159+
/// Apply alignment to the layout relative to the specified container width or full layout
160+
/// width.
161+
///
162+
/// You must perform line breaking prior to aligning, through [`Layout::break_lines`] or
163+
/// [`Layout::break_all_lines`]. If `container_width` is not specified, the layout's
164+
/// [`Layout::full_width`] is used.
165+
///
166+
/// If `align_when_overflowing` is set to `true`, alignment will apply even if the line
167+
/// contents are wider than the alignment width. If it is set to `false`, all overflowing lines
168+
/// will be [`Alignment::Start`] aligned.
165169
pub fn align(
166170
&mut self,
167171
container_width: Option<f32>,

0 commit comments

Comments
 (0)