# SPDX-License-Identifier: MIT """Generates the OpenPU application icon (original artwork) in every format the project needs. One description of the shapes (64x64 grid, the native grid of HVIF) produces: data/icons/openpu.svg master SVG data/icons/openpu-small.svg simplified variant for 16 and 24 px data/icons/openpu.hvif Haiku Vector Icon Format (embedded in the Haiku executable) data/icons/png/openpu-N.png PNG renderings, N in SIZES Standard library only: the PNGs come from a small antialiased rasterizer below, so no SVG tool is required. Run from the repository root: python3 -I tools/make_icons.py """ import math import os import struct import sys import zlib SIZES = (16, 24, 32, 48, 64, 128, 256, 512) SMALL_SIZES = (16, 24) GRADIENT_TOP = (0x25, 0x63, 0xB8) GRADIENT_BOTTOM = (0x16, 0x3A, 0x6B) WHITE = (0xFF, 0xFF, 0xFF) ARROW = (0x8E, 0xC5, 0xFF) GRADIENT_Y = (4.0, 60.0) # vertical extent of the gradient, in icon units # --- geometry ------------------------------------------------------------------------------------ # A path is a closed list of points (x, y, in_handle, out_handle); a None handle means a straight # segment on that side. The segment from p[i] to p[i+1] uses p[i].out and p[i+1].in. def pt(x, y, hin=None, hout=None): return (x, y, hin, hout) def shield(): return [ pt(12, 4, hin=(9.8, 4)), pt(52, 4, hout=(54.2, 4)), pt(56, 8, hin=(56, 5.8)), pt(56, 34, hout=(56, 49)), pt(32, 61, hin=(45, 57), hout=(19, 57)), pt(8, 34, hin=(8, 49)), pt(8, 8, hout=(8, 5.8)), ] def transform(path, fn): def f(h): return None if h is None else fn(*h) return [(*fn(x, y), f(hin), f(hout)) for x, y, hin, hout in path] def scaled(path, s, cx=32.0, cy=32.5): return transform(path, lambda x, y: (cx + s * (x - cx), cy + s * (y - cy))) def moved(path, dx, dy): return transform(path, lambda x, y: (x + dx, y + dy)) def letter_p(): return [ pt(16, 15), pt(24, 15, hout=(27.9, 15)), pt(30, 22, hin=(30, 18.1), hout=(30, 25.9)), pt(24, 29, hin=(27.9, 29)), pt(22, 29), pt(22, 37), pt(16, 37), ] def letter_p_counter(): return [ pt(22, 20), pt(24, 20, hout=(25.1, 20)), pt(26, 22, hin=(26, 20.9), hout=(26, 23.1)), pt(24, 24, hin=(25.1, 24)), pt(22, 24), ] def letter_u(): return [ pt(34, 15), pt(40, 15), pt(40, 29, hout=(40, 30.1)), pt(42, 31, hin=(40.9, 31), hout=(43.1, 31)), pt(44, 29, hin=(44, 30.1)), pt(44, 15), pt(50, 15), pt(50, 29, hout=(50, 33.4)), pt(42, 37, hin=(46.4, 37), hout=(37.6, 37)), pt(34, 29, hin=(34, 33.4)), ] def arrow(): return [pt(30, 41), pt(34, 41), pt(34, 47), pt(39, 47), pt(32, 54), pt(25, 47), pt(30, 47)] def layers(small): """(style, path) pairs, painted in order. Holes are painted over with the background style, so every path is a simple filled outline (same result in SVG, HVIF and the rasterizer).""" dy = 4 if small else 0 # without the arrow, the letters move down to the optical centre letters = [ ("white", moved(letter_p(), -1, dy)), ("gradient", moved(letter_p_counter(), -1, dy)), ("white", moved(letter_u(), -1, dy)), ] out = [("gradient", shield())] if not small: out += [("white", scaled(shield(), 0.90)), ("gradient", scaled(shield(), 0.86))] out += letters if not small: out.append(("arrow", arrow())) return out # --- SVG ----------------------------------------------------------------------------------------- def fmt(v): return ("%.2f" % v).rstrip("0").rstrip(".") def svg_path(path): x0, y0 = path[0][:2] d = ["M%s %s" % (fmt(x0), fmt(y0))] for i in range(len(path)): a, b = path[i], path[(i + 1) % len(path)] if a[3] is None and b[2] is None: if i + 1 < len(path): d.append("L%s %s" % (fmt(b[0]), fmt(b[1]))) continue c1 = a[3] or a[:2] c2 = b[2] or b[:2] d.append("C%s %s %s %s %s %s" % (fmt(c1[0]), fmt(c1[1]), fmt(c2[0]), fmt(c2[1]), fmt(b[0]), fmt(b[1]))) return " ".join(d) + "Z" def hexcolor(c): return "#%02x%02x%02x" % c def write_svg(file, small): fills = {"gradient": "url(#bg)", "white": hexcolor(WHITE), "arrow": hexcolor(ARROW)} lines = [ '', " OpenPU", " ", ' ' % (fmt(GRADIENT_Y[0]), fmt(GRADIENT_Y[1])), ' ' % hexcolor(GRADIENT_TOP), ' ' % hexcolor(GRADIENT_BOTTOM), " ", " ", ] for style, path in layers(small): lines.append(' ' % (fills[style], svg_path(path))) lines.append("") with open(file, "w", encoding="utf-8", newline="\n") as f: f.write("\n".join(lines) + "\n") # --- HVIF ---------------------------------------------------------------------------------------- # Format of Haiku's FlatIconImporter: "ncif", then styles, paths and shapes, each list prefixed # with a uint8 count. Coordinates: one byte (value + 32) for integers in -32..95, otherwise two # bytes, 0x8000 | round((value + 128) * 102). Matrices use 24-bit floats. STYLE_SOLID_NO_ALPHA = 3 STYLE_GRADIENT = 2 GRADIENT_LINEAR = 0 GRADIENT_FLAG_TRANSFORM = 1 << 1 GRADIENT_FLAG_NO_ALPHA = 1 << 2 PATH_FLAG_CLOSED = 1 << 1 PATH_FLAG_NO_CURVES = 1 << 3 SHAPE_TYPE_PATH_SOURCE = 10 def hvif_coord(v): if abs(v - round(v)) < 1e-9 and -32 <= round(v) <= 95: return bytes([int(round(v)) + 32]) n = int(round((v + 128.0) * 102.0)) if not 0 <= n < 0x8000: raise ValueError("coordinate out of range: %r" % v) return bytes([0x80 | (n >> 8), n & 0xFF]) def float24(v): if v == 0: return b"\0\0\0" bits = struct.unpack("> 31 exponent = ((bits >> 23) & 0xFF) - 127 + 32 if not 0 <= exponent < 64: raise ValueError("float out of range: %r" % v) value = (sign << 23) | (exponent << 17) | ((bits & 0x7FFFFF) >> 6) return bytes([(value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF]) def hvif_path(path): curved = any(p[2] is not None or p[3] is not None for p in path) out = bytearray([PATH_FLAG_CLOSED | (0 if curved else PATH_FLAG_NO_CURVES), len(path)]) for x, y, hin, hout in path: hin = hin or (x, y) hout = hout or (x, y) values = (x, y, *hin, *hout) if curved else (x, y) for v in values: out += hvif_coord(v) return bytes(out) def write_hvif(file, small): # Linear gradients run from -64 to 64 on their own x axis: map it to the vertical extent. half = (GRADIENT_Y[1] - GRADIENT_Y[0]) / 128.0 mid = (GRADIENT_Y[0] + GRADIENT_Y[1]) / 2.0 matrix = (0.0, half, -half, 0.0, 32.0, mid) # agg::trans_affine: sx, shy, shx, sy, tx, ty gradient = bytearray([STYLE_GRADIENT, GRADIENT_LINEAR, GRADIENT_FLAG_TRANSFORM | GRADIENT_FLAG_NO_ALPHA, 2]) for m in matrix: gradient += float24(m) gradient += bytes([0, *GRADIENT_TOP, 255, *GRADIENT_BOTTOM]) styles = { "gradient": bytes(gradient), "white": bytes([STYLE_SOLID_NO_ALPHA, *WHITE]), "arrow": bytes([STYLE_SOLID_NO_ALPHA, *ARROW]), } used = [] for style, _ in layers(small): if style not in used: used.append(style) data = bytearray(b"ncif") data.append(len(used)) for s in used: data += styles[s] ls = layers(small) data.append(len(ls)) for _, path in ls: data += hvif_path(path) data.append(len(ls)) for i, (style, _) in enumerate(ls): data += bytes([SHAPE_TYPE_PATH_SOURCE, used.index(style), 1, i, 0]) with open(file, "wb") as f: f.write(data) return len(data) # --- rasterizer and PNG ---------------------------------------------------------------------------- def flatten(path, steps=24): poly = [] for i in range(len(path)): a, b = path[i], path[(i + 1) % len(path)] poly.append(a[:2]) if a[3] is None and b[2] is None: continue c1, c2 = a[3] or a[:2], b[2] or b[:2] for k in range(1, steps): t = k / steps u = 1 - t poly.append(( u ** 3 * a[0] + 3 * u * u * t * c1[0] + 3 * u * t * t * c2[0] + t ** 3 * b[0], u ** 3 * a[1] + 3 * u * u * t * c1[1] + 3 * u * t * t * c2[1] + t ** 3 * b[1], )) return poly def coverage(poly, size, sub=16): """Antialiased non-zero coverage (0..1) of a polygon given in 64-unit coordinates.""" scale = size / 64.0 pts = [(x * scale, y * scale) for x, y in poly] edges = [] for i in range(len(pts)): (x0, y0), (x1, y1) = pts[i], pts[(i + 1) % len(pts)] if y0 != y1: edges.append((x0, y0, x1, y1, 1 if y1 > y0 else -1)) cov = [0.0] * (size * size) w = 1.0 / sub for row in range(size): base = row * size for s in range(sub): y = row + (s + 0.5) / sub crossings = [] for x0, y0, x1, y1, d in edges: if (y0 <= y < y1) or (y1 <= y < y0): crossings.append((x0 + (y - y0) * (x1 - x0) / (y1 - y0), d)) crossings.sort() winding = 0 for i in range(len(crossings) - 1): winding += crossings[i][1] if winding == 0: continue xa, xb = max(crossings[i][0], 0.0), min(crossings[i + 1][0], float(size)) if xb <= xa: continue ia, ib = int(xa), int(xb) if ia == ib: cov[base + ia] += (xb - xa) * w continue cov[base + ia] += (ia + 1 - xa) * w for px in range(ia + 1, min(ib, size)): cov[base + px] += w if ib < size: cov[base + ib] += (xb - ib) * w return cov def render(size, small): rgba = [[0.0, 0.0, 0.0, 0.0] for _ in range(size * size)] # premultiplied for style, path in layers(small): cov = coverage(flatten(path), size) for i, c in enumerate(cov): if c <= 0: continue a = min(c, 1.0) if style == "gradient": y = ((i // size) + 0.5) * 64.0 / size t = min(max((y - GRADIENT_Y[0]) / (GRADIENT_Y[1] - GRADIENT_Y[0]), 0.0), 1.0) color = [GRADIENT_TOP[k] + (GRADIENT_BOTTOM[k] - GRADIENT_TOP[k]) * t for k in range(3)] else: color = WHITE if style == "white" else ARROW p = rgba[i] for k in range(3): p[k] = color[k] / 255.0 * a + p[k] * (1 - a) p[3] = a + p[3] * (1 - a) return rgba def write_png(file, size, rgba): raw = bytearray() for row in range(size): raw.append(0) for p in rgba[row * size:(row + 1) * size]: a = p[3] if a <= 0: raw += b"\0\0\0\0" continue raw += bytes([min(255, int(round(p[k] / a * 255))) for k in range(3)] + [int(round(a * 255))]) def chunk(kind, payload): body = kind + payload return struct.pack(">I", len(payload)) + body + struct.pack(">I", zlib.crc32(body) & 0xFFFFFFFF) with open(file, "wb") as f: f.write(b"\x89PNG\r\n\x1a\n") f.write(chunk(b"IHDR", struct.pack(">IIBBBBB", size, size, 8, 6, 0, 0, 0))) f.write(chunk(b"IDAT", zlib.compress(bytes(raw), 9))) f.write(chunk(b"IEND", b"")) def main(): root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "data", "icons") os.makedirs(os.path.join(root, "png"), exist_ok=True) write_svg(os.path.join(root, "openpu.svg"), small=False) write_svg(os.path.join(root, "openpu-small.svg"), small=True) n = write_hvif(os.path.join(root, "openpu.hvif"), small=False) print("openpu.hvif: %d bytes" % n) sizes = [int(a) for a in sys.argv[1:]] or SIZES for size in sizes: write_png(os.path.join(root, "png", "openpu-%d.png" % size), size, render(size, size in SMALL_SIZES)) print("openpu-%d.png" % size) if __name__ == "__main__": main()