Fix math alignment points

This commit is contained in:
Laurenz 2023-01-29 17:44:58 +01:00
parent 60dfe8f893
commit 17f8939f86
2 changed files with 17 additions and 5 deletions

View File

@ -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() {

View File

@ -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();