| 1 | """Keep a CBDT Noto Color Emoji face loadable by GPUI. |
| 2 | |
| 3 | GPUI drops a requested family when the face has no glyph for "m", and it |
| 4 | only paints color bitmaps for the PostScript name NotoColorEmoji. Cosmic Text |
| 5 | on Linux falls back to the family name Noto Color Emoji. This script leaves |
| 6 | both names alone and maps U+006D onto an existing glyph so the face survives |
| 7 | that check. The system COLRv1 face of the same name is hidden at runtime. |
| 8 | """ |
| 9 | |
| 10 | import sys |
| 11 | from fontTools.ttLib import TTFont |
| 12 | |
| 13 | SOURCE = sys.argv[1] |
| 14 | DEST = sys.argv[2] |
| 15 | |
| 16 | |
| 17 | def add_latin_m(font: TTFont) -> None: |
| 18 | cmap = font.getBestCmap() |
| 19 | if not cmap: |
| 20 | raise SystemExit("emoji font has no unicode cmap") |
| 21 | glyph = next(iter(cmap.values())) |
| 22 | for table in font["cmap"].tables: |
| 23 | if table.isUnicode(): |
| 24 | table.cmap[ord("m")] = glyph |
| 25 | if font.getGlyphID(glyph) == 0: |
| 26 | raise SystemExit("mapped m onto .notdef") |
| 27 | |
| 28 | |
| 29 | def main() -> None: |
| 30 | font = TTFont(SOURCE) |
| 31 | if "CBDT" not in font or "CBLC" not in font: |
| 32 | raise SystemExit("source is not a CBDT/CBLC color font") |
| 33 | if "COLR" in font: |
| 34 | raise SystemExit("source also has a COLR table") |
| 35 | add_latin_m(font) |
| 36 | font.save(DEST) |
| 37 | saved = TTFont(DEST) |
| 38 | names = {record.nameID: record.toUnicode() for record in saved["name"].names if record.nameID in (1, 6)} |
| 39 | if names.get(1) != "Noto Color Emoji" or names.get(6) != "NotoColorEmoji": |
| 40 | raise SystemExit(f"unexpected names: {names}") |
| 41 | if ord("m") not in (saved.getBestCmap() or {}): |
| 42 | raise SystemExit("cmap is missing m") |
| 43 | print("wrote", DEST) |
| 44 | |
| 45 | |
| 46 | if __name__ == "__main__": |
| 47 | main() |