rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,132 @@
|
||||
--!strict
|
||||
-- font/woff: WOFF 1.0 (W3C Recommendation, 2012): an sfnt font whose tables are zlib-compressed one by one. Decoding
|
||||
-- rebuilds the sfnt and reads it through codecs/font/TrueType (TrueType or CFF outlines, see font/ttf); the extended
|
||||
-- metadata XML is kept in meta.woffMetadata. Written from the spec. Not supported: writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Deflate = require("@rosetta/codecs/compress/Deflate")
|
||||
local TrueType = require("@rosetta/codecs/font/TrueType")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Woff = {
|
||||
name = "woff",
|
||||
category = "font",
|
||||
extensions = { "woff" },
|
||||
mime = { "font/woff" },
|
||||
deps = { "core/Err", "core/Bytes", "codecs/compress/Deflate", "codecs/font/TrueType" },
|
||||
caps = { decode = true, encode = false, outlines = { "truetype", "cff" } },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse a font whose sfnt is bigger (default 2^28)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Woff.sniff(r: Bytes.Reader): number
|
||||
return if r:peek(4) == "wOFF" then 1 else 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- sfnt(r, opts?) -> the rebuilt sfnt font file (a buffer) | nil, Err
|
||||
function Woff.sfnt(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?, string?)
|
||||
local maxBytes = (opts and opts.maxBytes) or 2 ^ 28
|
||||
local base = r.base
|
||||
if not r:match("wOFF") then
|
||||
return nil, Err.new(Err.CORRUPT, "bad signature", base), nil
|
||||
end
|
||||
local flavor = r:string(4)
|
||||
local length = r:u32be()
|
||||
local numTables = r:u16be()
|
||||
r:skip(2)
|
||||
local total = r:u32be()
|
||||
r:skip(4)
|
||||
local metaOffset, metaLength, metaOrig = r:u32be(), r:u32be(), r:u32be()
|
||||
r:skip(8)
|
||||
if r.err then
|
||||
return nil, r.err, nil
|
||||
end
|
||||
if flavor == "ttcf" then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "font collections in WOFF", base + 4), nil
|
||||
end
|
||||
if length > r:size() then
|
||||
return nil, Err.new(Err.TRUNCATED, string.format("file is %d bytes, header says %d", r:size(), length), base + 8), nil
|
||||
end
|
||||
if numTables == 0 or total > maxBytes or total < 12 + 16 * numTables then
|
||||
return nil, Err.new(if total > maxBytes then Err.LIMIT else Err.CORRUPT, "bad table count or sfnt size", base + 12), nil
|
||||
end
|
||||
local dir = {}
|
||||
for i = 1, numTables do
|
||||
local e = { tag = r:string(4), offset = r:u32be(), comp = r:u32be(), orig = r:u32be(), sum = r:u32be() }
|
||||
dir[i] = e
|
||||
end
|
||||
if r.err then
|
||||
return nil, r.err, nil
|
||||
end
|
||||
local out = buffer.create(total)
|
||||
local w = Bytes.writer(12)
|
||||
w:string(flavor)
|
||||
w:u16be(numTables)
|
||||
local es, sel = 1, 0
|
||||
while es * 2 <= numTables do
|
||||
es *= 2
|
||||
sel += 1
|
||||
end
|
||||
w:u16be(es * 16)
|
||||
w:u16be(sel)
|
||||
w:u16be(numTables * 16 - es * 16)
|
||||
buffer.copy(out, 0, w.buf, 0, 12)
|
||||
local p = 12 + 16 * numTables
|
||||
for i, e in dir do
|
||||
if e.offset + e.comp > r:size() or e.comp > e.orig then
|
||||
return nil, Err.new(Err.CORRUPT, "table " .. e.tag .. " out of range", base + 44 + (i - 1) * 20), nil
|
||||
end
|
||||
if p + e.orig > total then
|
||||
return nil, Err.new(Err.CORRUPT, "tables larger than totalSfntSize", base + 16), nil
|
||||
end
|
||||
local d = 12 + (i - 1) * 16
|
||||
buffer.writestring(out, d, e.tag)
|
||||
buffer.writeu32(out, d + 4, bit32.byteswap(e.sum))
|
||||
buffer.writeu32(out, d + 8, bit32.byteswap(p))
|
||||
buffer.writeu32(out, d + 12, bit32.byteswap(e.orig))
|
||||
if e.comp < e.orig then
|
||||
local t, err = Deflate.zlib(r.buf, base + e.offset, e.comp, e.orig)
|
||||
if not t then
|
||||
return nil, Err.new((err :: any).code, "table " .. e.tag .. ": " .. (err :: any).message, base + e.offset), nil
|
||||
end
|
||||
buffer.copy(out, p, t, 0, e.orig)
|
||||
else
|
||||
buffer.copy(out, p, r.buf, base + e.offset, e.orig)
|
||||
end
|
||||
p += e.orig
|
||||
p = (p + 3) // 4 * 4
|
||||
if p > total then
|
||||
p = total
|
||||
end
|
||||
end
|
||||
local xml = nil
|
||||
if metaLength > 0 and metaOffset + metaLength <= r:size() then
|
||||
local m = Deflate.zlib(r.buf, base + metaOffset, metaLength, metaOrig)
|
||||
if m then
|
||||
xml = buffer.tostring(m)
|
||||
end
|
||||
end
|
||||
return out, nil, xml
|
||||
end
|
||||
|
||||
function Woff.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local sfnt, err, xml = Woff.sfnt(r, opts)
|
||||
if not sfnt then
|
||||
return nil, err
|
||||
end
|
||||
local font, ferr = TrueType.font(sfnt)
|
||||
if not font then
|
||||
return nil, ferr
|
||||
end
|
||||
font.meta.woffMetadata = xml
|
||||
font.meta.source = { format = "woff", outlines = font.meta.outlines }
|
||||
return font, nil
|
||||
end
|
||||
|
||||
return Woff
|
||||
Reference in new issue
Block a user