Support implicit alignment (#749)

This commit is contained in:
Alex Saveau 2023-04-25 02:22:32 -07:00 committed by GitHub
parent f38989358e
commit fb99090208
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 4 deletions

View File

@ -28,13 +28,13 @@ pub(super) fn alignments(rows: &[MathRow]) -> AlignmentResult {
for row in rows { for row in rows {
let mut width = Abs::zero(); let mut width = Abs::zero();
let mut alignment_index = 0; let mut alignment_index = 0;
for fragment in row.iter() { for fragment in row.iter() {
if matches!(fragment, MathFragment::Align) { if matches!(fragment, MathFragment::Align) {
if alignment_index < widths.len() { if alignment_index < widths.len() {
widths[alignment_index].set_max(width); widths[alignment_index].set_max(width);
} else { } else {
widths.push(width); widths.push(width.max(pending_width));
pending_width = Abs::zero();
} }
width = Abs::zero(); width = Abs::zero();
alignment_index += 1; alignment_index += 1;
@ -42,7 +42,13 @@ pub(super) fn alignments(rows: &[MathRow]) -> AlignmentResult {
width += fragment.width(); width += fragment.width();
} }
} }
if widths.is_empty() {
pending_width.set_max(width); pending_width.set_max(width);
} else if alignment_index < widths.len() {
widths[alignment_index].set_max(width);
} else {
widths.push(width.max(pending_width));
}
} }
let mut points = widths; let mut points = widths;
@ -51,7 +57,7 @@ pub(super) fn alignments(rows: &[MathRow]) -> AlignmentResult {
points[i] += prev; points[i] += prev;
} }
AlignmentResult { AlignmentResult {
width: points.last().copied().unwrap_or_default() + pending_width, width: points.last().copied().unwrap_or(pending_width),
points, points,
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.3 KiB

After

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -2,6 +2,7 @@
--- ---
// Test alignment step functions. // Test alignment step functions.
#set page(width: 225pt)
$ $
"a" &= c \ "a" &= c \
&= c + 1 & "By definition" \ &= c + 1 & "By definition" \