Set multline rows flush to the margins

This commit is contained in:
gloamxun committed 2026-10-26 00:46:22 +08:00
1 parent 4edc951958
commit 29d363d47a
6 files changed
+209 -10

No files matched your search

+89
View File
@@ -103,6 +103,11 @@ pub enum MathAtom {
left: Option<char>,
right: Option<char>,
},
/// Rows of a `multline`: first flush left, last flush right, middle centred.
Multline {
rows: Vec<MathAtom>,
tag: Option<String>,
},
/// Rows of an `align` (column-aligned) or `gather` (centred) display.
Aligned {
rows: Vec<AlignedRow>,
@@ -567,6 +572,62 @@ pub(crate) struct MatrixLayout {
pub depth: i64,
}
/// Geometry shared by the layout and painter of a `multline` environment.
pub(crate) struct MultlineLayout {
pub offsets: Vec<i64>,
pub body_width: i64,
pub width: i64,
pub baselines: Vec<i64>,
pub first_height: i64,
pub height: i64,
pub depth: i64,
}
/// Lay out the rows of a `multline`, with the last row flush right.
pub(crate) fn multline_layout(
rows: &[MathAtom],
tag: Option<&str>,
font: &Cmr10Metrics,
) -> MultlineLayout {
let boxes: Vec<MathBox> = rows.iter().map(|row| layout_math(row, font)).collect();
let body_width = boxes.iter().map(|row| row.width).max().unwrap_or(0);
let tag_width = tag
.map(|text| text.chars().map(|ch| Cmr10Metrics::Roman.width(ch)).sum())
.unwrap_or(0);
let mut baselines = vec![0_i64; boxes.len()];
for index in 1..boxes.len() {
let advance = (boxes[index - 1].depth + boxes[index].height + 2 * 65_536).max(655_360);
baselines[index] = baselines[index - 1] + advance;
}
let first_height = boxes.first().map(|row| row.height).unwrap_or(0);
let last_baseline = baselines.last().copied().unwrap_or(0);
let last_depth = boxes.last().map(|row| row.depth).unwrap_or(0);
let extent = first_height + last_baseline + last_depth;
let axis = 2 * 65_536 + 32_768;
let offsets: Vec<i64> = boxes
.iter()
.enumerate()
.map(|(index, row)| {
if index == 0 {
0
} else if index + 1 == boxes.len() {
body_width - row.width
} else {
(body_width - row.width) / 2
}
})
.collect();
MultlineLayout {
offsets,
body_width,
width: body_width + tag_width,
baselines,
first_height,
height: extent / 2 + axis,
depth: (extent / 2 - axis).max(0),
}
}
/// Lay out the rows of a matrix/array and the delimiters around them.
pub(crate) fn matrix_layout(
rows: &[Vec<MathAtom>],
@@ -920,6 +981,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: layout.depth,
}
}
MathAtom::Multline { rows, tag } => {
let layout = multline_layout(rows, tag.as_deref(), font);
MathBox {
width: layout.width,
height: layout.height,
depth: layout.depth,
}
}
MathAtom::Accent { base, .. } => {
let base_box = layout_math(base, font);
MathBox {
@@ -2608,6 +2677,26 @@ mod tests {
assert!(box_.height > 0);
}
#[test]
fn multline_rows_flush_to_the_edges() {
let font = Cmr10Metrics::default();
let rows = vec![
MathAtom::Symbol('a'),
MathAtom::Symbol('b'),
MathAtom::Row(vec![MathAtom::Symbol('c'), MathAtom::Symbol('d')]),
];
let layout = multline_layout(&rows, Some("(1)"), &font);
assert_eq!(layout.offsets[0], 0);
assert_eq!(
layout.offsets[2],
layout.body_width - layout_math(&rows[2], &font).width
);
assert_eq!(layout.offsets[2], 0);
assert!(layout.offsets[1] > 0);
let plain = multline_layout(&rows, None, &font);
assert!(layout.width > plain.width);
}
#[test]
fn matrix_environments_wrap_their_grid() {
let atom = parse_math("\\begin{pmatrix} a & b \\\\ c & d \\end{pmatrix}");
+29
View File
@@ -1091,6 +1091,35 @@ impl Page {
});
}
}
MathAtom::Multline { rows, tag } => {
let layout = math::multline_layout(rows, tag.as_deref(), font);
let first_baseline = baseline - layout.height + layout.first_height;
for (index, row) in rows.iter().enumerate() {
Self::paint_math(
row,
font,
x + layout.offsets[index],
first_baseline + layout.baselines[index],
size,
list,
);
}
if let Some(tag) = tag {
let row_baseline =
first_baseline + layout.baselines.last().copied().unwrap_or(0);
let mut tag_x = x + layout.body_width;
for character in tag.chars() {
list.push(DisplayItem::Text {
font: FontFace::Roman,
x: tag_x,
y: row_baseline,
size,
text: character.to_string(),
});
tag_x += Cmr10Metrics::Roman.width(character);
}
}
}
MathAtom::Accent { accent, base } => {
let base_box = math::layout_math(base, font);
Self::paint_math(base, font, x, baseline, size, list);