f-office-docx 0.1.0

DOCX model and Word's layout rules.
Documentation
//! Endnotes after the text: a page that they go on to starts with their continuation
//! separator. A page holds 64 lines of 200 twips from the top margin of 1440 twips.

use f_office_paint::DisplayList;

use crate::layout::testing::{Fake, base, find_all, p, rects};
use crate::testing::{WordParts, docx};

/// The endnotes part: a separator, a continuation separator with 300 twips after it, and
/// endnote 1, whose 80 paragraphs "e" go on to the next page.
fn endnotes() -> String {
    let separator = r#"<w:endnote w:type="separator" w:id="-1"><w:p><w:r><w:separator/></w:r></w:p></w:endnote>"#;
    let continuation = r#"<w:endnote w:type="continuationSeparator" w:id="0"><w:p><w:pPr><w:spacing w:after="300"/></w:pPr><w:r><w:continuationSeparator/></w:r></w:p></w:endnote>"#;
    let note = format!(r#"<w:endnote w:id="1">{}</w:endnote>"#, p("e").repeat(80));
    format!("{separator}{continuation}{note}")
}

/// Lays out a paragraph that references endnote 1, with the endnotes of [`endnotes`], in the
/// compatibility mode `mode` or in none.
fn laid_out(mode: Option<u32>) -> DisplayList {
    let body = r#"<w:p><w:r><w:t>a</w:t></w:r><w:r><w:endnoteReference w:id="1"/></w:r></w:p>"#;
    let notes = endnotes();
    let mut builder =
        docx(body)
            .plain_styles()
            .word_part("t0", "endnotes", "endnotes.xml", "endnotes", &notes);
    if let Some(mode) = mode {
        let settings = format!(
            r#"<w:compat><w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="{mode}"/></w:compat>"#
        );
        builder = builder.word_part("t1", "settings", "settings.xml", "settings", &settings);
    }
    let bytes = builder.zip().unwrap();
    let package = f_office_opc::Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
    crate::layout_with_fonts(
        &package,
        (&Fake, &crate::ArtPictures::new()),
        &crate::Options::base(),
    )
    .unwrap()
    .value
    .list
}

#[test]
fn starts_a_page_of_endnotes_below_their_continuation_separator() {
    let list = laid_out(None);
    let on_second: Vec<i64> = find_all(&list, 'e')
        .into_iter()
        .filter(|glyph| glyph.0 == 1)
        .map(|glyph| glyph.2)
        .collect();
    // The separator's line of 200 twips and the 300 twips after it come first.
    assert_eq!(on_second.first(), Some(&base(1440 + 200 + 300)));
}

#[test]
fn draws_the_rule_above_endnotes_that_go_on_as_wide_as_the_column_in_every_mode() {
    let widths = |mode| -> Vec<i64> { rects(&laid_out(mode), 1).iter().map(|r| r.2).collect() };
    let column = widths(None);
    assert_eq!(column.len(), 1);
    // A rule of 2 inches, as the rule above the rest of a footnote in mode 15, is 600 of
    // these dots; the column is wider.
    assert!(column[0] > 600, "{column:?}");
    assert_eq!(widths(Some(15)), column);
}