typst/library/src/math/align.rs
2023-03-19 22:39:19 +01:00

50 lines
1.2 KiB
Rust

use super::*;
/// A math alignment point: `&`, `&&`.
///
/// Display: Alignment Point
/// Category: math
#[element(LayoutMath)]
pub struct AlignPointElem {}
impl LayoutMath for AlignPointElem {
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
ctx.push(MathFragment::Align);
Ok(())
}
}
/// 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 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;
}
x += fragment.width();
}
}
}
points
}