versionator.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # This script gathers Noto versions from third-party sources.
  2. # It's designed to run on Simon's computer, and probably not
  3. # anywhere else.
  4. import json
  5. import os
  6. import platform
  7. import plistlib
  8. import re
  9. from collections import defaultdict
  10. from pathlib import Path
  11. import requests
  12. from fontTools.ttLib import TTFont
  13. from gfpipeline import \
  14. FontFamilies # This is a private module, you won't find it
  15. from tqdm import tqdm
  16. MACOS_PATH = Path("/System/Library/Fonts/")
  17. MAC_VERSION = "macOS " + re.sub(r".\d+$", "", platform.mac_ver()[0])
  18. IOS_PATH_1 = Path(
  19. "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Fonts/UnicodeSupport/"
  20. )
  21. IOS_PATH_2 = Path(IOS_PATH_1.parent) / "Core"
  22. ANDROID_PATH = Path("~/Downloads/android_fonts-master/api_level/").expanduser()
  23. LATEST_ANDROID_PATH = sorted(list(ANDROID_PATH.glob("??")))[-1]
  24. IOS_VERSION_PATH = "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Info.plist"
  25. IOS_VERSION = plistlib.load(open(IOS_VERSION_PATH, "rb"))["CFBundleExecutable"]
  26. ANDROID_API_VERSION_MAP = {
  27. "31": "Android 12",
  28. "32": "Android 12L",
  29. "33": "Android 13",
  30. }
  31. ANDROID_VERSION = ANDROID_API_VERSION_MAP[LATEST_ANDROID_PATH.stem]
  32. # These will need manual updates
  33. FEDORA_VERSION = "Fedora 38"
  34. FEDORA_SRC = "https://kojipkgs.fedoraproject.org//packages/google-noto-fonts/20201206^1.git0c78c8329/9.fc38/src/google-noto-fonts-20201206^1.git0c78c8329-9.fc38.src.rpm"
  35. FEDORA_TAR = "noto-fonts-0c78c8329.tar.xz"
  36. notoversions = defaultdict(dict)
  37. def tidy_name(name):
  38. if "Old Italic" not in name:
  39. name = re.sub(r" Italic", "", name)
  40. if "Hmong Nyiakeng" in name:
  41. name = "Noto Serif Nyiakeng Puachue Hmong"
  42. name = re.sub(r"( (Regular|Bold|Black))+$", "", name)
  43. name = re.sub(r" PhagsPa$", " Phags Pa", name)
  44. return name
  45. def tidy_version(name5version):
  46. version = re.sub(";.*", "", name5version)
  47. version = re.sub("Version ", "", version)
  48. return version
  49. def register_version(file, system):
  50. global notoversions
  51. ttfont = TTFont(file, fontNumber=0)
  52. name = tidy_name(ttfont["name"].getDebugName(4))
  53. if "Emoji" in name:
  54. return
  55. version = "%1.3f" % ttfont["head"].fontRevision
  56. notoversions[name][system] = version
  57. if __name__ == "__main__":
  58. for file in (IOS_PATH_1).glob("Noto*.tt?"):
  59. register_version(file, IOS_VERSION)
  60. for file in (IOS_PATH_2).glob("Noto*.tt?"):
  61. register_version(file, IOS_VERSION)
  62. assert "Noto Sans Armenian" in notoversions
  63. for file in MACOS_PATH.glob("Noto*.tt?"):
  64. register_version(file, MAC_VERSION)
  65. for file in (MACOS_PATH / "Supplemental/").glob("Noto*.tt?"):
  66. register_version(file, MAC_VERSION)
  67. for file in (LATEST_ANDROID_PATH).glob("Noto*.tt?"):
  68. register_version(file, ANDROID_VERSION)
  69. # Fedora 38
  70. if not os.path.exists(FEDORA_TAR):
  71. if not os.path.exists("noto-fedora.src.rpm"):
  72. response = requests.get(FEDORA_SRC, stream=True)
  73. total_size_in_bytes = int(response.headers.get("content-length", 0))
  74. print("Downloading Fedora 38 sources")
  75. progress_bar = tqdm(total=total_size_in_bytes, unit="iB", unit_scale=True)
  76. with open("noto-fedora.src.rpm", "wb") as file:
  77. for data in response.iter_content(1024):
  78. progress_bar.update(len(data))
  79. file.write(data)
  80. progress_bar.close()
  81. print("Opening RPM")
  82. os.system("rpm2cpio noto-fedora.src.rpm | cpio -id noto-fonts-0c78c8329.tar.xz")
  83. if not os.path.exists("fedora-noto"):
  84. os.makedirs("fedora-noto")
  85. os.system(
  86. " xz -dc " + FEDORA_TAR + " | tar -C fedora-noto -xf - '*Regular.ttf'"
  87. )
  88. for file in Path("fedora-noto").glob("**/Noto*.tt?"):
  89. register_version(file, "Fedora 38")
  90. # Google Fonts
  91. versions = FontFamilies(list(notoversions.keys()))
  92. for family_dict in versions.data:
  93. notoversions[family_dict["name"]]["Google Fonts"] = tidy_version(
  94. family_dict["production"]["version_nameid5"]
  95. )
  96. json.dump(
  97. notoversions, open("docs/versions.json", "w"), indent=True, sort_keys=True
  98. )