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