fonttools/Snippets/woff2_decompress.py
Cosimo Lupo b7bb391033
don't use sys.exit(...) inside main(), but only under if __name__ == "__main__"
The convention is that sys.exit(...) is called only if a module is run as a script,
and that main() entry points use return statements to report exit codes: 0 (or None)
for successful execution, or any non-zero integer for errors.

E.g. see the console scripts generated when installing with pip.
2017-01-11 12:10:58 +00:00

40 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python
from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import *
from fontTools.ttLib import TTFont
from fontTools.ttx import makeOutputFileName
import sys
import os
def make_output_name(filename):
with open(filename, "rb") as f:
f.seek(4)
sfntVersion = f.read(4)
assert len(sfntVersion) == 4, "not enough data"
ext = '.ttf' if sfntVersion == b"\x00\x01\x00\x00" else ".otf"
outfilename = makeOutputFileName(filename, outputDir=None, extension=ext)
return outfilename
def main(args=None):
if args is None:
args = sys.argv[1:]
if len(args) < 1:
print("One argument, the input filename, must be provided.", file=sys.stderr)
return 1
filename = args[0]
outfilename = make_output_name(filename)
print("Processing %s => %s" % (filename, outfilename))
font = TTFont(filename, recalcBBoxes=False, recalcTimestamp=False)
font.flavor = None
font.save(outfilename, reorderTables=True)
if __name__ == '__main__':
sys.exit(main())