codecs/font: comments to specs only

This commit is contained in:
4DBug committed 2026-11-01 01:57:58 -05:00
1 parent 878e1b3a44
commit f1ad32105b
7 files changed
+52 -155

No files matched your search

+10 -28
View File
@@ -1,17 +1,7 @@
--!strict
-- codecs/font/Cff: Compact Font Format (Adobe TN #5176) and Type 2 charstrings (TN #5177): the outlines of OpenType fonts
-- with a "CFF " table (.otf). Parses the header, the INDEXes, the Top / Private DICTs (name-keyed and CID-keyed
-- fonts with FDArray / FDSelect formats 0 and 3), local and global subroutines, and runs charstrings to path commands
-- (moveTo / lineTo / cubicTo / close, font units, y up). The charstring interpreter is ported from stb_truetype 1.26
-- (public domain / MIT; Dougall Johnson's Type 2 support) through imgui-luau's imstb_truetype.lua, bounds-checked,
-- with fixes: flex1's last delta, an operation budget against subroutine bombs, and the arithmetic / storage
-- operators (TN #5177 4.4: and or not abs add sub div neg eq drop put get ifelse random mul sqrt dup exch index roll).
-- Not supported: CFF2 (variable fonts), seac accented glyphs in endchar (deprecated in OpenType), hinting.
--
-- local cff, err = Cff.parse(buf, offset, length)
-- local cmds, err = Cff.outline(cff, glyphIndex)
--
-- Never throws: malformed data returns nil, message.
-- codecs/font/Cff: Compact Font Format (Adobe TN #5176) and Type 2 charstrings (TN #5177), the outlines of .otf fonts.
-- Charstrings ported from stb_truetype 1.26 (public domain / MIT) via imgui-luau's imstb_truetype.lua, plus
-- flex1's last delta, an operation budget, and the TN #5177 4.4 arithmetic / storage operators.
local Cff = {}
local readu8 = buffer.readu8
@@ -38,15 +28,14 @@ export type Font = {
limit: number,
charStrings: Index,
gsubrs: Index,
subrs: Index?, -- name-keyed fonts
fdSubrs: { Index? }?, -- CID-keyed fonts: per font dict
fdSelect: number?, -- absolute offset of FDSelect
subrs: Index?,
fdSubrs: { Index? }?,
fdSelect: number?,
numGlyphs: number,
cid: boolean,
fontMatrix: { number }?,
}
-- INDEX at absolute offset p (within [base, limit)) -> Index | nil
local function index(b: buffer, p: number, limit: number): Index?
if p + 2 > limit then
return nil
@@ -71,7 +60,6 @@ local function index(b: buffer, p: number, limit: number): Index?
return { count = count, offSize = offSize, offsets = offsets, data = data, next = data + last }
end
-- item(idx, i) -> absolute start, length (nil when out of range / corrupt)
local function item(b: buffer, idx: Index, i: number): (number?, number)
if i < 0 or i >= idx.count then
return nil, 0
@@ -85,7 +73,7 @@ local function item(b: buffer, idx: Index, i: number): (number?, number)
end
Cff.item = item
-- DICT at [p, p + n) -> { [op] = { operands } } (two-byte ops are 1200 + second byte)
-- DICT operators: two-byte ops (12 x) are keyed 1200 + x
local function dict(b: buffer, p: number, n: number): { [number]: { number } }
local out: { [number]: { number } } = {}
local ops: { number } = {}
@@ -171,7 +159,6 @@ local function privateSubrs(b: buffer, base: number, limit: number, d: { [number
return index(b, ps + s[1], limit)
end
-- parse(buf, offset, length) -> Font | nil, message
function Cff.parse(b: buffer, offset: number, length: number): (Font?, string?)
local base, limit = offset, math.min(offset + length, blen(b))
if limit - base < 4 then
@@ -256,7 +243,6 @@ local function bias(count: number): number
return if count < 1240 then 107 elseif count < 33900 then 1131 else 32768
end
-- the local subrs of glyph g (CID fonts: through FDSelect)
local function localSubrs(f: Font, g: number): Index?
if not f.cid then
return f.subrs
@@ -286,7 +272,6 @@ end
local BUDGET = 200000 -- operators per glyph (subroutine bombs)
-- outline(f, g) -> path commands | nil, message
function Cff.outline(f: Font, g: number): ({ any }?, string?)
local b = f.buf
local p, len = item(b, f.charStrings, g)
@@ -295,14 +280,14 @@ function Cff.outline(f: Font, g: number): ({ any }?, string?)
end
local e = p + len
local cmds: { any } = {}
local s: { number } = table.create(48, 0) -- operand stack, 1-based; sp = count
local s: { number } = table.create(48, 0)
local sp = 0
local trans = table.create(32, 0) :: { number }
local x, y = 0, 0
local open = false
local maskbits = 0
local inHeader = true
local stack: { { number } } = {} -- return addresses { p, e }
local stack: { { number } } = {}
local subrs: Index? = nil
local haveSubrs = false
local ops = 0
@@ -334,13 +319,12 @@ function Cff.outline(f: Font, g: number): ({ any }?, string?)
x, y = x2 + dx3, y2 + dy3
table.insert(cmds, { op = "cubicTo", x1, y1, x2, y2, x, y })
end
local function S(i: number): number -- operand i (0-based), 0 past the stack
local function S(i: number): number
return s[i + 1] or 0
end
while true do
if p >= e then
-- the end of a subroutine without return, or of the charstring without endchar
if #stack > 0 then
local r = table.remove(stack) :: { number }
p, e = r[1], r[2]
@@ -535,12 +519,10 @@ function Cff.outline(f: Font, g: number): ({ any }?, string?)
curveTo(S(0), S(1), S(2), S(3), S(4), S(5))
curveTo(S(6), S(7), S(8), S(9), dx6, dy6)
else
-- arithmetic and storage operators: they leave results on the stack
clear = false
local top = sp
local a, c = S(top - 2), S(top - 1)
local function set(n: number, v: number)
-- pop n operands, push v
sp = math.max(0, sp - n)
sp += 1
s[sp] = v