summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--library/src/math/align.rs42
-rw-r--r--tests/ref/math/alignment.pngbin0 -> 9752 bytes
-rw-r--r--tests/typ/math/alignment.typ28
3 files changed, 47 insertions, 23 deletions
diff --git a/library/src/math/align.rs b/library/src/math/align.rs
index d34379e2..03abeac2 100644
--- a/library/src/math/align.rs
+++ b/library/src/math/align.rs
@@ -16,34 +16,30 @@ impl LayoutMath for AlignPointElem {
/// Determine the position of the alignment points.
pub(super) fn alignments(rows: &[MathRow]) -> Vec<Abs> {
- let count = rows
- .iter()
- .map(|row| {
- row.iter()
- .filter(|fragment| matches!(fragment, MathFragment::Align))
- .count()
- })
- .max()
- .unwrap_or(0);
+ let mut widths = Vec::<Abs>::new();
- let mut points = vec![Abs::zero(); count];
- for current in 0..count {
- for row in rows {
- let mut x = Abs::zero();
- let mut i = 0;
- for fragment in row.iter() {
- if matches!(fragment, MathFragment::Align) {
- if i < current {
- x = points[i];
- } else if i == current {
- points[i].set_max(x);
- }
- i += 1;
+ for row in rows {
+ let mut width = Abs::zero();
+ let mut alignment_index = 0;
+ for fragment in row.iter() {
+ if matches!(fragment, MathFragment::Align) {
+ if alignment_index < widths.len() {
+ widths[alignment_index].set_max(width);
+ } else {
+ widths.push(width);
}
- x += fragment.width();
+ width = Abs::zero();
+ alignment_index += 1;
+ } else {
+ width += fragment.width();
}
}
}
+ let mut points = widths;
+ for i in 1..points.len() {
+ let prev = points[i - 1];
+ points[i] += prev;
+ }
points
}
diff --git a/tests/ref/math/alignment.png b/tests/ref/math/alignment.png
new file mode 100644
index 00000000..45b0cd86
--- /dev/null
+++ b/tests/ref/math/alignment.png
Binary files differ
diff --git a/tests/typ/math/alignment.typ b/tests/typ/math/alignment.typ
new file mode 100644
index 00000000..8482d89e
--- /dev/null
+++ b/tests/typ/math/alignment.typ
@@ -0,0 +1,28 @@
+// Test implicit alignment math.
+
+---
+// Test alignment step functions.
+#set page(width: 300pt)
+$
+"abc" &= c \
+&= c + 1 & "By definition" \
+&= d + 100 + 1000 \
+&= x && "Even longer" \
+$
+
+---
+// Test post-fix alignment.
+#set page(width: 300pt)
+$
+& "right" \
+"a very long line" \
+$
+
+---
+// Test alternating alignment.
+#set page(width: 300pt)
+$
+"abc" & "abc abc abc" & "abc abc" \
+"abc abc" & "abc abc" & "abc" \
+"abc abc abc" & "abc" & "abc abc abc" \
+$