Files
geminux/scripts/gen-xcursor-theme.py
T

397 lines
9.8 KiB
Python
Raw Normal View History

#!/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("<I", pixels, (y * size + x) * 4, argb)
xhot = max(0, min(size - 1, int(round(base_hot[0] * size))))
yhot = max(0, min(size - 1, int(round(base_hot[1] * size))))
return (size, size, xhot, yhot, bytes(pixels))
def shape_left_ptr(nx, ny):
arrow = [
(0.11, 0.08),
(0.11, 0.80),
(0.28, 0.64),
(0.41, 0.90),
(0.53, 0.84),
(0.40, 0.59),
(0.62, 0.59),
]
return signed_dist_polys(nx, ny, [arrow])
def shape_xterm(nx, ny):
caps = [
(0.50, 0.14, 0.50, 0.86, 0.028),
(0.35, 0.14, 0.65, 0.14, 0.025),
(0.35, 0.86, 0.65, 0.86, 0.025),
(0.41, 0.50, 0.59, 0.50, 0.020),
]
return signed_dist_capsules(nx, ny, caps)
def shape_hand(nx, ny):
# Clean stylized pointing hand silhouette
hand_poly = [
(0.33, 0.10),
(0.43, 0.10),
(0.45, 0.36),
(0.70, 0.39),
(0.75, 0.45),
(0.75, 0.66),
(0.67, 0.88),
(0.31, 0.88),
(0.20, 0.62),
(0.23, 0.52),
(0.33, 0.56),
]
sd_p = signed_dist_polys(nx, ny, [hand_poly])
sd_tip = math.hypot(nx - 0.38, ny - 0.11) - 0.052
return min(sd_p, sd_tip)
def shape_fleur(nx, ny):
caps = [
(0.16, 0.50, 0.84, 0.50, 0.030),
(0.50, 0.16, 0.50, 0.84, 0.030),
]
heads = [
[(0.50, 0.07), (0.37, 0.23), (0.63, 0.23)],
[(0.50, 0.93), (0.37, 0.77), (0.63, 0.77)],
[(0.07, 0.50), (0.23, 0.37), (0.23, 0.63)],
[(0.93, 0.50), (0.77, 0.37), (0.77, 0.63)],
]
return min(signed_dist_capsules(nx, ny, caps), signed_dist_polys(nx, ny, heads))
def shape_sb_h(nx, ny):
caps = [(0.16, 0.50, 0.84, 0.50, 0.032)]
heads = [
[(0.07, 0.50), (0.26, 0.34), (0.26, 0.66)],
[(0.93, 0.50), (0.74, 0.34), (0.74, 0.66)],
]
return min(signed_dist_capsules(nx, ny, caps), signed_dist_polys(nx, ny, heads))
def shape_sb_v(nx, ny):
caps = [(0.50, 0.16, 0.50, 0.84, 0.032)]
heads = [
[(0.50, 0.07), (0.34, 0.26), (0.66, 0.26)],
[(0.50, 0.93), (0.34, 0.74), (0.66, 0.74)],
]
return min(signed_dist_capsules(nx, ny, caps), signed_dist_polys(nx, ny, heads))
def shape_bottom_right(nx, ny):
caps = [
(0.24, 0.24, 0.76, 0.76, 0.030),
(0.78, 0.52, 0.78, 0.78, 0.034),
(0.52, 0.78, 0.78, 0.78, 0.034),
]
head = [[(0.14, 0.14), (0.36, 0.18), (0.18, 0.36)]]
return min(signed_dist_capsules(nx, ny, caps), signed_dist_polys(nx, ny, head))
def shape_bottom_left(nx, ny):
return shape_bottom_right(1.0 - nx, ny)
def shape_crosshair(nx, ny):
caps = [
(0.14, 0.50, 0.38, 0.50, 0.026),
(0.62, 0.50, 0.86, 0.50, 0.026),
(0.50, 0.14, 0.50, 0.38, 0.026),
(0.50, 0.62, 0.50, 0.86, 0.026),
]
dot = math.hypot(nx - 0.50, ny - 0.50) - 0.034
return min(signed_dist_capsules(nx, ny, caps), dot)
def shape_watch(nx, ny):
ring = abs(math.hypot(nx - 0.50, ny - 0.50) - 0.28) - 0.042
hands = signed_dist_capsules(
nx,
ny,
[
(0.50, 0.50, 0.50, 0.32, 0.026),
(0.50, 0.50, 0.64, 0.54, 0.026),
],
)
return min(ring, hands)
def write_xcursor_file(path, frames):
"""Write an Xcursor v1.0 multi-size binary file."""
ntoc = len(frames)
toc_offset = 16
data_offset = toc_offset + ntoc * 12
tocs = []
chunks = bytearray()
cur_off = data_offset
for nom_size, w, h, xhot, yhot, rgba_bytes in frames:
tocs.append((XCURSOR_IMAGE_TYPE, nom_size, cur_off))
hdr = struct.pack(
"<IIIIIIIII",
36,
XCURSOR_IMAGE_TYPE,
nom_size,
XCURSOR_IMAGE_VERSION,
w,
h,
xhot,
yhot,
0,
)
chunks.extend(hdr)
chunks.extend(rgba_bytes)
cur_off += len(hdr) + len(rgba_bytes)
out = bytearray(struct.pack("<4sIII", XCURSOR_MAGIC, 16, XCURSOR_VERSION, ntoc))
for typ, sub, pos in tocs:
out.extend(struct.pack("<III", typ, sub, pos))
out.extend(chunks)
with open(path, "wb") as f:
f.write(out)
def main():
out_dir = sys.argv[1] if len(sys.argv) > 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())