diff --git a/showbits-thermal-printer/src/drawer.rs b/showbits-thermal-printer/src/drawer.rs index 3080d94..2bf18f8 100644 --- a/showbits-thermal-printer/src/drawer.rs +++ b/showbits-thermal-printer/src/drawer.rs @@ -18,6 +18,7 @@ pub enum Command { Test, Text(String), Image(RgbaImage), + Photo { image: RgbaImage, title: String }, ChatMessage { username: String, content: String }, } @@ -67,6 +68,7 @@ impl Drawer { Command::Test => self.on_test()?, Command::Text(text) => self.on_text(text)?, Command::Image(image) => self.on_image(image)?, + Command::Photo { image, title } => self.on_photo(image, title)?, Command::ChatMessage { username, content } => { self.on_chat_message(username, content)? } @@ -146,6 +148,36 @@ impl Drawer { Ok(()) } + fn on_photo(&mut self, image: RgbaImage, title: String) -> anyhow::Result<()> { + let mut tree = Tree::::new(WHITE); + + let image = Image::new(image) + .with_dither_palette(&[BLACK, WHITE]) + .node() + .register(&mut tree)?; + + let title = Text::new() + .with_metrics(Text::default_metrics().scale(2.0)) + .and_plain(title) + .widget(&mut self.ctx.font_stuff) + .node() + .register(&mut tree)?; + + let root = Node::empty() + .with_size_width(percent(1.0)) + .with_padding_bottom(length(Self::FEED)) + .with_display(Display::Flex) + .with_flex_direction(FlexDirection::Column) + .with_align_items(Some(AlignItems::Center)) + .with_gap(length(16.0)) + .and_child(image) + .and_child(title) + .register(&mut tree)?; + + self.printer.print_tree(&mut tree, &mut self.ctx, root)?; + Ok(()) + } + fn on_chat_message(&mut self, username: String, content: String) -> anyhow::Result<()> { let mut tree = Tree::::new(WHITE); diff --git a/showbits-thermal-printer/src/server.rs b/showbits-thermal-printer/src/server.rs index 4bb2ce6..43444ad 100644 --- a/showbits-thermal-printer/src/server.rs +++ b/showbits-thermal-printer/src/server.rs @@ -28,6 +28,7 @@ pub async fn run(tx: mpsc::Sender, addr: String) -> anyhow::Result<()> .route("/rip", post(post_rip)) .route("/text", post(post_text)) .route("/image", post(post_image).fallback(get_static_file)) + .route("/photo", post(post_photo).fallback(get_static_file)) .route("/chat_message", post(post_chat_message)) .fallback(get(get_static_file)) .layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB @@ -63,21 +64,49 @@ async fn post_image(server: State, mut multipart: Multipart) -> somehow: let mut image = None; while let Some(field) = multipart.next_field().await? { - if let Some(name) = field.name() { - if name == "image" { + if let Some("image") = field.name() { + let data = field.bytes().await?; + let decoded = image::load_from_memory(&data)?.into_rgba8(); + image = Some(decoded); + } + } + + let Some(image) = image else { + return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY)); + }; + + let _ = server.tx.send(Command::Image(image)).await; + Ok(Redirect::to("image").into_response()) +} + +async fn post_photo(server: State, mut multipart: Multipart) -> somehow::Result { + let mut image = None; + let mut title = None; + + while let Some(field) = multipart.next_field().await? { + match field.name() { + Some("image") => { let data = field.bytes().await?; let decoded = image::load_from_memory(&data)?.into_rgba8(); image = Some(decoded); } + Some("title") => { + title = Some(field.text().await?); + } + _ => {} } } - if let Some(image) = image { - let _ = server.tx.send(Command::Image(image)).await; - return Ok(Redirect::to("image").into_response()); - } + let Some(image) = image else { + return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY)); + }; - Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY)) + let Some(title) = title else { + return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY)); + }; + + let _ = server.tx.send(Command::Photo { image, title }).await; + Ok(Redirect::to("photo").into_response()) } #[derive(Deserialize)] diff --git a/showbits-thermal-printer/static/index.html b/showbits-thermal-printer/static/index.html index 05d51f7..1e09797 100644 --- a/showbits-thermal-printer/static/index.html +++ b/showbits-thermal-printer/static/index.html @@ -8,7 +8,8 @@

Thermal Printer Control

diff --git a/showbits-thermal-printer/static/photo.html b/showbits-thermal-printer/static/photo.html new file mode 100644 index 0000000..f79b19c --- /dev/null +++ b/showbits-thermal-printer/static/photo.html @@ -0,0 +1,20 @@ + + + + + + TP: Photo + + +
+
    +
  1. +
  2. +
  3. +
+
+ +