1import sys 2from fontTools.ttLib import TTLibError, TTLibFileIsCollectionError 3from fontTools.ttLib.ttFont import * 4from fontTools.ttLib.ttCollection import TTCollection 5 6 7def main(args=None): 8 """Open/save fonts with TTFont() or TTCollection() 9 10 ./fonttools ttLib [-oFILE] [-yNUMBER] files... 11 12 If multiple files are given on the command-line, 13 they are each opened (as a font or collection), 14 and added to the font list. 15 16 If -o (output-file) argument is given, the font 17 list is then saved to the output file, either as 18 a single font, if there is only one font, or as 19 a collection otherwise. 20 21 If -y (font-number) argument is given, only the 22 specified font from collections is opened. 23 24 The above allow extracting a single font from a 25 collection, or combining multiple fonts into a 26 collection. 27 28 If --lazy or --no-lazy are give, those are passed 29 to the TTFont() or TTCollection() constructors. 30 """ 31 from fontTools import configLogger 32 33 if args is None: 34 args = sys.argv[1:] 35 36 import argparse 37 38 parser = argparse.ArgumentParser( 39 "fonttools ttLib", 40 description="Open/save fonts with TTFont() or TTCollection()", 41 epilog=""" 42 If multiple files are given on the command-line, 43 they are each opened (as a font or collection), 44 and added to the font list. 45 46 The above, when combined with -o / --output, 47 allows for extracting a single font from a 48 collection, or combining multiple fonts into a 49 collection. 50 """, 51 ) 52 parser.add_argument("font", metavar="font", nargs="*", help="Font file.") 53 parser.add_argument( 54 "-t", "--table", metavar="table", nargs="*", help="Tables to decompile." 55 ) 56 parser.add_argument( 57 "-o", "--output", metavar="FILE", default=None, help="Output file." 58 ) 59 parser.add_argument( 60 "-y", metavar="NUMBER", default=-1, help="Font number to load from collections." 61 ) 62 parser.add_argument( 63 "--lazy", action="store_true", default=None, help="Load fonts lazily." 64 ) 65 parser.add_argument( 66 "--no-lazy", dest="lazy", action="store_false", help="Load fonts immediately." 67 ) 68 parser.add_argument( 69 "--flavor", 70 dest="flavor", 71 default=None, 72 help="Flavor of output font. 'woff' or 'woff2'.", 73 ) 74 options = parser.parse_args(args) 75 76 fontNumber = int(options.y) if options.y is not None else None 77 outFile = options.output 78 lazy = options.lazy 79 flavor = options.flavor 80 tables = options.table if options.table is not None else [] 81 82 fonts = [] 83 for f in options.font: 84 try: 85 font = TTFont(f, fontNumber=fontNumber, lazy=lazy) 86 fonts.append(font) 87 except TTLibFileIsCollectionError: 88 collection = TTCollection(f, lazy=lazy) 89 fonts.extend(collection.fonts) 90 91 for font in fonts: 92 for table in tables if "*" not in tables else font.keys(): 93 font[table] # Decompiles 94 95 if outFile is not None: 96 if len(fonts) == 1: 97 fonts[0].flavor = flavor 98 fonts[0].save(outFile) 99 else: 100 if flavor is not None: 101 raise TTLibError("Cannot set flavor for collections.") 102 collection = TTCollection() 103 collection.fonts = fonts 104 collection.save(outFile) 105 106 107if __name__ == "__main__": 108 sys.exit(main()) 109