Traverse when there is no layer list

This commit is contained in:
Rod S 2021-11-06 14:26:27 -07:00
parent a2990a25df
commit 7dd3488732
3 changed files with 41 additions and 2 deletions

View File

@ -38,6 +38,14 @@ def parseXML(xmlSnippet):
return reader.root[2] 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: class FakeFont:
def __init__(self, glyphs): def __init__(self, glyphs):
self.glyphOrder_ = glyphs self.glyphOrder_ = glyphs

View File

@ -1516,7 +1516,11 @@ class Paint(getFormatSwitchingBaseTableClass("uint8")):
def getChildren(self, colr): def getChildren(self, colr):
if self.Format == PaintFormat.PaintColrLayers: 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 self.FirstLayerIndex : self.FirstLayerIndex + self.NumLayers
] ]

View File

@ -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.textTools import deHexStr, hexStr
from fontTools.misc.xmlWriter import XMLWriter from fontTools.misc.xmlWriter import XMLWriter
from fontTools.ttLib.tables.otBase import OTTableReader, OTTableWriter from fontTools.ttLib.tables.otBase import OTTableReader, OTTableWriter
@ -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__": if __name__ == "__main__":
import sys import sys
sys.exit(unittest.main()) sys.exit(unittest.main())