You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.5KB

  1. #!/usr/bin/env python3
  2. from __future__ import print_function
  3. import json
  4. import os
  5. import os.path
  6. import subprocess
  7. FONTS = {
  8. ("Exo 2", "700", "italic"): "./dist/fonts/Exo2-BoldItalic.woff2",
  9. ("Exo 2", "700", "normal"): "./dist/fonts/Exo2-Bold.woff2",
  10. ("Exo 2", "400", "normal"): "./dist/fonts/Exo2-Regular.woff2",
  11. ("Open Sans", "400", "normal"): "./dist/fonts/OpenSans.woff2",
  12. ("Open Sans", "400", "italic"): "./dist/fonts/OpenSans-Italic.woff2",
  13. ("Open Sans", "700", "normal"): "./dist/fonts/OpenSans-Bold.woff2",
  14. ("Open Sans", "700", "italic"): "./dist/fonts/OpenSans-BoldItalic.woff2",
  15. ("Jetbrains Mono", "400", "normal"): "./dist/fonts/Jetbrains-Mono.woff2",
  16. }
  17. OUTPUT_DIR = "./dist"
  18. def codepoint(char):
  19. return hex(ord(char))[2:]
  20. def main():
  21. files = [
  22. os.path.abspath(f)
  23. for f in subprocess.check_output(["find", OUTPUT_DIR, "-name", "*.html"])
  24. .decode("utf-8")
  25. .strip()
  26. .split("\n")
  27. ]
  28. print("Finding subsets of %d fonts on %d pages" % (len(FONTS), len(files)))
  29. faces_output = subprocess.check_output(["node", "./script/faces.js"] + files).decode('utf-8')
  30. faces = json.loads(faces_output)
  31. for font_and_chars in faces:
  32. font = font_and_chars["font"]
  33. filename = FONTS.get((font["face"], font["weight"], font["style"]), None)
  34. if filename is None:
  35. # not a problem, we just don't care about this face. Stuff like
  36. # "sans-serif" and "monospace" shows up here.
  37. continue
  38. unicodes = ",".join(codepoint(char) for char in font_and_chars["chars"])
  39. out_filename = filename + ".subset"
  40. subprocess.check_call(
  41. [
  42. "pyftsubset",
  43. filename,
  44. "--unicodes=%s" % unicodes,
  45. "--drop-tables=BASE,JSTF,DSIG,EBDT,EBLC,EBSC,SVG,PCLT,LTSH,Feat,Glat,Gloc,Silf,Sill,CBLC,CBDT,sbix,FFTM",
  46. "--flavor=woff2",
  47. "--output-file=%s" % out_filename,
  48. ]
  49. )
  50. before_size = os.path.getsize(filename)
  51. after_size = os.path.getsize(out_filename)
  52. print(
  53. "Subset %s from %d to %d bytes (%.2f%% of original size, %d glyphs)"
  54. % (
  55. filename,
  56. before_size,
  57. after_size,
  58. (float(after_size) / float(before_size)) * 100,
  59. len(font_and_chars["chars"]),
  60. )
  61. )
  62. os.rename(out_filename, filename)
  63. if __name__ == "__main__":
  64. main()