font formats: comments are specs only
This commit is contained in:
1 parent
0452f26eef
commit
d91dc6c597
14 files changed
+64
-167
No files matched your search
@@ -1,10 +1,5 @@
|
||||
--!strict
|
||||
-- font/woff2/Rebuild: rebuilds the sfnt of a WOFF2 file (W3C WOFF2 Recommendation, 2024 edition): the table
|
||||
-- directory, the Brotli stream, the transformed glyf / loca tables (section 5: nContour, nPoints, flag, glyph,
|
||||
-- composite, bbox and instruction streams, triplet-encoded coordinates, the overlap bitmap) and the transformed hmtx
|
||||
-- (section 6: side bearings from the glyph bounds). Written from the spec. Not supported: collections (ttcf flavor).
|
||||
--
|
||||
-- Never throws: malformed data returns nil, message.
|
||||
-- font/woff2/Rebuild: the sfnt of a WOFF2 file (W3C WOFF2, 2024 edition: section 5 glyf / loca, section 6 hmtx).
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Brotli = require("@rosetta/codecs/compress/Brotli")
|
||||
|
||||
@@ -40,7 +35,6 @@ local function base128(r: Bytes.Reader): number?
|
||||
return nil
|
||||
end
|
||||
|
||||
-- a cursor over a stream inside the decompressed block
|
||||
type Stream = { buf: buffer, p: number, e: number, bad: boolean }
|
||||
local function stream(b: buffer, p: number, n: number): Stream
|
||||
return { buf = b, p = p, e = p + n, bad = false }
|
||||
@@ -77,7 +71,6 @@ local function withSign(flag: number, v: number): number
|
||||
return if band(flag, 1) ~= 0 then v else -v
|
||||
end
|
||||
|
||||
-- the glyf transform -> glyf bytes (a Writer), loca offsets, per-glyph xMin (for hmtx), numGlyphs, indexFormat
|
||||
local function glyf(b: buffer, p: number, n: number, maxBytes: number): (any?, string?)
|
||||
local h = stream(b, p, n)
|
||||
su16(h) -- reserved
|
||||
@@ -132,7 +125,6 @@ local function glyf(b: buffer, p: number, n: number, maxBytes: number): (any?, s
|
||||
return nil, "empty glyph with a bounding box"
|
||||
end
|
||||
elseif nc > 0 then
|
||||
-- simple glyph
|
||||
local ends = table.create(nc, 0)
|
||||
local total = 0
|
||||
for c = 1, nc do
|
||||
@@ -363,7 +355,6 @@ local function hmtx(b: buffer, p: number, n: number, numGlyphs: number, numH: nu
|
||||
return out:finish(), nil
|
||||
end
|
||||
|
||||
-- sfnt(r, maxBytes) -> sfnt buffer | nil, message, code ("truncated" / "corrupt" / "unsupported" / "limit")
|
||||
function Rebuild.sfnt(r: Bytes.Reader, maxBytes: number): (buffer?, string?, string?)
|
||||
if not r:match("wOF2") then
|
||||
return nil, "bad signature", "corrupt"
|
||||
@@ -433,7 +424,6 @@ function Rebuild.sfnt(r: Bytes.Reader, maxBytes: number): (buffer?, string?, str
|
||||
if buffer.len(data) < streamTotal then
|
||||
return nil, "decompressed stream shorter than the tables", "corrupt"
|
||||
end
|
||||
-- the tables, glyf / loca / hmtx rebuilt
|
||||
local byTag: { [string]: any } = {}
|
||||
for _, e in dir do
|
||||
byTag[e.tag] = e
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
--!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/font/TrueType (TrueType or CFF
|
||||
-- outlines, see font/ttf). Written from the spec; Brotli is codecs/compress/Brotli. Not supported: collections, writing.
|
||||
-- font/woff2: WOFF 2.0 (W3C Recommendation): Brotli-compressed sfnt tables with the glyf / loca and hmtx transforms,
|
||||
-- rebuilt by font/woff2/Rebuild. Not supported: collections, writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -20,7 +19,7 @@ local Woff2 = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse a font whose sfnt is bigger (default 2^28)
|
||||
maxBytes: number?, -- default 2^28
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -30,7 +29,6 @@ function Woff2.sniff(r: Bytes.Reader): number
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user