Remove all dependencies on showbits-common
This commit is contained in:
parent
de7ae63a5e
commit
6e6dfb2b66
8 changed files with 26 additions and 28 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -2740,11 +2740,11 @@ dependencies = [
|
|||
"image",
|
||||
"jiff",
|
||||
"mime_guess",
|
||||
"palette",
|
||||
"rand 0.9.0",
|
||||
"rust-embed",
|
||||
"serde",
|
||||
"showbits-assets",
|
||||
"showbits-common",
|
||||
"showbits-typst",
|
||||
"tokio",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -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"] }
|
||||
|
||||
|
|
|
|||
13
showbits-thermal-printer/src/color.rs
Normal file
13
showbits-thermal-printer/src/color.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#![allow(unused)]
|
||||
|
||||
use palette::Srgba;
|
||||
|
||||
pub fn image_to_palette(color: image::Rgba<u8>) -> 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<u8> {
|
||||
let color = color.into_format::<u8, u8>();
|
||||
image::Rgba([color.red, color.green, color.blue, color.alpha])
|
||||
}
|
||||
|
|
@ -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<dyn Drawing + Send>);
|
||||
|
|
@ -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<Command>,
|
||||
printer: PersistentPrinter,
|
||||
ctx: Context,
|
||||
}
|
||||
|
||||
impl Drawer {
|
||||
pub fn new(rx: mpsc::Receiver<Command>, 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(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(())
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
mod color;
|
||||
mod documents;
|
||||
mod drawer;
|
||||
mod persistent_printer;
|
||||
|
|
|
|||
|
|
@ -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<EPrinter<FileDriver>>,
|
||||
|
|
@ -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<u8>) -> 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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue