| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- #!/usr/bin/env python3
- """Wrap a TrueType/OpenType file as WOFF 1.0 (stdlib zlib only).
- Used to build the ufont WOFF test fixture: testdata/fonts/sans.woff is
- `tools/mkwoff.py fonts/sans.ttf testdata/fonts/sans.woff`. Tables that do
- not shrink are stored uncompressed, as the spec requires."""
- import struct
- import sys
- import zlib
- def main(src, dst):
- data = open(src, "rb").read()
- flavor, ntables = struct.unpack(">IH", data[:6])
- tables = []
- for i in range(ntables):
- tag, csum, off, length = struct.unpack(">4sIII",
- data[12 + i * 16:28 + i * 16])
- raw = data[off:off + length]
- comp = zlib.compress(raw, 9)
- if len(comp) >= len(raw):
- comp = raw
- tables.append((tag, csum, raw, comp))
- tables.sort(key=lambda t: t[0])
- off = 44 + 20 * ntables
- dirs, body = b"", b""
- total = 12 + 16 * ntables
- for tag, csum, raw, comp in tables:
- dirs += struct.pack(">4sIIII", tag, off + len(body), len(comp),
- len(raw), csum)
- body += comp + b"\0" * (-len(comp) % 4)
- total += len(raw) + (-len(raw) % 4)
- length = off + len(body)
- hdr = struct.pack(">4sIIHHIHHIIIII", b"wOFF", flavor, length, ntables,
- 0, total, 1, 0, 0, 0, 0, 0, 0)
- open(dst, "wb").write(hdr + dirs + body)
- if __name__ == "__main__":
- main(sys.argv[1], sys.argv[2])
|