Stack substack rows with TeX inter-row gaps
This commit is contained in:
1 parent
0b611b6b1e
commit
8686b9a35e
6 files changed
+80
-12
No files matched your search
@@ -176,6 +176,43 @@ pub(crate) struct AlignedLayout {
|
||||
pub depth: i64,
|
||||
}
|
||||
|
||||
pub(crate) struct StackLayout {
|
||||
pub(crate) boxes: Vec<MathBox>,
|
||||
pub(crate) offsets: Vec<i64>,
|
||||
pub(crate) width: i64,
|
||||
pub(crate) height: i64,
|
||||
pub(crate) depth: i64,
|
||||
}
|
||||
|
||||
pub(crate) fn stack_layout(rows: &[MathAtom], font: &Cmr10Metrics) -> StackLayout {
|
||||
if rows.is_empty() {
|
||||
return StackLayout {
|
||||
boxes: Vec::new(),
|
||||
offsets: Vec::new(),
|
||||
width: 0,
|
||||
height: 0,
|
||||
depth: 0,
|
||||
};
|
||||
}
|
||||
let boxes: Vec<MathBox> = rows.iter().map(|row| layout_math(row, font)).collect();
|
||||
let width = boxes.iter().map(|box_| box_.width).max().unwrap_or(0);
|
||||
let mut offsets = vec![0_i64; boxes.len()];
|
||||
let mut offset = 0_i64;
|
||||
for index in 1..boxes.len() {
|
||||
offset += boxes[index - 1].depth + boxes[index].height + 65_536;
|
||||
offsets[index] = offset;
|
||||
}
|
||||
let height = boxes[0].height;
|
||||
let depth = *offsets.last().unwrap_or(&0) + boxes.last().map_or(0, |box_| box_.depth);
|
||||
StackLayout {
|
||||
boxes,
|
||||
offsets,
|
||||
width,
|
||||
height,
|
||||
depth,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn layout_aligned(
|
||||
rows: &[AlignedRow],
|
||||
centered: bool,
|
||||
@@ -1139,11 +1176,11 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
|
||||
}
|
||||
}
|
||||
MathAtom::StackLines(rows) => {
|
||||
let boxes: Vec<MathBox> = rows.iter().map(|atom| layout_math(atom, font)).collect();
|
||||
let layout = stack_layout(rows, font);
|
||||
MathBox {
|
||||
width: boxes.iter().map(|b| b.width).max().unwrap_or(0),
|
||||
height: boxes.iter().map(|b| b.height).sum(),
|
||||
depth: boxes.last().map_or(0, |b| b.depth),
|
||||
width: layout.width,
|
||||
height: layout.height,
|
||||
depth: layout.depth,
|
||||
}
|
||||
}
|
||||
MathAtom::Stack { top, base, above } => {
|
||||
@@ -2982,7 +3019,7 @@ mod tests {
|
||||
let font = Cmr10Metrics::default();
|
||||
let plain = layout_math(&parse_math("a"), &font);
|
||||
let stacked = layout_math(&parse_math(r"\substack{a\\b}"), &font);
|
||||
assert!(stacked.height > plain.height);
|
||||
assert!(stacked.height + stacked.depth > plain.height + plain.depth);
|
||||
}
|
||||
#[test]
|
||||
fn overbrace_accepts_a_superscript_label() {
|
||||
@@ -3145,6 +3182,25 @@ mod tests {
|
||||
let limited = layout_math(&parse_math(r"\mathop{argmax}_{x}"), &font);
|
||||
assert!(limited.depth > plain.depth);
|
||||
}
|
||||
#[test]
|
||||
fn substack_rows_stack_downward() {
|
||||
let atom = parse_math(r"\substack{a \\ b}");
|
||||
let MathAtom::Row(mut atoms) = atom else {
|
||||
panic!("expected a row");
|
||||
};
|
||||
assert_eq!(atoms.len(), 1);
|
||||
let MathAtom::StackLines(rows) = atoms.remove(0) else {
|
||||
panic!("expected StackLines");
|
||||
};
|
||||
assert_eq!(rows.len(), 2);
|
||||
let box_ = layout_math(&MathAtom::StackLines(rows), &Cmr10Metrics::default());
|
||||
assert!(box_.width > 0);
|
||||
assert!(
|
||||
box_.depth > box_.height,
|
||||
"second row should sit below the baseline"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn genfrac_builds_fractions_with_optional_delimiters() {
|
||||
let font = Cmr10Metrics::default();
|
||||
|
||||
@@ -1205,9 +1205,13 @@ impl Page {
|
||||
}
|
||||
}
|
||||
MathAtom::StackLines(lines) => {
|
||||
let height = 12 * 65_536;
|
||||
let layout = crate::math::stack_layout(lines, font);
|
||||
let scale = |value: i64| value.saturating_mul(size) / 655_360;
|
||||
for (index, line) in lines.iter().enumerate() {
|
||||
Self::paint_math(line, font, x, baseline + index as i64 * height, size, list);
|
||||
let box_ = &layout.boxes[index];
|
||||
let row_x = x + scale((layout.width - box_.width).max(0)) / 2;
|
||||
let row_y = baseline + scale(layout.offsets[index]);
|
||||
Self::paint_math(line, font, row_x, row_y, size, list);
|
||||
}
|
||||
}
|
||||
MathAtom::Stack { top, base, above } => {
|
||||
|
||||
Reference in new issue
Block a user