Skip to content

Commit dcb31b2

Browse files
authored
Use Layout::width instead of calculating for alignment (#269)
This does require line breaking to have happened, but that is already a requirement, as otherwise `Layout::lines` is empty. This is a slight change in behavior, as trailing white space is no longer included in the alignment width.
1 parent 76ab37d commit dcb31b2

5 files changed

+27
-31
lines changed

parley/src/layout/alignment.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,26 @@ 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(layout.width);
2719

2820
// Apply alignment to line items
2921
for line in &mut layout.lines {
3022
// TODO: remove this field
3123
line.alignment = alignment;
3224

25+
if is_rtl {
26+
// In RTL text, trailing whitespace is on the left. As we hang that whitespace, offset
27+
// the line to the left.
28+
line.metrics.offset = -line.metrics.trailing_whitespace;
29+
}
30+
3331
// Compute free space.
3432
let free_space = alignment_width - line.metrics.advance + line.metrics.trailing_whitespace;
3533

3634
if !align_when_overflowing && free_space <= 0.0 {
3735
if is_rtl {
3836
// In RTL text, right-align on overflow.
39-
line.metrics.offset = free_space;
37+
line.metrics.offset += free_space;
4038
}
4139
continue;
4240
}
@@ -46,10 +44,10 @@ pub(crate) fn align<B: Brush>(
4644
// Do nothing
4745
}
4846
(Alignment::Right, _) | (Alignment::Start, true) | (Alignment::End, false) => {
49-
line.metrics.offset = free_space;
47+
line.metrics.offset += free_space;
5048
}
5149
(Alignment::Middle, _) => {
52-
line.metrics.offset = free_space * 0.5;
50+
line.metrics.offset += free_space * 0.5;
5351
}
5452
(Alignment::Justified, _) => {
5553
// Justified alignment doesn't have any effect if free_space is negative or zero
@@ -62,7 +60,7 @@ pub(crate) fn align<B: Brush>(
6260
// case, start-align, i.e., left-align for LTR text and right-align for RTL text.
6361
if line.break_reason == BreakReason::None || line.num_spaces == 0 {
6462
if is_rtl {
65-
line.metrics.offset = free_space;
63+
line.metrics.offset += free_space;
6664
}
6765
continue;
6866
}
@@ -100,12 +98,6 @@ pub(crate) fn align<B: Brush>(
10098
});
10199
}
102100
}
103-
104-
if is_rtl {
105-
// In RTL text, trailing whitespace is on the left. As we hang that whitespace, offset
106-
// the line to the left.
107-
line.metrics.offset -= line.metrics.trailing_whitespace;
108-
}
109101
}
110102
}
111103

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::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>,
Loading
Loading
Loading

0 commit comments

Comments
 (0)