From dae37e6b1abbebb31f97a46beaf7fe3f67f3c729 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Thu, 2 Nov 2023 14:57:51 +0000 Subject: [PATCH] Improve compatibility checker (#1052) * Check for contextual anchors * Improve display of error * Update tests * Make flake8 happier --- Lib/fontmake/compatibility.py | 23 +++++++++++++++++++++-- tests/test_compatibility.py | 4 ++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Lib/fontmake/compatibility.py b/Lib/fontmake/compatibility.py index 8a164cee..a830e6f4 100644 --- a/Lib/fontmake/compatibility.py +++ b/Lib/fontmake/compatibility.py @@ -46,6 +46,22 @@ def check_glyph(self, glyphs): anchors, "anchors", ) + # Context for contextual anchors + libs = [g.lib for g in glyphs] + for each_anchors in zip(*anchors): + if each_anchors[0].name[0] == "*": + objectlibs = [ + libs[font_ix] + .get("public.objectLibs", {}) + .get(anchor.identifier, {}) + for font_ix, anchor in enumerate(each_anchors) + ] + with Context(self, f"anchor {each_anchors[0].name}"): + self.ensure_all_same( + lambda lib: lib.get("GPOS_Context", "None").strip(), + objectlibs, + "GPOS context", + ) components = [g.components for g in glyphs] if self.ensure_all_same(len, components, "number of components"): @@ -68,14 +84,17 @@ def ensure_all_same(self, func, objs, what): if len(values) < 2: logger.debug(f"All fonts had same {what} in {context}") return True - logger.error(f"Fonts had differing {what} in {context}:") + report = f"\nFonts had differing {what} in {context}:\n" debug_enabled = logger.isEnabledFor(logging.DEBUG) for value, fonts in values.items(): if debug_enabled or len(fonts) <= 6: key = ", ".join(fonts) else: key = f"{len(fonts)} fonts" - logger.error(f" * {key} had {value}") + if len(str(value)) > 20: + value = "\n " + str(value) + report += f" * {key} had: {value}\n" + logger.error(report) self.okay = False return False diff --git a/tests/test_compatibility.py b/tests/test_compatibility.py index b2fff2f9..54459644 100644 --- a/tests/test_compatibility.py +++ b/tests/test_compatibility.py @@ -14,12 +14,12 @@ def test_compatibility_checker(data_dir, caplog): CompatibilityChecker([s.font for s in designspace.sources]).check() assert "differing number of contours in glyph A" in caplog.text - assert "Incompatible Sans Regular had 2" in caplog.text + assert "Incompatible Sans Regular had: 2" in caplog.text assert "differing number of points in glyph B, contour 0" in caplog.text assert "differing anchors in glyph A" in caplog.text - assert 'Incompatible Sans Bold had "foo"' in caplog.text + assert 'Incompatible Sans Bold had: "foo"' in caplog.text assert "Fonts had differing number of components in glyph C" in caplog.text