use super::xml::{MATH_NS, WORD_NS, attr, child, children, is};
use crate::analysis::math::{Alphabet, styled};
use crate::model::MathNode;
use roxmltree::Node;
const UNDER_OVER_OPERATORS: &[char] = &['∑', '∏', '∐', '⋃', '⋂', '⋁', '⋀', '⨁', '⨂', '⨀', '⨄', '⨆'];
const DEFAULT_NARY: &str = "∫";
const NARY_HEIGHT_EM: f32 = 2.2;
const LINE_HEIGHT_EM: f32 = 1.0;
const RAISE_EM: f32 = 0.3;
const PLAIN_DELIMITER_MAX_EM: f32 = 1.4;
const SPACE_EM: f32 = 0.25;
pub(crate) fn formula(node: Node<'_, '_>) -> MathNode {
if is(node, MATH_NS, "oMathPara") {
let lines: Vec<MathNode> = children(node, MATH_NS, "oMath").map(argument).collect();
return match lines.len() {
0 => MathNode::Row(Vec::new()),
1 => lines.into_iter().next().expect("one line"),
_ => MathNode::Table {
rows: lines.into_iter().map(|line| vec![line]).collect(),
align: vec!["center"],
},
};
}
argument(node)
}
pub(crate) fn plain_text(node: &MathNode) -> String {
let mut out = String::new();
write_plain(node, &mut out);
out.trim().to_string()
}
fn argument(node: Node<'_, '_>) -> MathNode {
let mut nodes = Vec::new();
for item in node.children().filter(Node::is_element) {
push_element(item, &mut nodes);
}
MathNode::row(nodes)
}
fn arg(node: Node<'_, '_>, name: &str) -> MathNode {
child(node, MATH_NS, name).map_or(MathNode::Row(Vec::new()), argument)
}
fn opt_arg(node: Node<'_, '_>, name: &str) -> Option<Box<MathNode>> {
let arg = child(node, MATH_NS, name).map(argument)?;
(!is_empty(&arg)).then(|| Box::new(arg))
}
fn prop<'a>(node: Node<'a, '_>, props: &str, prop: &str) -> Option<&'a str> {
attr(child(child(node, MATH_NS, props)?, MATH_NS, prop)?, MATH_NS, "val")
}
fn flag(node: Node<'_, '_>, props: &str, prop: &str) -> bool {
child(node, MATH_NS, props)
.and_then(|props| child(props, MATH_NS, prop))
.is_some_and(|flag| !matches!(attr(flag, MATH_NS, "val"), Some("0" | "off" | "false")))
}
fn push_element(node: Node<'_, '_>, out: &mut Vec<MathNode>) {
let ns = node.tag_name().namespace();
if ns == Some(WORD_NS) {
return push_word_element(node, out);
}
if ns != Some(MATH_NS) {
return;
}
let name = node.tag_name().name();
if name.ends_with("Pr") {
return;
}
match name {
"r" => push_run(node, out),
"f" => out.push(fraction(node)),
"rad" => out.push(radical(node)),
"sSub" => out.push(scripts(arg(node, "e"), opt_arg(node, "sub"), None)),
"sSup" => out.push(scripts(arg(node, "e"), None, opt_arg(node, "sup"))),
"sSubSup" => out.push(scripts(arg(node, "e"), opt_arg(node, "sub"), opt_arg(node, "sup"))),
"sPre" => {
out.push(scripts(
MathNode::Row(Vec::new()),
opt_arg(node, "sub"),
opt_arg(node, "sup"),
));
out.push(arg(node, "e"));
}
"nary" => nary(node, out),
"d" => delimited(node, out),
"func" => {
out.push(upright(arg(node, "fName")));
out.push(arg(node, "e"));
}
"acc" => {
let mark = prop(node, "accPr", "chr").unwrap_or("\u{302}");
out.push(over(arg(node, "e"), mark, true));
}
"bar" => {
let top = prop(node, "barPr", "pos") == Some("top");
out.push(if top {
over(arg(node, "e"), "\u{305}", true)
} else {
under(arg(node, "e"), "_")
});
}
"groupChr" => {
let top = prop(node, "groupChrPr", "pos") == Some("top");
let mark = prop(node, "groupChrPr", "chr").unwrap_or("⏟");
out.push(if top {
over(arg(node, "e"), mark, false)
} else {
under(arg(node, "e"), mark)
});
}
"limLow" => out.push(MathNode::UnderOver {
base: Box::new(arg(node, "e")),
under: opt_arg(node, "lim"),
over: None,
accent: false,
}),
"limUpp" => out.push(MathNode::UnderOver {
base: Box::new(arg(node, "e")),
under: None,
over: opt_arg(node, "lim"),
accent: false,
}),
"m" => out.push(matrix(node)),
"eqArr" => out.push(equation_array(node)),
"phant" => {}
"oMath" | "oMathPara" => out.push(formula(node)),
_ => out.push(argument(node)),
}
}
fn push_word_element(node: Node<'_, '_>, out: &mut Vec<MathNode>) {
match node.tag_name().name() {
"r" => {
let text = super::xml::run_text(node);
if !text.trim().is_empty() {
out.push(MathNode::Text(text));
}
}
"ins" | "smartTag" | "customXml" | "sdt" | "sdtContent" | "hyperlink" => {
node.children()
.filter(Node::is_element)
.for_each(|item| push_element(item, out));
}
_ => {}
}
}
fn fraction(node: Node<'_, '_>) -> MathNode {
let (num, den) = (arg(node, "num"), arg(node, "den"));
match prop(node, "fPr", "type") {
Some("lin") | Some("skw") => MathNode::Row(vec![num, operator("/"), den]),
_ => MathNode::Frac {
num: Box::new(num),
den: Box::new(den),
},
}
}
fn radical(node: Node<'_, '_>) -> MathNode {
let base = Box::new(arg(node, "e"));
match opt_arg(node, "deg").filter(|_| !flag(node, "radPr", "degHide")) {
Some(index) => MathNode::Root { base, index },
None => MathNode::Sqrt(base),
}
}
fn scripts(base: MathNode, sub: Option<Box<MathNode>>, sup: Option<Box<MathNode>>) -> MathNode {
if sub.is_none() && sup.is_none() {
return base;
}
MathNode::Scripts {
base: Box::new(base),
sub,
sup,
}
}
fn nary(node: Node<'_, '_>, out: &mut Vec<MathNode>) {
let symbol = prop(node, "naryPr", "chr").unwrap_or(DEFAULT_NARY).to_string();
let lower = opt_arg(node, "sub").filter(|_| !flag(node, "naryPr", "subHide"));
let upper = opt_arg(node, "sup").filter(|_| !flag(node, "naryPr", "supHide"));
let under_over = match prop(node, "naryPr", "limLoc") {
Some(location) => location == "undOvr",
None => symbol
.chars()
.next()
.is_some_and(|ch| UNDER_OVER_OPERATORS.contains(&ch)),
};
let base = Box::new(operator(&symbol));
out.push(if lower.is_none() && upper.is_none() {
*base
} else if under_over {
MathNode::UnderOver {
base,
under: lower,
over: upper,
accent: false,
}
} else {
MathNode::Scripts {
base,
sub: lower,
sup: upper,
}
});
let operand = arg(node, "e");
if !is_empty(&operand) {
out.push(operand);
}
}
fn delimited(node: Node<'_, '_>, out: &mut Vec<MathNode>) {
let begin = prop(node, "dPr", "begChr").unwrap_or("(");
let end = prop(node, "dPr", "endChr").unwrap_or(")");
let separator = prop(node, "dPr", "sepChr").unwrap_or("|");
let parts: Vec<MathNode> = children(node, MATH_NS, "e").map(argument).collect();
let tallest = parts.iter().map(height_em).fold(LINE_HEIGHT_EM, f32::max);
let height = (tallest > PLAIN_DELIMITER_MAX_EM).then_some(tallest);
let delimiter = |text: &str| MathNode::Operator {
text: text.to_string(),
height_em: height,
};
if !begin.is_empty() {
out.push(delimiter(begin));
}
for (i, part) in parts.into_iter().enumerate() {
if i > 0 {
out.push(delimiter(separator));
}
out.push(part);
}
if !end.is_empty() {
out.push(delimiter(end));
}
}
fn matrix(node: Node<'_, '_>) -> MathNode {
let rows: Vec<Vec<MathNode>> = children(node, MATH_NS, "mr")
.map(|row| children(row, MATH_NS, "e").map(argument).collect())
.collect();
let n_cols = rows.iter().map(Vec::len).max().unwrap_or(0);
let mut align: Vec<&'static str> = child(node, MATH_NS, "mPr")
.and_then(|props| child(props, MATH_NS, "mcs"))
.map(|columns| {
children(columns, MATH_NS, "mc")
.flat_map(|column| {
let justify = prop(column, "mcPr", "mcJc");
let count = prop(column, "mcPr", "count")
.and_then(|count| count.parse::<usize>().ok())
.unwrap_or(1);
std::iter::repeat_n(alignment(justify), count)
})
.collect()
})
.unwrap_or_default();
align.resize(n_cols, "center");
MathNode::Table { rows, align }
}
fn equation_array(node: Node<'_, '_>) -> MathNode {
let rows: Vec<Vec<MathNode>> = children(node, MATH_NS, "e")
.map(|line| split_at_marks(argument(line)))
.collect();
let n_cols = rows.iter().map(Vec::len).max().unwrap_or(1);
let align = (0..n_cols)
.map(|col| match (n_cols, col % 2) {
(1, _) => "center",
(_, 0) => "right",
_ => "left",
})
.collect();
MathNode::Table { rows, align }
}
fn split_at_marks(line: MathNode) -> Vec<MathNode> {
let nodes = match line {
MathNode::Row(nodes) => nodes,
other => vec![other],
};
let mut cells = vec![Vec::new()];
for node in nodes {
match node {
MathNode::Operator { ref text, .. } if text == "&" => cells.push(Vec::new()),
other => cells.last_mut().expect("one cell at least").push(other),
}
}
cells.into_iter().map(MathNode::row).collect()
}
fn alignment(justify: Option<&str>) -> &'static str {
match justify {
Some("left") => "left",
Some("right") => "right",
_ => "center",
}
}
fn over(base: MathNode, mark: &str, accent: bool) -> MathNode {
MathNode::UnderOver {
base: Box::new(base),
under: None,
over: Some(Box::new(operator(mark))),
accent,
}
}
fn under(base: MathNode, mark: &str) -> MathNode {
MathNode::UnderOver {
base: Box::new(base),
under: Some(Box::new(operator(mark))),
over: None,
accent: false,
}
}
fn operator(text: &str) -> MathNode {
MathNode::Operator {
text: text.to_string(),
height_em: None,
}
}
fn upright(node: MathNode) -> MathNode {
match node {
MathNode::Ident { text, .. } => MathNode::Ident { text, upright: true },
MathNode::Row(nodes) => {
if nodes.iter().all(|node| matches!(node, MathNode::Ident { .. })) && nodes.len() > 1 {
let text = nodes
.iter()
.map(|node| match node {
MathNode::Ident { text, .. } => text.as_str(),
_ => "",
})
.collect();
return MathNode::Ident { text, upright: true };
}
MathNode::Row(nodes.into_iter().map(upright).collect())
}
other => other,
}
}
fn push_run(node: Node<'_, '_>, out: &mut Vec<MathNode>) {
let text: String = node
.descendants()
.filter(|item| is(*item, MATH_NS, "t") || is(*item, WORD_NS, "t"))
.filter_map(|item| item.text())
.collect();
if text.is_empty() {
return;
}
let props = child(node, MATH_NS, "rPr");
let normal = props.and_then(|props| child(props, MATH_NS, "nor")).is_some();
if normal {
out.push(MathNode::Text(text));
return;
}
let style = props
.and_then(|props| child(props, MATH_NS, "sty"))
.and_then(|sty| attr(sty, MATH_NS, "val"));
let script = props
.and_then(|props| child(props, MATH_NS, "scr"))
.and_then(|scr| attr(scr, MATH_NS, "val"));
let alphabet = match (script, style) {
(Some("double-struck"), _) => Alphabet::DoubleStruck,
(Some("script"), _) => Alphabet::Script,
(Some("fraktur"), _) => Alphabet::Fraktur,
(_, Some("b")) => Alphabet::Bold,
(_, Some("bi")) => Alphabet::BoldItalic,
_ => Alphabet::Plain,
};
let straight = matches!(style, Some("p" | "b")) && alphabet == Alphabet::Plain;
tokenize(&styled(&text, alphabet), straight, out);
}
fn tokenize(text: &str, straight: bool, out: &mut Vec<MathNode>) {
let chars: Vec<char> = text.chars().collect();
let mut i = 0;
while i < chars.len() {
let ch = chars[i];
if ch.is_ascii_digit() {
let start = i;
while i < chars.len()
&& (chars[i].is_ascii_digit()
|| (chars[i] == '.' && chars.get(i + 1).is_some_and(char::is_ascii_digit)))
{
i += 1;
}
out.push(MathNode::Number(chars[start..i].iter().collect()));
continue;
}
if ch.is_alphabetic() {
if straight {
let start = i;
while i < chars.len() && chars[i].is_alphabetic() {
i += 1;
}
out.push(MathNode::Ident {
text: chars[start..i].iter().collect(),
upright: true,
});
continue;
}
out.push(MathNode::Ident {
text: ch.to_string(),
upright: false,
});
} else if ch.is_whitespace() {
out.push(MathNode::Space(SPACE_EM));
} else {
out.push(operator(&ch.to_string()));
}
i += 1;
}
}
fn is_empty(node: &MathNode) -> bool {
match node {
MathNode::Row(nodes) => nodes.iter().all(is_empty),
_ => false,
}
}
fn height_em(node: &MathNode) -> f32 {
match node {
MathNode::Row(nodes) => nodes.iter().map(height_em).fold(LINE_HEIGHT_EM, f32::max),
MathNode::Frac { num, den } => height_em(num) + height_em(den) + RAISE_EM,
MathNode::Sqrt(base) | MathNode::Root { base, .. } => height_em(base) + RAISE_EM,
MathNode::Scripts { base, sub, sup } => {
height_em(base) + if sub.is_some() || sup.is_some() { RAISE_EM } else { 0.0 }
}
MathNode::UnderOver { base, under, over, .. } => {
let base_height = match base.as_ref() {
MathNode::Operator { text, .. } if text.chars().count() == 1 && !under.is_none() => NARY_HEIGHT_EM,
other => height_em(other),
};
base_height + [under, over].iter().filter(|part| part.is_some()).count() as f32 * RAISE_EM * 2.0
}
MathNode::Table { rows, .. } => rows
.iter()
.map(|row| row.iter().map(height_em).fold(LINE_HEIGHT_EM, f32::max))
.sum(),
MathNode::Operator { text, .. } if text.chars().next().is_some_and(|ch| UNDER_OVER_OPERATORS.contains(&ch)) => {
NARY_HEIGHT_EM
}
_ => LINE_HEIGHT_EM,
}
}
fn write_plain(node: &MathNode, out: &mut String) {
match node {
MathNode::Ident { text, .. }
| MathNode::Number(text)
| MathNode::Operator { text, .. }
| MathNode::Text(text) => out.push_str(text),
MathNode::Space(_) => out.push(' '),
MathNode::Row(nodes) => nodes.iter().for_each(|node| write_plain(node, out)),
MathNode::Frac { num, den } => {
write_plain(num, out);
out.push('/');
write_plain(den, out);
}
MathNode::Sqrt(base) => {
out.push('√');
write_plain(base, out);
}
MathNode::Root { base, index } => {
write_plain(index, out);
out.push('√');
write_plain(base, out);
}
MathNode::Scripts { base, sub, sup } => {
write_plain(base, out);
for part in [sub, sup].into_iter().flatten() {
write_plain(part, out);
}
}
MathNode::UnderOver { base, under, over, .. } => {
write_plain(base, out);
for part in [under, over].into_iter().flatten() {
write_plain(part, out);
}
}
MathNode::Table { rows, .. } => {
for (i, row) in rows.iter().enumerate() {
if i > 0 {
out.push_str("; ");
}
row.iter().for_each(|cell| write_plain(cell, out));
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::render::docx_omml_for_tests as write;
fn read(xml: &str) -> MathNode {
let doc = format!("<root xmlns:m=\"{MATH_NS}\" xmlns:w=\"{WORD_NS}\">{xml}</root>",);
let parsed = roxmltree::Document::parse(&doc).unwrap();
let node = parsed.root_element().first_element_child().unwrap();
formula(node)
}
fn ident(text: &str) -> MathNode {
MathNode::Ident {
text: text.into(),
upright: false,
}
}
#[test]
fn round_trip_through_the_word_output() {
let node = MathNode::Frac {
num: Box::new(MathNode::Row(vec![
ident("a"),
operator("+"),
MathNode::Number("2".into()),
])),
den: Box::new(MathNode::Sqrt(Box::new(ident("x")))),
};
assert_eq!(read(&write(&node)), node);
let scripts = MathNode::Scripts {
base: Box::new(ident("x")),
sub: Some(Box::new(MathNode::Number("1".into()))),
sup: Some(Box::new(MathNode::Number("2".into()))),
};
assert_eq!(read(&write(&scripts)), scripts);
}
#[test]
fn nary_sum() {
let node = read(
"<m:oMath><m:nary><m:naryPr><m:chr m:val=\"∑\"/></m:naryPr><m:sub><m:r><m:t>i</m:t></m:r></m:sub>\
<m:sup><m:r><m:t>n</m:t></m:r></m:sup><m:e><m:r><m:t>x</m:t></m:r></m:e></m:nary></m:oMath>",
);
let MathNode::Row(nodes) = node else { panic!("a row") };
assert!(matches!(
&nodes[0],
MathNode::UnderOver {
under: Some(_),
over: Some(_),
..
}
));
assert_eq!(nodes[1], ident("x"));
}
#[test]
fn runs_are_tokenized() {
let node = read(
"<m:oMath><m:func><m:fName><m:r><m:rPr><m:sty m:val=\"p\"/></m:rPr><m:t>sin</m:t></m:r></m:fName>\
<m:e><m:r><m:t>x</m:t></m:r></m:e></m:func><m:r><m:t>=0.5</m:t></m:r></m:oMath>",
);
assert_eq!(
node,
MathNode::Row(vec![
MathNode::Ident {
text: "sin".into(),
upright: true
},
ident("x"),
operator("="),
MathNode::Number("0.5".into()),
])
);
assert_eq!(plain_text(&node), "sinx=0.5");
}
#[test]
fn delimiters_grow_with_their_content() {
let tall = read(
"<m:oMath><m:d><m:e><m:f><m:num><m:r><m:t>1</m:t></m:r></m:num><m:den><m:r><m:t>2</m:t></m:r></m:den>\
</m:f></m:e></m:d></m:oMath>",
);
let MathNode::Row(nodes) = tall else { panic!("a row") };
assert!(matches!(&nodes[0], MathNode::Operator { height_em: Some(_), .. }));
let flat = read("<m:oMath><m:d><m:e><m:r><m:t>x</m:t></m:r></m:e></m:d></m:oMath>");
assert_eq!(flat, MathNode::Row(vec![operator("("), ident("x"), operator(")")]));
}
}