Files
rosetta/tests/font/pcf.luau
T

63 lines
2.3 KiB
Luau

-- font/pcf tests: PCF fonts compiled by bdftopcf with every glyph pad / scan unit / bit and byte order and -t, Tamzen
-- PCFs and X11 misc-misc .pcf.gz fonts against FreeType's renders (bitmaps, offsets, advances) and metrics; charsets
-- other than Unicode / Latin-1 stay unmapped; .pcf.gz through the api; errors; the fuzz pass.
-- Fixtures: tools/fixtures/bitmapfonts.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Pcf = require("@rosetta/font/pcf")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Deflate = require("@rosetta/codecs/compress/Deflate")
local B = require("@tests/font/_bitmap")
local Fx = require("@tests/gen/bitmapfonts")
local t = T.suite("pcf")
t:eq(Pcf.name, "pcf", "name")
local samples = {}
for _, c in Fx.fonts do
if c.format == "pcf" then
local data = buffer.fromstring(c.data)
if c.gz then
data = Deflate.gunzip(data, 0, buffer.len(data)) :: buffer
end
local font, err = Pcf.decode(Bytes.reader(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")
t:eq(-font.descent, c.ref.descent, c.name .. ": descent")
t:eq(#font.bitmap.glyphs, c.ref.numGlyphs - 1, c.name .. ": glyph count (FreeType counts one more)")
if string.find(c.name, "ISO8859-5", 1, true) then
t:check(font.meta.unicode == false and font:glyphIndex(65) == (font.bitmap.default or 0), c.name .. ": not mapped to Unicode")
end
end
t:eq(F.detect(data), "pcf", c.name .. ": detect")
if #samples < 3 then
table.insert(samples, data)
end
end
end
-- a .pcf.gz through the api: the gzip unwraps to the font (meta.compression = "gzip")
for _, c in Fx.fonts do
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" and #v.bitmap.glyphs > 100, c.name .. ": F.load unwraps the gzip")
end
end
-- errors
local function code(s: string): string?
local _, e = Pcf.decode(Bytes.reader(buffer.fromstring(s)))
return e and e.code
end
t:eq(code("\1fc"), Err.TRUNCATED, "short")
t:eq(code("xxxx\0\0\0\0"), Err.CORRUPT, "bad magic")
t:eq(code("\1fcp\0\0\0\0"), Err.CORRUPT, "no tables")
t:fuzz("decode", function(b: buffer)
return Pcf.decode(Bytes.reader(b))
end, samples, { mutations = 40 })
t:done()