f-office-docx 0.3.0

DOCX model and Word's layout rules.
Documentation
//! The x axis of a drawn scatter chart, on a target whose quirk `ChartScatter` is `Drawn`: its
//! units from the frame's width, the plot area's right edge, the places across of its values,
//! its tick marks and its labels; and the clip of its series' lines. The scatter chart takes the plot area, the value axis and the
//! value labels of its line chart twin, the line chart of its y values in the same frame.

use f_office_paint::{GroupClip, Item};
use f_office_units::scale::TENTHS;
use f_office_units::{Edges, Length};

use super::{LabelLines, ValueAxis, chart_props};
use crate::chart::{Columns, Decimal, LabelFormat, PlotPlace, SetScale, all_digits};
use crate::layout::Layout;
use crate::params::{
    CHART_AXIS_STEPS, CHART_SCATTER_CLIP_ROOM, CHART_SCATTER_FRAME_ROOM, CHART_SCATTER_ZERO_SPACING,
};

impl Layout<'_> {
    /// The x axis of a scatter chart whose series' x values are `x_values`, in its frame
    /// `frame`, and the right edge of its plot area from `plot`, its line chart twin's: the
    /// automatic axis of the x values that takes at most [`x_units`] units for the frame's width
    /// and the advance of the labels' zero, and the twin's right edge less half the width of
    /// the last x label. `None` when a number leaves the digits of a decimal or a label has no
    /// width in one face.
    pub(super) fn x_axis(
        &mut self,
        chart: &Columns,
        (x_values, x_scale): (&[Vec<Decimal>], SetScale),
        (frame, plot): (Edges<Length>, Edges<Length>),
    ) -> Option<(ValueAxis, Length)> {
        let props = chart_props(chart, chart.label_size);
        let zero = self.chart_text(&props, "0")?.width();
        let most = x_units(frame.right - frame.left, zero);
        let axis = ValueAxis::over(x_values.iter().flatten().copied(), x_scale, most)?;
        if axis.low.digits.is_negative() {
            return None;
        }
        let right = if chart.axis_forms.category_hidden
            || matches!(chart.plot_place, PlotPlace::Manual(layout) if layout.inner)
        {
            plot.right
        } else {
            let last = self
                .chart_text(&props, &all_digits(axis.label(axis.steps)?))?
                .width();
            plot.right - last.div_toward_zero(2)
        };
        Some((axis, right))
    }

    /// The clip inside which a scatter chart's series lines show, and a line chart's on a
    /// target that clips them, for its plot area `plot`: on
    /// the layout device's dots, from the plot area's left edge and top, each rounded down, to
    /// its right edge and bottom, each [`CHART_SCATTER_CLIP_ROOM`] further out and rounded
    /// down. A point at the plot area's left edge shows the right half of its line's round cap.
    pub(super) fn lines_clip(&self, plot: Edges<Length>) -> GroupClip {
        let left = self.device.floor(plot.left);
        let top = self.device.floor(plot.top);
        let right = self.device.floor(plot.right + CHART_SCATTER_CLIP_ROOM);
        let bottom = self.device.floor(plot.bottom + CHART_SCATTER_CLIP_ROOM);
        GroupClip::rect(left, top, right - left, bottom - top)
    }

    /// The x axis' tick marks outside the plot area below the axis at `crossing`, when the
    /// axis across draws them ([`Columns::ticks`]): one at each label, placed across as
    /// [`Self::place`] places it, [`LabelLines::top_part`] of the labels long, in the
    /// axis line's look.
    pub(super) fn x_ticks(
        &mut self,
        chart: &Columns,
        axis: &ValueAxis,
        plot: Edges<Length>,
        crossing: Length,
    ) -> Vec<Item> {
        if !chart.ticks.category {
            return Vec::new();
        }
        let Some(length) = self.label_lines(chart).map(LabelLines::top_part) else {
            return Vec::new();
        };
        axis.labels()
            .filter_map(|value| self.place(axis, (plot.left, plot.right), value))
            .flat_map(|x| self.chart_upright(&chart.axis, (crossing, crossing + length), x))
            .collect()
    }

    /// One label for each value of the axis `axis` across the bottom, in the label format
    /// `format` (General for a scatter chart's x axis): centered by its width at the size
    /// `size`, in half-points, on its value's exact place across, with its baseline
    /// [`LabelLines::category_baseline`] below the plot area, as a category label stands. When
    /// a label is wider than the distance between two labels, which these rules do not place,
    /// none is drawn.
    pub(super) fn x_labels(
        &mut self,
        chart: &Columns,
        (axis, format, size): (&ValueAxis, &LabelFormat, i64),
        plot: Edges<Length>,
        lines: LabelLines,
    ) -> Option<Vec<Item>> {
        let baseline = plot.bottom + lines.category_baseline()?;
        let spacing = (plot.right - plot.left).div_toward_zero(axis.steps.max(1));
        let color = self.label_color(chart.category_label_fill.as_ref());
        let props = chart_props(chart, size);
        axis.labels()
            .map(|value| {
                let at = axis.along((plot.left, plot.right), value)?;
                self.value_label(&props, (format, value), color, |room| {
                    (room <= spacing).then(|| (at - room.div_toward_zero(2), baseline))
                })
            })
            .collect()
    }
}

/// The most units of a scatter chart's x axis in a frame `width` wide whose labels' zero
/// advances `zero` at their size: as many as the width less [`CHART_SCATTER_FRAME_ROOM`] holds
/// at [`CHART_SCATTER_ZERO_SPACING`] tenths of the zero each, rounded down, at least one and at
/// most [`CHART_AXIS_STEPS`].
pub(super) fn x_units(width: Length, zero: Length) -> i64 {
    let room = i128::from((width - CHART_SCATTER_FRAME_ROOM).units()) * i128::from(TENTHS);
    let unit = i128::from(zero.units()) * i128::from(CHART_SCATTER_ZERO_SPACING);
    if unit <= 0 {
        return CHART_AXIS_STEPS;
    }
    i64::try_from(room.div_euclid(unit))
        .unwrap_or(CHART_AXIS_STEPS)
        .clamp(1, CHART_AXIS_STEPS)
}