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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// SPDX-License-Identifier: GPL-3.0-or-later
//! The skin's resources as the runtime reads them: bitmaps through their aliases, bitmap fonts,
//! fonts, gamma groups and colors.
use super::{
names_for, BitmapDef, BitmapFontDef, FontDef, Gamma, Rc, Rect, Rgb, SkinRuntime,
MAX_ALIAS_DEPTH, MAX_OPAQUE_SCAN,
};
impl SkinRuntime {
/// The bitmap element `id` names, through aliases; a plain file name used as an image.
pub(crate) fn bitmap(&mut self, id: &str) -> Option<BitmapDef> {
if let Some(found) = self.bitmap_lookups.get(id) {
return found.clone();
}
let found = self.find_bitmap(id);
self.bitmap_lookups.insert(id.to_string(), found.clone());
found
}
/// The size of picture `file`, as the host decoded it; asked once.
pub(crate) fn image_size(&mut self, file: &str) -> Option<(i32, i32)> {
if let Some(size) = self.image_sizes.get(file) {
return Some(*size);
}
let size = self.host.image_size(file)?;
self.image_sizes.insert(file.to_string(), size);
Some(size)
}
/// [`SkinRuntime::bitmap`] worked out: the element `id` names, through its aliases and
/// old-style twin, a skin's before the base's, else a file of that name.
fn find_bitmap(&mut self, id: &str) -> Option<BitmapDef> {
let mut from_base: Option<BitmapDef> = None;
for name in names_for(id) {
let Some((key, def)) = self.aliased_bitmap(&name) else {
continue;
};
if !self.base_bitmaps.contains(&key) {
return Some(def);
}
if from_base.is_none() {
from_base = Some(def);
}
}
if let Some(def) = from_base {
self.base_usage.insert(format!("bitmap:{}", def.id));
return Some(def);
}
// a plain file name used as an image
if self.files.exists(id) {
let key = id.to_lowercase();
Some(
self.bitmaps
.entry(key.clone())
.or_insert_with(|| BitmapDef::new(&key, id, 0, 0, None, None, None))
.clone(),
)
} else {
None
}
}
/// The bitmap element `name` defines, or names through element aliases (at most 8 hops),
/// with the id it is defined under; `None` when the chain ends without one.
fn aliased_bitmap(&self, name: &str) -> Option<(String, BitmapDef)> {
let mut key: &str = name;
for _ in 0..8 {
if let Some(def) = self.bitmaps.get(key) {
return Some((key.to_string(), def.clone()));
}
key = self.aliases.get(key)?;
}
None
}
/// The size of bitmap `id`: as its element says, else from its file.
pub(crate) fn bitmap_size(&mut self, id: &str) -> Option<(i32, i32)> {
let def = self.bitmap(id)?;
if let (Some(w), Some(h)) = (def.w, def.h) {
return Some((w, h));
}
let full = self.image_size(&def.file)?;
Some((
def.w.unwrap_or(full.0 - def.x),
def.h.unwrap_or(full.1 - def.y),
))
}
/// Whether the bitmap drawn into `rect` is not fully transparent at a point. Unknown
/// pixels count as opaque, so a host that cannot read pixels keeps rectangle hit testing.
pub(crate) fn opaque_at(&mut self, image: Option<&str>, rect: Rect, px: i32, py: i32) -> bool {
let Some(def) = image.and_then(|i| self.bitmap(i)) else {
return true;
};
let Some((w, h)) = self.bitmap_size(&def.id) else {
return true;
};
if rect.width <= 0 || rect.height <= 0 {
return true;
}
let sx = def.x + (px - rect.left) * w / rect.width;
let sy = def.y + (py - rect.top) * h / rect.height;
let Some(p) = self.host.pixel(&def.file, sx, sy) else {
return true;
};
(p >> 24) != 0
}
/// Where `image` drawn into `dst` stops showing on the right: the right edge of its last
/// column with a pixel that is not fully transparent, in layout pixels, or `dst`'s left
/// edge when none shows. The column is looked up once per bitmap part.
pub(crate) fn opaque_right(&mut self, image: &BitmapDef, dst: Rect) -> i32 {
if dst.width <= 0 {
return dst.left;
}
let Some(full) = self.image_size(&image.file) else {
return dst.right();
};
let sw = image.w.unwrap_or(full.0 - image.x);
let sh = image.h.unwrap_or(full.1 - image.y);
// only the part the sheet holds is drawn
let cw = sw.min(full.0 - image.x);
let ch = sh.min(full.1 - image.y);
let no_source = cw <= 0 || ch <= 0;
if image.x < 0 || image.y < 0 || no_source {
return dst.left;
}
let key = format!(
"{}|{}|{}|{}|{}",
image.file.to_lowercase(),
image.x,
image.y,
sw,
sh
);
let column = if let Some(c) = self.opaque_columns.get(&key) {
*c
} else {
let c = if cw as i64 * ch as i64 > MAX_OPAQUE_SCAN {
sw - 1
} else {
match self.host.pixels(&image.file, image.x, image.y, cw, ch) {
None => sw - 1,
Some(px) => (0..cw)
.rev()
.find(|c| (0..ch).any(|r| (px[(r * cw + c) as usize] >> 24) != 0))
.unwrap_or(-1),
}
};
self.opaque_columns.insert(key, c);
c
};
if column < 0 {
return dst.left;
}
// stretched to dst, source column c covers dst pixels up to (c + 1) * dst / source, rounded up
dst.right()
.min(dst.left + ((column + 1) * dst.width + sw - 1) / sw)
}
/// A bitmap font; its `file` may name a bitmap element, which then gives the file and the offset.
pub(crate) fn bitmap_font(&mut self, id: &str) -> Option<Rc<BitmapFontDef>> {
let key = id.to_lowercase();
if let Some(f) = self.resolved_fonts.get(&key) {
return f.clone();
}
let resolved = self.bitmap_fonts.get(&key).map(|def| {
let element = self.bitmaps.get(&def.file.to_lowercase());
// the color theme reaches a bitmap font only through the bitmap element it draws from;
// Winamp 5.666 ignores gammagroup on <bitmapfont> itself
match element {
Some(e) => Rc::new(BitmapFontDef {
id: def.id.clone(),
file: e.file.clone(),
char_width: def.char_width,
char_height: def.char_height,
h_spacing: def.h_spacing,
v_spacing: def.v_spacing,
ox: e.x,
oy: e.y,
gamma_group: e.gamma_group.clone(),
}),
None => Rc::new(BitmapFontDef {
ox: 0,
oy: 0,
gamma_group: None,
..def.clone()
}),
}
});
self.resolved_fonts.insert(key, resolved.clone());
resolved
}
/// The TrueType font `id`.
pub(crate) fn font(&self, id: &str) -> Option<FontDef> {
self.fonts.get(&id.to_lowercase()).cloned()
}
/// The tint of gamma group `group` in the color theme worn now.
pub(crate) fn gamma(&mut self, group: Option<&str>) -> Option<Gamma> {
let group = group?;
if let Some(g) = self.gammas.get(group) {
return *g;
}
let g = self
.current_gamma_set
.as_ref()
.and_then(|set| self.gamma_sets.iter().find(|(n, _)| n == set))
.and_then(|(_, groups)| groups.get(&group.to_lowercase()).copied())
.filter(|g| !g.is_identity());
self.gammas.insert(group.to_string(), g);
g
}
/// The color `id`, tinted by the theme.
pub(crate) fn color(&mut self, id: &str) -> Option<Rgb> {
self.color_at_depth(id, 0)
}
fn color_at_depth(&mut self, id: &str, depth: i32) -> Option<Rgb> {
let key = self.color_key(id)?;
if self.base_colors.contains(&key) {
self.base_usage.insert(format!("color:{key}"));
}
// an alias takes the other color's RGB, tinted by the alias's group (or the other's when it has none)
let c = if let Some(c) = self.colors.get(&key) {
*c
} else {
let alias = self
.color_aliases
.get(&key)
.filter(|_| depth < MAX_ALIAS_DEPTH)?
.clone();
self.plain_color(&alias, depth + 1)?
};
// a color that names no gammagroup takes the theme's "Text" group (Wasabi SDK skinclr)
let group = self
.color_groups
.get(&key)
.cloned()
.or_else(|| {
self.color_aliases
.get(&key)
.and_then(|a| self.color_groups.get(a))
.cloned()
})
.unwrap_or_else(|| "text".to_string());
let Some(g) = self.gamma(Some(&group)) else {
return Some(c);
};
Some(Rgb(g.apply(c.0 | 0xff00_0000) & 0xffffff))
}
/// A color's RGB before any tint, following aliases.
fn plain_color(&self, id: &str, depth: i32) -> Option<Rgb> {
let key = self.color_key(id)?;
if let Some(c) = self.colors.get(&key) {
Some(*c)
} else {
let alias = self
.color_aliases
.get(&key)
.filter(|_| depth < MAX_ALIAS_DEPTH)?;
self.plain_color(alias, depth + 1)
}
}
/// The name a color is defined under, as an RGB or an alias: `id` or its old-style twin, a
/// skin's definition before the wasabi base's. `None` when neither is defined.
fn color_key(&self, id: &str) -> Option<String> {
let names: Vec<String> = names_for(id)
.into_iter()
.filter(|n| self.colors.contains_key(n) || self.color_aliases.contains_key(n))
.collect();
names
.iter()
.find(|n| !self.base_colors.contains(*n))
.or(names.first())
.cloned()
}
}