Commit Graph
6 Commits
Author SHA1 Message Date
kiwi4boy 4a5cb98651 craft-math: review fixes: a number base after a number, a cap on kept OMML, is_number without a Vec
From the second review round:

- A number base right after a digit, `.` or `,` keeps its brackets. Bare, it continued the number
  before it: `1` then `23^4` was written `123^4` and read back with 123 as the base, and the same
  for a one-digit base (`12^4`), `x.` then `5^2` and `1,` then `5^2`. This came with the bare
  number base, before the first review round. A number base after a letter or an operator stays
  bare (`a2^4`, `1+2^4`).
- Both parses of kept OMML are capped at the elements the reader keeps (MAX_ELEMENTS × 2) instead
  of craft-xml's defaults, and roxmltree gets the same cap as `nodes_limit`. Kept OMML can come
  from a hostile JSON document; larger kept OMML is written from its nodes. The reader can't keep
  more than this, so OMML read from a file is unaffected.
- is_number scans bytes instead of collecting a Vec<char>: everything a number holds is ASCII.

The two new tests fail without their fix.
2026-11-12 22:51:39 -05:00
kiwi4boy ae00359d2b craft-math: review fixes: number bases, a comma after a script, whitespace in kept OMML
From the review of this series:

- A base that is one number by the parser's rule (1,000 as well as 2.5 and .5) is written bare,
  1,000^2 rather than 〖1,000〗^2. lin_base tested numbers with digits and points only, so a
  comma made it a multi-letter base. The rule is now is_number, the parser's number_len over the
  whole text; simple_operand, which fractions share, is unchanged.
- A bare script followed by a comma and a digit is ended with a space: x^2 then ,3 was written
  x^2,3, and the number script read the ,3 back into the exponent. Only a comma before a digit
  gets one; x^2, y is written as before.
- Kept OMML whose element name is followed by a tab, carriage return or line feed (all XML
  whitespace) is written back like one followed by a space, instead of from the nodes.

Each new test fails without its fix.
2026-11-12 22:36:13 -05:00
kiwi4boy cc63fb5074 craft-math: a multi-letter script base survives linear text (port of wordcraft#6230)
The parser attaches a script to the last character before it (mc^2 is m, c²), so a base of
several letters from a Word or LibreOffice file, written bare, read back split. lin_base now writes
such a base in invisible brackets, 〖mc〗^2, as Word linearizes it; a number (10^3, as Word writes
it) and an upright function name stay bare.

Ported from storytold/wordcraft#6230 (issue wordcraft#6228), which is stacked on wordcraft#6084:
its own change to math.rs and its tests/linear_multichar_base.rs. The ODT test that pinned
LibreOffice's E=mc^2 (<msup><mi>mc</mi>…) moves with it: in craft-odf it now expects E=〖mc〗^2,
as in the WordCraft PR. It runs only with craft-odf's `text` feature.
2026-11-12 21:59:46 -05:00
kiwi4boy 8d2481a748 craft-math: an empty shown n-ary limit survives linear text (port of wordcraft#6085)
to_linear wrote a limit only when it wasn't empty, so half-typed ∑_ (an empty, shown lower limit)
came out as ∑▒() and read back with both limits hidden. A shown limit is now written even when
empty, as _() or ^().

Ported from storytold/wordcraft#6085 (issue wordcraft#6076): its change to math.rs and its
tests/linear_empty_limits.rs, with only paths and crate names changed.
2026-11-12 21:53:35 -05:00
kiwi4boy 10f2625365 craft-math: a script on a script prints back the same in linear text (port of wordcraft#6084)
to_linear wrote a script base that isn't simple in parentheses, so sSup(sSup(a, b), c) came out
as (a^b)^c and read back with a visible delimiter. It now writes 〖a^b〗^c, as Word does, and the
parser takes a lone 〖…〗 group as the base itself, as Word builds 〖a+b〗^2, instead of an invisible
delimiter. simple_operand is split out of lin_operand so both use one rule.

From the PR's review: an invisible delimiter that is itself a script base gets an outer pair
(〖〖a〗〗^2); a one-character operator base is written 〖⊗〗_i, since a script after an operator reads
as a pre-script; and only a base just closed by 〗 (after_group) is unwrapped, so an explicit ├a┤^2
keeps its delimiter.

Ported from storytold/wordcraft#6084 (issue wordcraft#6075): its changes to math.rs and
math_linear.rs and its tests/linear_script_base.rs. In the function-name branch of the parser,
where #6083 (stacked scripts) and this PR both change the `attach_script` calls, the stacked-script
chain is kept and the calls pass this PR's new `group` argument (`false`).
2026-11-12 21:53:35 -05:00
Dylan M. Taylorandshuff57 85b4dabf0f Add craft-math: equations as one tree with OMML, MathML, LaTeX and UnicodeMath (#1013)
* Add craft-math: the equation tree with OMML, MathML, LaTeX and UnicodeMath

The math tree, LaTeX, linear-format, symbol and tree-walk modules come
from craft-wordmodel (storytold/craft-libs#963, WordCraft's crates/doc at
07f97737) unchanged, with the run colour (Rgb) and the symbol-font tables
they use. MathML is WordCraft's crates/formats/src/mathml.rs and OMML is
WordCraft's crates/docx/src/read/math.rs and write/math.rs (06c2ae6f /
07f97737), moved onto craft-xml. The HTML-specific MathML tests stay in
WordCraft; the rest moved with the code.

mathml::parse and omml::parse/read_str/write_str are new: string entry
points for callers that have no tree of their own.

* craft-math: read MathLive's and other spellings of LaTeX math commands

\abs{...} (an absolute value), \lvert \rvert \lVert \rVert, \nparallel,
\backprime, \infinity, \cross, \differentialD, \exponentialE and
\imaginaryI (MathLive's d, upright e and i), \emph, \textsf, \hbox,
\mathnormal and \underleftarrow / \underrightarrow /
\underleftrightarrow were unknown and read as literal text. They are read
as equaSHion's typed-input parser reads them (storytold/craft-libs#29).

* craft-math: equaSHion's equation corpus as test data

corpus/equations.tsv, corpus/latex.tsv and the OMML samples of
storytold/craft-libs#29 (7a6ef6b9), unmodified.

* craft-math: corpus round trips, GridCraft's OMML cases, entry points and limits tested; README

The corpus equations go through OMML, MathML and LaTeX and back; the OMML
samples are written back as kept and from the tree. GridCraft's OMML to
LaTeX test cases (crates/xlsx/src/omml.rs) give LaTeX that reads back the
same through the tree. omml::parse and read_str are tested against other
prefixes, the strict namespace, kept OMML, junk, the element cap and deep
nesting.

---------

Co-authored-by: shuff57 <62350898+shuff57@users.noreply.github.com>
2026-11-11 20:31:02 -05:00