Skip to content

Commit

Permalink
Fix inconsistent naming across formats & patching
Browse files Browse the repository at this point in the history
  • Loading branch information
OJFord committed Feb 11, 2024
1 parent 7cc1729 commit dd305c6
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions iosevka-generate
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand All @@ -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)

0 comments on commit dd305c6

Please sign in to comment.