203 lines
13 KiB
Python
203 lines
13 KiB
Python
# 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/Formats/vector/svg", "tools/fixtures/_ours.luau"]
|
|
|
|
def svg(w, h, body, extra=""):
|
|
return '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="%d" height="%d"%s>\n%s\n</svg>\n' % (w, h, extra, body)
|
|
|
|
CASES = {
|
|
"shapes": svg(64, 64, """
|
|
<rect x="4" y="4" width="20" height="14" fill="red"/>
|
|
<rect x="30" y="4" width="28" height="16" rx="5" fill="#0a0" stroke="black" stroke-width="2"/>
|
|
<circle cx="14" cy="34" r="9" fill="blue"/>
|
|
<ellipse cx="44" cy="34" rx="14" ry="7" fill="orange" stroke="purple"/>
|
|
<line x1="4" y1="50" x2="30" y2="60" stroke="black" stroke-width="3"/>
|
|
<polyline points="34,60 40,48 46,58 52,46 60,60" fill="none" stroke="teal" stroke-width="2"/>
|
|
<polygon points="4,62 10,52 16,62" fill="magenta"/>"""),
|
|
"path-commands": svg(64, 64, """
|
|
<path d="M4 4 L20 4 H28 V12 L4 12 Z" fill="#c00"/>
|
|
<path d="m34 4 l10 0 h8 v8 l-18 0 z" fill="#0c0"/>
|
|
<path d="M4 20 C10 14 18 30 24 20 S36 26 40 20" fill="none" stroke="blue" stroke-width="2"/>
|
|
<path d="M4 34 Q14 24 24 34 T44 34 T60 30" fill="none" stroke="black" stroke-width="2"/>
|
|
<path d="m4 44 c4-6 12 6 16 0 s8-6 12 0 q4 6 8 0 t8 0" fill="none" stroke="#a0a" stroke-width="2"/>
|
|
<path d="M4,60L10,52,16,60zM20 60 52 60 36 50z" fill="gold" stroke="brown"/>"""),
|
|
"arcs": svg(64, 64, """
|
|
<path d="M8 16 A12 8 0 0 1 32 16" fill="none" stroke="red" stroke-width="2"/>
|
|
<path d="M36 16 A12 8 0 1 0 60 16" fill="none" stroke="blue" stroke-width="2"/>
|
|
<path d="M8 40 a10 10 0 1 1 20 0 a10 10 0 1 1 -20 0" fill="#9c9"/>
|
|
<path d="M36 32 A14 6 30 0 1 58 48" fill="none" stroke="black" stroke-width="2"/>
|
|
<path d="M36 58 A3 3 0 0 1 58 58" fill="none" stroke="purple" stroke-width="2"/>"""),
|
|
"transforms": svg(64, 64, """
|
|
<g transform="translate(32 32)">
|
|
<rect x="-6" y="-6" width="12" height="12" fill="red" transform="rotate(30)"/>
|
|
<g transform="scale(1.5, 0.75)"><circle cx="12" cy="-20" r="5" fill="blue"/></g>
|
|
<rect x="-28" y="10" width="14" height="10" fill="green" transform="skewX(20)"/>
|
|
<rect x="10" y="10" width="10" height="10" fill="orange" transform="matrix(1 0.3 -0.2 1 2 0) rotate(-15 15 15)"/>
|
|
</g>"""),
|
|
"stroke-styles": svg(64, 64, """
|
|
<polyline points="6,20 18,6 30,20" fill="none" stroke="black" stroke-width="6" stroke-linejoin="miter"/>
|
|
<polyline points="36,20 48,6 60,20" fill="none" stroke="black" stroke-width="6" stroke-linejoin="round"/>
|
|
<polyline points="6,40 18,26 30,40" fill="none" stroke="black" stroke-width="6" stroke-linejoin="bevel"/>
|
|
<polyline points="36,40 48,38 60,40" fill="none" stroke="black" stroke-width="5" stroke-miterlimit="1.5"/>
|
|
<line x1="8" y1="48" x2="56" y2="48" stroke="blue" stroke-width="5" stroke-linecap="butt"/>
|
|
<line x1="8" y1="55" x2="56" y2="55" stroke="blue" stroke-width="5" stroke-linecap="round"/>
|
|
<line x1="8" y1="62" x2="56" y2="62" stroke="blue" stroke-width="3" stroke-linecap="square"/>"""),
|
|
"dashes": svg(64, 64, """
|
|
<line x1="4" y1="8" x2="60" y2="8" stroke="black" stroke-width="3" stroke-dasharray="6 3"/>
|
|
<line x1="4" y1="18" x2="60" y2="18" stroke="red" stroke-width="3" stroke-dasharray="5,2,1" stroke-dashoffset="3"/>
|
|
<circle cx="20" cy="42" r="14" fill="none" stroke="blue" stroke-width="3" stroke-dasharray="8 4"/>
|
|
<rect x="40" y="30" width="20" height="24" fill="none" stroke="green" stroke-width="2" stroke-dasharray="4" stroke-linecap="round"/>"""),
|
|
"fill-rule": svg(64, 32, """
|
|
<path d="M16 2 L22 30 L2 12 L30 12 L10 30 Z" fill="navy" fill-rule="nonzero"/>
|
|
<path d="M48 2 L54 30 L34 12 L62 12 L42 30 Z" fill="navy" fill-rule="evenodd"/>"""),
|
|
"opacity": svg(64, 64, """
|
|
<rect x="2" y="2" width="60" height="60" fill="#eee"/>
|
|
<g opacity="0.5"><rect x="6" y="6" width="24" height="24" fill="red"/><rect x="16" y="16" width="24" height="24" fill="blue"/></g>
|
|
<rect x="34" y="6" width="24" height="24" fill="green" fill-opacity="0.4" stroke="black" stroke-width="4" stroke-opacity="0.6"/>
|
|
<circle cx="20" cy="48" r="12" fill="orange" stroke="purple" stroke-width="6" opacity="0.6"/>
|
|
<circle cx="46" cy="48" r="12" fill="rgba(0,0,255,0.5)"/>"""),
|
|
"linear-gradient": svg(64, 64, """
|
|
<defs>
|
|
<linearGradient id="a"><stop offset="0" stop-color="red"/><stop offset="0.5" stop-color="yellow"/><stop offset="1" stop-color="blue"/></linearGradient>
|
|
<linearGradient id="b" x1="0" y1="0" x2="0.3" y2="0" spreadMethod="reflect"><stop offset="0" stop-color="#000"/><stop offset="1" stop-color="#fff"/></linearGradient>
|
|
<linearGradient id="c" x1="0" y1="0" x2="0.25" y2="0.25" spreadMethod="repeat"><stop offset="0%" stop-color="green"/><stop offset="100%" stop-color="white" stop-opacity="0.2"/></linearGradient>
|
|
<linearGradient id="d" gradientTransform="rotate(90 0.5 0.5)"><stop offset="0" stop-color="purple"/><stop offset="1" stop-color="orange"/></linearGradient>
|
|
</defs>
|
|
<rect x="2" y="2" width="60" height="12" fill="url(#a)"/>
|
|
<rect x="2" y="16" width="60" height="12" fill="url(#b)"/>
|
|
<rect x="2" y="30" width="28" height="30" fill="url(#c)"/>
|
|
<circle cx="47" cy="45" r="14" fill="url(#d)" stroke="url(#a)" stroke-width="3"/>"""),
|
|
"radial-gradient": svg(64, 64, """
|
|
<defs>
|
|
<radialGradient id="r1"><stop offset="0" stop-color="white"/><stop offset="1" stop-color="blue"/></radialGradient>
|
|
<radialGradient id="r2" cx="0.5" cy="0.5" r="0.5" fx="0.25" fy="0.25"><stop offset="0" stop-color="yellow"/><stop offset="0.6" stop-color="red"/><stop offset="1" stop-color="black"/></radialGradient>
|
|
<radialGradient id="r3" gradientUnits="userSpaceOnUse" cx="16" cy="48" r="8" spreadMethod="repeat"><stop offset="0" stop-color="#fff"/><stop offset="1" stop-color="#080"/></radialGradient>
|
|
</defs>
|
|
<rect x="2" y="2" width="28" height="28" fill="url(#r1)"/>
|
|
<ellipse cx="47" cy="16" rx="15" ry="12" fill="url(#r2)"/>
|
|
<rect x="2" y="34" width="28" height="28" fill="url(#r3)"/>
|
|
<circle cx="47" cy="48" r="13" fill="url(#r2)" fill-opacity="0.7"/>"""),
|
|
"gradient-href": svg(64, 32, """
|
|
<defs>
|
|
<linearGradient id="base"><stop offset="0" stop-color="#f00"/><stop offset="1" stop-color="#00f"/></linearGradient>
|
|
<linearGradient id="tilted" xlink:href="#base" x2="0" y2="1"/>
|
|
<radialGradient id="rad" href="#base"/>
|
|
</defs>
|
|
<rect x="2" y="2" width="28" height="28" fill="url(#tilted)"/>
|
|
<rect x="34" y="2" width="28" height="28" fill="url(#rad)"/>"""),
|
|
"css": svg(64, 64, """
|
|
<style>
|
|
rect { fill: red; stroke: black }
|
|
.blue { fill: blue }
|
|
#special { fill: green; stroke-width: 4 }
|
|
circle.big, ellipse { fill: orange; }
|
|
/* a comment */
|
|
@media print { rect { fill: pink } }
|
|
</style>
|
|
<rect x="4" y="4" width="20" height="20"/>
|
|
<rect x="30" y="4" width="20" height="20" class="blue"/>
|
|
<rect x="4" y="30" width="20" height="20" class="blue" id="special"/>
|
|
<rect x="30" y="30" width="20" height="20" class="blue" style="fill: purple"/>
|
|
<circle class="big" cx="58" cy="58" r="5"/>
|
|
<ellipse cx="10" cy="58" rx="6" ry="4" fill="black"/>"""),
|
|
"use-symbol": svg(64, 64, """
|
|
<defs>
|
|
<g id="dot"><circle r="5" fill="red"/><circle r="2" fill="white"/></g>
|
|
<symbol id="sym" viewBox="0 0 10 10"><rect width="10" height="10" fill="blue"/><path d="M0 0 L10 10" stroke="white"/></symbol>
|
|
<rect id="bar" width="20" height="4"/>
|
|
</defs>
|
|
<use href="#dot" x="10" y="10"/>
|
|
<use xlink:href="#dot" transform="translate(30 10) scale(1.5)"/>
|
|
<use href="#sym" x="4" y="24" width="20" height="20"/>
|
|
<use href="#sym" x="30" y="24" width="30" height="15"/>
|
|
<use href="#bar" x="4" y="54" fill="green"/>
|
|
<use href="#bar" x="30" y="54" style="fill: orange" opacity="0.5"/>"""),
|
|
"viewbox-meet": svg(64, 40, """
|
|
<rect width="100" height="100" fill="#ccc"/><circle cx="50" cy="50" r="40" fill="red"/>""", ' viewBox="0 0 100 100" preserveAspectRatio="xMinYMax meet"'),
|
|
"viewbox-slice": svg(64, 40, """
|
|
<rect width="100" height="100" fill="#ccc"/><circle cx="50" cy="50" r="40" fill="blue"/>""", ' viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice"'),
|
|
"viewbox-none": svg(64, 40, """
|
|
<rect width="100" height="100" fill="#ccc"/><circle cx="50" cy="50" r="40" fill="green"/>""", ' viewBox="0 0 100 100" preserveAspectRatio="none"'),
|
|
"nested-svg": svg(64, 64, """
|
|
<rect width="64" height="64" fill="#eef"/>
|
|
<svg x="8" y="8" width="48" height="24" viewBox="0 0 20 10"><circle cx="5" cy="5" r="5" fill="red"/><circle cx="15" cy="5" r="5" fill="blue"/></svg>
|
|
<svg x="8" y="36" width="20" height="20"><rect x="5" y="5" width="50%" height="50%" fill="green"/><circle cx="20" cy="10" r="8" fill="red"/></svg>"""),
|
|
"clip-path": svg(64, 64, """
|
|
<defs>
|
|
<clipPath id="c1"><circle cx="16" cy="16" r="12"/></clipPath>
|
|
<clipPath id="c2" clipPathUnits="objectBoundingBox"><rect x="0.25" y="0" width="0.5" height="1"/></clipPath>
|
|
<clipPath id="c3"><path d="M34 34 h26 v26 h-26z M40 40 h14 v14 h-14z" clip-rule="evenodd"/></clipPath>
|
|
</defs>
|
|
<rect x="2" y="2" width="30" height="30" fill="red" clip-path="url(#c1)"/>
|
|
<g clip-path="url(#c2)"><rect x="34" y="2" width="28" height="28" fill="blue"/><circle cx="48" cy="16" r="10" fill="yellow"/></g>
|
|
<rect x="2" y="34" width="28" height="28" fill="green" clip-path="url(#c2)"/>
|
|
<rect x="34" y="34" width="28" height="28" fill="purple" clip-path="url(#c3)"/>"""),
|
|
"units": svg(64, 64, """
|
|
<rect x="2mm" y="2mm" width="10mm" height="5mm" fill="red"/>
|
|
<rect x="40pt" y="4" width="12pt" height="12pt" fill="blue"/>
|
|
<rect x="10%" y="50%" width="25%" height="10%" fill="green"/>
|
|
<circle cx="48" cy="48" r="1em" fill="orange" style="font-size: 8px"/>
|
|
<rect x="4" y="52" width="1in" height="0.1in" fill="black" transform="scale(0.5)"/>"""),
|
|
"colors": svg(64, 32, """
|
|
<g color="teal">
|
|
<rect x="0" y="0" width="8" height="16" fill="cornflowerblue"/>
|
|
<rect x="8" y="0" width="8" height="16" fill="#f80"/>
|
|
<rect x="16" y="0" width="8" height="16" fill="#ff008080"/>
|
|
<rect x="24" y="0" width="8" height="16" fill="rgb(10%, 50%, 90%)"/>
|
|
<rect x="32" y="0" width="8" height="16" fill="rgba(200, 0, 100, 0.5)"/>
|
|
<rect x="40" y="0" width="8" height="16" fill="hsl(120, 100%, 25%)"/>
|
|
<rect x="48" y="0" width="8" height="16" fill="currentColor"/>
|
|
<rect x="56" y="0" width="8" height="16" fill="transparent" stroke="currentColor"/>
|
|
<rect x="0" y="16" width="64" height="16" fill="none" stroke="DarkOrchid" stroke-width="4"/>
|
|
</g>"""),
|
|
"inheritance": svg(64, 64, """
|
|
<g fill="red" stroke="blue" stroke-width="3">
|
|
<rect x="4" y="4" width="24" height="24"/>
|
|
<g fill="green"><rect x="36" y="4" width="24" height="24" stroke="inherit"/></g>
|
|
<g style="stroke: none"><circle cx="16" cy="46" r="12"/></g>
|
|
<g display="none"><rect x="36" y="36" width="24" height="24"/></g>
|
|
<rect x="36" y="36" width="10" height="10" visibility="hidden"/>
|
|
<g visibility="hidden"><rect x="48" y="48" width="12" height="12" visibility="visible"/></g>
|
|
</g>"""),
|
|
}
|
|
|
|
|
|
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})
|