ends_with_control_word walked back over the whole trailing run of
letters for every letter written, so a long run took quadratic time:
200,000 letters took about 10 s in a release build, and 400,000 did not
finish in 3 minutes in a test build. The output position after the last
plain letter is now remembered; while the output still ends there its
trailing letters are plain text and are not scanned again.
Review finding on #1162 (ends_with_control_word).
linear_to_latex separated a control word from a following letter only by
looking at the next input character. A character it then dropped (an
unmatched 〖 or 〗, or a control character) hid the letter after it, so
`\in〗put` and `∈〗put` were written as `\input`, a command the exported
.tex would run on the machine of whoever compiles it. A letter is now
separated whenever the output so far ends in a control word.
Review finding on #1162 (latex_text.rs, the 〗 arm). The control
character case was already on main.
A script whose base isn't simple prints as `〖a^b〗^c` in linear text (port of
wordcraft#6084), and LaTeX export copied the brackets into the .tex, where the
exporter's preamble can't set them. They are now a TeX group, `{a^b}^c`; one
without its partner is dropped. Also pins that `〖a+b〗 ^2` keeps the delimiter.
Port of the review fix on wordcraft#6084.
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.
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.
The OMML writer (omml/write.rs) had no tests of its own, and the linear-format parser
(math_linear.rs) none in its own module.
- omml/write/tests.rs: every MNode variant and option, run properties, the three sources
write_equation takes an equation from (kept OMML, nodes, linear text), the equation colour
(#1467), display jc, resolve_numbers, m:mathPr and the matrix and depth caps (#4000) are written
and read back with read_omath. What the reader gives back differently on purpose (merged runs,
half-point sizes, matrix column alignments, an empty delimiter...) is stated as such. Element
order, which a round trip can't see, is pinned by exact XML: each structure with its defaults
and with every property on, a run, m:oMathPara and m:mathPr. The strings follow the ECMA-376
shared-math sequences.
- math_linear/tests.rs: one case per construct of the module doc, the helpers (closer,
is_combining, strip_parens), to_linear round trips and truncated input. Where Word builds the
same text, the expected tree is Word's.
Tests only; no behaviour change.
Ported from storytold/wordcraft#6080 (issue wordcraft#6079), on top of the other ports in this
series, which they pass with unchanged. The writer tests move from WordCraft's
docx/src/write/math/tests.rs to omml/write/tests.rs, with craft-math's paths for what they used
from WordCraft (read_omath, para_jc and read_math_pr from omml, each_run_mut from math_tree,
map_args from math_linear), and OMML read back with omml::parse instead of WordCraft's
xml::parse_strict over namespace-wrapped text.
write_equation writes Math::omml verbatim into the host document once a check passes, and documents
saved as JSON carry that field, so a JSON → .docx conversion could write a document.xml that isn't
well-formed, or any WordprocessingML verbatim. craft-math's check was a strict craft-xml read of
text starting with <m:oMath, which already refuses unclosed and mismatched tags, unknown entities
and trailing markup, but let through: an m:oMathPara (nested in a second one), an m: bound to
another namespace, names that aren't XML names, undeclared prefixes, two attributes with one
expanded name, a < in an attribute value, ]]> in text, and characters XML 1.0 doesn't allow.
The kept XML is now written only when it starts with <m:oMath> (not m:oMathPara), passes the strict
read (which also bounds the nesting), and then parses with roxmltree, a conforming parser, as
exactly one element that is m:oMath by namespace. The canonical prefixes of kept OMML (m:, w:, mc:)
are declared for that parse, as craft-math already documents them and the .docx root declares
them; any other prefix is undeclared there. Otherwise the equation is written from its nodes.
In craft-docx, Flat OPC's one_element (the gate for inlining kept bytes as pkg:xmlData) now also
requires XML names and attribute values that unescape, as in the WordCraft PR.
Ported from storytold/wordcraft#6081 (issue wordcraft#6072). The check differs from the PR's in
where it lives: the PR combined flat::one_element, parse_strict and roxmltree in the DOCX writer,
over the OMML wrapped in every namespace document.xml declares. Here the writer is in craft-math,
which doesn't know the host's namespaces, so it declares the three canonical ones. Its tests go
into craft-docx's integration tests (tests/it/kept_omml_check.rs), with only the crate names
changed; all 22 malformed cases fail without this commit except the 11 craft-math already refused.
roxmltree 0.21 is already in the lock as a dependency of other crates.
Checked on the 72 .docx files with equations from earlier Word probes (488 equations with kept
OMML, 35 files saved by Word): the same equations are written back verbatim as before, except one
empty <m:oMath/> in a hand-made repro file, which is now written from its (empty) nodes.
CT_RPR holds m:nor or m:scr/m:sty, not both, and Word keeps only the m:nor of such a run, so bold
normal text lost its bold there. Normal text now writes neither; its bold and italic go in w:rPr
(w:b/w:i), which the reader already reads back for normal text.
Ported from storytold/wordcraft#6082 (issue wordcraft#6073). The writer change moves from
WordCraft's docx/src/write/math.rs to omml/write.rs, where the OMML writer now lives. Its test
saves and reopens a .docx, so it goes into craft-docx's integration tests
(tests/it/math_normal_text.rs) with only the crate names changed.
The parser pushed digits one character at a time, so a script took only the last digit as its
base: 10^3 built 1 followed by 0³, where Word builds 10³. A number is now read as one atom, by the
rule Word follows: digits, with a . or , continuing it only when a digit follows (2.5, 3.14, 1,000)
and a leading . before a digit starting one (.5); a trailing point (2.) isn't part of it, and
letters and signs are separate items. A script or a fraction operand takes the whole number (10^3,
2.5/3). A number script follows the same rule (x^.5, x_1,000; x^2. keeps the point out), keeping
letters after the digits (#1609).
Ported from storytold/wordcraft#6236 (issue wordcraft#6229): its change to math_linear.rs and its
tests/number_bases.rs, with only paths and crate names changed. Its typing test (engine) and the
docs/typing-parity.md note stay in WordCraft.
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.
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.
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`).
a^b^c built (a^b)^c; Word builds a^(b^c), and the same for a_b_c and x^2^3. After a script
operand, the same script operator now nests inside it. The chain stops at WRAP_DEPTH like every
other wrap, so the #1456 guard holds. a_b^c, a^b_c and (a^b)^c are unchanged.
From the PR's review: sin^2^3 on the function-name path nests to the right too (script_chain for
the first script; a second script joins only when it is the other operator, log_2^3). A level that
would make the whole tree too deep keeps its operator as text, as expr does at its own cap, and a
^ or _ the chain left at its cap isn't taken as the start of a function's operand.
Ported from storytold/wordcraft#6083 (issue wordcraft#6074): its change to math_linear.rs and its
tests/stacked_scripts.rs, with only paths and crate names changed. Its typing test (engine) and
the docs/typing-parity.md note stay in WordCraft.
#1019 (22161d2d) wrote \sin^2\theta as \sin ^{2}{\theta } so it reads back as
the same tree. The squash merge of #1038, whose branch predated it, put
math_latex.rs and the corpus test back as they were before. This applies
#1019's change again, unchanged, with its test.
* Add craft-wordmodel: WordCraft's document model without its editing code
WordCraft's crates/doc (wordcraft-doc, at 07f97737) moved as it is, so the DOCX,
RTF, ODT and other text-format crates can read into and write from one model.
Module paths, type names and serde attributes are unchanged: an app that
re-exports the crate keeps its native file format.
Left in the app: range editing (edit.rs), IME text and equation editing. Not
model, and already in craft-libs: code pages (craft-rtf), the ZIP entry-count
check (craft-opc), the compound-file guard (craft-cfb). The paragraph walks
moved to walk.rs and the unit helpers of wordcraft-geom to geom.rs.
* craft-wordmodel: math_tree, the arguments of an equation node and a walk over its runs
The DOCX reader and writer walk an equation's runs. The walk and the child
accessors it uses were in WordCraft's equation editor (math_edit.rs), which
stays in the app; they are structure, not editing.
* craft-mime: detect, for bytes that name no character set
is_text, decode (BOM, unmarked UTF-16, UTF-8, else the legacy encoding
the bytes look like), legacy and legacy_label, behind a detect feature
that brings in chardetng. Moved from WordCraft's crates/formats/src/txt.rs
at 06c2ae6f with its text-sniffing tests. Also charset::is_known, so a
caller can tell a label decode would read as UTF-8 from one it knows.
* craft-wordmodel: keep the document operations (edit.rs) with the model
The DOCX and RTF readers use them: alternative-format chunks are merged into
a document with adopt_blocks, and a reader formats ranges with format_range
and inserts objects. They operate on the document, not on a view, so they
belong beside it. The tests that drove them come back with them; the
paragraph walks stay in walk.rs and edit re-exports them.
base64 0.23, as WordCraft and craft-offcrypto use.
* 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.
* Add craft-flow: the flow model the text formats map to, on craft-wordmodel
WordCraft's crates/formats/src/model (07f97737) moved as it is: Flow,
FBlock, Para, Inline, Fmt, tables and cells, pictures, to_doc and
from_doc, picture sniffing, colours, base64 and the Dropped report. Only
the paths changed (wordcraft_doc is craft_wordmodel); the report helpers
are public so the format crates can call them; the image re-encoding
helper stays with WordCraft's RTF writer.
* Add craft-markdown: Markdown read and written, on craft-flow
WordCraft's crates/formats/src/markdown (07f97737) with its 35 tests,
moved onto craft-flow and craft-wordmodel. Character references come from
craft-html. The table cell budget lives in craft-flow, which names it in
its warning. Two test lines that wrote plain text through WordCraft's txt
module stay in WordCraft.
* WIP: craft-latex sources from WordCraft (unmodified)
* Add craft-latex: LaTeX documents read and written, on craft-flow
WordCraft's crates/formats/src/latex (07f97737) with its 21 tests, moved
onto craft-flow and craft-wordmodel; bytes are decoded by craft-mime's
detect. The LaTeX parts of WordCraft's cross-format tests (nesting,
column specs, labels, no TeX commands from the document, the fuzz and
round-trip properties) come along as tests/hostile.rs. The ligature test
builds its three-font paragraph as a flow instead of reading HTML.
* craft-math: latex_text, LaTeX math and the linear format converted as text
WordCraft's crates/formats/src/latex/math.rs (07f97737): the converter its
LaTeX document reader and writer use, which keeps the source's spacing.
It moves here so craft-math is the one place that converts LaTeX math;
craft-latex calls it, and its output does not change. Its tests (from
WordCraft's latex tests and cross-format tests) come along.
* craft-math: latex_text's group finders are public, for craft-latex's length parser
* craft-latex: math through craft_math::latex_text; label timing test by ratio
The document math converter moved to craft-math (#1040) unchanged; this
crate calls it there (and its length parser uses its group finders), so
export output is the same. The label test compared one read with a
ten-second wall clock and failed on a loaded machine; it now compares the
fastest of three reads of 25,000 and 100,000 labels, which a linear read
keeps near 4x and a quadratic one near 16x.
---------
Co-authored-by: shuff57 <62350898+shuff57@users.noreply.github.com>
* 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.
* craft-math: an operator name with a script keeps applying to its argument in LaTeX
to_latex wrote \sin^2\theta as {\sin }^{2}{\theta }, which reads back as
(\sin)^2 followed by θ: the braces cut the name off from its argument. An
operator name written as a command now takes its scripts directly
(\sin ^{2}{\theta }). The corpus test no longer needs its exception.
---------
Co-authored-by: shuff57 <62350898+shuff57@users.noreply.github.com>
* 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>