From dd305c6fa3378b46d80586baeaf242f05550520d Mon Sep 17 00:00:00 2001 From: Oliver Ford Date: Sun, 11 Feb 2024 02:20:23 +0000 Subject: [PATCH] Fix inconsistent naming across formats & patching --- iosevka-generate | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/iosevka-generate b/iosevka-generate index 22ec4f9..e994fa6 100755 --- a/iosevka-generate +++ b/iosevka-generate @@ -87,15 +87,17 @@ def generate_font_plan( fp.write(dedent(conf)) -def generate_font(iosevka_dir: Path, font_name: str) -> None: +def generate_font(iosevka_dir: Path, plan_name: str) -> None: subprocess.run( - ["npm", "run", "build", "--", f"ttf::{font_name}"], cwd=iosevka_dir + ["npm", "run", "build", "--", f"ttf::{plan_name}"], + cwd=iosevka_dir, ).check_returncode() def nerdfont_patch( font_dir: Path, cache_dir: Path, + family: str, options: Iterable[str] = [], mono: bool = True, ) -> None: @@ -112,18 +114,26 @@ def nerdfont_patch( for font in filter(Path.is_file, font_dir.glob("*")): print(f"Patching {font} with nerdfont ...", file=sys.stderr) + # nerdfont patcher will overwrite the family with --name, which is also the filename; + # so we name it with the family name, and now move it back to overwrite the original. + patched_font = ( + font.parent + / f"{family.replace(' ', '')}-{font.stem.split('-', 1)[1]}{font.suffix}" + ) subprocess.run( [ f"{cache_dir}/font-patcher", "--careful", "--progressbars", *(["--mono"] if mono else []), + f"--name={patched_font.stem}", *map(lambda o: f"--{o}", options), font, ], cwd=font_dir, ).check_returncode() - font.rename(f"{font}.unpatched") + print(f"Replacing {font} with patched {patched_font}", file=sys.stderr) + patched_font.rename(font) def parse_config_ini( @@ -163,7 +173,7 @@ def parse_config_ini( def parse_config_toml( config_file: Path, -) -> Tuple[str, Dict[str, Set[str]], Optional[List]]: +) -> Tuple[Tuple[str, str], Dict[str, Set[str]], Optional[List]]: print(f"Parsing iosevka-generate conf: {config_file} ...", file=sys.stderr) with open(config_file, "rb") as fp: @@ -184,13 +194,13 @@ def parse_config_toml( opts = config["buildPlans"][name].get("iosevka-generate", {}) nerdfont = opts.get("nerdfont", []) - return (name, styles, nerdfont) + return ((name, config["buildPlans"][name].get("family", name)), styles, nerdfont) def store_fonts(dest: Path, source: Path): dest.mkdir(parents=True, exist_ok=True) for font in filter(lambda f: f.is_file(), source.glob("*.ttf")): - shutil.move(str(source / font), dest / font.name) + shutil.move(str(source / font), dest) print(f"Fonts installed to {dest}", file=sys.stderr) subprocess.run(["fc-cache", "--force"]).check_returncode() @@ -212,20 +222,23 @@ if __name__ == "__main__": print(f"Found {conf.stem}", file=sys.stderr) if conf.suffix == ".ini": - name, styles, nerdfont_opts = parse_config_ini(conf) - generate_font_plan(install_dir, name, styles) + plan, styles, nerdfont_opts = parse_config_ini(conf) + family = generate_font_plan(install_dir, plan, styles) else: - name, styles, nerdfont_opts = parse_config_toml(conf) + (plan, family), styles, nerdfont_opts = parse_config_toml(conf) shutil.copy(conf, install_dir / "private-build-plans.toml") - generate_font(install_dir, name) - gen_dir = install_dir / "dist" / name / "TTF" + generate_font(install_dir, plan) + gen_dir = install_dir / "dist" / plan / "TTF" if nerdfont_opts is not None: mono = any(filter(lambda o: o in styles["common"], ("sp-term", "sp-fixed"))) - nerdfont_patch( - gen_dir, cache_dir / "nerdfont", options=nerdfont_opts, mono=mono + gen_dir, + cache_dir / "nerdfont", + family, + options=nerdfont_opts, + mono=mono, ) store_fonts(font_dir, gen_dir)