Repository: googlefonts/glyphsLib
Pull request #581 (2020-01-29 13:48:18)

update autogenerated GDEF table after applying bracket glyphs
Fixes https://github.com/googlefonts/glyphsLib/issues/578

This is the easier way.
I'll come up with a test after lunch.


Patch:
diff --git a/Lib/glyphsLib/builder/builders.py b/Lib/glyphsLib/builder/builders.py
index f7af89af..1d48dbb9 100644
--- a/Lib/glyphsLib/builder/builders.py
+++ b/Lib/glyphsLib/builder/builders.py
@@ -418,6 +418,11 @@ class UFOBuilder(_LoggerMixin):
         # Finally, copy bracket layers to their own glyphs.
         self._copy_bracket_layers_to_ufo_glyphs(bracket_layer_map)
 
+        # re-generate the GDEF table since we have added new BRACKET glyphs, which may
+        # also need to be included: https://github.com/googlefonts/glyphsLib/issues/578
+        if self.generate_GDEF:
+            self.to_ufo_features()
+
     def _copy_bracket_layers_to_ufo_glyphs(self, bracket_layer_map):
         font = self.font
         master_ids = {m.id for m in font.masters}


Test patch:
diff --git a/tests/builder/designspace_gen_test.py b/tests/builder/designspace_gen_test.py
index 62c9b78b..fd08dd1b 100644
--- a/tests/builder/designspace_gen_test.py
+++ b/tests/builder/designspace_gen_test.py
@@ -14,12 +14,16 @@
 # limitations under the License.
 
 
+import io
 import os
 from xmldiff import main, formatting
 
 import itertools
 import pytest
 
+import fontTools.feaLib.parser
+import fontTools.feaLib.ast
+
 import glyphsLib
 from glyphsLib import to_designspace, to_glyphs
 from glyphsLib.util import open_ufo
@@ -409,3 +413,51 @@ def test_designspace_generation_bracket_no_export_glyph(datadir, ufo_module):
         "Bold",
         "Bold ]570]",
     }
+
+
+def test_designspace_generation_bracket_GDEF(datadir, ufo_module):
+    with open(str(datadir.join("BracketTestFont.glyphs"))) as f:
+        font = glyphsLib.load(f)
+
+    # add some attaching anchors to the "x" glyph and its (bracket) layers to
+    # trigger the generation of GDEF table
+    for layer in font.glyphs["x"].layers:
+        anchor = glyphsLib.classes.GSAnchor()
+        anchor.name = "top"
+        anchor.position = (0, 0)
+        layer.anchors.append(anchor)
+
+    designspace = to_designspace(font, ufo_module=ufo_module, generate_GDEF=True)
+
+    for source in designspace.sources:
+        ufo = source.font
+        features = fontTools.feaLib.parser.Parser(
+            io.StringIO(ufo.features.text), glyphNames=ufo.keys()
+        ).parse()
+        for stmt in features.statements:
+            if (
+                isinstance(stmt, fontTools.feaLib.ast.TableBlock)
+                and stmt.name == "GDEF"
+            ):
+                gdef = stmt
+                for stmt in gdef.statements:
+                    if isinstance(stmt, fontTools.feaLib.ast.GlyphClassDefStatement):
+                        glyph_class_defs = stmt
+                        break
+                else:
+                    pytest.fail(
+                        f"No GDEF.GlyphClassDef statement found in {ufo!r} features:\n"
+                        f"{ufo.features.text}"
+                    )
+                break
+        else:
+            pytest.fail(
+                f"No GDEF table definition found in {ufo!r} features:\n"
+                f"{ufo.features.text}"
+            )
+
+        assert set(glyph_class_defs.baseGlyphs.glyphSet()) == {
+            "x",
+            "x.BRACKET.300",
+            "x.BRACKET.600",
+        }