From 7d42fe7211db1edd7992b30d4dd53b3582b9f36d Mon Sep 17 00:00:00 2001 From: Jurn Wubben Date: Fri, 26 Sep 2025 23:22:54 +0200 Subject: [PATCH] Implemented text --- src/escpos/errors.rs | 1 + src/escpos/job.rs | 44 +++++++++++++++++++++++++++++++++++++------- src/main.rs | 41 +++++++++++++++++++++++++++-------------- 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/src/escpos/errors.rs b/src/escpos/errors.rs index d40865f..d7761f1 100644 --- a/src/escpos/errors.rs +++ b/src/escpos/errors.rs @@ -4,4 +4,5 @@ pub enum EscPosError { InvalidQueueIndex, InvalidBitmapMode, InvalidBarcodeLength(String), + InvalidTextSize, } diff --git a/src/escpos/job.rs b/src/escpos/job.rs index ebd34ba..4c9fcd3 100644 --- a/src/escpos/job.rs +++ b/src/escpos/job.rs @@ -32,15 +32,15 @@ pub enum ImageOrientation { pub enum JustifyOrientation { Left = 0, Center = 1, - Rigth = 2, + Right = 2, } -#[repr(u8)] pub enum TextEffect { - DoubleHeight = 16, - Bold = 8, - DoubleWidth = 32, - Justify(JustifyOrientation) = b'a', + Bold, + DoubleHeight, + DoubleWidth, + InvertColor, + Justify(JustifyOrientation), } pub struct Job { @@ -53,6 +53,13 @@ fn is_numeric(s: &[u8]) -> bool { s.iter().all(|&b| b.is_ascii_digit()) } +impl Copy for JustifyOrientation {} +impl Clone for JustifyOrientation { + fn clone(&self) -> Self { + *self + } +} + impl Job { pub fn new(max_width: u16) -> Self { Job { @@ -66,7 +73,30 @@ impl Job { let amount = amount.unwrap_or(1); self.content.extend_from_slice(&[0x1B, b'd', amount]) } - pub fn write_text(&mut self, text: String) {} + pub fn write_text( + &mut self, + text: &String, + text_effect: &[TextEffect], + ) -> Result<(), EscPosError> { + let buf = &mut self.content; + buf.extend_from_slice(&[0x1B, b'@']); + + for i in text_effect { + match i { + TextEffect::Bold => buf.extend_from_slice(&[0x1B, b'E', 1]), + TextEffect::DoubleWidth => buf.extend_from_slice(&[0x1B, b'E', 1]), + TextEffect::DoubleHeight => buf.extend_from_slice(&[0x1B, b'E', 1]), + TextEffect::InvertColor => buf.extend_from_slice(&[0x1D, 0x42, 1]), + TextEffect::Justify(orientation) => { + buf.extend_from_slice(&[0x1B, b'a', orientation.clone() as u8]) + } + } + } + + buf.extend_from_slice(text.as_bytes()); + Ok(()) + } + pub fn write_bitmap( &mut self, img: &DynamicImage, diff --git a/src/main.rs b/src/main.rs index 91bef30..8c885ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,23 +5,23 @@ use image::{ImageError, ImageReader}; use std::{env, process}; use crate::escpos::{ - job::{BARTextPosition, BARType}, + job::{BARTextPosition, BARType, TextEffect}, printer::Printer, }; fn main() { - let args: Vec = env::args().collect(); - - let len = args.len(); - if len < 2 || len > 2 { - println!("Please provide a path to the image."); - process::exit(1); - } - - let img = ImageReader::open(&args[1]) - .map_err(|err| ImageError::IoError(err)) - .and_then(|v| v.decode()) - .unwrap(); + // let args: Vec = env::args().collect(); + // + // let len = args.len(); + // if len < 2 || len > 2 { + // println!("Please provide a path to the image."); + // process::exit(1); + // } + // + // let img = ImageReader::open(&args[1]) + // .map_err(|err| ImageError::IoError(err)) + // .and_then(|v| v.decode()) + // .unwrap(); let mut printer = Printer::new(384); let job = printer.new_job().unwrap(); @@ -34,7 +34,20 @@ fn main() { // Some(BARType::CODE128), // ) // .unwrap(); - job.write_bitmap(&img, None, None).unwrap(); + // job.write_bitmap(&img, None, None).unwrap(); + job.write_text( + &"Kanker homo! Ik hoop dat je dood gaat \n Sorry grapje, hier is een mooie QR code die je mag scannen.".to_string(), + &[ + TextEffect::Justify(escpos::job::JustifyOrientation::Center), + TextEffect::Size(5, 5) + // TextEffect::Bold, + // TextEffect::InvertColor, + + ] + ).unwrap(); + job.write_qr("https://pornhub.com".to_string(), None, None) + .unwrap(); + job.write_feed(Some(2)); job.ready = true;