83 lines
3.5 KiB
Luau
83 lines
3.5 KiB
Luau
-- font/psf tests: kbd / Spleen / Tamzen / Terminus / Cozette console fonts (PSF 1 and 2, with and without Unicode
|
|
-- tables) against an independent parser (sizes, glyph bitmaps) and kbd's psfgettable (every code point -> glyph
|
|
-- pair); our PSF 2 / PSF 1 output read back by psfgettable and the parser (fixture step) and round-tripping; an
|
|
-- outline font written as PSF; .psfu.gz through the api; errors; the fuzz pass. Fixtures: tools/fixtures/psf.py.
|
|
local T = require("@tests/T")
|
|
local F = require("@formats")
|
|
local Psf = require("@formats/font/psf")
|
|
local Ttf = require("@formats/font/ttf")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Err = require("@formats/core/Err")
|
|
local Deflate = require("@formats/codecs/compress/Deflate")
|
|
local Fx = require("@tests/gen/psf")
|
|
local Fonts = require("@tests/gen/fonts")
|
|
|
|
local t = T.suite("psf")
|
|
t:eq(Psf.name, "psf", "name")
|
|
|
|
local samples = {}
|
|
for _, c in Fx.fonts do
|
|
local data = buffer.fromstring(c.data)
|
|
if c.gz then
|
|
data = Deflate.gunzip(data, 0, buffer.len(data)) :: buffer
|
|
end
|
|
local font, err = Psf.decode(Bytes.reader(data))
|
|
t:check(font, c.name .. ": " .. tostring(err))
|
|
if font then
|
|
t:eq(font.meta.source.version, c.version, c.name .. ": version")
|
|
t:eq(#font.bitmap.glyphs, c.count, c.name .. ": glyph count")
|
|
t:eq(font.meta.width, c.width, c.name .. ": width")
|
|
t:eq(font.meta.height, c.height, c.name .. ": height")
|
|
for _, g in c.glyphs do
|
|
local img = font:rasterize(g.index, c.height)
|
|
t:check(img and buffer.tostring(img.pixels) == g.pixels, c.name .. ": glyph " .. g.index)
|
|
end
|
|
local mismatched = 0
|
|
for _, p in c.table do
|
|
local gi = font:glyphIndex(p.cp)
|
|
-- a code point on several glyphs maps to the first: the glyph images are equal or the pair is an alias
|
|
if gi ~= p.glyph and font.meta.unicodeTable[gi + 1] == nil then
|
|
mismatched += 1
|
|
end
|
|
end
|
|
t:eq(mismatched, 0, c.name .. ": Unicode table (" .. #c.table .. " pairs)")
|
|
t:check(c.ours2.ok, c.name .. ": our PSF 2 reads back: " .. c.ours2.detail)
|
|
if c.ours1 then
|
|
t:check(c.ours1.ok, c.name .. ": our PSF 1 reads back: " .. c.ours1.detail)
|
|
end
|
|
local back = Psf.decode(Bytes.reader(Psf.encode(font) :: buffer)) :: any
|
|
t:check(back and #back.bitmap.glyphs == c.count and back:glyphIndex(65) == font:glyphIndex(65), c.name .. ": round trip")
|
|
end
|
|
t:eq(F.detect(data), "psf", c.name .. ": detect")
|
|
if c.gz then
|
|
local v = F.load(c.data, { name = c.name }) :: any
|
|
t:check(v and v.kind == "font" and v.meta.compression == "gzip", c.name .. ": F.load unwraps the gzip")
|
|
end
|
|
if #samples < 3 then
|
|
table.insert(samples, data)
|
|
end
|
|
end
|
|
|
|
-- an outline font as PSF 2
|
|
local ttf = Ttf.decode(Bytes.reader(buffer.fromstring(Fonts.fonts[1].data)))
|
|
local out, err = Psf.encode(ttf, { px = 16, chars = "AB" })
|
|
t:check(out, "ttf -> psf: " .. tostring(err))
|
|
local back = Psf.decode(Bytes.reader(out :: buffer)) :: any
|
|
t:check(back and back:glyphIndex(66) == 2 and back.meta.source.version == 2, "ttf -> psf glyphs")
|
|
|
|
-- errors
|
|
local function code(s: string): string?
|
|
local _, e = Psf.decode(Bytes.reader(buffer.fromstring(s)))
|
|
return e and e.code
|
|
end
|
|
t:eq(code("\54\4\0\16"), Err.TRUNCATED, "psf1 without glyphs")
|
|
t:eq(code("\114\181\74\134"), Err.TRUNCATED, "psf2 header cut")
|
|
t:eq(code("\114\181\74\134\0\0\0\0\32\0\0\0\0\0\0\0\1\0\0\0\99\0\0\0\8\0\0\0\8\0\0\0"), Err.CORRUPT, "bad charsize")
|
|
t:eq(code("hello"), Err.CORRUPT, "not psf")
|
|
t:eq((select(2, Psf.encode(back, { version = 3 })) :: any).code, Err.ARG, "bad version")
|
|
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Psf.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 30 })
|
|
t:done()
|