~vono22/narau/fdroid

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
#!/usr/bin/env python
#    Copyright (C) 2012 Yvon TANGUY
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
# ---------------------------------------------------------------------------
# This script generate png images from the svg files.
# It needs inkscape and trimage to run.
#

import os
import sys
import subprocess

DIRS=['drawable-ldpi', 'drawable-mdpi', 'drawable-hdpi', 'drawable-xhdpi']
RES=[36, 48, 72, 96]

def usage():
	print('usage:')
	print(sys.argv[0] + ' [-s <dir_suffix>] [-r <res_values>] [file.svg...]')
	print('')
	print('file.svg        : list of svg files. If there is no file, it takes every svg file')
	print('                  in the current directory')
	print('-s <dir_suffix> : a suffix like v11 to create drawable-{lmhx}dpi-v11')
	print('-r <res_values> : exported png file size: 24,32,48,64')
	print('                  default values are 36,48,72,96')
	sys.exit(0)

args_n = 1
dir_suffix=''

# search for --help first
if " ".join(sys.argv).find('--help') > -1:
	usage()

if len(sys.argv) >= (args_n + 2) and sys.argv[args_n] == '-s':
	dir_suffix = '-' + sys.argv[args_n+1]
	args_n += 2
if len(sys.argv) >= (args_n + 2) and sys.argv[args_n] == '-r':
	# transform the parameter into an int array
	try:
		png_res = map(int, sys.argv[args_n+1].split(','))
	except ValueError as ve:
		print('Error: ' + str(ve))
		usage()
	if len(png_res) != 4:
		print('Error: Not enough resolutions')
		usage()
	args_n += 2
else:
	png_res = RES

start_files_args = args_n

for i in range(len(DIRS)):
	d_dir = DIRS[i] + dir_suffix
	res = str(png_res[i])

	if len(sys.argv) > start_files_args:
		files = sys.argv[start_files_args:]
	else:
		files = os.listdir('.')

	print('Gen png for ' + d_dir + ' (' + res + 'x' + res + ')...')
	subprocess.call(['/bin/mkdir', '-p', d_dir])
	for svg in files:
		if len(svg) > 4 and svg[-4:] == '.svg' and os.path.isfile(svg):
			png=d_dir + '/' + svg[:-4] + '.png'
			subprocess.call(['/usr/bin/inkscape', '-z', '-C', svg, '-e', png, '-w', res, '-h', res])
			trimage = subprocess.Popen(['/usr/bin/trimage', '-f', png])
		else:
			print('ignoring ' + svg)

if trimage:
	trimage.wait()