__main__.py 3.3 KB

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