Skip to content

Commit

Permalink
build extra icons pack
Browse files Browse the repository at this point in the history
  • Loading branch information
jamie-mh committed Oct 21, 2023
1 parent 233a520 commit bcb1195
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
- name: Install oxipng
run: which oxipng || cargo install oxipng

- name: Install ImageMagick
run: sudo apt-get install -y imagemagick

- name: Build packs
run: |
for builder in build_*.py; do
Expand Down
21 changes: 21 additions & 0 deletions build_extra_icons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python3

from util.basic_builder import BasicBuildSettings, build_basic_pack


def main():
settings = BasicBuildSettings()
settings.repo_url = "https://github.com/jamie-mh/AuthenticatorPro.git"
settings.png_glob = ["extraicons", "*.png"]
settings.output_name = "extra-icons"
settings.pack_name = "Extra Icons"
settings.pack_description = (
"Icons that don't fit the inclusion criteria or that were removed"
)
settings.pack_url = "https://github.com/jamie-mh/AuthenticatorPro"

build_basic_pack(settings)


if __name__ == "__main__":
main()
39 changes: 26 additions & 13 deletions util/basic_builder.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,49 @@
import glob
import shutil
import tempfile
from os import path
from typing import Optional

from util import icon_pack_pb2
from util.git import clone_repo
from util.image import render_svg_as_png, optimise_png
from util.image import render_svg_as_png, optimise_png, resize_png
from util.log import LOG
from util.pack import add_png_files_to_pack


class BasicBuildSettings:
repo_url: str
svg_glob: list[str]
svg_glob: Optional[list[str]] = None
png_glob: Optional[list[str]] = None
output_name: str
pack_name: str
pack_description: str
pack_url: str


def process_icons(settings: BasicBuildSettings, pack_dir: str, build_dir: str):
glob_path = path.join(pack_dir, *settings.svg_glob)
icons = glob.glob(glob_path)
if settings.svg_glob:
glob_path = path.join(pack_dir, *settings.svg_glob)
icons = glob.glob(glob_path)

for icon in icons:
base_name = path.basename(icon)
output_path = path.join(build_dir, base_name.replace("svg", "png"))
for icon in icons:
base_name = path.basename(icon)
output_path = path.join(build_dir, base_name.replace("svg", "png"))

with open(icon, "r") as file:
svg = file.read()
render_svg_as_png(svg, output_path)
with open(icon, "r") as file:
svg = file.read()
render_svg_as_png(svg, output_path)

optimise_png(output_path)
optimise_png(output_path)
elif settings.png_glob:
glob_path = path.join(pack_dir, *settings.png_glob)
icons = glob.glob(glob_path)

for icon in icons:
base_name = path.basename(icon)
output_path = path.join(build_dir, base_name)
resize_png(icon, output_path)
optimise_png(output_path)


def build_pack(settings: BasicBuildSettings, build_dir: str):
Expand All @@ -52,10 +65,10 @@ def build_basic_pack(settings: BasicBuildSettings):
clone_repo(settings.repo_url, pack_dir)

with tempfile.TemporaryDirectory() as build_dir:
LOG.info("Converting icons to png")
LOG.info("Processing icons")
process_icons(settings, pack_dir, build_dir)

LOG.info("Building pack")
build_pack(settings, build_dir)

LOG.info("Written to %s", settings.output_name)
LOG.info("Written to %s.iconpack", settings.output_name)
13 changes: 13 additions & 0 deletions util/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ def get_tinted_svg(source: str, colour: str) -> str:
return svg.replace("/></svg>", f' fill="#{colour}"/></svg>')


def resize_png(input_path: str, output_path: str):
convert = subprocess.run(
["convert", "-resize", f"{ICON_SIZE}x{ICON_SIZE}", input_path, output_path],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
)

if convert.stdout:
message = convert.stdout.decode("utf-8")
raise RuntimeError("Error calling convert command " + message)


def optimise_png(path: str):
optimisation = subprocess.run(
["oxipng", "-o", "4", "-i", "0", "--strip", "safe", path],
Expand Down

0 comments on commit bcb1195

Please sign in to comment.