use super::{EqLayout, EqNode, LayoutBox, LayoutKind, SpaceKind};
fn items(row: &EqNode) -> &[EqNode] {
match row.ungrouped() {
EqNode::Row(items) => items,
_ => std::slice::from_ref(row),
}
}
fn is_boundary(node: &EqNode) -> bool {
matches!(node, EqNode::Space(SpaceKind::Tab))
}
impl EqLayout {
pub(super) fn layout_cases_rows(&self, rows: &[EqNode], fs: f64) -> Vec<LayoutBox> {
if !rows.iter().any(|row| items(row).iter().any(is_boundary)) {
return rows
.iter()
.map(|row| self.matrix_row_box(self.layout_node(row, fs), fs))
.collect();
}
let mut cells: Vec<Vec<LayoutBox>> = rows
.iter()
.map(|row| {
items(row)
.split(is_boundary)
.map(|column| self.layout_row(column, fs))
.collect()
})
.collect();
let column_count = cells.iter().map(Vec::len).max().unwrap_or(0);
let mut widths = vec![0.0_f64; column_count];
for row in &cells {
for (column, cell) in row.iter().enumerate() {
widths[column] = widths[column].max(cell.width);
}
}
for width in &mut widths {
if *width == 0.0 {
*width = fs * 0.5;
}
}
let gap = fs * 0.2;
let mut origins = Vec::with_capacity(column_count);
let mut x = 0.0;
for width in widths {
origins.push(x);
x += width + gap;
}
cells
.iter_mut()
.map(|row| {
let frames: Vec<_> = row
.iter()
.map(|cell| self.matrix_cell_frame(cell, fs))
.collect();
let ascent = frames
.iter()
.map(|(_, baseline)| *baseline)
.fold(0.0_f64, f64::max);
let descent = frames
.iter()
.map(|(height, baseline)| height - baseline)
.fold(0.0_f64, f64::max);
let mut width = 0.0_f64;
for (column, cell) in row.iter_mut().enumerate() {
cell.x = origins[column];
cell.y = ascent - cell.baseline;
width = width.max(cell.x + cell.width);
}
LayoutBox {
font_size: fs,
x: 0.0,
y: 0.0,
width,
height: ascent + descent,
baseline: ascent,
kind: LayoutKind::Row(std::mem::take(row)),
}
})
.collect()
}
}