Skip to content

Commit a045b48

Browse files
authored
Merge pull request #1117 from googlefonts/color-postscript-names
2 parents 9932805 + bea3f75 commit a045b48

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

Lib/glyphsLib/builder/color_layers.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ def _to_ufo_color_palette_layers(builder, master, layerMapping):
3636
layerGlyphName = f"{glyph.name}.color{i}"
3737
ufo_layer = builder.to_ufo_layer(glyph, masterLayer)
3838
ufo_glyph = ufo_layer.newGlyph(layerGlyphName)
39-
builder.to_ufo_glyph(ufo_glyph, layer, glyph)
40-
# Remove Unicode mapping from each color layer to avoid
41-
# duplicate entries.
42-
ufo_glyph.unicodes = []
39+
builder.to_ufo_glyph(ufo_glyph, layer, glyph, is_color_layer_glyph=True)
4340
colorLayers.append((layerGlyphName, colorId))
4441
layerMapping[glyph.name] = colorLayers
4542

@@ -194,10 +191,13 @@ def _to_ufo_color_layers(builder, ufo, master, layerMapping):
194191
layerGlyphName = f"{glyph.name}.color{i}"
195192
ufo_layer = builder.to_ufo_layer(glyph, masterLayer)
196193
ufo_glyph = ufo_layer.newGlyph(layerGlyphName)
197-
builder.to_ufo_glyph(ufo_glyph, layer, glyph, do_color_layers=False)
198-
# Remove Unicode mapping from each color layer to avoid
199-
# duplicate entries.
200-
ufo_glyph.unicodes = []
194+
builder.to_ufo_glyph(
195+
ufo_glyph,
196+
layer,
197+
glyph,
198+
do_color_layers=False,
199+
is_color_layer_glyph=True,
200+
)
201201

202202
attributes = layer.paths[0].attributes if layer.paths else {}
203203
if "gradient" in attributes:

Lib/glyphsLib/builder/glyph.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,19 @@ def _clone_layer(layer, paths=None, components=None):
6969
USV_EXTENSIONS = tuple(USV_MAP.keys())
7070

7171

72-
def to_ufo_glyph(self, ufo_glyph, layer, glyph, do_color_layers=True): # noqa: C901
72+
def to_ufo_glyph( # noqa: C901
73+
self, ufo_glyph, layer, glyph, do_color_layers=True, is_color_layer_glyph=False
74+
):
7375
"""Add .glyphs metadata, paths, components, and anchors to a glyph."""
7476
ufo_font = self._sources[layer.associatedMasterId or layer.layerId].font
7577

7678
if layer.layerId == layer.associatedMasterId and do_color_layers:
7779
self.to_ufo_glyph_color(ufo_glyph, layer, glyph)
7880

79-
ufo_glyph.unicodes = [int(uval, 16) for uval in glyph.unicodes]
81+
# Color layer glyphs (e.g., "Acaron.color0") should not get unicode values
82+
# from their parent glyph to avoid duplicate unicode mappings.
83+
if not is_color_layer_glyph:
84+
ufo_glyph.unicodes = [int(uval, 16) for uval in glyph.unicodes]
8085

8186
export = glyph.export
8287
if not export:

tests/builder/builder_test.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,48 @@ def test_glyph_color_palette_layers_no_unicode_mapping(ufo_module):
12381238
assert ufo["a.color1"].unicode is None
12391239

12401240

1241+
def test_glyph_color_palette_layers_postscript_names(ufo_module):
1242+
"""Test that color layer glyphs get unique postscript names with .colorN suffix."""
1243+
font = generate_minimal_font(format_version=3)
1244+
# Add a glyph with unicode (production name will be auto-generated)
1245+
glyph_with_unicode = add_glyph(font, "Acaron")
1246+
glyph_with_unicode.unicode = "01CD"
1247+
1248+
# Add a glyph without unicode but with suffix
1249+
glyph_with_suffix = add_glyph(font, "caroncomb.alt")
1250+
1251+
# Add color palette layers
1252+
for glyph in [glyph_with_unicode, glyph_with_suffix]:
1253+
color0 = GSLayer()
1254+
color1 = GSLayer()
1255+
color0.attributes["colorPalette"] = 0
1256+
color1.attributes["colorPalette"] = 1
1257+
glyph.layers.append(color0)
1258+
glyph.layers.append(color1)
1259+
1260+
ds = to_designspace(font, ufo_module=ufo_module, minimal=True)
1261+
ufo = ds.sources[0].font
1262+
1263+
postscriptNames = ufo.lib.get("public.postscriptNames", {})
1264+
1265+
# Color layer glyphs should have unique postscript names with .colorN suffix
1266+
assert postscriptNames.get("Acaron") == "uni01CD"
1267+
assert postscriptNames.get("Acaron.color0") == "uni01CD.color0"
1268+
assert postscriptNames.get("Acaron.color1") == "uni01CD.color1"
1269+
1270+
assert postscriptNames.get("caroncomb.alt") == "uni030C.alt"
1271+
assert postscriptNames.get("caroncomb.alt.color0") == "uni030C.alt.color0"
1272+
assert postscriptNames.get("caroncomb.alt.color1") == "uni030C.alt.color1"
1273+
1274+
# Color layer glyphs should not have unicode values
1275+
assert ufo["Acaron"].unicode == 0x01CD
1276+
assert ufo["Acaron.color0"].unicode is None
1277+
assert ufo["Acaron.color1"].unicode is None
1278+
assert ufo["caroncomb.alt"].unicode is None
1279+
assert ufo["caroncomb.alt.color0"].unicode is None
1280+
assert ufo["caroncomb.alt.color1"].unicode is None
1281+
1282+
12411283
def test_glyph_color_layers_components_2(ufo_module):
12421284
filename = os.path.join(
12431285
os.path.dirname(__file__), "..", "data", "ColorComponents.glyphs"

0 commit comments

Comments
 (0)