50 lines
1.9 KiB
Luau
50 lines
1.9 KiB
Luau
--!strict
|
|
-- font/ttf: TrueType / OpenType (glyf or CFF) fonts and collections via codecs/font/TrueType, ported from stb_truetype
|
|
-- 1.26 (public domain / MIT) via imgui-luau's imstb_truetype.lua. Not supported: hinting, variable fonts, bitmap /
|
|
-- colour glyphs, GSUB, writing.
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@rosetta/core/Err")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local TrueType = require("@rosetta/codecs/font/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/font/TrueType" },
|
|
caps = { decode = true, encode = false, outlines = { "truetype", "cff" }, collection = true, kerning = true },
|
|
}
|
|
|
|
export type DecodeOptions = {
|
|
index: number?, -- 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
|
|
function Ttf.count(r: Bytes.Reader): number
|
|
return TrueType.count(r.buf, r.base)
|
|
end
|
|
|
|
return Ttf
|