feat: caption printed photos with the event and a keepsake line
This commit is contained in:
@@ -23,6 +23,11 @@
|
||||
//! unless the printer is known to understand them. They are sent with every
|
||||
//! 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 below, in German with the English directly underneath. 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.
|
||||
//!
|
||||
//! Each photo is converted to gray, resized to the exact dot width, contrast
|
||||
//! equalized (CLAHE), slightly sharpened, gamma brightened and finally Atkinson
|
||||
//! dithered, in that order: scaling a dithered image would ruin the pattern.
|
||||
@@ -59,6 +64,11 @@ const NETWORK_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
/// Raster rows per `GS v 0` command, 4.5 KiB per band at 576 dots.
|
||||
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 KEEPSAKE_DE: &str = "Ich war dabei!";
|
||||
const KEEPSAKE_EN: &str = "I was there!";
|
||||
/// 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);
|
||||
@@ -328,6 +338,7 @@ fn dither(gray: &[u8], width: usize, rows: usize) -> Vec<u8> {
|
||||
fn encode(bitmap: &Bitmap, head: &Head, copies: usize) -> Vec<Vec<u8>> {
|
||||
let mut job = Vec::new();
|
||||
for _ 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;
|
||||
let mut write = vec![0x1B, b'@']; // ESC @: initialize
|
||||
@@ -338,14 +349,45 @@ fn encode(bitmap: &Bitmap, head: &Head, copies: usize) -> Vec<Vec<u8>> {
|
||||
write.extend(band);
|
||||
job.push(write);
|
||||
}
|
||||
job.push(vec![
|
||||
0x1B, b'd', FEED_LINES, // ESC d n: feed n lines
|
||||
0x1D, b'V', 66, 3, // GS V 66 n: feed and partial cut
|
||||
]);
|
||||
job.push(footer(head));
|
||||
}
|
||||
job
|
||||
}
|
||||
|
||||
/// Starts a write with `ESC @` and the head settings, then centres the text.
|
||||
fn centered(head: &Head) -> Vec<u8> {
|
||||
let mut write = vec![0x1B, b'@']; // ESC @: initialize
|
||||
write.extend(head.commands());
|
||||
write.extend([0x1B, b'a', 1]); // ESC a n: centre the following lines
|
||||
write
|
||||
}
|
||||
|
||||
/// Emphasizes the next line: double width and height, bold.
|
||||
const LARGE: [u8; 6] = [0x1D, b'!', 0x11, 0x1B, b'E', 1]; // GS ! n, ESC E n
|
||||
const SMALL: [u8; 6] = [0x1D, b'!', 0x00, 0x1B, b'E', 0];
|
||||
|
||||
fn header(head: &Head) -> Vec<u8> {
|
||||
let mut write = centered(head);
|
||||
write.extend(LARGE);
|
||||
write.extend(format!("{EVENT_TITLE}\n").as_bytes());
|
||||
write.extend(SMALL);
|
||||
write.extend(format!("{EVENT_PLACE}\n").as_bytes());
|
||||
write.extend([0x1B, b'd', 1]); // a blank line between caption and photo
|
||||
write
|
||||
}
|
||||
|
||||
fn footer(head: &Head) -> Vec<u8> {
|
||||
let mut write = centered(head);
|
||||
write.extend([0x1B, b'd', 1]);
|
||||
write.extend(LARGE);
|
||||
write.extend(format!("{KEEPSAKE_DE}\n").as_bytes());
|
||||
write.extend(SMALL);
|
||||
write.extend(format!("{KEEPSAKE_EN}\n").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
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -410,7 +452,7 @@ mod tests {
|
||||
data: vec![0; BAND_ROWS + 1],
|
||||
};
|
||||
let job = encode(&bitmap, &Head::from_values(Some("85"), Some("quality")), 1);
|
||||
for band in &job[..2] {
|
||||
for band in &job[1..3] {
|
||||
assert_eq!(
|
||||
&band[..12],
|
||||
[0x1B, b'@', 0x12, b'~', 85, 0x1D, b's', 1, 0x1D, b'v', b'0', 0]
|
||||
@@ -450,27 +492,46 @@ mod tests {
|
||||
data: vec![0xAA; 2 * (BAND_ROWS + 1)],
|
||||
};
|
||||
let job = encode(&bitmap, &Head::default(), 2);
|
||||
// Per copy: a full band, the leftover row, then the feed and cut.
|
||||
assert_eq!(job.len(), 2 * 3);
|
||||
let cut = vec![0x1B, b'd', FEED_LINES, 0x1D, b'V', 66, 3];
|
||||
for copy in job.chunks(3) {
|
||||
// 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) {
|
||||
assert_eq!(
|
||||
©[0][..10],
|
||||
©[1][..10],
|
||||
[0x1B, b'@', 0x1D, b'v', b'0', 0, 2, 0, BAND_ROWS as u8, 0]
|
||||
);
|
||||
assert_eq!(copy[0].len(), 10 + 2 * BAND_ROWS);
|
||||
assert_eq!(copy[1].len(), 10 + 2 * BAND_ROWS);
|
||||
assert_eq!(
|
||||
copy[1],
|
||||
copy[2],
|
||||
[vec![0x1B, b'@', 0x1D, b'v', b'0', 0, 2, 0, 1, 0], vec![0xAA; 2]].concat()
|
||||
);
|
||||
assert_eq!(copy[2], cut);
|
||||
}
|
||||
// No write carries dot data without initializing first.
|
||||
assert!(
|
||||
job.iter()
|
||||
.all(|write| write.starts_with(&[0x1B, b'@']) || *write == cut)
|
||||
);
|
||||
// Every write initializes first, so no dot data is read as commands.
|
||||
assert!(job.iter().all(|write| write.starts_with(&[0x1B, b'@'])));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn captions_the_photo_and_cuts_once_per_copy() {
|
||||
let bitmap = Bitmap {
|
||||
width_bytes: 1,
|
||||
data: vec![0; 1],
|
||||
};
|
||||
let job = encode(&bitmap, &Head::default(), 2);
|
||||
let text: Vec<String> = 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));
|
||||
// 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()).iter().all(u8::is_ascii));
|
||||
let cuts = job
|
||||
.iter()
|
||||
.filter(|write| write.ends_with(&[0x1D, b'V', 66, 3]))
|
||||
.count();
|
||||
assert_eq!(cuts, 2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user