f-office-docx 0.2.2

DOCX model and Word's layout rules.
Documentation
//! The place down the page of a table that floats from the page or the margin (quirk
//! `FloatingTableTop`).

use f_office_units::Length;

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

/// The top of the first cell line of a table that floats 2,000 twips below `anchor`, on a
/// device whose floating tables take `rule`.
fn first_cell_top(anchor: &str, rule: FloatingTableTop) -> Option<Length> {
    let properties = format!(
        r#"<w:tblpPr w:vertAnchor="{anchor}" w:horzAnchor="margin" w:tblpY="2000"/><w:tblW w:w="3000" w:type="dxa"/>"#
    );
    let body = table(&properties, &[3000], &[("", &[&p("a")])]) + &p("z");
    let target = on_base_with(|p| p.with_variant(rule));
    let pass = pass_with(&body, ("", None), None, Numbering::default(), target);
    pass.lines
        .iter()
        .find(|line| line.cell.is_some())
        .map(|line| line.top)
}

#[test]
fn raises_a_table_that_floats_from_the_page_or_the_margin_by_one_twip() {
    let twip = Length::from_units(Length::UNITS_PER_TWIP);
    for anchor in ["page", "margin"] {
        let at = first_cell_top(anchor, FloatingTableTop::AtOffset);
        let higher = first_cell_top(anchor, FloatingTableTop::TwipHigher);
        assert_eq!(
            at.zip(higher).map(|(at, higher)| at - higher),
            Some(twip),
            "{anchor}"
        );
    }
}

#[test]
fn keeps_a_table_that_floats_from_the_text_at_its_offset() {
    let at = first_cell_top("text", FloatingTableTop::AtOffset);
    assert!(at.is_some());
    assert_eq!(at, first_cell_top("text", FloatingTableTop::TwipHigher));
}