101 lines
3.7 KiB
Luau
101 lines
3.7 KiB
Luau
-- font/woff2 tests: every fixture font as WOFF2 (fontTools: the default glyf / loca transform, and with the hmtx transform too) decodes to the same font as the sfnt itself (font/ttf, checked against
|
|
-- fontTools / FreeType in tests/font/ttf.luau): metrics, names, every glyph's outline, advance and side bearing,
|
|
-- cmap and kerning samples; rasterize works; the api detects it; errors; the fuzz pass. Fixtures: tools/fixtures/fonts.py.
|
|
local T = require("@tests/T")
|
|
local F = require("@rosetta")
|
|
local Woff2 = require("@rosetta/font/woff2")
|
|
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("woff2")
|
|
t:eq(Woff2.name, "woff2", "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 { "woff2", "woff2h" } do
|
|
local data = w[key]
|
|
local font, err = Woff2.decode(Bytes.reader(buffer.fromstring(data)))
|
|
t:check(font, w.name .. " (" .. key .. "): " .. tostring(err))
|
|
t:eq(font.meta.source.format, "woff2", "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")
|
|
|
|
-- the api
|
|
do
|
|
local data = Fx.woff[1].woff2
|
|
t:eq(F.detect(data), "woff2", "F.detect")
|
|
local f = F.font.load(data)
|
|
t:check(f and f.kind == "font", "F.font.load")
|
|
t:eq(Woff2.sniff(Bytes.reader(buffer.fromstring("wOFF"))) + Woff2.sniff(Bytes.reader(buffer.fromstring("wOF2"))), 1, "sniff")
|
|
end
|
|
|
|
-- errors
|
|
do
|
|
local data = Fx.woff[1].woff2
|
|
local _, e1 = Woff2.decode(Bytes.reader(buffer.fromstring(string.sub(data, 1, 30))))
|
|
t:check(e1 and e1.code == Err.TRUNCATED, "truncated header: " .. tostring(e1))
|
|
local _, e2 = Woff2.decode(Bytes.reader(buffer.fromstring(string.sub(data, 1, #data - 100))))
|
|
t:check(e2 ~= nil, "truncated data: " .. tostring(e2))
|
|
local _, e3 = Woff2.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 = Woff2.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()
|