f-office-docx 0.2.2

DOCX model and Word's layout rules.
Documentation
//! The dots of a row's top lines. Each line of a row's top border takes the dot of its own top
//! as a line's top takes it there, with the twips rest of the row's top: at the body's top on
//! its page and below it, where the rest decides a top on half a dot; without a rest, at the
//! page's top the dot of `TopHalves` and lower the nearest dot.

use f_office_paint::Item;
use f_office_units::Length;

use crate::layout::testing::{NORMAL, p, pass_with, table};
use crate::numbering::Numbering;

#[test]
fn starts_a_tables_top_line_at_the_dot_of_its_top_as_a_line_top() {
    // A top margin of 1,134 twips lies exactly half a dot past a whole dot of a device of
    // 600 dpi. The table's top line, 1 pt wide, starts at the dot that the target gives a
    // line's top there, which can be the dot above the nearest dot a half down.
    let body = table(
        r#"<w:tblBorders><w:top w:val="single" w:sz="8"/></w:tblBorders>"#,
        &[2000],
        &[("", &[&p("a")])],
    ) + r#"<w:sectPr><w:pgMar w:top="1134"/></w:sectPr>"#;
    let top = Length::from_twips(1134).unwrap();
    let mut moved = false;
    for target in crate::Target::all() {
        let profile = crate::profile::Profile::of(target).unwrap();
        let device = profile.device();
        let list = pass_with(
            &body,
            (NORMAL, None),
            None,
            Numbering::default(),
            (device, profile.quirks()),
        )
        .list;
        let line = list.pages()[0]
            .items()
            .iter()
            .filter_map(|item| match item {
                Item::Rect(r) => Some(r.y),
                _ => None,
            })
            .min()
            .unwrap();
        assert_eq!(line, device.top_dot(top), "{target}");
        moved |= device.top_dot(top) != device.snap(top);
    }
    assert!(moved);
}

#[test]
fn starts_each_line_of_a_double_top_border_at_the_dot_of_its_own_top() {
    // A double border of 0.75 pt lines draws its second line 1.5 pt below its first. At a top
    // margin of 1,135 twips the first line's top lies past the middle of a dot, so the second
    // line takes the dot of its own top, not the dot of the first line's dot plus 1.5 pt,
    // which is half a dot past a whole dot.
    let body = table(
        r#"<w:tblBorders><w:top w:val="double" w:sz="6"/></w:tblBorders>"#,
        &[2000],
        &[("", &[&p("a")])],
    ) + r#"<w:sectPr><w:pgMar w:top="1135"/></w:sectPr>"#;
    let top = Length::from_twips(1135).unwrap();
    let second = top + Length::from_twips(30).unwrap();
    let mut rounded_twice = false;
    for target in crate::Target::all() {
        let profile = crate::profile::Profile::of(target).unwrap();
        let device = profile.device();
        let list = pass_with(
            &body,
            (NORMAL, None),
            None,
            Numbering::default(),
            (device, profile.quirks()),
        )
        .list;
        let mut tops: Vec<Length> = list.pages()[0]
            .items()
            .iter()
            .filter_map(|item| match item {
                Item::Rect(r) if r.width > r.height => Some(r.y),
                _ => None,
            })
            .collect();
        tops.sort();
        tops.dedup();
        assert_eq!(
            tops,
            [device.top_dot(top), device.top_dot(second)],
            "{target}"
        );
        let twice = device.snap(device.top_dot(top) + Length::from_twips(30).unwrap());
        rounded_twice |= twice != device.top_dot(second);
    }
    assert!(rounded_twice);
}

#[test]
fn starts_a_lower_rows_top_line_at_the_dot_of_its_twips_rest() {
    // Below a first row of exactly 240 twips, the second row's top lies exactly half a dot past
    // a whole dot at the top margins of 1,134 and 1,146 twips (1,374 and 1,386 twips). A target
    // that adds whole twips in whole 1/4096 pt enters 1,134 twips 0.2 of a part short and
    // 1,146 twips 0.2 of a part past, and 240 twips exactly, so the first top takes the dot above
    // and the second the dot below; a target that adds them exactly takes the nearest dot, a
    // half down, for both.
    let row = r#"<w:trHeight w:val="240" w:hRule="exact"/>"#;
    let mut rested = false;
    for (margin, short) in [(1134, true), (1146, false)] {
        let body = table(
            r#"<w:tblBorders><w:insideH w:val="single" w:sz="8"/></w:tblBorders>"#,
            &[2000],
            &[(row, &[&p("a")]), (row, &[&p("b")])],
        ) + &format!(r#"<w:sectPr><w:pgMar w:top="{margin}"/></w:sectPr>"#);
        let second = Length::from_twips(margin + 240).unwrap();
        for target in crate::Target::all() {
            let profile = crate::profile::Profile::of(target).unwrap();
            let device = profile.device();
            let list = pass_with(
                &body,
                (NORMAL, None),
                None,
                Numbering::default(),
                (device, profile.quirks()),
            )
            .list;
            let tops: Vec<Length> = list.pages()[0]
                .items()
                .iter()
                .filter_map(|item| match item {
                    Item::Rect(r) if r.width > r.height => Some(r.y),
                    _ => None,
                })
                .collect();
            let summed = device.in_twip_parts(()).is_some();
            let expected = if summed && short {
                device.snap(second) - device.dots(1)
            } else {
                device.snap(second)
            };
            assert_eq!(tops, [expected], "{target} {margin}");
            rested |= expected != device.snap(second);
        }
    }
    assert!(rested);
}

#[test]
fn stacks_the_parts_of_a_table_line_of_several_lines_in_whole_dots() {
    // `thinThickThinMediumGap` at 3 pt: thin lines and gaps of 1.5 pt, 12.5 dots of 0.12 pt,
    // and the line of 3 pt, 25 dots. A target that places such a border as one line of all its
    // parts gives each part whole dots, 12, 12, 25, 12 and 12, one after the other from the
    // first line's top.
    let body = table(
        r#"<w:tblBorders><w:top w:val="thinThickThinMediumGap" w:sz="24"/></w:tblBorders>"#,
        &[2000],
        &[("", &[&p("a")])],
    );
    let mut checked = false;
    for target in crate::Target::all() {
        let profile = crate::profile::Profile::of(target).unwrap();
        let quirks = profile.quirks();
        if quirks.compound_table_lines != crate::quirks::CompoundTableLines::WholeBorder {
            continue;
        }
        let device = profile.device();
        let list = pass_with(
            &body,
            (NORMAL, None),
            None,
            Numbering::default(),
            (device, quirks),
        )
        .list;
        let mut lines: Vec<(Length, Length)> = list.pages()[0]
            .items()
            .iter()
            .filter_map(|item| match item {
                Item::Rect(r) if r.width > r.height => Some((r.y, r.height)),
                _ => None,
            })
            .collect();
        lines.sort();
        lines.dedup();
        let first = lines[0].0;
        assert_eq!(
            lines,
            [
                (first, device.dots(12)),
                (first + device.dots(24), device.dots(25)),
                (first + device.dots(61), device.dots(12)),
            ],
            "{target}"
        );
        checked = true;
    }
    assert!(checked);
}