-- font/bdf tests: Spleen BDF fonts against FreeType's renders (tools/fixtures/bitmapfonts.py, tools/fixtures/fonts.py). local T = require("@tests/T") local F = require("@rosetta") local Bdf = require("@rosetta/font/bdf") local Ttf = require("@rosetta/font/ttf") 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 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") 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 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") 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()