ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
C2, the hierarchy (tools/move.py, every reference rewritten mechanically): - codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder; codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url; codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err Crc32 Adler32 Base64 Md5 Input Sniff. - LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md). - Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes. C3, generic code out of format folders: - containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first, trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64 through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it. - codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position, priming and end trimming) that aac, caf and m4a each carried (~70 lines each). - codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of its own (the AND mask / alpha rule stay ICO's). - codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model), Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the fixtures' bind shapes). - README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes: FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
9c553b9d1a
commit
434a304315
283 files changed
+1877
-1660
No files matched your search
@@ -0,0 +1,657 @@
|
||||
--!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.
|
||||
local Cff = {}
|
||||
|
||||
local readu8 = buffer.readu8
|
||||
local blen = buffer.len
|
||||
|
||||
local function u8(b: buffer, o: number): number
|
||||
if o < 0 or o + 1 > blen(b) then
|
||||
return 0
|
||||
end
|
||||
return readu8(b, o)
|
||||
end
|
||||
local function uN(b: buffer, o: number, n: number): number
|
||||
local v = 0
|
||||
for i = 0, n - 1 do
|
||||
v = v * 256 + u8(b, o + i)
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
export type Index = { count: number, offSize: number, offsets: number, data: number, next: number }
|
||||
export type Font = {
|
||||
buf: buffer,
|
||||
base: number,
|
||||
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
|
||||
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
|
||||
end
|
||||
local count = uN(b, p, 2)
|
||||
if count == 0 then
|
||||
return { count = 0, offSize = 1, offsets = p + 2, data = p + 2, next = p + 2 }
|
||||
end
|
||||
local offSize = u8(b, p + 2)
|
||||
if offSize < 1 or offSize > 4 then
|
||||
return nil
|
||||
end
|
||||
local offsets = p + 3
|
||||
local data = offsets + (count + 1) * offSize - 1 -- offsets are 1-based
|
||||
if data + 1 > limit then
|
||||
return nil
|
||||
end
|
||||
local last = uN(b, offsets + count * offSize, offSize)
|
||||
if data + last > limit then
|
||||
return nil
|
||||
end
|
||||
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
|
||||
end
|
||||
local s = uN(b, idx.offsets + i * idx.offSize, idx.offSize)
|
||||
local e = uN(b, idx.offsets + (i + 1) * idx.offSize, idx.offSize)
|
||||
if s < 1 or e < s or idx.data + e > idx.next then
|
||||
return nil, 0
|
||||
end
|
||||
return idx.data + s, e - s
|
||||
end
|
||||
Cff.item = item
|
||||
|
||||
-- DICT at [p, p + n) -> { [op] = { operands } } (two-byte ops are 1200 + second byte)
|
||||
local function dict(b: buffer, p: number, n: number): { [number]: { number } }
|
||||
local out: { [number]: { number } } = {}
|
||||
local ops: { number } = {}
|
||||
local e = p + n
|
||||
while p < e do
|
||||
local b0 = u8(b, p)
|
||||
if b0 <= 21 then
|
||||
local op = b0
|
||||
p += 1
|
||||
if b0 == 12 then
|
||||
op = 1200 + u8(b, p)
|
||||
p += 1
|
||||
end
|
||||
out[op] = ops
|
||||
ops = {}
|
||||
elseif b0 == 28 then
|
||||
local v = uN(b, p + 1, 2)
|
||||
table.insert(ops, if v >= 32768 then v - 65536 else v)
|
||||
p += 3
|
||||
elseif b0 == 29 then
|
||||
local v = uN(b, p + 1, 4)
|
||||
table.insert(ops, if v >= 2147483648 then v - 4294967296 else v)
|
||||
p += 5
|
||||
elseif b0 == 30 then -- real number: BCD nibbles
|
||||
local s = {}
|
||||
p += 1
|
||||
local done = false
|
||||
while p < e and not done do
|
||||
local byte = u8(b, p)
|
||||
p += 1
|
||||
for _, nib in { byte // 16, byte % 16 } do
|
||||
if nib <= 9 then
|
||||
table.insert(s, tostring(nib))
|
||||
elseif nib == 10 then
|
||||
table.insert(s, ".")
|
||||
elseif nib == 11 then
|
||||
table.insert(s, "E")
|
||||
elseif nib == 12 then
|
||||
table.insert(s, "E-")
|
||||
elseif nib == 14 then
|
||||
table.insert(s, "-")
|
||||
elseif nib == 15 then
|
||||
done = true
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
table.insert(ops, tonumber(table.concat(s)) or 0)
|
||||
elseif b0 >= 32 and b0 <= 246 then
|
||||
table.insert(ops, b0 - 139)
|
||||
p += 1
|
||||
elseif b0 >= 247 and b0 <= 250 then
|
||||
table.insert(ops, (b0 - 247) * 256 + u8(b, p + 1) + 108)
|
||||
p += 2
|
||||
elseif b0 >= 251 and b0 <= 254 then
|
||||
table.insert(ops, -(b0 - 251) * 256 - u8(b, p + 1) - 108)
|
||||
p += 2
|
||||
else
|
||||
p += 1 -- reserved
|
||||
end
|
||||
if #ops > 48 then
|
||||
return out
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function privateSubrs(b: buffer, base: number, limit: number, d: { [number]: { number } }): Index?
|
||||
local pr = d[18]
|
||||
if not pr or #pr < 2 then
|
||||
return nil
|
||||
end
|
||||
local size, off = pr[1], pr[2]
|
||||
local ps = base + off
|
||||
if size < 0 or off < 0 or ps + size > limit then
|
||||
return nil
|
||||
end
|
||||
local pd = dict(b, ps, size)
|
||||
local s = pd[19]
|
||||
if not s or #s < 1 then
|
||||
return nil
|
||||
end
|
||||
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
|
||||
return nil, "truncated CFF header"
|
||||
end
|
||||
local major, hdrSize = u8(b, base), u8(b, base + 2)
|
||||
if major == 2 then
|
||||
return nil, "unsupported: CFF2 (variable font outlines)"
|
||||
end
|
||||
if major ~= 1 then
|
||||
return nil, "bad CFF version " .. major
|
||||
end
|
||||
local names = index(b, base + hdrSize, limit)
|
||||
if not names then
|
||||
return nil, "bad name INDEX"
|
||||
end
|
||||
local tops = index(b, names.next, limit)
|
||||
if not tops or tops.count < 1 then
|
||||
return nil, "bad Top DICT INDEX"
|
||||
end
|
||||
local strings = index(b, tops.next, limit)
|
||||
if not strings then
|
||||
return nil, "bad String INDEX"
|
||||
end
|
||||
local gsubrs = index(b, strings.next, limit)
|
||||
if not gsubrs then
|
||||
return nil, "bad global subrs INDEX"
|
||||
end
|
||||
local ts, tl = item(b, tops, 0)
|
||||
if not ts then
|
||||
return nil, "bad Top DICT"
|
||||
end
|
||||
local top = dict(b, ts, tl)
|
||||
local cso = top[17]
|
||||
if not cso or #cso < 1 then
|
||||
return nil, "no CharStrings"
|
||||
end
|
||||
local cst = top[1206]
|
||||
if cst and cst[1] ~= 2 then
|
||||
return nil, "unsupported: charstring type " .. tostring(cst[1])
|
||||
end
|
||||
local charStrings = index(b, base + cso[1], limit)
|
||||
if not charStrings then
|
||||
return nil, "bad CharStrings INDEX"
|
||||
end
|
||||
local f: Font = {
|
||||
buf = b,
|
||||
base = base,
|
||||
limit = limit,
|
||||
charStrings = charStrings,
|
||||
gsubrs = gsubrs,
|
||||
subrs = nil,
|
||||
fdSubrs = nil,
|
||||
fdSelect = nil,
|
||||
numGlyphs = charStrings.count,
|
||||
cid = top[1230] ~= nil,
|
||||
fontMatrix = top[1207],
|
||||
}
|
||||
if f.cid then
|
||||
local fda, fds = top[1236], top[1237]
|
||||
if not fda or not fds then
|
||||
return nil, "CID font without FDArray / FDSelect"
|
||||
end
|
||||
local fdArray = index(b, base + fda[1], limit)
|
||||
if not fdArray then
|
||||
return nil, "bad FDArray"
|
||||
end
|
||||
local list: { Index? } = {}
|
||||
for i = 0, math.min(fdArray.count, 256) - 1 do
|
||||
local s, l = item(b, fdArray, i)
|
||||
list[i + 1] = if s then privateSubrs(b, base, limit, dict(b, s, l)) else nil
|
||||
end
|
||||
f.fdSubrs = list
|
||||
f.fdSelect = base + fds[1]
|
||||
else
|
||||
f.subrs = privateSubrs(b, base, limit, top)
|
||||
end
|
||||
return f, nil
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
local b, p = f.buf, f.fdSelect :: number
|
||||
local fmt = u8(b, p)
|
||||
local fd = -1
|
||||
if fmt == 0 then
|
||||
fd = u8(b, p + 1 + g)
|
||||
elseif fmt == 3 then
|
||||
local n = uN(b, p + 1, 2)
|
||||
local first = uN(b, p + 3, 2)
|
||||
for i = 0, n - 1 do
|
||||
local q = p + 5 + i * 3
|
||||
local v = u8(b, q)
|
||||
local nxt = uN(b, q + 1, 2)
|
||||
if g >= first and g < nxt then
|
||||
fd = v
|
||||
break
|
||||
end
|
||||
first = nxt
|
||||
end
|
||||
end
|
||||
local list = f.fdSubrs :: { Index? }
|
||||
return list[fd + 1]
|
||||
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)
|
||||
if not p then
|
||||
return nil, "bad glyph index " .. tostring(g)
|
||||
end
|
||||
local e = p + len
|
||||
local cmds: { any } = {}
|
||||
local s: { number } = table.create(48, 0) -- operand stack, 1-based; sp = count
|
||||
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 subrs: Index? = nil
|
||||
local haveSubrs = false
|
||||
local ops = 0
|
||||
local seed = 12345
|
||||
|
||||
local function moveTo(dx: number, dy: number)
|
||||
if open then
|
||||
table.insert(cmds, { op = "close" })
|
||||
end
|
||||
x += dx
|
||||
y += dy
|
||||
table.insert(cmds, { op = "moveTo", x, y })
|
||||
open = true
|
||||
end
|
||||
local function lineTo(dx: number, dy: number)
|
||||
if not open then
|
||||
moveTo(0, 0)
|
||||
end
|
||||
x += dx
|
||||
y += dy
|
||||
table.insert(cmds, { op = "lineTo", x, y })
|
||||
end
|
||||
local function curveTo(dx1: number, dy1: number, dx2: number, dy2: number, dx3: number, dy3: number)
|
||||
if not open then
|
||||
moveTo(0, 0)
|
||||
end
|
||||
local x1, y1 = x + dx1, y + dy1
|
||||
local x2, y2 = x1 + dx2, y1 + dy2
|
||||
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
|
||||
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]
|
||||
continue
|
||||
end
|
||||
break
|
||||
end
|
||||
ops += 1
|
||||
if ops > BUDGET then
|
||||
return nil, "charstring too long (subroutine loop?)"
|
||||
end
|
||||
local b0 = u8(b, p)
|
||||
p += 1
|
||||
local clear = true
|
||||
local i = 0
|
||||
if b0 == 19 or b0 == 20 then -- hintmask, cntrmask
|
||||
if inHeader then
|
||||
maskbits += sp // 2
|
||||
end
|
||||
inHeader = false
|
||||
p += (maskbits + 7) // 8
|
||||
elseif b0 == 1 or b0 == 3 or b0 == 18 or b0 == 23 then -- hstem vstem hstemhm vstemhm
|
||||
maskbits += sp // 2
|
||||
elseif b0 == 21 then -- rmoveto
|
||||
inHeader = false
|
||||
if sp < 2 then
|
||||
return nil, "rmoveto stack"
|
||||
end
|
||||
moveTo(S(sp - 2), S(sp - 1))
|
||||
elseif b0 == 4 then -- vmoveto
|
||||
inHeader = false
|
||||
if sp < 1 then
|
||||
return nil, "vmoveto stack"
|
||||
end
|
||||
moveTo(0, S(sp - 1))
|
||||
elseif b0 == 22 then -- hmoveto
|
||||
inHeader = false
|
||||
if sp < 1 then
|
||||
return nil, "hmoveto stack"
|
||||
end
|
||||
moveTo(S(sp - 1), 0)
|
||||
elseif b0 == 5 then -- rlineto
|
||||
if sp < 2 then
|
||||
return nil, "rlineto stack"
|
||||
end
|
||||
while i + 1 < sp do
|
||||
lineTo(S(i), S(i + 1))
|
||||
i += 2
|
||||
end
|
||||
elseif b0 == 6 or b0 == 7 then -- hlineto / vlineto: alternate
|
||||
if sp < 1 then
|
||||
return nil, "hlineto stack"
|
||||
end
|
||||
local horiz = b0 == 6
|
||||
while i < sp do
|
||||
if horiz then
|
||||
lineTo(S(i), 0)
|
||||
else
|
||||
lineTo(0, S(i))
|
||||
end
|
||||
horiz = not horiz
|
||||
i += 1
|
||||
end
|
||||
elseif b0 == 31 or b0 == 30 then -- hvcurveto / vhcurveto: alternate
|
||||
if sp < 4 then
|
||||
return nil, "hvcurveto stack"
|
||||
end
|
||||
local horiz = b0 == 31
|
||||
while i + 3 < sp do
|
||||
local last = if sp - i == 5 then S(i + 4) else 0
|
||||
if horiz then
|
||||
curveTo(S(i), 0, S(i + 1), S(i + 2), last, S(i + 3))
|
||||
else
|
||||
curveTo(0, S(i), S(i + 1), S(i + 2), S(i + 3), last)
|
||||
end
|
||||
horiz = not horiz
|
||||
i += 4
|
||||
end
|
||||
elseif b0 == 8 then -- rrcurveto
|
||||
if sp < 6 then
|
||||
return nil, "rrcurveto stack"
|
||||
end
|
||||
while i + 5 < sp do
|
||||
curveTo(S(i), S(i + 1), S(i + 2), S(i + 3), S(i + 4), S(i + 5))
|
||||
i += 6
|
||||
end
|
||||
elseif b0 == 24 then -- rcurveline
|
||||
if sp < 8 then
|
||||
return nil, "rcurveline stack"
|
||||
end
|
||||
while i + 5 < sp - 2 do
|
||||
curveTo(S(i), S(i + 1), S(i + 2), S(i + 3), S(i + 4), S(i + 5))
|
||||
i += 6
|
||||
end
|
||||
lineTo(S(i), S(i + 1))
|
||||
elseif b0 == 25 then -- rlinecurve
|
||||
if sp < 8 then
|
||||
return nil, "rlinecurve stack"
|
||||
end
|
||||
while i + 1 < sp - 6 do
|
||||
lineTo(S(i), S(i + 1))
|
||||
i += 2
|
||||
end
|
||||
curveTo(S(i), S(i + 1), S(i + 2), S(i + 3), S(i + 4), S(i + 5))
|
||||
elseif b0 == 26 or b0 == 27 then -- vvcurveto / hhcurveto
|
||||
if sp < 4 then
|
||||
return nil, "vv/hhcurveto stack"
|
||||
end
|
||||
local f0 = 0
|
||||
if sp % 2 == 1 then
|
||||
f0 = S(0)
|
||||
i = 1
|
||||
end
|
||||
while i + 3 < sp do
|
||||
if b0 == 27 then
|
||||
curveTo(S(i), f0, S(i + 1), S(i + 2), S(i + 3), 0)
|
||||
else
|
||||
curveTo(f0, S(i), S(i + 1), S(i + 2), 0, S(i + 3))
|
||||
end
|
||||
f0 = 0
|
||||
i += 4
|
||||
end
|
||||
elseif b0 == 10 or b0 == 29 then -- callsubr / callgsubr
|
||||
if b0 == 10 and not haveSubrs then
|
||||
subrs = localSubrs(f, g)
|
||||
haveSubrs = true
|
||||
end
|
||||
if sp < 1 then
|
||||
return nil, "callsubr stack"
|
||||
end
|
||||
local idx = if b0 == 10 then subrs else f.gsubrs
|
||||
if not idx then
|
||||
return nil, "no subroutines"
|
||||
end
|
||||
local n = math.floor(S(sp - 1)) + bias(idx.count)
|
||||
sp -= 1
|
||||
if #stack >= 10 then
|
||||
return nil, "subroutine recursion limit"
|
||||
end
|
||||
local sp0, sl = item(b, idx, n)
|
||||
if not sp0 then
|
||||
return nil, "subroutine " .. n .. " not found"
|
||||
end
|
||||
table.insert(stack, { p, e })
|
||||
p, e = sp0, sp0 + sl
|
||||
clear = false
|
||||
elseif b0 == 11 then -- return
|
||||
if #stack == 0 then
|
||||
return nil, "return outside a subroutine"
|
||||
end
|
||||
local r = table.remove(stack) :: { number }
|
||||
p, e = r[1], r[2]
|
||||
clear = false
|
||||
elseif b0 == 14 then -- endchar
|
||||
if open then
|
||||
table.insert(cmds, { op = "close" })
|
||||
end
|
||||
return cmds, nil
|
||||
elseif b0 == 12 then -- escape
|
||||
local b1 = u8(b, p)
|
||||
p += 1
|
||||
if b1 == 34 then -- hflex
|
||||
if sp < 7 then
|
||||
return nil, "hflex stack"
|
||||
end
|
||||
curveTo(S(0), 0, S(1), S(2), S(3), 0)
|
||||
curveTo(S(4), 0, S(5), -S(2), S(6), 0)
|
||||
elseif b1 == 35 then -- flex
|
||||
if sp < 13 then
|
||||
return nil, "flex stack"
|
||||
end
|
||||
curveTo(S(0), S(1), S(2), S(3), S(4), S(5))
|
||||
curveTo(S(6), S(7), S(8), S(9), S(10), S(11))
|
||||
elseif b1 == 36 then -- hflex1
|
||||
if sp < 9 then
|
||||
return nil, "hflex1 stack"
|
||||
end
|
||||
curveTo(S(0), S(1), S(2), S(3), S(4), 0)
|
||||
curveTo(S(5), 0, S(6), S(7), S(8), -(S(1) + S(3) + S(7)))
|
||||
elseif b1 == 37 then -- flex1
|
||||
if sp < 11 then
|
||||
return nil, "flex1 stack"
|
||||
end
|
||||
local dx = S(0) + S(2) + S(4) + S(6) + S(8)
|
||||
local dy = S(1) + S(3) + S(5) + S(7) + S(9)
|
||||
local dx6, dy6 = S(10), S(10)
|
||||
if math.abs(dx) > math.abs(dy) then
|
||||
dy6 = -dy
|
||||
else
|
||||
dx6 = -dx
|
||||
end
|
||||
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
|
||||
end
|
||||
if b1 == 3 then
|
||||
set(2, if a ~= 0 and c ~= 0 then 1 else 0) -- and
|
||||
elseif b1 == 4 then
|
||||
set(2, if a ~= 0 or c ~= 0 then 1 else 0) -- or
|
||||
elseif b1 == 5 then
|
||||
set(1, if c == 0 then 1 else 0) -- not
|
||||
elseif b1 == 9 then
|
||||
set(1, math.abs(c))
|
||||
elseif b1 == 10 then
|
||||
set(2, a + c)
|
||||
elseif b1 == 11 then
|
||||
set(2, a - c)
|
||||
elseif b1 == 12 then
|
||||
set(2, if c ~= 0 then a / c else 0)
|
||||
elseif b1 == 14 then
|
||||
set(1, -c)
|
||||
elseif b1 == 15 then
|
||||
set(2, if a == c then 1 else 0)
|
||||
elseif b1 == 18 then
|
||||
sp = math.max(0, sp - 1) -- drop
|
||||
elseif b1 == 20 then -- put: val i
|
||||
local k = math.floor(c)
|
||||
if k >= 0 and k < 32 then
|
||||
trans[k + 1] = a
|
||||
end
|
||||
sp = math.max(0, sp - 2)
|
||||
elseif b1 == 21 then -- get
|
||||
local k = math.floor(c)
|
||||
set(1, if k >= 0 and k < 32 then trans[k + 1] else 0)
|
||||
elseif b1 == 22 then -- ifelse: s1 s2 v1 v2 -> v1 <= v2 ? s1 : s2
|
||||
local s1, s2, v1, v2 = S(top - 4), S(top - 3), S(top - 2), S(top - 1)
|
||||
set(4, if v1 <= v2 then s1 else s2)
|
||||
elseif b1 == 23 then -- random (deterministic)
|
||||
seed = (seed * 1103515245 + 12345) % 2147483648
|
||||
set(0, (seed % 65535 + 1) / 65536)
|
||||
elseif b1 == 24 then
|
||||
set(2, a * c)
|
||||
elseif b1 == 26 then
|
||||
set(1, math.sqrt(math.max(0, c)))
|
||||
elseif b1 == 27 then
|
||||
set(0, c) -- dup
|
||||
elseif b1 == 28 then -- exch
|
||||
if sp >= 2 then
|
||||
s[sp - 1], s[sp] = s[sp], s[sp - 1]
|
||||
end
|
||||
elseif b1 == 29 then -- index
|
||||
local k = math.floor(c)
|
||||
local v = if k < 0 then S(top - 2) else S(top - 2 - k)
|
||||
set(1, v)
|
||||
elseif b1 == 30 then -- roll: n j
|
||||
local n, j = math.floor(a), math.floor(c)
|
||||
sp = math.max(0, sp - 2)
|
||||
if n > 0 and n <= sp then
|
||||
local base = sp - n
|
||||
local tmp = table.create(n, 0)
|
||||
for k = 0, n - 1 do
|
||||
tmp[((k + j) % n) + 1] = s[base + k + 1]
|
||||
end
|
||||
for k = 0, n - 1 do
|
||||
s[base + k + 1] = tmp[k + 1]
|
||||
end
|
||||
end
|
||||
else
|
||||
return nil, "unknown operator 12 " .. b1
|
||||
end
|
||||
if sp > 48 then
|
||||
return nil, "stack overflow"
|
||||
end
|
||||
end
|
||||
elseif b0 >= 32 or b0 == 28 then -- operands
|
||||
local v
|
||||
if b0 == 28 then
|
||||
v = uN(b, p, 2)
|
||||
if v >= 32768 then
|
||||
v -= 65536
|
||||
end
|
||||
p += 2
|
||||
elseif b0 <= 246 then
|
||||
v = b0 - 139
|
||||
elseif b0 <= 250 then
|
||||
v = (b0 - 247) * 256 + u8(b, p) + 108
|
||||
p += 1
|
||||
elseif b0 <= 254 then
|
||||
v = -(b0 - 251) * 256 - u8(b, p) - 108
|
||||
p += 1
|
||||
else -- 255: 16.16 fixed
|
||||
v = uN(b, p, 4)
|
||||
if v >= 2147483648 then
|
||||
v -= 4294967296
|
||||
end
|
||||
v /= 65536
|
||||
p += 4
|
||||
end
|
||||
if sp >= 48 then
|
||||
return nil, "operand stack overflow"
|
||||
end
|
||||
sp += 1
|
||||
s[sp] = v
|
||||
clear = false
|
||||
else
|
||||
return nil, "reserved operator " .. b0
|
||||
end
|
||||
if clear then
|
||||
sp = 0
|
||||
end
|
||||
end
|
||||
return nil, "no endchar"
|
||||
end
|
||||
|
||||
return Cff
|
||||
Reference in new issue
Block a user