#!/usr/bin/env python3 """Copy Docs' font vocabulary and TTFs into the Apple apps' resources. internal/fonts/fonts.json is the one list of families, and internal/fonts/files/ holds the files scripts/vendor-fonts.py cut for it. The server, the editor and the Rust renderer read them there. Swift Package Manager cannot reach outside native/apple, so the apps carry a copy at native/apple/Sources/DocsKit/Fonts: the manifest, each family's TTFs and its licence, in the same directories. Git stores identical files once, so the copy adds nothing to the repository. native/scripts/sync-fonts.py # write the copy native/scripts/sync-fonts.py --check # fail if it has drifted check-native.sh runs --check, on Linux too, so CI notices a font change that was not copied. """ import json import pathlib import shutil import sys ROOT = pathlib.Path(__file__).resolve().parents[2] SOURCE = ROOT / "internal" / "fonts" TARGET = ROOT / "native" / "apple" / "Sources" / "DocsKit" / "Fonts" def wanted(): """Every file the apps carry, as {relative path: source path}.""" manifest = json.loads((SOURCE / "fonts.json").read_text(encoding="utf-8")) files = {"fonts.json": SOURCE / "fonts.json"} for family in manifest["families"]: directory = family["dir"] files[f"{directory}/{family['licence']}"] = SOURCE / "files" / directory / family["licence"] for name in family["styles"].values(): files[f"{directory}/{name}.ttf"] = SOURCE / "files" / directory / f"{name}.ttf" return files def present(): if not TARGET.exists(): return set() return {str(p.relative_to(TARGET)) for p in TARGET.rglob("*") if p.is_file()} def main(argv): check = argv[1:] == ["--check"] if argv[1:] not in ([], ["--check"]): print("usage: sync-fonts.py [--check]", file=sys.stderr) return 2 files = wanted() missing = [str(p) for p in files.values() if not p.is_file()] if missing: print("sync-fonts: the manifest names files that do not exist:\n " + "\n ".join(missing), file=sys.stderr) return 1 stale = sorted(present() - set(files)) changed = sorted(rel for rel, src in files.items() if not (TARGET / rel).is_file() or (TARGET / rel).read_bytes() != src.read_bytes()) if check: if stale or changed: print("sync-fonts: native/apple/Sources/DocsKit/Fonts differs from internal/fonts. " "Run native/scripts/sync-fonts.py.", file=sys.stderr) for rel in changed: print(" differs: " + rel, file=sys.stderr) for rel in stale: print(" not in the manifest: " + rel, file=sys.stderr) return 1 size = sum((TARGET / rel).stat().st_size for rel in files if rel.endswith(".ttf")) print(f"sync-fonts: {sum(r.endswith('.ttf') for r in files)} faces match internal/fonts ({size / 1e6:.1f} MB).") return 0 for rel in stale: (TARGET / rel).unlink() for rel in changed: (TARGET / rel).parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(files[rel], TARGET / rel) for directory in sorted((p for p in TARGET.rglob("*") if p.is_dir()), reverse=True): if not any(directory.iterdir()): directory.rmdir() print(f"sync-fonts: wrote {len(changed)}, removed {len(stale)}.") return 0 if __name__ == "__main__": sys.exit(main(sys.argv))