From 74a1bb5fae4d39fcd2c1457ffaeef9c7ac07f2d6 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Mon, 31 Oct 2022 13:33:47 +0000 Subject: [PATCH] Use shaper to check whether glyphs exist, see #7 --- shaperglot/checks/orthographies.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/shaperglot/checks/orthographies.py b/shaperglot/checks/orthographies.py index 8242307..13bd3a1 100644 --- a/shaperglot/checks/orthographies.py +++ b/shaperglot/checks/orthographies.py @@ -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" @@ -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 @@ -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")