use super::font::MathFonts;
use super::mlist;
use super::noad::{Atom, Class, Field, Noad, Scaled, Style};
use crate::glue::Glue;
use crate::node::{BoxNode, GlueNode, Node};
use crate::pack::{hpack, Spec, Tolerances, NATURAL};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Align {
Right,
Centre,
Left,
}
#[derive(Clone, Copy, Debug)]
pub struct Column {
pub align: Align,
pub gap: Scaled,
pub empty_ord: bool,
}
const ARRAY_COL_SEP: Scaled = 5 * 65536;
const MIN_ALIGN_SEP: Scaled = 10 * 65536;
pub fn columns(environment: &str, n: usize) -> Vec<Column> {
let pattern: &[Align] = match environment {
"align" | "align*" | "aligned" | "alignat" | "alignat*" | "flalign" | "flalign*" => {
&[Align::Right, Align::Left]
}
"eqnarray" | "eqnarray*" => &[Align::Right, Align::Centre, Align::Left],
_ => &[Align::Centre],
};
let pairs = matches!(pattern, [Align::Right, Align::Left]);
(0..n)
.map(|j| {
let align = pattern[j % pattern.len()];
Column {
align,
gap: match (j, pairs) {
(0, _) => 0,
(_, true) if j % 2 == 0 => MIN_ALIGN_SEP,
(_, true) => 0,
_ => 2 * ARRAY_COL_SEP,
},
empty_ord: align == Align::Left,
}
})
.collect()
}
fn fil() -> Node {
Node::Glue(GlueNode::new(Glue {
natural: 0,
stretch: 65536,
stretch_order: 1,
shrink: 0,
shrink_order: 0,
}))
}
pub fn rows(
fonts: &MathFonts,
rows: &[Vec<Vec<Noad>>],
style: Style,
columns: &[Column],
) -> Vec<BoxNode> {
let entries: Vec<Vec<BoxNode>> = rows
.iter()
.map(|row| {
row.iter()
.enumerate()
.map(|(j, cell)| {
let empty = columns.get(j).map(|c| c.empty_ord).unwrap_or(false);
if !empty {
return mlist::set(fonts, cell, style);
}
let mut list = Vec::with_capacity(cell.len() + 1);
list.push(Noad::Atom(Atom::new(Class::Ord, Field::List(Vec::new()))));
list.extend(cell.iter().cloned());
mlist::set(fonts, &list, style)
})
.collect()
})
.collect();
let count = entries.iter().map(|r| r.len()).max().unwrap_or(0);
let widths: Vec<Scaled> = (0..count)
.map(|j| {
entries
.iter()
.filter_map(|row| row.get(j))
.map(|b| b.width)
.max()
.unwrap_or(0)
})
.collect();
entries
.into_iter()
.map(|row| {
let mut row = row.into_iter();
let mut list: Vec<Node> = Vec::new();
for (j, width) in widths.iter().enumerate() {
let column = columns.get(j).copied().unwrap_or(Column {
align: Align::Centre,
gap: 0,
empty_ord: false,
});
if column.gap != 0 {
list.push(Node::Kern {
width: column.gap,
explicit: true,
});
}
let entry = row.next().unwrap_or_else(BoxNode::null);
list.push(Node::Box(in_column(entry, *width, column.align)));
}
hpack(list, NATURAL, Tolerances::plain(), None).node
})
.collect()
}
fn in_column(entry: BoxNode, width: Scaled, align: Align) -> BoxNode {
let inner = vec![Node::Box(entry)];
let list = match align {
Align::Right => {
let mut l = vec![fil()];
l.extend(inner);
l
}
Align::Left => {
let mut l = inner;
l.push(fil());
l
}
Align::Centre => {
let mut l = vec![fil()];
l.extend(inner);
l.push(fil());
l
}
};
hpack(list, Spec::Exactly(width), Tolerances::plain(), None).node
}
pub fn numbered(
fonts: &MathFonts,
body: BoxNode,
number: BoxNode,
z: Scaled,
left: bool,
) -> BoxNode {
let mut body = body;
let mut w = body.width;
let mut e = number.width;
let q = match e {
0 => 0,
_ => e + fonts.math_quad(super::font::TEXT_SIZE),
};
if w + q > z {
e = 0;
if w > z {
body = hpack(
vec![Node::Box(body)],
Spec::Exactly(z),
Tolerances::plain(),
None,
)
.node;
w = body.width;
}
}
let mut d = half(z - w);
if e > 0 && d < 2 * e {
d = half(z - w - e);
}
let mut list: Vec<Node> = Vec::new();
match (e != 0, left) {
(true, true) => {
list.push(Node::Box(number));
list.push(Node::Kern {
width: z - w - e - d,
explicit: true,
});
list.push(Node::Box(body));
}
(true, false) => {
list.push(Node::Kern {
width: d,
explicit: true,
});
list.push(Node::Box(body));
list.push(Node::Kern {
width: z - w - e - d,
explicit: true,
});
list.push(Node::Box(number));
}
(false, _) => {
list.push(Node::Kern {
width: d,
explicit: true,
});
list.push(Node::Box(body));
if number.width > 0 {
let rest = z - d - w - number.width;
list.push(Node::Kern {
width: rest,
explicit: true,
});
list.push(Node::Box(number));
}
}
}
hpack(list, Spec::Exactly(z), Tolerances::plain(), None).node
}
fn half(x: Scaled) -> Scaled {
match x % 2 == 0 {
true => x / 2,
false => (x + 1) / 2,
}
}