| 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 |
'scene-divider': 'original', |
| 28 |
} |
| 29 |
|
| 30 |
skip = {'calibre'} |
| 31 |
|
| 32 |
j = os.path.join |
| 33 |
base = os.path.dirname(os.path.abspath(__file__)) |
| 34 |
output_base = j(os.path.dirname(base), 'resources', 'images') |
| 35 |
hash_path = j(os.path.dirname(base), '.build-cache', 'imgsrc-gen.json') |
| 36 |
if os.path.exists(hash_path): |
| 37 |
with open(hash_path, 'rb') as f: |
| 38 |
hashes = json.load(f) |
| 39 |
else: |
| 40 |
hashes = {} |
| 41 |
src_hashes = {} |
| 42 |
|
| 43 |
|
| 44 |
def iterfiles(only=()): |
| 45 |
for src in glob.glob(j(base, '*.svg')) + glob.glob(j(base, 'plugins/*.svg')): |
| 46 |
name = os.path.relpath(src, base).rpartition('.')[0] |
| 47 |
if only and name not in only: |
| 48 |
continue |
| 49 |
src_hashes[name] = h = hashlib.sha1(open(src, 'rb').read()).hexdigest() |
| 50 |
if not only and h == hashes.get(name): |
| 51 |
continue |
| 52 |
output_names = [n for n in [name] + duplicates.get(name, []) if n not in skip] |
| 53 |
output_files = [j(output_base, n) + '.png' for n in output_names] |
| 54 |
if output_files: |
| 55 |
yield src, output_files |
| 56 |
|
| 57 |
|
| 58 |
def rsvg(src, size, dest): |
| 59 |
cmd = ['rsvg-convert', '-d', '96', '-p', '96'] |
| 60 |
if size != 'original': |
| 61 |
cmd += ['--width', size, '--height', size] |
| 62 |
subprocess.check_call(cmd + ['-o', dest, src]) |
| 63 |
cmd = 'zopflipng -m -y --lossy_8bit --lossy_transparent'.split() |
| 64 |
subprocess.check_call(cmd + [dest, dest]) |
| 65 |
|
| 66 |
|
| 67 |
def render(src, output_files): |
| 68 |
for dest in output_files: |
| 69 |
oname = os.path.basename(dest).rpartition('.')[0] |
| 70 |
size = sizes.get(oname, '128') |
| 71 |
print('Rendering', oname, 'at size:', size) |
| 72 |
rsvg(src, size, dest) |
| 73 |
name = os.path.relpath(src, base).rpartition('.')[0] |
| 74 |
hashes[name] = src_hashes[name] |
| 75 |
|
| 76 |
|
| 77 |
def main(): |
| 78 |
p = argparse.ArgumentParser() |
| 79 |
p.add_argument('only', nargs='*', default=[], help='Only render the specified icons') |
| 80 |
args = p.parse_args() |
| 81 |
for src, ofiles in iterfiles(args.only): |
| 82 |
render(src, ofiles) |
| 83 |
with open(hash_path, 'w') as f: |
| 84 |
json.dump(hashes, f, indent=2, sort_keys=True) |
| 85 |
subprocess.check_call([ |
| 86 |
'inkscape', |
| 87 |
os.path.join(base, 'calibre.svg'), |
| 88 |
'--export-type=svg', |
| 89 |
'--export-plain-svg', |
| 90 |
'--export-filename', |
| 91 |
os.path.join(output_base, 'calibre.svg'), |
| 92 |
]) |
| 93 |
|
| 94 |
|
| 95 |
if __name__ == '__main__': |
| 96 |
main() |