-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
110 lines (89 loc) · 2.82 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from concurrent.futures import ThreadPoolExecutor
import json
import os
from pathlib import Path
import subprocess
from typing import NamedTuple
import svg
with open("config.json", encoding="utf8") as f:
CFG = json.load(f)
STYLE = """
text {
font-family: Oswald;
}
"""
STYLE_ELEMENT = svg.Defs(
elements=[
svg.Style(type="text/css", text=STYLE),
],
)
background = lambda color: svg.Rect(x=0, y=0, width=500, height=500, fill=color)
primary = lambda text, y, size: svg.Text(
fill="#fff",
x=250,
y=y,
font_size=size,
font_weight="bold",
dominant_baseline="central",
text_anchor="middle",
text=text,
)
secondary = lambda text : svg.Text(
fill="#fff",
opacity=0.7,
x=25,
y=100,
font_size=125,
dominant_baseline="central",
text=text,
)
primary_size = lambda text : CFG["TEXT_SIZES"][text] if text in CFG["TEXT_SIZES"] else 250
def create_icon(primary_text: str, seconday_text: str, color: str) -> svg.SVG:
elements: list[svg.Element] = [STYLE_ELEMENT, background(color)]
primary_y = 250
if seconday_text != "":
primary_y = 300
elements.append(secondary(seconday_text))
elements.append(primary(primary_text, primary_y, primary_size(primary_text)))
return svg.SVG(
viewBox=svg.ViewBoxSpec(0, 0, 500, 500),
elements=elements,
)
def raster(folder: Path, filename: str):
subprocess.run([
"resvg",
"--width", "500",
"--height", "500",
folder.joinpath(filename + ".svg"),
folder.joinpath(filename + ".png"),
], capture_output=True)
class Entry(NamedTuple):
category: str
subcategory: str
data: list[str]
def process(entry: Entry):
data = entry.data
if len(entry.data) < 2:
return
for color_name, color in CFG["COLORS"].items():
name = data[0]
folder = Path("icons").joinpath(entry.category).joinpath(entry.subcategory).joinpath(color_name)
os.makedirs(folder, exist_ok=True)
secondary_text = data[2] if len(data) == 3 else ""
svg_data = str(create_icon(data[1], secondary_text, "#" + color))
filename = folder.joinpath(name + ".svg")
with open(filename, "w", encoding="utf8", newline="\n") as f:
f.write(svg_data)
raster(folder, name)
os.remove(filename)
print(f"{name}: {color_name}")
def main():
entries: list[Entry] = []
for category, subcategories in CFG["ICONS"].items():
for subcategory, entry_list in subcategories.items():
for entry_data in entry_list:
entries.append(Entry(category, subcategory, entry_data))
with ThreadPoolExecutor(max_workers=5) as executor:
executor.map(process, entries)
if __name__ == "__main__":
main()