184 lines
8.2 KiB
Luau
184 lines
8.2 KiB
Luau
-- font/bmfont tests against an independent Python writer and FreeType's bitmaps (tools/fixtures/bmfont.py).
|
|
local T = require("@tests/T")
|
|
local F = require("@rosetta")
|
|
local Bmfont = require("@rosetta/font/bmfont")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Fx = require("@tests/gen/bmfont")
|
|
|
|
local t = T.suite("bmfont")
|
|
t:eq(Bmfont.name, "bmfont", "name")
|
|
|
|
local CHAR_KEYS = { "id", "x", "y", "width", "height", "xoffset", "yoffset", "xadvance", "page", "chnl" }
|
|
|
|
local function sameDesc(got: any, want: any, label: string, faceQuotes: boolean)
|
|
local face = want.info.face
|
|
if not faceQuotes then
|
|
face = string.gsub(face, '"', "")
|
|
end
|
|
t:eq(got.info.face, face, label .. ": face")
|
|
for _, k in { "size", "bold", "italic", "unicode", "stretchH", "smooth", "aa", "outline" } do
|
|
t:eq(got.info[k], want.info[k], label .. ": info." .. k)
|
|
end
|
|
for i = 1, 4 do
|
|
t:eq(got.info.padding[i], want.info.padding[i], label .. ": padding")
|
|
end
|
|
t:eq(got.info.spacing[1] .. "," .. got.info.spacing[2], want.info.spacing[1] .. "," .. want.info.spacing[2], label .. ": spacing")
|
|
for k, v in want.common do
|
|
t:eq(got.common[k], v, label .. ": common." .. k)
|
|
end
|
|
t:eq(#got.pages, #want.pages, label .. ": pages")
|
|
t:eq(got.pages[1], want.pages[1], label .. ": page file")
|
|
t:eq(#got.chars, #want.chars, label .. ": char count")
|
|
for i, c in want.chars do
|
|
for _, k in CHAR_KEYS do
|
|
t:eq(got.chars[i][k], c[k], string.format("%s: char %d %s", label, c.id, k))
|
|
end
|
|
end
|
|
t:eq(#got.kernings, #want.kernings, label .. ": kerning count")
|
|
for i, k in want.kernings do
|
|
local g = got.kernings[i]
|
|
t:check(g[1] == k[1] and g[2] == k[2] and g[3] == k[3], label .. ": kerning " .. i)
|
|
end
|
|
end
|
|
|
|
local fonts = {}
|
|
for _, v in { "text", "xml", "binary" } do
|
|
local f, err = Bmfont.decode(Bytes.reader(buffer.fromstring(Fx[v])))
|
|
t:check(f, v .. ": " .. tostring(err))
|
|
t:eq(f.meta.variant, v, v .. ": variant")
|
|
sameDesc(f.meta.bmfont, Fx.desc, v, v ~= "text")
|
|
fonts[v] = f
|
|
t:eq(F.detect(Fx[v]), "bmfont", v .. ": F.detect")
|
|
end
|
|
|
|
for from, f in fonts do
|
|
for _, to in { "text", "xml", "binary" } do
|
|
local b, err = Bmfont.encode(f, { variant = to :: any })
|
|
t:check(b, from .. " -> " .. to .. ": " .. tostring(err))
|
|
local back = Bmfont.decode(Bytes.reader(b :: buffer))
|
|
t:check(back and back.meta.variant == to, from .. " -> " .. to .. ": decodes")
|
|
sameDesc(back.meta.bmfont, Fx.desc, from .. " -> " .. to, from ~= "text" and to ~= "text")
|
|
if from == "binary" and to == "binary" then
|
|
t:eq(buffer.tostring(b :: buffer), Fx.binary, "binary re-encode is byte-identical to the reference writer")
|
|
end
|
|
end
|
|
end
|
|
t:eq(buffer.tostring(F.save(fonts.xml, nil, { format = "fnt" })):sub(1, 5), "<?xml", "F.save keeps the variant")
|
|
|
|
do
|
|
local f = fonts.text
|
|
local gA, gV = f:glyphIndex(65), f:glyphIndex(86)
|
|
t:check(gA > 0 and gV > 0, "glyphIndex")
|
|
t:eq(f:glyphIndex(0x1F600), 0, "missing glyph")
|
|
t:eq(f:advance(gA), Fx.desc.chars[gA].xadvance, "advance")
|
|
t:eq(f:kern(gA, gV), Fx.desc.kernings[2][3], "kern A V")
|
|
t:eq(f:kern(gV, gA), Fx.desc.kernings[9][3], "kern V A")
|
|
t:eq(f.unitsPerEm, 24, "size")
|
|
t:check(f:outline(gA) == nil, "bitmap fonts have no outline")
|
|
t:check(f:rasterize(gA, 24) == nil, "no pages loaded yet")
|
|
local ok, e = f:loadPages()
|
|
t:check(not ok and e and e.code == Err.ARG, "loadPages without a loader")
|
|
end
|
|
|
|
-- lazy pages: the api attaches opts.loadPage; the PNG goes through F.load
|
|
do
|
|
local asked = {}
|
|
local f = F.load(Fx.binary, {
|
|
loadPage = function(file: string)
|
|
table.insert(asked, file)
|
|
return Fx.page
|
|
end,
|
|
})
|
|
t:check(f and f.pages == nil and #asked == 0, "pages are lazy")
|
|
local crcs = {}
|
|
for _, c in Fx.crops do
|
|
crcs[c.id] = c.crc
|
|
end
|
|
local n = 0
|
|
for _, c in Fx.desc.chars do
|
|
local img = f:rasterize(f:glyphIndex(c.id), 24)
|
|
t:check(img and img.format == "alpha8" and img.width == c.width, "rasterize " .. c.id)
|
|
t:eq(Crc32.of(img.pixels), crcs[c.id], "glyph crop " .. c.id .. " = FreeType's bitmap")
|
|
t:eq(img.meta.left, c.xoffset, "left")
|
|
n += 1
|
|
end
|
|
t:eq(#asked, 1, "page loaded once")
|
|
t:eq(asked[1], "karla_0.png", "page file name")
|
|
t:note("%d glyph crops from the lazily loaded page match FreeType", n)
|
|
end
|
|
|
|
do
|
|
local ttf = F.font.load(Fx.ttf)
|
|
t:check(ttf, "ttf loads")
|
|
local baked, err = Bmfont.bake(ttf, { px = 20, chars = "Hello, wörld! AV" })
|
|
t:check(baked, "bake: " .. tostring(err))
|
|
t:eq(#baked.meta.bmfont.chars, 13, "unique characters baked")
|
|
t:check(baked.pages and #baked.pages == 1, "one page")
|
|
local g = baked:glyphIndex(string.byte("H"))
|
|
local a = baked:rasterize(g, 20)
|
|
local b = ttf:rasterize(ttf:glyphIndex(string.byte("H")), 20)
|
|
t:check(a and b and a.width == b.width and a.height == b.height, "baked glyph size")
|
|
t:eq(Crc32.of(a.pixels), Crc32.of(b.pixels), "baked glyph pixels = rasterize")
|
|
t:eq(a.meta.left, b.meta.left, "baked left")
|
|
t:eq(a.meta.top, b.meta.top, "baked top")
|
|
local enc = F.save(baked, nil, { format = "bmfont", variant = "binary" })
|
|
local back = F.load(enc)
|
|
t:check(back and #back.meta.bmfont.chars == 13, "baked font round trip")
|
|
-- an outline font saved as fnt is baked on the way
|
|
local fnt = F.convert(Fx.ttf, "fnt", { px = 16 })
|
|
t:check(fnt and string.sub(buffer.tostring(fnt), 1, 9) == "info face", "F.convert ttf -> fnt (text)")
|
|
-- the baked pages: returned as the third value without a destination ({ [page file] = PNG bytes }) ...
|
|
local desc, derr, pages = F.save(ttf, nil, { format = "bmfont", px = 16, chars = "Hi" })
|
|
t:check(desc and derr == nil and type(pages) == "table", "F.save(ttf, bmfont) returns the pages")
|
|
local png = pages and pages["font_0.png"]
|
|
t:check(png and F.detect(png) == "png", "page 0 as PNG under the descriptor's name")
|
|
local again = F.font.load(desc, { loadPage = function(file: string): any
|
|
return pages and pages[file]
|
|
end })
|
|
local hg = again and again:rasterize(again:glyphIndex(string.byte("H")))
|
|
local ref = Bmfont.bake(ttf, { px = 16, chars = "Hi" })
|
|
local rg = ref:rasterize(ref:glyphIndex(string.byte("H")))
|
|
t:check(hg and rg and Crc32.of(hg.pixels) == Crc32.of(rg.pixels), "the saved page gives the baked glyph back")
|
|
-- ... or into the caller's opts.files (then not returned), named after opts.name / opts.file
|
|
local mine = {}
|
|
local d2, _, p2 = F.save(ttf, nil, { format = "bmfont", px = 16, chars = "Hi", files = mine, name = "ui.fnt" })
|
|
t:check(d2 and p2 == nil and mine["ui_0.png"] ~= nil, "opts.files filled, pages named after opts.name")
|
|
t:check(string.find(buffer.tostring(d2), 'file="ui_0.png"', 1, true) ~= nil, "descriptor names the page")
|
|
local _, _, p3 = F.save(ttf, nil, { format = "bmfont", px = 16, chars = "Hi", file = "x" })
|
|
t:check(p3 and p3["x_0.png"] ~= nil, "opts.file sets the prefix")
|
|
-- a loaded BMFont with its pages: re-saved with them; without pages: the descriptor alone
|
|
local _, _, p4 = F.save(again, nil, { format = "bmfont" })
|
|
t:check(p4 == nil or p4["font_0.png"] ~= nil, "lazy pages not loaded: descriptor only")
|
|
local _, _, p5 = F.save(ref, nil, { format = "bmfont" })
|
|
t:check(p5 and p5["font_0.png"] ~= nil, "a baked font re-saved with its pages")
|
|
-- direct calls without the hooks: no pages, no error
|
|
t:check(Bmfont.encode(ttf, { px = 16, chars = "Hi", files = {} }) ~= nil, "encode without encodeImage")
|
|
end
|
|
|
|
do
|
|
local _, e1 = Bmfont.decode(Bytes.reader(buffer.fromstring("BMF\2")))
|
|
t:check(e1 and e1.code == Err.UNSUPPORTED, "binary version 2: " .. tostring(e1))
|
|
local _, e2 = Bmfont.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.binary, 1, 40))))
|
|
t:check(e2 and e2.code == Err.TRUNCATED, "truncated binary: " .. tostring(e2))
|
|
local _, e3 = Bmfont.decode(Bytes.reader(buffer.fromstring("hello")))
|
|
t:check(e3 and e3.code == Err.CORRUPT, "not a descriptor")
|
|
local f = Bmfont.decode(Bytes.reader(buffer.fromstring(Fx.text)))
|
|
f.meta.bmfont.chars[3].x = 1.5
|
|
local _, e4 = Bmfont.encode(f)
|
|
t:check(e4 and e4.code == Err.ARG, "fractional field: " .. tostring(e4))
|
|
local _, e5 = Bmfont.encode({ kind = "image" })
|
|
t:check(e5 and e5.code == Err.ARG, "not a font")
|
|
end
|
|
|
|
t:fuzz("decode", function(b: buffer)
|
|
local f = Bmfont.decode(Bytes.reader(b))
|
|
if f then
|
|
Bmfont.encode(f, { variant = "binary" })
|
|
end
|
|
return f
|
|
end, { Fx.binary, string.sub(Fx.text, 1, 3000), string.sub(Fx.xml, 1, 3000) }, { mutations = 40 })
|
|
|
|
t:done()
|