f-office-docx 0.2.2

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:?}");
    }
}