37 lines
1.6 KiB
Python
37 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Regenerate src/icondata.rs from the Material Symbols subset font (Apache 2.0) plus Mulberry-style extras.
|
|
|
|
python tools/gen_icons.py path/to/AudiphiSymbols.ttf [extra_icondata.rs]
|
|
"""
|
|
import re, sys
|
|
from fontTools.ttLib import TTFont
|
|
from fontTools.pens.svgPathPen import SVGPathPen
|
|
from fontTools.pens.transformPen import TransformPen
|
|
from fontTools.pens.boundsPen import BoundsPen
|
|
|
|
font = TTFont(sys.argv[1])
|
|
gs = font.getGlyphSet()
|
|
skip = {".notdef", "underscore"} | {f"digit_{n}" for n in "zero one two three four five six seven eight nine".split()}
|
|
out = {}
|
|
for name in font.getGlyphOrder():
|
|
if name in skip or name.startswith(".") or len(name) < 3:
|
|
continue
|
|
bp = BoundsPen(gs)
|
|
gs[name].draw(bp)
|
|
if not bp.bounds:
|
|
continue
|
|
# Google SVGs use viewBox "0 -960 960 960" with the icon box on y in [-960, 0] (y down)
|
|
pen = SVGPathPen(gs, ntos=lambda v: ("%.1f" % v).rstrip("0").rstrip("."))
|
|
tp = TransformPen(pen, (1, 0, 0, -1, 0, 96)) # flip; descent offset puts the 960 box at y in [-960, 0]
|
|
gs[name].draw(tp)
|
|
out[name.replace(".fill", "_fill")] = pen.getCommands()
|
|
if len(sys.argv) > 2:
|
|
for m in re.finditer(r'\("([a-z_0-9]+)", "([^"]*)"\)', open(sys.argv[2]).read()):
|
|
out.setdefault(m.group(1), m.group(2))
|
|
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")
|