Files

97 lines
3.3 KiB
Luau

-- font/woff tests: fontTools' WOFF 1.0 decodes to the same font as the sfnt (tools/fixtures/fonts.py).
local T = require("@tests/T")
local F = require("@rosetta")
local Woff = require("@rosetta/font/woff")
local Ttf = require("@rosetta/font/ttf")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/fonts")
local t = T.suite("woff")
t:eq(Woff.name, "woff", "name")
local function same(a: any, b: any, label: string)
for _, k in { "unitsPerEm", "ascent", "descent", "lineGap", "numGlyphs" } do
t:eq(a[k], b[k], label .. ": " .. k)
end
t:eq(a.info.family, b.info.family, label .. ": family")
t:eq(a.info.fullName, b.info.fullName, label .. ": fullName")
for g = 0, a.numGlyphs - 1 do
t:eq(a:advance(g), b:advance(g), label .. ": advance " .. g)
t:eq(a:leftSideBearing(g), b:leftSideBearing(g), label .. ": lsb " .. g)
local oa, ob = a:outline(g), b:outline(g)
t:check(oa and ob and #oa == #ob, label .. ": outline " .. g .. " command count")
for i, c in oa do
local d = ob[i]
local ok = c.op == d.op and #c == #d
for j = 1, #c do
ok = ok and c[j] == d[j]
end
if not ok then
t:check(false, string.format("%s: glyph %d command %d differs", label, g, i))
end
end
end
for cp = 0, 0x2FF do
t:eq(a:glyphIndex(cp), b:glyphIndex(cp), label .. ": cmap " .. cp)
end
local ga, gv = a:glyphIndex(65), a:glyphIndex(86)
t:eq(a:kern(ga, gv), b:kern(ga, gv), label .. ": kern A V")
end
local refs: { [string]: string } = {}
for _, c in Fx.fonts do
refs[c.name] = c.data
end
local samples = {}
local n = 0
for _, w in Fx.woff do
for _, key in { "woff" } do
local data = w[key]
local font, err = Woff.decode(Bytes.reader(buffer.fromstring(data)))
t:check(font, w.name .. " (" .. key .. "): " .. tostring(err))
t:eq(font.meta.source.format, "woff", "source format")
local ref = assert(Ttf.decode(Bytes.reader(buffer.fromstring(refs[w.name]))))
same(font, ref, w.name .. " (" .. key .. ")")
local img = font:rasterize(font:glyphIndex(65), 24)
t:check(img and img.width > 0, w.name .. ": rasterize")
n += 1
if #data < 40000 then
table.insert(samples, data)
end
t:note("%-20s %-7s %7d bytes (sfnt %d), %d glyphs identical", w.name, key, #data, #refs[w.name], font.numGlyphs)
end
end
t:check(n >= 6, "fixture count")
do
local data = Fx.woff[1].woff
t:eq(F.detect(data), "woff", "F.detect")
local f = F.font.load(data)
t:check(f and f.kind == "font", "F.font.load")
t:eq(Woff.sniff(Bytes.reader(buffer.fromstring("wOFF"))) + Woff.sniff(Bytes.reader(buffer.fromstring("wOF2"))), 1, "sniff")
end
do
local data = Fx.woff[1].woff
local _, e1 = Woff.decode(Bytes.reader(buffer.fromstring(string.sub(data, 1, 30))))
t:check(e1 and e1.code == Err.TRUNCATED, "truncated header: " .. tostring(e1))
local _, e2 = Woff.decode(Bytes.reader(buffer.fromstring(string.sub(data, 1, #data - 100))))
t:check(e2 ~= nil, "truncated data: " .. tostring(e2))
local _, e3 = Woff.decode(Bytes.reader(buffer.fromstring("ttcf" .. string.rep("\0", 60))))
t:check(e3 and e3.code == Err.CORRUPT, "bad signature: " .. tostring(e3))
end
t:fuzz("decode", function(b: buffer)
local f = Woff.decode(Bytes.reader(b))
if f then
for g = 0, math.min(f.numGlyphs, 40) - 1 do
f:outline(g)
f:rasterize(g, 8)
end
end
return f
end, samples, { mutations = 25, garbage = 200 })
t:done()