Debian Codegraph @

imgsrc/generate.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3
4 import argparse
5 import glob
6 import hashlib
7 import json
8 import os
9 import subprocess
10
11 duplicates = {
12 'character-set': ['languages'],
13 'calibre': ['library', 'lt', 'favicon-192', 'favicon-512'],
14 'format-text-color': ['lookfeel'],
15 'books_in_series': ['series'],
16 'plugins.svg': ['plugins/plugin_upgrade_ok'],
17 }
18
19 sizes = {
20 'lt': '256',
21 'library': '1024',
22 'default_cover': 'original',
23 'viewer': '256',
24 'tweak': '256',
25 'favicon-192': '192',
26 'favicon-512': '512',
27 }
28
29 skip = {'calibre'}
30
31 j = os.path.join
32 base = os.path.dirname(os.path.abspath(__file__))
33 output_base = j(os.path.dirname(base), 'resources', 'images')
34 hash_path = j(os.path.dirname(base), '.build-cache', 'imgsrc-gen.json')
35 if os.path.exists(hash_path):
36 with open(hash_path, 'rb') as f:
37 hashes = json.load(f)
38 else:
39 hashes = {}
40 src_hashes = {}
41
42
43 def iterfiles(only=()):
44 for src in glob.glob(j(base, '*.svg')) + glob.glob(j(base, 'plugins/*.svg')):
45 name = os.path.relpath(src, base).rpartition('.')[0]
46 if only and name not in only:
47 continue
48 src_hashes[name] = h = hashlib.sha1(open(src, 'rb').read()).hexdigest()
49 if not only and h == hashes.get(name):
50 continue
51 output_names = [n for n in [name] + duplicates.get(name, []) if n not in skip]
52 output_files = [j(output_base, n) + '.png' for n in output_names]
53 if output_files:
54 yield src, output_files
55
56
57 def rsvg(src, size, dest):
58 cmd = ['rsvg-convert', '-d', '96', '-p', '96']
59 if size != 'original':
60 cmd += ['--width', size, '--height', size]
61 subprocess.check_call(cmd + ['-o', dest, src])
62 cmd = 'zopflipng -m -y --lossy_8bit --lossy_transparent'.split()
63 subprocess.check_call(cmd + [dest, dest])
64
65
66 def render(src, output_files):
67 for dest in output_files:
68 oname = os.path.basename(dest).rpartition('.')[0]
69 size = sizes.get(oname, '128')
70 print('Rendering', oname, 'at size:', size)
71 rsvg(src, size, dest)
72 name = os.path.relpath(src, base).rpartition('.')[0]
73 hashes[name] = src_hashes[name]
74
75
76 def main():
77 p = argparse.ArgumentParser()
78 p.add_argument('only', nargs='*', default=[], help='Only render the specified icons')
79 args = p.parse_args()
80 for src, ofiles in iterfiles(args.only):
81 render(src, ofiles)
82 with open(hash_path, 'w') as f:
83 json.dump(hashes, f, indent=2, sort_keys=True)
84 subprocess.check_call([
85 'inkscape',
86 os.path.join(base, 'calibre.svg'),
87 '--export-type=svg',
88 '--export-plain-svg',
89 '--export-filename',
90 os.path.join(output_base, 'calibre.svg'),
91 ])
92
93
94 if __name__ == '__main__':
95 main()