From 2f0ff81c9542cc6fc813f0dac8218e15a0b592f4 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 14:03:45 -0500 Subject: [PATCH] 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 --- docs/FORMATS.md | 2 +- docs/guide/font.md | 14 +++++++++++-- src/Formats/Registry.luau | 2 +- src/Formats/font/bmfont/init.luau | 35 +++++++++++++++++++++++++++++-- src/Formats/init.luau | 13 +++++++++--- tests/font/bmfont.luau | 26 +++++++++++++++++++++++ 6 files changed, 83 insertions(+), 9 deletions(-) diff --git a/docs/FORMATS.md b/docs/FORMATS.md index 0a41a66..41489e3 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -45,7 +45,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build | model | usda | .usda .usd .usdz | yes | yes | materials: true, animation: true, cameras: true, lights: true, polygons: true, vertexColors: true, usdz: "read", usdc: false | `tests/model/usda.luau` | | model | vox | .vox | yes | yes | voxels: true, sceneGraph: true, materials: true | `tests/model/vox.luau` | | cad | step | .step .stp .p21 | yes | yes | complexEntities: true, sections: true, brep: "plugin", tessellate: "plugin" | `tests/cad/step.luau` | -| font | bmfont | .fnt | yes | yes | bitmap: true, variants: "text", "xml", "binary" | `tests/font/bmfont.luau` | +| font | bmfont | .fnt | yes | yes | bitmap: true, variants: "text", "xml", "binary", extraFiles: true | `tests/font/bmfont.luau` | | font | ttf | .ttf .otf .ttc .otc | yes | - | outlines: "truetype", "cff", collection: true, kerning: true | `tests/font/ttf.luau` | | font | woff | .woff | yes | - | outlines: "truetype", "cff" | `tests/font/woff.luau` | | font | woff2 | .woff2 | yes | - | outlines: "truetype", "cff", transforms: "glyf", "loca", "hmtx" | `tests/font/woff2.luau` | diff --git a/docs/guide/font.md b/docs/guide/font.md index f6590d3..d784a96 100644 --- a/docs/guide/font.md +++ b/docs/guide/font.md @@ -219,8 +219,18 @@ local again = assert(F.font.load(bin, { pages = baked.pages })) assert(again:advance(again:glyphIndex(string.byte("H"))) == baked:advance(baked:glyphIndex(string.byte("H")))) ``` -`F.save(ttf, nil, { format = "bmfont", px = 16 })` bakes too, but returns only the descriptor: the page images are -not written anywhere. Use `bake` when you need the pages. +`F.save(ttf, dest, { format = "bmfont", px = 16, chars = ... })` bakes too, and writes the page PNGs next to the +`.fnt`, named after it (`ui.fnt` gets `ui_0.png`, ...; `file` sets another prefix). Without a destination it +returns the pages as a third value, `{ [page file name] = PNG buffer }` (the names the descriptor uses), or puts +them into your `opts.files` table when you pass one. A BMFont whose pages are loaded saves them the same way. + +```luau +local ttf = assert(F.font.load(ttfBytes)) +local fnt, _, pages = F.save(ttf, nil, { format = "bmfont", px = 16, chars = "Hi", file = "hud" }) +assert(fnt and pages and F.detect(pages["hud_0.png"]) == "png") +local bm = assert(F.font.load(fnt, { loadPage = function(file) return pages[file] end })) +assert(bm:rasterize(bm:glyphIndex(string.byte("H")))) +``` ## Formats diff --git a/src/Formats/Registry.luau b/src/Formats/Registry.luau index 6c0344c..429bc2a 100644 --- a/src/Formats/Registry.luau +++ b/src/Formats/Registry.luau @@ -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, diff --git a/src/Formats/font/bmfont/init.luau b/src/Formats/font/bmfont/init.luau index 617033a..e7d4294 100644 --- a/src/Formats/font/bmfont/init.luau +++ b/src/Formats/font/bmfont/init.luau @@ -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 diff --git a/src/Formats/init.luau b/src/Formats/init.luau index f3eed45..7d65e41 100644 --- a/src/Formats/init.luau +++ b/src/Formats/init.luau @@ -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 diff --git a/tests/font/bmfont.luau b/tests/font/bmfont.luau index 46ae665..5f4bb60 100644 --- a/tests/font/bmfont.luau +++ b/tests/font/bmfont.luau @@ -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