instantiate_condensed.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import sys
  2. import shutil
  3. import os
  4. from fontTools.ttLib import TTFont
  5. from fontTools.varLib.instancer import instantiateVariableFont
  6. from scripts import (
  7. update_names,
  8. update_attribs,
  9. mkdir
  10. )
  11. roman_instance = {
  12. "attribs": {"usWidthClass": 5},
  13. "axes": {"wdth": 75},
  14. "filename": "RobotoCondensed[wght].ttf",
  15. "names": {
  16. "1,3,1,1033": "Roboto Condensed",
  17. "2,3,1,1033": "Regular",
  18. "3,3,1,1033": "Google:Roboto Condensed Regular:2016",
  19. "4,3,1,1033": "Roboto Condensed Regular",
  20. "6,3,1,1033": "RobotoCondensed-Regular",
  21. "25,3,1,1033": "RobotoCondensed"
  22. },
  23. }
  24. italic_instance = {
  25. "attribs": {"usWidthClass": 5, "italicAngle": -12, "caretSlopeRise": 2048, "caretSlopeRun": 435},
  26. "axes": {"wdth": 75},
  27. "filename": "RobotoCondensed-Italic[wght].ttf",
  28. "names": {
  29. "1,3,1,1033": "Roboto Condensed",
  30. "2,3,1,1033": "Italic",
  31. "3,3,1,1033": "Google:Roboto Condensed Italic:2016",
  32. "4,3,1,1033": "Roboto Condensed Italic",
  33. "6,3,1,1033": "RobotoCondensed-Italic",
  34. "25,3,1,1033": "RobotoCondensed"
  35. },
  36. }
  37. def update_fvar_instances(ttfont):
  38. name = ttfont["name"]
  39. instances = ttfont['fvar'].instances
  40. for instance in instances:
  41. subfamily_id = instance.subfamilyNameID
  42. post_id = instance.postscriptNameID
  43. for record in name.names:
  44. if record.nameID in [subfamily_id]:
  45. current_name = record.toUnicode()
  46. new_name = current_name.replace("Condensed", "").strip()
  47. name.setName(new_name, record.nameID, record.platformID, record.platEncID, record.langID)
  48. if record.nameID in [post_id]:
  49. current_name = record.toUnicode()
  50. new_name = current_name.replace("-Condensed", "Condensed-").strip()
  51. name.setName(new_name, record.nameID, record.platformID, record.platEncID, record.langID)
  52. string = record.toUnicode()
  53. name.setName(string.replace("Roboto-Condensed", "RobotoCondensed-"), record.nameID, record.platformID, record.platEncID, record.langID)
  54. def update_stat(ttfont):
  55. # Elide Condensed STAT AvisValue
  56. stat = ttfont["STAT"].table
  57. name_table = ttfont["name"]
  58. axis_values = [a for a in stat.AxisValueArray.AxisValue]
  59. for av in axis_values:
  60. name_id = av.ValueNameID
  61. name = name_table.getName(name_id, 3, 1, 0x409).toUnicode()
  62. if name == "Condensed":
  63. av.Flags = 2
  64. vf_roman = TTFont(sys.argv[1])
  65. vf_italic = TTFont(sys.argv[2])
  66. out_dir = mkdir(sys.argv[3])
  67. for inst, vf in zip([roman_instance, italic_instance], [vf_roman, vf_italic]):
  68. print(f"Making {inst['filename']}")
  69. instance = instantiateVariableFont(vf, inst["axes"])
  70. update_attribs(instance, **inst["attribs"])
  71. update_names(instance, rm_private=False, **inst["names"])
  72. update_fvar_instances(instance)
  73. update_stat(instance)
  74. out_path = os.path.join(sys.argv[3], inst["filename"])
  75. instance.save(out_path)