Skip to content

Commit

Permalink
Use shaper to check whether glyphs exist, see #7
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed Oct 31, 2022
1 parent 0132189 commit 74a1bb5
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions shaperglot/checks/orthographies.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import re

from strictyaml import Map

from .common import ShaperglotCheck, and_join

def parse_bases(bases):
return [x[0] or x[1] for x in re.findall(r"\{([^}]+)\}|(\S+)", bases)]


class OrthographiesCheck(ShaperglotCheck):
name = "orthographies"
Expand All @@ -11,7 +16,7 @@ class OrthographiesCheck(ShaperglotCheck):
def __init__(self, lang):
exemplar_chars = lang.get("exemplarChars", {})
marks = exemplar_chars.get("marks", "").replace("◌", "").split() or []
bases = exemplar_chars.get("base", "").split() or []
bases = parse_bases(exemplar_chars.get("base", ""))
self.all_glyphs = marks + bases
self.marks = set(marks)
self.bases = set(bases) - self.marks
Expand All @@ -21,22 +26,26 @@ def describe(self):
f"'{g}'" for g in self.all_glyphs
)

def can_shape(self, text, checker):
buf = checker.vharfbuzz.shape(text)
return all(gi.codepoint != 0 for gi in buf.glyph_infos)

def execute(self, checker):
if not self.all_glyphs:
checker.results.warn(
f"No glyphs were defined for language {checker.lang['name']}"
)
return
missing = [x for x in self.bases if ord(x) not in checker.cmap]
missing = [x for x in self.bases if not self.can_shape(x, checker)]
if missing:
missing = ", ".join(missing)
checker.results.fail(f"Some base glyphs were missing: {missing}")
else:
checker.results.okay("All base glyphs were present in the font")
if self.marks:
missing = [x for x in self.marks if ord(x) not in checker.cmap]
missing = [x for x in self.marks if not self.can_shape(x, checker)]
if missing:
missing = ", ".join(missing)
missing = ", ".join([chr(0x25cc)+x for x in missing])
checker.results.fail(f"Some mark glyphs were missing: {missing}")
else:
checker.results.okay("All mark glyphs were present in the font")

0 comments on commit 74a1bb5

Please sign in to comment.