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
This commit is contained in:
1 parent
d73b93c740
commit
2f0ff81c95
6 files changed
+83
-9
No files matched your search
@@ -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
|
||||
|
||||
Reference in new issue
Block a user