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
// SPDX-License-Identifier: GPL-3.0-or-later
//! A window's drawing as a renderer sees it: the objects' commands with every picture fitted to
//! its sheet and tinted, bitmap text spelled out, and text placed.
use std::rc::Rc;
use super::{SkinRuntime, WindowDrawing};
use crate::draw::{Align, BitmapTextCmd, DrawCmd, Rect, Rgb, VAlign};
use crate::fasthash::FastMap;
use crate::gui::ObjId;
impl SkinRuntime {
/// The drawing of the window `layout` shows.
pub(crate) fn draw_layout(&mut self, layout: ObjId) -> Vec<DrawCmd> {
Rc::unwrap_or_clone(self.draw_layout_owned(layout).cmds)
}
/// The drawing of the window `layout` shows, and the object that drew each command: the
/// innermost object whose drawing it is, the layout for what no child drew, and nothing for
/// the end of a shape or a clip.
pub(crate) fn draw_layout_owned(&mut self, layout: ObjId) -> WindowDrawing {
if self.needs_layout {
self.do_layout();
}
self.dirty = false;
let mut drawn = Vec::new();
self.draw_window(layout, &mut drawn);
let owners = std::mem::take(&mut self.drawn_owners);
let claimed = std::mem::take(&mut self.drawn_spans);
let mut at = Vec::with_capacity(drawn.len() + 1);
let (cmds, owners) = self.finish_drawn(layout, drawn, owners, Some(&mut at));
// an object drawn twice cannot be drawn again in one place
let mut spans = FastMap::default();
for (o, from, to) in claimed {
let span = (at[from], at[to]);
spans
.entry(o)
.and_modify(|s| *s = (usize::MAX, usize::MAX))
.or_insert(span);
}
WindowDrawing {
cmds: Rc::new(cmds),
owners: Rc::new(owners),
spans,
}
}
/// Object `o` of window `layout` drawn again by itself, as its commands come out in the
/// window's drawing.
pub(crate) fn redraw_object(
&mut self,
layout: ObjId,
o: ObjId,
) -> (Vec<DrawCmd>, Vec<Option<ObjId>>) {
let owners = std::mem::take(&mut self.drawn_owners);
let spans = std::mem::take(&mut self.drawn_spans);
let tracking = std::mem::replace(&mut self.tracking_owners, true);
let mut drawn = Vec::new();
self.draw_object(o, &mut drawn);
self.claim_drawn(0, drawn.len(), o);
let mine = std::mem::replace(&mut self.drawn_owners, owners);
self.drawn_spans = spans;
self.tracking_owners = tracking;
self.finish_drawn(layout, drawn, mine, None)
}
/// The commands objects drew for window `layout`, with the object that drew each, as a
/// renderer takes them; `at`, when given, gets where each drawn command starts out.
fn finish_drawn(
&mut self,
layout: ObjId,
drawn: Vec<DrawCmd>,
mut owners: Vec<Option<ObjId>>,
mut at: Option<&mut Vec<usize>>,
) -> (Vec<DrawCmd>, Vec<Option<ObjId>>) {
owners.resize(drawn.len(), None);
for (cmd, owner) in drawn.iter().zip(owners.iter_mut()) {
*owner = match cmd {
DrawCmd::Unclip | DrawCmd::WindowEnd => None,
_ => owner.or(Some(layout)),
};
}
let mut out = Vec::with_capacity(drawn.len());
let mut out_owners = Vec::with_capacity(drawn.len());
for (cmd, owner) in drawn.into_iter().zip(owners) {
if let Some(at) = at.as_deref_mut() {
at.push(out.len());
}
match cmd {
DrawCmd::Image {
bitmap,
dst,
alpha,
smooth,
..
} => {
if let Some((bitmap, dst)) = self.fit_part_to_sheet(&bitmap, dst) {
let gamma = self.gamma(bitmap.gamma_group.as_deref());
out.push(DrawCmd::Image {
bitmap,
dst,
alpha,
gamma,
smooth,
});
out_owners.push(owner);
}
}
DrawCmd::WindowStart(region) => {
let at = self.objects[layout]
.layout()
.map(|d| d.shape_at)
.unwrap_or(-1);
out.push(self.window_start(layout, ®ion, at));
out_owners.push(owner);
}
DrawCmd::BitmapText(cmd) => {
let from = out.len();
self.spell(&cmd, &mut out);
for c in &out[from..] {
out_owners.push(match c {
DrawCmd::Unclip => None,
_ => owner,
});
}
}
DrawCmd::Text(mut cmd) => {
self.place_text(&mut cmd);
out.push(DrawCmd::Text(cmd));
out_owners.push(owner);
}
other => {
out.push(other);
out_owners.push(owner);
}
}
}
if let Some(at) = at {
at.push(out.len());
}
(out, out_owners)
}
/// The layout's drawing: inside its shape when it has one.
fn draw_window(&mut self, layout: ObjId, out: &mut Vec<DrawCmd>) {
if !self.objects[layout].base.visible {
return;
}
let desktop_alpha = self.objects[layout]
.layout()
.is_some_and(|d| d.desktop_alpha);
let (shape, _) = if desktop_alpha {
(None, -1)
} else {
self.window_shape(layout)
};
// the shape may have drawn objects to measure them: the owners start here
self.drawn_owners.clear();
self.drawn_spans.clear();
self.tracking_owners = true;
match shape {
None => {
if !desktop_alpha {
out.push(self.window_black(layout));
}
self.draw_object(layout, out);
}
Some(shape) => {
out.push(DrawCmd::WindowStart(shape));
out.push(self.window_black(layout));
self.draw_object(layout, out);
out.push(DrawCmd::WindowEnd);
}
}
self.tracking_owners = false;
}
/// Black over the whole window, under its objects: Winamp fills a window's canvas with black
/// before the objects draw, so a pixel inside the window that no object draws is black (the
/// shape still cuts the window's outline). A `desktopalpha` window starts clear instead.
fn window_black(&mut self, layout: ObjId) -> DrawCmd {
let (w, h) = self.layout_size(layout);
DrawCmd::Fill {
rect: Rect::new(0, 0, w, h),
color: Rgb(0),
alpha: 255,
}
}
/// Commands `from` to `to` of the window being drawn, as `owner` drew them unless a child of
/// it did: the innermost object claims a command.
pub(crate) fn claim_drawn(&mut self, from: usize, to: usize, owner: ObjId) {
if !self.tracking_owners {
return;
}
self.drawn_spans.push((owner, from, to));
if self.drawn_owners.len() < to {
self.drawn_owners.resize(to, None);
}
for o in &mut self.drawn_owners[from..to] {
if o.is_none() {
*o = Some(owner);
}
}
}
/// `cmd` as a renderer draws it, added to `out`: inside its rectangle, each character's cell
/// of the font sheet at its place, aligned by the text's width and less its scroll, a colon
/// `colon_width` wide where the skin narrows it, a space advancing only.
fn spell(&mut self, cmd: &BitmapTextCmd, out: &mut Vec<DrawCmd>) {
let f = &cmd.font;
let r = cmd.rect;
let advance = f.char_width + f.h_spacing;
let colon = cmd.colon_width.unwrap_or(advance);
let total: i32 = cmd
.text
.chars()
.map(|c| if c == ':' { colon } else { advance })
.sum();
let mut x = match cmd.align {
Align::Left => r.left,
Align::Center => r.left + (r.width - total) / 2,
Align::Right => r.right() - total,
} - cmd.scroll;
let y = match cmd.valign {
VAlign::Top => r.top,
VAlign::Center => r.top + (r.height - f.char_height) / 2,
VAlign::Bottom => r.bottom() - f.char_height,
};
let gamma = self.gamma(f.gamma_group.as_deref());
out.push(DrawCmd::Clip(r));
for c in cmd.text.chars() {
if let Some(cell) = f.cell(c) {
let g = super::objects::glyph(f, cell);
if let Some((bitmap, dst)) =
self.fit_part_to_sheet(&g, Rect::new(x, y, f.char_width, f.char_height))
{
out.push(DrawCmd::Image {
bitmap,
dst,
alpha: cmd.alpha,
gamma,
smooth: false,
});
}
}
x += if c == ':' { colon } else { advance };
}
out.push(DrawCmd::Unclip);
}
}