From 92e2db54b3334dfcbb489fd4ffab2d858d1a1ea3 Mon Sep 17 00:00:00 2001 From: bankosegger Date: Sat, 14 Nov 2026 15:42:49 +0100 Subject: [PATCH] feat: print a random TU Wien quote under each photo --- src/printer.rs | 118 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 100 insertions(+), 18 deletions(-) diff --git a/src/printer.rs b/src/printer.rs index 310c74e..0be09e5 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -24,11 +24,11 @@ //! band and are not stored by the printer (no memory switch writes). //! //! Each photo is captioned with the event name and place above it, and a -//! keepsake line plus the capture date and time below it, in German with the -//! English directly underneath. The timestamp is the system clock in local -//! time, so the booth's clock has to be right for it to mean anything. The -//! captions are ASCII only: the printer's code page is not configured here, so -//! `Treitlstrasse` is spelled out rather than risking a mangled sharp s. +//! keepsake line, the capture date and time and one of `QUOTES` below it, in +//! German with the English directly underneath. The timestamp is the system +//! clock in local time, so the booth's clock has to be right for it to mean +//! anything. Text is sent as UTF-8, which the booth's printer renders without +//! a code page being selected. //! //! Each photo is converted to gray, resized to the exact dot width, contrast //! equalized (CLAHE), slightly sharpened, gamma brightened and finally Atkinson @@ -69,11 +69,47 @@ const BAND_ROWS: usize = 64; const FEED_LINES: u8 = 4; /// Caption printed around every photo. const EVENT_TITLE: &str = "WINFNACHTEN"; -const EVENT_PLACE: &str = "WINF Treitlstrasse 3"; +const EVENT_PLACE: &str = "WINF Treitlstraße 3"; const KEEPSAKE_DE: &str = "Ich war dabei!"; const KEEPSAKE_EN: &str = "I was there!"; /// Capture time, written the German way and the same in both languages. const STAMP_FORMAT: &str = "%d.%m.%Y, %H:%M"; +/// One is printed per slip. Each line is short enough for a single 80 mm line; +/// on 58 mm paper the longer ones wrap. +const QUOTES: [[&str; 2]; 8] = [ + [ + "Analysis bestanden. Wunder gibt es doch.", + "Passed Analysis. Miracles do happen.", + ], + [ + "Im Freihaus gibt es Kaffee und Hoffnung.", + "The Freihaus serves coffee and hope.", + ], + [ + "TISS sagt nein. Mein Herz sagt auch nein.", + "TISS says no. So does my heart.", + ], + [ + "WINF: zwei Fächer, eine Deadline.", + "WINF: two subjects, one deadline.", + ], + [ + "Karlsplatz: der schönste Umweg zur Uni.", + "Karlsplatz: the prettiest detour to class.", + ], + [ + "Prüfungswoche: Schlafen ist optional.", + "Exam week: sleeping is optional.", + ], + [ + "Die Bibliothek kennt alle meine Ausreden.", + "The library has heard all my excuses.", + ], + [ + "Mathe ist einfach, sagt die Mathematik.", + "Maths is easy, says maths.", + ], +]; /// Pause between writes, leaving the head time to drain a band (8 mm of paper) /// before the next one arrives. const PACE: Duration = Duration::from_millis(100); @@ -210,7 +246,7 @@ impl Printer { let bitmap = render(frame, self.dots, self.gamma).map_err(|error| format!("render: {error}"))?; let stamp = Local::now().format(STAMP_FORMAT).to_string(); - self.send(&encode(&bitmap, &self.head, copies, &stamp)) + self.send(&encode(&bitmap, &self.head, copies, &stamp, first_quote())) } /// Sends the job one write at a time, pacing them to the print head. @@ -341,9 +377,15 @@ fn dither(gray: &[u8], width: usize, rows: usize) -> Vec { /// tested Birch 0CP-Q3 only keeps sync when `GS v 0` follows `ESC @` directly: /// given several bands in one initialized stream it abandons the image and /// prints the remaining dot data as text, about two metres of it per photo. -fn encode(bitmap: &Bitmap, head: &Head, copies: usize, stamp: &str) -> Vec> { +fn encode( + bitmap: &Bitmap, + head: &Head, + copies: usize, + stamp: &str, + first_quote: usize, +) -> Vec> { let mut job = Vec::new(); - for _ in 0..copies { + for copy in 0..copies { job.push(header(head)); for band in bitmap.data.chunks(BAND_ROWS * bitmap.width_bytes) { let rows = band.len() / bitmap.width_bytes; @@ -355,7 +397,7 @@ fn encode(bitmap: &Bitmap, head: &Head, copies: usize, stamp: &str) -> Vec Vec { write } -fn footer(head: &Head, stamp: &str) -> Vec { +/// Which quote the next slip gets. The clock is random enough for a joke, and +/// saves pulling in a generator. +fn first_quote() -> usize { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |since| since.subsec_nanos() as usize) + % QUOTES.len() +} + +fn footer(head: &Head, stamp: &str, quote: &[&str; 2]) -> Vec { let mut write = centered(head); write.extend([0x1B, b'd', 1]); write.extend(LARGE); @@ -391,6 +442,8 @@ fn footer(head: &Head, stamp: &str) -> Vec { write.extend(format!("{KEEPSAKE_EN}\n").as_bytes()); write.extend([0x1B, b'd', 1]); write.extend(format!("{stamp}\n").as_bytes()); + write.extend([0x1B, b'd', 1]); + write.extend(format!("{}\n{}\n", quote[0], quote[1]).as_bytes()); write.extend([0x1B, b'd', FEED_LINES]); // ESC d n: feed n lines write.extend([0x1D, b'V', 66, 3]); // GS V 66 n: feed and partial cut write @@ -402,6 +455,30 @@ mod tests { const STAMP: &str = "14.11.2026, 15:42"; + #[test] + fn printed_lines_fit_an_80_mm_line() { + // Font A is 12 dots wide, so 48 columns; the large lines take two each. + let columns = DEFAULT_DOTS / 12; + for line in [EVENT_TITLE, KEEPSAKE_DE] { + assert!(line.chars().count() <= columns / 2, "{line}"); + } + let regular = [EVENT_PLACE, KEEPSAKE_EN, STAMP] + .into_iter() + .chain(QUOTES.iter().flatten().copied()); + for line in regular { + assert!(line.chars().count() <= columns, "{line}"); + } + } + + #[test] + fn quotes_are_picked_from_the_list_and_vary() { + let picked: std::collections::BTreeSet = + (0..1000).map(|_| first_quote()).collect(); + assert!(picked.last().is_some_and(|last| *last < QUOTES.len())); + // Not all eight on a coarse clock, but a slip must not be predictable. + assert!(picked.len() > 1, "{picked:?}"); + } + #[test] fn parses_device_and_network_targets() { assert_eq!(Target::parse(" "), None); @@ -461,7 +538,7 @@ mod tests { width_bytes: 1, data: vec![0; BAND_ROWS + 1], }; - let job = encode(&bitmap, &Head::from_values(Some("85"), Some("quality")), 1, STAMP); + let job = encode(&bitmap, &Head::from_values(Some("85"), Some("quality")), 1, STAMP, 0); for band in &job[1..3] { assert_eq!( &band[..12], @@ -501,7 +578,7 @@ mod tests { width_bytes: 2, data: vec![0xAA; 2 * (BAND_ROWS + 1)], }; - let job = encode(&bitmap, &Head::default(), 2, STAMP); + let job = encode(&bitmap, &Head::default(), 2, STAMP, 0); // Per copy: the caption, a full band, the leftover row, then the footer. assert_eq!(job.len(), 2 * 4); for copy in job.chunks(4) { @@ -525,7 +602,7 @@ mod tests { width_bytes: 1, data: vec![0; 1], }; - let job = encode(&bitmap, &Head::default(), 2, STAMP); + let job = encode(&bitmap, &Head::default(), 2, STAMP, 6); let text: Vec = job .iter() .map(|write| String::from_utf8_lossy(write).into_owned()) @@ -533,10 +610,14 @@ mod tests { assert!(text[0].contains(EVENT_TITLE) && text[0].contains(EVENT_PLACE)); assert!(text[2].contains(KEEPSAKE_DE) && text[2].contains(KEEPSAKE_EN)); assert!(text[2].contains(STAMP)); - // The captions are ASCII, so no code page has to be selected. - assert!(header(&Head::default()).iter().all(u8::is_ascii)); - assert!(footer(&Head::default(), STAMP).iter().all(u8::is_ascii)); - // The real stamp is ASCII too. + // Consecutive copies get consecutive quotes, wrapping round the list. + assert!(text[2].contains(QUOTES[6][0]) && text[2].contains(QUOTES[6][1])); + assert!(text[5].contains(QUOTES[7][0]) && text[5].contains(QUOTES[7][1])); + assert!( + encode(&bitmap, &Head::default(), 1, STAMP, QUOTES.len() - 1)[2] + .ends_with(&footer(&Head::default(), STAMP, "ES[QUOTES.len() - 1])[1..]) + ); + // The real stamp is as wide as the one used here. let now = Local::now().format(STAMP_FORMAT).to_string(); assert!(now.is_ascii() && now.len() == STAMP.len(), "{now}"); let cuts = job @@ -550,3 +631,4 @@ mod tests { +