use super::pdf417_error_correction::PDF417ErrorCorrection;
use crate::models::print_sections::Pdf417 as Pdf417Section;
#[derive(Debug, Clone)]
pub struct PDF417 {
data: String,
columns: u8, rows: u8, width: u8, height: u8, error_correction: PDF417ErrorCorrection,
}
impl PDF417 {
pub fn new(data: String) -> Self {
Self {
data,
columns: 0, rows: 0, width: 3,
height: 3,
error_correction: PDF417ErrorCorrection::Level1,
}
}
pub fn set_columns(mut self, columns: u8) -> Self {
if columns <= 30 {
self.columns = columns;
}
self
}
pub fn set_rows(mut self, rows: u8) -> Self {
if rows == 0 || (rows >= 3 && rows <= 90) {
self.rows = rows;
}
self
}
pub fn set_width(mut self, width: u8) -> Self {
if (2..=8).contains(&width) {
self.width = width;
}
self
}
pub fn set_height(mut self, height: u8) -> Self {
if (2..=8).contains(&height) {
self.height = height;
}
self
}
pub fn set_error_correction(mut self, error_correction: PDF417ErrorCorrection) -> Self {
self.error_correction = error_correction;
self
}
pub fn get_command(&self) -> Vec<u8> {
let mut output = Vec::new();
let data_bytes = self.data.as_bytes();
let data_length = (data_bytes.len() + 3) as u16;
let p_l = (data_length & 0xFF) as u8;
let p_h = ((data_length >> 8) & 0xFF) as u8;
output.extend_from_slice(&[
0x1D, 0x28, 0x6B, 0x03, 0x00, 0x30, 0x41, self.columns, ]);
output.extend_from_slice(&[
0x1D, 0x28, 0x6B, 0x03, 0x00, 0x30, 0x42, self.rows, ]);
output.extend_from_slice(&[
0x1D, 0x28, 0x6B, 0x03, 0x00, 0x30, 0x43, self.width, ]);
output.extend_from_slice(&[
0x1D, 0x28, 0x6B, 0x03, 0x00, 0x30, 0x44, self.height, ]);
output.extend_from_slice(&[
0x1D, 0x28, 0x6B, 0x03, 0x00, 0x30, 0x45, self.error_correction.value(), ]);
output.extend_from_slice(&[
0x1D, 0x28, 0x6B, p_l, p_h, 0x30, 0x50, 0x30, ]);
output.extend_from_slice(data_bytes);
output.extend_from_slice(&[
0x1D, 0x28, 0x6B, 0x03, 0x00, 0x30, 0x51, 0x30, ]);
output
}
}
pub fn process_section(pdf417: &Pdf417Section) -> Result<Vec<u8>, String> {
let error_correction = match pdf417.error_correction {
0 => PDF417ErrorCorrection::Level0,
1 => PDF417ErrorCorrection::Level1,
2 => PDF417ErrorCorrection::Level2,
3 => PDF417ErrorCorrection::Level3,
4 => PDF417ErrorCorrection::Level4,
5 => PDF417ErrorCorrection::Level5,
6 => PDF417ErrorCorrection::Level6,
7 => PDF417ErrorCorrection::Level7,
8 => PDF417ErrorCorrection::Level8,
_ => PDF417ErrorCorrection::Level1,
};
let esc_pos_pdf417 = PDF417::new(pdf417.data.clone())
.set_columns(pdf417.columns)
.set_rows(pdf417.rows)
.set_height(pdf417.height)
.set_width(pdf417.width)
.set_error_correction(error_correction);
let mut data = esc_pos_pdf417.get_command();
data.extend_from_slice(b"\n");
Ok(data)
}
impl PDF417 {
}