f-office-docx 0.1.0

DOCX model and Word's layout rules.
Documentation
//! Table rows kept with the next row. A page
//! holds 64 lines of 200 twips, and every row here has lines of one paragraph each.

use crate::layout::testing::{
    NORMAL, filler, find, find_all, kept, lay_out, marked, p, pages, table,
};

#[test]
fn moves_a_row_whose_first_cell_starts_with_a_kept_paragraph() {
    // Row 1 stands on the page's last line, and row 2 does not fit below it.
    let rows = |first: &str, second: &str| {
        let body = filler(63)
            + &table(
                "",
                &[2000, 2000],
                &[("", &[first, second]), ("", &[&p("c"), &p("d")])],
            );
        pages(&body, NORMAL, "ac")
    };
    assert_eq!(rows(&p("a"), &p("b")), [0, 1]);
    assert_eq!(rows(&kept("a"), &p("b")), [1, 1]);
    // The second cell's mark does not keep the row.
    assert_eq!(rows(&p("a"), &kept("b")), [0, 1]);
    // Nor does the mark of a later paragraph of the first cell.
    let body = filler(62)
        + &table(
            "",
            &[2000],
            &[("", &[&(p("a") + &kept("b"))]), ("", &[&p("c")])],
        );
    assert_eq!(pages(&body, NORMAL, "ac"), [0, 1]);
    // An empty first paragraph that is kept keeps the row.
    let body = filler(62)
        + &table(
            "",
            &[2000],
            &[
                ("", &[&(marked("<w:keepNext/>", "") + &p("a"))]),
                ("", &[&p("c")]),
            ],
        );
    assert_eq!(pages(&body, NORMAL, "ac"), [1, 1]);
}

#[test]
fn keeps_a_row_from_the_style_unless_its_paragraph_turns_it_off() {
    let styles = format!(
        r#"{NORMAL}<w:style w:type="paragraph" w:styleId="Kept"><w:pPr><w:keepNext/></w:pPr></w:style>"#
    );
    let rows = |first: &str| {
        let body = filler(63) + &table("", &[2000], &[("", &[first]), ("", &[&p("c")])]);
        pages(&body, &styles, "a")
    };
    assert_eq!(rows(&marked(r#"<w:pStyle w:val="Kept"/>"#, "a")), [1]);
    let off = r#"<w:pStyle w:val="Kept"/><w:keepNext w:val="0"/>"#;
    assert_eq!(rows(&marked(off, "a")), [0]);
}

#[test]
fn takes_the_first_cell_of_the_markup() {
    // In a right-to-left table, the first cell of the markup stands at the right.
    let rows = |props: &str, first: &str, second: &str| {
        let body = filler(63)
            + &table(
                props,
                &[2000, 2000],
                &[("", &[first, second]), ("", &[&p("c"), &p("d")])],
            );
        pages(&body, NORMAL, "a")
    };
    let bidi = "<w:bidiVisual/>";
    assert_eq!(rows(bidi, &kept("a"), &p("b")), [1]);
    assert_eq!(rows(bidi, &p("a"), &kept("b")), [0]);
    // A row whose first grid column holds no cell starts with the cell after it.
    let before = r#"<w:gridBefore w:val="1"/><w:wBefore w:w="2000" w:type="dxa"/>"#;
    let body = filler(63)
        + &table(
            "",
            &[2000, 2000],
            &[(before, &[&kept("a")]), ("", &[&p("c"), &p("d")])],
        );
    assert_eq!(pages(&body, NORMAL, "a"), [1]);
}

#[test]
fn stays_when_the_first_line_of_the_next_row_fits() {
    // Row 2 has three lines in its first cell; only the first fits below row 1.
    let next = |props: &str| {
        let body = filler(62)
            + &table(
                "",
                &[2000],
                &[
                    ("", &[&kept("a")]),
                    (props, &[&(p("c") + &p("e") + &p("f"))]),
                ],
            );
        pages(&body, NORMAL, "acf")
    };
    assert_eq!(next(""), [0, 0, 1]);
    // A row that cannot split must fit whole, and the kept row goes with it.
    assert_eq!(next("<w:cantSplit/>"), [1, 1, 1]);
}

#[test]
fn moves_a_kept_row_that_does_not_fit_whole() {
    // A kept row needs all its lines on the page, as a kept paragraph does, also as the
    // table's last row. A row that is not kept splits there.
    let row = |first: &str| {
        let cell = first.to_owned() + &p("e") + &p("f");
        let body = filler(62) + &table("", &[2000], &[("", &[&cell])]) + &p("y");
        pages(&body, NORMAL, "aef")
    };
    assert_eq!(row(&kept("a")), [1, 1, 1]);
    assert_eq!(row(&p("a")), [0, 0, 1]);
}

#[test]
fn moves_a_chain_of_kept_rows_with_the_kept_paragraphs_before_the_table() {
    let rows: &[(&str, &[&str])] = &[
        ("", &[&kept("a")]),
        ("", &[&kept("b")]),
        ("<w:cantSplit/>", &[&(p("c") + &p("e"))]),
    ];
    // The two kept rows go with the row that cannot split.
    let body = filler(61) + &table("", &[2000], rows);
    assert_eq!(pages(&body, NORMAL, "abc"), [1, 1, 1]);
    // A kept paragraph before the table goes with them.
    let body = filler(60) + &kept("k") + &table("", &[2000], rows);
    assert_eq!(pages(&body, NORMAL, "kabc"), [1, 1, 1, 1]);
}

#[test]
fn keeps_a_kept_paragraph_with_the_start_of_a_first_row_that_splits() {
    // A kept paragraph on line 63 and a first row, not kept, of three lines of which one
    // fits: the paragraph stays and the row splits below it, unless the row cannot split.
    let first = |props: &str| {
        let rows: &[(&str, &[&str])] = &[(props, &[&(p("a") + &p("e") + &p("f"))])];
        let body = filler(62) + &kept("k") + &table("", &[2000], rows);
        pages(&body, NORMAL, "kaf")
    };
    assert_eq!(first(""), [0, 0, 1]);
    assert_eq!(first("<w:cantSplit/>"), [1, 1, 1]);
}

/// The pages of each glyph of `header`, and the page of the first glyph of `row`.
fn header_pages(body: &str, mode: u32, (header, row): (char, char)) -> (Vec<usize>, usize) {
    let list = lay_out(body, NORMAL, Some(mode));
    let pages = find_all(&list, header)
        .iter()
        .map(|found| found.0)
        .collect();
    (pages, find(&list, row).0)
}

#[test]
fn keeps_a_header_row_with_the_row_below_it_from_mode_15() {
    // A header row on the page's last line, and a row that does not fit below it.
    let rows: &[(&str, &[&str])] = &[("<w:tblHeader/>", &[&p("h")]), ("", &[&p("a")])];
    let body = filler(63) + &table("", &[2000], rows);
    // From mode 15 the header row moves with the row and stands on the next page once.
    assert_eq!(header_pages(&body, 15, ('h', 'a')), (vec![1], 1));
    // Before mode 15 it stays at the foot and repeats above the row on the next page.
    assert_eq!(header_pages(&body, 14, ('h', 'a')), (vec![0, 1], 1));
    // A kept paragraph before the table goes with the header row from mode 15.
    let body = filler(62) + &kept("k") + &table("", &[2000], rows);
    assert_eq!(header_pages(&body, 15, ('k', 'a')), (vec![1], 1));
    assert_eq!(header_pages(&body, 14, ('k', 'a')), (vec![0], 1));
    // A table of one header row keeps the place that it had before.
    let alone: &[(&str, &[&str])] = &[("<w:tblHeader/>", &[&p("h")])];
    let body = filler(63) + &table("", &[2000], alone) + &p("y");
    assert_eq!(header_pages(&body, 15, ('h', 'y')), (vec![0], 1));
}

#[test]
fn keeps_the_last_row_with_the_paragraph_after_the_table() {
    let rows: &[(&str, &[&str])] = &[("", &[&p("a")]), ("", &[&kept("b")])];
    let body = filler(62) + &table("", &[2000], rows) + &p("y");
    assert_eq!(pages(&body, NORMAL, "aby"), [0, 1, 1]);
    // Without the mark, the paragraph alone goes to the next page.
    let rows: &[(&str, &[&str])] = &[("", &[&p("a")]), ("", &[&p("b")])];
    let body = filler(62) + &table("", &[2000], rows) + &p("y");
    assert_eq!(pages(&body, NORMAL, "aby"), [0, 0, 1]);
}

#[test]
fn starts_a_chain_longer_than_a_page_on_the_next_page() {
    // 70 kept rows and one more: the chain starts the next page and breaks at its foot,
    // where the rows in the middle of the chain do not move again.
    let mut rows: Vec<String> = vec![kept("a")];
    rows.extend((0..69).map(|_| kept("r")));
    rows.push(p("z"));
    let cells: Vec<[&str; 1]> = rows.iter().map(|row| [row.as_str()]).collect();
    let rows: Vec<(&str, &[&str])> = cells.iter().map(|cell| ("", &cell[..])).collect();
    let body = filler(20) + &table("", &[2000], &rows);
    let list = lay_out(&body, NORMAL, Some(15));
    assert_eq!(find(&list, 'a').0, 1);
    // Rows 1 to 64 fill page 2, and rows 65 to 71 start page 3.
    assert_eq!(find(&list, 'z').0, 2);
    let on_page_3 = find_all(&list, 'r')
        .iter()
        .filter(|glyph| glyph.0 == 2)
        .count();
    assert_eq!(on_page_3, 6);
}

#[test]
fn ends_a_chain_before_a_paragraph_that_starts_a_page() {
    let starts = |text: &str| marked("<w:pageBreakBefore/>", text);
    // A kept last row on the page's last line stays there.
    let rows: &[(&str, &[&str])] = &[("", &[&p("a")]), ("", &[&kept("b")])];
    let body = filler(62) + &table("", &[2000], rows) + &starts("y");
    assert_eq!(pages(&body, NORMAL, "aby"), [0, 0, 1]);
    // Also when that paragraph is kept with the paragraph after it.
    let both = marked("<w:keepNext/><w:pageBreakBefore/>", "y") + &p("z");
    let body = filler(62) + &table("", &[2000], rows) + &both;
    assert_eq!(pages(&body, NORMAL, "abyz"), [0, 0, 1, 1]);
    // A chain of kept paragraphs ends there too.
    let body = filler(63) + &kept("k") + &starts("y");
    assert_eq!(pages(&body, NORMAL, "ky"), [0, 1]);
}

/// The number of glyphs of character `ch` on each page.
fn per_page(list: &f_office_paint::DisplayList, ch: char) -> Vec<usize> {
    let mut counts = Vec::new();
    for (page, ..) in find_all(list, ch) {
        counts.resize(counts.len().max(page + 1), 0);
        if let Some(count) = counts.get_mut(page) {
            *count += 1;
        }
    }
    counts
}

#[test]
fn tests_a_chain_only_at_its_first_block() {
    // A chain of 67 kept paragraphs from the page's top fills the page and breaks at its
    // foot: the paragraphs after the first one do not test the chain again.
    let chain = kept("a") + &kept("r").repeat(65) + &p("z");
    let list = lay_out(&chain, NORMAL, Some(15));
    assert_eq!(per_page(&list, 'r'), [63, 2]);
    assert_eq!(find(&list, 'z').0, 1);
    // From the middle of a page, the chain starts the next page and fills it.
    let list = lay_out(&(filler(20) + &chain), NORMAL, Some(15));
    assert_eq!(find(&list, 'a').0, 1);
    assert_eq!(per_page(&list, 'r'), [0, 63, 2]);
    // Through a table of kept rows, the kept paragraph after the table stays on the page's
    // last line, although the paragraph after it does not fit there.
    let r = kept("r");
    let cells = [r.as_str()];
    let rows: Vec<(&str, &[&str])> = vec![("", &cells[..]); 61];
    let body = kept("a") + &kept("b") + &table("", &[2000], &rows) + &kept("y") + &p("z");
    assert_eq!(pages(&body, NORMAL, "abyz"), [0, 0, 0, 1]);
}