diff --git a/Cargo.lock b/Cargo.lock index d4d33d1..a9dbefd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2740,11 +2740,11 @@ dependencies = [ "image", "jiff", "mime_guess", + "palette", "rand 0.9.0", "rust-embed", "serde", "showbits-assets", - "showbits-common", "showbits-typst", "tokio", ] diff --git a/showbits-thermal-printer/Cargo.toml b/showbits-thermal-printer/Cargo.toml index 689c188..b6d1a38 100644 --- a/showbits-thermal-printer/Cargo.toml +++ b/showbits-thermal-printer/Cargo.toml @@ -11,11 +11,11 @@ escpos = { workspace = true } image = { workspace = true, default-features = true } jiff = { workspace = true } mime_guess = { workspace = true } +palette = { workspace = true } rand = { workspace = true } rust-embed = { workspace = true } serde = { workspace = true } showbits-assets = { workspace = true } -showbits-common = { workspace = true } showbits-typst = { workspace = true } tokio = { workspace = true, features = ["full"] } diff --git a/showbits-thermal-printer/src/color.rs b/showbits-thermal-printer/src/color.rs new file mode 100644 index 0000000..2b20cfa --- /dev/null +++ b/showbits-thermal-printer/src/color.rs @@ -0,0 +1,13 @@ +#![allow(unused)] + +use palette::Srgba; + +pub fn image_to_palette(color: image::Rgba) -> Srgba { + let [r, g, b, a] = color.0; + Srgba::new(r, g, b, a).into_format() +} + +pub fn palette_to_image(color: Srgba) -> image::Rgba { + let color = color.into_format::(); + image::Rgba([color.red, color.green, color.blue, color.alpha]) +} diff --git a/showbits-thermal-printer/src/drawer.rs b/showbits-thermal-printer/src/drawer.rs index c6203cd..5e914bf 100644 --- a/showbits-thermal-printer/src/drawer.rs +++ b/showbits-thermal-printer/src/drawer.rs @@ -1,20 +1,14 @@ mod backlog; mod new_typst; -use showbits_common::widgets::{FontStuff, HasFontStuff}; use tokio::sync::mpsc; use crate::persistent_printer::PersistentPrinter; pub use self::{backlog::BacklogDrawing, new_typst::NewTypstDrawing}; -#[derive(Default)] -pub struct Context { - font_stuff: FontStuff, -} - pub trait Drawing { - fn draw(&self, printer: &mut PersistentPrinter, ctx: &mut Context) -> anyhow::Result<()>; + fn draw(&self, printer: &mut PersistentPrinter) -> anyhow::Result<()>; } pub struct Command(Box); @@ -25,30 +19,19 @@ impl Command { } } -impl HasFontStuff for Context { - fn font_stuff(&mut self) -> &mut FontStuff { - &mut self.font_stuff - } -} - pub struct Drawer { rx: mpsc::Receiver, printer: PersistentPrinter, - ctx: Context, } impl Drawer { pub fn new(rx: mpsc::Receiver, printer: PersistentPrinter) -> Self { - Self { - rx, - printer, - ctx: Context::default(), - } + Self { rx, printer } } pub fn run(&mut self) -> anyhow::Result<()> { while let Some(command) = self.rx.blocking_recv() { - command.0.draw(&mut self.printer, &mut self.ctx)?; + command.0.draw(&mut self.printer)?; } Ok(()) } diff --git a/showbits-thermal-printer/src/drawer/backlog.rs b/showbits-thermal-printer/src/drawer/backlog.rs index 9afd542..b982f7d 100644 --- a/showbits-thermal-printer/src/drawer/backlog.rs +++ b/showbits-thermal-printer/src/drawer/backlog.rs @@ -1,11 +1,11 @@ use crate::persistent_printer::PersistentPrinter; -use super::{Context, Drawing}; +use super::Drawing; pub struct BacklogDrawing; impl Drawing for BacklogDrawing { - fn draw(&self, printer: &mut PersistentPrinter, _ctx: &mut Context) -> anyhow::Result<()> { + fn draw(&self, printer: &mut PersistentPrinter) -> anyhow::Result<()> { printer.print_backlog()?; Ok(()) } diff --git a/showbits-thermal-printer/src/drawer/new_typst.rs b/showbits-thermal-printer/src/drawer/new_typst.rs index aa777e3..6710adf 100644 --- a/showbits-thermal-printer/src/drawer/new_typst.rs +++ b/showbits-thermal-printer/src/drawer/new_typst.rs @@ -2,7 +2,7 @@ use showbits_typst::Typst; use crate::persistent_printer::PersistentPrinter; -use super::{Context, Drawing}; +use super::Drawing; pub struct NewTypstDrawing(pub Typst); @@ -13,7 +13,7 @@ impl NewTypstDrawing { } impl Drawing for NewTypstDrawing { - fn draw(&self, printer: &mut PersistentPrinter, _ctx: &mut Context) -> anyhow::Result<()> { + fn draw(&self, printer: &mut PersistentPrinter) -> anyhow::Result<()> { let image = self.0.render()?; printer.print_image(&image)?; Ok(()) diff --git a/showbits-thermal-printer/src/main.rs b/showbits-thermal-printer/src/main.rs index 0770812..abaafa5 100644 --- a/showbits-thermal-printer/src/main.rs +++ b/showbits-thermal-printer/src/main.rs @@ -1,3 +1,4 @@ +mod color; mod documents; mod drawer; mod persistent_printer; diff --git a/showbits-thermal-printer/src/printer.rs b/showbits-thermal-printer/src/printer.rs index b999426..701c99a 100644 --- a/showbits-thermal-printer/src/printer.rs +++ b/showbits-thermal-printer/src/printer.rs @@ -8,7 +8,8 @@ use escpos::{ utils::{GS, PageCode, Protocol}, }; use image::{Rgba, RgbaImage}; -use showbits_common::color; + +use crate::color; pub struct Printer { printer: Option>, @@ -143,7 +144,7 @@ impl Printer { /// Instead of doing the physically accurate thing, I do what makes the most /// sense visually. fn pixel_to_bit(pixel: Rgba) -> bool { - let color = color::from_image_color(pixel); + let color = color::image_to_palette(pixel); let avg = (color.red + color.green + color.blue) / 3.0; avg < 0.5 // true == black }