forked from numixproject/numix-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.py
144 lines (130 loc) · 5.06 KB
/
gen.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/python3
# Copyright (C) 2016
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License (version 3+) as
# published by the Free Software Foundation. You should have received
# a copy of the GNU General Public License along with this program.
# If not, see <http://www.gnu.org/licenses/>.
# Sorting out modules
from json import load
from os import listdir, makedirs, path, symlink
from shutil import copy2, move, rmtree
from subprocess import PIPE, Popen, call
from io import BytesIO
from gi import require_version
require_version('Rsvg', '2.0')
try:
from gi.repository import Rsvg
import cairo
use_inkscape = False
except (ImportError, AttributeError):
ink_flag = call(['which', 'inkscape'], stdout=PIPE, stderr=PIPE)
if ink_flag == 0:
use_inkscape = True
else:
exit("Can't load cariosvg nor inkscape")
# Importing JSON
with open('data.json') as data:
icons = load(data)
# User selects the theme
theme, themes = "", listdir("icons")
while True:
theme = input("What theme would you like to build? ").lower().strip()
if theme in themes:
break
else:
print("Please enter one of the following:", ", ".join(themes), "\n")
# User selects the platform
platform, platforms = "", ["android", "linux", "osx"]
while True:
platform = input("What OS would you like to build for? ").lower().strip()
if platform in platforms:
break
else:
print("Please enter one of the following:", ", ".join(platforms), "\n")
def mkdir(dir):
if not path.exists(dir):
makedirs(dir)
def convert_svg2png(infile, outfile, w, h):
"""
Converts svg files to png using Cairosvg or Inkscape
@file_path : String; the svg file absolute path
@dest_path : String; the png file absolute path
"""
if use_inkscape:
p = Popen(["inkscape", "-f", infile, "-e", outfile,
"-w" + str(w), "-h" + str(h)],
stdout=PIPE, stderr=PIPE)
output, err = p.communicate()
else:
handle = Rsvg.Handle()
svg = handle.new_from_file(infile)
dim = svg.get_dimensions()
img = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
ctx = cairo.Context(img)
ctx.scale(w / dim.width, h / dim.height)
svg.render_cairo(ctx)
png_io = BytesIO()
img.write_to_png(png_io)
with open(outfile, 'wb') as fout:
fout.write(png_io.getvalue())
svg.close()
png_io.close()
img.finish()
# Only certain icon sizes may be covered
sizes = listdir("icons/" + theme)
# The Generation Stuff
if platform == "android":
print("\nGenerating Android theme...")
adir = "com.numix.icons_{0}/".format(theme)
adir_extra = "MainActivity22/app/src/main/res/drawable-xxhdpi/"
adir = adir + adir_extra
mkdir(adir)
for icon in icons:
for name in icons[icon].get("android", []):
name = name.replace("_", ".")
if path.exists("icons/" + theme + "/48/" + icon + ".svg"):
convert_svg2png("icons/" + theme + "/48/" + icon + ".svg",
adir + name + ".png", 192, 192)
elif platform == "linux":
print("\nGenerating Linux theme...")
ldir = "numix-icon-theme-{0}/Numix-{1}/".format(theme, theme.title())
for size in sizes:
mkdir(ldir + size + "/apps")
for icon in icons:
if "linux" in icons[icon].keys():
for size in sizes:
root = icons[icon]["linux"]["root"] + ".svg"
if path.exists("icons/" + theme + "/" + size + "/" + icon + ".svg"):
copy2("icons/" + theme + "/" + size + "/" + icon + ".svg",
ldir + size + "/apps/" + root)
for link in icons[icon]["linux"].get("symlinks", []):
try:
symlink(root,
ldir + size + "/apps/" + link + ".svg")
except FileExistsError:
continue
elif platform == "osx":
print("\nGenerating OSX theme...")
odir = "numix-{0}.icns/".format(theme)
for ext in ["icns", "pngs", "vectors"]:
mkdir(odir + ext)
try:
ink_flag = call(['which', 'png2icns'], stdout=PIPE, stderr=PIPE)
if ink_flag != 0:
raise Exception
except (FileNotFoundError, Exception):
exit("You will need png2icns in order to generate OSX theme")
for icon in icons:
for name in icons[icon].get("osx", []):
if path.exists("icons/" + theme + "/48/" + icon + ".svg"):
copy2("icons/" + theme + "48/" + icon + ".svg",
odir + "vectors/" + name + ".svg")
convert_svg2png("icons/" + theme + "/48/" + icon + ".svg",
odir + "pngs/" + name + ".png", 1024, 1024)
call(["png2icns", odir + "icns/" + name + ".icn",
odir + "pngs/" + name + ".png"],
stdout=PIPE, stderr=PIPE)
# Clean Up
print("Done!\n")
exit(0)