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,
|
InvalidQueueIndex,
|
||||||
InvalidBitmapMode,
|
InvalidBitmapMode,
|
||||||
InvalidBarcodeLength(String),
|
InvalidBarcodeLength(String),
|
||||||
|
InvalidTextSize,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,15 +32,15 @@ pub enum ImageOrientation {
|
||||||
pub enum JustifyOrientation {
|
pub enum JustifyOrientation {
|
||||||
Left = 0,
|
Left = 0,
|
||||||
Center = 1,
|
Center = 1,
|
||||||
Rigth = 2,
|
Right = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(u8)]
|
|
||||||
pub enum TextEffect {
|
pub enum TextEffect {
|
||||||
DoubleHeight = 16,
|
Bold,
|
||||||
Bold = 8,
|
DoubleHeight,
|
||||||
DoubleWidth = 32,
|
DoubleWidth,
|
||||||
Justify(JustifyOrientation) = b'a',
|
InvertColor,
|
||||||
|
Justify(JustifyOrientation),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Job {
|
pub struct Job {
|
||||||
|
|
@ -53,6 +53,13 @@ fn is_numeric(s: &[u8]) -> bool {
|
||||||
s.iter().all(|&b| b.is_ascii_digit())
|
s.iter().all(|&b| b.is_ascii_digit())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Copy for JustifyOrientation {}
|
||||||
|
impl Clone for JustifyOrientation {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
*self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Job {
|
impl Job {
|
||||||
pub fn new(max_width: u16) -> Self {
|
pub fn new(max_width: u16) -> Self {
|
||||||
Job {
|
Job {
|
||||||
|
|
@ -66,7 +73,30 @@ impl Job {
|
||||||
let amount = amount.unwrap_or(1);
|
let amount = amount.unwrap_or(1);
|
||||||
self.content.extend_from_slice(&[0x1B, b'd', amount])
|
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(
|
pub fn write_bitmap(
|
||||||
&mut self,
|
&mut self,
|
||||||
img: &DynamicImage,
|
img: &DynamicImage,
|
||||||
|
|
|
||||||
41
src/main.rs
41
src/main.rs
|
|
@ -5,23 +5,23 @@ use image::{ImageError, ImageReader};
|
||||||
use std::{env, process};
|
use std::{env, process};
|
||||||
|
|
||||||
use crate::escpos::{
|
use crate::escpos::{
|
||||||
job::{BARTextPosition, BARType},
|
job::{BARTextPosition, BARType, TextEffect},
|
||||||
printer::Printer,
|
printer::Printer,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
// let args: Vec<String> = env::args().collect();
|
||||||
|
//
|
||||||
let len = args.len();
|
// let len = args.len();
|
||||||
if len < 2 || len > 2 {
|
// if len < 2 || len > 2 {
|
||||||
println!("Please provide a path to the image.");
|
// println!("Please provide a path to the image.");
|
||||||
process::exit(1);
|
// process::exit(1);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
let img = ImageReader::open(&args[1])
|
// let img = ImageReader::open(&args[1])
|
||||||
.map_err(|err| ImageError::IoError(err))
|
// .map_err(|err| ImageError::IoError(err))
|
||||||
.and_then(|v| v.decode())
|
// .and_then(|v| v.decode())
|
||||||
.unwrap();
|
// .unwrap();
|
||||||
|
|
||||||
let mut printer = Printer::new(384);
|
let mut printer = Printer::new(384);
|
||||||
let job = printer.new_job().unwrap();
|
let job = printer.new_job().unwrap();
|
||||||
|
|
@ -34,7 +34,20 @@ fn main() {
|
||||||
// Some(BARType::CODE128),
|
// Some(BARType::CODE128),
|
||||||
// )
|
// )
|
||||||
// .unwrap();
|
// .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.write_feed(Some(2));
|
||||||
job.ready = true;
|
job.ready = true;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue