use std::sync::{Arc, Mutex};
use crate::args::{CBold, CBoldOptions, CMag, CMagOptions, CUnderLine, CUnderLineOptions};
use psdk_father::{
args::{ArgContext, PsdkArgBase, PsdkEasyArg},
command::{CommandClause, PsdkTextCommand},
errors::PsdkFatherResult,
types::psdk_types::PsdkPrinterCommandEnumValue,
};
use crate::marks::{Font, Rotation};
#[derive(Clone, Debug)]
pub struct CTextOptions {
pub font: Option<Font>,
pub x: i32,
pub y: i32,
pub content: String,
pub bold: Option<bool>,
pub underline: Option<bool>,
pub mag: Option<bool>,
pub rotation: Option<Rotation>,
}
#[derive(Clone, Debug)]
pub struct CText {
options: CTextOptions,
ctx: Arc<Mutex<ArgContext>>,
}
impl CText {
pub fn new(options: CTextOptions) -> Self {
Self {
options,
ctx: Default::default(),
}
}
}
impl<T: Into<String>> From<(i32, i32, T)> for CText {
fn from((x, y, content): (i32, i32, T)) -> Self {
Self::new(CTextOptions {
font: None,
x,
y,
content: content.into(),
bold: None,
underline: None,
mag: None,
rotation: None,
})
}
}
impl PsdkEasyArg<String> for CText {
fn context(&self) -> Arc<Mutex<ArgContext>> {
self.ctx.clone()
}
}
impl PsdkArgBase<String> for CText {
fn header(&self) -> String {
let rotation = self.options.rotation.clone().unwrap_or(Rotation::Rotation0);
format!("T{}", rotation.command_value())
}
fn clause(&self) -> PsdkFatherResult<CommandClause> {
let mut ptc = PsdkTextCommand::with_header_format_cpcl(self.header());
let font = self.options.font.clone().unwrap_or(Font::Tss16);
if let Some(bold) = self.options.bold {
let cbold = CBold::new(CBoldOptions { enable: bold });
self.prepend(&cbold).unwrap();
}
if let Some(underline) = self.options.underline {
let cunder_line = CUnderLine::new(CUnderLineOptions { enable: underline });
self.prepend(&cunder_line).unwrap();
}
if self.options.mag.is_some() {
let cmag = CMag::new(CMagOptions {
font: Some(font.clone()),
});
self.prepend(&cmag).unwrap();
}
ptc
.append(font.get_family())
.append(font.get_size())
.append(self.options.x)
.append(self.options.y)
.append(&self.options.content);
Ok(ptc.clause())
}
}
psdk_father::impl_psdk_arg_to_easy_arg!(String, CText, true);