Home | History | Annotate | Download | only in Snippets
      1 #!/usr/bin/env python
      2 
      3 from __future__ import print_function, division, absolute_import
      4 from fontTools.misc.py23 import *
      5 from fontTools.ttLib import TTFont
      6 from fontTools.ttx import makeOutputFileName
      7 import sys
      8 import os
      9 
     10 
     11 def make_output_name(filename):
     12     with open(filename, "rb") as f:
     13         f.seek(4)
     14         sfntVersion = f.read(4)
     15     assert len(sfntVersion) == 4, "not enough data"
     16     ext = '.ttf' if sfntVersion == b"\x00\x01\x00\x00" else ".otf"
     17     outfilename = makeOutputFileName(filename, outputDir=None, extension=ext)
     18     return outfilename
     19 
     20 
     21 def main(args=None):
     22     if args is None:
     23         args = sys.argv[1:]
     24     if len(args) < 1:
     25         print("One argument, the input filename, must be provided.", file=sys.stderr)
     26         return 1
     27 
     28     filename = args[0]
     29     outfilename = make_output_name(filename)
     30 
     31     print("Processing %s => %s" % (filename, outfilename))
     32 
     33     font = TTFont(filename, recalcBBoxes=False, recalcTimestamp=False)
     34     font.flavor = None
     35     font.save(outfilename, reorderTables=True)
     36 
     37 
     38 if __name__ == '__main__':
     39     sys.exit(main())
     40