Files
Dolan 2fd675a0c4 feat(layout): lay out equations of text, as Word does (#3675)
* feat(layout): lay out equations of text, as Word does

* docs(layout): say a line's equation alone in its paragraph is lined up as its justification says

A line's text is lined up in its room as the paragraph's alignment says,
but Word centres an equation alone in its paragraph in a paragraph aligned
left (word-equations.docx EQ2c, EQ2d), as its m:jc, or the document's
m:defJc, says. The layout gives each line's room and how wide its text is,
not where the text starts in it, so nothing it lays out moves.

* fix(layout): stop at a displayed equation beside text, and at equations alone together or after a number

Word shows an equation in a line of text in the line, and one alone in its
paragraph displayed, on a line of its own (word-equations.docx EQ2). An
m:oMathPara is displayed, so one beside text in its paragraph was laid out
in the line, where Word hasn't been seen putting it. More than one equation
alone in a paragraph, and one alone after its list's number, haven't been
seen either, so the layout stops at them, as it does at what Word's
behaviour isn't known for.

* fix(layout): stop at equations in maths settings the layout doesn't follow

An equation's widths are Cambria Math's, so one in a document whose maths
settings (m:mathPr) give another maths font was laid out too wide or too
narrow. Margins and space around equations on lines of their own
(m:lMargin, m:rMargin, m:preSp, m:postSp) haven't been seen in Word either.
The layout now stops at the equations of such a document, and follows the
settings Word writes: Cambria Math and margins of 0. Text without
equations is laid out as it was.

* fix(layout): count only the equations the reader reads in a paragraph

An equation deleted, or moved elsewhere, in a tracked change is left out
of its paragraph, but the check of a paragraph's equations counted it from
the XML, so text, an equation and a deleted equation stopped as two
equations alone in a paragraph. It now counts the equations read, the
boxes that stop where they don't fit on their line, and looks for an
equation displayed (m:oMathPara) only where the reader reads.
2026-10-04 02:30:10 +01:00

41 lines
1.9 KiB
Python

# Reads the probes of word-equations.ts from a PDF of word-equations.docx: each line's top and bottom, its words, and for
# a line with an equation between "before" and "after", how far apart they are, which is the equation's width and the two
# spaces around it.
#
# pdftotext -bbox-layout word-equations.pdf word-equations.html
# python3 word-equations.py word-equations.html
#
# Lengths are in twips: a line's top and bottom, the top of its highest word and the bottom of its lowest, from the top
# of the page, and a word's start and end from the left margin. pdftotext gives each word to a hundredth of a point, and
# Word's PDFs put text on a grid of 1/300 inch, 4.8 twips.
import html
import re
import sys
LEFT = 1440
def main(path):
text = open(path, encoding="utf8").read()
for number, content in enumerate(re.findall(r"<page.*?</page>", text, re.S), start=1):
print(f"\n== page {number}")
for line in re.findall(r"<line.*?</line>", content, re.S):
words = [
(float(x0) * 20 - LEFT, float(y0) * 20, float(x1) * 20 - LEFT, float(y1) * 20, html.unescape(word))
for x0, y0, x1, y1, word in re.findall(
r'<word xMin="([\d.]+)" yMin="([\d.]+)" xMax="([\d.]+)" yMax="([\d.]+)">([^<]*)</word>', line
)
]
if not words:
continue
top = min(word[1] for word in words)
bottom = max(word[3] for word in words)
shown = " ".join(f"{word[4]}[{round(word[0], 1)}-{round(word[2], 1)}]" for word in words)
before = next((word for word in words if word[4] == "before"), None)
after = next((word for word in words if word[4] == "after"), None)
gap = f" gap {round(after[0] - before[2], 1)}" if before and after else ""
print(f" {round(top, 1):7} {round(bottom, 1):7}{gap} {shown}")
main(sys.argv[1])