| 1 | from pathlib import Path |
| 2 | import sys |
| 3 | |
| 4 | from fontTools.ttLib import TTFont |
| 5 | |
| 6 | |
| 7 | RETAINED_RANGES = ( |
| 8 | (0x23FB, 0x23FE), # IEC power symbols |
| 9 | (0x2630, 0x2630), # Powerline Extra |
| 10 | (0x2665, 0x2665), # Octicons |
| 11 | (0x26A1, 0x26A1), # Octicons |
| 12 | (0x276C, 0x2771), # Nerd Fonts custom heavy brackets |
| 13 | (0x2B58, 0x2B58), # IEC power symbols |
| 14 | (0xE000, 0xE00A), # Pomicons |
| 15 | (0xE0A3, 0xE0A3), # Powerline Extra; standard Powerline ends at U+E0A2 |
| 16 | (0xE0B4, 0xE0D7), # Powerline Extra; standard Powerline ends at U+E0B3 |
| 17 | (0xE200, 0xE2A9), # Font Awesome Extension |
| 18 | (0xE300, 0xE3E3), # Weather Icons |
| 19 | (0xE5FA, 0xE6B8), # Seti-UI and Nerd Fonts custom |
| 20 | (0xE700, 0xE8EF), # Devicons |
| 21 | (0xEA60, 0xEC1E), # Codicons |
| 22 | (0xED00, 0xEFCE), # Font Awesome and Nerd Fonts progress indicators |
| 23 | (0xF000, 0xF2FF), # Font Awesome compatibility codepoints |
| 24 | (0xF400, 0xF533), # Octicons |
| 25 | (0xF0001, 0xF1AF0), # Material Design Icons |
| 26 | ) |
| 27 | |
| 28 | |
| 29 | def main() -> None: |
| 30 | if len(sys.argv) != 3: |
| 31 | raise SystemExit("usage: subset-symbols.py INPUT.ttf OUTPUT.txt") |
| 32 | |
| 33 | source = TTFont(sys.argv[1]) |
| 34 | cmap = source.getBestCmap() |
| 35 | if cmap is None: |
| 36 | raise SystemExit("input font has no Unicode cmap") |
| 37 | |
| 38 | codepoints = sorted( |
| 39 | codepoint |
| 40 | for codepoint in cmap |
| 41 | if any(start <= codepoint <= end for start, end in RETAINED_RANGES) |
| 42 | ) |
| 43 | Path(sys.argv[2]).write_text( |
| 44 | "".join(f"U+{codepoint:04X}\n" for codepoint in codepoints), encoding="ascii" |
| 45 | ) |
| 46 | |
| 47 | |
| 48 | if __name__ == "__main__": |
| 49 | main() |