diff --git a/showbits-thermal-printer/src/drawer.rs b/showbits-thermal-printer/src/drawer.rs index 9e2314e..357e70e 100644 --- a/showbits-thermal-printer/src/drawer.rs +++ b/showbits-thermal-printer/src/drawer.rs @@ -1,24 +1,25 @@ mod calendar; mod cells; mod image; +mod photo; mod text; -use ::image::{Luma, Pixel, RgbaImage}; use showbits_common::{ color::{BLACK, WHITE}, - widgets::{Block, FontStuff, HasFontStuff, Image, Text}, + widgets::{Block, FontStuff, HasFontStuff, Text}, Node, Tree, WidgetExt, }; use taffy::{ style_helpers::{length, percent}, - AlignItems, Display, FlexDirection, + AlignItems, FlexDirection, }; use tokio::sync::mpsc; use crate::printer::Printer; pub use self::{ - calendar::CalendarDrawing, cells::CellsDrawing, image::ImageDrawing, text::TextDrawing, + calendar::CalendarDrawing, cells::CellsDrawing, image::ImageDrawing, photo::PhotoDrawing, + text::TextDrawing, }; #[derive(Default)] @@ -35,7 +36,6 @@ pub struct BoxedDrawing(Box); pub enum Command { Draw(BoxedDrawing), - Photo { image: RgbaImage, title: String }, ChatMessage { username: String, content: String }, } @@ -58,8 +58,6 @@ pub struct Drawer { } impl Drawer { - const FEED: f32 = 64.0; - pub fn new(rx: mpsc::Receiver, printer: Printer) -> Self { Self { rx, @@ -79,7 +77,6 @@ impl Drawer { match command { Command::Draw(drawing) => drawing.0.draw(&mut self.printer, &mut self.ctx)?, - Command::Photo { image, title } => self.on_photo(image, title)?, Command::ChatMessage { username, content } => { self.on_chat_message(username, content)? } @@ -87,49 +84,6 @@ impl Drawer { Ok(()) } - fn on_photo(&mut self, mut image: RgbaImage, title: String) -> anyhow::Result<()> { - println!( - "Printing photo {title:?} ({}x{})", - image.width(), - image.height() - ); - let mut tree = Tree::::new(WHITE); - - for pixel in image.pixels_mut() { - let [l] = pixel.to_luma().0; - let l = l as f32 / 255.0; // Convert to [0, 1] - let l = 1.0 - (0.4 * (1.0 - l)); // Lerp to [0.6, 1] - let l = (l.clamp(0.0, 1.0) * 255.0) as u8; // Convert back to [0, 255] - *pixel = Luma([l]).to_rgba(); - } - - 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(8.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/drawer/photo.rs b/showbits-thermal-printer/src/drawer/photo.rs new file mode 100644 index 0000000..52b1a1d --- /dev/null +++ b/showbits-thermal-printer/src/drawer/photo.rs @@ -0,0 +1,60 @@ +use image::{Luma, Pixel, RgbaImage}; +use showbits_common::{ + color::{BLACK, WHITE}, + widgets::{Image, Text}, + Node, Tree, WidgetExt, +}; +use taffy::{ + style_helpers::{length, percent}, + AlignItems, Display, FlexDirection, +}; + +use crate::printer::Printer; + +use super::{Context, Drawing}; + +pub struct PhotoDrawing { + pub image: RgbaImage, + pub title: String, +} + +impl Drawing for PhotoDrawing { + fn draw(&self, printer: &mut Printer, ctx: &mut Context) -> anyhow::Result<()> { + let mut tree = Tree::::new(WHITE); + + let mut image = self.image.clone(); + for pixel in image.pixels_mut() { + let [l] = pixel.to_luma().0; + let l = l as f32 / 255.0; // Convert to [0, 1] + let l = 1.0 - (0.4 * (1.0 - l)); // Lerp to [0.6, 1] + let l = (l.clamp(0.0, 1.0) * 255.0) as u8; // Convert back to [0, 255] + *pixel = Luma([l]).to_rgba(); + } + + 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(&self.title) + .widget(&mut ctx.font_stuff) + .node() + .register(&mut tree)?; + + let root = Node::empty() + .with_size_width(percent(1.0)) + .with_display(Display::Flex) + .with_flex_direction(FlexDirection::Column) + .with_align_items(Some(AlignItems::Center)) + .with_gap(length(8.0)) + .and_child(image) + .and_child(title) + .register(&mut tree)?; + + printer.print_tree(&mut tree, ctx, root)?; + printer.feed()?; + Ok(()) + } +} diff --git a/showbits-thermal-printer/src/server.rs b/showbits-thermal-printer/src/server.rs index 0465d05..afb34ce 100644 --- a/showbits-thermal-printer/src/server.rs +++ b/showbits-thermal-printer/src/server.rs @@ -12,7 +12,9 @@ use axum::{ use serde::Deserialize; use tokio::{net::TcpListener, sync::mpsc}; -use crate::drawer::{CalendarDrawing, CellsDrawing, Command, ImageDrawing, TextDrawing}; +use crate::drawer::{ + CalendarDrawing, CellsDrawing, Command, ImageDrawing, PhotoDrawing, TextDrawing, +}; use self::{r#static::get_static_file, statuscode::status_code}; @@ -105,7 +107,10 @@ async fn post_photo(server: State, mut multipart: Multipart) -> somehow: return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY)); }; - let _ = server.tx.send(Command::Photo { image, title }).await; + let _ = server + .tx + .send(Command::draw(PhotoDrawing { image, title })) + .await; Ok(Redirect::to("photo").into_response()) }