From 17f8939f8631104737b928ddeebb401134da1f6e Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 29 Jan 2023 17:44:58 +0100 Subject: [PATCH] Fix math alignment points --- library/src/math/row.rs | 16 +++++++++++++--- library/src/math/stack.rs | 6 ++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/library/src/math/row.rs b/library/src/math/row.rs index bc0131828..f35d51c85 100644 --- a/library/src/math/row.rs +++ b/library/src/math/row.rs @@ -86,7 +86,7 @@ impl MathRow { let points = alignments(&rows); for (i, row) in rows.into_iter().enumerate() { let size = frame.size_mut(); - let sub = row.to_line_frame(ctx, &points); + let sub = row.to_line_frame(ctx, &points, Align::Center); if i > 0 { size.y += leading; } @@ -97,11 +97,11 @@ impl MathRow { } frame } else { - self.to_line_frame(ctx, &[]) + self.to_line_frame(ctx, &[], Align::Center) } } - pub fn to_line_frame(self, ctx: &MathContext, points: &[Abs]) -> Frame { + pub fn to_line_frame(self, ctx: &MathContext, points: &[Abs], align: Align) -> Frame { let ascent = self.0.iter().map(MathFragment::ascent).max().unwrap_or_default(); let descent = self.0.iter().map(MathFragment::descent).max().unwrap_or_default(); @@ -110,6 +110,16 @@ impl MathRow { let mut x = Abs::zero(); frame.set_baseline(ascent); + if let (Some(&first), Align::Center) = (points.first(), align) { + let segment: Abs = self + .0 + .iter() + .take_while(|fragment| !matches!(fragment, MathFragment::Align)) + .map(|fragment| fragment.width()) + .sum(); + x = first - segment; + } + let mut fragments = self.0.into_iter().peekable(); let mut i = 0; while let Some(fragment) = fragments.next() { diff --git a/library/src/math/stack.rs b/library/src/math/stack.rs index c8a1252c0..ec233cd91 100644 --- a/library/src/math/stack.rs +++ b/library/src/math/stack.rs @@ -290,8 +290,10 @@ pub(super) fn stack( let mut height = rows.len().saturating_sub(1) as f64 * gap; let points = alignments(&rows); - let rows: Vec<_> = - rows.into_iter().map(|row| row.to_line_frame(ctx, &points)).collect(); + let rows: Vec<_> = rows + .into_iter() + .map(|row| row.to_line_frame(ctx, &points, align)) + .collect(); for row in &rows { height += row.height();