ws-arch2: font/eot (EOT 1.0 / 2.1 / 2.2, XOR; read via codecs/font/TrueType, write 2.1; MTX unsupported)
This commit is contained in:
1 parent
375140ece2
commit
e34e32901f
5 files changed
+377
No files matched your search
@@ -518,6 +518,18 @@ local list: { Entry } = {
|
||||
return require("@formats/font/bmfont")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "eot",
|
||||
category = "font",
|
||||
path = "font/eot",
|
||||
extensions = { "eot" },
|
||||
mime = { "application/vnd.ms-fontobject" },
|
||||
deps = { "core/Err", "core/Bytes", "codecs/text/Text", "codecs/font/TrueType" },
|
||||
caps = { decode = true, encode = true, variants = { "1.0", "2.1", "2.2" } },
|
||||
load = function()
|
||||
return require("@formats/font/eot")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "fon",
|
||||
category = "font",
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
--!strict
|
||||
-- font/eot: Embedded OpenType (.eot; versions 0x00010000, 0x00020001 and 0x00020002): the EOT header (PANOSE, charset,
|
||||
-- weight, fsType, Unicode / code page ranges, family / style / version / full names, the root string, EUDC data) and
|
||||
-- the font data inside, XOR-0x50 obfuscated or not, read as the standard Font through codecs/font/TrueType (meta.eot
|
||||
-- holds the header fields, meta.fontData the TrueType bytes). Writes EOT 2.1 around TrueType bytes (a buffer / string,
|
||||
-- or a Font read from an EOT), filling the header from the font's OS/2, head and name tables. Written from the W3C
|
||||
-- EOT submission (2008). Not supported: MicroType Express compressed fonts (flag 0x4: Err.UNSUPPORTED), signatures.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Text = require("@formats/codecs/text/Text")
|
||||
local TrueType = require("@formats/codecs/font/TrueType")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Eot = {
|
||||
name = "eot",
|
||||
category = "font",
|
||||
extensions = { "eot" },
|
||||
mime = { "application/vnd.ms-fontobject" },
|
||||
deps = { "core/Err", "core/Bytes", "codecs/text/Text", "codecs/font/TrueType" },
|
||||
caps = { decode = true, encode = true, variants = { "1.0", "2.1", "2.2" } },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {}
|
||||
export type EncodeOptions = {
|
||||
xor: boolean?, -- obfuscate the font data with XOR 0x50 (default false)
|
||||
rootString: string?, -- the URLs allowed to use the font (default "")
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local MAGIC = 0x504C
|
||||
local COMPRESSED, XOR = 0x4, 0x10000000
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Eot.sniff(r: Bytes.Reader): number
|
||||
local h = r:peek(36)
|
||||
if #h < 36 then
|
||||
return 0
|
||||
end
|
||||
local ver = string.sub(h, 9, 12)
|
||||
local magic = string.byte(h, 35) + string.byte(h, 36) * 256
|
||||
if magic == MAGIC and (ver == "\0\0\1\0" or ver == "\1\0\2\0" or ver == "\2\0\2\0") then
|
||||
return 1
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Eot.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local eotSize, dataSize, version, flags = r:u32le(), r:u32le(), r:u32le(), r:u32le()
|
||||
local panose = r:string(10)
|
||||
local charset, italic = r:u8(), r:u8()
|
||||
local weight = r:u32le()
|
||||
local fsType, magic = r:u16le(), r:u16le()
|
||||
local ranges = { r:u32le(), r:u32le(), r:u32le(), r:u32le() }
|
||||
local codepages = { r:u32le(), r:u32le() }
|
||||
local checksum = r:u32le()
|
||||
r:skip(16)
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
if magic ~= MAGIC then
|
||||
return nil, Err.new(Err.CORRUPT, "bad magic number")
|
||||
end
|
||||
if version ~= 0x00010000 and version ~= 0x00020001 and version ~= 0x00020002 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, string.format("version 0x%08x", version))
|
||||
end
|
||||
local function name(): string
|
||||
r:skip(2)
|
||||
local n = r:u16le()
|
||||
local s = r:bytes(n)
|
||||
return Text.utf16(s, 0, buffer.len(s))
|
||||
end
|
||||
local eot: { [string]: any } = {
|
||||
version = version,
|
||||
flags = flags,
|
||||
panose = panose,
|
||||
charset = charset,
|
||||
italic = italic ~= 0,
|
||||
weight = weight,
|
||||
fsType = fsType,
|
||||
unicodeRange = ranges,
|
||||
codePageRange = codepages,
|
||||
checkSumAdjustment = checksum,
|
||||
eotSize = eotSize,
|
||||
}
|
||||
eot.familyName, eot.styleName, eot.versionName, eot.fullName = name(), name(), name(), name()
|
||||
if version >= 0x00020001 then
|
||||
eot.rootString = name()
|
||||
end
|
||||
if version == 0x00020002 then
|
||||
eot.rootStringChecksum = r:u32le()
|
||||
eot.eudcCodePage = r:u32le()
|
||||
r:skip(2)
|
||||
r:skip(r:u16le()) -- signature
|
||||
eot.eudcFlags = r:u32le()
|
||||
r:skip(r:u32le()) -- EUDC font data
|
||||
end
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
if bit32.band(flags, COMPRESSED) ~= 0 then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "MicroType Express compressed font data")
|
||||
end
|
||||
local data = r:bytes(dataSize)
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
if bit32.band(flags, XOR) ~= 0 then
|
||||
for i = 0, dataSize - 1 do
|
||||
buffer.writeu8(data, i, bit32.bxor(buffer.readu8(data, i), 0x50))
|
||||
end
|
||||
end
|
||||
local font, ferr = TrueType.font(data)
|
||||
if not font then
|
||||
return nil, ferr
|
||||
end
|
||||
font.meta.eot = eot
|
||||
font.meta.fontData = data
|
||||
font.meta.source = { format = "eot", version = version, outlines = font.meta.outlines }
|
||||
return font, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local function utf16(s: string): string
|
||||
local out = {}
|
||||
if utf8.len(s) then
|
||||
for _, c in utf8.codes(s) do
|
||||
if c >= 0x10000 then
|
||||
c -= 0x10000
|
||||
local hi, lo = 0xD800 + bit32.rshift(c, 10), 0xDC00 + bit32.band(c, 0x3FF)
|
||||
table.insert(out, string.char(hi % 256, hi // 256, lo % 256, lo // 256))
|
||||
else
|
||||
table.insert(out, string.char(c % 256, c // 256))
|
||||
end
|
||||
end
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
function Eot.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local o: EncodeOptions = opts or {}
|
||||
local data: buffer
|
||||
if type(value) == "buffer" then
|
||||
data = value
|
||||
elseif type(value) == "string" then
|
||||
data = buffer.fromstring(value)
|
||||
elseif type(value) == "table" and type(value.meta) == "table" and type(value.meta.fontData) == "buffer" then
|
||||
data = value.meta.fontData
|
||||
else
|
||||
return nil, Err.new(Err.ARG, "expected TrueType bytes (or a Font read from an EOT)")
|
||||
end
|
||||
local font, ferr = TrueType.font(data)
|
||||
if not font then
|
||||
return nil, ferr
|
||||
end
|
||||
local tables = TrueType.tables(data, 0, 0, buffer.len(data)) or {}
|
||||
local function tab(tagName: string): Bytes.Reader?
|
||||
local t = tables[tagName]
|
||||
return if t then Bytes.reader(data, t[1], t[2]) else nil
|
||||
end
|
||||
-- OS/2: PANOSE, weight, fsType, ranges, selection; head: checkSumAdjustment
|
||||
local panose, weight, fsType, italic, charset = string.rep("\0", 10), 400, 0, 0, 1
|
||||
local ranges, codepages = { 0, 0, 0, 0 }, { 0, 0 }
|
||||
local os2 = tab("OS/2")
|
||||
if os2 then
|
||||
os2:seek(4)
|
||||
weight = os2:u16be()
|
||||
os2:seek(8)
|
||||
fsType = os2:u16be()
|
||||
os2:seek(32)
|
||||
panose = os2:string(10)
|
||||
ranges = { os2:u32be(), os2:u32be(), os2:u32be(), os2:u32be() }
|
||||
os2:seek(62)
|
||||
local sel = os2:u16be()
|
||||
italic = if bit32.band(sel, 1) ~= 0 then 1 else 0
|
||||
os2:seek(78)
|
||||
codepages = { os2:u32be(), os2:u32be() }
|
||||
if os2.err then
|
||||
codepages = { 0, 0 }
|
||||
end
|
||||
end
|
||||
local head = tab("head")
|
||||
local checksum = 0
|
||||
if head then
|
||||
head:seek(8)
|
||||
checksum = head:u32be()
|
||||
end
|
||||
local info = font.info or {}
|
||||
local names = { info.family or "", info.style or "", info.version or "", info.fullName or "" }
|
||||
local w = Bytes.writer(buffer.len(data) + 512)
|
||||
w:u32le(0) -- EOTSize, patched
|
||||
w:u32le(buffer.len(data))
|
||||
w:u32le(0x00020001)
|
||||
w:u32le(if o.xor then XOR else 0)
|
||||
w:string(panose)
|
||||
w:u8(charset)
|
||||
w:u8(italic)
|
||||
w:u32le(weight)
|
||||
w:u16le(fsType)
|
||||
w:u16le(MAGIC)
|
||||
for _, v in ranges do
|
||||
w:u32le(v)
|
||||
end
|
||||
for _, v in codepages do
|
||||
w:u32le(v)
|
||||
end
|
||||
w:u32le(checksum)
|
||||
w:string(string.rep("\0", 16))
|
||||
for _, s in names do
|
||||
local u = utf16(tostring(s))
|
||||
w:u16le(0)
|
||||
w:u16le(#u)
|
||||
w:string(u)
|
||||
end
|
||||
local root = utf16(o.rootString or "")
|
||||
w:u16le(0)
|
||||
w:u16le(#root)
|
||||
w:string(root)
|
||||
local start = w:tell()
|
||||
w:bytes(data)
|
||||
local out = w:finish()
|
||||
if o.xor then
|
||||
for i = start, start + buffer.len(data) - 1 do
|
||||
buffer.writeu8(out, i, bit32.bxor(buffer.readu8(out, i), 0x50))
|
||||
end
|
||||
end
|
||||
buffer.writeu32(out, 0, buffer.len(out))
|
||||
return out, nil
|
||||
end
|
||||
|
||||
return Eot
|
||||
Reference in new issue
Block a user