__main__.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import sys
  2. from fontTools.ttLib import OPTIMIZE_FONT_SPEED, 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", action="append", 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. parser.add_argument(
  63. "--no-recalc-timestamp",
  64. dest="recalcTimestamp",
  65. action="store_false",
  66. help="Keep the original font 'modified' timestamp.",
  67. )
  68. parser.add_argument(
  69. "-b",
  70. dest="recalcBBoxes",
  71. action="store_false",
  72. help="Don't recalc glyph bounding boxes: use the values in the original font.",
  73. )
  74. parser.add_argument(
  75. "--optimize-font-speed",
  76. action="store_true",
  77. help=(
  78. "Enable optimizations that prioritize speed over file size. This "
  79. "mainly affects how glyf table and gvar / VARC tables are compiled."
  80. ),
  81. )
  82. options = parser.parse_args(args)
  83. fontNumber = int(options.y) if options.y is not None else None
  84. outFile = options.output
  85. lazy = options.lazy
  86. flavor = options.flavor
  87. tables = options.table
  88. recalcBBoxes = options.recalcBBoxes
  89. recalcTimestamp = options.recalcTimestamp
  90. optimizeFontSpeed = options.optimize_font_speed
  91. fonts = []
  92. for f in options.font:
  93. try:
  94. font = TTFont(
  95. f,
  96. recalcBBoxes=recalcBBoxes,
  97. recalcTimestamp=recalcTimestamp,
  98. fontNumber=fontNumber,
  99. lazy=lazy,
  100. )
  101. if optimizeFontSpeed:
  102. font.cfg[OPTIMIZE_FONT_SPEED] = optimizeFontSpeed
  103. fonts.append(font)
  104. except TTLibFileIsCollectionError:
  105. collection = TTCollection(f, lazy=lazy)
  106. fonts.extend(collection.fonts)
  107. if tables is None:
  108. if lazy is False:
  109. tables = ["*"]
  110. elif optimizeFontSpeed:
  111. tables = {"glyf", "gvar", "VARC"}.intersection(font.keys())
  112. else:
  113. tables = []
  114. for font in fonts:
  115. if "GlyphOrder" in tables:
  116. font.getGlyphOrder()
  117. for table in tables if "*" not in tables else font.keys():
  118. font[table] # Decompiles
  119. if outFile is not None:
  120. if len(fonts) == 1:
  121. fonts[0].flavor = flavor
  122. fonts[0].save(outFile)
  123. else:
  124. if flavor is not None:
  125. raise TTLibError("Cannot set flavor for collections.")
  126. collection = TTCollection()
  127. collection.fonts = fonts
  128. collection.save(outFile)
  129. if __name__ == "__main__":
  130. sys.exit(main())