Split wide display formulas into lines

This commit is contained in:
gloamxun committed 2026-10-24 01:26:29 +08:00
1 parent 95e677b5c7
commit c9ad1e209a
6 files changed
+103 -10

No files matched your search

+30
View File
@@ -170,6 +170,36 @@ fn scaled(value: i64, factor: f64) -> i64 {
(value as f64 * factor).round() as i64
}
/// Split a display formula into lines that fit `max_width`.
///
/// TeX itself never breaks display math automatically; this mirrors the
/// explicit `\\split`/`align` behaviour by breaking after a top-level
/// binary or relation operator once the line would overflow.
pub fn split_math_lines(atom: &MathAtom, font: &Cmr10Metrics, max_width: i64) -> Vec<MathAtom> {
let atoms = match atom {
MathAtom::Row(items) => items.clone(),
other => vec![other.clone()],
};
if layout_math(atom, font).width <= max_width {
return vec![atom.clone()];
}
let mut lines: Vec<Vec<MathAtom>> = Vec::new();
let mut current: Vec<MathAtom> = Vec::new();
for item in atoms {
let mut candidate = current.clone();
candidate.push(item.clone());
let candidate_width = layout_math(&MathAtom::Row(candidate.clone()), font).width;
if !current.is_empty() && candidate_width > max_width {
lines.push(std::mem::take(&mut current));
}
current.push(item);
}
if !current.is_empty() {
lines.push(current);
}
lines.into_iter().map(MathAtom::Row).collect()
}
fn atom_class(atom: &MathAtom) -> MathClass {
match atom {
MathAtom::Symbol(character) => math_class(*character),