This is just to exemplify how one could use the `TTFont.flavorData` attribute to get/set the WOFF metadata. See discussion at https://github.com/behdad/fonttools/issues/630
43 lines
928 B
Python
43 lines
928 B
Python
from __future__ import print_function
|
|
import sys
|
|
import os
|
|
from fontTools.ttx import makeOutputFileName
|
|
from fontTools.ttLib import TTFont
|
|
|
|
|
|
def main(args=None):
|
|
if args is None:
|
|
args = sys.argv[1:]
|
|
|
|
if len(args) < 2:
|
|
print("usage: merge_woff_metadata.py METADATA.xml "
|
|
"INPUT.woff [OUTPUT.woff]", file=sys.stderr)
|
|
return 1
|
|
|
|
metadata_file = args[0]
|
|
with open(metadata_file, 'rb') as f:
|
|
metadata = f.read()
|
|
|
|
infile = args[1]
|
|
if len(args) > 2:
|
|
outfile = args[2]
|
|
else:
|
|
filename, ext = os.path.splitext(infile)
|
|
outfile = makeOutputFileName(filename, None, ext)
|
|
|
|
font = TTFont(infile)
|
|
|
|
if font.flavorData is None:
|
|
font.flavorData = WOFFFlavorData()
|
|
|
|
data = font.flavorData
|
|
|
|
# this sets the new WOFF metadata
|
|
data.metaData = metadata
|
|
|
|
font.save(outfile)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|