Debian Codegraph @

cursors/src/generate_cursors

raw
1 #!/usr/bin/env python3
2
3 import sys
4 import math
5 import re
6 import subprocess
7 import shutil
8 import json
9 from pathlib import Path
10 from PySide6.QtSvg import QSvgRenderer
11
12 # Displace the hotspot to the right and down by 1/100th of a pixel, then
13 # floor. So if by some float error the hotspot is at 4.995, it will be
14 # displaced to 5.005, then floored to 5. This is to prevent the hotspot
15 # from potential off-by-one errors when the cursor is scaled.
16 HOTSPOT_DISPLACE = 1
17
18 if len(sys.argv) <= 7:
19 print("Usage: " + sys.argv[0] + " <svg cursor dir> <pixmap dir> <xcursor output dir> <svg cursor output dir> <base size> <animation frame time> <scales>")
20 sys.exit(1)
21
22 svg_dir = Path(sys.argv[1])
23 pixmap_dir = Path(sys.argv[2])
24 xcursor_output_dir = Path(sys.argv[3])
25 svg_cursor_output_dir = Path(sys.argv[4])
26 nominal_size = int(sys.argv[5])
27 delay = int(sys.argv[6])
28 scales = [int(i) for i in sys.argv[7:]]
29
30 svg_files = list(svg_dir.glob("*.svg"))
31 svg_files.sort()
32
33 processed_svgs = set()
34
35 for svg_file in svg_files:
36 if svg_file in processed_svgs:
37 continue
38 processed_svgs.add(svg_file)
39
40 basename = svg_file.stem
41 basename = re.sub(r"-[0-9_]*$", "", basename)
42 print(f" {basename}")
43
44 # Animated?
45 frames = list(svg_dir.glob(basename + "-[0-9]*.svg"))
46 frames.sort()
47 processed_svgs.update(frames)
48
49 svg = QSvgRenderer(str(svg_file))
50 hotspot = svg.transformForElement("hotspot").map(svg.boundsOnElement("hotspot")).boundingRect().topLeft()
51
52 # Generate xcursor
53 xcursor_config_path = pixmap_dir / "config" / f"{basename}.config"
54 with open(xcursor_config_path, "w") as config:
55 for scale in scales:
56 size = math.floor(nominal_size * scale / 100)
57 hotspot_x = math.floor((hotspot.x() * scale + HOTSPOT_DISPLACE) / 100)
58 hotspot_y = math.floor((hotspot.y() * scale + HOTSPOT_DISPLACE) / 100)
59
60 if len(frames) == 0:
61 print(f"{size} {hotspot_x} {hotspot_y} x{scale}/{basename}.png", file=config)
62 else:
63 for i in frames:
64 t = i.stem
65 print(f"{size} {hotspot_x} {hotspot_y} x{scale}/{t}.png {delay}", file=config)
66
67 subprocess.run(["xcursorgen", "-p", pixmap_dir, xcursor_config_path, xcursor_output_dir / basename], check=True)
68
69 # Generate SVG cursor
70 output_dir = svg_cursor_output_dir / basename
71 output_dir.mkdir(parents=True, exist_ok=True)
72 hotspot_x = hotspot.x()
73 hotspot_y = hotspot.y()
74 with open(output_dir / "metadata.json", "w") as metadata:
75 if len(frames) == 0:
76 filename = svg_file.name
77 shutil.copyfile(svg_file, output_dir / filename)
78 json.dump([{
79 "filename": filename,
80 "hotspot_x": hotspot_x,
81 "hotspot_y": hotspot_y
82 }],
83 metadata)
84 else:
85 l = []
86 for i in frames:
87 filename = i.name
88 shutil.copyfile(i, output_dir / filename)
89 l.append({
90 "filename": filename,
91 "hotspot_x": hotspot_x,
92 "hotspot_y": hotspot_y,
93 "delay": delay
94 })
95 json.dump(l, metadata)