mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
* feat: math from LaTeX in docx/math, with accents, bars, braces, boxes, phantoms and large operators latexToMath turns LaTeX math into docx's math components, to go in a Math. It reads what LaTeX, MathJax and KaTeX write: fractions, roots, scripts, large operators, functions, \left and \right, accents, braces, boxes, fonts, \text, and the environments for matrices, cases and aligned equations, with \tag. It throws for what it can't read, saying where. The parts LaTeX needs are public components too: MathLargeOperator, MathAccent, MathBar, MathBrace, MathBox and MathPhantom in docx/math, and in docx, MathRun's style, script and literal, MathFraction's type, and limits on MathSum and MathIntegral. Closes #2994 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: draw MathRun alphabets upright unless given a style, and escape the \middle| table cell A run with a script but no style wrote no m:sty, which Word draws as italic, so sansSerif came out italic sans-serif rather than the upright 𝖠 its docs show. A script other than roman now defaults to a plain style, as LaTeX draws its alphabets; a given style is kept. latexToMath already gave each alphabet its style. The | after \middle in the LaTeX docs' table ended the cell, splitting the row into three. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
4.3 KiB
Bash
Executable File
83 lines
4.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Runs the shape, chart and math demos, checks where their drawings' Word 2010 extensions are and that their drawing ids
|
|
# are unique, and draws each page as a PNG with LibreOffice. The Demos workflow checks them with the Open XML SDK validator.
|
|
#
|
|
# Usage: scripts/render-shape-demos.sh [output directory] [demo ...]
|
|
#
|
|
# Each demo is its topic and name, such as shapes/shape-groups. The output directory (default build/shape-demos) gets
|
|
# each demo's .docx, .pdf and one PNG per page, named after the demo. Without demos, all the shape, chart and math demos
|
|
# are run. Needs the package built (npm run build) and perl.
|
|
#
|
|
# The pages are drawn with LibreOffice (set SOFFICE if soffice isn't on the PATH) and pdftoppm from Poppler.
|
|
# When SHAPE_RENDER_IMAGE names a Docker image built from scripts/shape-demos/Dockerfile, they are drawn in
|
|
# that image instead, with the pinned LibreOffice and fonts the visual regression tests compare against:
|
|
#
|
|
# docker build --platform linux/amd64 -t docx-shape-renderer scripts/shape-demos
|
|
# SHAPE_RENDER_IMAGE=docx-shape-renderer scripts/render-shape-demos.sh
|
|
# npm run run-ts -- scripts/compare-shape-renders.ts
|
|
|
|
set -euo pipefail
|
|
|
|
OUT="${1:-build/shape-demos}"
|
|
shift || true
|
|
DEMOS=("$@")
|
|
if [ ${#DEMOS[@]} -eq 0 ]; then
|
|
DEMOS=(shapes/inline-shapes shapes/shapes shapes/shape-groups shapes/shape-connectors shapes/shape-styles shapes/shape-diagrams shapes/shape-layout shapes/shape-document-styles shapes/shape-swimlanes shapes/shape-page-layout styles/theme shapes/custom-shapes charts/charts charts/chart-options charts/charts-in-templates charts/chart-data-in-templates charts/more-charts math/matrices-and-alignment math/latex)
|
|
fi
|
|
SOFFICE="${SOFFICE:-soffice}"
|
|
|
|
mkdir -p "$OUT"
|
|
# Pages left over from an earlier run would be compared as if they were new
|
|
rm -f "$OUT"/*.png "$OUT"/*.pdf
|
|
failed=0
|
|
|
|
for demo in "${DEMOS[@]}"; do
|
|
echo "::group::$demo"
|
|
npm run --silent run-ts -- "./demo/$demo.ts"
|
|
docx="$OUT/$(basename "$demo").docx"
|
|
cp "My Document.docx" "$docx"
|
|
|
|
# Where the Word 2010 drawing extensions (wp14) are: a floating drawing's relative sizes come after its graphic, and
|
|
# a percentage offset is the choice for a position, with its offset in EMUs as the fallback
|
|
extracted="$(mktemp -d)"
|
|
unzip -q -o "$docx" -d "$extracted"
|
|
misplaced="$(perl -0ne '$all = () = /<wp14:(?:sizeRel[HV]|pctPos[HV]Offset)\b/g;
|
|
$placed = () = /<wp:position([HV]) relativeFrom="\w+"><mc:AlternateContent><mc:Choice Requires="wp14"><wp14:pctPos\1Offset>-?\d+<\/wp14:pctPos\1Offset><\/mc:Choice><mc:Fallback><wp:posOffset>-?\d+<\/wp:posOffset><\/mc:Fallback><\/mc:AlternateContent><\/wp:position\1>/g;
|
|
while (/<\/a:graphic>((?:<wp14:sizeRel[HV]\b.*?<\/wp14:sizeRel[HV]>)+)<\/wp:anchor>/gs) { my $sizes = $1; $placed += () = $sizes =~ /<wp14:sizeRel[HV]\b/g; }
|
|
print $all - $placed' "$extracted/word/document.xml")"
|
|
if [ "$misplaced" != 0 ]; then
|
|
echo "::error::$demo has $misplaced Word 2010 drawing extensions where they don't go"
|
|
failed=1
|
|
fi
|
|
|
|
# Drawing ids must be unique across the document. grep finds none in a document without drawings, such as math's
|
|
repeated="$( (grep -ho '\(wp:docPr\|wps:cNvPr\|pic:cNvPr\|wpg:cNvPr\) id="[0-9]*"' "$extracted"/word/*.xml || true) | sed 's/.*id=//' | sort | uniq -d)"
|
|
if [ -n "$repeated" ]; then
|
|
echo "::error::$demo repeats drawing ids: $repeated"
|
|
failed=1
|
|
fi
|
|
rm -rf "$extracted"
|
|
echo "::endgroup::"
|
|
done
|
|
|
|
# Draw the pages
|
|
echo "::group::Drawing the pages"
|
|
DRAW='for docx in "$@"; do
|
|
soffice ${PROFILE:+"-env:UserInstallation=file://$PROFILE"} --headless --convert-to pdf --outdir "$(dirname "$docx")" "$docx" > /dev/null 2>&1
|
|
pdftoppm -png -r 96 "${docx%.docx}.pdf" "${docx%.docx}"
|
|
done'
|
|
FILES=()
|
|
for demo in "${DEMOS[@]}"; do
|
|
FILES+=("$(basename "$demo").docx")
|
|
done
|
|
if [ -n "${SHAPE_RENDER_IMAGE:-}" ]; then
|
|
# LibreOffice keeps its settings in a profile, which has to be somewhere the user running it can write
|
|
docker run --rm --platform linux/amd64 --user "$(id -u):$(id -g)" -e PROFILE=/tmp/profile -v "$(cd "$OUT" && pwd):/work" -w /work \
|
|
"$SHAPE_RENDER_IMAGE" sh -c "$DRAW" sh "${FILES[@]}"
|
|
else
|
|
(cd "$OUT" && PATH="$(dirname "$(command -v "$SOFFICE")"):$PATH" sh -c "$DRAW" sh "${FILES[@]}")
|
|
fi
|
|
echo "::endgroup::"
|
|
|
|
exit $failed
|