Lay out align and gather displays row by row

This commit is contained in:
gloamxun committed 2026-10-24 12:39:02 +08:00
1 parent 8c42f2de2f
commit cce656a407
7 files changed
+529 -19

No files matched your search

+207
View File
@@ -88,6 +88,11 @@ pub enum MathAtom {
},
/// A `\middle` delimiter; TeX sizes it with the enclosing `\left`/`\right`.
Middle(char),
/// Rows of an `align` (column-aligned) or `gather` (centred) display.
Aligned {
rows: Vec<AlignedRow>,
centered: bool,
},
StackLines(Vec<MathAtom>),
Stack {
top: Box<MathAtom>,
@@ -116,6 +121,132 @@ pub enum MathAtom {
},
}
/// One row of an `align`/`gather` display.
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct AlignedRow {
/// The cells between `&`; `gather` rows carry a single cell.
pub cells: Vec<MathAtom>,
/// The equation number rendered at the row's right edge.
pub tag: Option<String>,
}
impl AlignedRow {
pub fn new(cells: Vec<MathAtom>, tag: Option<String>) -> Self {
Self { cells, tag }
}
}
/// Geometry shared by the layout and painter of an `align`/`gather` display.
pub(crate) struct AlignedLayout {
pub boxes: Vec<Vec<MathBox>>,
pub column_widths: Vec<i64>,
pub column_starts: Vec<i64>,
pub tag_widths: Vec<i64>,
pub body_width: i64,
pub total_width: i64,
pub baselines: Vec<i64>,
pub first_height: i64,
pub height: i64,
pub depth: i64,
}
pub(crate) fn layout_aligned(
rows: &[AlignedRow],
centered: bool,
font: &Cmr10Metrics,
) -> AlignedLayout {
if rows.is_empty() {
return AlignedLayout {
boxes: Vec::new(),
column_widths: Vec::new(),
column_starts: Vec::new(),
tag_widths: Vec::new(),
body_width: 0,
total_width: 0,
baselines: Vec::new(),
first_height: 0,
height: 0,
depth: 0,
};
}
let boxes: Vec<Vec<MathBox>> = rows
.iter()
.map(|row| {
row.cells
.iter()
.map(|cell| layout_math(cell, font))
.collect()
})
.collect();
let columns = boxes.iter().map(Vec::len).max().unwrap_or(0);
let mut column_widths = vec![0i64; columns];
for row in &boxes {
for (index, cell) in row.iter().enumerate() {
column_widths[index] = column_widths[index].max(cell.width);
}
}
let mut column_starts = Vec::with_capacity(columns);
let mut pen = 0i64;
for width in &column_widths {
column_starts.push(pen);
pen += width;
}
let body_width = if centered {
boxes
.iter()
.flat_map(|row| row.iter())
.map(|cell| cell.width)
.max()
.unwrap_or(0)
} else {
pen
};
let tag_widths: Vec<i64> = rows
.iter()
.map(|row| {
row.tag
.as_ref()
.map(|tag| {
tag.chars()
.map(|character| Cmr10Metrics::Roman.width(character))
.sum()
})
.unwrap_or(0)
})
.collect();
let total_width = body_width.max(tag_widths.iter().copied().max().unwrap_or(0));
let row_heights: Vec<i64> = boxes
.iter()
.map(|row| row.iter().map(|cell| cell.height).max().unwrap_or(0))
.collect();
let row_depths: Vec<i64> = boxes
.iter()
.map(|row| row.iter().map(|cell| cell.depth).max().unwrap_or(0))
.collect();
let mut baselines = vec![0i64; rows.len()];
for index in 1..rows.len() {
let minimum = 12 * 65_536;
let touching = row_depths[index - 1] + row_heights[index] + 65_536;
baselines[index] = baselines[index - 1] + minimum.max(touching);
}
let first_height = row_heights.first().copied().unwrap_or(0);
let below = baselines.last().copied().unwrap_or(0) + row_depths.last().copied().unwrap_or(0);
let extent = first_height + below;
let axis = 2 * 65_536 + 32_768;
AlignedLayout {
boxes,
column_widths,
column_starts,
tag_widths,
body_width,
total_width,
baselines,
first_height,
height: extent / 2 + axis,
depth: (extent / 2 - axis).max(0),
}
}
fn fraction_bar_default() -> bool {
true
}
@@ -579,6 +710,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
}
}
MathAtom::Middle(delimiter) => layout_math(&MathAtom::Symbol(*delimiter), font),
MathAtom::Aligned { rows, centered } => {
let layout = layout_aligned(rows, *centered, font);
MathBox {
width: layout.total_width,
height: layout.height,
depth: layout.depth,
}
}
MathAtom::StackLines(rows) => {
let boxes: Vec<MathBox> = rows.iter().map(|atom| layout_math(atom, font)).collect();
MathBox {
@@ -1871,6 +2010,74 @@ mod tests {
);
}
#[test]
fn aligned_rows_share_their_column_widths() {
let font = Cmr10Metrics::default();
let rows = vec![
AlignedRow::new(
vec![
MathAtom::Text("a".to_string()),
MathAtom::Text("= x".to_string()),
],
None,
),
AlignedRow::new(
vec![
MathAtom::Text("long".to_string()),
MathAtom::Text("= y".to_string()),
],
None,
),
];
let layout = layout_aligned(&rows, false, &font);
assert_eq!(layout.column_widths.len(), 2);
assert_eq!(
layout.column_widths[0],
layout_math(&MathAtom::Text("long".to_string()), &font).width
);
assert_eq!(layout.body_width, layout.column_widths.iter().sum::<i64>());
assert!(layout.baselines.len() == 2 && layout.baselines[1] > 0);
}
#[test]
fn gather_rows_centre_their_cells() {
let font = Cmr10Metrics::default();
let rows = vec![
AlignedRow::new(vec![MathAtom::Text("a".to_string())], None),
AlignedRow::new(vec![MathAtom::Text("wide row".to_string())], None),
];
let layout = layout_aligned(&rows, true, &font);
assert_eq!(
layout.body_width,
layout_math(&MathAtom::Text("wide row".to_string()), &font).width
);
assert_eq!(layout.column_widths.len(), 1);
}
#[test]
fn aligned_rows_are_centred_on_the_math_axis() {
let font = Cmr10Metrics::default();
let single = layout_math(
&MathAtom::Aligned {
rows: vec![AlignedRow::new(vec![MathAtom::Text("a".to_string())], None)],
centered: false,
},
&font,
);
let two = layout_math(
&MathAtom::Aligned {
rows: vec![
AlignedRow::new(vec![MathAtom::Text("a".to_string())], None),
AlignedRow::new(vec![MathAtom::Text("b".to_string())], None),
],
centered: false,
},
&font,
);
assert!(two.height > single.height);
assert!(two.depth > single.depth);
}
#[test]
fn big_delimiters_allow_math_spaces() {
let font = Cmr10Metrics::default();