1import sys 2import os 3from fontTools.ttx import makeOutputFileName 4from fontTools.ttLib import TTFont 5 6 7def main(args=None): 8 if args is None: 9 args = sys.argv[1:] 10 11 if len(args) < 2: 12 print("usage: merge_woff_metadata.py METADATA.xml " 13 "INPUT.woff [OUTPUT.woff]", file=sys.stderr) 14 return 1 15 16 metadata_file = args[0] 17 with open(metadata_file, 'rb') as f: 18 metadata = f.read() 19 20 infile = args[1] 21 if len(args) > 2: 22 outfile = args[2] 23 else: 24 filename, ext = os.path.splitext(infile) 25 outfile = makeOutputFileName(filename, None, ext) 26 27 font = TTFont(infile) 28 29 if font.flavor not in ("woff", "woff2"): 30 print("Input file is not a WOFF or WOFF2 font", file=sys.stderr) 31 return 1 32 33 data = font.flavorData 34 35 # this sets the new WOFF metadata 36 data.metaData = metadata 37 38 font.save(outfile) 39 40 41if __name__ == "__main__": 42 sys.exit(main()) 43