use f_office_units::scale::{DEGREE, QUARTER_TURN};
use f_office_xml::{Document, NodeId};
use super::{BarDirection, BarGrouping, ChartGroup, ChartReason, Flag, PlotRules, read_value};
use crate::ns::{A, C};
use crate::params::{
CHART_DEPTH_PERCENT, CHART_DEPTH_PERCENTS, CHART_GAP_DEPTH, CHART_LARGEST_GAP,
};
use crate::reader::PartReader;
const LARGEST_ANGLE: i64 = QUARTER_TURN / DEGREE;
pub(super) struct Solid {
gap_depth: i64,
boxes: bool,
view: Option<ColumnView>,
plain_walls: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct ColumnView {
pub(crate) tilt: i64,
pub(crate) turn: i64,
pub(crate) depth: i64,
}
impl Solid {
pub(super) fn read(
reader: &mut PartReader,
node: NodeId,
(view, plain_walls): (Option<NodeId>, bool),
) -> Self {
let doc = reader.doc;
let box_shape = |at: NodeId| {
doc.child(at, C, "shape")
.is_none_or(|shape| doc.attribute(shape, None, "val").is_none_or(|v| v == "box"))
};
let boxes = box_shape(node)
&& doc
.child_elements(node)
.filter(|&n| doc.is(n, C, "ser"))
.all(box_shape);
let gap_depth = read_value(reader, node, "gapDepth", f_office_drawingml::chart_percent)
.filter(|gap| (0..=CHART_LARGEST_GAP).contains(gap))
.unwrap_or(CHART_GAP_DEPTH);
Self {
gap_depth,
boxes,
view: view.and_then(|view| ColumnView::read(reader, view)),
plain_walls,
}
}
pub(super) fn drawn(
&self,
(direction, grouping): (BarDirection, BarGrouping),
(gap, overlap): (i64, i64),
rules: PlotRules,
) -> Result<ChartGroup, ChartReason> {
let drawn = rules.draws_columns_3d()
&& direction == BarDirection::Vertical
&& grouping == BarGrouping::Clustered
&& overlap == 0
&& self.boxes
&& self.plain_walls;
match self.view {
Some(view) if drawn => Ok(ChartGroup::Columns3D {
gap,
gap_depth: self.gap_depth,
view,
}),
_ => Err(ChartReason::OtherGroup),
}
}
}
impl ColumnView {
fn read(reader: &mut PartReader, node: NodeId) -> Option<Self> {
if !Flag::of(reader.doc, node, "rAngAx").is_on() {
return None;
}
let mut angle =
|name: &str| read_value(reader, node, name, |v| v.parse::<i64>().ok()).unwrap_or(0);
let (tilt, turn) = (angle("rotX"), angle("rotY"));
let depth = read_value(
reader,
node,
"depthPercent",
f_office_drawingml::chart_percent,
)
.unwrap_or(CHART_DEPTH_PERCENT);
let within = |value: i64| (0..=LARGEST_ANGLE).contains(&value);
(within(tilt)
&& within(turn)
&& (CHART_DEPTH_PERCENTS.0..=CHART_DEPTH_PERCENTS.1).contains(&depth))
.then_some(Self { tilt, turn, depth })
}
}
pub(super) fn plain_walls(doc: &Document, chart: NodeId) -> bool {
let fills = |at: NodeId| {
doc.child_elements(at).any(|n| {
["solidFill", "gradFill", "pattFill", "blipFill", "grpFill"]
.iter()
.any(|name| doc.is(n, A, name))
})
};
["floor", "sideWall", "backWall"].iter().all(|name| {
doc.child(chart, C, name)
.and_then(|wall| doc.child(wall, C, "spPr"))
.is_none_or(|props| {
!fills(props)
&& doc
.child(props, A, "ln")
.is_none_or(|line| doc.child(line, A, "noFill").is_some())
})
})
}