Set multline rows flush to the margins
This commit is contained in:
1 parent
4edc951958
commit
29d363d47a
6 files changed
+209
-10
No files matched your search
@@ -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}");
|
||||
|
||||
Reference in new issue
Block a user