f-office-docx 0.2.1

DOCX model and Word's layout rules.
Documentation
//! Border minima tested through parsing, line breaking and placement with fixed fake widths.

use crate::layout::testing::{du, find_all, on_base_with, p, pass_with, table, x_du};
use crate::numbering::Numbering;
use crate::quirks::CellTextRoom;
use std::collections::BTreeSet;

#[test]
fn cell_room_uses_the_larger_sum_instead_of_clamping_each_side() {
    for (margin, border_left, border_right, grid, expected) in
        [(0, 24, 24, 1420, [1, 2]), (20, 8, 24, 1442, [1, 1])]
    {
        let props = format!(
            r#"<w:tblLayout w:type="fixed"/><w:tblCellMar><w:left w:w="{margin}" w:type="dxa"/><w:right w:w="{margin}" w:type="dxa"/></w:tblCellMar><w:tblBorders><w:left w:val="single" w:sz="{border_left}"/><w:right w:val="single" w:sz="{border_right}"/></w:tblBorders>"#
        );
        // The fake text is 70 pt wide. In the asymmetric case the combined minimum leaves
        // 70.1 pt, while separate minima would leave only 69.6 pt and wrap the last word.
        let text = p("nnnn nnnn nnnn");
        let body = table(&props, &[grid], &[("", &[&text])]);
        for (room, lines) in [CellTextRoom::Margins, CellTextRoom::CombinedBorderMinimum]
            .into_iter()
            .zip(expected)
        {
            let target = on_base_with(|p| p.with_variant(room));
            let pass = pass_with(&body, ("", Some(15)), None, Numbering::default(), target);
            let glyphs = find_all(&pass.list, 'n');
            assert_eq!(glyphs.len(), 12);
            let baselines: BTreeSet<_> = glyphs.iter().map(|g| (g.0, g.2)).collect();
            assert_eq!(baselines.len(), lines);
        }
    }
}

#[test]
fn legacy_explicit_indent_fixes_the_text_start_after_its_border_minimum() {
    use crate::quirks::TableIndent;
    for (margin, unindented, modern, old) in [(0, 1470, 1500, 1470), (80, 1520, 1550, 1440)] {
        for (indent, old_text, new_text) in [
            (None, old, unindented),
            (Some(0), old, 1440),
            (Some(120), old + 120, 1560),
        ] {
            let ind = indent.map_or_else(String::new, |value| {
                format!(r#"<w:tblInd w:w="{value}" w:type="dxa"/>"#)
            });
            let props = format!(
                r#"{ind}<w:tblLayout w:type="fixed"/><w:tblCellMar><w:left w:w="{margin}" w:type="dxa"/><w:right w:w="0" w:type="dxa"/></w:tblCellMar><w:tblBorders><w:left w:val="single" w:sz="24"/></w:tblBorders>"#
            );
            let text = p("n");
            let body = table(&props, &[4000], &[("", &[&text])]);
            for (rule, expected) in [
                (TableIndent::FirstMargin, old_text),
                (TableIndent::ExplicitTextStart, new_text),
            ] {
                let target = on_base_with(|p| {
                    p.with_variant(CellTextRoom::CombinedBorderMinimum)
                        .with_variant(rule)
                });
                for (mode, twips) in [(14, expected), (15, modern + indent.unwrap_or(0))] {
                    let pass =
                        pass_with(&body, ("", Some(mode)), None, Numbering::default(), target);
                    let x = find_all(&pass.list, 'n')[0].1;
                    assert_eq!(x_du(x), du(twips));
                }
            }
        }
    }
}

#[test]
fn a_side_margin_that_nothing_sets_takes_the_default_where_the_target_gives_one() {
    use crate::quirks::{TableIndent, UnsetCellMargins};
    // A cell of 4,000 twips whose table sets side lines of `size` eighths of a point and no
    // margin, in mode 14 and without an indent, so that its grid lines lie at 1,440 and 5,440
    // twips. Without a margin, the text starts after the whole left line and ends at the right
    // grid line. With the default of 10 twips on each side, the text starts at the larger of the
    // margin and half the left line, and the lines leave the larger of the two margins and half
    // the two lines: (start, end) of the lines in twips after 1,440, before the device grid.
    for (size, after_line, default_sides) in
        [(2, (5, 4000), (10, 3990)), (16, (40, 4000), (20, 3980))]
    {
        let props = format!(
            r#"<w:tblLayout w:type="fixed"/><w:tblBorders><w:left w:val="single" w:sz="{size}"/><w:right w:val="single" w:sz="{size}"/></w:tblBorders>"#
        );
        let right = r#"<w:p><w:pPr><w:jc w:val="right"/></w:pPr><w:r><w:t>m</w:t></w:r></w:p>"#;
        let cell = p("n") + right;
        let body = table(&props, &[4000], &[("", &[&cell])]);
        for (rule, (start, end)) in [
            (UnsetCellMargins::AfterLeftLine, after_line),
            (UnsetCellMargins::DefaultSides, default_sides),
        ] {
            let target = on_base_with(|p| {
                p.with_variant(CellTextRoom::CombinedBorderMinimum)
                    .with_variant(TableIndent::ExplicitTextStart)
                    .with_variant(rule)
            });
            let pass = pass_with(&body, ("", Some(14)), None, Numbering::default(), target);
            let left = find_all(&pass.list, 'n')[0].1;
            let right = find_all(&pass.list, 'm')[0].1;
            assert_eq!(x_du(left), du(1440 + start), "{rule:?} at {size}");
            // The start lies on the device grid and the lines keep their width from there, so the
            // right-aligned glyph of 100 twips lies the lines' width less its own right of it.
            let lines = (end - start - 100) * f_office_units::Length::UNITS_PER_TWIP;
            assert_eq!(right - left, lines, "{rule:?} at {size}");
        }
    }
}