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

+1 -1
View File
@@ -501,7 +501,7 @@ local list: { Entry } = {
extensions = { "fnt" },
mime = { "application/x-bmfont" },
deps = { "core/Err", "core/Bytes", "Types" },
caps = { decode = true, encode = true, bitmap = true, variants = { "text", "xml", "binary" } },
caps = { decode = true, encode = true, bitmap = true, variants = { "text", "xml", "binary" }, extraFiles = true },
load = function()
return require("@formats/font/bmfont")
end,
+33 -2
View File
@@ -20,7 +20,7 @@ local Bmfont = {
extensions = { "fnt" },
mime = { "application/x-bmfont" },
deps = { "core/Err", "core/Bytes", "Types" },
caps = { decode = true, encode = true, bitmap = true, variants = { "text", "xml", "binary" } },
caps = { decode = true, encode = true, bitmap = true, variants = { "text", "xml", "binary" }, extraFiles = true },
}
export type DecodeOptions = {
@@ -31,6 +31,11 @@ export type EncodeOptions = {
variant: ("text" | "xml" | "binary")?, -- default: the variant it was read from, else "text"
px: number?, -- fonts without a BMFont description are baked first (see bake)
chars: (string | { number })?,
file: string?, -- baking: the page file name prefix (default: opts.name without its extension, else "font")
name: string?, -- the destination's file name (set by the api)
files: { [string]: any }?, -- the page images are written here, encoded by opts.encodeImage (the api passes
-- both): files[page file name] = PNG bytes, for the baked pages or a BMFont's loaded pages
encodeImage: ((img: any, format: string) -> (buffer?, any))?,
}
export type BakeOptions = {
px: number?, -- pixels per em (default 32)
@@ -237,16 +242,42 @@ function Bmfont.encode(font: any, opts: EncodeOptions?): (buffer?, Err.Err?)
return nil, Err.new(Err.ARG, "expected a Font")
end
local d = type(font.meta) == "table" and font.meta.bmfont or nil
local pages = font.pages
if d == nil then
local baked, err = Bmfont.bake(font, { px = opts and opts.px, chars = opts and opts.chars })
-- page names: opts.file, else the destination's base name (the api passes opts.name), else "font"
local file = opts and opts.file
if file == nil and opts and type(opts.name) == "string" then
file = string.match(opts.name, "([^/\\]-)%.[^./\\]*$") or string.match(opts.name, "([^/\\]+)$")
if file == "" then
file = nil
end
end
local baked, err = Bmfont.bake(font, { px = opts and opts.px, chars = opts and opts.chars, file = file })
if not baked then
return nil, err
end
d = baked.meta.bmfont
pages = baked.pages
end
if not validDesc(d) then
return nil, Err.new(Err.ARG, "bad BMFont description in font.meta.bmfont")
end
-- the page images into opts.files, under the names the descriptor gives them
local files = opts and opts.files
local encodeImage = opts and opts.encodeImage
if type(files) == "table" and type(encodeImage) == "function" and type(pages) == "table" then
for i, name in d.pages do
local img = pages[i]
if type(img) == "table" and type(name) == "string" and name ~= "" then
local ext = string.lower(string.match(name, "%.([%w]+)$") or "png")
local bytes, eerr = encodeImage(img, ext)
if not bytes then
return nil, Err.new(Err.ARG, "page " .. (i - 1) .. " (" .. name .. "): " .. tostring(eerr and eerr.message or eerr))
end
files[name] = bytes
end
end
end
local variant = (opts and opts.variant) or (font.meta and font.meta.variant) or "text"
if variant == "text" then
return buffer.fromstring(Parse.writeText(d)), nil
+10 -3
View File
@@ -557,8 +557,10 @@ function encodeTextures(scene: any): Err.Err?
return nil
end
-- save(value, dest?, opts?) -> buffer (no dest) | true (written to dest) | nil, Err
function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
-- save(value, dest?, opts?) -> buffer (no dest) | true (written to dest) | nil, Err. Formats that write extra files
-- (caps.extraFiles: bmfont pages) return them without a dest as a third value { [file name] = bytes } (or fill the
-- caller's opts.files); with a dest they are written next to it.
function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?, { [string]: any }?)
local o: Options = opts or {}
local entry, m, err, key = encoderFor(value, dest, o)
if not entry then
@@ -573,6 +575,8 @@ function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
if o.files == nil then
o.files = {}
end
elseif o.files == nil and entry.caps.extraFiles then
o.files = {} -- returned as the third value: F.save(ttf, nil, { format = "bmfont" }) -> fnt, nil, pages
end
if entry.category == "video" and type(value) == "table" and value.kind == "image" then
local v, verr = F.video.fromImage(value)
@@ -624,6 +628,9 @@ function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
end
return true
end
if type(o.files) == "table" and o.files ~= (opts and opts.files) and next(o.files) ~= nil then
return buf, nil, o.files
end
return buf
end
@@ -637,7 +644,7 @@ function F.saveString(value: any, opts: Options?): (string?, Err.Err?)
end
-- convert(src, format, opts?) -> buffer (or true with opts.out = a path) | nil, Err. opts go to both load and save.
function F.convert(src: any, format: string, opts: Options?): (any, Err.Err?)
function F.convert(src: any, format: string, opts: Options?): (any, Err.Err?, { [string]: any }?)
local o: Options = opts or {}
local value, err = F.load(src, o)
if value == nil then