Repository: fonttools/fonttools (Python)

Issue:
subset AssertionError
I’m getting this assertion while building `AmiriQuranColored.otf`:

```
$ make AmiriQuranColored.otf
Traceback (most recent call last):
  File "/amiri/build.py", line 556, in <module>
    makeQuran(args)
  File "/amiri/build.py", line 411, in makeQuran
    subsetter.subset(otf)
  File "/amiri/venv/lib/python3.9/site-packages/fontTools/subset/__init__.py", line 2861, in subset
    self._prune_post_subset(font)
  File "/amiri/venv/lib/python3.9/site-packages/fontTools/subset/__init__.py", line 2844, in _prune_post_subset
    retain = table.prune_post_subset(font, self.options)
  File "/amiri/venv/lib/python3.9/site-packages/fontTools/subset/__init__.py", line 2094, in prune_post_subset
    assert len(palette) == len(retained_palette_indices)
AssertionError
make: *** [Makefile:32: AmiriQuranColored.otf] Error 1
```

It seems to have been introduced in 056aba8e4afbaccc363f665c79394fbd56b4c380. My `retained_palette_indices` has one extra index, 65535, used for parts of the glyphs that take text color. I’m not sure what is the point of the assertion, but 65535 is legit color index.


Discussion:
Just remove `0xFFFF` from `retained_palette_indices` right before that assertion.



Patch:
diff --git a/Lib/fontTools/subset/__init__.py b/Lib/fontTools/subset/__init__.py
index f687b05680..f0e31c1528 100644
--- a/Lib/fontTools/subset/__init__.py
+++ b/Lib/fontTools/subset/__init__.py
@@ -2087,7 +2087,9 @@ def collect_colors_by_index(paint):
 		for record in colr.table.BaseGlyphV1List.BaseGlyphV1Record:
 			record.Paint.traverse(colr.table, collect_colors_by_index)
 
-	retained_palette_indices = set(colors_by_index.keys())
+	# don't remap palette entry index 0xFFFF, this is always the foreground color
+	# https://github.com/fonttools/fonttools/issues/2257
+	retained_palette_indices = set(colors_by_index.keys()) - {0xFFFF}
 	for palette in self.palettes:
 		palette[:] = [c for i, c in enumerate(palette) if i in retained_palette_indices]
 		assert len(palette) == len(retained_palette_indices)


Test patch:
diff --git a/Tests/subset/subset_test.py b/Tests/subset/subset_test.py
index 6fa1bf6081..91f1f95fe5 100644
--- a/Tests/subset/subset_test.py
+++ b/Tests/subset/subset_test.py
@@ -1076,7 +1076,8 @@ def colrv1_path(tmp_path):
             },
             "uniE004": [
                 ("glyph00016", 1),
-                ("glyph00017", 2),
+                ("glyph00017", 0xFFFF),  # special palette index for foreground text
+                ("glyph00018", 2),
             ],
         },
     )
@@ -1130,6 +1131,7 @@ def test_subset_COLRv1_and_CPAL(colrv1_path):
     assert "uniE004" in glyph_set
     assert "glyph00016" in glyph_set
     assert "glyph00017" in glyph_set
+    assert "glyph00018" in glyph_set
 
     assert "COLR" in subset_font
     colr = subset_font["COLR"].table
@@ -1151,10 +1153,11 @@ def test_subset_COLRv1_and_CPAL(colrv1_path):
     baseRecV0 = colr.BaseGlyphRecordArray.BaseGlyphRecord[0]
     assert baseRecV0.BaseGlyph == "uniE004"
     layersV0 = colr.LayerRecordArray.LayerRecord
-    assert len(layersV0) == 2
-    # check v0 palette indices were remapped
+    assert len(layersV0) == 3
+    # check v0 palette indices were remapped (except for 0xFFFF)
     assert layersV0[0].PaletteIndex == 0
-    assert layersV0[1].PaletteIndex == 1
+    assert layersV0[1].PaletteIndex == 0xFFFF
+    assert layersV0[2].PaletteIndex == 1
 
     assert "CPAL" in subset_font
     cpal = subset_font["CPAL"]
@@ -1208,6 +1211,7 @@ def test_subset_COLRv1_downgrade_version(colrv1_path):
         "uniE004",
         "glyph00016",
         "glyph00017",
+        "glyph00018",
     }
 
     assert "COLR" in subset_font