Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CompatibilityChecker: check all layers within designspace sources #1090

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions Lib/fontmake/compatibility.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from __future__ import annotations

import logging

from ufoLib2 import Font
from ufoLib2.objects import Layer
from fontTools.designspaceLib import DesignSpaceDocument

logger = logging.getLogger(__name__)


Expand All @@ -16,20 +22,32 @@ def __exit__(self, type, value, traceback):


class CompatibilityChecker:
def __init__(self, fonts):
def __init__(self, designspace: DesignSpaceDocument):
self.errors = []
self.context = []
self.okay = True
self.fonts = fonts

def check(self):
self.fonts: list[Font] = [source.font for source in designspace.sources]
self.layers: list[tuple[Font, Layer]] = [
(
source.font,
source.font.layers.defaultLayer
if source.layerName is None
else source.font.layers[source.layerName],
)
for source in designspace.sources
]

def check(self) -> bool:
first = self.fonts[0]
skip_export_glyphs = set(first.lib.get("public.skipExportGlyphs", ()))
for glyph in first.keys():
if glyph in skip_export_glyphs:
continue
self.current_fonts = [font for font in self.fonts if glyph in font]
glyphs = [font[glyph] for font in self.current_fonts]
self.current_layers = [
(font, layer) for (font, layer) in self.layers if glyph in layer
]
glyphs = [layer[glyph] for (_, layer) in self.current_layers]
with Context(self, f"glyph {glyph}"):
self.check_glyph(glyphs)
return self.okay
Expand Down Expand Up @@ -76,28 +94,32 @@ def check_contours(self, contours):
with Context(self, f"point {ix}"):
self.ensure_all_same(lambda x: x.type, point, "point type")

def ensure_all_same(self, func, objs, what):
def ensure_all_same(self, func, objs, what) -> bool:
values = {}
context = ", ".join(self.context)
for obj, font in zip(objs, self.current_fonts):
values.setdefault(func(obj), []).append(self._name_for(font))
for obj, (font, layer) in zip(objs, self.current_layers):
values.setdefault(func(obj), []).append(self._name_for(font, layer))
if len(values) < 2:
logger.debug(f"All fonts had same {what} in {context}")
logger.debug(f"All sources had same {what} in {context}")
return True
report = f"\nFonts had differing {what} in {context}:\n"
report = f"\nSources 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)
for value, source_names in values.items():
if debug_enabled or len(source_names) <= 6:
key = ", ".join(source_names)
else:
key = f"{len(fonts)} fonts"
key = f"{len(source_names)} sources"
if len(str(value)) > 20:
value = "\n " + str(value)
report += f" * {key} had: {value}\n"
logger.error(report)
self.okay = False
return False

def _name_for(self, font):
names = list(filter(None, [font.info.familyName, font.info.styleName]))
def _name_for(self, font: Font, layer: Layer) -> str:
names: list[str] = [
name
for name in (font.info.familyName, font.info.styleName, layer.name)
if name is not None and name != "public.default"
]
return " ".join(names)
8 changes: 4 additions & 4 deletions Lib/fontmake/font_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ def run_from_designspace(
f"expected path or DesignSpaceDocument, found {type(designspace.__name__)}"
)

logger.info("Loading %s DesignSpace source UFOs", len(designspace.sources))
logger.info("Loading %s DesignSpace sources", len(designspace.sources))
designspace.loadSourceFonts(opener=self.open_ufo)

# if no --feature-writers option was passed, check in the designspace's
Expand All @@ -1177,14 +1177,14 @@ def run_from_designspace(
for discrete_location, subDoc in splitInterpolable(designspace):
# glyphsLib currently stores this custom parameter on the fonts,
# not the designspace, so we check if it exists in any font's lib.
source_fonts = [source.font for source in subDoc.sources]
explicit_check = any(
font.lib.get(COMPAT_CHECK_KEY, False) for font in source_fonts
source.font.lib.get(COMPAT_CHECK_KEY, False)
for source in subDoc.sources
)
if check_compatibility is not False and (
interp_outputs or check_compatibility or explicit_check
):
if not CompatibilityChecker(source_fonts).check():
if not CompatibilityChecker(subDoc).check():
message = "Compatibility check failed"
if discrete_location:
message += f" in interpolable sub-space at {discrete_location}"
Expand Down
6 changes: 3 additions & 3 deletions tests/test_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_compatibility_checker(data_dir, caplog):
)
designspace.loadSourceFonts(opener=ufoLib2.objects.Font.open)

CompatibilityChecker([s.font for s in designspace.sources]).check()
CompatibilityChecker(designspace).check()
assert "differing number of contours in glyph A" in caplog.text
assert "Incompatible Sans Regular had: 2" in caplog.text

Expand All @@ -21,10 +21,10 @@ def test_compatibility_checker(data_dir, caplog):
assert "differing anchors in glyph A" 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
assert "Sources had differing number of components in glyph C" in caplog.text

assert (
"Fonts had differing point type in glyph D, contour 0, point 10" in caplog.text
"Sources had differing point type in glyph D, contour 0, point 10" in caplog.text
)


Expand Down
Loading