Snippets/woff2_compress: add --hmtx-trasform option; use argparse

This commit is contained in:
Cosimo Lupo 2019-06-12 19:24:35 +01:00
parent deaeb909a7
commit ba41877d10
No known key found for this signature in database
GPG Key ID: 20D4A261E4A0E642

View File

@ -2,39 +2,76 @@
from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import *
from fontTools import configLogger
from fontTools.ttLib import TTFont
from fontTools.ttx import makeOutputFileName
from fontTools.ttLib.woff2 import WOFF2FlavorData
import sys
import logging
import argparse
def woff2_compress(input_file, output_file, transform_tables=None):
logging.info("Processing %s => %s" % (input_file, output_file))
font = TTFont(input_file, recalcBBoxes=False, recalcTimestamp=False)
font.flavor = "woff2"
if transform_tables is not None:
font.flavorData = WOFF2FlavorData(transformedTables=transform_tables)
font.save(output_file, reorderTables=False)
def main(args=None):
if args is None:
args = sys.argv[1:]
parser = argparse.ArgumentParser()
disableTransforms = False
if "--disable-transforms" in args:
disableTransforms = True
args.remove("--disable-transforms")
parser.add_argument("input_file", metavar="INPUT_FILE")
parser.add_argument("-o", "--output-file", default=None)
if len(args) < 1:
print("One argument, the input filename, must be provided.", file=sys.stderr)
return 1
transform_group = parser.add_argument_group()
transform_group.add_argument(
"--no-glyf-transform",
action="store_true",
help="Do not transform glyf (and loca) tables",
)
transform_group.add_argument(
"--hmtx-transform",
action="store_true",
help="Enable optional transformation for 'hmtx' table",
)
filename = args[0]
outfilename = makeOutputFileName(filename, outputDir=None, extension='.woff2')
logging_group = parser.add_mutually_exclusive_group(required=False)
logging_group.add_argument(
"-v", "--verbose", action="store_true", help="Run more verbosely."
)
logging_group.add_argument(
"-q", "--quiet", action="store_true", help="Turn verbosity off."
)
options = parser.parse_args(args)
print("Processing %s => %s" % (filename, outfilename))
configLogger(
logger=logging.getLogger(),
level=("DEBUG" if options.verbose else "ERROR" if options.quiet else "INFO"),
)
font = TTFont(filename, recalcBBoxes=False, recalcTimestamp=False)
font.flavor = "woff2"
input_file = options.input_file
if disableTransforms:
# an empty tuple signals that we don't want any table to be transformed
font.flavorData = WOFF2FlavorData(transformedTables=())
if options.output_file:
output_file = options.output_file
else:
output_file = makeOutputFileName(input_file, outputDir=None, extension=".woff2")
font.save(outfilename, reorderTables=False)
if options.no_glyf_transform:
transform_tables = set()
else:
transform_tables = {"glyf", "loca"}
if options.hmtx_transform:
transform_tables.add("hmtx")
woff2_compress(input_file, output_file, transform_tables)
if __name__ == '__main__':
if __name__ == "__main__":
sys.exit(main())