__init__.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """Helper functions for hotfixing fonts"""
  2. import os
  3. import shutil
  4. from datetime import datetime as date
  5. from fontTools.ttLib import TTFont, newTable
  6. __all__ = ["update_attribs", "update_names",
  7. "disable_oblique_bits", "update_font_version", "update_gasp", "mkdir",
  8. "update_psname_and_fullname", "android_and_cros_vert_metrics",]
  9. def update_psname_and_fullname(ttfont, include_year=False):
  10. family_name = ttfont['name'].getName(16, 3, 1, 1033) or \
  11. ttfont['name'].getName(1, 3, 1, 1033)
  12. style_name = ttfont['name'].getName(17, 3, 1, 1033) or \
  13. ttfont['name'].getName(2, 3, 1, 1033)
  14. full_name = family_name.toUnicode() + " " + style_name.toUnicode()
  15. if full_name == "Roboto Regular":
  16. full_name = "Roboto"
  17. if full_name == "Roboto Condensed Regular":
  18. full_name = "Roboto Condensed"
  19. if include_year:
  20. year = date.today().year
  21. unique_id = f"Google:{full_name}:{year}"
  22. ttfont['name'].setName(unique_id, 3,3,1,1033)
  23. else:
  24. ttfont['name'].setName(full_name, 3,3,1,1033)
  25. ttfont['name'].setName(full_name, 4,3,1,1033)
  26. def update_attribs(font, **kwargs):
  27. for table in font.keys():
  28. for k in kwargs:
  29. if hasattr(font[table], k):
  30. print(f"Setting {k} to {kwargs[k]}")
  31. setattr(font[table], k, kwargs[k])
  32. def update_names(font, **kwargs):
  33. nametable = font["name"]
  34. for k in kwargs:
  35. print(f"Setting {k} to {kwargs[k]}")
  36. nametable.setName(kwargs[k], *tuple(map(int, k.split(","))))
  37. for name_id in range(256, 400):
  38. font['name'].removeNames(name_id)
  39. def disable_oblique_bits(font):
  40. if font['OS/2'].fsSelection & 512 == 512:
  41. font['OS/2'].fsSelection ^= 512
  42. def enable_bold_bits(font):
  43. # Enable Bold bits for Black styles
  44. if "Black" in font_path and "fvar" not in font:
  45. if "Italic" in font_path:
  46. font["OS/2"].fsSelection |= 32
  47. else:
  48. font["OS/2"].fsSelection ^= 64 | 32
  49. font["head"].macStyle |= 1
  50. def update_font_version(font):
  51. version_record = 'Version %s; %d' % (round(font['head'].fontRevision, 3), date.today().year)
  52. font['name'].setName(version_record, 5, 3, 1, 1033)
  53. def update_gasp(font, gasp_ranges):
  54. gasp_tbl = newTable("gasp")
  55. gasp_tbl.gaspRange = gasp_ranges
  56. font['gasp'] = gasp_tbl
  57. def mkdir(path):
  58. if os.path.isdir(path):
  59. shutil.rmtree(path)
  60. os.mkdir(path)
  61. android_and_cros_vert_metrics = {
  62. "ascent": 1900,
  63. "descent": -500,
  64. "lineGap": 0,
  65. "sTypoAscender": 2146,
  66. "sTypoDescender": -555,
  67. "sTypoLineGap": 0,
  68. "usWinAscent": 2146,
  69. "usWinDescent": 555,
  70. "yMin": -555,
  71. "yMax": 2163,
  72. }