f-office-paint 0.2.2

Display list: the contract between layout and output.
Documentation
use f_office_units::binary32::Binary32;

use super::{
    ClassRule, DocumentedSide, FormSide, PaperForm, SizedRule, entry_steps, frame_side, page_entry,
};
use crate::Orientation;

/// Word for Windows's writer: 16 parts of a step, 3,000 steps and 600 dots to the inch.
const WRITER: (i64, i64, i64) = (16, 3_000, 600);

const LETTER: PaperForm = PaperForm::inches("LETTER", 8_500, 11_000);
const A3: PaperForm = PaperForm::millimetres("A3", 297, 420);
const A4: PaperForm = PaperForm::millimetres("A4", 210, 297);

/// Some documented forms in the documentation's order, with two that repeat a form's sides.
const FORMS: &[PaperForm] = &[
    LETTER,
    PaperForm::inches("LETTERSMALL", 8_500, 11_000),
    A3,
    PaperForm::millimetres("A3_ROTATED", 420, 297).rotated(),
    A4,
    PaperForm::millimetres("A4_ROTATED", 297, 210).rotated(),
    PaperForm::millimetres("A4SMALL", 210, 297),
    PaperForm::millimetres("A5", 148, 210),
    PaperForm::inches("STATEMENT", 5_500, 8_500),
    PaperForm::inches("LEDGER", 17_000, 11_000),
];

const RULE: ClassRule = ClassRule {
    forms: FORMS,
    reach: 100,
    holding: &[A4, A3],
    last: LETTER,
};

/// The sides of the form `form` at 600 dpi.
fn sides(form: PaperForm) -> (FormSide, FormSide) {
    (form.across.form_side(600), form.down.form_side(600))
}

/// A form side of `millimetres` whole mm and `dots` dots.
const fn side(millimetres: i64, dots: i64) -> FormSide {
    FormSide { millimetres, dots }
}

/// The binary32 number nearest to a decimal of `places` places.
fn decimal(digits: i128, places: u32) -> Binary32 {
    Binary32::nearest(digits, 10_i128.pow(places))
}

#[test]
fn a_documented_side_gives_whole_mm_and_dots_each_rounded_a_half_up() {
    assert_eq!(sides(A4), (side(210, 4_961), side(297, 7_016)));
    assert_eq!(sides(LETTER), (side(216, 5_100), side(279, 6_600)));
    assert_eq!(sides(A3), (side(297, 7_016), side(420, 9_921)));
    // 3.875 by 8.875 inches: 98.425 by 225.425 mm.
    let envelope = PaperForm::inches("ENV_9", 3_875, 8_875);
    assert_eq!(sides(envelope), (side(98, 2_325), side(225, 5_325)));
    assert_eq!(
        DocumentedSide::InchThousandths(9_270).form_side(600),
        side(235, 5_562)
    );
}

#[test]
fn gives_the_dots_per_inch_of_a_form_side() {
    // Letter down gives 600.860107 dots per inch, and A3 down 599.984192.
    assert_eq!(super::density(side(279, 6_600)), decimal(600_860_107, 6));
    assert_eq!(super::density(side(420, 9_921)), decimal(599_984_192, 6));
}

#[test]
fn gives_the_frame_side_of_a_page_side() {
    let (a4_across, a4_down) = sides(A4);
    assert_eq!(frame_side(4_958, a4_across), Some(20_983));
    assert_eq!(frame_side(4_961, a4_across), Some(20_996));
    assert_eq!(frame_side(7_017, a4_down), Some(29_700));
    assert_eq!(frame_side(5_100, sides(LETTER).0), Some(21_596));
    assert_eq!(frame_side(6_600, sides(A3).1), Some(27_937));
}

#[test]
fn gives_the_page_entry_of_a_page_side() {
    let (a4_across, a4_down) = sides(A4);
    let entry = |dots, form_side| page_entry(dots, form_side, WRITER);
    assert_eq!(entry(4_958, a4_across), Some(decimal(80_016_396, 6)));
    assert_eq!(entry(7_017, a4_down), Some(decimal(80_011_406, 6)));
    assert_eq!(entry(4_961, a4_across), Some(decimal(80_015_244, 6)));
    assert_eq!(entry(6_600, sides(A3).1), Some(decimal(80_010_689, 6)));
}

#[test]
fn a_given_raster_gives_its_frame_and_entry() {
    // Two pages whose rasters are given as input: their sides in whole mm and dots in place
    // of a form's.
    for (dots, raster, frame, entry) in [
        (5_100, side(216, 5_102), 21_587, 80_016_799_927),
        (6_520, side(277, 6_543), 27_598, 80_013_420_105),
        (4_725, side(197, 4_654), 19_996, 80_018_150_330),
        (6_261, side(267, 6_307), 26_501, 80_012_870_789),
    ] {
        assert_eq!(frame_side(dots, raster), Some(frame), "{dots}");
        assert_eq!(
            page_entry(dots, raster, WRITER),
            Some(decimal(entry, 9)),
            "{dots}"
        );
    }
}

#[test]
fn a_side_of_one_dot_or_a_form_side_of_nothing_has_no_frame() {
    let (a4_across, _) = sides(A4);
    assert_eq!(frame_side(1, a4_across), None);
    assert_eq!(frame_side(0, a4_across), None);
    assert_eq!(frame_side(4_961, side(0, 4_961)), None);
    assert_eq!(frame_side(4_961, side(210, 0)), None);
    assert_eq!(page_entry(1, a4_across, WRITER), None);
}

#[test]
fn the_steps_of_an_entry_give_the_entry_back() {
    // entry = parts (steps per dot + c / D), exactly.
    let (a4_across, a4_down) = sides(A4);
    for (dots, form_side) in [
        (4_962, a4_across),
        (7_014, a4_down),
        (5_100, sides(LETTER).0),
    ] {
        let entry = page_entry(dots, form_side, WRITER).unwrap_or(Binary32::ZERO);
        let Some((top, bottom)) = entry_steps(entry, dots, WRITER) else {
            panic!("{dots}");
        };
        // entry = 16 (5 + top / (bottom dots)), with entry = mantissa 2^exponent.
        let power = 1_i128 << entry.exponent().unsigned_abs();
        let left = i128::from(entry.mantissa()) * i128::from(bottom) * i128::from(dots);
        let right = 16 * (5 * i128::from(bottom) * i128::from(dots) + i128::from(top)) * power;
        assert!(entry.exponent() < 0);
        assert_eq!(left, right, "{dots}");
    }
}

/// The name of the form that the class rule gives a page in a section of `orientation`.
fn form(dots: (i64, i64), orientation: Orientation) -> &'static str {
    SizedRule::of(&RULE, 600).form(dots, orientation).form.name
}

#[test]
fn the_class_rule_takes_the_nearest_form_within_the_reach() {
    let portrait = Orientation::Portrait;
    assert_eq!(form((4_962, 7_014), portrait), "A4");
    assert_eq!(form((5_195, 6_660), portrait), "LETTER");
    assert_eq!(form((5_100, 6_550), portrait), "LETTER");
    // Two forms as near: the first in the documentation's order.
    assert_eq!(form((4_961, 7_016), portrait), "A4");
    assert_eq!(form((5_100, 6_600), portrait), "LETTER");
    // A form wider than high that the documentation does not name rotated.
    assert_eq!(form((10_200, 6_600), portrait), "LEDGER");
    assert_eq!(form((3_300, 5_100), Orientation::Landscape), "STATEMENT");
}

#[test]
fn the_class_rule_holds_a_page_far_from_every_form_in_a4_a3_or_letter() {
    let portrait = Orientation::Portrait;
    assert_eq!(form((4_725, 6_261), portrait), "A4");
    assert_eq!(form((5_120, 6_705), portrait), "A3");
    assert_eq!(form((5_205, 6_660), portrait), "A3");
    assert_eq!(form((20_000, 30_000), portrait), "LETTER");
}

#[test]
fn a_portrait_section_takes_no_rotated_form() {
    assert_eq!(form((7_016, 4_961), Orientation::Portrait), "A3");
    assert_eq!(form((9_921, 7_016), Orientation::Portrait), "LETTER");
}

#[test]
fn a_page_across_in_a_landscape_section_takes_its_upright_form_turned() {
    let rule = SizedRule::of(&RULE, 600);
    let landscape = Orientation::Landscape;
    let turned = |form: PaperForm| (form.down.form_side(600), form.across.form_side(600));
    let taken = |dots| {
        let sized = rule.form(dots, landscape);
        (sized.form.name, (sized.across, sized.down))
    };
    // A4 and A3 across take A4 and A3 turned; their rotated forms have the same sides.
    assert_eq!(taken((7_016, 4_961)), ("A4", turned(A4)));
    assert_eq!(taken((9_921, 7_016)), ("A3", turned(A3)));
    // A page across within the reach of A4 rotated takes the form of its upright page.
    assert_eq!(taken((7_050, 4_900)), ("A4", turned(A4)));
    // A page across that no form reaches takes the form that holds its upright page: A4, where
    // A3 holds the page as it lies.
    assert_eq!(taken((6_300, 4_350)), ("A4", turned(A4)));
    // A page that is not wider than high takes the form that the steps give it as it lies.
    assert_eq!(form((3_300, 5_100), landscape), "STATEMENT");
}

#[test]
fn an_entry_whose_fraction_does_not_fit_gives_none() {
    // A power of two or a product beyond an `i128` gives `None`. 2^127 (exponent 104) and 2^-104 (exponent -127) lie inside the modelled
    // range, where the old shifts also gave `None`. 2^133 (exponent 110) lies above
    // the largest single-precision number, which `Binary32` does not model but `times` makes;
    // only there did the old shift give a wrong `Some`, as the mantissa shifted in 128 bits
    // is 0.
    let top = Binary32::nearest(i128::MAX, 1);
    let tiny = Binary32::nearest(1, 1 << 104);
    for (entry, exponent) in [(top, 104), (top.times(64), 110), (tiny, -127)] {
        assert_eq!(entry.exponent(), exponent);
        assert_eq!(entry_steps(entry, 4_960, WRITER), None, "{entry:?}");
    }
}