f-office-docx 0.2.4

DOCX model and Word's layout rules.
Documentation
//! The page break rules of paragraphs: lines below the bottom margin, widow control, page
//! breaks before, the paragraph that ends a section and chains of kept paragraphs. A page holds
//! 64 lines of 200 twips.

use crate::layout::testing::{
    NORMAL, base, filler, find, find_all, kept, lay_out, on_base_with, p, pages, pass_with,
};
use crate::numbering::Numbering;
use crate::quirks::PageBreakBeforeSpace;

#[test]
fn moves_a_paragraph_line_that_ends_below_the_bottom_margin() {
    let list = lay_out(&(filler(64) + &p("a")), NORMAL, None);
    assert_eq!(find(&list, 'a'), (1, 1440 * 1270, base(1440)));
    // A line that ends exactly at the bottom margin stays.
    let first = r#"<w:p><w:pPr><w:spacing w:before="160"/></w:pPr><w:r><w:t>q</w:t></w:r></w:p>"#;
    let list = lay_out(&(first.to_owned() + &filler(62) + &p("b")), NORMAL, None);
    assert_eq!(find(&list, 'b').0, 0);
}

#[test]
fn keeps_a_heading_with_the_lines_that_widow_control_keeps_together() {
    // 64 lines of 10 pt fit on a page. After 61 lines, the heading and two more lines fit.
    let filler = "<w:p><w:r><w:t>a</w:t></w:r></w:p>".repeat(61);
    let heading = "<w:p><w:pPr><w:keepNext/></w:pPr><w:r><w:t>H</w:t></w:r></w:p>";
    let page_of_heading = |lines: &[&str], props: &str| {
        let text = lines.join("</w:t><w:br/><w:t>");
        let body = format!(
            "{filler}{heading}<w:p><w:pPr>{props}</w:pPr><w:r><w:t>{text}</w:t></w:r></w:p>"
        );
        find(&lay_out(&body, NORMAL, None), 'H').0
    };
    // Three lines cannot split, so the heading moves with them.
    assert_eq!(page_of_heading(&["X", "Y", "Z"], ""), 1);
    // Four lines split two and two, so the heading stays.
    assert_eq!(page_of_heading(&["W", "X", "Y", "Z"], ""), 0);
    // Lines kept together move whole, and without widow control one line is enough.
    assert_eq!(page_of_heading(&["W", "X", "Y", "Z"], "<w:keepLines/>"), 1);
    let free = r#"<w:widowControl w:val="0"/>"#;
    assert_eq!(page_of_heading(&["X", "Y", "Z"], free), 0);
}

#[test]
fn splits_a_kept_paragraph_of_four_lines_at_the_foot_from_word_2013_mode() {
    // After 59 lines, the heading, the kept paragraph's four lines and no more fit.
    let heading = kept("H");
    let body = |props: &str| {
        let lines = "W</w:t><w:br/><w:t>X</w:t><w:br/><w:t>Y</w:t><w:br/><w:t>Z";
        let kept =
            format!("<w:p><w:pPr><w:keepNext/>{props}</w:pPr><w:r><w:t>{lines}</w:t></w:r></w:p>");
        filler(59) + &heading + &kept + &p("z")
    };
    // In mode 15, the last two lines go to the next page with the paragraph after them.
    assert_eq!(pages(&body(""), NORMAL, "HWXYZz"), [0, 0, 0, 1, 1, 1]);
    // Without widow control, only the last line goes.
    let free = r#"<w:widowControl w:val="0"/>"#;
    assert_eq!(pages(&body(free), NORMAL, "HWXYZz"), [0, 0, 0, 0, 1, 1]);
    // With its lines kept together, or in mode 12, the chain moves whole.
    assert_eq!(pages(&body("<w:keepLines/>"), NORMAL, "HWz"), [1, 1, 1]);
    let older = lay_out(&body(""), NORMAL, Some(12));
    assert_eq!(['H', 'W', 'z'].map(|ch| find(&older, ch).0), [1, 1, 1]);
    // In every mode, a kept paragraph whose lines run past the foot splits there.
    let lines = "W</w:t><w:br/><w:t>X</w:t><w:br/><w:t>Y</w:t><w:br/><w:t>Z";
    let past = filler(60)
        + &heading
        + &format!("<w:p><w:pPr><w:keepNext/></w:pPr><w:r><w:t>{lines}</w:t></w:r></w:p>")
        + &p("z");
    let older = lay_out(&past, NORMAL, Some(12));
    assert_eq!(
        ['H', 'W', 'X', 'Y', 'Z'].map(|ch| find(&older, ch).0),
        [0, 0, 0, 1, 1]
    );
}

#[test]
fn keeps_the_space_before_a_page_break_before_in_word_2010_mode() {
    let body = r#"<w:p><w:r><w:t>A</w:t></w:r></w:p><w:p><w:pPr><w:pageBreakBefore/><w:spacing w:before="240"/></w:pPr><w:r><w:t>B</w:t></w:r></w:p>"#;
    let top = |mode: u32| {
        let list = lay_out(body, NORMAL, Some(mode));
        let b = find(&list, 'B');
        assert_eq!(b.0, 1);
        b.2
    };
    // 12 pt is 50 device dots of 0.24 pt.
    assert_eq!(top(14) - top(15), 50);
}

#[test]
fn keeps_only_the_space_before_above_the_space_after_at_a_page_break_before_in_word_2010_mode() {
    let top = |after: u32, mode: u32, variant: PageBreakBeforeSpace| {
        let body = format!(
            r#"<w:p><w:pPr><w:spacing w:after="{after}"/></w:pPr><w:r><w:t>A</w:t></w:r></w:p><w:p><w:pPr><w:pageBreakBefore/><w:spacing w:before="240"/></w:pPr><w:r><w:t>B</w:t></w:r></w:p>"#
        );
        let target = on_base_with(|p| p.with_variant(variant));
        let list = pass_with(
            &body,
            (NORMAL, Some(mode)),
            None,
            Numbering::default(),
            target,
        )
        .list;
        let b = find(&list, 'B');
        assert_eq!(b.0, 1);
        b.2
    };
    let (all, above) = (
        PageBreakBeforeSpace::KeepsSpaceBefore,
        PageBreakBeforeSpace::AboveSpaceAfter,
    );
    // Of 12 pt before, a space after of 6 pt leaves 6 pt, 25 device dots of 0.24 pt, and the
    // other variant keeps all 12 pt.
    assert_eq!(top(120, 14, above) - top(120, 15, above), 25);
    assert_eq!(top(120, 14, all) - top(120, 15, all), 50);
    // A space after of 15 pt leaves none.
    assert_eq!(top(300, 14, above), top(300, 15, above));
}

#[test]
fn a_page_break_in_a_paragraph_without_text_drops_the_space_before_above_its_space_after() {
    use crate::quirks::{DroppedSpaceBox, SingleBaseline};
    // The page break sits in a paragraph without text with the space after `after`, or at the
    // end of the text of a paragraph without space after when `after` is `None`. The paragraph
    // `B` after it has the space before `before`.
    let baseline = |after: Option<u32>, before: u32, variant: DroppedSpaceBox| {
        let page_break = r#"<w:r><w:br w:type="page"/></w:r>"#;
        let breaks = match after {
            Some(after) => format!(
                r#"<w:p><w:r><w:t>A</w:t></w:r></w:p><w:p><w:pPr><w:spacing w:after="{after}"/></w:pPr>{page_break}</w:p>"#
            ),
            None => format!(r"<w:p><w:r><w:t>A</w:t></w:r>{page_break}</w:p>"),
        };
        let body = format!(
            r#"{breaks}<w:p><w:pPr><w:spacing w:before="{before}"/></w:pPr><w:r><w:t>B</w:t></w:r></w:p>"#
        );
        let target = on_base_with(|p| {
            p.with_variant(SingleBaseline::RoundedDescentBelowSpace)
                .with_variant(variant)
        });
        let list = pass_with(
            &body,
            (NORMAL, Some(15)),
            None,
            Numbering::default(),
            target,
        )
        .list;
        let b = find(&list, 'B');
        assert_eq!(b.0, 1);
        b.2
    };
    let (held, left) = (DroppedSpaceBox::HeldAbove, DroppedSpaceBox::LeftOut);
    // 280 twips before, 200 after: 80 twips (16.67 dots) are dropped. The box of the line of
    // 41.67 dots holds them above the body's top of 300 dots: 58.33 dots round down to 58 below
    // its top at 283.33, dot 283, and the baseline lies the rounded descent of 8 dots above
    // its bottom, at 333. Without them, the line's height rounds up, and the baseline lies the
    // rounded descent above its exact bottom at 341.67: 333.67, dot 334.
    assert_eq!(baseline(Some(200), 280, held), 333);
    assert_eq!(baseline(Some(200), 280, left), 334);
    assert_eq!(baseline(Some(0), 80, held), 333);
    assert_eq!(baseline(Some(200), 0, held), 334);
    // A space before no larger than the space after drops nothing.
    assert_eq!(baseline(Some(200), 200, held), 334);
    // A page break at the end of a paragraph with text leaves the space out.
    assert_eq!(baseline(None, 80, held), 334);
}

#[test]
fn keeps_no_room_for_the_empty_paragraph_that_ends_a_section() {
    // After 61 lines, the three kept lines fill the page. The empty paragraph that ends the
    // section stays on the page anyway, so the kept lines need no room for it.
    let filler = "<w:p><w:r><w:t>a</w:t></w:r></w:p>".repeat(61);
    let kept = "<w:p><w:pPr><w:keepNext/></w:pPr><w:r><w:t>K</w:t></w:r></w:p>".repeat(3);
    let body = format!(
        "{filler}{kept}<w:p><w:pPr><w:sectPr/></w:pPr></w:p><w:p><w:r><w:t>N</w:t></w:r></w:p><w:sectPr/>"
    );
    let pages: Vec<usize> = find_all(&lay_out(&body, NORMAL, None), 'K')
        .iter()
        .map(|found| found.0)
        .collect();
    assert_eq!(pages, [0, 0, 0]);
}

#[test]
fn ends_a_chain_at_a_page_break_in_a_kept_paragraph() {
    // A paragraph with `marks` that holds `text` and then a page break.
    let breaks = |marks: &str, text: &str| {
        format!(
            r#"<w:p><w:pPr>{marks}</w:pPr><w:r><w:t>{text}</w:t><w:br w:type="page"/></w:r></w:p>"#
        )
    };
    let after = kept("x") + &kept("y") + &p("z");
    // A kept paragraph of only a page break on the page's 63rd line stays there, and the
    // chain after it starts the next page, which it does not leave empty.
    let body = filler(62) + &breaks("<w:keepNext/>", "") + &after;
    assert_eq!(pages(&body, NORMAL, "xz"), [1, 1]);
    let body = filler(62) + &breaks("<w:keepNext/><w:keepLines/>", "") + &after;
    assert_eq!(pages(&body, NORMAL, "xz"), [1, 1]);
    // The same with text before the break.
    let body = filler(62) + &breaks("<w:keepNext/>", "k") + &after;
    assert_eq!(pages(&body, NORMAL, "kxz"), [0, 1, 1]);
    // A kept paragraph before it needs room for itself and the break's line only.
    let body = filler(61) + &kept("k") + &breaks("<w:keepNext/>", "") + &after;
    assert_eq!(pages(&body, NORMAL, "kxz"), [0, 1, 1]);
}