__main__.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from fontTools.ttLib import TTFont
  2. from fontTools.feaLib.builder import addOpenTypeFeatures, Builder
  3. from fontTools.feaLib.error import FeatureLibError
  4. from fontTools import configLogger
  5. from fontTools.misc.cliTools import makeOutputFileName
  6. import sys
  7. import argparse
  8. import logging
  9. log = logging.getLogger("fontTools.feaLib")
  10. def main(args=None):
  11. """Add features from a feature file (.fea) into an OTF font"""
  12. parser = argparse.ArgumentParser(
  13. description="Use fontTools to compile OpenType feature files (*.fea)."
  14. )
  15. parser.add_argument(
  16. "input_fea", metavar="FEATURES", help="Path to the feature file"
  17. )
  18. parser.add_argument(
  19. "input_font", metavar="INPUT_FONT", help="Path to the input font"
  20. )
  21. parser.add_argument(
  22. "-o",
  23. "--output",
  24. dest="output_font",
  25. metavar="OUTPUT_FONT",
  26. help="Path to the output font.",
  27. )
  28. parser.add_argument(
  29. "-t",
  30. "--tables",
  31. metavar="TABLE_TAG",
  32. choices=Builder.supportedTables,
  33. nargs="+",
  34. help="Specify the table(s) to be built.",
  35. )
  36. parser.add_argument(
  37. "-d",
  38. "--debug",
  39. action="store_true",
  40. help="Add source-level debugging information to font.",
  41. )
  42. parser.add_argument(
  43. "-v",
  44. "--verbose",
  45. help="Increase the logger verbosity. Multiple -v " "options are allowed.",
  46. action="count",
  47. default=0,
  48. )
  49. parser.add_argument(
  50. "--traceback", help="show traceback for exceptions.", action="store_true"
  51. )
  52. options = parser.parse_args(args)
  53. levels = ["WARNING", "INFO", "DEBUG"]
  54. configLogger(level=levels[min(len(levels) - 1, options.verbose)])
  55. output_font = options.output_font or makeOutputFileName(options.input_font)
  56. log.info("Compiling features to '%s'" % (output_font))
  57. font = TTFont(options.input_font)
  58. try:
  59. addOpenTypeFeatures(
  60. font, options.input_fea, tables=options.tables, debug=options.debug
  61. )
  62. except FeatureLibError as e:
  63. if options.traceback:
  64. raise
  65. log.error(e)
  66. sys.exit(1)
  67. font.save(output_font)
  68. if __name__ == "__main__":
  69. sys.exit(main())