#!/usr/bin/env python3 """Generate Geminux crisp HiDPI X11 Xcursor theme under overlay/rootfs/usr/share/icons/default.""" import math import os import struct import sys BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DEFAULT_OUT = os.path.join( BASE_DIR, "overlay", "rootfs", "usr", "share", "icons", "default" ) SIZES = (32, 48, 64) XCURSOR_MAGIC = b"Xcur" XCURSOR_VERSION = 0x00010000 XCURSOR_IMAGE_TYPE = 0xFFFD0002 XCURSOR_IMAGE_VERSION = 1 def point_in_poly(px, py, poly): inside = False n = len(poly) j = n - 1 for i in range(n): xi, yi = poly[i] xj, yj = poly[j] if ((yi > py) != (yj > py)) and ( px < (xj - xi) * (py - yi) / ((yj - yi) or 1e-12) + xi ): inside = not inside j = i return inside def dist_point_to_seg(px, py, ax, ay, bx, by): vx = bx - ax vy = by - ay l2 = vx * vx + vy * vy if l2 <= 1e-12: return math.hypot(px - ax, py - ay) t = ((px - ax) * vx + (py - ay) * vy) / l2 if t < 0.0: t = 0.0 elif t > 1.0: t = 1.0 dx = px - (ax + t * vx) dy = py - (ay + t * vy) return math.hypot(dx, dy) def dist_to_poly_edges(px, py, poly): best = 1e9 n = len(poly) for i in range(n): ax, ay = poly[i] bx, by = poly[(i + 1) % n] d = dist_point_to_seg(px, py, ax, ay, bx, by) if d < best: best = d return best def signed_dist_polys(px, py, polys): """Signed distance (<0 inside any polygon, >0 outside).""" best_abs = 1e9 inside = False for poly in polys: if point_in_poly(px, py, poly): inside = True d = dist_to_poly_edges(px, py, poly) if d < best_abs: best_abs = d return -best_abs if inside else best_abs def signed_dist_capsules(px, py, capsules): """Signed distance to union of rounded line segments (ax, ay, bx, by, r).""" best = 1e9 for ax, ay, bx, by, r in capsules: d = dist_point_to_seg(px, py, ax, ay, bx, by) - r if d < best: best = d return best def blend_over(dst, src): sa, sr, sg, sb = src if sa <= 0.0: return dst da, dr, dg, db = dst inv = 1.0 - sa oa = sa + da * inv if oa <= 1e-9: return (0.0, 0.0, 0.0, 0.0) return ( oa, sr * sa + dr * inv, sg * sa + dg * inv, sb * sa + db * inv, ) def render_cursor(size, shape_fn, base_hot, outline_w=0.036, shadow_off=(0.024, 0.032)): """Render an anti-aliased RGBA cursor canvas using 4x4 supersampling.""" sub = 4 inv_sub2 = 1.0 / (sub * sub) step = 1.0 / sub pixels = bytearray(size * size * 4) for y in range(size): for x in range(size): sum_a = 0.0 sum_r = 0.0 sum_g = 0.0 sum_b = 0.0 for sy in range(sub): ny = (y + (sy + 0.5) * step) / size for sx in range(sub): nx = (x + (sx + 0.5) * step) / size sd = shape_fn(nx, ny) sh_sd = shape_fn(nx - shadow_off[0], ny - shadow_off[1]) col = (0.0, 0.0, 0.0, 0.0) # Subtle drop shadow if sh_sd < outline_w + 0.022: sh_cov = max(0.0, min(1.0, (outline_w + 0.016 - sh_sd) / 0.022)) if sh_cov > 0.0: col = blend_over(col, (sh_cov * 0.32, 0.02, 0.03, 0.05)) # Dark outline aa = 1.1 / size out_cov = max(0.0, min(1.0, (outline_w + aa - sd) / (2.0 * aa))) if out_cov > 0.0: col = blend_over(col, (out_cov, 0.06, 0.08, 0.12)) # Crisp white fill fill_cov = max(0.0, min(1.0, (aa - sd) / (2.0 * aa))) if fill_cov > 0.0: col = blend_over(col, (fill_cov, 0.98, 0.99, 1.0)) sum_a += col[0] sum_r += col[1] sum_g += col[2] sum_b += col[3] oa = max(0.0, min(1.0, sum_a * inv_sub2)) r_p = max(0, min(255, int(round(sum_r * inv_sub2 * 255.0)))) g_p = max(0, min(255, int(round(sum_g * inv_sub2 * 255.0)))) b_p = max(0, min(255, int(round(sum_b * inv_sub2 * 255.0)))) a_u = max(0, min(255, int(round(oa * 255.0)))) if r_p > a_u: r_p = a_u if g_p > a_u: g_p = a_u if b_p > a_u: b_p = a_u argb = (a_u << 24) | (r_p << 16) | (g_p << 8) | b_p struct.pack_into(" 1 else DEFAULT_OUT cur_dir = os.path.join(out_dir, "cursors") os.makedirs(cur_dir, exist_ok=True) index_path = os.path.join(out_dir, "index.theme") with open(index_path, "w", encoding="utf-8") as f: f.write("[Icon Theme]\nName=Default\nComment=Geminux HiDPI X11 Cursor Theme\n") specs = { "left_ptr": ( shape_left_ptr, (0.11, 0.08), ("default", "arrow", "top_left_arrow", "left_ptr_watch"), ), "xterm": ( shape_xterm, (0.50, 0.50), ("ibeam", "text"), ), "hand2": ( shape_hand, (0.38, 0.11), ("hand1", "pointer", "pointing_hand"), ), "fleur": ( shape_fleur, (0.50, 0.50), ("move", "all-scroll", "grab", "grabbing"), ), "sb_h_double_arrow": ( shape_sb_h, (0.50, 0.50), ("ew-resize", "h_double_arrow", "col-resize"), ), "sb_v_double_arrow": ( shape_sb_v, (0.50, 0.50), ("ns-resize", "v_double_arrow", "row-resize"), ), "bottom_right_corner": ( shape_bottom_right, (0.78, 0.78), ("se-resize", "nwse-resize", "top_left_corner", "sizing"), ), "bottom_left_corner": ( shape_bottom_left, (0.22, 0.78), ("sw-resize", "nesw-resize", "top_right_corner"), ), "crosshair": ( shape_crosshair, (0.50, 0.50), ("tcross", "cross"), ), "watch": ( shape_watch, (0.50, 0.50), ("wait",), ), } for name, (fn, hot, aliases) in specs.items(): frames = [] for sz in SIZES: w, h, xhot, yhot, px = render_cursor(sz, fn, hot) frames.append((sz, w, h, xhot, yhot, px)) target_path = os.path.join(cur_dir, name) write_xcursor_file(target_path, frames) for alias in aliases: ap = os.path.join(cur_dir, alias) if os.path.lexists(ap): os.unlink(ap) os.symlink(name, ap) print(f"[PASS] Generated {len(specs)} multi-size HiDPI cursors ({SIZES}) in {cur_dir}") return 0 if __name__ == "__main__": sys.exit(main())