use crate::model::MathNode;
const OLE_HEADER_LEN: usize = 28;
const END: u8 = 0;
const LINE: u8 = 1;
const CHAR: u8 = 2;
const TMPL: u8 = 3;
const PILE: u8 = 4;
const MATRIX: u8 = 5;
const EMBELL: u8 = 6;
const RULER: u8 = 7;
const FONT_OR_STYLE_DEF: u8 = 8;
const SIZE: u8 = 9;
const SIZE_FULL: u8 = 10;
const SIZE_SUBSYM: u8 = 14;
const COLOR: u8 = 15;
const COLOR_DEF: u8 = 16;
const FONT_DEF: u8 = 17;
const EQN_PREFS: u8 = 18;
const ENCODING_DEF: u8 = 19;
const FUTURE: u8 = 100;
const OPT_NUDGE: u8 = 0x08;
const OPT_CHAR_EMBELL: u8 = 0x01;
const OPT_CHAR_ENC_CHAR_8: u8 = 0x04;
const OPT_CHAR_ENC_CHAR_16: u8 = 0x10;
const OPT_CHAR_ENC_NO_MTCODE: u8 = 0x20;
const OPT_LINE_NULL: u8 = 0x01;
const OPT_LP_RULER: u8 = 0x02;
const OPT_LINE_LSPACE: u8 = 0x04;
const FACE_TEXT: u8 = 1;
const FACE_FUNCTION: u8 = 2;
const FACE_NUMBER: u8 = 8;
const FACE_VECTOR: u8 = 7;
const THIN_SPACE_EM: f32 = 0.17;
pub(crate) fn equation(native: &[u8]) -> Option<MathNode> {
let header_len = usize::from(u16::from_le_bytes(native.get(0..2)?.try_into().ok()?));
let start = if header_len == OLE_HEADER_LEN { header_len } else { 0 };
let mut reader = Reader {
bytes: native.get(start..)?,
pos: 0,
version: 0,
};
reader.header()?;
let nodes = reader.objects()?;
let node = MathNode::row(nodes);
(!is_empty(&node)).then_some(node)
}
fn is_empty(node: &MathNode) -> bool {
match node {
MathNode::Row(nodes) => nodes.iter().all(is_empty),
MathNode::Space(_) => true,
_ => false,
}
}
enum Object {
Char {
ch: char,
face: u8,
embells: Vec<u8>,
},
Node(MathNode),
Scripts {
sub: Option<MathNode>,
sup: Option<MathNode>,
},
}
struct Reader<'a> {
bytes: &'a [u8],
pos: usize,
version: u8,
}
impl Reader<'_> {
fn byte(&mut self) -> Option<u8> {
let byte = *self.bytes.get(self.pos)?;
self.pos += 1;
Some(byte)
}
fn word(&mut self) -> Option<u16> {
Some(u16::from(self.byte()?) | (u16::from(self.byte()?) << 8))
}
fn skip_string(&mut self) -> Option<()> {
while self.byte()? != 0 {}
Some(())
}
fn header(&mut self) -> Option<()> {
self.version = self.byte()?;
if !matches!(self.version, 3 | 5) {
return None;
}
for _ in 0..4 {
self.byte()?;
}
if self.version == 5 {
self.skip_string()?;
self.byte()?;
}
Some(())
}
fn tag(&mut self) -> Option<(u8, u8)> {
let tag = self.byte()?;
if self.version == 3 {
return Some((tag & 0x0F, tag >> 4));
}
let options = if (LINE..=RULER).contains(&tag) && tag != RULER {
self.byte()?
} else {
0
};
Some((tag, options))
}
fn nudge(&mut self, options: u8) -> Option<()> {
if options & OPT_NUDGE != 0 {
let (dx, dy) = (self.byte()?, self.byte()?);
if dx == 128 && dy == 128 {
self.word()?;
self.word()?;
}
}
Some(())
}
fn objects(&mut self) -> Option<Vec<MathNode>> {
let mut objects = Vec::new();
while let Some((kind, options)) = self.tag() {
match kind {
END => break,
LINE => {
if let Some(line) = self.line(options)? {
objects.push(Object::Node(line));
}
}
CHAR => {
if let Some(object) = self.character(options)? {
objects.push(object);
}
}
TMPL => objects.push(self.template(options)?),
PILE => objects.push(Object::Node(self.pile(options)?)),
MATRIX => objects.push(Object::Node(self.matrix(options)?)),
_ => self.skip_record(kind, options)?,
}
}
Some(group(objects))
}
fn line(&mut self, options: u8) -> Option<Option<MathNode>> {
self.nudge(options)?;
if self.version == 5 && options & OPT_LINE_LSPACE != 0 {
self.word()?;
}
if self.version == 3 && options & OPT_LINE_LSPACE != 0 {
self.word()?;
}
if options & OPT_LP_RULER != 0 {
self.ruler_record()?;
}
if options & OPT_LINE_NULL != 0 {
return Some(None);
}
Some(Some(MathNode::row(self.objects()?)))
}
fn ruler_record(&mut self) -> Option<()> {
self.tag()?;
self.ruler()
}
fn ruler(&mut self) -> Option<()> {
let stops = self.byte()?;
for _ in 0..stops {
self.byte()?;
self.word()?;
}
Some(())
}
fn character(&mut self, options: u8) -> Option<Option<Object>> {
self.nudge(options)?;
let face = self.byte()?.wrapping_sub(128);
let code = if self.version == 3 {
Some(self.word()?)
} else {
let code = if options & OPT_CHAR_ENC_NO_MTCODE == 0 {
Some(self.word()?)
} else {
None
};
if options & OPT_CHAR_ENC_CHAR_8 != 0 {
self.byte()?;
}
if options & OPT_CHAR_ENC_CHAR_16 != 0 {
self.word()?;
}
code
};
let has_embells = if self.version == 3 {
options & 0x02 != 0
} else {
options & OPT_CHAR_EMBELL != 0
};
let mut embells = Vec::new();
if has_embells {
while let Some((kind, embell_options)) = self.tag() {
if kind == END {
break;
}
if kind != EMBELL {
self.skip_record(kind, embell_options)?;
continue;
}
self.nudge(embell_options)?;
embells.push(self.byte()?);
}
}
Some(code.and_then(char_of).map(|ch| Object::Char { ch, face, embells }))
}
fn template(&mut self, options: u8) -> Option<Object> {
self.nudge(options)?;
let selector = self.byte()?;
let mut variation = u16::from(self.byte()?);
if self.version == 5 && variation & 0x80 != 0 {
variation = (variation & 0x7F) | (u16::from(self.byte()?) << 8);
}
self.byte()?;
let mut slots: Vec<Option<MathNode>> = Vec::new();
let mut chars = Vec::new();
while let Some((kind, slot_options)) = self.tag() {
match kind {
END => break,
LINE => slots.push(self.line(slot_options)?),
PILE => slots.push(Some(self.pile(slot_options)?)),
CHAR => {
if let Some(Object::Char { ch, .. }) = self.character(slot_options)? {
chars.push(ch);
}
}
TMPL => {
let inner = self.template(slot_options)?;
slots.push(Some(
group(vec![inner])
.into_iter()
.next()
.unwrap_or(MathNode::Row(Vec::new())),
));
}
MATRIX => slots.push(Some(self.matrix(slot_options)?)),
_ => self.skip_record(kind, slot_options)?,
}
}
if self.version == 3 {
let (selector, brackets) = v3_selector(selector);
if chars.is_empty()
&& let Some((left, right)) = brackets
{
chars.extend([left, right]);
}
return Some(build_template(selector, variation, slots, &chars));
}
Some(build_template(selector, variation, slots, &chars))
}
fn pile(&mut self, options: u8) -> Option<MathNode> {
self.nudge(options)?;
let halign = self.byte()?;
self.byte()?;
if options & OPT_LP_RULER != 0 {
self.ruler_record()?;
}
let mut lines = Vec::new();
while let Some((kind, line_options)) = self.tag() {
match kind {
END => break,
LINE => lines.push(self.line(line_options)?.unwrap_or(MathNode::Row(Vec::new()))),
_ => self.skip_record(kind, line_options)?,
}
}
if lines.len() == 1 {
return lines.pop();
}
let align = match halign {
1 => "left",
3 => "right",
_ => "center",
};
Some(MathNode::Table {
rows: lines.into_iter().map(|line| vec![line]).collect(),
align: vec![align],
})
}
fn matrix(&mut self, options: u8) -> Option<MathNode> {
self.nudge(options)?;
for _ in 0..3 {
self.byte()?;
}
let rows = usize::from(self.byte()?);
let cols = usize::from(self.byte()?);
for count in [rows, cols] {
for _ in 0..(2 * (count + 1)).div_ceil(8) {
self.byte()?;
}
}
let mut cells = Vec::new();
while let Some((kind, cell_options)) = self.tag() {
match kind {
END => break,
LINE => cells.push(self.line(cell_options)?.unwrap_or(MathNode::Row(Vec::new()))),
_ => self.skip_record(kind, cell_options)?,
}
}
let cols = cols.max(1);
Some(MathNode::Table {
rows: cells.chunks(cols).map(<[MathNode]>::to_vec).collect(),
align: vec!["center"; cols],
})
}
fn skip_record(&mut self, kind: u8, options: u8) -> Option<()> {
match kind {
EMBELL => {
self.nudge(options)?;
self.byte()?;
}
RULER => self.ruler()?,
FONT_OR_STYLE_DEF if self.version == 3 => {
self.byte()?;
self.byte()?;
self.skip_string()?;
}
FONT_OR_STYLE_DEF => {
let font = self.byte()?;
if font != 0 {
self.byte()?;
}
}
SIZE => {
let first = self.byte()?;
match first {
101 => {
self.word()?;
}
100 => {
self.byte()?;
self.word()?;
}
_ => {
self.byte()?;
}
}
}
SIZE_FULL..=SIZE_SUBSYM => {}
COLOR => {
self.byte()?;
}
COLOR_DEF => {
let color_options = self.byte()?;
let components = if color_options & 0x01 != 0 { 4 } else { 3 };
for _ in 0..components {
self.word()?;
}
if color_options & 0x04 != 0 {
self.skip_string()?;
}
}
FONT_DEF => {
self.byte()?;
self.skip_string()?;
}
EQN_PREFS => self.preferences()?,
ENCODING_DEF => self.skip_string()?,
kind if kind >= FUTURE => {
let len = usize::from(self.word()?);
self.pos += len;
}
_ => return None,
}
Some(())
}
fn preferences(&mut self) -> Option<()> {
self.byte()?;
for _ in 0..2 {
let count = self.byte()?;
self.dimensions(count)?;
}
let styles = self.byte()?;
for _ in 0..styles {
if self.byte()? != 0 {
self.byte()?;
}
}
Some(())
}
fn dimensions(&mut self, count: u8) -> Option<()> {
let mut left = count;
let mut high = true;
let mut byte = 0;
let mut in_value = false;
while left > 0 {
if high {
byte = self.byte()?;
}
let nibble = if high { byte >> 4 } else { byte & 0x0F };
high = !high;
if !in_value {
in_value = true;
} else if nibble == 0x0F {
in_value = false;
left -= 1;
}
}
Some(())
}
}
fn char_of(code: u16) -> Option<char> {
let mapped = match code {
0xEF01..=0xEF08 | 0xEB00..=0xEB0F => 0x2009,
0xF8E7 => 0x2212,
0xEC00 => 0x2192,
0xEC01 => 0x2190,
other => other,
};
char::from_u32(u32::from(mapped)).filter(|ch| !ch.is_control())
}
fn group(objects: Vec<Object>) -> Vec<MathNode> {
let mut out: Vec<MathNode> = Vec::new();
let mut run: Option<(u8, String)> = None;
let finish = |run: &mut Option<(u8, String)>, out: &mut Vec<MathNode>| {
if let Some((kind, text)) = run.take() {
out.push(match kind {
FACE_NUMBER => MathNode::Number(text),
FACE_FUNCTION => MathNode::Ident { text, upright: true },
_ => MathNode::Text(text),
});
}
};
for object in objects {
match object {
Object::Char { ch, face, embells } => {
let groupable = match face {
FACE_NUMBER => ch.is_ascii_digit() || ch == '.',
FACE_FUNCTION => ch.is_alphabetic(),
FACE_TEXT => !ch.is_whitespace() || run.is_some(),
_ => false,
};
if groupable && embells.is_empty() {
match &mut run {
Some((kind, text)) if *kind == face => text.push(ch),
_ => {
finish(&mut run, &mut out);
run = Some((face, ch.to_string()));
}
}
continue;
}
finish(&mut run, &mut out);
let node = char_node(ch, face);
out.push(embellish(node, &embells));
}
Object::Node(node) => {
finish(&mut run, &mut out);
out.push(node);
}
Object::Scripts { sub, sup } => {
finish(&mut run, &mut out);
let base = out.pop().unwrap_or(MathNode::Row(Vec::new()));
out.push(MathNode::Scripts {
base: Box::new(base),
sub: sub.map(Box::new),
sup: sup.map(Box::new),
});
}
}
}
finish(&mut run, &mut out);
out
}
fn char_node(ch: char, face: u8) -> MathNode {
if ch == '\u{2009}' || ch == ' ' {
return MathNode::Space(THIN_SPACE_EM);
}
if ch.is_ascii_digit() {
return MathNode::Number(ch.to_string());
}
if ch.is_alphabetic() {
let text = if face == FACE_VECTOR {
bold(ch).to_string()
} else {
ch.to_string()
};
return MathNode::Ident {
text,
upright: face == FACE_FUNCTION || face == FACE_TEXT,
};
}
MathNode::Operator {
text: ch.to_string(),
height_em: None,
}
}
fn bold(ch: char) -> char {
let offset = match ch {
'A'..='Z' => 0x1D400 + (u32::from(ch) - u32::from('A')),
'a'..='z' => 0x1D41A + (u32::from(ch) - u32::from('a')),
_ => return ch,
};
char::from_u32(offset).unwrap_or(ch)
}
fn embellish(node: MathNode, embells: &[u8]) -> MathNode {
let mut node = node;
for &embell in embells {
let (mark, above) = match embell {
2 => ("\u{2D9}", true),
3 => ("\u{A8}", true),
4 => ("\u{20DB}", true),
5 => ("\u{2032}", false),
6 => ("\u{2033}", false),
7 => ("\u{2035}", false),
8 => ("~", true),
9 => ("^", true),
11 => ("\u{2192}", true),
12 => ("\u{2190}", true),
13 => ("\u{2194}", true),
16 | 17 => ("\u{AF}", true),
18 => ("\u{2034}", false),
_ => continue,
};
let mark = Box::new(MathNode::Operator {
text: mark.to_string(),
height_em: None,
});
node = if above {
MathNode::UnderOver {
base: Box::new(node),
under: None,
over: Some(mark),
accent: true,
}
} else {
MathNode::Scripts {
base: Box::new(node),
sub: None,
sup: Some(mark),
}
};
}
node
}
fn op(text: &str) -> MathNode {
MathNode::Operator {
text: text.to_string(),
height_em: None,
}
}
type Slots = std::vec::IntoIter<Option<MathNode>>;
fn next_slot(slots: &mut Slots) -> MathNode {
slots.next().flatten().unwrap_or_else(|| MathNode::Row(Vec::new()))
}
fn build_template(selector: u8, variation: u16, slots: Vec<Option<MathNode>>, chars: &[char]) -> Object {
let mut slots = slots.into_iter();
let node = match selector {
0..=9 => fence_template(selector, variation, &mut slots, chars),
10 => {
let base = next_slot(&mut slots);
match slots.next().flatten() {
Some(index) if variation & 1 == 1 => MathNode::Root {
base: Box::new(base),
index: Box::new(index),
},
_ => MathNode::Sqrt(Box::new(base)),
}
}
11 => MathNode::Frac {
num: Box::new(next_slot(&mut slots)),
den: Box::new(next_slot(&mut slots)),
},
12 => accent(next_slot(&mut slots), "_", false),
13 => accent(next_slot(&mut slots), "\u{AF}", true),
14 => {
let over = slots.next().flatten();
let under = slots.next().flatten();
MathNode::UnderOver {
base: Box::new(op("\u{2192}")),
under: under.map(Box::new),
over: over.map(Box::new),
accent: false,
}
}
15..=23 => operator_template(selector, &mut slots, chars),
24 | 25 => brace_template(selector, &mut slots),
26 => {
let dividend = next_slot(&mut slots);
let mut row = vec![op(")"), dividend];
if let Some(quotient) = slots.next().flatten() {
row.insert(0, quotient);
}
MathNode::Row(row)
}
27..=29 => {
let sub = slots.next().flatten();
let sup = slots.next().flatten();
return Object::Scripts { sub, sup };
}
30 => {
let mut row = vec![op("\u{27E8}"), next_slot(&mut slots)];
if let Some(right) = slots.next().flatten() {
row.push(op("|"));
row.push(right);
}
row.push(op("\u{27E9}"));
MathNode::Row(row)
}
31 => accent(next_slot(&mut slots), "\u{2192}", true),
32 => accent(next_slot(&mut slots), "~", true),
33 => accent(next_slot(&mut slots), "^", true),
34 => accent(next_slot(&mut slots), "\u{2322}", true),
_ => next_slot(&mut slots),
};
Object::Node(node)
}
fn accent(base: MathNode, mark: &str, above: bool) -> MathNode {
let mark = Some(Box::new(op(mark)));
MathNode::UnderOver {
base: Box::new(base),
under: if above { None } else { mark.clone() },
over: if above { mark } else { None },
accent: true,
}
}
fn fence_template(selector: u8, variation: u16, slots: &mut Slots, chars: &[char]) -> MathNode {
let (left, right) = fence(selector, chars);
let mut row = Vec::new();
if variation & 0x01 != 0 || variation == 0 {
row.push(op(left));
}
row.push(next_slot(slots));
if variation & 0x02 != 0 || variation == 0 {
row.push(op(right));
}
MathNode::Row(row)
}
fn operator_template(selector: u8, slots: &mut Slots, chars: &[char]) -> MathNode {
if selector >= 21 {
let main = if selector == 23 { slots.next().flatten() } else { None };
let lower = slots.next().flatten();
let upper = slots.next().flatten();
let base = main.unwrap_or_else(|| op(&big_operator(selector, chars)));
return MathNode::UnderOver {
base: Box::new(base),
under: lower.map(Box::new),
over: upper.map(Box::new),
accent: false,
};
}
let operand = next_slot(slots);
let lower = slots.next().flatten().map(Box::new);
let upper = slots.next().flatten().map(Box::new);
let base = Box::new(op(&big_operator(selector, chars)));
let operator = if selector == 15 {
MathNode::Scripts {
base,
sub: lower,
sup: upper,
}
} else {
MathNode::UnderOver {
base,
under: lower,
over: upper,
accent: false,
}
};
MathNode::Row(vec![operator, operand])
}
fn brace_template(selector: u8, slots: &mut Slots) -> MathNode {
let main = next_slot(slots);
let label = slots.next().flatten();
let brace = op(if selector == 24 { "\u{23DE}" } else { "\u{23B4}" });
let braced = MathNode::UnderOver {
base: Box::new(main),
under: None,
over: Some(Box::new(brace)),
accent: false,
};
match label {
Some(label) => MathNode::UnderOver {
base: Box::new(braced),
under: None,
over: Some(Box::new(label)),
accent: false,
},
None => braced,
}
}
fn v3_selector(selector: u8) -> (u8, Option<(char, char)>) {
match selector {
0..=7 => (selector, None),
8 => (9, Some(('[', '['))),
9 => (9, Some((']', ']'))),
10 => (9, Some((']', '['))),
11 => (9, Some(('[', ')'))),
12 => (9, Some(('(', ']'))),
13 => (10, None),
14 | 41 => (11, None),
15 | 44 => (29, None),
16 => (12, None),
17 => (13, None),
18..=20 => (14, None),
21..=26 => (15, None),
27 => (24, None),
28 => (25, None),
29 | 30 => (16, None),
31 | 32 => (17, None),
33 | 34 => (18, None),
35 | 36 => (19, None),
37 | 38 => (20, None),
39 => (23, None),
40 => (26, None),
42 => (21, None),
43 => (22, None),
45 => (30, None),
46..=49 => (selector - 15, None),
_ => (selector.saturating_add(100), None),
}
}
fn fence(selector: u8, chars: &[char]) -> (&'static str, &'static str) {
let default = match selector {
0 => ("\u{27E8}", "\u{27E9}"),
1 => ("(", ")"),
2 => ("{", "}"),
3 => ("[", "]"),
4 => ("|", "|"),
5 => ("\u{2016}", "\u{2016}"),
6 => ("\u{230A}", "\u{230B}"),
7 => ("\u{2308}", "\u{2309}"),
8 => ("\u{27E6}", "\u{27E7}"),
_ => ("(", ")"),
};
if selector != 9 {
return default;
}
let side = |ch: Option<&char>, fallback: &'static str| match ch {
Some('(') => "(",
Some(')') => ")",
Some('[') => "[",
Some(']') => "]",
_ => fallback,
};
(side(chars.first(), "("), side(chars.get(1), ")"))
}
fn big_operator(selector: u8, chars: &[char]) -> String {
if let Some(ch) = chars.iter().find(|ch| !ch.is_whitespace()) {
return ch.to_string();
}
match selector {
15 | 21 => "\u{222B}",
17 => "\u{220F}",
18 => "\u{2210}",
19 => "\u{22C3}",
20 => "\u{22C2}",
23 => "lim",
_ => "\u{2211}",
}
.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore = "a tool for looking at equation objects"]
fn ole_formula() {
let path = std::env::var("KAKEMONO_OLE").expect("KAKEMONO_OLE names a file");
let ole = std::fs::read(path).expect("the object is readable");
let native = crate::word::equation_native(&ole).expect("an Equation Native stream");
let head: Vec<String> = native.iter().take(160).map(|byte| format!("{byte:02x}")).collect();
println!("{}", head.join(" "));
println!("{:?}", equation(&native));
}
fn mtef5(records: &[u8]) -> Vec<u8> {
let mut data = vec![5, 1, 1, 6, 0];
data.extend_from_slice(b"DSMT6\0");
data.push(0);
data.extend_from_slice(records);
data
}
fn ch(face: u8, code: char) -> Vec<u8> {
let code = code as u16;
vec![CHAR, 0, face + 128, (code & 0xFF) as u8, (code >> 8) as u8]
}
#[test]
fn fraction_with_a_square() {
let mut records = vec![LINE, 0];
records.extend_from_slice(&[TMPL, 0, 11, 0, 0]);
records.extend_from_slice(&[LINE, 0]);
records.extend(ch(3, 'x'));
records.extend_from_slice(&[TMPL, 0, 28, 0, 0, LINE, OPT_LINE_NULL, LINE, 0]);
records.extend(ch(8, '2'));
records.extend_from_slice(&[END, END, END]);
records.extend_from_slice(&[LINE, 0]);
records.extend(ch(8, '2'));
records.extend_from_slice(&[END, END, END]);
let node = equation(&mtef5(&records)).expect("a formula");
let MathNode::Frac { num, den } = node else {
panic!("not a fraction: {node:?}")
};
assert!(matches!(*den, MathNode::Number(ref text) if text == "2"));
let MathNode::Scripts { base, sup, sub } = *num else {
panic!("no script")
};
assert!(sub.is_none());
assert!(matches!(*base, MathNode::Ident { ref text, .. } if text == "x"));
assert!(matches!(sup.as_deref(), Some(MathNode::Number(text)) if text == "2"));
}
#[test]
fn function_names_and_numbers_are_grouped() {
let mut records = vec![LINE, 0];
for letter in "sin".chars() {
records.extend(ch(FACE_FUNCTION, letter));
}
records.extend(ch(3, 'x'));
records.extend(ch(6, '='));
for digit in "0.5".chars() {
records.extend(ch(FACE_NUMBER, digit));
}
records.extend_from_slice(&[END, END]);
let MathNode::Row(nodes) = equation(&mtef5(&records)).expect("a formula") else {
panic!("not a row")
};
assert!(matches!(&nodes[0], MathNode::Ident { text, upright: true } if text == "sin"));
assert!(matches!(&nodes[3], MathNode::Number(text) if text == "0.5"));
}
#[test]
fn version_3_overline() {
let mut data = vec![0x1C, 0, 0, 0, 2, 0];
data.resize(OLE_HEADER_LEN, 0);
data.extend_from_slice(&[3, 1, 1, 3, 1, 0x0A, 0x01, 0x03, 0x11, 0, 0, 0x01]);
data.extend_from_slice(&[0x02, 0x88, b'1', 0, 0x02, 0x82, b',', 0, 0x02, 0x88, b'2', 0]);
data.extend_from_slice(&[0, 0, 0, 0]);
let node = equation(&data).expect("a formula");
let MathNode::UnderOver {
over: Some(mark),
accent: true,
..
} = node
else {
panic!("not an overline: {node:?}")
};
assert!(matches!(*mark, MathNode::Operator { ref text, .. } if text == "\u{AF}"));
}
fn slot(face: u8, code: char) -> Vec<u8> {
let mut line = vec![LINE, 0];
line.extend(ch(face, code));
line.push(END);
line
}
fn template(selector: u8, variation: u8) -> Option<MathNode> {
let mut records = vec![LINE, 0, TMPL, 0, selector, variation, 0];
for code in ['a', 'b', 'c'] {
records.extend(slot(3, code));
}
records.extend_from_slice(&[END, END, END]);
equation(&mtef5(&records))
}
#[test]
fn every_template_gives_a_formula() {
for selector in 0..=40 {
for variation in [0, 1, 2, 3] {
assert!(
template(selector, variation).is_some(),
"template {selector} {variation}"
);
}
}
}
#[test]
fn templates_give_their_structure() {
assert!(matches!(template(10, 0), Some(MathNode::Sqrt(_))));
assert!(matches!(template(10, 1), Some(MathNode::Root { .. })));
assert!(matches!(template(11, 0), Some(MathNode::Frac { .. })));
assert!(matches!(
template(23, 0),
Some(MathNode::UnderOver { accent: false, .. })
));
assert!(matches!(template(24, 0), Some(MathNode::UnderOver { .. })));
assert!(matches!(
template(31, 0),
Some(MathNode::UnderOver { accent: true, .. })
));
let Some(MathNode::Row(row)) = template(1, 3) else {
panic!("a fence is a row")
};
assert!(matches!(&row[0], MathNode::Operator { text, .. } if text == "("));
let Some(MathNode::Row(row)) = template(15, 0) else {
panic!("an integral is a row")
};
assert!(matches!(&row[0], MathNode::Scripts { .. }));
}
#[test]
fn piles_and_matrices_are_tables() {
let mut records = vec![LINE, 0, PILE, 0, 1, 0];
records.extend(slot(3, 'x'));
records.extend(slot(3, 'y'));
records.extend_from_slice(&[END, END, END]);
let Some(MathNode::Table { rows, align }) = equation(&mtef5(&records)) else {
panic!("a pile is a table")
};
assert_eq!((rows.len(), align), (2, vec!["left"]));
let mut records = vec![LINE, 0, MATRIX, 0, 0, 0, 0, 2, 2, 0, 0];
for code in ['a', 'b', 'c', 'd'] {
records.extend(slot(3, code));
}
records.extend_from_slice(&[END, END, END]);
let Some(MathNode::Table { rows, .. }) = equation(&mtef5(&records)) else {
panic!("a matrix is a table")
};
assert_eq!((rows.len(), rows[0].len()), (2, 2));
}
#[test]
fn embellishments_mark_their_character() {
for (embell, prime) in [(5, true), (2, false)] {
let records = vec![
LINE,
0,
CHAR,
OPT_CHAR_EMBELL,
3 + 128,
b'f',
0,
EMBELL,
0,
embell,
END,
END,
END,
];
let node = equation(&mtef5(&records)).expect("a formula");
if prime {
assert!(matches!(node, MathNode::Scripts { sup: Some(_), .. }), "{node:?}");
} else {
assert!(matches!(node, MathNode::UnderOver { accent: true, .. }), "{node:?}");
}
}
}
#[test]
fn records_without_formula_are_skipped() {
let mut records = vec![
SIZE,
2,
0,
SIZE,
101,
0x20,
0x00,
SIZE,
100,
1,
0x10,
0x00,
SIZE_FULL,
SIZE_SUBSYM,
COLOR,
1,
COLOR_DEF,
0x04,
0,
0,
0,
0,
0,
0,
b'r',
b'e',
b'd',
0,
FONT_DEF,
1,
b'X',
0,
ENCODING_DEF,
b'U',
b'N',
b'I',
0,
FONT_OR_STYLE_DEF,
1,
0,
FUTURE,
2,
0,
9,
9,
EQN_PREFS,
0,
1,
0x21,
0xF0,
0,
2,
1,
3,
0,
RULER,
1,
0,
0x10,
0x00,
];
records.extend(slot(3, 'z'));
records.push(END);
let node = equation(&mtef5(&records)).expect("a formula");
assert!(
matches!(node, MathNode::Ident { ref text, .. } if text == "z"),
"{node:?}"
);
}
#[test]
fn version_3_templates_map_to_version_5() {
assert_eq!(v3_selector(13), (10, None));
assert_eq!(v3_selector(14), (11, None));
assert_eq!(v3_selector(11), (9, Some(('[', ')'))));
assert_eq!(v3_selector(39), (23, None));
assert_eq!(fence(9, &['[', ')']), ("[", ")"));
assert_eq!(big_operator(17, &[]), "\u{220F}");
assert_eq!(big_operator(16, &['\u{2211}']), "\u{2211}");
}
}