| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
|
| 4 |
__license__ = 'GPL v3' |
| 5 |
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>' |
| 6 |
|
| 7 |
import json |
| 8 |
import os |
| 9 |
import shutil |
| 10 |
import subprocess |
| 11 |
import sys |
| 12 |
import xml.etree.ElementTree as ET |
| 13 |
from copy import deepcopy |
| 14 |
|
| 15 |
d, j, a = (getattr(os.path, x) for x in ('dirname', 'join', 'abspath')) |
| 16 |
base = d(a(__file__)) |
| 17 |
|
| 18 |
# To generate this template create an icon using Icon Composer on macOS and in |
| 19 |
# the saved .icon (which is a folder) look for icon.js |
| 20 |
icon_settings = { |
| 21 |
'fill-specializations': [ |
| 22 |
{'value': {'automatic-gradient': 'extended-gray:1.00000,1.00000'}}, |
| 23 |
{'appearance': 'dark', 'value': {'automatic-gradient': 'display-p3:0.20500,0.20500,0.20500,1.00000'}}, |
| 24 |
], |
| 25 |
'groups': [ |
| 26 |
{ |
| 27 |
'layers': [ |
| 28 |
{ |
| 29 |
'blend-mode': 'normal', |
| 30 |
'glass': False, |
| 31 |
'hidden': False, |
| 32 |
'image-name': 'icon.svg', |
| 33 |
'name': 'icon', |
| 34 |
'opacity': 1, |
| 35 |
'position': {'scale': 0.9, 'translation-in-points': [0, 0]}, |
| 36 |
} |
| 37 |
], |
| 38 |
'shadow': {'kind': 'neutral', 'opacity': 0.5}, |
| 39 |
'translucency': {'enabled': True, 'value': 0.5}, |
| 40 |
} |
| 41 |
], |
| 42 |
'supported-platforms': {'circles': ['watchOS'], 'squares': 'shared'}, |
| 43 |
} |
| 44 |
|
| 45 |
imgsrc = j(d(d(base)), 'imgsrc') |
| 46 |
sources = {'calibre': j(imgsrc, 'calibre.svg'), 'ebook-edit': j(imgsrc, 'tweak.svg'), 'ebook-viewer': j(imgsrc, 'viewer.svg'), 'book': j(imgsrc, 'book.svg')} |
| 47 |
|
| 48 |
frames = { |
| 49 |
'light': j(imgsrc, 'frame.svg'), |
| 50 |
'dark': j(imgsrc, 'frame-dark.svg'), |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
def get_svg_viewbox(file_path: str) -> tuple[float, ...]: |
| 55 |
tree = ET.parse(file_path) |
| 56 |
root = tree.getroot() |
| 57 |
viewbox = root.get('viewBox') |
| 58 |
if viewbox: |
| 59 |
return tuple(float(x) for x in viewbox.split()) |
| 60 |
width = root.get('width') |
| 61 |
height = root.get('height') |
| 62 |
return (0.0, 0.0, float(width or 0), float(height or 0)) |
| 63 |
|
| 64 |
|
| 65 |
def create_icon(name: str, svg_path: str, output_path: str) -> str: |
| 66 |
view_box = get_svg_viewbox(svg_path) |
| 67 |
sz = view_box[-1] |
| 68 |
scale = 0.9 * 1024 / sz |
| 69 |
icon_dir = os.path.join(output_path, f'{name}.icon') |
| 70 |
if os.path.exists(icon_dir): |
| 71 |
shutil.rmtree(icon_dir) |
| 72 |
os.mkdir(icon_dir) |
| 73 |
s = deepcopy(icon_settings) |
| 74 |
for group in s['groups']: |
| 75 |
assert isinstance(group, dict) |
| 76 |
for layer in group['layers']: |
| 77 |
assert isinstance(layer, dict) |
| 78 |
layer['image-name'] = os.path.basename(svg_path) |
| 79 |
layer['name'] = name |
| 80 |
assert isinstance(layer['position'], dict) |
| 81 |
layer['position']['scale'] = scale |
| 82 |
with open(os.path.join(icon_dir, 'icon.json'), 'w') as f: |
| 83 |
json.dump(s, f, indent=2) |
| 84 |
assets_dir = os.path.join(icon_dir, 'Assets') |
| 85 |
os.mkdir(assets_dir) |
| 86 |
shutil.copy(svg_path, assets_dir) |
| 87 |
return icon_dir |
| 88 |
|
| 89 |
|
| 90 |
def create_assets(): |
| 91 |
os.chdir(base) |
| 92 |
actool = [ |
| 93 |
'xcrun', |
| 94 |
'actool', |
| 95 |
'--warnings', |
| 96 |
'--platform', |
| 97 |
'macosx', |
| 98 |
'--compile', |
| 99 |
'.', |
| 100 |
'--minimum-deployment-target', |
| 101 |
'15.0', |
| 102 |
'--output-partial-info-plist', |
| 103 |
'/dev/stdout', |
| 104 |
] |
| 105 |
primary = '' |
| 106 |
icons = [] |
| 107 |
alternates = [] |
| 108 |
for name, svg in sources.items(): |
| 109 |
if not primary: |
| 110 |
primary = name |
| 111 |
icons.append(create_icon(name, svg, os.getcwd())) |
| 112 |
if name != primary: |
| 113 |
alternates.extend(('--alternate-app-icon', name)) |
| 114 |
# Generate .icns for backwards compat |
| 115 |
subprocess.check_call(actool + ['--app-icon', name, icons[-1]]) |
| 116 |
os.remove('Assets.car') |
| 117 |
subprocess.check_call(actool + ['--app-icon', primary] + alternates + icons) |
| 118 |
for x in icons: |
| 119 |
shutil.rmtree(x) |
| 120 |
|
| 121 |
|
| 122 |
def main(): |
| 123 |
if 'darwin' in sys.platform.lower(): |
| 124 |
create_assets() |
| 125 |
else: |
| 126 |
subprocess.check_call(['ssh', 'ox', 'sh', '-c', '~/bin/update-calibre && python3 ~/calibre-src/icons/icns/make_iconsets.py']) |
| 127 |
subprocess.check_call(['rsync', '-avz', '--include=*.icns', '--include=*.car', '--exclude=*', 'ox:~/calibre-src/icons/icns/', base + '/']) |
| 128 |
|
| 129 |
|
| 130 |
if __name__ == '__main__': |
| 131 |
main() |