|
8 | 8 | import shutil |
9 | 9 | import subprocess |
10 | 10 | import sys |
| 11 | +import tempfile |
11 | 12 |
|
12 | 13 | d, j, a = (getattr(os.path, x) for x in ('dirname', 'join', 'abspath')) |
13 | 14 | base = d(a(__file__)) |
|
18 | 19 | if sys.argv[-1] == 'only-logo': |
19 | 20 | sources = {'calibre':sources['calibre']} |
20 | 21 |
|
21 | | -for name, src in sources.items(): |
22 | | - iconset = name + '.iconset' |
23 | | - if os.path.exists(iconset): |
24 | | - shutil.rmtree(iconset) |
25 | | - os.mkdir(iconset) |
26 | | - os.chdir(iconset) |
27 | | - try: |
28 | | - for sz in (16, 32, 128, 256, 512, 1024): |
29 | | - iname = f'icon_{sz}x{sz}.png' |
30 | | - iname2x = f'icon_{sz // 2}x{sz // 2}@2x.png' |
31 | | - if src.endswith('.svg'): |
32 | | - subprocess.check_call(['rsvg-convert', src, '-w', str(sz), '-h', str(sz), '-o', iname]) |
33 | | - else: |
34 | | - # We have a 512x512 png image |
35 | | - if sz == 512: |
36 | | - shutil.copy2(src, iname) |
| 22 | + |
| 23 | +def render_svg(src, sz, dest): |
| 24 | + subprocess.check_call(['rsvg-convert', src, '-w', str(sz), '-h', str(sz), '-o', dest]) |
| 25 | + |
| 26 | + |
| 27 | +with tempfile.TemporaryDirectory() as tdir: |
| 28 | + |
| 29 | + def render_frame(sz: int): |
| 30 | + f = os.path.join(tdir, f'frame-{sz}.png') |
| 31 | + if not os.path.exists(f): |
| 32 | + render_svg(j(imgsrc, 'frame.svg'), sz, f) |
| 33 | + return f |
| 34 | + |
| 35 | + def render_framed(sz: int, iname: str, shrink_factor: float = 0.76): |
| 36 | + frame = render_frame(sz) |
| 37 | + icon = os.path.join(tdir, f'icon-{sz}.png') |
| 38 | + render_svg(src, int(shrink_factor * sz), icon) |
| 39 | + subprocess.check_call(f'convert {frame} {icon} -gravity center -compose over -composite {iname}'.split()) |
| 40 | + |
| 41 | + for name, src in sources.items(): |
| 42 | + iconset = name + '.iconset' |
| 43 | + if os.path.exists(iconset): |
| 44 | + shutil.rmtree(iconset) |
| 45 | + os.mkdir(iconset) |
| 46 | + os.chdir(iconset) |
| 47 | + try: |
| 48 | + for sz in (16, 32, 128, 256, 512, 1024): |
| 49 | + iname = f'icon_{sz}x{sz}.png' |
| 50 | + iname2x = f'icon_{sz // 2}x{sz // 2}@2x.png' |
| 51 | + if sz < 128: |
| 52 | + render_svg(src, sz, iname) |
37 | 53 | else: |
38 | | - subprocess.check_call(['convert', src, '-resize', f'{sz}x{sz}', iname]) |
39 | | - if sz > 16: |
40 | | - shutil.copy2(iname, iname2x) |
41 | | - if sz > 512: |
42 | | - os.remove(iname) |
43 | | - for name in (iname, iname2x): |
44 | | - if os.path.exists(name): |
45 | | - subprocess.check_call(['optipng', '-o7', '-strip', 'all', name]) |
46 | | - finally: |
47 | | - os.chdir('..') |
| 54 | + render_framed(sz, iname) |
| 55 | + if sz > 16: |
| 56 | + shutil.copy2(iname, iname2x) |
| 57 | + if sz > 512: |
| 58 | + os.remove(iname) |
| 59 | + for name in (iname, iname2x): |
| 60 | + if os.path.exists(name): |
| 61 | + subprocess.check_call(['optipng', '-o7', '-strip', 'all', name]) |
| 62 | + finally: |
| 63 | + os.chdir('..') |
0 commit comments