#!/usr/bin/env python3 """ Subset the vendored Font Awesome webfonts down to only the icons the panel actually uses, with no loss of quality for any icon in use. Why: the full FA 6 webfonts ship ~2000 glyphs (fa-solid-900.woff2 alone is ~154KB). The panel uses ~165. woff2 is already Brotli-compressed internally, so it cannot be "recompressed" — the only lossless way to shrink it is to drop the unused glyphs. This keeps every icon we reference at full fidelity. How it works: 1. Build an icon-name -> Unicode codepoint map by parsing the vendored all.min.css (`.fa-name{--fa:"\\fXXX"}`, including grouped selectors). 2. Scan every template, client/server JS and custom CSS for `fa-` usages and direct `content:"\\fXXX"` codepoints. 3. Run pyftsubset over fa-solid-900 / fa-regular-400 / fa-brands-400, keeping only the used codepoints (each font keeps the ones it actually has). Re-run this whenever you add new icons. Originals are backed up once to public/vendor/fontawesome/webfonts-full/ so a full restore is always possible. Usage: scripts/subset-fontawesome.sh (sets up the venv, then runs this) """ import os import re import sys import glob import shutil import subprocess ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) VENDOR = os.path.join(ROOT, "public", "vendor", "fontawesome") WEBFONTS = os.path.join(VENDOR, "webfonts") BACKUP = os.path.join(VENDOR, "webfonts-full") CSS = os.path.join(VENDOR, "css", "all.min.css") FONTS = ["fa-solid-900", "fa-regular-400", "fa-brands-400"] SCAN_GLOBS = [ "views/**/*.ejs", "public/js/**/*.js", "*.js", "public/css/**/*.css", "public/css-ext/**/*.css", ] def build_name_map(css_text): name2cp = {} for block in re.finditer(r'((?:\.fa-[a-z0-9-]+,?)+)\{--fa:"\\([0-9a-fA-F]+)"\}', css_text): cp = block.group(2).lower() for n in re.findall(r'\.fa-([a-z0-9-]+)', block.group(1)): name2cp.setdefault(n, cp) return name2cp def collect_codepoints(name2cp): valid = set(name2cp.values()) files = [] for pat in SCAN_GLOBS: files += glob.glob(os.path.join(ROOT, pat), recursive=True) files = [f for f in files if "vendor/fontawesome" not in f.replace("\\", "/") and "fontawesome" not in os.path.basename(f).lower()] cps = set() for f in files: try: t = open(f, encoding="utf-8", errors="ignore").read() except OSError: continue for n in re.findall(r'fa-([a-z0-9-]+)', t): if n in name2cp: cps.add(name2cp[n]) if f.endswith(".css"): for c in re.findall(r'content:\s*"\\([0-9a-fA-F]{3,5})"', t): if c.lower() in valid: cps.add(c.lower()) return sorted(cps), len(files) def main(): if not os.path.exists(CSS): sys.exit(f"Missing {CSS}") name2cp = build_name_map(open(CSS, encoding="utf-8", errors="ignore").read()) cps, nfiles = collect_codepoints(name2cp) if not cps: sys.exit("No icons detected — aborting so we don't ship empty fonts.") unicodes = ",".join("U+" + c.upper() for c in cps) print(f"Scanned {nfiles} files; {len(name2cp)} known icons; using {len(cps)} codepoints.") os.makedirs(BACKUP, exist_ok=True) for font in FONTS: src = os.path.join(WEBFONTS, font + ".woff2") if not os.path.exists(src): print(f" skip {font}: not found") continue backup = os.path.join(BACKUP, font + ".woff2") if not os.path.exists(backup): shutil.copy2(src, backup) # preserve the original once source = backup # always subset from the pristine original before = os.path.getsize(source) subprocess.check_call([ sys.executable, "-m", "fontTools.subset", source, f"--unicodes={unicodes}", "--flavor=woff2", "--layout-features=*", f"--output-file={src}", ]) after = os.path.getsize(src) print(f" {font}: {before // 1024}KB -> {after // 1024}KB") print("Done. Originals preserved in webfonts-full/.") if __name__ == "__main__": main()