ws-arch2: font/bdf (read / write; FreeType reads our output identically) and font/pcf (every pad / unit / order, .pcf.gz via the api), shared bitmap-font fixtures vs FreeType

init.luau unwrap: a single-stream archive whose entry is a .pcf / .psf / .bdf loads as that font (category font).
This commit is contained in:
4DBug committed 2026-10-31 14:56:44 -05:00
1 parent f1c2c6ed06
commit 87cbc62fb6
11 files changed
+913 -2

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
-- shared by the bitmap font tests: compare(t, font, ref, label) checks every glyph FreeType rendered (code point,
-- bitmap size, left / top offsets, advance, pixels) against font:rasterize(font:glyphIndex(cp)).
local B = {}
function B.compare(t: any, font: any, ref: any, label: string)
for _, g in ref.glyphs do
local gi = font:glyphIndex(g.cp)
local img = font:rasterize(gi, 16)
local tag = string.format("%s U+%04X", label, g.cp)
t:check(gi > 0 or font.bitmap.default == 0, tag .. ": mapped")
t:check(img, tag .. ": rasterize")
if img then
t:eq(img.width, g.width, tag .. ": width")
t:eq(img.height, g.height, tag .. ": height")
t:eq(img.meta.left, g.left, tag .. ": left")
t:eq(img.meta.top, g.top, tag .. ": top")
t:eq(img.meta.advance, g.advance, tag .. ": advance")
t:check(buffer.tostring(img.pixels) == g.pixels, tag .. ": pixels")
end
end
end
return B
+61
View File
@@ -0,0 +1,61 @@
-- font/bdf tests: Spleen BDF fonts against FreeType's renders (bitmaps, offsets, advances), properties and metrics; our
-- BDF output (of BDF, PCF and outline fonts) read back by FreeType identically (fixture step) and round-tripping; an
-- outline font written as BDF; errors; the fuzz pass. Fixtures: tools/fixtures/bitmapfonts.py, tools/fixtures/fonts.py.
local T = require("@tests/T")
local F = require("@formats")
local Bdf = require("@formats/font/bdf")
local Ttf = require("@formats/font/ttf")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local B = require("@tests/font/_bitmap")
local Fx = require("@tests/gen/bitmapfonts")
local Fonts = require("@tests/gen/fonts")
local t = T.suite("bdf")
t:eq(Bdf.name, "bdf", "name")
local samples = {}
for _, c in Fx.fonts do
t:check(c.ours.ok, c.name .. ": FreeType reads our BDF of it the same: " .. c.ours.detail)
if c.format == "bdf" then
local font, err = Bdf.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")
t:eq(-font.descent, c.ref.descent, c.name .. ": descent")
t:eq(font.info.family, "Spleen", c.name .. ": family")
-- round trip through our writer
local out = Bdf.encode(font) :: buffer
local back = Bdf.decode(Bytes.reader(out)) :: any
t:check(back and #back.bitmap.glyphs == #font.bitmap.glyphs, c.name .. ": round trip glyph count")
if back then
B.compare(t, back, c.ref, c.name .. " (round trip)")
end
end
t:eq(F.detect(c.data), "bdf", c.name .. ": detect")
table.insert(samples, string.sub(c.data, 1, 6000) .. "\nENDFONT\n")
end
end
-- an outline font as BDF
local ttf = Ttf.decode(Bytes.reader(buffer.fromstring(Fonts.fonts[1].data)))
local out, err = Bdf.encode(ttf, { px = 14, chars = "Hello" })
t:check(out, "ttf -> bdf: " .. tostring(err))
local back = Bdf.decode(Bytes.reader(out :: buffer)) :: any
t:check(back and back:glyphIndex(72) > 0 and back.unitsPerEm == 14 and back:rasterize(back:glyphIndex(72), 14).width > 3, "ttf -> bdf glyphs")
-- errors
local function code(s: string): string?
local _, e = Bdf.decode(Bytes.reader(buffer.fromstring(s)))
return e and e.code
end
t:eq(code("hello"), Err.CORRUPT, "not BDF")
t:eq(code("STARTFONT 2.1\nFONT x\n"), Err.TRUNCATED, "no ENDFONT")
t:eq(code("STARTFONT 2.1\nSTARTCHAR a\nBBX 99999 2 0 0\nBITMAP\nENDCHAR\nENDFONT\n"), Err.CORRUPT, "bad BBX")
t:eq((select(2, Bdf.encode({}))) and (select(2, Bdf.encode({})) :: any).code, Err.ARG, "encode a non-font")
t:fuzz("decode", function(b: buffer)
return Bdf.decode(Bytes.reader(b))
end, samples)
t:done()
+62
View File
@@ -0,0 +1,62 @@
-- 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("@formats")
local Pcf = require("@formats/font/pcf")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Deflate = require("@formats/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()