init.luau unwrap: a single-stream archive whose entry is a .pcf / .psf / .bdf loads as that font (category font).
62 lines
2.7 KiB
Luau
62 lines
2.7 KiB
Luau
-- 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()
|