font formats: comments are specs only
This commit is contained in:
1 parent
0452f26eef
commit
d91dc6c597
14 files changed
+64
-167
No files matched your search
@@ -1,11 +1,7 @@
|
||||
--!strict
|
||||
-- font/bmfont: AngelCode BMFont descriptors (.fnt) in all three encodings: text, XML and binary (version 3), read and
|
||||
-- written. Decodes to the standard Font (a bitmap font: glyphIndex / advance / kern work, outline is nil, rasterize
|
||||
-- crops the glyph from its page as alpha8). Pages are image files named by the descriptor (meta.pageFiles); they are
|
||||
-- loaded lazily through font:loadPages(loader) or the loader the api attaches (opts.loadPage(file) -> Image, or the
|
||||
-- files next to the .fnt on hosts with a file system). Bmfont.bake(font, opts) renders any outline Font (ttf / otf /
|
||||
-- woff) into a packed bitmap font with its pages. Written from the BMFont documentation.
|
||||
-- Not supported: packed multi-channel glyphs (chnl other than one channel or all: read as alpha).
|
||||
-- font/bmfont: AngelCode BMFont descriptors (.fnt: text, XML, binary version 3), read and written, from the BMFont
|
||||
-- documentation. Pages are image files (meta.pageFiles), loaded lazily; Bmfont.bake renders an outline Font to pages.
|
||||
-- Not supported: packed multi-channel glyphs (read as alpha).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -24,25 +20,24 @@ local Bmfont = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
pages: { Types.Image }?, -- the page images, if the caller has them already (page id 0 first)
|
||||
loadPage: ((file: string, page: number) -> any)?, -- loader for lazy pages: file -> Image (the api passes one)
|
||||
pages: { Types.Image }?, -- page id 0 first
|
||||
loadPage: ((file: string, page: number) -> any)?,
|
||||
}
|
||||
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)
|
||||
px: number?,
|
||||
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
|
||||
file: string?, -- default: opts.name without its extension, else "font"
|
||||
name: string?,
|
||||
files: { [string]: any }?,
|
||||
encodeImage: ((img: any, format: string) -> (buffer?, any))?,
|
||||
}
|
||||
export type BakeOptions = {
|
||||
px: number?, -- pixels per em (default 32)
|
||||
chars: (string | { number })?, -- code points to include (default printable ASCII 32..126)
|
||||
pageSize: number?, -- page width = height (default: the smallest power of two that fits, up to 4096)
|
||||
padding: number?, -- empty pixels around each glyph (default 1)
|
||||
file: string?, -- page file name prefix (default "font"): pages are <file>_<n>.png
|
||||
px: number?, -- default 32
|
||||
chars: (string | { number })?, -- default printable ASCII 32..126
|
||||
pageSize: number?, -- default: the smallest power of two that fits, up to 4096
|
||||
padding: number?, -- default 1
|
||||
file: string?, -- default "font"; pages are <file>_<n>.png
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -61,7 +56,6 @@ function Bmfont.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- the Font value
|
||||
local Face = {}
|
||||
Face.__index = Face
|
||||
|
||||
@@ -80,10 +74,9 @@ function Face.kern(self: any, a: number, b: number): number
|
||||
return self._kern[ca.id * 2097152 + cb.id] or 0
|
||||
end
|
||||
function Face.outline(_self: any, _g: number): { any }?
|
||||
return nil -- bitmap font
|
||||
return nil
|
||||
end
|
||||
|
||||
-- loadPages(loader?) -> true | nil, Err: loader(file, pageIndex) -> Image (rgba8) | nil
|
||||
function Face.loadPages(self: any, loader: any?): (boolean?, Err.Err?)
|
||||
local load = loader or self.meta.pageLoader
|
||||
if type(load) ~= "function" then
|
||||
@@ -103,8 +96,6 @@ end
|
||||
|
||||
local CHANNEL = { [1] = 2, [2] = 1, [4] = 0, [8] = 3 } -- chnl bit -> byte in rgba8
|
||||
|
||||
-- rasterize(g, px?) -> alpha8 Image cropped from the glyph's page (px is ignored: bitmap fonts have one size;
|
||||
-- meta.scale = px / size tells the caller how much to scale). nil when the pages are not loaded / available.
|
||||
function Face.rasterize(self: any, g: number, px: number?): Types.Image?
|
||||
local c = self._chars[g]
|
||||
if not c then
|
||||
@@ -141,7 +132,6 @@ function Face.rasterize(self: any, g: number, px: number?): Types.Image?
|
||||
return img
|
||||
end
|
||||
|
||||
-- the Font value of a description
|
||||
local function face(d: Parse.Desc, variant: string): any
|
||||
local f: any = setmetatable({
|
||||
kind = "font",
|
||||
@@ -244,7 +234,6 @@ function Bmfont.encode(font: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local d = type(font.meta) == "table" and font.meta.bmfont or nil
|
||||
local pages = font.pages
|
||||
if d == nil then
|
||||
-- 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, "([^/\\]+)$")
|
||||
@@ -262,7 +251,6 @@ function Bmfont.encode(font: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
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
|
||||
@@ -310,9 +298,6 @@ local function codepoints(chars: any): { number }
|
||||
return out
|
||||
end
|
||||
|
||||
-- bake(font, opts?) -> a BMFont Font (pages: rgba8 Images, white with the coverage as alpha) | nil, Err. Glyphs are
|
||||
-- rasterized with font:rasterize and packed in rows (tallest first); kerning pairs between the baked characters are
|
||||
-- kept (rounded to pixels).
|
||||
function Bmfont.bake(font: any, opts: BakeOptions?): (any, Err.Err?)
|
||||
if type(font) ~= "table" or type(font.rasterize) ~= "function" or type(font.glyphIndex) ~= "function" then
|
||||
return nil, Err.new(Err.ARG, "bake: expected an outline Font")
|
||||
|
||||
Reference in new issue
Block a user