gen_stat.py 2.3 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(nominalValue=75, rangeMinValue=75, rangeMaxValue=87, name="Condensed"),
  11. dict(nominalValue=100, rangeMinValue=87, rangeMaxValue=100, name="Normal", flags=0x2),
  12. ],
  13. ),
  14. dict(
  15. tag="wght",
  16. name="Weight",
  17. ordering=1,
  18. values=[
  19. dict(nominalValue=100, rangeMinValue=100, rangeMaxValue=150, name="Thin"),
  20. dict(nominalValue=200, rangeMinValue=150, rangeMaxValue=250, name="ExtraLight"),
  21. dict(nominalValue=300, rangeMinValue=250, rangeMaxValue=350, name="Light"),
  22. dict(nominalValue=400, rangeMinValue=350, rangeMaxValue=450, name="Regular", flags=0x2),
  23. dict(nominalValue=500, rangeMinValue=450, rangeMaxValue=650, name="Medium"),
  24. dict(nominalValue=700, rangeMinValue=650, rangeMaxValue=750, name="Bold"),
  25. dict(nominalValue=800, rangeMinValue=750, rangeMaxValue=850, name="ExtraBold"),
  26. dict(nominalValue=900, rangeMinValue=850, rangeMaxValue=900, name="Black"),
  27. dict(value=400, name="Regular", flags=0x2, linkedValue=700), # Regular
  28. ],
  29. ),
  30. dict(
  31. tag="ital",
  32. name="Italic",
  33. ordering=2,
  34. values=[
  35. dict(nominalValue=0, rangeMinValue=0, rangeMaxValue=0, name="Roman", flags=0x2),
  36. dict(nominalValue=1, rangeMinValue=0.1, rangeMaxValue=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()