f-office-docx 0.1.0

DOCX model and Word's layout rules.
Documentation
//! The dashes of a table's lines of a dash style (quirk `BorderDashes`).

use crate::layout::testing::{on_base_with, p, pass_with, rects, table};
use crate::numbering::Numbering;
use crate::quirks::BorderDashes;

/// The filled rectangles of the first page, in dots, of a table of one row of two cells with
/// lines of `style`, 1 pt wide, on its six sides, on a device that draws dashes as `dashes`
/// says.
fn lines(style: &str, dashes: BorderDashes) -> Vec<(i64, i64, i64, i64)> {
    let side = |name: &str| format!(r#"<w:{name} w:val="{style}" w:sz="8"/>"#);
    let borders = format!(
        "<w:tblBorders>{}</w:tblBorders>",
        ["top", "left", "bottom", "right", "insideH", "insideV"]
            .map(side)
            .concat()
    );
    let body = table(&borders, &[2880, 2880], &[("", &[&p("a"), &p("b")])]) + &p("c");
    let target = on_base_with(|p| p.with_variant(dashes));
    let list = pass_with(&body, ("", None), None, Numbering::default(), target).list;
    rects(&list, 0)
}

#[test]
fn draws_a_dash_style_solid_where_the_target_draws_no_dashes() {
    assert_eq!(
        lines("dotted", BorderDashes::DrawsSolid),
        lines("single", BorderDashes::DrawsSolid)
    );
}

#[test]
fn draws_a_solid_style_alike_on_both_variants() {
    assert_eq!(
        lines("single", BorderDashes::DashesCellPieces),
        lines("single", BorderDashes::DrawsSolid)
    );
}

/// A rectangle in dots: left, top, width and height.
type Box = (i64, i64, i64, i64);

/// Whether `inner` lies inside `outer`.
fn inside((x, y, w, h): Box, (ox, oy, ow, oh): Box) -> bool {
    x >= ox && y >= oy && x + w <= ox + ow && y + h <= oy + oh
}

/// The width across of the table's lines, the narrowest side of its solid rectangles.
fn thickness(solid: &[Box]) -> i64 {
    solid.iter().map(|&(_, _, w, h)| w.min(h)).min().unwrap()
}

#[test]
fn draws_each_dot_inside_a_solid_piece_and_no_longer_than_the_line_is_wide() {
    let solid = lines("single", BorderDashes::DrawsSolid);
    let dotted = lines("dotted", BorderDashes::DashesCellPieces);
    assert!(dotted.len() > solid.len() * 10);
    let thick = thickness(&solid);
    assert!(dotted.iter().all(|&(_, _, w, h)| w <= thick && h <= thick));
    assert!(
        dotted
            .iter()
            .all(|&dot| solid.iter().any(|&piece| inside(dot, piece)))
    );
}

#[test]
fn starts_each_piece_of_the_top_line_with_a_dot_at_its_left_end() {
    let solid = lines("single", BorderDashes::DrawsSolid);
    let dotted = lines("dotted", BorderDashes::DashesCellPieces);
    let thick = thickness(&solid);
    let top = solid.iter().map(|&(_, y, _, _)| y).min().unwrap();
    let mut on_top: Vec<Box> = dotted
        .into_iter()
        .filter(|&(_, y, _, _)| y == top)
        .collect();
    on_top.sort_unstable();
    // The pieces between the junctions, without the squares.
    let starts: Vec<i64> = solid
        .iter()
        .filter(|&&(_, y, w, h)| y == top && w > h)
        .map(|&(x, ..)| x)
        .collect();
    assert_eq!(starts.len(), 2);
    for left in starts {
        let first = on_top.iter().position(|&(x, ..)| x == left).unwrap();
        // The dot is one width long, and the next one starts two widths further right.
        assert_eq!(on_top[first].2, thick);
        assert_eq!(on_top[first + 1].0, left + 2 * thick);
    }
}

/// The limits that the warnings of `pass` name, with their counts.
fn limits(pass: &crate::layout::Pass) -> Vec<(crate::warnings::Limit, u32)> {
    pass.warnings
        .iter()
        .filter_map(|(warning, count)| match warning {
            crate::Warning::LimitReached { limit, .. } => Some((*limit, count)),
            _ => None,
        })
        .collect()
}

#[test]
fn draws_a_dashed_line_whole_where_its_dashes_would_pass_the_limit_of_its_page() {
    use crate::params::MAX_DASHED_PAGE_ITEMS;
    use crate::warnings::Limit;
    // A grid column of 50,000,000 twips, about 2.5 million points: each of its dotted lines,
    // 1 pt wide, would give more than a million dots.
    let side = |name: &str| format!(r#"<w:{name} w:val="dotted" w:sz="8"/>"#);
    let borders = format!(
        "<w:tblBorders>{}{}</w:tblBorders>",
        side("top"),
        side("bottom")
    );
    let body = table(&borders, &[50_000_000], &[("", &[&p("a")])]) + &p("c");
    let target = on_base_with(|p| p.with_variant(BorderDashes::DashesCellPieces));
    let pass = pass_with(&body, ("", None), None, Numbering::default(), target);
    assert!(rects(&pass.list, 0).len() <= MAX_DASHED_PAGE_ITEMS);
    let reached = limits(&pass);
    assert!(!reached.is_empty(), "{reached:?}");
    assert!(
        reached
            .iter()
            .all(|(limit, _)| *limit == Limit::TableDashes)
    );
    // The same table of an ordinary width keeps its dashes, without the warning.
    let body = table(&borders, &[2880], &[("", &[&p("a")])]) + &p("c");
    let target = on_base_with(|p| p.with_variant(BorderDashes::DashesCellPieces));
    let pass = pass_with(&body, ("", None), None, Numbering::default(), target);
    assert_eq!(limits(&pass), Vec::new());
}

/// The dots of the top line of a table of one row of two cells, 2,880 and 2,900 twips wide,
/// with dotted lines 1 pt wide above and below its cells and none down at its sides or between
/// them, on a device that draws dashes as `dashes` says, in dots from the left, with the top
/// line's width.
fn top_dots_without_lines_down(dashes: BorderDashes) -> (Vec<i64>, i64) {
    let side = |name: &str, style: &str| format!(r#"<w:{name} w:val="{style}" w:sz="8"/>"#);
    let borders = format!(
        "<w:tblBorders>{}{}{}{}{}</w:tblBorders>",
        side("top", "dotted"),
        side("bottom", "dotted"),
        side("left", "none"),
        side("right", "none"),
        side("insideV", "none")
    );
    let body = table(&borders, &[2880, 2900], &[("", &[&p("a"), &p("b")])]) + &p("c");
    let target = on_base_with(|p| p.with_variant(dashes));
    let list = pass_with(&body, ("", None), None, Numbering::default(), target).list;
    let lines = rects(&list, 0);
    let top = lines.iter().map(|&(_, y, _, _)| y).min().unwrap();
    let mut on_top: Vec<Box> = lines.into_iter().filter(|&(_, y, _, _)| y == top).collect();
    on_top.sort_unstable();
    let thick = on_top.iter().map(|&(_, _, _, h)| h).min().unwrap();
    (on_top.into_iter().map(|(x, ..)| x).collect(), thick)
}

#[test]
fn starts_the_dots_again_in_each_cell_where_no_line_down_divides_the_line() {
    let (dots, thick) = top_dots_without_lines_down(BorderDashes::DashesCellPieces);
    // The dots run two widths apart in each cell, and the line breaks once, at the pass from
    // the first cell to the second: there the step is another, and the second cell's dots
    // start a whole room of one width after the first cell's line ends.
    let breaks: Vec<usize> = (1..dots.len())
        .filter(|&i| dots[i] - dots[i - 1] != 2 * thick)
        .collect();
    assert_eq!(breaks.len(), 1, "{dots:?}");
    assert!(breaks[0] > 1 && breaks[0] < dots.len() - 1, "{dots:?}");
}