Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions brother_ql/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,50 @@ pub struct PrinterStatus {
phase_state: PhaseState,
}

impl PrinterStatus {
/// Get the pixel width (print area width in dots) for the loaded media
pub fn pixel_width(&self) -> Option<u16> {
match (self.media_width, self.media_length) {
// Endless tapes (length = 0) - dots_total - offset_r

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is not only the total width, but there is also an offset, would it make sense to include the offset in the return value?

(12, 0) => Some(142 - 29), // 113
(18, 0) => Some(256 - 171), // 85
(29, 0) => Some(342 - 6), // 336
(38, 0) => Some(449 - 12), // 437
(50, 0) => Some(590 - 12), // 578
(54, 0) => Some(636 - 0), // 636
(62, 0) => Some(732 - 12), // 720
(102, 0) => Some(1200 - 12), // 1188
(104, 0) => Some(1224 - 12), // 1212

// Die-cut labels
(17, 54) => Some(201 - 0), // 201
(17, 87) => Some(201 - 0), // 201
(23, 23) => Some(272 - 42), // 230
(29, 42) => Some(342 - 6), // 336
(29, 90) => Some(342 - 6), // 336
(38, 90) => Some(449 - 12), // 437
(39, 48) => Some(461 - 6), // 455
(52, 29) => Some(614 - 0), // 614
(54, 29) => Some(630 - 60), // 570
(60, 87) => Some(708 - 18), // 690
(62, 29) => Some(732 - 12), // 720
(62, 100) => Some(732 - 12), // 720
(102, 51) => Some(1200 - 12), // 1188
(102, 153) => Some(1200 - 12), // 1188
(104, 164) => Some(1224 - 12), // 1212

// Round die-cut labels
// This can't be right
//(12, 12) => Some(142 - 113), // 29
(24, 24) => Some(284 - 42), // 242
(58, 58) => Some(688 - 51), // 637

// Unknown media
_ => None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to capture width and height here and at least log an error or a warning, that we were unable to determine the width.

}
}
}

#[derive(Clone)]
pub enum PrinterCommandMode {
/// ESC/P mode (normal)
Expand Down Expand Up @@ -255,6 +299,7 @@ impl PrinterCommander {
}

pub fn read_status(&mut self) -> Result<PrinterStatus, std::io::Error> {
self.send_command(PrinterCommand::StatusInfoRequest)?;
let res = self.printer.read(32)?;
assert!(res[0] == 0x80);
assert!(res[1] == 0x20);
Expand Down
19 changes: 14 additions & 5 deletions brother_ql/src/image.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::env;

Comment on lines +1 to +2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the unused import

use crate::driver::{PrinterCommand, PrinterCommandMode, PrinterExpandedMode, PrinterMode};
use crate::error::BrotherQlError;
use crate::{driver, Settings};
Expand Down Expand Up @@ -58,7 +60,10 @@ fn apply_threshold(
Ok(img)
}

fn img_to_lines(img: ImageBuffer<Rgba<u8>, Vec<u8>>) -> Result<Vec<[u8; 90]>, BrotherQlError> {
fn img_to_lines(
img: ImageBuffer<Rgba<u8>, Vec<u8>>,
image_width: u32,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not need the image width here, as img.width() is the same size

) -> Result<Vec<[u8; 90]>, BrotherQlError> {
// convert to vec of line bits
/*
let mut lines = Vec::new();
Expand All @@ -83,13 +88,14 @@ fn img_to_lines(img: ImageBuffer<Rgba<u8>, Vec<u8>>) -> Result<Vec<[u8; 90]>, Br
*/

let mut lines = Vec::new();
let padding = 720 - image_width;

for y in 0..img.height() {
let mut line = [0u8; 90];

for x in 0..img.width() {
let i = img.get_pixel(x, y).0[0];

let x = x + padding;
Comment on lines 96 to +98

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified by using for x in padding..720 {

let byte = x / 8;
let bit = x % 8;

Expand Down Expand Up @@ -133,8 +139,10 @@ pub fn render_image(file_path: &str, settings: &Settings) -> Result<Vec<[u8; 90]

// resize

let new_width = 720; //630 per la carta piccola

// let new_width = 720; //630 per la carta piccola

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line instead of commenting out

let mut printer = driver::PrinterCommander::main("/dev/usb/lp0")?;
let status = printer.read_status()?;
let new_width = status.pixel_width().unwrap_or(720) as u32;
let new_height = new_width * img.height() / img.width() * if settings.dpi_600 { 2 } else { 1 };

let mut img = image::imageops::resize(
Expand All @@ -152,7 +160,8 @@ pub fn render_image(file_path: &str, settings: &Settings) -> Result<Vec<[u8; 90]

dithered_img.save("/tmp/out_processed.png")?;

let lines = img_to_lines(dithered_img)?;
// if the paper format is not known, assume the biggest one

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this comment not be part of line 145?

let lines = img_to_lines(dithered_img, new_width)?;
Ok(lines)
}

Expand Down
2 changes: 1 addition & 1 deletion example.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
BOT_TOKEN=999999999:xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxx
OWNER_ID=222222222
RUST_LOG=printer_bot_rs=DEBUG
RUST_LOG=printer_bot_rs=DEBUG
1 change: 1 addition & 0 deletions printer_bot_rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ log = "0.4.20"
teloxide-core = "0.13"
thiserror = "2.0.16"
tokio = { version = "1.34.0", features = ["full"] }
openssl = { version = "0.10", features = ["vendored"] }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a new dependency for openssl, when it is never used by us directly?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's used by the telegram bot

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we probably want to put it with the vendored feature behind a feature, as not everyone would want the statically linked openssl