#!/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 = () = /-?\d+<\/wp14:pctPos\1Offset><\/mc:Choice>-?\d+<\/wp:posOffset><\/mc:Fallback><\/mc:AlternateContent><\/wp:position\1>/g; while (/<\/a:graphic>((?:)+)<\/wp:anchor>/gs) { my $sizes = $1; $placed += () = $sizes =~ /