ws-apifix: an outline font saved as BMFont writes its pages (opts.files; without a destination F.save returns them as a third value { [page file] = PNG bytes }; caps.extraFiles); pages named after the destination

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 14:03:45 -05:00
1 parent 4557a3c792
commit 2abada340b
6 files changed
+83 -9

No files matched your search

+26
View File
@@ -137,6 +137,32 @@ do
-- 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
-- 6. errors and validation