#!/usr/bin/env python3 """Filter the bookshelf font pool (stdin: bookshelf-listallfonts output). Drops fonts whose file is larger than MAX_MB or that are colour/emoji fonts. luaotfload parses a font into Lua tables the first time it is used; for the 20-190 MB CJK/emoji faces that takes minutes and gigabytes, and a random draw of one of them for a spine (2026-09-24: the CJK title on the shelf, tried against up to 100 random fonts) hung the spine build until the process was killed. Small fonts load in milliseconds. """ import os, re, sys MAX_MB = 12 out = kept = dropped = 0 for line in sys.stdin: m = re.search(r'\[([^\]]+)\]', line) path = m.group(1) if m else '' low = (line + path).lower() try: size = os.path.getsize(path) if path else 0 except OSError: size = 0 if 'emoji' in low or size > MAX_MB * 1024 * 1024: dropped += 1 continue sys.stdout.write(line) kept += 1 print(f'bookshelf-filterfonts: kept {kept}, dropped {dropped} (> {MAX_MB} MB or emoji)', file=sys.stderr)