__init__.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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, rm_private=True, **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. if not rm_private:
  38. return
  39. for name_id in range(256, 400):
  40. font['name'].removeNames(name_id)
  41. def disable_oblique_bits(font):
  42. if font['OS/2'].fsSelection & 512 == 512:
  43. font['OS/2'].fsSelection ^= 512
  44. def enable_bold_bits(font):
  45. # Enable Bold bits for Black styles
  46. if "Black" in font_path and "fvar" not in font:
  47. if "Italic" in font_path:
  48. font["OS/2"].fsSelection |= 32
  49. else:
  50. font["OS/2"].fsSelection ^= 64 | 32
  51. font["head"].macStyle |= 1
  52. def update_font_version(font):
  53. version_record = 'Version %s; %d' % (round(font['head'].fontRevision, 3), date.today().year)
  54. font['name'].setName(version_record, 5, 3, 1, 1033)
  55. def update_gasp(font, gasp_ranges):
  56. gasp_tbl = newTable("gasp")
  57. gasp_tbl.gaspRange = gasp_ranges
  58. font['gasp'] = gasp_tbl
  59. def mkdir(path):
  60. if os.path.isdir(path):
  61. shutil.rmtree(path)
  62. os.mkdir(path)
  63. android_and_cros_vert_metrics = {
  64. "ascent": 1900,
  65. "descent": -500,
  66. "lineGap": 0,
  67. "sTypoAscender": 2146,
  68. "sTypoDescender": -555,
  69. "sTypoLineGap": 0,
  70. "usWinAscent": 2146,
  71. "usWinDescent": 555,
  72. "yMin": -555,
  73. "yMax": 2163,
  74. }