# SVG fixtures (tools/fixtures.py): hand-written SVGs, one feature area each, rendered by resvg (nixpkgs#resvg, the
# reference rasterizer), and the same documents decoded and written back by our encoder (tools/fixtures/_ours.luau)
# then rendered by resvg too:
# tests/gen/svg.luau { cases = { { name, svg, width, height, ref (resvg RGBA), ours (our SVG text), ours_ref
# (resvg's RGBA of our SVG) } } }
import json, os, shutil, subprocess, tempfile
INPUTS = ["src/Rosetta/vector/svg", "tools/fixtures/_ours.luau"]
def svg(w, h, body, extra=""):
return '\n' % (w, h, extra, body)
CASES = {
"shapes": svg(64, 64, """
"""),
"path-commands": svg(64, 64, """
"""),
"arcs": svg(64, 64, """
"""),
"transforms": svg(64, 64, """
"""),
"stroke-styles": svg(64, 64, """
"""),
"dashes": svg(64, 64, """
"""),
"fill-rule": svg(64, 32, """
"""),
"opacity": svg(64, 64, """
"""),
"linear-gradient": svg(64, 64, """
"""),
"radial-gradient": svg(64, 64, """
"""),
"gradient-href": svg(64, 32, """
"""),
"css": svg(64, 64, """
"""),
"use-symbol": svg(64, 64, """
"""),
"viewbox-meet": svg(64, 40, """
""", ' viewBox="0 0 100 100" preserveAspectRatio="xMinYMax meet"'),
"viewbox-slice": svg(64, 40, """
""", ' viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice"'),
"viewbox-none": svg(64, 40, """
""", ' viewBox="0 0 100 100" preserveAspectRatio="none"'),
"nested-svg": svg(64, 64, """
"""),
"clip-path": svg(64, 64, """
"""),
"units": svg(64, 64, """
"""),
"colors": svg(64, 32, """
"""),
"inheritance": svg(64, 64, """
"""),
}
def resvg(ctx, data, w, h):
with tempfile.TemporaryDirectory() as d:
src, png = os.path.join(d, "in.svg"), os.path.join(d, "out.png")
open(src, "wb").write(data)
argv = ["resvg", "-w", str(w), "-h", str(h), src, png]
if shutil.which("resvg"):
subprocess.run(argv, check=True, capture_output=True)
else:
subprocess.run(["nix", "shell", "nixpkgs#resvg", "-c"] + argv, check=True, capture_output=True)
argv = ["magick", png, "-depth", "8", "rgba:-"]
try:
return subprocess.run(argv, check=True, capture_output=True).stdout
except (OSError, subprocess.CalledProcessError):
return ctx.nix("imagemagick", argv)
def ours(ctx, text):
script = os.path.join(ctx.root, "tools", "fixtures", "_ours.luau")
luau = shutil.which("luau")
argv = [luau, script, "-a", "svg", text] if luau else ["nix", "shell", "nixpkgs#luau", "-c", "luau", script, "-a", "svg", text]
return json.loads(subprocess.run(argv, check=True, capture_output=True, cwd=ctx.root).stdout)["svg"]
def generate(ctx):
import re
cases = []
for name, text in CASES.items():
w, h = (int(x) for x in re.search(r'width="(\d+)" height="(\d+)"', text).groups())
data = text.encode()
mine = ours(ctx, text)
cases.append({"name": name, "svg": data, "width": w, "height": h, "ref": resvg(ctx, data, w, h), "ours": mine,
"ours_ref": resvg(ctx, mine.encode(), w, h)})
ctx.emit("svg", {"cases": cases})