f-office-docx 0.2.2

DOCX model and Word's layout rules.
Documentation
//! The clips of table cells (quirk `CellClip`).

use f_office_paint::{DisplayList, Group, Item, PathCommand};

use crate::grid::DOT;
use crate::layout::testing::{SINGLE, find_all, on_base_with, p, pass_with, rects, table};
use crate::numbering::Numbering;
use crate::quirks::{CellClip, TableLines};

/// A table of one exact row of 20 pt whose one cell holds three lines of text, which cross the
/// row's bottom, with lines on every side, then a paragraph after the table, on a device that
/// draws table lines as `lines` says.
fn cut_cell(clip: CellClip, lines: TableLines) -> DisplayList {
    let borders = format!(
        "<w:tblBorders><w:top {SINGLE}/><w:left {SINGLE}/><w:bottom {SINGLE}/><w:right {SINGLE}/></w:tblBorders>"
    );
    let cell = p("a") + &p("b") + &p("c");
    let row = r#"<w:trHeight w:val="400" w:hRule="exact"/>"#;
    let body = table(&borders, &[2880], &[(row, &[&cell])]) + &p("d");
    let (device, quirks) = on_base_with(|p| p.with_variant(clip).with_variant(lines));
    pass_with(
        &body,
        ("", None),
        None,
        Numbering::default(),
        (device, quirks),
    )
    .list
}

/// The groups of the first page that have a clip.
fn clipped_groups(list: &DisplayList) -> Vec<&Group> {
    list.pages()[0]
        .items()
        .iter()
        .filter_map(|item| match item {
            Item::Group(group) if group.clip.is_some() => Some(group),
            _ => None,
        })
        .collect()
}

/// The box around the points of a group's clip, in device dots: left, top, right and bottom.
fn clip_box(group: &Group) -> (i64, i64, i64, i64) {
    let points: Vec<_> = group
        .clip
        .iter()
        .flat_map(|clip| &clip.commands)
        .filter_map(|command| match command {
            PathCommand::MoveTo(p) | PathCommand::LineTo(p) => Some(p),
            _ => None,
        })
        .collect();
    let dots = |v: f_office_units::Length| v.units() / DOT;
    (
        points.iter().map(|p| dots(p.x)).min().unwrap(),
        points.iter().map(|p| dots(p.y)).min().unwrap(),
        points.iter().map(|p| dots(p.x)).max().unwrap(),
        points.iter().map(|p| dots(p.y)).max().unwrap(),
    )
}

#[test]
fn a_target_that_cuts_a_cell_groups_its_content_under_a_clip_on_the_inner_edges_of_its_lines() {
    for lines in [TableLines::RoundsStarts, TableLines::RoundsCenters] {
        cut_on_the_inner_edges(&cut_cell(CellClip::InsideLines, lines));
    }
}

fn cut_on_the_inner_edges(list: &DisplayList) {
    let groups = clipped_groups(list);
    assert_eq!(groups.len(), 1);
    let (left, top, right, bottom) = clip_box(groups[0]);
    // The lines of the table: the top and the bottom line run across, the left and the right
    // line down.
    let lines = rects(list, 0);
    let across: Vec<_> = lines.iter().filter(|r| r.2 > r.3).collect();
    let down: Vec<_> = lines.iter().filter(|r| r.3 > r.2).collect();
    let (top_line, bottom_line) = (
        across.iter().min_by_key(|r| r.1),
        across.iter().max_by_key(|r| r.1),
    );
    let (left_line, right_line) = (
        down.iter().min_by_key(|r| r.0),
        down.iter().max_by_key(|r| r.0),
    );
    assert_eq!(top, top_line.map(|r| r.1 + r.3).unwrap());
    assert_eq!(bottom, bottom_line.map(|r| r.1).unwrap());
    assert_eq!(left, left_line.map(|r| r.0 + r.2).unwrap());
    assert_eq!(right, right_line.map(|r| r.0).unwrap());
    // The third line of the cell lies below the row and its clip; the paragraph after the
    // table lies in no group.
    let (_, _, c) = find_all(list, 'c')[0];
    assert!(c > bottom);
    assert!(list.pages()[0].items().iter().any(|item| matches!(
        item,
        Item::Glyphs(run) if run.text.contains('d')
    )));
}

#[test]
fn a_target_that_does_not_cut_a_cell_draws_its_content_without_a_clip() {
    let cut = cut_cell(CellClip::InsideLines, TableLines::RoundsStarts);
    let uncut = cut_cell(CellClip::Unclipped, TableLines::RoundsStarts);
    assert_eq!(clipped_groups(&uncut), Vec::<&Group>::new());
    // The clip moves nothing.
    for ch in ['a', 'b', 'c', 'd'] {
        assert_eq!(find_all(&cut, ch), find_all(&uncut, ch));
    }
}