<NME> unbuilder_test.py
<BEF> from fontTools.ttLib.tables import otTables as ot
from fontTools.colorLib.builder import buildColrV1
from fontTools.colorLib.unbuilder import unbuildColrV1
import pytest


TEST_COLOR_GLYPHS = {
    "glyph00010": [
        {
            "format": int(ot.Paint.Format.PaintGlyph),
            "glyph": "glyph00011",
            "paint": {
                "format": int(ot.Paint.Format.PaintSolid),
                "paletteIndex": 2,
                "alpha": 0.5,
            },
        },
        {
            "format": int(ot.Paint.Format.PaintGlyph),
            "glyph": "glyph00012",
            "paint": {
                "format": int(ot.Paint.Format.PaintLinearGradient),
                "colorLine": {
                    "stops": [
                        {"offset": 0.0, "paletteIndex": 3, "alpha": 1.0},
                        "ColorStop": [
                            {
                                "StopOffset": 0.0,
                                "PaletteIndex": 3,
                                "Alpha": 1.0,
                                "VarIndexBase": 0,
                            },
                            {
            },
        },
        {
            "format": int(ot.Paint.Format.PaintGlyph),
            "glyph": "glyph00013",
            "paint": {
                "format": int(ot.Paint.Format.PaintTransform),
                "transform": (-13.0, 14.0, 15.0, -17.0, 18.0, 19.0),
                "paint": {
                    "format": int(ot.Paint.Format.PaintRadialGradient),
                    "colorLine": {
                        "stops": [
                            {"offset": 0.0, "paletteIndex": 6, "alpha": 1.0},
                    "x0": 1,
                    "y0": 2,
                    "x1": -3,
                    "y1": -4,
                    "x2": 5,
                    "y2": 6,
                    "VarIndexBase": 0xFFFFFFFF,
                },
                "Glyph": "glyph00012",
            },
            {
                "Format": int(ot.PaintFormat.PaintGlyph),
                "Paint": {
            },
        },
        {
            "format": int(ot.Paint.Format.PaintTranslate),
            "dx": 257.0,
            "dy": 258.0,
            "paint": {
                "format": int(ot.Paint.Format.PaintRotate),
                "angle": 45.0,
                "centerX": 255.0,
                "centerY": 256.0,
                "paint": {
                    "format": int(ot.Paint.Format.PaintSkew),
                    "xSkewAngle": -11.0,
                    "ySkewAngle": 5.0,
                    "centerX": 253.0,
                    "centerY": 254.0,
                    "paint": {
                        "format": int(ot.Paint.Format.PaintGlyph),
                        "glyph": "glyph00011",
                        "paint": {
                            "format": int(ot.Paint.Format.PaintSolid),
                            "paletteIndex": 2,
                            "alpha": 0.5,
                        },
                    "Transform": {
                        "xx": -13.0,
                        "yx": 14.0,
        },
    ],
    "glyph00014": {
        "format": int(ot.Paint.Format.PaintComposite),
        "mode": "src_over",
        "source": {
            "format": int(ot.Paint.Format.PaintColrGlyph),
            "glyph": "glyph00010",
        },
        "backdrop": {
            "format": int(ot.Paint.Format.PaintTransform),
            "transform": (1.0, 0.0, 0.0, 1.0, 300.0, 0.0),
            "paint": {
                "format": int(ot.Paint.Format.PaintColrGlyph),
                "glyph": "glyph00010",
            },
        },
    },
    "glyph00015": {
        "format": int(ot.Paint.Format.PaintGlyph),
        "glyph": "glyph00011",
        "paint": {
            "format": int(ot.Paint.Format.PaintSweepGradient),
            "colorLine": {
                "stops": [
                    {"offset": 0.0, "paletteIndex": 3, "alpha": 1.0},
                        "VarIndexBase": 4,
                    },
                    "angle": 45.0,
                },
                "dx": 257.0,
                "dy": 258.0,
                "VarIndexBase": 5,
            },
        ],
    },
    "glyph00016": [
        {
            "format": int(ot.Paint.Format.PaintGlyph),
            "glyph": "glyph00011",
            "paint": {
                "format": int(ot.Paint.Format.PaintSolid),
                "paletteIndex": 2,
                "alpha": 0.5,
            },
        },
        {
            "format": int(ot.Paint.Format.PaintGlyph),
            "glyph": "glyph00012",
            "paint": {
                "format": int(ot.Paint.Format.PaintLinearGradient),
                "colorLine": {
                    "stops": [
                        {"offset": 0.0, "paletteIndex": 3, "alpha": 1.0},
                "dx": 300.0,
                "dy": 0.0,
            },
        },
    },
    "glyph00015": {
        "Format": int(ot.PaintFormat.PaintGlyph),
        "Paint": {
            "Format": int(ot.PaintFormat.PaintSweepGradient),
            "ColorLine": {
                "Extend": "pad",
                "ColorStop": [
                    {
                        "StopOffset": 0.0,
                        "PaletteIndex": 3,
                        "Alpha": 1.0,
                    },
                    {
                        "StopOffset": 1.0,
                        "PaletteIndex": 5,
                        "Alpha": 1.0,
                    },
                ],
            },
            "centerX": 259,
            "centerY": 300,
            "startAngle": 45.0,
            "endAngle": 135.0,
        },
        "Glyph": "glyph00011",
    },
    "glyph00016": {
        "Format": int(ot.PaintFormat.PaintColrLayers),
        "Layers": [
            {
                "Format": int(ot.PaintFormat.PaintGlyph),
                "Paint": {
                    "Format": int(ot.PaintFormat.PaintVarSolid),
                    "PaletteIndex": 2,
                    "Alpha": 0.5,
                    "VarIndexBase": 6,
                },
                "Glyph": "glyph00011",
            },
            {
                "Format": int(ot.PaintFormat.PaintGlyph),
                "Paint": {
                    "Format": int(ot.PaintFormat.PaintVarLinearGradient),
                    "ColorLine": {
                        "Extend": "repeat",
                        "ColorStop": [
                            {
                                "StopOffset": 0.0,
                                "PaletteIndex": 3,
                                "Alpha": 1.0,
                                "VarIndexBase": 7,
                            },
                            {
                                "StopOffset": 0.5,
                                "PaletteIndex": 4,
                                "Alpha": 1.0,
                                "VarIndexBase": 8,
                            },
                            {
                                "StopOffset": 1.0,
                                "PaletteIndex": 5,
                                "Alpha": 1.0,
                                "VarIndexBase": 9,
                            },
                        ],
                    },
                    "x0": 1,
                    "y0": 2,
                    "x1": -3,
                    "y1": -4,
                    "x2": 5,
                    "y2": 6,
                    "VarIndexBase": 0xFFFFFFFF,
                },
                "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)
        ],
    },
}


def test_unbuildColrV1():
    layers, baseGlyphs = buildColrV1(TEST_COLOR_GLYPHS)
    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)

<MSG> Merge pull request #2175 from fonttools/move-paint-format-enum

COLRv1: fix lazy loading by moving Paint.Format enum outside of Paint class nested scope
<DFF> @@ -7,19 +7,19 @@ import pytest
 TEST_COLOR_GLYPHS = {
     "glyph00010": [
         {
-            "format": int(ot.Paint.Format.PaintGlyph),
+            "format": int(ot.PaintFormat.PaintGlyph),
             "glyph": "glyph00011",
             "paint": {
-                "format": int(ot.Paint.Format.PaintSolid),
+                "format": int(ot.PaintFormat.PaintSolid),
                 "paletteIndex": 2,
                 "alpha": 0.5,
             },
         },
         {
-            "format": int(ot.Paint.Format.PaintGlyph),
+            "format": int(ot.PaintFormat.PaintGlyph),
             "glyph": "glyph00012",
             "paint": {
-                "format": int(ot.Paint.Format.PaintLinearGradient),
+                "format": int(ot.PaintFormat.PaintLinearGradient),
                 "colorLine": {
                     "stops": [
                         {"offset": 0.0, "paletteIndex": 3, "alpha": 1.0},
@@ -34,13 +34,13 @@ TEST_COLOR_GLYPHS = {
             },
         },
         {
-            "format": int(ot.Paint.Format.PaintGlyph),
+            "format": int(ot.PaintFormat.PaintGlyph),
             "glyph": "glyph00013",
             "paint": {
-                "format": int(ot.Paint.Format.PaintTransform),
+                "format": int(ot.PaintFormat.PaintTransform),
                 "transform": (-13.0, 14.0, 15.0, -17.0, 18.0, 19.0),
                 "paint": {
-                    "format": int(ot.Paint.Format.PaintRadialGradient),
+                    "format": int(ot.PaintFormat.PaintRadialGradient),
                     "colorLine": {
                         "stops": [
                             {"offset": 0.0, "paletteIndex": 6, "alpha": 1.0},
@@ -60,25 +60,25 @@ TEST_COLOR_GLYPHS = {
             },
         },
         {
-            "format": int(ot.Paint.Format.PaintTranslate),
+            "format": int(ot.PaintFormat.PaintTranslate),
             "dx": 257.0,
             "dy": 258.0,
             "paint": {
-                "format": int(ot.Paint.Format.PaintRotate),
+                "format": int(ot.PaintFormat.PaintRotate),
                 "angle": 45.0,
                 "centerX": 255.0,
                 "centerY": 256.0,
                 "paint": {
-                    "format": int(ot.Paint.Format.PaintSkew),
+                    "format": int(ot.PaintFormat.PaintSkew),
                     "xSkewAngle": -11.0,
                     "ySkewAngle": 5.0,
                     "centerX": 253.0,
                     "centerY": 254.0,
                     "paint": {
-                        "format": int(ot.Paint.Format.PaintGlyph),
+                        "format": int(ot.PaintFormat.PaintGlyph),
                         "glyph": "glyph00011",
                         "paint": {
-                            "format": int(ot.Paint.Format.PaintSolid),
+                            "format": int(ot.PaintFormat.PaintSolid),
                             "paletteIndex": 2,
                             "alpha": 0.5,
                         },
@@ -88,26 +88,26 @@ TEST_COLOR_GLYPHS = {
         },
     ],
     "glyph00014": {
-        "format": int(ot.Paint.Format.PaintComposite),
+        "format": int(ot.PaintFormat.PaintComposite),
         "mode": "src_over",
         "source": {
-            "format": int(ot.Paint.Format.PaintColrGlyph),
+            "format": int(ot.PaintFormat.PaintColrGlyph),
             "glyph": "glyph00010",
         },
         "backdrop": {
-            "format": int(ot.Paint.Format.PaintTransform),
+            "format": int(ot.PaintFormat.PaintTransform),
             "transform": (1.0, 0.0, 0.0, 1.0, 300.0, 0.0),
             "paint": {
-                "format": int(ot.Paint.Format.PaintColrGlyph),
+                "format": int(ot.PaintFormat.PaintColrGlyph),
                 "glyph": "glyph00010",
             },
         },
     },
     "glyph00015": {
-        "format": int(ot.Paint.Format.PaintGlyph),
+        "format": int(ot.PaintFormat.PaintGlyph),
         "glyph": "glyph00011",
         "paint": {
-            "format": int(ot.Paint.Format.PaintSweepGradient),
+            "format": int(ot.PaintFormat.PaintSweepGradient),
             "colorLine": {
                 "stops": [
                     {"offset": 0.0, "paletteIndex": 3, "alpha": 1.0},
@@ -123,19 +123,19 @@ TEST_COLOR_GLYPHS = {
     },
     "glyph00016": [
         {
-            "format": int(ot.Paint.Format.PaintGlyph),
+            "format": int(ot.PaintFormat.PaintGlyph),
             "glyph": "glyph00011",
             "paint": {
-                "format": int(ot.Paint.Format.PaintSolid),
+                "format": int(ot.PaintFormat.PaintSolid),
                 "paletteIndex": 2,
                 "alpha": 0.5,
             },
         },
         {
-            "format": int(ot.Paint.Format.PaintGlyph),
+            "format": int(ot.PaintFormat.PaintGlyph),
             "glyph": "glyph00012",
             "paint": {
-                "format": int(ot.Paint.Format.PaintLinearGradient),
+                "format": int(ot.PaintFormat.PaintLinearGradient),
                 "colorLine": {
                     "stops": [
                         {"offset": 0.0, "paletteIndex": 3, "alpha": 1.0},

{'addition_count': 22, 'commit_subject': 'Merge pull request #2175 from fonttools/move-paint-format-enum', 'deletion_count': 22, 'file_extension': '.py', 'lang': 'py', 'license': 'mit', 'repo_name': 'fonttools/fonttools'}