gen_stat.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from fontTools.otlLib.builder import buildStatTable, _addName
  2. from fontTools.ttLib import TTFont
  3. import sys
  4. AXES = [
  5. dict(
  6. tag="wdth",
  7. name="Width",
  8. ordering=0,
  9. values=[
  10. dict(value=75, name="Condensed"),
  11. dict(value=100, name="Normal", flags=0x2),
  12. ],
  13. ),
  14. dict(
  15. tag="wght",
  16. name="Weight",
  17. ordering=1,
  18. values=[
  19. dict(value=100, name="Thin"),
  20. dict(value=200, name="ExtraLight"),
  21. dict(value=300, name="Light"),
  22. dict(value=400, name="Regular", flags=0x2, linkedValue=700),
  23. dict(value=500, name="Medium"),
  24. dict(value=600, name="SemiBold"),
  25. dict(value=700, name="Bold"),
  26. dict(value=800, name="ExtraBold"),
  27. dict(value=900, name="Black"),
  28. ],
  29. ),
  30. dict(
  31. tag="ital",
  32. name="Italic",
  33. ordering=2,
  34. values=[
  35. dict(value=0, name="Roman", flags=0x2),
  36. dict(value=1, name="Italic"),
  37. ],
  38. ),
  39. ]
  40. def update_fvar(ttfont):
  41. fvar = ttfont['fvar']
  42. nametable = ttfont['name']
  43. family_name = nametable.getName(16, 3, 1, 1033) or nametable.getName(1, 3, 1, 1033)
  44. family_name = family_name.toUnicode().replace(" ", "")
  45. nametable.setName(family_name, 25, 3, 1, 1033)
  46. for instance in fvar.instances:
  47. instance_style = nametable.getName(instance.subfamilyNameID, 3, 1, 1033).toUnicode()
  48. ps_name = f"{family_name}-{instance_style.replace(' ', '')}"
  49. instance.postscriptNameID = _addName(nametable, ps_name, 256)
  50. def main():
  51. filepath = sys.argv[1]
  52. tt = TTFont(filepath)
  53. buildStatTable(tt, AXES)
  54. update_fvar(tt)
  55. tt.save(filepath)
  56. print(f"Added STAT table to {filepath}")
  57. if __name__ == "__main__":
  58. main()