Implemented text
This commit is contained in:
parent
998e5926dc
commit
7d42fe7211
3 changed files with 65 additions and 21 deletions
|
|
@ -4,4 +4,5 @@ pub enum EscPosError {
|
|||
InvalidQueueIndex,
|
||||
InvalidBitmapMode,
|
||||
InvalidBarcodeLength(String),
|
||||
InvalidTextSize,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
41
src/main.rs
41
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<String> = 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<String> = 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;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue