ws-arch: wip font/ttf + font fixtures

This commit is contained in:
4DBug committed 2026-10-31 08:38:47 -05:00
1 parent 9fadc7155f
commit 20092f6324
3 files changed
+324

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
--!strict
-- font/ttf: TrueType and OpenType fonts (.ttf, .otf with CFF outlines) and collections (.ttc / .otc), read into the
-- standard Font (Types.Font) by codecs/TrueType: metrics, names, cmap, outlines (glyf or CFF), kerning ('kern' table,
-- GPOS pair kerning) and rasterize(glyph, px) through codecs/Raster. Ported from stb_truetype 1.26 (public domain /
-- MIT) via imgui-luau's imstb_truetype.lua; see codecs/TrueType. opts.index picks the font of a collection.
-- Not supported: hinting, variable fonts, bitmap / colour glyphs, GSUB, writing.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local TrueType = require("@formats/codecs/TrueType")
-- ------------------------------------------------------------------------------------------------ contract
local Ttf = {
name = "ttf",
category = "font",
extensions = { "ttf", "otf", "ttc", "otc" },
mime = { "font/ttf", "font/otf", "font/collection" },
deps = { "core/Err", "core/Bytes", "codecs/TrueType" },
caps = { decode = true, encode = false, outlines = { "truetype", "cff" }, collection = true, kerning = true },
}
export type DecodeOptions = {
index: number?, -- the font of a collection (default 0)
}
-- ------------------------------------------------------------------------------------------------ sniff
function Ttf.sniff(r: Bytes.Reader): number
local m = r:peek(4)
if m == "\0\1\0\0" or m == "OTTO" or m == "ttcf" or m == "true" then
return 0.9
end
return 0
end
-- ------------------------------------------------------------------------------------------------ decode
function Ttf.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local font, err = TrueType.font(r.buf, r.base, r.limit - r.base, opts)
if not font then
return nil, err
end
font.meta.source = { format = "ttf", outlines = font.meta.outlines, sfnt = font.meta.sfnt }
return font, nil
end
-- ------------------------------------------------------------------------------------------------ extras
-- count(r) -> the number of fonts in the file (1 unless a collection)
function Ttf.count(r: Bytes.Reader): number
return TrueType.count(r.buf, r.base)
end
return Ttf