Damson/tools/gen_icons.py
astar76 8151aa71c0 Damson: a Material You code editor in Rust
Runs Kate's syntax definitions directly, on a hand-written editor core.
Spring-driven UI with a pop-out scrolling preview, baked-in terminal
grown from a morphing button, jelly tabs, command palette, side panels,
git, language servers and hot exit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-06 17:05:28 -06:00

56 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Regenerate src/icondata.rs from the Material Symbols Rounded variable font (Apache 2.0).
python tools/gen_icons.py path/to/MaterialSymbolsRounded.ttf
"""
import sys
from fontTools.ttLib import TTFont
from fontTools.varLib import instancer
from fontTools.pens.svgPathPen import SVGPathPen
from fontTools.pens.transformPen import TransformPen
NAMES = """add arrow_back arrow_downward arrow_forward arrow_upward account_tree article bolt bookmark
bookmark_add build call_split check check_circle chevron_left chevron_right close close_fullscreen code
commit content_copy content_cut content_paste contrast dark_mode data_object delete description difference
do_not_disturb_on draft edit error expand_less expand_more find_replace folder folder_open folder_zip
format_align_left format_indent_increase format_list_bulleted fullscreen history home info insert_drive_file
keyboard_arrow_down keyboard_arrow_right keyboard_arrow_up keyboard_command_key language light_mode line_end
list looks_one manage_search map match_case memory merge minimize more_horiz more_vert motion_mode
move_up new_window open_in_full open_in_new palette play_arrow pin_drop push_pin redo refresh regular_expression
remove replace restart_alt rocket_launch save save_as search segment select_all settings
splitscreen splitscreen_right stop swap_vert sync tab tag terminal text_fields tune undo unfold_less unfold_more
upload download vertical_split view_sidebar visibility warning wrap_text""".split()
font = TTFont(sys.argv[1])
font = instancer.instantiateVariableFont(font, {"FILL": 0, "wght": 400, "GRAD": 0, "opsz": 24})
filled = TTFont(sys.argv[1])
filled = instancer.instantiateVariableFont(filled, {"FILL": 1, "wght": 400, "GRAD": 0, "opsz": 24})
def path(f, name):
gs = f.getGlyphSet()
if name not in gs:
return None
upem = f["head"].unitsPerEm
s = 960 / upem
pen = SVGPathPen(gs, ntos=lambda v: ("%.1f" % v).rstrip("0").rstrip("."))
gs[name].draw(TransformPen(pen, (s, 0, 0, -s, 0, 0)))
return pen.getCommands()
out = {}
missing = []
for n in NAMES:
p = path(font, n)
if p is None:
missing.append(n)
continue
out[n] = p
q = path(filled, n)
if q and q != p:
out[n + "_fill"] = q
with open("src/icondata.rs", "w") as f:
f.write("//! Material Symbols Rounded (Apache 2.0) path data, viewBox 0 -960 960 960. Generated by tools/gen_icons.py.\n\n")
f.write("pub const ICONS: &[(&str, &str)] = &[\n")
for k in sorted(out):
f.write(f' ("{k}", "{out[k]}"),\n')
f.write("];\n")
print(len(out), "icons; missing:", missing)