| 1 | #!/usr/bin/env python3 |
| 2 | """Copy Docs' font vocabulary and TTFs into the Apple apps' resources. |
| 3 | |
| 4 | internal/fonts/fonts.json is the one list of families, and |
| 5 | internal/fonts/files/ holds the files scripts/vendor-fonts.py cut for it. |
| 6 | The server, the editor and the Rust renderer read them there. Swift |
| 7 | Package Manager cannot reach outside native/apple, so the apps carry a |
| 8 | copy at native/apple/Sources/DocsKit/Fonts: the manifest, each family's |
| 9 | TTFs and its licence, in the same directories. Git stores identical |
| 10 | files once, so the copy adds nothing to the repository. |
| 11 | |
| 12 | native/scripts/sync-fonts.py # write the copy |
| 13 | native/scripts/sync-fonts.py --check # fail if it has drifted |
| 14 | |
| 15 | check-native.sh runs --check, on Linux too, so CI notices a font change |
| 16 | that was not copied. |
| 17 | """ |
| 18 | |
| 19 | import json |
| 20 | import pathlib |
| 21 | import shutil |
| 22 | import sys |
| 23 | |
| 24 | ROOT = pathlib.Path(__file__).resolve().parents[2] |
| 25 | SOURCE = ROOT / "internal" / "fonts" |
| 26 | TARGET = ROOT / "native" / "apple" / "Sources" / "DocsKit" / "Fonts" |
| 27 | |
| 28 | |
| 29 | def wanted(): |
| 30 | """Every file the apps carry, as {relative path: source path}.""" |
| 31 | manifest = json.loads((SOURCE / "fonts.json").read_text(encoding="utf-8")) |
| 32 | files = {"fonts.json": SOURCE / "fonts.json"} |
| 33 | for family in manifest["families"]: |
| 34 | directory = family["dir"] |
| 35 | files[f"{directory}/{family['licence']}"] = SOURCE / "files" / directory / family["licence"] |
| 36 | for name in family["styles"].values(): |
| 37 | files[f"{directory}/{name}.ttf"] = SOURCE / "files" / directory / f"{name}.ttf" |
| 38 | return files |
| 39 | |
| 40 | |
| 41 | def present(): |
| 42 | if not TARGET.exists(): |
| 43 | return set() |
| 44 | return {str(p.relative_to(TARGET)) for p in TARGET.rglob("*") if p.is_file()} |
| 45 | |
| 46 | |
| 47 | def main(argv): |
| 48 | check = argv[1:] == ["--check"] |
| 49 | if argv[1:] not in ([], ["--check"]): |
| 50 | print("usage: sync-fonts.py [--check]", file=sys.stderr) |
| 51 | return 2 |
| 52 | files = wanted() |
| 53 | missing = [str(p) for p in files.values() if not p.is_file()] |
| 54 | if missing: |
| 55 | print("sync-fonts: the manifest names files that do not exist:\n " + "\n ".join(missing), file=sys.stderr) |
| 56 | return 1 |
| 57 | stale = sorted(present() - set(files)) |
| 58 | changed = sorted(rel for rel, src in files.items() |
| 59 | if not (TARGET / rel).is_file() or (TARGET / rel).read_bytes() != src.read_bytes()) |
| 60 | if check: |
| 61 | if stale or changed: |
| 62 | print("sync-fonts: native/apple/Sources/DocsKit/Fonts differs from internal/fonts. " |
| 63 | "Run native/scripts/sync-fonts.py.", file=sys.stderr) |
| 64 | for rel in changed: |
| 65 | print(" differs: " + rel, file=sys.stderr) |
| 66 | for rel in stale: |
| 67 | print(" not in the manifest: " + rel, file=sys.stderr) |
| 68 | return 1 |
| 69 | size = sum((TARGET / rel).stat().st_size for rel in files if rel.endswith(".ttf")) |
| 70 | print(f"sync-fonts: {sum(r.endswith('.ttf') for r in files)} faces match internal/fonts ({size / 1e6:.1f} MB).") |
| 71 | return 0 |
| 72 | for rel in stale: |
| 73 | (TARGET / rel).unlink() |
| 74 | for rel in changed: |
| 75 | (TARGET / rel).parent.mkdir(parents=True, exist_ok=True) |
| 76 | shutil.copyfile(files[rel], TARGET / rel) |
| 77 | for directory in sorted((p for p in TARGET.rglob("*") if p.is_dir()), reverse=True): |
| 78 | if not any(directory.iterdir()): |
| 79 | directory.rmdir() |
| 80 | print(f"sync-fonts: wrote {len(changed)}, removed {len(stale)}.") |
| 81 | return 0 |
| 82 | |
| 83 | |
| 84 | if __name__ == "__main__": |
| 85 | sys.exit(main(sys.argv)) |
| 86 | |