Snippets/woff2_compress: add --hmtx-trasform option; use argparse
This commit is contained in:
parent
deaeb909a7
commit
ba41877d10
@ -2,39 +2,76 @@
|
|||||||
|
|
||||||
from __future__ import print_function, division, absolute_import
|
from __future__ import print_function, division, absolute_import
|
||||||
from fontTools.misc.py23 import *
|
from fontTools.misc.py23 import *
|
||||||
|
from fontTools import configLogger
|
||||||
from fontTools.ttLib import TTFont
|
from fontTools.ttLib import TTFont
|
||||||
from fontTools.ttx import makeOutputFileName
|
from fontTools.ttx import makeOutputFileName
|
||||||
from fontTools.ttLib.woff2 import WOFF2FlavorData
|
from fontTools.ttLib.woff2 import WOFF2FlavorData
|
||||||
import sys
|
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):
|
def main(args=None):
|
||||||
if args is None:
|
parser = argparse.ArgumentParser()
|
||||||
args = sys.argv[1:]
|
|
||||||
|
|
||||||
disableTransforms = False
|
parser.add_argument("input_file", metavar="INPUT_FILE")
|
||||||
if "--disable-transforms" in args:
|
parser.add_argument("-o", "--output-file", default=None)
|
||||||
disableTransforms = True
|
|
||||||
args.remove("--disable-transforms")
|
|
||||||
|
|
||||||
if len(args) < 1:
|
transform_group = parser.add_argument_group()
|
||||||
print("One argument, the input filename, must be provided.", file=sys.stderr)
|
transform_group.add_argument(
|
||||||
return 1
|
"--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]
|
logging_group = parser.add_mutually_exclusive_group(required=False)
|
||||||
outfilename = makeOutputFileName(filename, outputDir=None, extension='.woff2')
|
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)
|
input_file = options.input_file
|
||||||
font.flavor = "woff2"
|
|
||||||
|
|
||||||
if disableTransforms:
|
if options.output_file:
|
||||||
# an empty tuple signals that we don't want any table to be transformed
|
output_file = options.output_file
|
||||||
font.flavorData = WOFF2FlavorData(transformedTables=())
|
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())
|
sys.exit(main())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user