diff --git a/imgsrc/generate.py b/imgsrc/generate.py index d340bc23ee..bbac76c816 100755 --- a/imgsrc/generate.py +++ b/imgsrc/generate.py @@ -4,14 +4,14 @@ from __future__ import (unicode_literals, division, absolute_import, print_function) -import os +import os, glob, subprocess, argparse duplicates = { 'character-set': ['languages'], 'calibre': ['library', 'lt'], - 'format-text-color': 'lookfeel', + 'format-text-color': ['lookfeel'], 'books_in_series': ['series'], - 'plugins.svg': ['plugins/plugin_upgrade_ok.svg'], + 'plugins.svg': ['plugins/plugin_upgrade_ok'], } sizes = { @@ -23,13 +23,45 @@ sizes = { skip = {'calibre'} +j = os.path.join base = os.path.dirname(os.path.abspath(__file__)) +output_base = j(os.path.dirname(base), 'resources', 'images') -for src in os.listdir(base): - if src.endswith('.svg'): - name = src.rpartition('.')[0] - names = [name] + duplicates.get(name, []) - for oname in names: - if oname in skip: - continue - src = os.path.join(base, name + '.svg') + +def iterfiles(only=()): + for src in glob.glob(j(base, '*.svg')) + glob.glob(j(base, 'plugins/*.svg')): + name = os.path.relpath(src, base).rpartition('.')[0] + if only and name not in only: + continue + output_names = [n for n in [name] + duplicates.get(name, []) if n not in skip] + output_files = [j(output_base, n) + '.png' for n in output_names] + if output_files: + yield src, output_files + + +def rsvg(src, size, dest): + cmd = ['rsvg-convert', '-d', '96', '-p', '96'] + if size != 'original': + cmd += ['--width', size, '--height', size] + subprocess.check_call(cmd + ['-o', dest, src]) + subprocess.check_call(['optipng', '-o7', '-quiet', '-strip', 'all', dest]) + + +def render(src, output_files): + for dest in output_files: + oname = os.path.basename(dest).rpartition('.')[0] + size = sizes.get(oname, '128') + print('Rendering', oname, 'at size:', size) + rsvg(src, size, dest) + + +def main(): + p = argparse.ArgumentParser() + p.add_argument('only', nargs='*', default=[], help='Only render the specified icons') + args = p.parse_args() + for src, ofiles in iterfiles(args.only): + render(src, ofiles) + + +if __name__ == '__main__': + main() diff --git a/src/calibre/utils/resources.py b/src/calibre/utils/resources.py index 943775cbdf..521d07852e 100644 --- a/src/calibre/utils/resources.py +++ b/src/calibre/utils/resources.py @@ -74,19 +74,11 @@ def get_path(path, data=False, allow_user_override=True): return f.read() return fpath -if os.environ.get('CALIBRE_TEST_ICONS') == '1' and os.path.exists('/home/kovid/work/calibre/imgsrc/new'): - def get_image_path(path, data=False, allow_user_override=True): - if not path: - return get_path('images', allow_user_override=allow_user_override) - q = '/home/kovid/work/calibre/imgsrc/new/' + path[:-4] + '.svg' - if os.path.exists(q): - return q - return get_path('images/'+path, data=data, allow_user_override=allow_user_override) -else: - def get_image_path(path, data=False, allow_user_override=True): - if not path: - return get_path('images', allow_user_override=allow_user_override) - return get_path('images/'+path, data=data, allow_user_override=allow_user_override) + +def get_image_path(path, data=False, allow_user_override=True): + if not path: + return get_path('images', allow_user_override=allow_user_override) + return get_path('images/'+path, data=data, allow_user_override=allow_user_override) def js_name_to_path(name, ext='.coffee'):