ws-arch: font/bmfont (text / XML / binary read + write, lazy pages, bake from outline fonts); api: lazy page loader for bitmap fonts (opts.loadPage or sibling files)

This commit is contained in:
4DBug committed 2026-10-31 08:47:27 -05:00
1 parent ef6c658d78
commit 945abe6d94
7 files changed
+1181

No files matched your search

+32
View File
@@ -166,6 +166,37 @@ export type Options = { [string]: any } -- format (name or extension), category,
local load: (src: any, opts: Options?) -> (any, Err.Err?)
-- bitmap fonts (font/bmfont) name their page images: attach a lazy loader, opts.loadPage(file, page) -> Image | buffer
-- | string, else the files next to the descriptor on hosts with a file system
local function pageLoader(value: any, name: string?, o: Options)
if type(value) ~= "table" or value.kind ~= "font" or type(value.meta) ~= "table" or type(value.meta.pageFiles) ~= "table" then
return
end
local user = o.loadPage
if value.pages or (value.meta.pageLoader and type(user) ~= "function") then
return
end
local dir = if name then string.match(name, "^(.*[/\\])") or "" else nil
if type(user) ~= "function" and not (dir and host.hasFiles) then
return
end
value.meta.pageLoader = function(file: string, page: number): (any, Err.Err?)
local src: any
if type(user) == "function" then
src = user(file, page)
else
src = (dir :: string) .. file
end
if type(src) == "table" then
return src -- already an Image
end
if src == nil then
return nil, Err.new(Err.IO, "no data for page " .. file)
end
return load(src, { category = "image", name = file, path = type(user) ~= "function" })
end
end
-- single-stream archives (gzip / zst / xz / bz2 / br: meta.stream) whose one entry sniffs as another archive (a
-- .tar.gz) load as that inner archive, with meta.compression = the outer format. opts.unwrap = false keeps the outer
-- one; an explicit opts.format also does.
@@ -242,6 +273,7 @@ function F.load(src: any, opts: Options?): (any, Err.Err?)
return nil, Err.wrap(err or Err.new(Err.CORRUPT, "decode failed"), entry.name)
end
tag(value, entry.name)
pageLoader(value, name, o)
return unwrap(value, entry.name, o)
end
load = F.load