Files
rosetta/tests/vector/svg.luau
T

152 lines
7.1 KiB
Luau

-- vector/svg tests: hand-written SVGs, one feature area each (shapes, every path command, arcs, transforms, stroke
-- joins / caps / dashes, fill rules, opacity, gradients, href, CSS, use / symbol, viewBox + preserveAspectRatio, nested
-- svg, clip paths, units, colours, inheritance), rasterized by us and compared with resvg by PSNR; our SVG writer's
-- output rendered by resvg against resvg's rendering of the original (tools/fixtures/_ours.luau); path data and
-- colour parsing details; round trips; errors; the fuzz pass. Fixtures: tools/fixtures/svg.py.
local T = require("@tests/T")
local Svg = require("@rosetta/vector/svg")
local PathData = require("@rosetta/vector/svg/PathData")
local Style = require("@rosetta/vector/svg/Style")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/svg")
local t = T.suite("svg")
local function decode(s: string): (any, any)
return Svg.decode(Bytes.reader(buffer.fromstring(s)))
end
-- PSNR over premultiplied RGBA (the colour of transparent pixels does not matter)
local function psnr(a: buffer | string, b: string): number
local sa = if type(a) == "string" then a else buffer.tostring(a)
local n = math.min(#sa, #b)
local se = 0
for i = 1, n, 4 do
local aa, ab = string.byte(sa, i + 3), string.byte(b, i + 3)
for k = 0, 2 do
local d = (string.byte(sa, i + k) * aa - string.byte(b, i + k) * ab) / 255
se += d * d
end
se += (aa - ab) * (aa - ab)
end
if se == 0 then
return 99
end
return 10 * math.log10(255 * 255 / (se / n))
end
t:eq(Svg.name, "svg", "name")
t:eq(Svg.category, "vector", "category")
t:check(Svg.caps.decode and Svg.caps.encode, "caps")
-- antialiasing differs between rasterizers (resvg: tiny-skia's analytic AA; ours: exact area coverage), so edges
-- cost a few dB; the floor catches wrong geometry / paint
local FLOOR = 30
local samples = {}
local worst, worstName = 99, ""
for _, c in Fx.cases do
local v, err = decode(c.svg)
t:check(v, c.name .. ": " .. tostring(err))
t:eq(v.kind, "vector", c.name .. ": kind")
local img = Svg.rasterize(v, c.width, c.height) :: any
t:check(img and img.width == c.width and img.height == c.height, c.name .. ": raster size")
local p = psnr(img.pixels, c.ref)
t:check(p >= FLOOR, string.format("%s: ours vs resvg %.1f dB", c.name, p))
t:note("%-16s ours vs resvg %5.1f dB, resvg(our SVG) vs resvg %5.1f dB", c.name, p, psnr(c.ours_ref, c.ref))
if p < worst then
worst, worstName = p, c.name
end
-- the writer: resvg renders our SVG like the original
t:check(#c.ours > 0, c.name .. ": our encoder wrote SVG")
local wp = psnr(c.ours_ref, c.ref)
t:check(wp >= 35, string.format("%s: resvg on our SVG vs the original %.1f dB", c.name, wp))
-- and we read our own SVG back to the same picture
local back = decode(c.ours)
t:check(back, c.name .. ": decode our SVG")
local img2 = Svg.rasterize(back, c.width, c.height) :: any
t:check(psnr(img2.pixels, buffer.tostring(img.pixels)) >= 40, c.name .. ": our SVG rasterizes like the original")
table.insert(samples, c.svg)
end
t:note("worst: %s %.1f dB", worstName, worst)
-- path data
do
local cmds, ok = PathData.parse("M1.5.5L-2-3e1 1e-1.2zm1,1 h2 v2 a1 1 0 0 1 2 2")
t:check(ok, "parse ok")
t:check(cmds[1].op == "M" and cmds[1][1] == 1.5 and cmds[1][2] == 0.5, "1.5.5 is two numbers")
t:check(cmds[2].op == "L" and cmds[2][1] == -2 and cmds[2][2] == -30, "-2-3e1")
t:check(cmds[3].op == "L" and cmds[3][1] == 0.1 and cmds[3][2] == 0.2, "implicit line-to after an exponent")
t:eq(cmds[4].op, "Z", "close")
t:check(cmds[5].op == "M" and cmds[5][1] == 2.5 and cmds[5][2] == 1.5, "relative move after close is from the start")
t:check(cmds[6].op == "L" and cmds[6][1] == 4.5, "h")
t:eq(cmds[#cmds].op, "C", "arc as cubics")
t:check(math.abs(cmds[#cmds][5] - 6.5) < 1e-9 and math.abs(cmds[#cmds][6] - 5.5) < 1e-9, "arc end point exact")
local part, ok2 = PathData.parse("M0 0 L10 10 L oops")
t:check(not ok2 and #part == 2, "an error keeps the commands before it")
local x0, y0, x1, y1 = PathData.bounds(PathData.parse("M0 0 C 10 -10 20 10 30 0"))
t:check(x0 == 0 and x1 == 30 and math.abs((y0 :: number) + 2.886751) < 1e-4 and math.abs((y1 :: number) - 2.886751) < 1e-4, "cubic bounds")
t:eq(PathData.serialize({ { op = "M", 1, 2.5 }, { op = "C", 0.12345, 1, 2, 3, 4, 5 }, { op = "Z" } }), "M 1 2.5 C 0.1235 1 2 3 4 5 Z", "serialize")
end
-- colours and lengths
do
local function col(s: string): string
local c = Style.color(s)
return if type(c) == "table" then table.concat(c, ",") else tostring(c)
end
t:eq(col("#f80"), "255,136,0,255", "#rgb")
t:eq(col("#ff008080"), "255,0,128,128", "#rrggbbaa")
t:eq(col("rgb(10%, 50%, 90%)"), "26,128,230,255", "rgb %")
t:eq(col("hsl(120, 100%, 25%)"), "0,128,0,255", "hsl")
t:eq(col("RebeccaPurple"), "102,51,153,255", "named, any case")
t:eq(col("none"), "none", "none")
t:eq(col("bogus"), "nil", "not a colour")
t:eq(Style.length("2mm"), 2 * 96 / 25.4, "mm")
t:eq(Style.length("50%", 64), 32, "%")
t:eq(Style.length("1.5em", nil, 10), 15, "em")
local m = Style.transform("translate(10) rotate(90) scale(2 3)") :: { number }
t:check(math.abs(m[1]) < 1e-12 and math.abs(m[2] - 2) < 1e-12 and math.abs(m[3] + 3) < 1e-12 and m[5] == 10, "transform list")
t:eq(Style.transform("translate(1,"), nil, "bad transform")
end
-- the model
do
local v = decode(Fx.cases[1].svg)
t:check(#v.paths == 7, "shapes: one path each")
t:check(v.paths[1].fill.type == "color" and v.paths[1].fill.color[1] == 255, "fill red")
t:eq(v.paths[2].strokeWidth, 2, "stroke width")
local text = decode("<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><text>hi</text><image href='x.png'/><rect width='5' height='5'/></svg>")
t:check(text.meta.unsupported.text == 1 and text.meta.unsupported.image == 1 and #text.paths == 1, "unsupported elements counted")
local ns = decode("<s:svg xmlns:s='http://www.w3.org/2000/svg' width='4' height='4'><s:rect width='4' height='4'/></s:svg>")
t:check(ns and #ns.paths == 1, "prefixed SVG namespace")
-- a round trip keeps the paths
local back = decode(buffer.tostring(Svg.encode(v) :: buffer))
t:eq(#back.paths, #v.paths, "round trip path count")
t:eq(Svg.sniff(Bytes.reader(buffer.fromstring(Fx.cases[1].svg))), 0.95, "sniff")
t:eq(Svg.sniff(Bytes.reader(buffer.fromstring("<html></html>"))), 0, "sniff html")
end
-- errors
do
local _, e = decode("<html/>")
t:check(e and e.code == Err.CORRUPT, "not svg: " .. tostring(e))
local _, e2 = decode("<svg><rect")
t:check(e2 ~= nil, "bad XML")
local _, e3 = Svg.encode({} :: any)
t:check(e3 and e3.code == Err.ARG, "encode a non-Vector")
local _, e4 = Svg.rasterize(decode(Fx.cases[1].svg), 0, 10)
t:check(e4 and e4.code == Err.ARG, "rasterize 0 x 10")
-- hostile: deep use recursion, huge coordinates
local r = decode("<svg xmlns='http://www.w3.org/2000/svg' width='8' height='8'><g id='a'><use href='#a'/><rect width='1e30' height='1e30'/></g></svg>")
t:check(r ~= nil and Svg.rasterize(r, 8, 8) ~= nil, "self-referencing use, huge rect")
end
t:fuzz("decode + rasterize", function(b: buffer)
local v = Svg.decode(Bytes.reader(b))
if v then
return Svg.rasterize(v, 16, 16)
end
return nil
end, samples, { mutations = 4, garbage = 50 })
t:done()