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
// SPDX-License-Identifier: GPL-3.0-or-later
//! Text placement, window shapes and fitting pictures to their sheets.
use super::{
BitmapDef, DrawCmd, Fit, FontDef, Gamma, ObjId, Rc, Rect, Region, SkinRuntime, DEFAULT_ASCENT,
DEFAULT_DESCENT,
};
impl SkinRuntime {
/// Ascent and descent in whole pixels as Windows lays the face out at `size`, read from
/// its file; `None` when the file gives none (or there is no file).
fn gdi_line(&mut self, font: Option<&FontDef>, size: i32, bold: bool) -> Option<(i32, i32)> {
let px = crate::gdi::em_size(size);
let classic = self.classic_skin;
self.gdi_metrics(font, bold).map(|m| {
classic
.then(|| m.measured(px))
.flatten()
.unwrap_or_else(|| m.at(px))
})
}
/// The height of the face's line at `px` pixels per em as Windows lays it out, where its
/// file says it for that size (see [`crate::gdi::GdiLineMetrics::measured`]).
pub(crate) fn gdi_measured_height(&mut self, font: Option<&FontDef>, px: i32) -> Option<i32> {
let (a, d) = self.gdi_metrics(font, false)?.measured(px as f32)?;
Some(a + d)
}
/// Windows' line of the face, read from its file once.
fn gdi_metrics(
&mut self,
font: Option<&FontDef>,
bold: bool,
) -> Option<crate::gdi::GdiLineMetrics> {
let face = crate::gdi::FontFace::of(font, bold);
let key = match &face {
crate::gdi::FontFace::StandIn { .. } => face.file()?,
crate::gdi::FontFace::SkinFile { font, .. } => font.id.to_string(),
crate::gdi::FontFace::Default { .. } => return None,
};
let m = if let Some(m) = self.gdi_lines.get(&key) {
m.clone()
} else {
let bytes: Option<Rc<[u8]>> = match &face {
crate::gdi::FontFace::StandIn { .. } => self.host.font_file(&key),
crate::gdi::FontFace::SkinFile { font, .. } => self.files.read(&font.file),
crate::gdi::FontFace::Default { .. } => None,
};
let m = bytes.and_then(|b| crate::gdi::GdiLineMetrics::read(&b));
self.gdi_lines.insert(key, m.clone());
m
};
m
}
/// How wide `text` draws in layout pixels: smoothed (`smooth`) by the host's measure for
/// smoothed text where it has one, else by its measure for any text.
pub(crate) fn text_advance_of(
&mut self,
text: &str,
font: Option<&FontDef>,
size: i32,
bold: bool,
smooth: bool,
) -> f32 {
if smooth {
if let Some(w) = self.host.smooth_text_advance(text, font, size, bold) {
return w;
}
}
self.host.text_advance(text, font, size, bold)
}
/// Places `cmd` in its rectangle, as every renderer then draws it: across by the width the
/// host measures, less the ticker's scroll; up and down by the font's line: for smoothed
/// text the line the host lays it out by, if it gives one, else Windows' where the file
/// gives it, else the host's. Centered and right-aligned text starts on a whole
/// pixel, as Windows places it: its width rounded, half the room left over dropping the odd
/// pixel, so the letters stay sharp instead of falling between two pixels.
pub(crate) fn place_text(&mut self, cmd: &mut crate::draw::TextCmd) {
let r = cmd.rect;
let width = self
.text_advance_of(&cmd.text, cmd.font.as_ref(), cmd.size, cmd.bold, cmd.smooth)
.round() as i32;
let x = match cmd.align {
crate::draw::Align::Left => r.left,
crate::draw::Align::Center => r.left + (r.width - width) / 2,
crate::draw::Align::Right => r.right() - width,
};
cmd.x = (x - cmd.scroll) as f32;
let modern = if cmd.smooth {
self.host
.smooth_line_metrics(cmd.font.as_ref(), cmd.size, cmd.bold)
} else {
None
};
let (asc, desc) = modern
.or_else(|| {
self.gdi_line(cmd.font.as_ref(), cmd.size, cmd.bold)
.map(|(a, d)| (a as f32, d as f32))
})
.or_else(|| {
self.host
.line_metrics(cmd.font.as_ref(), cmd.size, cmd.bold)
})
.unwrap_or_else(|| {
let em = crate::gdi::em_size(cmd.size);
(em * DEFAULT_ASCENT, em * DEFAULT_DESCENT)
});
let centered = r.top as f32 + ((r.height as f32 - (asc + desc)) / 2.0).trunc() + asc;
cmd.baseline = match cmd.valign {
crate::draw::VAlign::Top => r.top as f32 + asc,
crate::draw::VAlign::Center => centered,
crate::draw::VAlign::Bottom => r.bottom() as f32 - desc,
};
}
// ---- shapes and fitting ------------------------------------------------------------------
/// The start of `layout`'s shaped window with its pictures fitted to their sheets and tinted
/// for the color theme. A window keeps its shape until the skin changes, and a new theme
/// changes it, so the same shape gives back the same command from frame to frame.
pub(crate) fn window_start(
&mut self,
layout: ObjId,
region: &Region,
shape_at: i64,
) -> DrawCmd {
if let Some((at, cmd)) = self.shaped.get(&layout) {
if *at == shape_at {
return cmd.clone();
}
}
let mut fitted = region
.map_pics(&mut |img| self.fit_region_image(img))
.unwrap_or(Region::Op {
base: None,
add: Vec::new(),
keep: Vec::new(),
cut: Vec::new(),
});
let mut gammas: Vec<Option<Gamma>> = Vec::new();
fitted.pics_mut(&mut |img| {
gammas.push(Some(img.gamma.unwrap_or(Gamma {
r: 0,
g: 0,
b: 0,
boost: 0,
gray: 0,
})));
});
// tints are looked up through the runtime, so apply them in a second pass
let mut groups: Vec<Option<Rc<str>>> = Vec::new();
fitted.pics_mut(&mut |img| groups.push(img.bitmap.gamma_group.clone()));
let tints: Vec<Option<Gamma>> = groups.iter().map(|g| self.gamma(g.as_deref())).collect();
let mut i = 0;
fitted.pics_mut(&mut |img| {
img.gamma = tints[i];
i += 1;
});
let cmd = DrawCmd::WindowStart(fitted);
self.shaped.insert(layout, (shape_at, cmd.clone()));
cmd
}
fn fit_region_image(
&mut self,
img: &crate::draw::RegionImage,
) -> Option<crate::draw::RegionImage> {
let (bitmap, dst) = self.fit_part_to_sheet(&img.bitmap, img.dst)?;
Some(crate::draw::RegionImage {
bitmap,
dst,
alpha: img.alpha,
gamma: img.gamma,
smooth: img.smooth,
})
}
/// The part of an image command that lies on its sheet, with the destination cut the same
/// way: Winamp draws nothing for rows or columns a bitmap element asks for past the edge of
/// its file. `None` when nothing is left.
pub(crate) fn fit_part_to_sheet(
&mut self,
b: &BitmapDef,
dst: Rect,
) -> Option<(BitmapDef, Rect)> {
if !self.fits.contains_key(b) {
if let Some(fit) = self.fit_part(b) {
self.fits.insert(b.clone(), fit);
}
}
match self.fits.get(b) {
None | Some(Fit::Whole) => Some((b.clone(), dst)),
Some(Fit::None) => None,
Some(Fit::Cut {
part,
asked_w,
asked_h,
}) => {
let fw = part.w.unwrap_or(*asked_w);
let fh = part.h.unwrap_or(*asked_h);
// a stretched picture keeps its scale: the destination shrinks by the same share
let dw = if fw == *asked_w {
dst.width
} else {
dst.width * fw / *asked_w
};
let dh = if fh == *asked_h {
dst.height
} else {
dst.height * fh / *asked_h
};
if dw <= 0 || dh <= 0 {
None
} else {
Some((part.clone(), Rect::new(dst.left, dst.top, dw, dh)))
}
}
}
}
/// Works out once how much of the part `b` names lies on its sheet; a sheet whose size is
/// not known yet leaves it to the next frame.
fn fit_part(&mut self, b: &BitmapDef) -> Option<Fit> {
let (sheet_w, sheet_h) = self.image_size(&b.file)?;
let sw = b.w.unwrap_or(sheet_w - b.x);
let sh = b.h.unwrap_or(sheet_h - b.y);
let fw = sw.min(sheet_w - b.x);
let fh = sh.min(sheet_h - b.y);
let empty = fw <= 0 || fh <= 0;
let outside = sw <= 0 || sh <= 0;
Some(if empty || outside {
Fit::None
} else if fw == sw && fh == sh && b.w.is_some() && b.h.is_some() {
Fit::Whole
} else {
Fit::Cut {
part: b.part(b.x, b.y, Some(fw), Some(fh)),
asked_w: sw,
asked_h: sh,
}
})
}
}