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