f-office-docx 0.2.2

DOCX model and Word's layout rules.
Documentation
//! The place across the page of a table that floats at an offset (quirk `FloatingTableLeft`).

use f_office_units::Length;

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

/// The left edge of the column of the first cell line of a table that floats with the place
/// properties `place`, on a device whose floating tables take `rule`.
fn first_cell_left(place: &str, rule: FloatingTableLeft) -> Option<Length> {
    let properties = format!(
        r#"<w:tblpPr w:vertAnchor="page" w:tblpY="2000" {place}/><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.column_left)
}

#[test]
fn moves_a_table_placed_at_an_offset_one_twip_left() {
    let twip = Length::from_units(Length::UNITS_PER_TWIP);
    for anchor in ["page", "margin", "text"] {
        let place = format!(r#"w:horzAnchor="{anchor}" w:tblpX="2003""#);
        let at = first_cell_left(&place, FloatingTableLeft::AtOffset);
        let left = first_cell_left(&place, FloatingTableLeft::TwipLeft);
        assert_eq!(
            at.zip(left).map(|(at, left)| at - left),
            Some(twip),
            "{anchor}"
        );
    }
}

#[test]
fn keeps_a_table_aligned_across_or_without_an_offset_at_its_place() {
    for place in [
        r#"w:horzAnchor="page" w:tblpXSpec="center""#,
        r#"w:horzAnchor="margin""#,
    ] {
        let at = first_cell_left(place, FloatingTableLeft::AtOffset);
        assert!(at.is_some(), "{place}");
        assert_eq!(
            at,
            first_cell_left(place, FloatingTableLeft::TwipLeft),
            "{place}"
        );
    }
}

/// The left edge, in dots, of the left line of a table `width` twips wide that floats with the
/// place properties `place`, on a device whose table edges on half a dot take `halves`.
fn left_line(place: &str, width: i64, halves: crate::quirks::TableEdgeHalves) -> Option<i64> {
    use crate::layout::testing::{rects, target_at};
    use crate::quirks::TableLines;
    let properties = format!(
        r#"<w:tblpPr w:vertAnchor="page" w:tblpY="2000" {place}/><w:tblW w:w="{width}" w:type="dxa"/><w:tblBorders><w:left w:val="single" w:sz="6"/></w:tblBorders>"#
    );
    let body = table(&properties, &[width], &[("", &[&p("a")])]) + &p("z");
    // Dots of 600 per inch, on which 6 twips past a multiple of 12 lie on half a dot.
    let target = target_at(600, |p| {
        p.with_variant(halves)
            .with_variant(FloatingTableLeft::TwipLeft)
            .with_variant(TableLines::RoundsCenters)
    });
    let pass = pass_with(&body, ("", None), None, Numbering::default(), target);
    rects(&pass.list, 0)
        .into_iter()
        .filter(|&(_, _, w, h)| h > w)
        .map(|(x, ..)| x)
        .min()
}

#[test]
fn takes_the_fixed_point_dot_for_a_table_at_an_offset_on_half_a_dot() {
    use crate::quirks::TableEdgeHalves;
    // An offset of 2,191 twips puts the place one twip left, at 2,190 twips: 912.5 dots, whose
    // fixed-point points lie on the half and take the dot above; 2,178 twips (907.5 dots) lie
    // below the half and keep the dot below.
    for (offset, above) in [(2191, 1), (2179, 0)] {
        let place = format!(r#"w:horzAnchor="page" w:tblpX="{offset}""#);
        let below = left_line(&place, 3000, TableEdgeHalves::BelowOrExact);
        let fixed = left_line(&place, 3000, TableEdgeHalves::FixedPoint);
        assert_eq!(
            fixed.zip(below).map(|(fixed, below)| fixed - below),
            Some(above),
            "{offset}"
        );
    }
}

#[test]
fn keeps_the_dot_below_for_a_centered_table_on_half_a_dot() {
    use crate::quirks::TableEdgeHalves;
    let place = r#"w:horzAnchor="page" w:tblpXSpec="center""#;
    // Widths a twip apart, which move the place by half a twip each, through 24 twips of
    // places: two of them lie on exactly half a dot, at least one on or above the half in
    // fixed point, where an offset table takes the dot above.
    for width in 5000..5048 {
        let below = left_line(place, width, TableEdgeHalves::BelowOrExact);
        assert!(below.is_some());
        assert_eq!(
            below,
            left_line(place, width, TableEdgeHalves::FixedPoint),
            "{width}"
        );
    }
}