Rust
The engine is the walkit crate. It has no dependencies unless the host asks for the
truetype feature. It is on crates.io:
[dependencies]walkit = "0.10"A host
Section titled “A host”A host implements SkinHost for pictures, text and
logging, and Player for the transport.
FakePlayer plays nothing and is enough for a first
look at a skin.
use std::rc::Rc;use std::time::Instant;
use walkit::draw::{DrawCmd, FontDef};use walkit::files::{LayeredFiles, MemoryFiles};use walkit::host::{Clock, SkinHost, SkinSetup};use walkit::player::FakePlayer;use walkit::SkinRuntime;
struct Host;
impl SkinHost for Host { fn image_size(&mut self, file: &str) -> Option<(i32, i32)> { todo!("decode the picture and return its size") } fn pixel(&mut self, file: &str, x: i32, y: i32) -> Option<u32> { todo!("one pixel, ARGB; override `pixels` to read a block at once") } fn text_width(&mut self, text: &str, font: Option<&FontDef>, size: i32, bold: bool) -> i32 { todo!("measure with the host's font library") } fn log(&mut self, message: &str) { eprintln!("{message}"); } fn invalidate(&mut self) { // something changed: tick and draw soon }}
fn main() { let mut skin = MemoryFiles::new(); // insert each entry of the .wal or .wsz archive: skin.insert(path, bytes) let wasabi = MemoryFiles::new(); // the entries of Winamp's wasabi folder, for a modern skin
let start = Instant::now(); let clock: Clock = Rc::new(move || start.elapsed().as_millis() as i64);
let mut rt = SkinRuntime::new( Box::new(LayeredFiles::new(Box::new(skin), Box::new(wasabi))), Box::new(Host), Box::new(FakePlayer::new(clock.clone())), SkinSetup { skin_name: "Winamp Modern".into(), ..SkinSetup::default() }, clock, ); rt.set_viewport(1080, 1920); rt.load();
// every frame: if rt.tick() { for (rect, commands) in rt.draw_windows() { for command in commands { match command { DrawCmd::Image { bitmap, dst, alpha, .. } => { /* draw the bitmap */ } DrawCmd::Fill { rect, color, alpha } => { /* fill the box */ } _ => { /* text, clips, shapes, pixels */ } } } } }
// input, in viewport pixels; the last argument names the touch rt.pointer_down(100, 200, 1); rt.pointer_up(100, 200);}examples/macos is a complete
desktop app built this way: a SwiftUI app over a small Rust library that runs the engine behind
a C interface and draws the draw list with a software renderer.
The engine places every run of text and the host draws its letters. A host with its own font
library measures and draws with that. A host that draws into a buffer of pixels can take the
engine’s own letters instead, with the truetype feature (it adds the skrifa crate):
[dependencies]walkit = { version = "0.10", features = ["truetype"] }walkit::truetype draws TrueType text as Winamp
draws it, in one of two styles. Each DrawCmd::Text says which: smooth is set for a modern
skin’s text, unless the skin sets antialias="0" on it.
Style::Classic, for a classic skin’s text and unsmoothed text, as Windows draws it:
- The font’s own hinting fits each letter to the pixel grid.
- Letters advance by whole pixels: the width the font’s
hdmxorLTSHtable gives for the size, else the hinted width. Pairs in the font’skerntable move by whole pixels, unless the font’sGPOStable leaves kerning out of Latin script: Tahoma is not kerned, Arial is. - Letters are smoothed only at the sizes the font’s
gasptable asks for. Arial from 9 to 17 pixels, the classic playlist’s font, is not smoothed. A smoothed letter is sampled 4 by 4 per pixel and blends with a gamma of 2.3: itsGlyph::gamma, forwalkit::truetype::blend. - A stroke thinner than a pixel that passes between two pixel centers still lights one pixel.
- A pixel center exactly on the outline is inside, except at the right end of a stroke’s last row: the foot of a V at 10 pixels lights one pixel, the arm of a K keeps its own.
Style::Modern, for smoothed text:
- Always smoothed, each pixel as dark as the share of it a letter covers.
- An autohinter, not the font’s own hinting, fits each letter to the grid.
- The size is three quarters of the skin’s
fontsize, not rounded:fontsize="14"is 10.5 pixels per em. Letters advance by whole pixels. - Kerning pairs are scaled down below 25 pixels per em, then rounded.
- Bold is the regular letters drawn twice, one pixel apart, each letter one pixel wider, even where the font has a bold cut.
- The line the engine centers the text on reaches three pixels above a capital H and down to a pixel above the foot of a g.
use walkit::truetype::{em, Font, Style};
let font = Font::parse(bytes).expect("a TrueType file"); // once per font filelet style = if cmd.smooth { Style::Modern } else { Style::Classic };let px = em(style, size); // pixels per em for a skin's font sizelet bold = if fake_bold { 1 } else { 0 }; // how far apart the two strokes are
// SkinHost::text_advance and line_metrics in Style::Classic,// smooth_text_advance and smooth_line_metrics in Style::Modernlet width = font.width(text, style, px, bold);let (ascent, descent) = font.line(style, px);
// DrawCmd::Text: start on a whole pixel, then one bitmap per letterlet (x, baseline) = (cmd.x.round() as i32, cmd.baseline.round() as i32);for (at, glyph) in font.run(&cmd.text, style, px, bold) { // glyph.coverage is glyph.w x glyph.h bytes, 255 the text's color; // its corner is (x + at + glyph.left, baseline - glyph.top); // over an opaque pixel: blend(color, under, coverage, glyph.gamma)}A face the engine marks fake_bold has no bold cut: draw the run twice, one pixel apart. In
Style::Modern draw a bold run from the regular face that way too. At a scale above one, ask
for em(style, size) * scale, pass the scale as bold, and multiply the pen’s place by the
scale. The macOS example’s host.rs and render.rs work this way.
Compared with Winamp 5.666 in a virtual machine, with Arial and Tahoma themselves:
- The rows of a classic playlist come out pixel for pixel at the usual sizes. At sizes Arial smooths (18 and 24 pixels) and with Tahoma (10 to 24) the rows land where Winamp puts them and the letters match but for a few edge pixels and the odd letter a pixel off.
- Smoothed modern text comes out as Winamp draws it but for some letters sitting one pixel off (“1”, “L”, some digits), and bold strokes that are a little lighter than Winamp’s.
The engine sizes a classic playlist’s rows by the font’s own height for the size, from its
VDMX table, as Windows does: exact with Arial and Tahoma themselves. The free fonts that
stand in for them have no such table, so the rows keep Arial’s 5/4 of the em, which is right
up to 12 pixels; their letters differ from Arial’s and Tahoma’s, as the fonts do.
MemoryFiles holds a skin’s entries by path. The
engine reads no archive itself, so the host unzips the skin with any zip library.
LayeredFiles puts a skin over Winamp’s
wasabi base: paths under wasabi-base/ read from the base, everything
else from the skin. Winamp draws lists, scrollbars and frames a skin does not define from it.
The engine recognizes a classic skin by its main.bmp and generates its layout. Under a
classic skin, the base is a classic base skin instead: Winamp’s base skin,
say, unzipped into a MemoryFiles. The engine takes every bitmap the skin leaves out from it.
The clock
Section titled “The clock”Every timer, animation and script reads the Clock, in
milliseconds. A host passes the wall clock. A test steps one, so animations and timers land on
the same frame every run.