f-office-docx 0.1.0

DOCX model and Word's layout rules.
Documentation
//! The own outline of a table with cell spacing where the table goes on to another page
//! (`SpacedTableBreaks`).

use f_office_paint::DisplayList;

use crate::layout::testing::{
    NORMAL, du, filler, find, find_all, marked, on_base_with, p, pass_with, rects, table,
};
use crate::numbering::Numbering;
use crate::quirks::SpacedTableBreaks::{self, AroundPagePart, AroundTable};

/// The table's lines: a top line of 30 twips (12 eighths), a bottom line of 20 twips, side
/// lines of 15 twips and inside lines of 10 twips, with a cell spacing of 40 twips.
const SPACED: &str = r#"<w:tblCellSpacing w:w="40" w:type="dxa"/><w:tblBorders><w:top w:val="single" w:sz="12"/><w:left w:val="single" w:sz="6"/><w:bottom w:val="single" w:sz="8"/><w:right w:val="single" w:sz="6"/><w:insideH w:val="single" w:sz="4"/><w:insideV w:val="single" w:sz="4"/></w:tblBorders>"#;

/// Lays out `lead` lines, the last with a space of `after` twips after it, and a table of
/// [`SPACED`] with a first row of `a` and `b`, a second row with the row properties `second`
/// whose cells hold `c` and `lines` lines of `d`, and a third row of `e` and `f`, under
/// `breaks`.
fn spaced_table(
    (lead, after): (usize, i64),
    (second, lines): (&str, usize),
    breaks: SpacedTableBreaks,
) -> DisplayList {
    let space = format!(r#"<w:spacing w:after="{after}"/>"#);
    let body = filler(lead - 1) + &marked(&space, "q");
    let long: String = (0..lines).map(|_| p("d")).collect();
    let rows: &[(&str, &[&str])] = &[
        ("", &[&p("a"), &p("b")]),
        (second, &[&p("c"), &long]),
        ("", &[&p("e"), &p("f")]),
    ];
    let body = body + &table(SPACED, &[2000, 4000], rows) + &p("z");
    let target = on_base_with(|profile| profile.with_variant(breaks));
    pass_with(&body, (NORMAL, None), None, Numbering::default(), target).list
}

/// The rectangles of a page that run across the table, from its left line to its right one.
fn across(list: &DisplayList, page: usize) -> Vec<(i64, i64, i64, i64)> {
    rects(list, page)
        .into_iter()
        .filter(|rect| rect.2 > du(5500))
        .collect()
}

#[test]
fn opens_the_part_at_a_page_top_with_the_table_s_top_line_around_each_page_part() {
    // The second row cannot split and moves whole to the next page. Around each page's part,
    // that part starts with the table's top line of 30 twips at the page's top, and its boxes
    // take the inside line of 10 twips on top where around the whole table they take the
    // table's top line: its text lies 10 twips lower.
    let whole = |breaks| spaced_table((58, 0), ("<w:cantSplit/>", 10), breaks);
    let (page, table) = (whole(AroundPagePart), whole(AroundTable));
    assert_eq!((find(&page, 'c').0, find(&table, 'c').0), (1, 1));
    assert!((find(&page, 'c').2 - find(&table, 'c').2 - du(10)).abs() <= 1);
    let tops = |list: &DisplayList| across(list, 1).iter().filter(|r| r.1 == du(1440)).count();
    assert_eq!((tops(&page), tops(&table)), (1, 0));
    // On the first page, the part ends with the table's bottom line below the first row.
    assert_eq!(across(&page, 0).len(), across(&table, 0).len() + 1);
}

#[test]
fn ends_the_part_of_a_split_row_with_its_boxes_and_the_spacing_around_each_page_part() {
    // The second row splits. On the next page, its part starts below the table's top line
    // with boxes under an inside line, 10 twips lower than around the whole table, and ends
    // with its boxes' bottom lines and the spacing, as a whole row does, so the third row lies
    // 10, 10 and 40 twips lower than around the whole table.
    let split = |breaks| spaced_table((58, 0), ("", 20), breaks);
    let (page, table) = (split(AroundPagePart), split(AroundTable));
    assert_eq!((find(&page, 'e').0, find(&table, 'e').0), (1, 1));
    assert!((find(&page, 'e').2 - find(&table, 'e').2 - du(60)).abs() <= 1);
    // Each page's part closes or opens with a line of the table across it.
    assert_eq!(across(&page, 0).len(), across(&table, 0).len() + 1);
    assert_eq!(across(&page, 1).len(), across(&table, 1).len() + 1);
}

/// The glyphs of `ch` on the first page.
fn on_first_page(list: &DisplayList, ch: char) -> usize {
    find_all(list, ch).iter().filter(|g| g.0 == 0).count()
}

#[test]
fn keeps_a_line_of_a_split_row_only_with_room_for_its_box_and_the_closing_line() {
    // A line of the split row stays on the first page only where its box's bottom line, the
    // spacing and the table's bottom line fit below it, around each page's part, and the
    // spacing and the table's line alone around the whole table: around each page's part no
    // place keeps more lines, and some keep fewer.
    let counts: Vec<(usize, usize)> = (0..=200)
        .step_by(2)
        .map(|after| {
            let lines = |breaks| on_first_page(&spaced_table((58, after), ("", 20), breaks), 'd');
            (lines(AroundPagePart), lines(AroundTable))
        })
        .collect();
    assert!(
        counts.iter().all(|(page, table)| page <= table),
        "{counts:?}"
    );
    assert!(
        counts.iter().any(|(page, table)| page < table),
        "{counts:?}"
    );
}

#[test]
fn keeps_a_whole_row_at_the_foot_with_room_for_the_closing_line_alone() {
    // A whole row that ends a page's part needs room for the table's bottom line below its
    // boxes and spacing around each page's part, and for one more spacing around the whole
    // table: around each page's part no place moves more rows to the next page, and some
    // move fewer.
    let counts: Vec<(usize, usize)> = (0..=200)
        .step_by(2)
        .map(|after| {
            let rows = |breaks| {
                let list = spaced_table((61, after), ("<w:cantSplit/>", 1), breaks);
                on_first_page(&list, 'c') + on_first_page(&list, 'e')
            };
            (rows(AroundPagePart), rows(AroundTable))
        })
        .collect();
    assert!(
        counts.iter().all(|(page, table)| page >= table),
        "{counts:?}"
    );
    assert!(
        counts.iter().any(|(page, table)| page > table),
        "{counts:?}"
    );
}