use crate::model::chart::{Anchor, ChartColor, Fill, LineFormat, ShapeFormat};
use crate::style::{Color, DiffFont};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Shape {
pub name: String,
pub description: String,
pub anchor: Anchor,
pub rotation: i32,
pub flip_h: bool,
pub flip_v: bool,
pub geometry: Option<String>,
pub format: ShapeFormat,
pub text: String,
pub font: DiffFont,
pub origin: Option<ShapeOrigin>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) struct ShapeStyle {
pub fill: Option<(u32, Option<ChartColor>)>,
pub line: Option<(u32, Option<ChartColor>)>,
pub font: Option<Color>,
}
impl ShapeStyle {
pub(crate) fn office() -> Self {
let accent = || ChartColor::scheme("accent1");
let mut dark = accent();
dark.transforms
.push(crate::model::chart::ColorTransform::Shade(50_000));
Self {
fill: Some((1, Some(accent()))),
line: Some((2, Some(dark))),
font: Some(Color::Theme { id: 0, tint: 0 }),
}
}
}
fn style_fill(reference: Option<&(u32, Option<ChartColor>)>) -> Fill {
match reference {
Some((index, color)) if *index > 0 => color.clone().map_or(Fill::Other, Fill::Solid),
_ => Fill::None,
}
}
impl Shape {
#[must_use]
pub fn new(geometry: impl Into<String>, anchor: Anchor) -> Self {
Self {
name: String::new(),
description: String::new(),
anchor,
rotation: 0,
flip_h: false,
flip_v: false,
geometry: Some(geometry.into()),
format: ShapeFormat::default(),
text: String::new(),
font: DiffFont::default(),
origin: None,
}
}
fn style(&self) -> std::borrow::Cow<'_, ShapeStyle> {
self.origin.as_ref().map_or_else(
|| std::borrow::Cow::Owned(ShapeStyle::office()),
|o| std::borrow::Cow::Borrowed(&o.style),
)
}
#[must_use]
pub fn effective_fill(&self) -> Fill {
self.format
.fill
.clone()
.unwrap_or_else(|| style_fill(self.style().fill.as_ref()))
}
#[must_use]
pub fn effective_line(&self) -> LineFormat {
let own = self.format.line.clone().unwrap_or_default();
LineFormat {
fill: Some(
own.fill
.unwrap_or_else(|| style_fill(self.style().line.as_ref())),
),
width: own.width,
}
}
#[must_use]
pub fn effective_font(&self) -> DiffFont {
let mut font = self.font.clone();
if font.color.is_none() {
font.color.clone_from(&self.style().font);
}
font
}
#[must_use]
pub fn is_unchanged(&self) -> bool {
self.origin.as_ref().is_some_and(|o| {
o.name == self.name
&& o.description == self.description
&& o.anchor == self.anchor
&& o.rotation == self.rotation
&& o.flip_h == self.flip_h
&& o.flip_v == self.flip_v
&& o.geometry == self.geometry
&& o.format == self.format
&& o.text == self.text
&& o.font == self.font
})
}
pub(crate) fn settle(&mut self) {
if let Some(origin) = &mut self.origin {
origin.anchor = self.anchor;
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShapeOrigin {
pub(crate) drawing: String,
pub(crate) id: u32,
pub(crate) grouped: bool,
pub(crate) name: String,
pub(crate) description: String,
pub(crate) anchor: Anchor,
pub(crate) rotation: i32,
pub(crate) flip_h: bool,
pub(crate) flip_v: bool,
pub(crate) geometry: Option<String>,
pub(crate) format: ShapeFormat,
pub(crate) text: String,
pub(crate) font: DiffFont,
pub(crate) style: ShapeStyle,
pub(crate) read_from_drawing: usize,
}
impl ShapeOrigin {
#[must_use]
pub fn drawing(&self) -> &str {
&self.drawing
}
#[must_use]
pub const fn grouped(&self) -> bool {
self.grouped
}
}