Repository Public
1// ORIX Docs2// File: Packages/ORIXDocsKit/Sources/DocDOCX/MathMLWriter.swift3// Description: Writes the math tree as presentation MathML (used by the HTML exporter).4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation89struct MathMLWriter {10 static func write(_ nodes: [MathNode]) -> String { MathMLWriter().row(nodes, nil) }1112 private func variantAttr(_ style: MathStyle?) -> [(String, String?)] { style.map { [("mathvariant", $0.mathVariant)] } ?? [] }1314 private func token(_ c: String, _ style: MathStyle?) -> String {15 guard let ch = c.first else { return "" }16 if ch.isWhitespace || c == "\u{200B}" || c == "\u{00A0}" {17 let width: String18 switch c {19 case "\u{2009}", "\u{2006}", "\u{200A}": width = "0.167em"20 case "\u{205F}": width = "0.222em"21 case "\u{2004}", "\u{2005}": width = "0.278em"22 case "\u{2003}": width = "1em"23 case "\u{200B}": width = "0em"24 default: width = "0.333em"25 }26 return xmlTag("mspace", [("width", width)])27 }28 if ch.isLetter { return xmlTag("mi", variantAttr(style), XMLEscape.text(c)) }29 if c == "∞" || c == "∂" || c == "∇" || c == "∅" { return xmlTag("mi", [("mathvariant", "normal")], XMLEscape.text(c)) }30 return xmlTag("mo", [], XMLEscape.text(c))31 }3233 /// Children of a row, with consecutive digits merged into one number.34 private func row(_ nodes: [MathNode], _ style: MathStyle?) -> String {35 var out = ""36 var number = ""37 func flush() {38 if !number.isEmpty { out += xmlTag("mn", variantAttr(style), XMLEscape.text(number)); number = "" }39 }40 for (k, n) in nodes.enumerated() {41 if case .char(let c) = n, let ch = c.first {42 if ch.isNumber { number += c; continue }43 if (c == "." || c == ","), !number.isEmpty, k + 1 < nodes.count, case .char(let next) = nodes[k + 1], next.first?.isNumber == true, c == "." {44 number += c45 continue46 }47 }48 flush()49 out += node(n, style)50 }51 flush()52 return out53 }5455 /// One element: a single child as is, several wrapped in an mrow.56 private func one(_ n: MathNode?, _ style: MathStyle?) -> String {57 guard let n else { return "<mrow/>" }58 let items = n.items59 if items.count == 1 {60 if case .char(let c) = items[0], c.first?.isNumber == true { return xmlTag("mn", variantAttr(style), XMLEscape.text(c)) }61 switch items[0] {62 case .char, .text, .funcName, .frac, .sqrt, .sup, .sub, .subsup, .accent, .bar, .under, .over, .groupChr, .matrix, .eqArray, .boxed, .phantom:63 return node(items[0], style)64 default: break65 }66 }67 return "<mrow>" + row(items, style) + "</mrow>"68 }6970 private func table(_ rows: [[MathNode]], _ style: MathStyle?, attrs: [(String, String?)] = []) -> String {71 xmlTag("mtable", attrs, rows.map { cells in72 "<mtr>" + cells.map { "<mtd>" + row($0.items, style) + "</mtd>" }.joined() + "</mtr>"73 }.joined())74 }7576 private func fence(_ ch: String) -> String {77 ch.isEmpty ? "" : xmlTag("mo", [("fence", "true"), ("stretchy", "true")], XMLEscape.text(ch))78 }7980 private func node(_ n: MathNode, _ style: MathStyle?) -> String {81 switch n {82 case .row(let items): return "<mrow>" + row(items, style) + "</mrow>"83 case .char(let c): return token(c, style)84 case .text(let s): return xmlTag("mtext", [], XMLEscape.text(s))85 case .funcName(let name): return xmlTag("mi", [], XMLEscape.text(MathTables.functionDisplay(name)))86 case .styled(let s, let body): return "<mrow>" + row(body.items, s) + "</mrow>"87 case .frac(let a, let b, let type):88 switch type {89 case .bar: return "<mfrac>" + one(a, style) + one(b, style) + "</mfrac>"90 case .noBar: return "<mfrac linethickness=\"0\">" + one(a, style) + one(b, style) + "</mfrac>"91 case .skw: return "<mfrac bevelled=\"true\">" + one(a, style) + one(b, style) + "</mfrac>"92 case .lin: return "<mrow>" + one(a, style) + "<mo>/</mo>" + one(b, style) + "</mrow>"93 }94 case .sqrt(let index, let body):95 if let index { return "<mroot>" + one(body, style) + one(index, style) + "</mroot>" }96 return "<msqrt>" + row(body.items, style) + "</msqrt>"97 case .sup(let b, let p): return "<msup>" + one(b, style) + one(p, style) + "</msup>"98 case .sub(let b, let s): return "<msub>" + one(b, style) + one(s, style) + "</msub>"99 case .subsup(let b, let s, let p): return "<msubsup>" + one(b, style) + one(s, style) + one(p, style) + "</msubsup>"100 case .nary(let op, let sub, let sup, let body):101 let o = xmlTag("mo", [("largeop", "true")], XMLEscape.text(op))102 let limits = !MathTables.integrals.contains(op)103 var head = o104 switch (sub, sup) {105 case (let s?, let p?): head = limits ? "<munderover>\(o)\(one(s, style))\(one(p, style))</munderover>" : "<msubsup>\(o)\(one(s, style))\(one(p, style))</msubsup>"106 case (let s?, nil): head = limits ? "<munder>\(o)\(one(s, style))</munder>" : "<msub>\(o)\(one(s, style))</msub>"107 case (nil, let p?): head = limits ? "<mover>\(o)\(one(p, style))</mover>" : "<msup>\(o)\(one(p, style))</msup>"108 default: break109 }110 return "<mrow>" + head + (body.isEmptyRow ? "" : one(body, style)) + "</mrow>"111 case .delim(let open, let close, let sep, let parts):112 var inner = ""113 for (k, p) in parts.enumerated() {114 if k > 0 { inner += xmlTag("mo", [("separator", "true"), ("stretchy", "true")], XMLEscape.text(sep)) }115 inner += one(p, style)116 }117 return "<mrow>" + fence(open) + inner + fence(close) + "</mrow>"118 case .accent(let name, let body):119 return "<mover accent=\"true\">" + one(body, style) + xmlTag("mo", [], XMLEscape.text(MathTables.accentSpacing[name] ?? "^")) + "</mover>"120 case .bar(let top, let body):121 return top ? "<mover>" + one(body, style) + "<mo stretchy=\"true\">‾</mo></mover>" : "<munder>" + one(body, style) + "<mo stretchy=\"true\">_</mo></munder>"122 case .function(let head, let arg):123 return "<mrow>" + one(head, style) + "<mo>⁡</mo>" + (arg.isEmptyRow ? "" : one(arg, style)) + "</mrow>"124 case .under(let b, let u): return "<munder>" + one(b, style) + one(u, style) + "</munder>"125 case .over(let b, let o): return "<mover>" + one(b, style) + one(o, style) + "</mover>"126 case .groupChr(let ch, let top, let body):127 let mark = xmlTag("mo", [("stretchy", "true")], XMLEscape.text(ch))128 return top ? "<mover>" + one(body, style) + mark + "</mover>" : "<munder>" + one(body, style) + mark + "</munder>"129 case .matrix(let rows): return table(rows, style)130 case .eqArray(let rows):131 let columns = max(1, rows.map(\.count).max() ?? 1)132 let align = (0..<columns).map { $0 % 2 == 0 ? "right" : "left" }.joined(separator: " ")133 return table(rows, style, attrs: [("columnalign", columns == 1 ? "left" : align), ("columnspacing", "0em")])134 case .boxed(let body): return "<menclose notation=\"box\">" + row(body.items, style) + "</menclose>"135 case .phantom(let body): return "<mphantom>" + row(body.items, style) + "</mphantom>"136 }137 }138}139