Skip to content

Commit

Permalink
Traverse when there is no layer list
Browse files Browse the repository at this point in the history
  • Loading branch information
rsheeter committed Nov 6, 2021
1 parent a2990a2 commit 7dd3488
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Lib/fontTools/misc/testTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ def parseXML(xmlSnippet):
return reader.root[2]


def parseXmlInto(font, parseInto, xmlSnippet):
parsed_xml = [e for e in parseXML(xmlSnippet.strip()) if not isinstance(e, str)]
for name, attrs, content in parsed_xml:
parseInto.fromXML(name, attrs, content, font)
parseInto.populateDefaults()
return parseInto


class FakeFont:
def __init__(self, glyphs):
self.glyphOrder_ = glyphs
Expand Down
6 changes: 5 additions & 1 deletion Lib/fontTools/ttLib/tables/otTables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,11 @@ def toXML(self, xmlWriter, font, attrs=None, name=None):

def getChildren(self, colr):
if self.Format == PaintFormat.PaintColrLayers:
return colr.LayerList.Paint[
# https://github.com/fonttools/fonttools/issues/2438: don't die when no LayerList exists
layers = []
if colr.LayerList is not None:
layers = colr.LayerList.Paint
return layers[
self.FirstLayerIndex : self.FirstLayerIndex + self.NumLayers
]

Expand Down
29 changes: 28 additions & 1 deletion Tests/ttLib/tables/otTables_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fontTools.misc.testTools import getXML, parseXML, FakeFont
from fontTools.misc.testTools import getXML, parseXML, parseXmlInto, FakeFont
from fontTools.misc.textTools import deHexStr, hexStr
from fontTools.misc.xmlWriter import XMLWriter
from fontTools.ttLib.tables.otBase import OTTableReader, OTTableWriter
Expand Down Expand Up @@ -687,6 +687,33 @@ def test_splitMarkBasePos():
]


class ColrV1Test(unittest.TestCase):
def setUp(self):
self.font = FakeFont(['.notdef', 'meh'])

def test_traverseEmptyPaintColrLayersNeedsNoLayerList(self):
colr = parseXmlInto(self.font, otTables.COLR(),
'''
<Version value="1"/>
<BaseGlyphList>
<BaseGlyphPaintRecord index="0">
<BaseGlyph value="meh"/>
<Paint Format="1"><!-- PaintColrLayers -->
<NumLayers value="0"/>
<FirstLayerIndex value="42"/>
</Paint>
</BaseGlyphPaintRecord>
</BaseGlyphList>
''')
paint = colr.BaseGlyphList.BaseGlyphPaintRecord[0].Paint

# Just want to confirm we don't crash
visited = []
paint.traverse(colr, lambda p: visited.append(p))
assert len(visited) == 1



if __name__ == "__main__":
import sys
sys.exit(unittest.main())

0 comments on commit 7dd3488

Please sign in to comment.