From 6ab43a8d5e3ff797cc01b4168d5f1f5993af0dd3 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 8 Mar 2024 17:01:54 +0100 Subject: [PATCH] Try to draw simple "Hello world!" --- Cargo.lock | 1 + Cargo.toml | 1 + showbits-common/Cargo.toml | 2 +- showbits-common/src/widgets/text.rs | 2 +- showbits-thermal-printer/Cargo.toml | 1 + showbits-thermal-printer/src/drawer.rs | 48 ++++++++++++++++++++++---- 6 files changed, 47 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4159cfe..67b1497 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1173,6 +1173,7 @@ dependencies = [ "anyhow", "axum", "clap", + "cosmic-text", "escpos", "image", "palette", diff --git a/Cargo.toml b/Cargo.toml index 5bee944..cea2b4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [workspace.dependencies] anyhow = "1.0.80" +cosmic-text = "0.11.2" image = "0.24.9" palette = "0.7.5" showbits-common.path = "./showbits-common" diff --git a/showbits-common/Cargo.toml b/showbits-common/Cargo.toml index f5d52f2..6dc324f 100644 --- a/showbits-common/Cargo.toml +++ b/showbits-common/Cargo.toml @@ -5,7 +5,7 @@ edition.workspace = true [dependencies] anyhow.workspace = true -cosmic-text = "0.11.2" +cosmic-text.workspace = true image.workspace = true palette.workspace = true taffy.workspace = true diff --git a/showbits-common/src/widgets/text.rs b/showbits-common/src/widgets/text.rs index ed03269..9f0bf08 100644 --- a/showbits-common/src/widgets/text.rs +++ b/showbits-common/src/widgets/text.rs @@ -143,6 +143,6 @@ impl Widget for Text { view.rect(area, color); }); - todo!() + Ok(()) } } diff --git a/showbits-thermal-printer/Cargo.toml b/showbits-thermal-printer/Cargo.toml index ae09720..64d7a80 100644 --- a/showbits-thermal-printer/Cargo.toml +++ b/showbits-thermal-printer/Cargo.toml @@ -7,6 +7,7 @@ edition.workspace = true anyhow.workspace = true axum = "0.7.4" clap = { version = "4.5.1", features = ["derive", "deprecated"] } +cosmic-text.workspace = true escpos = { version = "0.7.2", features = ["full"] } image.workspace = true palette.workspace = true diff --git a/showbits-thermal-printer/src/drawer.rs b/showbits-thermal-printer/src/drawer.rs index 4dc5a82..733a59a 100644 --- a/showbits-thermal-printer/src/drawer.rs +++ b/showbits-thermal-printer/src/drawer.rs @@ -1,3 +1,10 @@ +use cosmic_text::{Attrs, Metrics}; +use palette::Srgb; +use showbits_common::{ + widgets::{FontStuff, HasFontStuff, Text}, + Tree, WidgetExt, +}; +use taffy::style_helpers::length; use tokio::sync::mpsc; use crate::printer::Printer; @@ -10,14 +17,30 @@ pub enum Command { ChatMessage { username: String, content: String }, } +#[derive(Default)] +struct Context { + font_stuff: FontStuff, +} + +impl HasFontStuff for Context { + fn font_stuff(&mut self) -> &mut FontStuff { + &mut self.font_stuff + } +} + pub struct Drawer { rx: mpsc::Receiver, printer: Printer, + ctx: Context, } impl Drawer { pub fn new(rx: mpsc::Receiver, printer: Printer) -> Self { - Self { rx, printer } + Self { + rx, + printer, + ctx: Context::default(), + } } pub fn run(&mut self) -> anyhow::Result<()> { @@ -35,17 +58,30 @@ impl Drawer { match command { Command::Stop => {} // Already handled one level above Command::Rip => self.printer.rip()?, - Command::Test => todo!(), + Command::Test => self.on_test()?, Command::Text(_) => todo!(), Command::ChatMessage { username, content } => todo!(), } Ok(()) } - // fn on_rip(&mut self) -> anyhow::Result<()> { - // self.printer.init()?.feeds(6)?.print()?; - // Ok(()) - // } + fn on_test(&mut self) -> anyhow::Result<()> { + let mut tree = Tree::::new(); + + let root = Text::simple( + &mut self.ctx.font_stuff, + Metrics::new(16.0, 32.0), + Attrs::new(), + "Hello world!", + ) + .color(Srgb::new(1.0, 1.0, 1.0)) + .node() + .margin_all(length(10.0)) + .register(&mut tree)?; + + self.printer.print_tree(&mut tree, &mut self.ctx, root)?; + Ok(()) + } // fn on_text(&mut self, text: String) -> anyhow::Result<()> { // let text = util::sanitize(&text);