Repository: googlefonts/glyphsLib

Issue:
Duplicate codepoint mapping when generating color fonts
The ufo2ft library recently fixed an issue where an [error was accidentally swallowed when multiple glyphs were mapped to the same codepoint](https://github.com/googlefonts/ufo2ft/pull/739).

When this fix made it downstream to fontmake, our Glyphs-based color fonts stopped building with errors:

```
Compiling UFO failed: cannot map 'A.color1' to U+0041; already mapped to 'A'
```

It seems that glyphsLib generates codepoint mappings for each glyph in a layer instead of for the base glyph only. Before I open a PR to fix it, I wanted to checked if the desired result is that the base glyph should have the codepoint mapping and any glyphs in layers should not.

Discussion:

Patch:
diff --git a/Lib/glyphsLib/builder/color_layers.py b/Lib/glyphsLib/builder/color_layers.py
index 90da6a37..e8e6d7bc 100644
--- a/Lib/glyphsLib/builder/color_layers.py
+++ b/Lib/glyphsLib/builder/color_layers.py
@@ -37,6 +37,9 @@ def _to_ufo_color_palette_layers(builder, master, layerMapping):
                 ufo_layer = builder.to_ufo_layer(glyph, masterLayer)
                 ufo_glyph = ufo_layer.newGlyph(layerGlyphName)
                 builder.to_ufo_glyph(ufo_glyph, layer, glyph)
+                # Remove Unicode mapping from each color layer to avoid
+                # duplicate entries.
+                ufo_glyph.unicodes = []
             colorLayers.append((layerGlyphName, colorId))
         layerMapping[glyph.name] = colorLayers
 


Test patch:
diff --git a/tests/builder/builder_test.py b/tests/builder/builder_test.py
index c41f92ce..af49cc4d 100644
--- a/tests/builder/builder_test.py
+++ b/tests/builder/builder_test.py
@@ -1188,6 +1188,28 @@ def test_glyph_color_layers_components(ufo_module):
     assert len(ufo.layers["color.3"]["a"]) == 0
 
 
+def test_glyph_color_palette_layers_no_unicode_mapping(ufo_module):
+    font = generate_minimal_font()
+    glypha = add_glyph(font, "a")
+
+    glypha.unicode = "0061"
+
+    color0 = GSLayer()
+    color1 = GSLayer()
+    color0.name = "Color 0"
+    color1.name = "Color 1"
+
+    glypha.layers.append(color0)
+    glypha.layers.append(color1)
+
+    ds = to_designspace(font, ufo_module=ufo_module, minimal=True)
+    ufo = ds.sources[0].font
+
+    assert ufo["a"].unicode == 97
+    assert ufo["a.color0"].unicode is None
+    assert ufo["a.color1"].unicode is None
+
+
 def test_glyph_color_palette_layers_explode(ufo_module):
     font = generate_minimal_font()
     glypha = add_glyph(font, "a")