83 lines
3.5 KiB
Python
Executable file
83 lines
3.5 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""Rebuild the embedded font and icons from the fonts installed on this PC. Run from anywhere; needs fontTools.
|
|
|
|
python3 tools/gen_assets.py [font|icons]
|
|
|
|
font assets/Rubik.ttf: Rubik (OFL) with its weight axis, unused scripts dropped, tabular digits as default
|
|
icons crates/app/src/icondata.rs: Material Symbols Rounded (Apache 2.0) path data for the names in tools/icons.txt
|
|
(one name per line; `name_fill` comes along by itself)
|
|
"""
|
|
import os
|
|
import sys
|
|
from fontTools import subset
|
|
from fontTools.pens.svgPathPen import SVGPathPen
|
|
from fontTools.pens.transformPen import TransformPen
|
|
from fontTools.ttLib import TTFont
|
|
from fontTools.varLib import instancer
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
FONTS = "/usr/share/fonts/TTF/"
|
|
|
|
|
|
def find(name):
|
|
for spelling in (name, name.replace("[", "%5B").replace("]", "%5D")):
|
|
if os.path.exists(FONTS + spelling):
|
|
return FONTS + spelling
|
|
sys.exit(f"{name} is not installed in {FONTS}")
|
|
|
|
|
|
def font():
|
|
f = TTFont(find("Rubik[wght].ttf"))
|
|
tnum = {}
|
|
for rec in f["GSUB"].table.FeatureList.FeatureRecord:
|
|
if rec.FeatureTag == "tnum":
|
|
for i in rec.Feature.LookupListIndex:
|
|
for sub in f["GSUB"].table.LookupList.Lookup[i].SubTable:
|
|
tnum.update(getattr(sub, "ExtSubTable", sub).mapping)
|
|
for table in f["cmap"].tables:
|
|
if table.isUnicode():
|
|
for code, glyph in table.cmap.items():
|
|
if 0x30 <= code <= 0x39:
|
|
table.cmap[code] = tnum.get(glyph, glyph)
|
|
opts = subset.Options(layout_features=["kern", "liga", "mark", "mkmk", "ccmp", "locl"], notdef_outline=True, name_IDs=["*"])
|
|
s = subset.Subsetter(opts)
|
|
# Latin, Greek, Cyrillic, punctuation, currency, arrows, maths signs.
|
|
ranges = [(0x20, 0x52F), (0x1E00, 0x1EFF), (0x2000, 0x22FF), (0x25A0, 0x25FF), (0xFB00, 0xFB06)]
|
|
s.populate(unicodes=[c for a, b in ranges for c in range(a, b + 1)])
|
|
s.subset(f)
|
|
widths = {f["hmtx"][f.getBestCmap()[c]][0] for c in range(0x30, 0x3A)}
|
|
assert len(widths) == 1, widths
|
|
out = f"{ROOT}/assets/Rubik.ttf"
|
|
f.save(out)
|
|
print(out, os.path.getsize(out), "bytes")
|
|
|
|
|
|
def icons():
|
|
names = open(f"{ROOT}/tools/icons.txt").read().split()
|
|
src = find("MaterialSymbolsRounded[FILL,GRAD,opsz,wght].ttf")
|
|
out, missing = {}, []
|
|
for fill, suffix in ((0, ""), (1, "_fill")):
|
|
f = instancer.instantiateVariableFont(TTFont(src), {"FILL": fill, "wght": 400, "GRAD": 0, "opsz": 24})
|
|
glyphs, s = f.getGlyphSet(), 960 / f["head"].unitsPerEm
|
|
for n in names:
|
|
if n not in glyphs:
|
|
missing.append(n)
|
|
continue
|
|
pen = SVGPathPen(glyphs, ntos=lambda v: ("%.1f" % v).rstrip("0").rstrip("."))
|
|
glyphs[n].draw(TransformPen(pen, (s, 0, 0, -s, 0, 0)))
|
|
if pen.getCommands() != out.get(n):
|
|
out[n + suffix] = pen.getCommands()
|
|
if missing:
|
|
sys.exit(f"no such icons: {sorted(set(missing))}")
|
|
with open(f"{ROOT}/crates/app/src/icondata.rs", "w") as f:
|
|
f.write("//! Material Symbols Rounded (Apache 2.0) path data, viewBox 0 -960 960 960. Made by tools/gen_assets.py, sorted.\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")
|
|
|
|
|
|
what = sys.argv[1:] or ["font", "icons"]
|
|
for w in what:
|
|
{"font": font, "icons": icons}[w]()
|