-- font/fon tests: Windows .fnt and NE .fon fonts (one and two strikes) made by FontForge from the Spleen BDFs against -- FreeType's renders (ASCII: FreeType maps FNT byte codes), strikes and size selection; errors; the fuzz pass. -- Fixtures: tools/fixtures/bitmapfonts.py. local T = require("@tests/T") local F = require("@rosetta") local Fon = require("@rosetta/font/fon") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local B = require("@tests/font/_bitmap") local Fx = require("@tests/gen/bitmapfonts") local t = T.suite("fon") t:eq(Fon.name, "fon", "name") local samples = {} local n = 0 for _, c in Fx.fonts do if c.format == "fon" then n += 1 local font, err = Fon.decode(Bytes.reader(buffer.fromstring(c.data))) t:check(font, c.name .. ": " .. tostring(err)) if font then B.compare(t, font, c.ref, c.name) t:eq(font.ascent, c.ref.ascent, c.name .. ": ascent") if string.find(c.name, "two", 1, true) then t:eq(#font.meta.strikes, 2, c.name .. ": two strikes") local big = Fon.decode(Bytes.reader(buffer.fromstring(c.data)), { size = 24 }) :: any t:eq(big.unitsPerEm, 24, c.name .. ": size selection") end end t:eq(F.detect(c.data), "fon", c.name .. ": detect") table.insert(samples, c.data) end end t:check(n >= 3, "has cases") local function code(s: string): string? local _, e = Fon.decode(Bytes.reader(buffer.fromstring(s))) return e and e.code end t:eq(code("\0\2\0\0"), Err.TRUNCATED, "short fnt") local vec = string.rep("\0", 200) vec = "\0\2" .. string.sub(vec, 3, 66) .. "\1\0" .. string.sub(vec, 69) t:eq(code(vec), Err.UNSUPPORTED, "vector font") t:eq(code("MZ" .. string.rep("\0", 100)), Err.CORRUPT, "MZ without NE") t:fuzz("decode", function(b: buffer) return Fon.decode(Bytes.reader(b)) end, samples, { mutations = 30 }) t:done()