f-office 0.3.0

Renders Office Open XML documents the way Microsoft Office does.
Documentation
//! Layout tests of frames at the foot of a page: the chain of paragraphs that are kept with the
//! next one, and the page of a frame and of its text. The body of the test page holds 10 lines
//! of exact 12 pt.

use super::{CARLITO_RUN, SECTION, compatibility_mode, docx, placed, render};

/// A paragraph with lines of exact 12 pt and the paragraph properties `more`, whose lines are
/// `names`, split by line breaks.
fn exact(names: &[&str], more: &str) -> String {
    let runs: Vec<String> = names
        .iter()
        .map(|name| format!("<w:r>{CARLITO_RUN}<w:t>{name}</w:t></w:r>"))
        .collect();
    let runs = runs.join(&format!("<w:r>{CARLITO_RUN}<w:br/></w:r>"));
    format!(r#"<w:p><w:pPr>{more}<w:spacing w:line="240" w:lineRule="exact"/></w:pPr>{runs}</w:p>"#)
}

/// A paragraph "Q" in a frame with the attributes `frame`, `lines` lines of 12 pt tall, which
/// is kept with the next paragraph.
fn framed(frame: &str, lines: usize) -> String {
    let height = 240 * lines;
    format!(
        r#"<w:p><w:pPr><w:keepNext/><w:framePr {frame} w:wrap="around" w:vAnchor="text" w:hAnchor="text"/><w:spacing w:line="{height}" w:lineRule="exact"/></w:pPr><w:r>{CARLITO_RUN}<w:t>Q</w:t></w:r></w:p>"#
    )
}

/// A drop cap of `lines` lines.
fn drop_cap(lines: usize) -> String {
    framed(&format!(r#"w:dropCap="drop" w:lines="{lines}""#), lines)
}

/// The page (from 1) and the start in points of each text of the document that holds `filler`
/// lines and then `blocks`, in the compatibility mode `mode`.
fn pages_of(filler: usize, blocks: &str, mode: u32) -> Vec<(String, usize, f64)> {
    let lines: String = (0..filler).map(|_| exact(&["f"], "")).collect();
    let body = format!("{lines}{blocks}<w:sectPr>{SECTION}</w:sectPr>");
    let list = render(&docx(&body, &[], &compatibility_mode(mode)));
    placed(&list)
        .iter()
        .enumerate()
        .flat_map(|(page, runs)| {
            runs.iter()
                .map(move |(x, _, text)| (text.trim().to_owned(), page + 1, *x))
        })
        .collect()
}

/// The page of the text `text`.
fn page_of(places: &[(String, usize, f64)], text: &str) -> usize {
    places.iter().find(|place| place.0 == text).unwrap().1
}

/// A heading that is kept with the next paragraph.
fn heading() -> String {
    exact(&["Head"], "<w:keepNext/>")
}

#[test]
fn keeps_a_heading_with_the_text_after_a_drop_cap() {
    let blocks = heading() + &drop_cap(2) + &exact(&["b", "t", "l"], "");
    // The heading and the three lines fit in room for 4 lines: the drop cap needs none.
    let fits = pages_of(6, &blocks, 15);
    assert_eq!(page_of(&fits, "Head"), 1, "{fits:?}");
    assert_eq!(page_of(&fits, "l"), 1, "{fits:?}");
    // In room for 3 lines, all of them go to the next page.
    let moves = pages_of(7, &blocks, 15);
    assert_eq!(page_of(&moves, "Head"), 2, "{moves:?}");
    assert_eq!(page_of(&moves, "Q"), 2, "{moves:?}");
}

#[test]
fn takes_a_drop_cap_to_the_next_page_with_its_text() {
    let blocks = drop_cap(2) + &exact(&["b", "t", "l"], "");
    // Three lines cannot split, and there is room for 2.
    let modern = pages_of(8, &blocks, 15);
    assert_eq!(page_of(&modern, "b"), 2, "{modern:?}");
    assert_eq!(page_of(&modern, "Q"), 2, "{modern:?}");
    // Before Word 2013, the drop cap stays where it fits.
    let old = pages_of(8, &blocks, 14);
    assert_eq!(page_of(&old, "b"), 2, "{old:?}");
    assert_eq!(page_of(&old, "Q"), 1, "{old:?}");
}

#[test]
fn moves_a_frame_that_does_not_fit_and_its_text_with_it() {
    let text = exact(&["b", "t", "u", "v", "w", "l"], "");
    // A drop cap of 3 lines has room for 2, and two lines of the text would fit.
    for mode in [15, 14] {
        let places = pages_of(8, &(drop_cap(3) + &text), mode);
        assert_eq!(page_of(&places, "Q"), 2, "{places:?}");
        assert_eq!(page_of(&places, "b"), 2, "{places:?}");
    }
    // A drop cap of 2 lines fits, and the text splits.
    let fits = pages_of(8, &(drop_cap(2) + &text), 15);
    assert_eq!(page_of(&fits, "Q"), 1, "{fits:?}");
    assert_eq!(page_of(&fits, "b"), 1, "{fits:?}");
    assert_eq!(page_of(&fits, "l"), 2, "{fits:?}");
}

#[test]
fn ends_a_chain_of_kept_paragraphs_at_a_frame_that_is_no_drop_cap() {
    let text = exact(&["b", "t", "l"], "");
    // The heading and the frame fit in room for 3 lines, and the text goes on alone.
    let plain = heading() + &framed(r#"w:w="600""#, 2) + &text;
    for mode in [15, 14] {
        let places = pages_of(7, &plain, mode);
        assert_eq!(page_of(&places, "Head"), 1, "{places:?}");
        assert_eq!(page_of(&places, "Q"), 1, "{places:?}");
        assert_eq!(page_of(&places, "b"), 2, "{places:?}");
    }
    // Before Word 2013, a drop cap ends the chain too.
    let old = pages_of(7, &(heading() + &drop_cap(2) + &text), 14);
    assert_eq!(page_of(&old, "Head"), 1, "{old:?}");
    assert_eq!(page_of(&old, "b"), 2, "{old:?}");
    // The heading goes with a frame that has no room below it.
    let tall = pages_of(8, &plain, 15);
    assert_eq!(page_of(&tall, "Head"), 2, "{tall:?}");
}

#[test]
fn gives_lines_their_whole_width_on_the_page_after_a_frame() {
    let text = exact(&["b", "t", "u", "v", "w", "l"], "");
    let places = pages_of(8, &(drop_cap(2) + &text), 15);
    let start = |text: &str| places.iter().find(|place| place.0 == text).unwrap().2;
    // The first line stands beside the drop cap, and the lines of page 2 at the margin.
    assert!(start("b") > 36.5, "{places:?}");
    assert_eq!(page_of(&places, "u"), 2, "{places:?}");
    assert!((start("u") - 36.0).abs() < 0.01, "{places:?}");
    assert!((start("l") - 36.0).abs() < 0.01, "{places:?}");
}

#[test]
fn keeps_a_frame_with_a_distance_down_on_its_page() {
    // The frame stands 60 pt below its place, past the body's end, and stays on page 1.
    let blocks = framed(r#"w:w="600" w:y="1200""#, 1) + &exact(&["b"], "");
    for mode in [15, 14] {
        let places = pages_of(8, &blocks, mode);
        assert_eq!(page_of(&places, "Q"), 1, "{places:?}");
        assert_eq!(page_of(&places, "b"), 1, "{places:?}");
    }
}

/// A paragraph `text` at the right margin in a frame without a width, with the paragraph
/// properties `more`, whose lines are `lines`.
fn at_right(lines: &[&str], more: &str) -> String {
    let frame =
        r#"<w:framePr w:wrap="none" w:vAnchor="text" w:hAnchor="margin" w:xAlign="right"/>"#;
    exact(lines, &format!("{frame}{more}"))
}

/// The start and the baseline in points of the text `text` on the first page of `body`.
fn place_of(body: &str, text: &str) -> (f64, f64) {
    let body = format!("{body}<w:sectPr>{SECTION}</w:sectPr>");
    let list = render(&docx(&body, &[], &compatibility_mode(15)));
    let runs = &placed(&list)[0];
    let run = runs.iter().find(|run| run.2.trim() == text).unwrap();
    (run.0, run.1)
}

#[test]
fn starts_a_frame_below_the_space_after_the_paragraph_before() {
    let lead = format!(
        r#"<w:p><w:pPr><w:spacing w:before="0" w:after="480" w:line="240" w:lineRule="exact"/></w:pPr><w:r>{CARLITO_RUN}<w:t>a</w:t></w:r></w:p>"#
    );
    let body = format!("{lead}{}{}", at_right(&["Q"], ""), exact(&["b"], ""));
    // The frame and the paragraph after it start 24 pt below the line of the first one.
    let (a, q, b) = (
        place_of(&body, "a").1,
        place_of(&body, "Q").1,
        place_of(&body, "b").1,
    );
    assert!((q - b).abs() < 0.01, "{q} {b}");
    assert!((q - a - 36.0).abs() < 0.01, "{q} {a}");
}

#[test]
fn counts_the_first_line_indent_in_a_frame_without_a_width() {
    let plain = place_of(&at_right(&["Q"], ""), "Q").0;
    let indented = place_of(&at_right(&["Q"], r#"<w:ind w:firstLine="432"/>"#), "Q").0;
    // The text ends at the right margin with and without the indent.
    assert!(plain > 200.0, "{plain}");
    assert!((plain - indented).abs() < 0.01, "{plain} {indented}");
}

#[test]
fn makes_a_frame_of_two_lines_as_wide_as_its_area() {
    // One line ends at the right margin; two lines start at the left margin, 36 pt.
    let one = place_of(&at_right(&["Q"], ""), "Q").0;
    let two = place_of(&at_right(&["Q", "R"], ""), "Q").0;
    assert!(one > 200.0, "{one}");
    assert!((two - 36.0).abs() < 0.01, "{two}");
}

/// How far in points the paragraph "b" stands below the paragraph "a", with a paragraph "Q"
/// between them whose `w:framePr` has `attributes`.
fn step_past(attributes: &str) -> f64 {
    let marked = exact(&["Q"], &format!("<w:framePr {attributes}/>"));
    let body = exact(&["a"], "") + &marked + &exact(&["b"], "");
    place_of(&body, "b").1 - place_of(&body, "a").1
}

#[test]
fn keeps_a_paragraph_in_the_flow_when_its_frame_mark_names_no_place() {
    // The paragraph takes its line of 12 pt, so "b" stands two lines below "a".
    for plain in [
        "",
        r#"w:wrap="auto""#,
        r#"w:hAnchor="text" w:xAlign="left" w:hSpace="141""#,
        r#"w:vAnchor="page""#,
        r#"w:vAnchor="margin" w:w="0" w:y="0""#,
        r#"w:dropCap="drop""#,
    ] {
        assert!((step_past(plain) - 24.0).abs() < 0.01, "{plain}");
    }
    // A frame takes no line.
    for frame in [
        r#"w:vAnchor="text""#,
        r#"w:wrap="none""#,
        r#"w:hAnchor="margin""#,
        r#"w:x="200""#,
    ] {
        assert!((step_past(frame) - 12.0).abs() < 0.01, "{frame}");
    }
}