#!/usr/bin/env python3 """ From https://www.redblobgames.com/x/2125-svg-symbols/ Copyright 2021 Red Blob Games @license Apache-2.0 Convert game-icons files to svg , like this: Before: after: """ DIR = "../../share/game-icons/" TOPFOLDER = DIR + "000000/transparent/1x1/" import re, os, argparse def make_license_map(): "Match a folder name to a screen name, for credits" folders = os.listdir(TOPFOLDER) license_map = {'various-artists': ('various artists', 'CC BY 3.0')} for line in open(DIR+"license.txt", 'r').readlines(): m = re.match(r"^- ([\w ]+),?", line) if m: author = m.group(1).strip() folder = author.lower() license = "CC BY 3.0" if "CC0" in line: license = "CC0" if author == "Lucas": folder = 'lucasms' if author == "HeavenlyDog": folder = 'heavenly-dog' if folder not in folders: folder = folder.replace(' ', '-') if folder not in folders: folder = folder.replace('-', '') if folder not in folders: print(f"License not matched: {author!r} in {line!r}") else: license_map[folder] = (author, license) return license_map def icon_inventory(): "Find all the icons" inventory = {} # {name: (author, license, filename)} name_lookup = {} # {name: [author/name, …]} for (dirname, _, filenames) in os.walk(TOPFOLDER): m = re.search(r"1x1/(.+)$", dirname) if m: author = m.group(1) (author, license) = licenses[author] for filename in filenames: name = filename.removesuffix(".svg") author_name = f"{author}/{name}" info = (author, license, dirname + "/" + filename) inventory[name] = info inventory[author_name] = info # NOTE: some names are duplicated across authors, # so I also have author/name as a key, but don't have a way # to include more than one of them in the output, as it duplicates # the id= name_lookup.setdefault(name, []).append(author_name) # if name in inventory: print(f"ERROR Duplicate icon {name}") return inventory, name_lookup licenses = make_license_map() inventory, name_lookup = icon_inventory() parser = argparse.ArgumentParser(description='Bundle game-icons SVGs into a single file.') parser.add_argument('names', metavar='name', type=str, nargs='+', help="Icon names") args = parser.parse_args() combined_svg = '\n' for name in args.names: if name in inventory: (author, license, filename) = inventory[name] svg = open(filename, 'r').read() svg = svg.replace('', '') svg = svg.replace(' fill="#000"', '') svg = re.sub(r'(viewBox=".*?">)', fr'\1', svg) if len(name_lookup[name]) > 1: combined_svg += f"\n" combined_svg += svg + "\n" else: combined_svg += f"\n" combined_svg += '\n' print(combined_svg)