From 0bc2942b2b17216598730c8f8ee30b3234c6b1ce Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 30 Mar 2024 12:32:52 +0100 Subject: [PATCH] Turn Text into drawing --- showbits-thermal-printer/src/drawer.rs | 24 ++--------------- showbits-thermal-printer/src/drawer/text.rs | 29 +++++++++++++++++++++ showbits-thermal-printer/src/server.rs | 7 +++-- 3 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 showbits-thermal-printer/src/drawer/text.rs diff --git a/showbits-thermal-printer/src/drawer.rs b/showbits-thermal-printer/src/drawer.rs index 55b6118..2d85b55 100644 --- a/showbits-thermal-printer/src/drawer.rs +++ b/showbits-thermal-printer/src/drawer.rs @@ -1,5 +1,6 @@ mod calendar; mod cells; +mod text; use image::{Luma, Pixel, RgbaImage}; use palette::{FromColor, IntoColor, LinLumaa}; @@ -16,7 +17,7 @@ use tokio::sync::mpsc; use crate::printer::Printer; -pub use self::{calendar::CalendarDrawing, cells::CellsDrawing}; +pub use self::{calendar::CalendarDrawing, cells::CellsDrawing, text::TextDrawing}; #[derive(Default)] pub struct Context { @@ -32,7 +33,6 @@ pub struct BoxedDrawing(Box); pub enum Command { Draw(BoxedDrawing), - Text(String), Image { image: RgbaImage, bright: bool }, Photo { image: RgbaImage, title: String }, ChatMessage { username: String, content: String }, @@ -78,7 +78,6 @@ impl Drawer { match command { Command::Draw(drawing) => drawing.0.draw(&mut self.printer, &mut self.ctx)?, - Command::Text(text) => self.on_text(text)?, Command::Image { image, bright } => self.on_image(image, bright)?, Command::Photo { image, title } => self.on_photo(image, title)?, Command::ChatMessage { username, content } => { @@ -88,25 +87,6 @@ impl Drawer { Ok(()) } - fn on_text(&mut self, text: String) -> anyhow::Result<()> { - let mut tree = Tree::::new(WHITE); - - let text = Text::new() - .with_metrics(Text::default_metrics().scale(2.0)) - .and_plain(text) - .widget(&mut self.ctx.font_stuff) - .node() - .register(&mut tree)?; - - let root = Node::empty() - .with_size_width(percent(1.0)) - .and_child(text) - .register(&mut tree)?; - - self.printer.print_tree(&mut tree, &mut self.ctx, root)?; - Ok(()) - } - fn on_image(&mut self, mut image: RgbaImage, bright: bool) -> anyhow::Result<()> { let mut tree = Tree::::new(WHITE); diff --git a/showbits-thermal-printer/src/drawer/text.rs b/showbits-thermal-printer/src/drawer/text.rs new file mode 100644 index 0000000..21546bd --- /dev/null +++ b/showbits-thermal-printer/src/drawer/text.rs @@ -0,0 +1,29 @@ +use showbits_common::{color::WHITE, widgets::Text, Node, Tree, WidgetExt}; +use taffy::style_helpers::percent; + +use crate::printer::Printer; + +use super::{Context, Drawing}; + +pub struct TextDrawing(pub String); + +impl Drawing for TextDrawing { + fn draw(&self, printer: &mut Printer, ctx: &mut Context) -> anyhow::Result<()> { + let mut tree = Tree::::new(WHITE); + + let text = Text::new() + .with_metrics(Text::default_metrics().scale(2.0)) + .and_plain(&self.0) + .widget(&mut ctx.font_stuff) + .node() + .register(&mut tree)?; + + let root = Node::empty() + .with_size_width(percent(1.0)) + .and_child(text) + .register(&mut tree)?; + + printer.print_tree(&mut tree, ctx, root)?; + Ok(()) + } +} diff --git a/showbits-thermal-printer/src/server.rs b/showbits-thermal-printer/src/server.rs index 4449a27..e727419 100644 --- a/showbits-thermal-printer/src/server.rs +++ b/showbits-thermal-printer/src/server.rs @@ -12,7 +12,7 @@ use axum::{ use serde::Deserialize; use tokio::{net::TcpListener, sync::mpsc}; -use crate::drawer::{CalendarDrawing, CellsDrawing, Command}; +use crate::drawer::{CalendarDrawing, CellsDrawing, Command, TextDrawing}; use self::{r#static::get_static_file, statuscode::status_code}; @@ -44,7 +44,10 @@ struct PostTextForm { } async fn post_text(server: State, request: Form) { - let _ = server.tx.send(Command::Text(request.0.text)).await; + let _ = server + .tx + .send(Command::draw(TextDrawing(request.0.text))) + .await; } async fn post_image(server: State, mut multipart: Multipart) -> somehow::Result {