feat: print a small random pixel-art sprite at the bottom of each slip

A mirrored 8x4 cell sprite, 72 dots high on 80 mm paper, is printed
centred below the quote and differs per copy.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bankosegger
2026-11-18 17:38:34 +01:00
parent a465c5c88f
commit 16993e8231
3 changed files with 204 additions and 35 deletions

65
src/artwork.rs Normal file
View File

@@ -0,0 +1,65 @@
//! A small random pixel-art sprite for the bottom of every printed slip.
//!
//! The sprite is a few chunky cells high and mirrored left to right, which
//! makes random bits read as a little creature or emblem instead of noise.
pub const COLUMNS: usize = 8;
pub const ROWS: usize = 4;
const HALF: usize = COLUMNS / 2;
/// How many of the 16 cells of the left half may be filled: enough to read as
/// a shape, not so many that it turns into a block.
const FILLED: std::ops::RangeInclusive<u32> = 6..=11;
pub type Sprite = [[bool; COLUMNS]; ROWS];
/// The sprite for `seed`; the same seed always gives the same sprite.
pub fn sprite(seed: u64) -> Sprite {
let mut state = seed;
loop {
let bits = next(&mut state) & 0xFFFF;
let row = |index: usize| (bits >> (index * HALF)) & 0xF;
if !FILLED.contains(&bits.count_ones()) || (0..ROWS).any(|index| row(index) == 0) {
continue;
}
return std::array::from_fn(|y| {
std::array::from_fn(|x| row(y) >> x.min(COLUMNS - 1 - x) & 1 == 1)
});
}
}
/// SplitMix64: a few lines, well mixed, and good enough for decoration.
fn next(state: &mut u64) -> u64 {
*state = state.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut mixed = *state;
mixed = (mixed ^ (mixed >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
mixed = (mixed ^ (mixed >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
mixed ^ (mixed >> 31)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sprites_are_mirrored_filled_in_every_row_and_not_a_block() {
for seed in 0..2000 {
let sprite = sprite(seed);
let filled = sprite.iter().flatten().filter(|cell| **cell).count();
assert!((12..=22).contains(&filled), "seed {seed}: {filled} cells");
for row in sprite {
assert!(row.iter().any(|cell| *cell), "seed {seed}: empty row");
assert!(
(0..COLUMNS).all(|x| row[x] == row[COLUMNS - 1 - x]),
"seed {seed}: not mirrored"
);
}
}
}
#[test]
fn a_seed_gives_one_sprite_and_seeds_give_many() {
assert_eq!(sprite(7), sprite(7));
let distinct: std::collections::HashSet<Sprite> = (0..1000).map(sprite).collect();
assert!(distinct.len() > 200, "{} sprites", distinct.len());
}
}