colorLib.unbuilder: ensure unbuildColrV1 flattens nested PaintColrLayers

This commit is contained in:
Cosimo Lupo 2022-06-13 16:50:01 +01:00
parent fc982c643b
commit c612b2637f
2 changed files with 28 additions and 8 deletions

View File

@ -13,12 +13,12 @@ def unbuildColrV1(layerList, baseGlyphList):
}
def _flatten(lst):
for el in lst:
if isinstance(el, list):
yield from _flatten(el)
def _flatten_layers(lst):
for paint in lst:
if paint["Format"] == ot.PaintFormat.PaintColrLayers:
yield from _flatten_layers(paint["Layers"])
else:
yield el
yield paint
class LayerListUnbuilder:
@ -41,7 +41,7 @@ class LayerListUnbuilder:
assert source["Format"] == ot.PaintFormat.PaintColrLayers
layers = list(
_flatten(
_flatten_layers(
[
self.unbuildPaint(childPaint)
for childPaint in self.layers[

View File

@ -221,7 +221,26 @@ TEST_COLOR_GLYPHS = {
"Glyph": "glyph00012",
},
],
}
},
# When PaintColrLayers contains more than 255 layers, we build a tree
# of nested PaintColrLayers of max 255 items (NumLayers field is a uint8).
# Below we test that unbuildColrV1 restores a flat list of layers without
# nested PaintColrLayers.
"glyph00017": {
"Format": int(ot.PaintFormat.PaintColrLayers),
"Layers": [
{
"Format": int(ot.PaintFormat.PaintGlyph),
"Paint": {
"Format": int(ot.PaintFormat.PaintSolid),
"PaletteIndex": i,
"Alpha": 1.0,
},
"Glyph": "glyph{str(18 + i).zfill(5)}",
}
for i in range(256)
],
},
}
@ -230,7 +249,8 @@ def test_unbuildColrV1():
colorGlyphs = unbuildColrV1(layers, baseGlyphs)
assert colorGlyphs == TEST_COLOR_GLYPHS
def test_unbuildColrV1_noLayers():
_, baseGlyphsV1 = buildColrV1(TEST_COLOR_GLYPHS)
# Just looking to see we don't crash
unbuildColrV1(None, baseGlyphsV1)
unbuildColrV1(None, baseGlyphsV1)