SPB Git

Repository Public

icons.py 12.3 KB 217 lines python

1#!/usr/bin/env python32# =============================================================================3#                  SP BOUCHER | SOFTWARE & RESEARCH4#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# PROJECT METADATA8# -----------------------------------------------------------------------------9# Project          : KA Maps Engine10# File             : themes/tools/icons.py11# Description      : Jeu d'icônes propriétaire KA Maps : sources SVG,12#                    manifeste, atlas SDF et planche d'aperçu.13# Module           : themes14# Repository       : https://git.spboucher.ai/ka-maps-engine15# Created          : 2026-12-0116# Last Modified    : 2026-12-0117#18# =============================================================================19# AUTHOR & PROFESSIONAL AFFILIATION20# =============================================================================21#22# Author           : Simon-Pierre Boucher23# Position         : Professor of Finance24# Institution      : Université du Québec en Outaouais (UQO)25# Department       : Département des sciences administratives26# Organization     : SP Boucher27#28# Website          : https://spboucher.ai29# Git              : https://git.spboucher.ai30#31# =============================================================================32# OFFICIAL CONTACT DIRECTORY33# =============================================================================34#35# General          : info@spboucher.ai36# Professional     : contact@spboucher.ai37# Direct           : simon@spboucher.ai38# Research         : research@spboucher.ai39# Development      : dev@spboucher.ai40# Support          : support@spboucher.ai41# Security/Admin   : admin@spboucher.ai42# Notifications    : notifications@spboucher.ai43#44# Preferred Contact: support@spboucher.ai45#46# =============================================================================47# COPYRIGHT & INTELLECTUAL PROPERTY48# =============================================================================49#50# Copyright (c) 202651# Simon-Pierre Boucher52#53# All rights reserved where applicable.54#55# This file is governed by the applicable project license,56# copyright notices, institutional agreements and relevant law.57#58# Unless expressly authorized by the applicable license or59# rights holder, copying, redistribution, sublicensing,60# publication and commercial exploitation are not permitted.61#62# Existing third-party rights and notices remain unaffected.63#64# =============================================================================65# CONFIDENTIALITY & SECURITY66# =============================================================================67#68# Classification   : Public69# Distribution     : Public repository; use is governed by the project license70# Security Contact : admin@spboucher.ai71#72# Access to this source does not independently grant permission73# to redistribute or disclose confidential materials.74#75# Do not embed credentials, private keys, access tokens,76# certificates or confidential personal information.77#78# =============================================================================79# LICENSE & MAINTENANCE80# =============================================================================81#82# License          : None specified (no license file in this repository)83# Maintainer       : Simon-Pierre Boucher84# Technical Contact: dev@spboucher.ai85#86# =============================================================================87# END OF SP BOUCHER SOURCE HEADER88# =============================================================================8990"""Jeu d'icônes propriétaire KA Maps : sources SVG, manifeste, atlas SDF et planche d'aperçu.9192Dessin maison (aucune reprise de Maki, Material ou Font Awesome). Règles du jeu :93  - grille 24 × 24, zone utile 20 × 20 (marge de 2) ;94  - un seul trait de 2 unités, extrémités et angles ronds ; aplats seulement pour un petit détail95    plein (poignée, phare) ; monochrome (`currentColor`), la teinte vient du thème ;96  - géométrie élémentaire : droites, arcs de cercle, angles à 45° ou 90° ;97  - lisible à 12 px : pas plus de trois idées par icône.9899    python3 themes/tools/icons.py     # écrit themes/icons/*.svg, manifest.json, atlas/, apercu.png100101Atlas : `themes/icons/atlas/ka-icons.png` (niveaux de gris, champ de distance signé) +102`ka-icons.json`. Même convention que l'atlas de glyphes (`ka-glyphs/1`) : 0,5 = contour,1030 et 1 à `spread_px` pixels. Dépendances : rsvg-convert, Pillow, numpy, scipy.104"""105import io106import json107import os108import subprocess109110import numpy as np111from PIL import Image, ImageDraw, ImageFont112from scipy import ndimage113114ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))115OUT = os.path.join(ROOT, "icons")116ICON_PX, SPREAD_PX = 36, 6117CELL = ICON_PX + 2 * SPREAD_PX118119S = 'fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"'120F = 'fill="currentColor" stroke="none"'121122# nom : (titre, corps SVG, genres ka1 servis : "poi:…", "transit:…", "marker:…")123ICONS = {124    "university": ("Université", f'<path {S} d="M3 9.5 12 4l9 5.5H3z"/><path {S} d="M6.5 12.5v5M12 12.5v5M17.5 12.5v5M4 20.5h16"/>', ["poi:university"]),125    "college": ("Cégep, collège", f'<path {S} d="M4 20.5V10l8-5.5 8 5.5v10.5"/><path {S} d="M9.5 20.5V16a2.5 2.5 0 0 1 5 0v4.5M3 20.5h18"/>', ["poi:college"]),126    "school": ("École", f'<path {S} d="M5 20.5V12l7-3.5 7 3.5v8.5H5z"/><path {S} d="M12 8.5V3h4.5l-1.2 1.6 1.2 1.6H12"/><path {S} d="M12 20.5V16"/>', ["poi:school"]),127    "hospital": ("Hôpital", f'<rect {S} x="3.5" y="3.5" width="17" height="17" rx="3.5"/><path {S} d="M12 8v8M8 12h8"/>', ["poi:hospital"]),128    "clinic": ("Clinique", f'<circle {S} cx="12" cy="12" r="8.5"/><path {S} d="M12 8.5v7M8.5 12h7"/>', ["poi:clinic"]),129    "pharmacy": ("Pharmacie", f'<rect {S} x="3" y="8" width="18" height="8" rx="4" transform="rotate(-45 12 12)"/><path {S} d="M9.2 9.2l5.6 5.6"/>', ["poi:pharmacy"]),130    "grocery": ("Épicerie", f'<path {S} d="M3.5 10h17l-2 9.5h-13l-2-9.5z"/><path {S} d="M8 10l4-6 4 6M9.5 13.5v3M14.5 13.5v3"/>', ["poi:grocery"]),131    "mall": ("Centre commercial", f'<path {S} d="M5 8h14v12.5H5V8z"/><path {S} d="M9 11V7a3 3 0 0 1 6 0v4"/>', ["poi:mall"]),132    "park": ("Parc", f'<circle {S} cx="12" cy="9" r="5.5"/><path {S} d="M12 9v12M12 13l3-2.5M8 21h8"/>', ["poi:park"]),133    "library": ("Bibliothèque", f'<path {S} d="M12 7.5v12.5M12 7.5C10 6 6.5 5.4 3.5 6v12.5c3-.6 6.5 0 8.5 1.5M12 7.5c2-1.5 5.5-2.1 8.5-1.5v12.5c-3-.6-6.5 0-8.5 1.5"/>', ["poi:library"]),134    "metro": ("Métro", f'<circle {S} cx="12" cy="12" r="9"/><path {S} d="M8 17v-5.5a4 4 0 0 1 8 0V17M8 17h8"/>', ["transit:metro"]),135    "rem": ("REM, train léger", f'<rect {S} x="5" y="3" width="14" height="14" rx="4.5"/><path {S} d="M5 10h14M8.5 17l-2 4M15.5 17l2 4"/><circle {F} cx="9" cy="13.5" r="1.1"/><circle {F} cx="15" cy="13.5" r="1.1"/>', ["transit:rem"]),136    "train": ("Gare, train", f'<rect {S} x="5.5" y="3" width="13" height="14.5" rx="2"/><path {S} d="M8.5 6.5h7v4h-7v-4zM8.5 17.5 7 21M15.5 17.5 17 21M6 21h12"/><circle {F} cx="12" cy="14" r="1.2"/>', ["transit:train", "poi:station"]),137    "bus-terminal": ("Terminus d'autobus", f'<rect {S} x="4.5" y="4" width="15" height="13" rx="2.5"/><path {S} d="M4.5 11.5h15M8 17v3.5M16 17v3.5"/><circle {F} cx="8" cy="14.3" r="1"/><circle {F} cx="16" cy="14.3" r="1"/>', ["transit:bus_terminal"]),138    "airport": ("Aéroport", f'<path {S} d="M12 3v18M12 9.5 3.5 14.5M12 9.5l8.5 5M9 21l3-2.2 3 2.2"/>', ["poi:airport"]),139    "ferry": ("Traversier", f'<path {S} d="M3 14.5h18l-3 5.5H6l-3-5.5z"/><path {S} d="M7.5 14.5V9h9v5.5M12 9V5"/>', ["road:ferry"]),140    "cycleway": ("Piste cyclable", f'<circle {S} cx="6.5" cy="16" r="3.5"/><circle {S} cx="17.5" cy="16" r="3.5"/><path {S} d="M6.5 16 10 8.5h4.5l3 7.5M10 8.5l4 7.5M8.5 8.5H11"/>', ["road:cycleway"]),141    "parking": ("Stationnement", f'<rect {S} x="3.5" y="3.5" width="17" height="17" rx="3.5"/><path {S} d="M9.5 17V7.5h3.2a2.8 2.8 0 0 1 0 5.6H9.5"/>', ["landuse:parking"]),142    "summit": ("Sommet", f'<path {S} d="M3 19.5 10 6l4 7.5 2.5-3.5 4.5 9.5H3z"/>', ["terrain:summit"]),143    "pin": ("Repère", f'<circle {S} cx="12" cy="9.5" r="5.5"/><path {S} d="M12 15v6"/><circle {F} cx="12" cy="9.5" r="1.6"/>', ["marker:generic"]),144    "door": ("Lou-Ka — la porte", f'<path {S} d="M6.5 21V3.5h11V21M4 21h16"/><path {S} d="M10 21V7.5l4-1.2"/><circle {F} cx="14.6" cy="13" r="1.1"/>', ["marker:louka"]),145    "house": ("Immo-Ka — la maison", f'<path {S} d="M4 11.5 12 4l8 7.5V20.5H4v-9z"/><rect {F} x="10.4" y="13.5" width="3.2" height="7" rx=".4"/>', ["marker:immoka"]),146    "plumb": ("Vrai-Prix — le fil à plomb", f'<path {S} d="M12 2.5v9M5 2.5h14"/><path {S} d="M8.5 11.5h7L12 21l-3.5-9.5z"/>', ["marker:vraiprix"]),147    "plex": ("Valo-Plex — l'élévation", f'<path {S} d="M5.5 21V4h13v17M3 21h18M5.5 9.7h13M5.5 15.3h13"/><path {S} d="M10.5 21v-2.8h3V21"/>', ["marker:valoplex"]),148}149150151def svg(name):152    title, body, _ = ICONS[name]153    return (f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">\n'154            f'  <!-- KA Maps — {title}. Grille 24, trait 2, monochrome. Dessin original, généré par themes/tools/icons.py. -->\n'155            f'  <title>{title}</title>\n  {body}\n</svg>\n')156157158def raster(name, px, color="#000000"):159    src = svg(name).replace("currentColor", color)160    png = subprocess.run(["rsvg-convert", "-w", str(px), "-h", str(px)], input=src.encode(), capture_output=True, check=True).stdout161    return Image.open(io.BytesIO(png)).convert("RGBA")162163164def sdf(name):165    """Champ de distance signé d'une icône : rendu ×8, distance euclidienne, réduction à la cellule."""166    k = 8167    big = np.zeros((CELL * k, CELL * k), dtype=bool)168    a = np.asarray(raster(name, ICON_PX * k))[:, :, 3] > 127169    o = SPREAD_PX * k170    big[o:o + ICON_PX * k, o:o + ICON_PX * k] = a171    d = ndimage.distance_transform_edt(big) - ndimage.distance_transform_edt(~big)172    d = d.reshape(CELL, k, CELL, k).mean(axis=(1, 3)) / k  # px de cellule, positif à l'intérieur173    return np.clip(0.5 + d / (2 * SPREAD_PX), 0, 1)174175176def main():177    os.makedirs(os.path.join(OUT, "atlas"), exist_ok=True)178    names = list(ICONS)179    for n in names:180        open(os.path.join(OUT, f"{n}.svg"), "w").write(svg(n))181    manifest = {"format": "ka-icon-set/1", "grid": 24, "stroke": 2, "safe_area": 20,182                "icons": {n: {"title": ICONS[n][0], "file": f"{n}.svg", "kinds": ICONS[n][2]} for n in names}}183    json.dump(manifest, open(os.path.join(OUT, "manifest.json"), "w"), ensure_ascii=False, indent=1)184    cols = 8185    rows = (len(names) + cols - 1) // cols186    atlas = np.zeros((rows * CELL, cols * CELL))187    index = {}188    for i, n in enumerate(names):189        x, y = (i % cols) * CELL, (i // cols) * CELL190        atlas[y:y + CELL, x:x + CELL] = sdf(n)191        index[n] = [x, y, CELL, CELL]192    Image.fromarray((atlas * 255).round().astype("uint8"), "L").save(os.path.join(OUT, "atlas", "ka-icons.png"), optimize=True)193    json.dump({"format": "ka-icons/1", "icon_px": ICON_PX, "spread_px": SPREAD_PX, "cell_px": CELL, "width": cols * CELL, "height": rows * CELL,194               "icon_fields": ["x", "y", "w", "h"], "icons": index}, open(os.path.join(OUT, "atlas", "ka-icons.json"), "w"), indent=1)195    # Planche d'aperçu : 48 px, 24 px, 14 px sur papier, et 24 px sur fond sombre.196    font = ImageFont.truetype(os.path.join(os.path.dirname(ROOT), "engine", "assets", "fonts", "Inter-Medium.ttf"), 12)197    cw, ch = 190, 112198    im = Image.new("RGB", (cols * cw // 2 * 2, ((len(names) + 3) // 4) * ch), "#f2ede2")199    im = Image.new("RGB", (4 * cw * 2, ((len(names) + 7) // 8) * ch + 8), "#f2ede2")200    d = ImageDraw.Draw(im)201    for i, n in enumerate(names):202        x, y = (i % 8) * cw + 10, (i // 8) * ch + 10203        for size, dx in ((48, 0), (24, 58), (14, 92)):204            ic = raster(n, size * 2, "#27231e").resize((size, size), Image.LANCZOS)205            im.paste(ic, (x + dx, y + (48 - size) // 2), ic)206        d.rounded_rectangle([x + 116, y + 6, x + 116 + 36, y + 42], 6, fill="#1c1b19")207        ic = raster(n, 48, "#ece5d6").resize((24, 24), Image.LANCZOS)208        im.paste(ic, (x + 122, y + 12), ic)209        d.text((x, y + 58), n, font=font, fill="#27231e")210        d.text((x, y + 74), ICONS[n][0][:28], font=font, fill="#676054")211    im.save(os.path.join(OUT, "apercu.png"), optimize=True)212    print(len(names), "icônes ; atlas", cols * CELL, "×", rows * CELL)213214215if __name__ == "__main__":216    main()217
Files main
Menu

Theme