use imbl::Vector;
use logisheets_workbook::prelude::{CtDxf, CtDxfs, StDxfId};
use super::RawStyle;
#[derive(Debug, Clone, Default)]
pub struct DxfManager {
dxfs: Vector<CtDxf>,
}
impl DxfManager {
pub fn from_ct_dxfs(part: Option<&CtDxfs>) -> Self {
let dxfs = match part {
Some(p) => p.dxfs.iter().cloned().collect(),
None => Vector::new(),
};
DxfManager { dxfs }
}
pub fn get(&self, id: StDxfId) -> Option<&CtDxf> {
self.dxfs.get(id as usize)
}
pub fn len(&self) -> usize {
self.dxfs.len()
}
pub fn is_empty(&self) -> bool {
self.dxfs.is_empty()
}
pub fn intern(&mut self, dxf: CtDxf) -> StDxfId {
self.dxfs.push_back(dxf);
(self.dxfs.len() - 1) as StDxfId
}
pub fn replace(&mut self, id: StDxfId, dxf: CtDxf) -> bool {
let idx = id as usize;
if idx >= self.dxfs.len() {
return false;
}
self.dxfs.set(idx, dxf);
true
}
pub fn to_ct_dxfs(&self) -> Option<CtDxfs> {
if self.dxfs.is_empty() {
return None;
}
Some(CtDxfs {
count: self.dxfs.len() as u32,
dxfs: self.dxfs.iter().cloned().collect(),
})
}
}
pub fn apply_dxf(mut base: RawStyle, dxf: &CtDxf) -> RawStyle {
if let Some(f) = &dxf.font {
let b = &mut base.font;
b.bold |= f.bold;
b.italic |= f.italic;
b.strike |= f.strike;
b.outline |= f.outline;
b.shadow |= f.shadow;
b.condense |= f.condense;
b.extend |= f.extend;
if f.underline.is_some() {
b.underline = f.underline.clone();
}
if f.color.is_some() {
b.color = f.color.clone();
}
if f.sz.is_some() {
b.sz = f.sz.clone();
}
if f.name.is_some() {
b.name = f.name.clone();
}
if f.charset.is_some() {
b.charset = f.charset.clone();
}
if f.family.is_some() {
b.family = f.family.clone();
}
if f.vert_align.is_some() {
b.vert_align = f.vert_align.clone();
}
if f.scheme.is_some() {
b.scheme = f.scheme.clone();
}
}
if let Some(fill) = &dxf.fill {
base.fill = fill.clone();
}
if let Some(bd) = &dxf.border {
let b = &mut base.border;
if bd.left.is_some() {
b.left = bd.left.clone();
}
if bd.right.is_some() {
b.right = bd.right.clone();
}
if bd.top.is_some() {
b.top = bd.top.clone();
}
if bd.bottom.is_some() {
b.bottom = bd.bottom.clone();
}
if bd.diagonal.is_some() {
b.diagonal = bd.diagonal.clone();
}
if bd.vertical.is_some() {
b.vertical = bd.vertical.clone();
}
if bd.horizontal.is_some() {
b.horizontal = bd.horizontal.clone();
}
}
if let Some(a) = &dxf.alignment {
base.alignment = Some(a.clone());
}
if let Some(p) = &dxf.protection {
base.protection = Some(p.clone());
}
if let Some(nf) = &dxf.num_fmt {
base.formatter = nf.format_code.clone();
}
base
}