-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·90 lines (67 loc) · 3.03 KB
/
build.py
File metadata and controls
executable file
·90 lines (67 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
"""Build Kobo-fixed (KF) fonts locally into ./out."""
from __future__ import annotations
import argparse
import shutil
import subprocess
import sys
import tempfile
import urllib.request
from pathlib import Path
KOBOFIX_URL = "https://raw.githubusercontent.com/nicoverbruggen/kobo-font-fix/main/kobofix.py"
def download_kobofix(target_path: Path) -> None:
target_path.parent.mkdir(parents=True, exist_ok=True)
urllib.request.urlretrieve(KOBOFIX_URL, target_path)
def process_collection(kobofix_path: Path, source_dir: Path, output_dir: Path) -> int:
output_dir.mkdir(parents=True, exist_ok=True)
for old_file in output_dir.glob("*.ttf"):
old_file.unlink()
source_fonts = sorted(source_dir.glob("*.ttf"))
if not source_fonts:
raise RuntimeError(f"No .ttf files found in {source_dir}")
with tempfile.TemporaryDirectory(prefix="kobofix-") as temp_dir:
temp_path = Path(temp_dir)
for font in source_fonts:
shutil.copy2(font, temp_path / font.name)
cmd = [sys.executable, str(kobofix_path), "--preset", "kf"] + [font.name for font in source_fonts]
subprocess.run(cmd, cwd=temp_path, check=True)
generated_fonts = sorted(temp_path.glob("KF_*.ttf"))
if not generated_fonts:
raise RuntimeError(f"kobofix did not generate any KF_*.ttf files for {source_dir}")
for generated_font in generated_fonts:
shutil.move(str(generated_font), output_dir / generated_font.name)
return len(generated_fonts)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Build KF fonts from ./fonts into ./out.")
parser.add_argument(
"--out-dir",
default="out",
type=Path,
help="Directory where generated KF fonts are written (default: ./out).",
)
parser.add_argument(
"--kobofix",
type=Path,
help="Path to a local kobofix.py. If omitted, it will be downloaded automatically.",
)
return parser.parse_args()
def main() -> int:
args = parse_args()
repo_root = Path(__file__).resolve().parent
out_dir = (repo_root / args.out_dir).resolve()
with tempfile.TemporaryDirectory(prefix="kobofix-script-") as temp_dir:
temp_path = Path(temp_dir)
kobofix_path = (args.kobofix.resolve() if args.kobofix else temp_path / "kobofix.py")
if args.kobofix:
if not kobofix_path.is_file():
raise FileNotFoundError(f"kobofix.py not found: {kobofix_path}")
else:
print(f"Downloading kobofix.py from {KOBOFIX_URL}")
download_kobofix(kobofix_path)
core_count = process_collection(kobofix_path, repo_root / "fonts/core", out_dir / "core")
extra_count = process_collection(kobofix_path, repo_root / "fonts/extra", out_dir / "extra")
print(f"Generated {core_count} KF fonts in {out_dir / 'core'}")
print(f"Generated {extra_count} KF fonts in {out_dir / 'extra'}")
return 0
if __name__ == "__main__":
raise SystemExit(main())