f-office-docx 0.3.0

DOCX model and Word's layout rules.
Documentation
//! The room of a row's top border. Rows hold one
//! line of 10 pt (200 twips) of the fake font. Word draws the line between two rows from the
//! upper row's bottom down, and the lower cell's text starts below that line or below the cell's
//! own top line and its top margin, whichever is lower.

use crate::layout::testing::{NORMAL, at, base, du, lay_out, p, rects, table};

/// A cell holding `text` with the border lines `lines` (`w:tcBorders`).
fn cell(lines: &str, text: &str) -> String {
    format!(
        "<w:tcPr><w:tcBorders>{lines}</w:tcBorders></w:tcPr>{}",
        p(text)
    )
}

/// A single line on `edge` of `eighths` eighths of a point.
fn line(edge: &str, eighths: u32) -> String {
    format!(r#"<w:{edge} w:val="single" w:sz="{eighths}"/>"#)
}

/// The table's border lines (`w:tblBorders`).
fn lines(lines: &str) -> String {
    format!("<w:tblBorders>{lines}</w:tblBorders>")
}

/// The table's top and bottom cell margins, in twips (`w:tblCellMar`).
fn margins(top: u32, bottom: u32) -> String {
    format!(
        r#"<w:tblCellMar><w:top w:w="{top}" w:type="dxa"/><w:bottom w:w="{bottom}" w:type="dxa"/></w:tblCellMar>"#
    )
}

#[test]
fn gives_the_wider_line_its_room_in_cells_without_a_top_margin() {
    // The table's lines between rows are 3 pt, and the second row's cell sets its own top line
    // of 0.5 pt. Word draws the 3 pt line there, and without a top margin the text starts below
    // it.
    let props = lines(&line("insideH", 24));
    let thin = cell(&line("top", 4), "c");
    let rows: &[(&str, &[&str])] = &[("", &[&p("a")]), ("", &[&thin]), ("", &[&p("e")])];
    let list = lay_out(&table(&props, &[2000], rows), NORMAL, None);
    assert_eq!(at(&list, 'c').1, base(1640 + 60));
    assert_eq!(at(&list, 'e').1, base(1640 + 60 + 200 + 60));
    let drawn = rects(&list, 0);
    assert!(
        drawn
            .iter()
            .any(|&(_, y, _, h)| y == du(1640) && h == du(60)),
        "{drawn:?}"
    );
}

#[test]
fn gives_a_row_its_own_line_where_its_top_margin_holds_the_wider_line() {
    // With a top margin of 3 pt, the 3 pt line lies in the second row's top margin, and the row
    // takes the room of its own 0.5 pt line. The third row's own line is the 3 pt line.
    let props = lines(&line("insideH", 24)) + &margins(60, 0);
    let thin = cell(&line("top", 4), "c");
    let rows: &[(&str, &[&str])] = &[("", &[&p("a")]), ("", &[&thin]), ("", &[&p("e")])];
    let list = lay_out(&table(&props, &[2000], rows), NORMAL, None);
    assert_eq!(at(&list, 'a').1, base(1440 + 60));
    assert_eq!(at(&list, 'c').1, base(1700 + 10 + 60));
    assert_eq!(at(&list, 'e').1, base(1970 + 60 + 60));
    let drawn = rects(&list, 0);
    assert!(
        drawn
            .iter()
            .any(|&(_, y, _, h)| y == du(1700) && h == du(60)),
        "{drawn:?}"
    );
}

#[test]
fn takes_the_part_of_the_line_below_the_top_margin() {
    // The first row's cell sets a 3 pt bottom line, and the second row has no line of its own.
    // With a top margin of 1.5 pt, the line takes 1.5 pt of room.
    let wide = cell(&line("bottom", 24), "a");
    let rows: &[(&str, &[&str])] = &[("", &[&wide]), ("", &[&p("c")])];
    let list = lay_out(&table(&margins(30, 0), &[2000], rows), NORMAL, None);
    assert_eq!(at(&list, 'c').1, base(1670 + 30 + 30));
}

#[test]
fn gives_a_bottom_line_its_room_beside_a_bottom_margin() {
    // The cells set a 3 pt bottom line and a bottom margin of 3 pt. The margin takes none of the
    // line, between the rows and below the table.
    let rows: &[(&str, &[&str])] = &[
        ("", &[&cell(&line("bottom", 24), "a")]),
        ("", &[&cell(&line("bottom", 24), "c")]),
    ];
    let body = table(&margins(0, 60), &[2000], rows) + &p("e");
    let list = lay_out(&body, NORMAL, None);
    assert_eq!(at(&list, 'c').1, base(1700 + 60));
    assert_eq!(at(&list, 'e').1, base(1760 + 200 + 60 + 60));
}

#[test]
fn takes_the_largest_room_of_the_cells_of_a_row() {
    // With a top margin of 3 pt, the second row's first cell takes the room of its own 0.5 pt
    // line, and its second cell the room of the table's 3 pt line, its own. The row takes 3 pt.
    let props = lines(&line("insideH", 24)) + &margins(60, 0);
    let thin = cell(&line("top", 4), "c");
    let rows: &[(&str, &[&str])] = &[("", &[&p("a"), &p("b")]), ("", &[&thin, &p("d")])];
    let list = lay_out(&table(&props, &[2000, 2000], rows), NORMAL, None);
    assert_eq!(at(&list, 'c').1, base(1700 + 60 + 60));
}

#[test]
fn takes_the_room_of_each_line_above_a_cell_below_several_cells() {
    // The second row's cell spans both grid columns. Of the two cells above it, the first has no
    // bottom line and the second a 3 pt bottom line. A target whose variant gives the cell the
    // room of every line above it starts its text below that line; the variant that takes the
    // line of the cell above its first grid column gives it none.
    use crate::layout::testing::{on_base_with, pass_with};
    use crate::numbering::Numbering;
    use crate::quirks::RowLineRoom;
    let wide = cell(&line("bottom", 24), "b");
    let spanned = format!(r#"<w:tcPr><w:gridSpan w:val="2"/></w:tcPr>{}"#, p("c"));
    let rows: &[(&str, &[&str])] = &[("", &[&p("a"), &wide]), ("", &[&spanned])];
    let body = table("", &[1000, 1000], rows);
    for (room, extra) in [(RowLineRoom::FirstColumn, 0), (RowLineRoom::CellsAbove, 60)] {
        let target = on_base_with(|p| p.with_variant(room));
        let pass = pass_with(&body, (NORMAL, None), None, Numbering::default(), target);
        assert_eq!(at(&pass.list, 'c').1, base(1640 + extra), "{room:?}");
    }
}

#[test]
fn draws_the_bottom_line_of_each_cell_above_along_its_width_on_a_target_that_does() {
    // The second row's cell spans both grid columns of 1000 twips. Of the two cells above it,
    // the first has no bottom line and the second a 3 pt bottom line. A target whose cells above
    // each draw their own bottom line draws that line over the second grid column only; the
    // other variant takes the line of the cell above the first grid column, which has none.
    use crate::layout::testing::{on_base_with, pass_with};
    use crate::numbering::Numbering;
    use crate::quirks::RowLineRoom;
    let wide = cell(&line("bottom", 24), "b");
    let spanned = format!(r#"<w:tcPr><w:gridSpan w:val="2"/></w:tcPr>{}"#, p("c"));
    let rows: &[(&str, &[&str])] = &[("", &[&p("a"), &wide]), ("", &[&spanned])];
    let body = table("", &[1000, 1000], rows);
    let across = |room: RowLineRoom| {
        let target = on_base_with(|p| p.with_variant(room));
        let pass = pass_with(&body, (NORMAL, None), None, Numbering::default(), target);
        rects(&pass.list, 0)
            .into_iter()
            .filter(|r| r.2 > 4 * r.3)
            .collect::<Vec<_>>()
    };
    assert_eq!(
        across(RowLineRoom::FirstColumn),
        Vec::<(i64, i64, i64, i64)>::new()
    );
    let parts = across(RowLineRoom::CellsAbove);
    assert_eq!(parts.len(), 1, "{parts:?}");
    assert!((parts[0].2 - du(1000)).abs() <= 1, "{parts:?}");
    assert!(parts[0].0 >= du(1440 + 800), "{parts:?}");
}

#[test]
fn takes_the_room_of_the_undrawn_line_above_a_cell_that_goes_on_with_a_merge() {
    // The first cell of row 1 starts a merged cell with a 3 pt bottom line, and row 2's first
    // cell goes on with it; the other cells have no lines. No line is drawn between the two
    // cells of the merged cell. A target whose variant gives the cell that goes on the room of
    // that line all the same starts row 2's text below it; the other variant gives it none.
    use crate::layout::testing::{on_base_with, pass_with};
    use crate::numbering::Numbering;
    use crate::quirks::MergedTopRoom;
    let start = format!(
        r#"<w:tcPr><w:vMerge w:val="restart"/><w:tcBorders>{}</w:tcBorders></w:tcPr>{}"#,
        line("bottom", 24),
        p("a")
    );
    let on = "<w:tcPr><w:vMerge/></w:tcPr><w:p/>";
    let rows: &[(&str, &[&str])] = &[("", &[&start, &p("b")]), ("", &[on, &p("c")])];
    let body = table("", &[1000, 1000], rows);
    for (room, extra) in [(MergedTopRoom::NoRoom, 0), (MergedTopRoom::UndrawnLine, 60)] {
        let target = on_base_with(|p| p.with_variant(room));
        let pass = pass_with(&body, (NORMAL, None), None, Numbering::default(), target);
        assert_eq!(at(&pass.list, 'c').1, base(1640 + extra), "{room:?}");
        assert_eq!(at(&pass.list, 'a').1, base(1440), "{room:?}");
    }
}

#[test]
fn overlaps_unbordered_cell_margins_with_a_neighbor_cells_border() {
    // Cell 0 has a 6 pt (120 twips) top and bottom border and 0 margin; cell 1 has no borders
    // and 3 pt (60 twips) top and bottom margins. Cell 1's margins overlap cell 0's borders, so
    // the row adds 0 extra margin above or below the 6 pt borders.
    let bordered = cell(&(line("top", 48) + &line("bottom", 48)), "a");
    let padded = format!(
        r#"<w:tcPr><w:tcMar><w:top w:w="60" w:type="dxa"/><w:bottom w:w="60" w:type="dxa"/></w:tcMar></w:tcPr>{}"#,
        p("b")
    );
    let rows: &[(&str, &[&str])] = &[("", &[&bordered, &padded]), ("", &[&bordered, &padded])];
    let body = table("", &[1000, 1000], rows) + &p("e");
    let list = lay_out(&body, NORMAL, None);
    assert_eq!(at(&list, 'a').1, base(1440 + 120));
    assert_eq!(at(&list, 'b').1, base(1440 + 120));
    assert_eq!(at(&list, 'e').1, base(1440 + 120 + 200 + 120 + 200 + 120));
    // When the unbordered cell's margin (200 twips = 10 pt) exceeds the neighbor's 0.5 pt
    // (10 twips) border + 2 pt (40 twips) margin, the effective top and bottom margins are
    // 200 - 10 = 190 twips each (row top + 200, row height = 10 + 190 + 200 + 190 = 590 twips).
    let thin = format!(
        r#"<w:tcPr><w:tcBorders>{}</w:tcBorders><w:tcMar><w:top w:w="40" w:type="dxa"/><w:bottom w:w="40" w:type="dxa"/></w:tcMar></w:tcPr>{}"#,
        line("top", 4) + &line("bottom", 4),
        p("c")
    );
    let tall = format!(
        r#"<w:tcPr><w:tcMar><w:top w:w="200" w:type="dxa"/><w:bottom w:w="200" w:type="dxa"/></w:tcMar></w:tcPr>{}"#,
        p("d")
    );
    let body2 = table("", &[1000, 1000], &[("", &[&thin, &tall])]) + &p("f");
    let list2 = lay_out(&body2, NORMAL, None);
    assert_eq!(at(&list2, 'c').1, base(1440 + 200));
    assert_eq!(at(&list2, 'd').1, base(1440 + 200));
    assert_eq!(at(&list2, 'f').1, base(1440 + 200 + 200 + 190 + 10));
}

#[test]
fn pairs_row_bottom_margins_with_table_outer_bottom_border_not_inside_h() {
    // Table sets only insideH = 6 pt (120 twips) and no outer bottom border. On row 0, c[1]
    // overrides bottom="nil" and sets bottom margin = 3 pt (60 twips). Because Word falls back
    // to tblBorders.bottom (absent) rather than insideH when pairing row bottom margins, row 0
    // keeps the full 3 pt bottom margin before row 1's 6 pt insideH border.
    let pad_bot = format!(
        r#"<w:tcPr><w:tcBorders><w:bottom w:val="nil"/></w:tcBorders><w:tcMar><w:bottom w:w="60" w:type="dxa"/></w:tcMar></w:tcPr>{}"#,
        p("b")
    );
    let nil_top = format!(
        r#"<w:tcPr><w:tcBorders><w:top w:val="nil"/></w:tcBorders></w:tcPr>{}"#,
        p("d")
    );
    let rows: &[(&str, &[&str])] = &[("", &[&p("a"), &pad_bot]), ("", &[&p("c"), &nil_top])];
    let list_inside_only = lay_out(
        &table(&lines(&line("insideH", 48)), &[1000, 1000], rows),
        NORMAL,
        None,
    );
    assert_eq!(at(&list_inside_only, 'c').1, base(1440 + 200 + 60 + 120));

    // When tblBorders also sets outer bottom = 6 pt (120 twips), row 0 pairs c[0]'s fallback
    // bottom border (6 pt) against c[1]'s 3 pt bottom margin, giving 0 extra bottom margin even
    // when insideH is only 0.5 pt (10 twips).
    let props_outer = lines(&(line("insideH", 4) + &line("bottom", 48)));
    let list_with_outer = lay_out(&table(&props_outer, &[1000, 1000], rows), NORMAL, None);
    assert_eq!(at(&list_with_outer, 'c').1, base(1440 + 200 + 10));
}

#[test]
fn includes_bottom_border_space_below_the_last_row() {
    // A bottom border with w:space="6" (120 twips) adds both width and space below the last row
    // (both for a drawn border and for val="none" with w:space).
    let spaced_line = r#"<w:bottom w:val="single" w:sz="4" w:space="6"/>"#;
    let body = table("", &[2000], &[("", &[&cell(spaced_line, "a")])]) + &p("e");
    let list = lay_out(&body, NORMAL, None);
    assert_eq!(at(&list, 'e').1, base(1440 + 200 + 10 + 120));

    let spaced_none = r#"<w:bottom w:val="none" w:space="6"/>"#;
    let body_none = table("", &[2000], &[("", &[&cell(spaced_none, "a")])]) + &p("e");
    let list_none = lay_out(&body_none, NORMAL, None);
    assert_eq!(at(&list_none, 'e').1, base(1440 + 200 + 120));
}