|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Generate all required icon variants from icon.ase |
| 4 | +""" |
| 5 | +import argparse |
| 6 | +import shlex |
| 7 | +import shutil |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | + |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +ICON_DIR = Path(__file__).resolve().parent |
| 14 | +REPO_DIR = ICON_DIR.parent |
| 15 | +WORK_DIR = ICON_DIR / "work" |
| 16 | + |
| 17 | +ANDROID_DIR = REPO_DIR / "android" |
| 18 | +ASSETS_DIR = ANDROID_DIR / "assets" |
| 19 | +RES_DIR = ANDROID_DIR / "res" |
| 20 | + |
| 21 | +GPLAY_ICON = REPO_DIR / "fastlane/metadata/android/en-US/images/icon.png" |
| 22 | + |
| 23 | +MACOS_ICON = REPO_DIR / "tools/packaging/macos/pixelwheels.icns" |
| 24 | + |
| 25 | +BGCOLOR = "#5a6988" |
| 26 | + |
| 27 | +DESKTOP_ICON_SIZES = [16, 32, 48] |
| 28 | + |
| 29 | +SCALED_REFERENCE_SIZE = 64 |
| 30 | +SCALED_DESKTOP_ICON_SIZES = [128, 256, 512, 1024] |
| 31 | + |
| 32 | + |
| 33 | +def run(*args): |
| 34 | + cmd = [str(x) for x in args] |
| 35 | + cmd_str = " \\\n ".join(shlex.quote(x) for x in cmd) |
| 36 | + res = subprocess.run(cmd) |
| 37 | + if res.returncode != 0: |
| 38 | + print(f"The following command failed with exit code {res.returncode}:") |
| 39 | + print(cmd_str) |
| 40 | + sys.exit(1) |
| 41 | + |
| 42 | + |
| 43 | +def aseprite(*args): |
| 44 | + cmd = ["aseprite", "--batch", ICON_DIR / "icon.ase"] + list(args) |
| 45 | + run(*cmd) |
| 46 | + |
| 47 | + |
| 48 | +def generate_macos(): |
| 49 | + for size in DESKTOP_ICON_SIZES: |
| 50 | + aseprite("--slice", f"icon-{size}", |
| 51 | + "--save-as", WORK_DIR / f"icon-{size}.png") |
| 52 | + |
| 53 | + for size in SCALED_DESKTOP_ICON_SIZES: |
| 54 | + scale = int(size / SCALED_REFERENCE_SIZE) |
| 55 | + aseprite("--slice", f"icon-{SCALED_REFERENCE_SIZE}", |
| 56 | + "--scale", scale, |
| 57 | + "--save-as", WORK_DIR / f"icon-{size}.png") |
| 58 | + |
| 59 | + run("png2icns", MACOS_ICON, *WORK_DIR.glob("*.png")) |
| 60 | + |
| 61 | + |
| 62 | +def generate_android(): |
| 63 | + """TODO: port a fixed version once the adaptive issue is sorted out. |
| 64 | +
|
| 65 | + See https://github.com/agateau/pixelwheels/issues/198 |
| 66 | +
|
| 67 | + Original code: |
| 68 | +
|
| 69 | + # mdpi & xhdpi |
| 70 | + aseprite --batch icon.ase --slice icon-48 \ |
| 71 | + --save-as $RES_DIR/drawable-mdpi/ic_launcher.png \ |
| 72 | + --scale 2 --save-as $RES_DIR/drawable-xhdpi/ic_launcher.png |
| 73 | +
|
| 74 | + # hdpi & xxhdpi |
| 75 | + aseprite --batch icon.ase --slice icon-72 \ |
| 76 | + --save-as $RES_DIR/drawable-hdpi/ic_launcher.png \ |
| 77 | + --scale 2 --save-as $RES_DIR/drawable-xxhdpi/ic_launcher.png |
| 78 | + """ |
| 79 | + pass |
| 80 | + |
| 81 | + |
| 82 | +def generate_android_tv(): |
| 83 | + aseprite("--slice", "tv-banner", |
| 84 | + "--scale", 2, |
| 85 | + "--save-as", RES_DIR / "drawable-xhdpi/tv_banner.png") |
| 86 | + |
| 87 | + |
| 88 | +def generate_desktop_assets(): |
| 89 | + aseprite("--slice", "icon-72", |
| 90 | + "--scale", 2, |
| 91 | + "--save-as", ASSETS_DIR / "desktop-icon/desktop-icon.png") |
| 92 | + |
| 93 | + |
| 94 | +def generate_gplay(): |
| 95 | + # Google Play: 512 x 512 RVB, flat |
| 96 | + |
| 97 | + # Start from the biggest, unscaled version |
| 98 | + tmp_image = WORK_DIR / "gplay-tmp.png" |
| 99 | + aseprite("--slice", "icon-72", "--save-as", tmp_image) |
| 100 | + |
| 101 | + run("convert", "-scale", "512x512", tmp_image, |
| 102 | + "-background", BGCOLOR, "-flatten", GPLAY_ICON) |
| 103 | + |
| 104 | + |
| 105 | +def main(): |
| 106 | + parser = argparse.ArgumentParser( |
| 107 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 108 | + description=__doc__) |
| 109 | + |
| 110 | + parser.add_argument("-k", "--keep", action="store_true", |
| 111 | + help="Keep work dir") |
| 112 | + |
| 113 | + parser.add_argument("targets", nargs="*") |
| 114 | + |
| 115 | + args = parser.parse_args() |
| 116 | + |
| 117 | + if WORK_DIR.exists(): |
| 118 | + shutil.rmtree(WORK_DIR) |
| 119 | + WORK_DIR.mkdir() |
| 120 | + |
| 121 | + try: |
| 122 | + if args.targets: |
| 123 | + targets = args.targets |
| 124 | + else: |
| 125 | + prefix = "generate_" |
| 126 | + targets = [k[len(prefix):] for k in globals() if k.startswith(prefix)] |
| 127 | + |
| 128 | + _generate_work_icons() |
| 129 | + |
| 130 | + for target in targets: |
| 131 | + print(f"== {target} ==") |
| 132 | + target_fn = eval(f"generate_{target}") |
| 133 | + target_fn() |
| 134 | + finally: |
| 135 | + if not args.keep: |
| 136 | + shutil.rmtree(WORK_DIR) |
| 137 | + |
| 138 | + return 0 |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == "__main__": |
| 142 | + sys.exit(main()) |
| 143 | +# vi: ts=4 sw=4 et |
0 commit comments