From fbf06b8d2f2ca016659e912d6fc3a5b0b1b8a765 Mon Sep 17 00:00:00 2001 From: bankosegger Date: Sat, 14 Nov 2026 15:47:58 +0100 Subject: [PATCH] fix: send printed text in the printer's PC437 code page UTF-8 bytes came out as stray glyphs, so umlauts and the sharp s in the captions and quotes were unreadable. --- src/printer.rs | 87 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 21 deletions(-) diff --git a/src/printer.rs b/src/printer.rs index 0be09e5..0ad3fb8 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -27,8 +27,8 @@ //! 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. +//! anything. Text goes out in the printer's own code page (PC437), since it +//! prints UTF-8 bytes as separate glyphs; see `code_page`. //! //! Each photo is converted to gray, resized to the exact dot width, contrast //! equalized (CLAHE), slightly sharpened, gamma brightened and finally Atkinson @@ -402,6 +402,29 @@ fn encode( job } +/// The German letters in the captions as single PC437 bytes, the code page the +/// booth's printer starts in. Anything else non-ASCII would need a wider table +/// or `ESC t`, so a test keeps the printed text within this set. +fn code_page(character: char) -> Option { + Some(match character { + 'ä' => 0x84, + 'ö' => 0x94, + 'ü' => 0x81, + 'Ä' => 0x8E, + 'Ö' => 0x99, + 'Ü' => 0x9A, + 'ß' => 0xE1, + ascii if ascii.is_ascii() => ascii as u8, + _ => return None, + }) +} + +/// Appends one line of text, encoded for the printer. +fn text_line(write: &mut Vec, line: &str) { + write.extend(line.chars().map(|character| code_page(character).unwrap_or(b'?'))); + write.push(b'\n'); +} + /// Starts a write with `ESC @` and the head settings, then centres the text. fn centered(head: &Head) -> Vec { let mut write = vec![0x1B, b'@']; // ESC @: initialize @@ -417,9 +440,9 @@ const SMALL: [u8; 6] = [0x1D, b'!', 0x00, 0x1B, b'E', 0]; fn header(head: &Head) -> Vec { let mut write = centered(head); write.extend(LARGE); - write.extend(format!("{EVENT_TITLE}\n").as_bytes()); + text_line(&mut write, EVENT_TITLE); write.extend(SMALL); - write.extend(format!("{EVENT_PLACE}\n").as_bytes()); + text_line(&mut write, EVENT_PLACE); write.extend([0x1B, b'd', 1]); // a blank line between caption and photo write } @@ -437,13 +460,14 @@ fn footer(head: &Head, stamp: &str, quote: &[&str; 2]) -> Vec { let mut write = centered(head); write.extend([0x1B, b'd', 1]); write.extend(LARGE); - write.extend(format!("{KEEPSAKE_DE}\n").as_bytes()); + text_line(&mut write, KEEPSAKE_DE); write.extend(SMALL); - write.extend(format!("{KEEPSAKE_EN}\n").as_bytes()); + text_line(&mut write, KEEPSAKE_EN); write.extend([0x1B, b'd', 1]); - write.extend(format!("{stamp}\n").as_bytes()); + text_line(&mut write, stamp); write.extend([0x1B, b'd', 1]); - write.extend(format!("{}\n{}\n", quote[0], quote[1]).as_bytes()); + text_line(&mut write, quote[0]); + text_line(&mut write, quote[1]); 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 @@ -455,6 +479,19 @@ mod tests { const STAMP: &str = "14.11.2026, 15:42"; + /// Every line the booth prints, large or small. + fn printed_lines() -> impl Iterator { + [EVENT_TITLE, EVENT_PLACE, KEEPSAKE_DE, KEEPSAKE_EN, STAMP] + .into_iter() + .chain(QUOTES.iter().flatten().copied()) + } + + fn contains_line(write: &[u8], line: &str) -> bool { + let mut needle = Vec::new(); + text_line(&mut needle, line); + write.windows(needle.len()).any(|window| window == needle) + } + #[test] fn printed_lines_fit_an_80_mm_line() { // Font A is 12 dots wide, so 48 columns; the large lines take two each. @@ -462,14 +499,25 @@ mod tests { 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 { + for line in printed_lines().filter(|line| ![EVENT_TITLE, KEEPSAKE_DE].contains(line)) { assert!(line.chars().count() <= columns, "{line}"); } } + #[test] + fn german_text_is_sent_in_the_printers_code_page() { + let mut write = Vec::new(); + text_line(&mut write, "schönste Prüfung, Fächer, Straße"); + assert_eq!(write, b"sch\x94nste Pr\x81fung, F\x84cher, Stra\xE1e\n"); + // No printed line needs a character the code page lacks. + for line in printed_lines() { + assert!( + line.chars().all(|character| code_page(character).is_some()), + "{line}" + ); + } + } + #[test] fn quotes_are_picked_from_the_list_and_vary() { let picked: std::collections::BTreeSet = @@ -603,16 +651,12 @@ mod tests { data: vec![0; 1], }; let job = encode(&bitmap, &Head::default(), 2, STAMP, 6); - let text: Vec = job - .iter() - .map(|write| String::from_utf8_lossy(write).into_owned()) - .collect(); - 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)); + assert!(contains_line(&job[0], EVENT_TITLE) && contains_line(&job[0], EVENT_PLACE)); + assert!(contains_line(&job[2], KEEPSAKE_DE) && contains_line(&job[2], KEEPSAKE_EN)); + assert!(contains_line(&job[2], STAMP)); // 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!(contains_line(&job[2], QUOTES[6][0]) && contains_line(&job[2], QUOTES[6][1])); + assert!(contains_line(&job[5], QUOTES[7][0]) && contains_line(&job[5], 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..]) @@ -632,3 +676,4 @@ mod tests { +