From 0782513d4de92a4e19e4b71d1d18e8be2f31e997 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Mon, 4 Jul 2016 15:51:51 +0100 Subject: [PATCH] Snippets: add 'merge_woff_metadata.py' and 'dump_woff_metatada.py' scripts 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 --- Snippets/dump_woff_metadata.py | 33 ++++++++++++++++++++++++++ Snippets/merge_woff_metadata.py | 42 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 Snippets/dump_woff_metadata.py create mode 100644 Snippets/merge_woff_metadata.py diff --git a/Snippets/dump_woff_metadata.py b/Snippets/dump_woff_metadata.py new file mode 100644 index 000000000..0023a1067 --- /dev/null +++ b/Snippets/dump_woff_metadata.py @@ -0,0 +1,33 @@ +from __future__ import print_function +import sys +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) < 1: + print("usage: dump_woff_metadata.py " + "INPUT.woff [OUTPUT.xml]", file=sys.stderr) + return 1 + + infile = args[0] + if len(args) > 1: + outfile = args[1] + else: + outfile = makeOutputFileName(infile, None, ".xml") + + font = TTFont(infile) + + if not font.flavorData or not font.flavorData.metaData: + print("No WOFF metadata") + return 1 + + with open(outfile, "wb") as f: + f.write(font.flavorData.metaData) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/Snippets/merge_woff_metadata.py b/Snippets/merge_woff_metadata.py new file mode 100644 index 000000000..0b79eac19 --- /dev/null +++ b/Snippets/merge_woff_metadata.py @@ -0,0 +1,42 @@ +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())