oxedyne/fe2o3/fe2o3_graphics/tests/svg/gen.sh
2.2 KiB, 1 run, executable
created by r1870400018:14465, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #!/bin/bash |
| 2 | # Render each `.path` fixture to the `.png` beside it, using a browser's SVG implementation. |
| 3 | # |
| 4 | # The PNGs are the oracle for `tests/svg_oracle.rs` and are committed alongside the data. Nothing |
| 5 | # here runs during a test: regenerate only when a fixture changes, and look at the result before |
| 6 | # keeping it. |
| 7 | # |
| 8 | # Each `.path` file is a viewBox on its first line, as `# minx miny width height`, and the path data |
| 9 | # on the rest. That split is what lets the fixture be shared: the browser reads it as an SVG |
| 10 | # attribute and the test reads the identical bytes into its own parser. No XML is involved on either |
| 11 | # side. |
| 12 | # |
| 13 | # The page is drawn on a transparent background with a black fill, so the alpha channel of the PNG |
| 14 | # *is* the coverage the renderer computed, with no luminance conversion to argue about. |
| 15 | set -euo pipefail |
| 16 | |
| 17 | DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 18 | SIZE=256 |
| 19 | |
| 20 | # A browser that is not confined by a snap sandbox is preferred, since a snap cannot write outside |
| 21 | # $HOME and this directory may sit anywhere. |
| 22 | CHROME="" |
| 23 | for c in \ |
| 24 | "$HOME/.cache/ms-playwright/chromium-1229/chrome-linux64/chrome" \ |
| 25 | "$HOME/.cache/ms-playwright/chromium-1228/chrome-linux64/chrome" \ |
| 26 | "$(command -v google-chrome || true)" \ |
| 27 | "$(command -v chromium || true)" |
| 28 | do |
| 29 | if [ -n "$c" ] && [ -x "$c" ]; then |
| 30 | CHROME="$c" |
| 31 | break |
| 32 | fi |
| 33 | done |
| 34 | if [ -z "$CHROME" ]; then |
| 35 | echo "No Chrome or Chromium found to render with." >&2 |
| 36 | exit 1 |
| 37 | fi |
| 38 | echo "Rendering with: $CHROME" |
| 39 | |
| 40 | TMP="$(mktemp -d)" |
| 41 | trap 'rm -rf "$TMP"' EXIT |
| 42 | |
| 43 | for p in "$DIR"/*.path; do |
| 44 | name="$(basename "$p" .path)" |
| 45 | view="$(head -1 "$p" | sed 's/^#[[:space:]]*//')" |
| 46 | data="$(tail -n +2 "$p")" |
| 47 | cat > "$TMP/$name.svg" <<EOF |
| 48 | <svg xmlns="http://www.w3.org/2000/svg" width="$SIZE" height="$SIZE" viewBox="$view"> |
| 49 | <path d="$data" fill="#000"/> |
| 50 | </svg> |
| 51 | EOF |
| 52 | "$CHROME" --headless=new --no-sandbox --disable-gpu --hide-scrollbars \ |
| 53 | --force-device-scale-factor=1 --default-background-color=00000000 \ |
| 54 | --window-size="$SIZE,$SIZE" --virtual-time-budget=3000 \ |
| 55 | --screenshot="$TMP/$name.png" "file://$TMP/$name.svg" >/dev/null 2>&1 || true |
| 56 | if [ ! -f "$TMP/$name.png" ]; then |
| 57 | echo " FAILED to render $name" >&2 |
| 58 | exit 1 |
| 59 | fi |
| 60 | mv "$TMP/$name.png" "$DIR/$name.png" |
| 61 | echo " $name.png" |
| 62 | done |
| 63 | echo "Done. Look at the PNGs before keeping them." |