-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.py
More file actions
165 lines (128 loc) · 4.74 KB
/
renderer.py
File metadata and controls
165 lines (128 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/python3
import os
import sys
import xml.etree.ElementTree as ET
import gi
gi.require_version('Rsvg', '2.0')
from gi.repository import Rsvg, GLib
import cairo
CACHE_DIR = os.path.expanduser("~/.cache/WindowControls")
def parse_color_to_rgb(color_str):
if not color_str:
return None
if color_str.startswith('shade/'):
parts = color_str.split('/')
if len(parts) >= 3:
base = parts[1].lstrip('#')
factor = float(parts[2])
if len(base) == 6:
r = min(1.0, (int(base[0:2], 16) / 255.0) * factor)
g = min(1.0, (int(base[2:4], 16) / 255.0) * factor)
b = min(1.0, (int(base[4:6], 16) / 255.0) * factor)
return (r, g, b)
elif color_str.startswith('#'):
h = color_str.lstrip('#')
if len(h) == 6:
return (int(h[0:2], 16) / 255.0,
int(h[2:4], 16) / 255.0,
int(h[4:6], 16) / 255.0)
return None
def render_svg_colorized(svg_path, color_rgb, size):
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, size, size)
ctx = cairo.Context(surface)
handle = Rsvg.Handle.new_from_file(svg_path)
viewport = Rsvg.Rectangle()
viewport.x = 0
viewport.y = 0
viewport.width = size
viewport.height = size
handle.render_document(ctx, viewport)
if color_rgb:
ctx.set_operator(cairo.OPERATOR_ATOP)
ctx.set_source_rgba(color_rgb[0], color_rgb[1], color_rgb[2], 1.0)
ctx.paint()
return surface
def composite_layers(layers, size):
result = cairo.ImageSurface(cairo.FORMAT_ARGB32, size, size)
ctx = cairo.Context(result)
for layer in layers:
ctx.set_source_surface(layer, 0, 0)
ctx.paint()
return result
def find_theme_dir(theme_name):
paths = [
os.path.expanduser(f"~/.themes/{theme_name}/metacity-1"),
f"/usr/share/themes/{theme_name}/metacity-1",
f"/usr/local/share/themes/{theme_name}/metacity-1",
]
for p in paths:
if os.path.isdir(p):
return p
return None
def render_theme(theme_name):
theme_dir = find_theme_dir(theme_name)
if not theme_dir:
print(f"error=Theme not found: {theme_name}", file=sys.stderr)
return False
xml_path = None
for fname in ['metacity-theme-3.xml', 'metacity-theme-2.xml', 'metacity-theme-1.xml']:
p = os.path.join(theme_dir, fname)
if os.path.isfile(p):
xml_path = p
break
if not xml_path:
print("error=No metacity theme XML found", file=sys.stderr)
return False
tree = ET.parse(xml_path)
root = tree.getroot()
constants = {}
for c in root.findall('.//constant'):
constants[c.get('name')] = c.get('value')
draw_ops = {}
for ops in root.findall('.//draw_ops'):
name = ops.get('name', '')
draw_ops[name] = ops
os.makedirs(CACHE_DIR, exist_ok=True)
SIZE = 16
button_states = {
('close', 'normal'): 'close_focused',
('close', 'hover'): 'close_focused_prelight',
('close', 'active'): 'close_focused_pressed',
('maximize', 'normal'): 'maximize_focused',
('maximize', 'hover'): 'maximize_focused_prelight',
('maximize', 'active'): 'maximize_focused_pressed',
('minimize', 'normal'): 'minimize_focused',
('minimize', 'hover'): 'minimize_focused_prelight',
('minimize', 'active'): 'minimize_focused_pressed',
}
for (btn_type, state), ops_name in button_states.items():
if ops_name not in draw_ops:
continue
ops = draw_ops[ops_name]
layers = []
for child in ops:
if child.tag == 'image':
filename = child.get('filename')
colorize_const = child.get('colorize')
svg_path = os.path.join(theme_dir, filename)
if not os.path.isfile(svg_path):
continue
color_rgb = None
if colorize_const and colorize_const in constants:
color_rgb = parse_color_to_rgb(constants[colorize_const])
elif colorize_const:
color_rgb = parse_color_to_rgb(colorize_const)
layer = render_svg_colorized(svg_path, color_rgb, SIZE)
layers.append(layer)
if layers:
result = composite_layers(layers, SIZE)
out_path = os.path.join(CACHE_DIR, f"{btn_type}_{state}.png")
result.write_to_png(out_path)
print(f"cache={CACHE_DIR}")
return True
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: renderer.py <theme_name>", file=sys.stderr)
sys.exit(1)
if not render_theme(sys.argv[1]):
sys.exit(1)