avar.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from fontTools.varLib import _add_avar, load_designspace
  2. from fontTools.misc.cliTools import makeOutputFileName
  3. import logging
  4. log = logging.getLogger("fontTools.varLib.avar")
  5. def main(args=None):
  6. """Add `avar` table from designspace file to variable font."""
  7. if args is None:
  8. import sys
  9. args = sys.argv[1:]
  10. from fontTools import configLogger
  11. from fontTools.ttLib import TTFont
  12. from fontTools.designspaceLib import DesignSpaceDocument
  13. import argparse
  14. parser = argparse.ArgumentParser(
  15. "fonttools varLib.avar",
  16. description="Add `avar` table from designspace file to variable font.",
  17. )
  18. parser.add_argument("font", metavar="varfont.ttf", help="Variable-font file.")
  19. parser.add_argument(
  20. "designspace", metavar="family.designspace", help="Designspace file."
  21. )
  22. parser.add_argument(
  23. "-o",
  24. "--output-file",
  25. type=str,
  26. help="Output font file name.",
  27. )
  28. parser.add_argument(
  29. "-v", "--verbose", action="store_true", help="Run more verbosely."
  30. )
  31. options = parser.parse_args(args)
  32. configLogger(level=("INFO" if options.verbose else "WARNING"))
  33. font = TTFont(options.font)
  34. if not "fvar" in font:
  35. log.error("Not a variable font.")
  36. return 1
  37. axisTags = [a.axisTag for a in font["fvar"].axes]
  38. ds = load_designspace(options.designspace)
  39. if "avar" in font:
  40. log.warning("avar table already present, overwriting.")
  41. del font["avar"]
  42. _add_avar(font, ds.axes, ds.axisMappings, axisTags)
  43. if options.output_file is None:
  44. outfile = makeOutputFileName(options.font, overWrite=True, suffix=".avar")
  45. else:
  46. outfile = options.output_file
  47. if outfile:
  48. log.info("Saving %s", outfile)
  49. font.save(outfile)
  50. if __name__ == "__main__":
  51. import sys
  52. sys.exit(main())