- core/Err.from(message, offset?, kind?): the private-submodule -> Err plumbing (a kind, or a 'code: ' prefix / 'truncated' / 'past the end' in the message) that Brotli, Bzip2, Zstd, Jpeg, WOFF2, FBX, plist, msgpack and bmfont each wrote. - core/Sniff (new): Sniff.text(r, n, trim?) (the start of a text file without its BOM), Sniff.dropBom(s), Sniff.magic(r, s); 13 text formats' sniff / decode BOM handling goes through it. - codecs/Pcm.drain(dec, source?, maxFrames?): a format's decode() through its open(); the collect-the-Decoder loops of aac, aiff, au, caf, flac, m4a, mp3, opus, vorbis and wav are one call each. - core/Bits.msbPut: the MSB-first bit writer the MP3 / MP2 / FLAC encoders each copied. - tools/lint_rules.py DUP: a function body of 12+ code lines repeated token for token in another module fails. - tools/move.py: moves a module and rewrites every reference (requires, deps, headers, fixture INPUTS, docs). - tools/fixtures/zstd.py: the incompressible corpus is smaller (zstd.luau 15.1 -> 13.4 MB; every fixture module is now under 14.3 MB). - tools/bench.luau: MPEG-1 also runs with the api's LuaJIT trace limits; the sections that compare tuned / default limits set them explicitly (jitTune), so the order of sections no longer leaks the tuning.
58 lines
2.3 KiB
Luau
58 lines
2.3 KiB
Luau
--!strict
|
|
-- font/woff2: WOFF 2.0 (W3C Recommendation): sfnt tables in one Brotli stream, with the glyf / loca and hmtx
|
|
-- transforms. Decoding rebuilds the sfnt (font/woff2/Rebuild) and reads it through codecs/TrueType (TrueType or CFF
|
|
-- outlines, see font/ttf). Written from the spec; Brotli is codecs/Brotli. Not supported: collections, writing.
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local TrueType = require("@formats/codecs/TrueType")
|
|
local Rebuild = require("@formats/font/woff2/Rebuild")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Woff2 = {
|
|
name = "woff2",
|
|
category = "font",
|
|
extensions = { "woff2" },
|
|
mime = { "font/woff2" },
|
|
deps = { "core/Err", "core/Bytes", "codecs/TrueType", "codecs/Brotli" },
|
|
caps = { decode = true, encode = false, outlines = { "truetype", "cff" }, transforms = { "glyf", "loca", "hmtx" } },
|
|
}
|
|
|
|
export type DecodeOptions = {
|
|
maxBytes: number?, -- refuse a font whose sfnt is bigger (default 2^28)
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ constants
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Woff2.sniff(r: Bytes.Reader): number
|
|
return if r:peek(4) == "wOF2" then 1 else 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
-- sfnt(r, opts?) -> the rebuilt sfnt font file | nil, Err
|
|
function Woff2.sfnt(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?)
|
|
local maxBytes = (opts and opts.maxBytes) or 2 ^ 28
|
|
local at = r.base
|
|
local out, msg, code = Rebuild.sfnt(r, maxBytes)
|
|
if not out then
|
|
return nil, Err.from(msg or "rebuild failed", at, code or "corrupt")
|
|
end
|
|
return out, nil
|
|
end
|
|
|
|
function Woff2.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
|
local sfnt, err = Woff2.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.source = { format = "woff2", outlines = font.meta.outlines }
|
|
return font, nil
|
|
end
|
|
|
|
return Woff2
|