f-office-paint 0.2.3

Display list: the contract between layout and output.
Documentation
//! The page offsets of a PDF writer whose page offset takes other steps on pages of other
//! sizes: given steps for some sizes, and the page entry of its paper form for every other page.

use f_office_units::{Length, div_round_i128};

use crate::paper_forms::{ClassRule, FormSide, SizedRule, entry_steps, page_entry};
use crate::{Fraction, Offset, OffsetProduct, Orientation};

/// A page size in written dots with given steps of the page form of the offset
/// ([`Offset::Page`]) across, down or both.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct GivenSize {
    /// The page's width and height in written dots.
    pub dots: (i64, i64),
    /// The orientation of the sections whose pages of this size take the steps, or `None` for
    /// every orientation.
    pub orientation: Option<Orientation>,
    /// The steps across and down, or `None` on an axis that takes the steps of the page entry
    /// of the page's paper form, as a page of another size does.
    pub steps: (Option<Fraction>, Option<Fraction>),
}

/// The page offsets of a writer whose page form of the offset ([`Offset::Page`]) takes the
/// steps of a page's size: a page whose size and orientation a [`GivenSize`] names takes its
/// steps, and every other page the steps of the page entry of its paper form, on each axis
/// ([`ClassRule`], [`page_entry`]). The steps round in `parts` of a step first, and the
/// product is taken as `product` says.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PageSizes {
    /// The parts of a step in which the offsets round the product before the division
    /// ([`Offset::Page`]).
    parts: i64,
    /// How the offsets take the product of a place and the steps ([`Offset::Page`]).
    product: OffsetProduct,
    /// How many of the writer's steps make an inch.
    per_inch: i64,
    /// How many written dots make an inch.
    dots_per_inch: i64,
    /// The sizes with given steps, the first that names a page's size and orientation first.
    sizes: Vec<GivenSize>,
    /// The class rule, with its forms' sides in written dots.
    rule: SizedRule,
}

impl PageSizes {
    /// The page offsets with `parts` of a step, `per_inch` steps and `dots_per_inch` written
    /// dots to the inch, the product `product`, the given sizes `sizes` and the class rule
    /// `rule`.
    #[must_use]
    pub fn new(
        (parts, per_inch, dots_per_inch): (i64, i64, i64),
        product: OffsetProduct,
        sizes: Vec<GivenSize>,
        rule: &ClassRule,
    ) -> Self {
        Self {
            parts,
            product,
            per_inch,
            dots_per_inch,
            sizes,
            rule: SizedRule::of(rule, dots_per_inch),
        }
    }

    /// The offsets across and down a page `width` by `height` half-EMUs in a section of
    /// `orientation`: on each axis, its size's given steps when a [`GivenSize`] names its size
    /// in written dots, each side rounded to whole dots a half up, and its orientation, and
    /// gives steps on that axis; else the steps of the page entry of its paper form. No offset
    /// on an axis whose entry the page does not give: a side of one dot or less, or a size or
    /// count that does not fit.
    #[must_use]
    pub fn offsets(&self, width: i64, height: i64, orientation: Orientation) -> (Offset, Offset) {
        let none = (Offset::None, Offset::None);
        if self.dots_per_inch <= 0 {
            return none;
        }
        let inch = i128::from(Length::UNITS_PER_INCH);
        let dots = |side: i64| {
            i64::try_from(div_round_i128(
                i128::from(side) * i128::from(self.dots_per_inch),
                inch,
            ))
            .ok()
        };
        let (Some(across), Some(down)) = (dots(width), dots(height)) else {
            return none;
        };
        let page = |steps: Option<Fraction>| {
            steps.map_or(Offset::None, |steps| Offset::Page {
                steps,
                parts: self.parts,
                per_inch: self.per_inch,
                product: self.product,
            })
        };
        let (given_across, given_down) = self
            .sizes
            .iter()
            .find(|given| {
                given.dots == (across, down) && given.orientation.is_none_or(|o| o == orientation)
            })
            .map_or((None, None), |given| given.steps);
        if let (Some(steps_across), Some(steps_down)) = (given_across, given_down) {
            return (page(Some(steps_across)), page(Some(steps_down)));
        }
        let form = self.rule.form((across, down), orientation);
        (
            page(given_across.or_else(|| self.form_steps(across, form.across))),
            page(given_down.or_else(|| self.form_steps(down, form.down))),
        )
    }

    /// The steps that the page entry of a side of `dots` written dots gives for the form side
    /// `side`.
    fn form_steps(&self, dots: i64, side: FormSide) -> Option<Fraction> {
        let writer = (self.parts, self.per_inch, self.dots_per_inch);
        entry_steps(page_entry(dots, side, writer)?, dots, writer)
    }
}

#[cfg(test)]
mod tests;