import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
OUT = ROOT / "src" / "web" / "page" / "icons.woff2"
def is_pua(cp: int) -> bool:
return 0xE000 <= cp <= 0xF8FF or cp >= 0xF0000
def used_codepoints(src: Path = ROOT / "src") -> set[int]:
cps: set[int] = set()
for path in src.rglob("*.rs"):
text = path.read_text(errors="ignore")
for m in re.finditer(r"\\u\{([0-9A-Fa-f]{4,6})\}", text):
cp = int(m.group(1), 16)
if is_pua(cp):
cps.add(cp)
cps.update(ord(ch) for ch in text if is_pua(ord(ch)))
return cps
def main(argv: list[str]) -> int:
if len(argv) != 2:
print(__doc__, file=sys.stderr)
return 2
from fontTools import subset
from fontTools.ttLib import TTFont
font = TTFont(argv[1])
have = font.getBestCmap()
wanted = used_codepoints()
missing = sorted(cp for cp in wanted if cp not in have)
options = subset.Options()
options.flavor = "woff2"
options.layout_features = []
options.name_IDs = []
options.notdef_outline = False
options.hinting = False
sub = subset.Subsetter(options)
sub.populate(unicodes=sorted(wanted - set(missing)))
sub.subset(font)
font["name"].setName("croft-icons", 1, 3, 1, 0x409)
font.flavor = "woff2"
font.save(OUT)
print(f"{OUT.relative_to(ROOT)}: {len(wanted) - len(missing)} glyphs, {OUT.stat().st_size} bytes")
if missing:
print("not in this font: " + ", ".join(f"U+{cp:04X}" for cp in missing))
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))