f-office-docx 0.3.0

DOCX model and Word's layout rules.
Documentation
//! The 3-D bar groups of a chart part (`c:bar3DChart`, ECMA-376 Part 1, 21.2.2.15): what a 3-D
//! group adds to a bar group as the part writes it, and the drawn case of clustered columns of
//! box shape seen with right-angle axes that a target whose plot rules draw them
//! ([`PlotRules::draws_columns_3d`]) draws.

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;

/// The largest tilt and turn of a drawn 3-D column chart's view, in degrees: a quarter turn.
const LARGEST_ANGLE: i64 = QUARTER_TURN / DEGREE;

/// What a 3-D bar group adds to a bar group, with the chart's view and walls.
pub(super) struct Solid {
    /// The gap in depth in front of and behind the columns, as a percentage of a column's
    /// depth (`c:gapDepth`), [`CHART_GAP_DEPTH`] when it does not read.
    gap_depth: i64,
    /// Whether the group's columns are boxes: `c:shape` of the group and of each series
    /// `box`, or not written.
    boxes: bool,
    /// The chart's view, `None` when it is not one of right-angle axes with a tilt and a
    /// turn from 0 to a quarter turn.
    view: Option<ColumnView>,
    /// Whether the floor and the walls have no fill and no line of their own.
    plain_walls: bool,
}

/// The view of a 3-D column chart with right-angle axes (`c:view3D` with `c:rAngAx` on).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct ColumnView {
    /// How far the view looks down onto the floor, in degrees (`c:rotX`).
    pub(crate) tilt: i64,
    /// How far the view looks from the right onto the columns' sides, in degrees (`c:rotY`).
    pub(crate) turn: i64,
    /// The depth of a column as a percentage of its width (`c:depthPercent`),
    /// [`CHART_DEPTH_PERCENT`] when it does not read.
    pub(crate) depth: i64,
}

impl Solid {
    /// Reads what the 3-D bar group `node` adds, with the chart's view `view` (`c:view3D`) and
    /// whether its floor and walls are plain (`plain_walls`, [`plain_walls`]).
    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,
        }
    }

    /// The drawn group of these clustered columns of `direction` and `grouping`, with the gap
    /// width `gap` and no overlap (`overlap`), on a target that draws 3-D columns (`rules`), or
    /// why it is not one: [`ChartReason::OtherGroup`] for another form.
    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;
        // quirk: ChartColumns3D::Picture (src/quirks.rs)
        match self.view {
            Some(view) if drawn => Ok(ChartGroup::Columns3D {
                gap,
                gap_depth: self.gap_depth,
                view,
            }),
            _ => Err(ChartReason::OtherGroup),
        }
    }
}

impl ColumnView {
    /// Reads the view `node`, or `None` when its axes are not at right angles or its tilt or
    /// turn lies outside 0 to a quarter turn.
    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 })
    }
}

/// Whether the floor and the walls of the chart `chart` (`c:floor`, `c:sideWall` and
/// `c:backWall`) have no fill and no line of their own: each not written, without shape
/// properties, or with properties that write no fill but `a:noFill` and whose line is not
/// written or is `a:noFill`.
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())
            })
    })
}