Skip to content

Commit

Permalink
Add glyphs finder for feature porting
Browse files Browse the repository at this point in the history
  • Loading branch information
mishamyrt committed Mar 29, 2023
1 parent d948f87 commit 36c6c80
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ fontmake==3.5.1
cu2qu==1.6.7
gftools==0.9.27
glyphsLib==6.2.1
arrrgs==0.0.5
arrrgs==2.0.0
ruff==0.0.259
pylint==2.17.1
70 changes: 70 additions & 0 deletions scripts/find.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python3
"""Utility script for feature porting.
Finds glyphs in the .fea file
and checks which of them are present in the source font file.
Usage: find.py ../features/calt/hyphen-arrows.fea missing
"""
from re import sub
from typing import List

from arrrgs import arg, command, global_args, run
from glyphsLib import GSFont

FONT_FILE = "Lilex.glyphs"

global_args(
arg("--file", "-f", required=True, help="Input .fea file")
)

@command()
def missing(_, gls: List[str]):
"""Finds missing glyphs"""
font = GSFont(FONT_FILE)
missing_glyphs = []
for glyph in gls:
if glyph not in font.glyphs:
missing_glyphs.append(glyph)
print("Missing glyphs:")
print("\n".join(missing_glyphs))

stock = len(gls) - len(missing_glyphs)
percent = stock / len(gls)
print(f"\nGlyphs coverage: {(percent * 100):.1f}% ({stock}/{len(gls)})")



@command()
def glyphs(_, gls: List[str]):
print("\n".join(gls))

KEYWORDS = [
"ignore", "sub", "by"
]

def _is_glyph_name(word: str) -> bool:
return len(word) > 0 and word not in KEYWORDS

def glyphs_from_fea(path: str) -> List[str]:
with open(path, mode="r", encoding="utf-8") as file:
content = file.read()
# Remove comments
content = sub(r"\#(.*)", "", content)
# Remove lookups
content = sub(r"lookup(.*){", "", content)
content = sub(r"}(.*);", "", content)
# Remove symbols
content = sub(r"[';\[\]]", "", content)
# Remove newlines and duplicated spaces
content = content.replace("\n", " ")
content = sub(" +", " ", content)
# Filter keywords
words = content.split(" ")
return list(filter(_is_glyph_name, words))

def load_words(args):
words = glyphs_from_fea(args.file)
return args, words

if __name__ == "__main__":
run(prepare=load_words)

0 comments on commit 36c6c80

Please sign in to comment.