63 lines
2.2 KiB
Luau
63 lines
2.2 KiB
Luau
-- font/hershey tests: JHF text with Roman-style glyphs and random stroke glyphs (records wrapped over 72-column lines)
|
|
-- against an independent parser (ids, advances, every moveTo / lineTo), ASCII and id mappings, stroked rasterization;
|
|
-- errors; the fuzz pass. Fixtures: tools/fixtures/hershey.py.
|
|
local T = require("@tests/T")
|
|
local F = require("@formats")
|
|
local Hershey = require("@formats/font/hershey")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Err = require("@formats/core/Err")
|
|
local Fx = require("@tests/gen/hershey")
|
|
|
|
local t = T.suite("hershey")
|
|
t:eq(Hershey.name, "hershey", "name")
|
|
|
|
local font, err = Hershey.decode(Bytes.reader(buffer.fromstring(Fx.text)))
|
|
t:check(font, "decode: " .. tostring(err))
|
|
if font then
|
|
t:eq(font.numGlyphs, #Fx.glyphs, "glyph count")
|
|
for k, g in Fx.glyphs do
|
|
local gi = k - 1
|
|
t:eq(font.meta.ids[k], g.id, "id " .. k)
|
|
t:eq(font:advance(gi), g.advance, "advance " .. g.id)
|
|
local cmds = font:outline(gi) :: any
|
|
t:eq(#cmds, #g.cmds, "commands " .. g.id)
|
|
local same = true
|
|
for i, c in g.cmds do
|
|
local m = cmds[i]
|
|
if not m or m.op ~= c.op or m[1] ~= c.x or m[2] ~= c.y then
|
|
same = false
|
|
end
|
|
end
|
|
t:check(same, "outline " .. g.id)
|
|
end
|
|
t:eq(font:glyphIndex(33), 1, "ascii mapping: the second glyph is \"!\"")
|
|
local img = font:rasterize(1, 64) :: any
|
|
t:check(img and img.format == "alpha8" and img.width > 20 and img.height > 30 and img.meta.top > 30, "rasterize A")
|
|
local ink = 0
|
|
for i = 0, buffer.len(img.pixels) - 1 do
|
|
ink += buffer.readu8(img.pixels, i)
|
|
end
|
|
t:check(ink > 255 * 50, "A has ink")
|
|
t:eq(font:rasterize(0, 32).width, 0, "space is empty")
|
|
end
|
|
local byId = Hershey.decode(Bytes.reader(buffer.fromstring(Fx.text)), { map = "id" }) :: any
|
|
t:eq(byId:glyphIndex(508), 2, "id mapping")
|
|
t:eq(F.detect(Fx.text), "hershey", "detect")
|
|
|
|
local function code(s: string): string?
|
|
local _, e = Hershey.decode(Bytes.reader(buffer.fromstring(s)))
|
|
return e and e.code
|
|
end
|
|
t:eq(code(" 501 9MWRFJ[ RRFZ["), Err.TRUNCATED, "short record")
|
|
t:eq(code("hello world\n"), Err.CORRUPT, "not jhf")
|
|
t:eq(code("\n\n"), Err.CORRUPT, "empty")
|
|
|
|
t:fuzz("decode", function(b: buffer)
|
|
local f = Hershey.decode(Bytes.reader(b))
|
|
if f then
|
|
f:rasterize(1, 24)
|
|
end
|
|
return f
|
|
end, { string.sub(Fx.text, 1, 1500) })
|
|
t:done()
|