diff --git a/docs/FORMATS.md b/docs/FORMATS.md index 0b59f75..62e2561 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -19,6 +19,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build | archive | zst | .zst .tzst | yes | - | stream: true | `tests/archive/zst.luau` | | data | json | .json .jsonl .ndjson .geojson | yes | yes | ordered: true, streaming: true | `tests/data/json.luau` | | data | xml | .xml .xsd .xsl .xslt .rss .atom .xhtml | yes | yes | namespaces: true, streaming: true | `tests/data/xml.luau` | +| vector | svg | .svg | yes | yes | rasterize: true, text: false | `tests/vector/svg.luau` | | vector | tiled | .tmx .tmj .tsx .tsj | yes | yes | tilesets: true, infinite: true | `tests/vector/tiled.luau` | | layered | aseprite | .aseprite .ase | yes | yes | layers: true, groups: true, animation: true, depths: 8, 16, 32 | `tests/layered/aseprite.luau` | | layered | psd | .psd .psb | yes | yes | layers: true, groups: true, depths: 1, 8, 16, 32 | `tests/layered/psd.luau` | diff --git a/src/Formats/Registry.luau b/src/Formats/Registry.luau index 4217f21..648a729 100644 --- a/src/Formats/Registry.luau +++ b/src/Formats/Registry.luau @@ -182,6 +182,18 @@ local list: { Entry } = { return require("@formats/data/xml") end, }, + { + name = "svg", + category = "vector", + path = "vector/svg", + extensions = { "svg" }, + mime = { "image/svg+xml" }, + deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml", "codecs/Raster", "codecs/Blend" }, + caps = { decode = true, encode = true, rasterize = true, text = false }, + load = function() + return require("@formats/vector/svg") + end, + }, { name = "tiled", category = "vector", diff --git a/src/Formats/vector/svg/Parse.luau b/src/Formats/vector/svg/Parse.luau new file mode 100644 index 0000000..86001c7 --- /dev/null +++ b/src/Formats/vector/svg/Parse.luau @@ -0,0 +1,758 @@ +--!strict +-- vector/svg/Parse: an SVG document (a codecs/Xml tree) to a Types.Vector: the cascade (presentation attributes, +-- + + + + + + """), + "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})