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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
use std::{fs, io::Result, os::unix, sync::{Arc, RwLock}};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use serde::{Deserialize, Serialize};
use tiny_skia::Pixmap;
use xcursor::{ImageInfo, XCursorEncoder};
mod xcursor;
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
struct Xy {
x: u32,
y: u32,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
struct BibataPlatformNames {
windows: Option<String>,
xorg: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
struct BibataFrame {
idx: i32,
id: String,
url: String,
delay: u32,
sha512: String,
svg: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
struct BibataCursor {
animated: bool,
id: String,
name: String,
aliases: Vec<String>,
hot: Xy,
frames: Vec<BibataFrame>,
frame_count: i32,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BibataCursorStyle {
kind: String,
cursors: Vec<BibataCursor>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BibataColourScheme<'a> {
pub base: &'a str,
pub outline: &'a str,
pub watch_background: &'a str,
pub watch_color: Vec<&'a str>,
/** Value between 0 and 1 - Bibata defaults to 0.8 */
pub watch_opacity: f32,
}
fn vector_to_pixmap(svg_data: &[u8]) -> Pixmap {
let tree = {
let mut opt = usvg::Options::default();
// Get file's absolute directory.
// opt.resources_dir = std::fs::canonicalize(&args[1])
// .ok()
// .and_then(|p| p.parent().map(|p| p.to_path_buf()));
opt.fontdb_mut().load_system_fonts();
usvg::Tree::from_data(svg_data, &opt).unwrap()
};
let pixmap_size = tree.size().to_int_size();
let mut pixmap = tiny_skia::Pixmap::new(pixmap_size.width(), pixmap_size.height()).unwrap();
resvg::render(&tree, tiny_skia::Transform::default(), &mut pixmap.as_mut());
pixmap
}
fn resize_cursvg(svg: &str, new_size: &u16) -> String {
svg.to_string().replace(
"width=\"256\" height=\"256\"",
format!("width=\"{new_size}\" height=\"{new_size}\"").as_str(),
)
}
fn apply_colours(unpatched_svg: String, colours: &BibataColourScheme) -> String {
if colours.watch_color.len() != 4 {
panic!("Must specify 4 watch colours!");
};
unpatched_svg
.replace("#00FF00", colours.base)
.replace("#0000FF", colours.outline)
.replace("#FF0000", colours.watch_background)
.replace("#F05024", colours.watch_color[0])
.replace("#FCB813", colours.watch_color[1])
.replace("#7EBA41", colours.watch_color[2])
.replace("#32A0DA", colours.watch_color[3])
.replace(
"fill-opacity=\"0.8\"",
format!("fill-opacity=\"{}\"", colours.watch_opacity).as_str(),
)
}
fn process_xcursor(
cursor: BibataCursor,
desired_colours: BibataColourScheme,
desired_sizes: Vec<u16>,
time_scale: u32,
) -> Vec<u8> {
let size = cursor.frames.len() * desired_sizes.len();
let image_info_vec: Arc<RwLock<Vec<ImageInfo>>> = Arc::new(RwLock::new(vec![ImageInfo {
r#type: 0,
subtype: 0,
width: 0,
height: 0,
xhot: 0,
yhot: 0,
data: vec![],
delay: 0,
};size]));
let desired_colours = Arc::new(desired_colours);
let desired_sizes = Arc::new(desired_sizes);
rayon::scope(|s| {
let mut i=0;
for cursor_frame in cursor.frames {
let ouridx = i;
i+=1;
let image_info_vec = image_info_vec.clone();
let desired_colours = desired_colours.clone();
let desired_sizes = desired_sizes.clone();
s.spawn(move |_| {
for desired_size in &desired_sizes.to_vec() {
let svg_data = apply_colours(
resize_cursvg(&cursor_frame.svg, desired_size),
&desired_colours,
);
let pixmap = vector_to_pixmap(svg_data.as_bytes());
let pixels = {
let pixels = pixmap.pixels();
let mut otherpixels: Vec<u8> = Vec::new();
for elem in pixels.iter() {
otherpixels.push(elem.red());
otherpixels.push(elem.green());
otherpixels.push(elem.blue());
otherpixels.push(elem.alpha());
}
otherpixels
};
let image_info = ImageInfo {
r#type: *desired_size as u32,
subtype: *desired_size as u32,
width: pixmap.width().try_into().unwrap(),
height: pixmap.height().try_into().unwrap(),
xhot: (cursor.hot.x * pixmap.width() / 256).try_into().unwrap(),
yhot: (cursor.hot.y * pixmap.height() / 256).try_into().unwrap(),
data: pixels.try_into().unwrap(),
delay: (time_scale / cursor_frame.delay).try_into().unwrap(),
};
{
let mut image_info_vec = image_info_vec.write().unwrap();
image_info_vec[ouridx] = image_info;
}
}
});
}
});
let mut encoder = XCursorEncoder::new(image_info_vec.read().unwrap().to_vec());
encoder.pack<
|