diff --git a/Lib/ufo2ft/constants.py b/Lib/ufo2ft/constants.py index c54b2530..9864e631 100644 --- a/Lib/ufo2ft/constants.py +++ b/Lib/ufo2ft/constants.py @@ -20,6 +20,8 @@ class CFFOptimization(IntEnum): EXPLICIT_CLOSING_LINE_KEY = UFO2FT_PREFIX + "explicitClosingLine" +EMPTY_NOT_SPARSE_KEY = UFO2FT_PREFIX + "instantiator.emptyNotSparse" + MTI_FEATURES_PREFIX = UFO2FT_PREFIX + "mtiFeatures" FEATURE_WRITERS_KEY = UFO2FT_PREFIX + "featureWriters" diff --git a/Lib/ufo2ft/instantiator.py b/Lib/ufo2ft/instantiator.py index 0c90012e..37833438 100644 --- a/Lib/ufo2ft/instantiator.py +++ b/Lib/ufo2ft/instantiator.py @@ -55,7 +55,7 @@ from fontTools.ttLib.tables._n_a_m_e import NameRecord as ftNameRecord from fontTools.ttLib.tables._n_a_m_e import _makeWindowsName -from ufo2ft.constants import OPENTYPE_CATEGORIES_KEY +from ufo2ft.constants import EMPTY_NOT_SPARSE_KEY, OPENTYPE_CATEGORIES_KEY from ufo2ft.util import ( _getNewGlyphFactory, importUfoModule, @@ -1084,6 +1084,10 @@ def collect_glyph_masters( what ufoProcessor is doing. In e.g. Mutator Sans, the 'S.closed' glyph is left empty in one source layer. One could treat this as a source error, but ufoProcessor specifically has code to skip that empty glyph and carry on. + + An empty glyph whose lib sets + "com.github.googlei18n.ufo2ft.instantiator.emptyNotSparse" to True is meant + to be empty, e.g. a master layer drawn empty in Glyphs, and is kept. """ locations_and_masters = [] default_glyph_empty = False @@ -1119,7 +1123,9 @@ def collect_glyph_masters( locations_and_masters = [ (loc, master) for loc, master in locations_and_masters - if master.contours or master.components + if master.contours + or master.components + or master.lib.get(EMPTY_NOT_SPARSE_KEY) ] return locations_and_masters diff --git a/tests/instantiator_test.py b/tests/instantiator_test.py index 20c71f3f..8fe0e2ed 100644 --- a/tests/instantiator_test.py +++ b/tests/instantiator_test.py @@ -1661,3 +1661,36 @@ def test_opentype_categories_from_source_at_instance_location(ufo_module): {"a": "base"}, {"a": "base", "b": "base"}, ] + + +@pytest.mark.parametrize("marked,empty", [(True, True), (False, True), (True, False)]) +def test_explicit_empty_not_sparse_glyph(ufo_module, marked, empty): + d = designspaceLib.DesignSpaceDocument() + d.addAxisDescriptor( + name="Weight", tag="wght", minimum=100, default=100, maximum=200 + ) + for value in (100, 200): + font = ufo_module.Font() + glyph = font.newGlyph("a") + glyph.width = 500 if value == 100 else 700 + if value == 100 or not empty: + pen = glyph.getPen() + pen.moveTo((0, 0)) + pen.lineTo((100, 0)) + pen.lineTo((100, 100)) + pen.closePath() + if value == 200 and marked: + glyph.lib["com.github.googlei18n.ufo2ft.instantiator.emptyNotSparse"] = True + d.addSourceDescriptor(location={"Weight": value}, font=font) + d.addInstanceDescriptor(styleName="Bold", location={"Weight": 200}) + d.addInstanceDescriptor(styleName="Between", location={"Weight": 150}) + generator = ufo2ft.instantiator.Instantiator.from_designspace(d) + glyph = generator.generate_instance(d.instances[0])["a"] + if marked and empty: + assert len(glyph) == 0 + assert glyph.width == 700 + with pytest.raises(ufo2ft.instantiator.InstantiatorError): + generator.generate_instance(d.instances[1]) + else: + assert len(glyph) == 1 + assert len(generator.generate_instance(d.instances[1])["a"]) == 1