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",
|
"image",
|
||||||
"jiff",
|
"jiff",
|
||||||
"mime_guess",
|
"mime_guess",
|
||||||
|
"palette",
|
||||||
"rand 0.9.0",
|
"rand 0.9.0",
|
||||||
"rust-embed",
|
"rust-embed",
|
||||||
"serde",
|
"serde",
|
||||||
"showbits-assets",
|
"showbits-assets",
|
||||||
"showbits-common",
|
|
||||||
"showbits-typst",
|
"showbits-typst",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -11,11 +11,11 @@ escpos = { workspace = true }
|
||||||
image = { workspace = true, default-features = true }
|
image = { workspace = true, default-features = true }
|
||||||
jiff = { workspace = true }
|
jiff = { workspace = true }
|
||||||
mime_guess = { workspace = true }
|
mime_guess = { workspace = true }
|
||||||
|
palette = { workspace = true }
|
||||||
rand = { workspace = true }
|
rand = { workspace = true }
|
||||||
rust-embed = { workspace = true }
|
rust-embed = { workspace = true }
|
||||||
serde = { workspace = true }
|
serde = { workspace = true }
|
||||||
showbits-assets = { workspace = true }
|
showbits-assets = { workspace = true }
|
||||||
showbits-common = { workspace = true }
|
|
||||||
showbits-typst = { workspace = true }
|
showbits-typst = { workspace = true }
|
||||||
tokio = { workspace = true, features = ["full"] }
|
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 backlog;
|
||||||
mod new_typst;
|
mod new_typst;
|
||||||
|
|
||||||
use showbits_common::widgets::{FontStuff, HasFontStuff};
|
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
use crate::persistent_printer::PersistentPrinter;
|
use crate::persistent_printer::PersistentPrinter;
|
||||||
|
|
||||||
pub use self::{backlog::BacklogDrawing, new_typst::NewTypstDrawing};
|
pub use self::{backlog::BacklogDrawing, new_typst::NewTypstDrawing};
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct Context {
|
|
||||||
font_stuff: FontStuff,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Drawing {
|
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>);
|
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 {
|
pub struct Drawer {
|
||||||
rx: mpsc::Receiver<Command>,
|
rx: mpsc::Receiver<Command>,
|
||||||
printer: PersistentPrinter,
|
printer: PersistentPrinter,
|
||||||
ctx: Context,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drawer {
|
impl Drawer {
|
||||||
pub fn new(rx: mpsc::Receiver<Command>, printer: PersistentPrinter) -> Self {
|
pub fn new(rx: mpsc::Receiver<Command>, printer: PersistentPrinter) -> Self {
|
||||||
Self {
|
Self { rx, printer }
|
||||||
rx,
|
|
||||||
printer,
|
|
||||||
ctx: Context::default(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&mut self) -> anyhow::Result<()> {
|
pub fn run(&mut self) -> anyhow::Result<()> {
|
||||||
while let Some(command) = self.rx.blocking_recv() {
|
while let Some(command) = self.rx.blocking_recv() {
|
||||||
command.0.draw(&mut self.printer, &mut self.ctx)?;
|
command.0.draw(&mut self.printer)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
use crate::persistent_printer::PersistentPrinter;
|
use crate::persistent_printer::PersistentPrinter;
|
||||||
|
|
||||||
use super::{Context, Drawing};
|
use super::Drawing;
|
||||||
|
|
||||||
pub struct BacklogDrawing;
|
pub struct BacklogDrawing;
|
||||||
|
|
||||||
impl Drawing for 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()?;
|
printer.print_backlog()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use showbits_typst::Typst;
|
||||||
|
|
||||||
use crate::persistent_printer::PersistentPrinter;
|
use crate::persistent_printer::PersistentPrinter;
|
||||||
|
|
||||||
use super::{Context, Drawing};
|
use super::Drawing;
|
||||||
|
|
||||||
pub struct NewTypstDrawing(pub Typst);
|
pub struct NewTypstDrawing(pub Typst);
|
||||||
|
|
||||||
|
|
@ -13,7 +13,7 @@ impl NewTypstDrawing {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drawing for 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()?;
|
let image = self.0.render()?;
|
||||||
printer.print_image(&image)?;
|
printer.print_image(&image)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
mod color;
|
||||||
mod documents;
|
mod documents;
|
||||||
mod drawer;
|
mod drawer;
|
||||||
mod persistent_printer;
|
mod persistent_printer;
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,8 @@ use escpos::{
|
||||||
utils::{GS, PageCode, Protocol},
|
utils::{GS, PageCode, Protocol},
|
||||||
};
|
};
|
||||||
use image::{Rgba, RgbaImage};
|
use image::{Rgba, RgbaImage};
|
||||||
use showbits_common::color;
|
|
||||||
|
use crate::color;
|
||||||
|
|
||||||
pub struct Printer {
|
pub struct Printer {
|
||||||
printer: Option<EPrinter<FileDriver>>,
|
printer: Option<EPrinter<FileDriver>>,
|
||||||
|
|
@ -143,7 +144,7 @@ impl Printer {
|
||||||
/// Instead of doing the physically accurate thing, I do what makes the most
|
/// Instead of doing the physically accurate thing, I do what makes the most
|
||||||
/// sense visually.
|
/// sense visually.
|
||||||
fn pixel_to_bit(pixel: Rgba<u8>) -> bool {
|
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;
|
let avg = (color.red + color.green + color.blue) / 3.0;
|
||||||
avg < 0.5 // true == black
|
avg < 0.5 // true == black
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue