blob: 4e4a76e97a352e8ca7abfda0d096d0b5e89c13bb (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
use super::*;
/// A math alignment point: `&`, `&&`.
///
/// Display: Alignment Point
/// Category: math
#[element(LayoutMath)]
pub struct AlignPointElem {}
impl LayoutMath for AlignPointElem {
#[tracing::instrument(skip(ctx))]
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
ctx.push(MathFragment::Align);
Ok(())
}
}
pub(super) struct AlignmentResult {
pub points: Vec<Abs>,
pub width: Abs,
}
/// Determine the position of the alignment points.
pub(super) fn alignments(rows: &[MathRow]) -> AlignmentResult {
let mut widths = Vec::<Abs>::new();
let mut pending_width = Abs::zero();
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);
pending_width = Abs::zero();
}
width = Abs::zero();
alignment_index += 1;
} else {
width += fragment.width();
}
}
pending_width.set_max(width);
}
let mut points = widths;
for i in 1..points.len() {
let prev = points[i - 1];
points[i] += prev;
}
AlignmentResult {
width: points.last().copied().unwrap_or_default() + pending_width,
points,
}
}
|