f-office-docx 0.3.0

DOCX model and Word's layout rules.
Documentation
use f_office_opc::Package;

use crate::body::References;
use crate::layout::Stories;
use crate::styles::{Styles, Theme};
use crate::testing::{self, WordParts as _};
use crate::{Options, StoryParts, Warnings, quirks_and_device};

/// The reference to the header part of [`package`] as the default header of a section.
pub(crate) const HEADER_REFERENCE: &str = r#"<w:headerReference w:type="default" r:id="h1"/>"#;

/// Packs a DOCX file with a body, a header part that [`HEADER_REFERENCE`] names, and the
/// properties of the last section. Its styles part has empty document defaults, so that the
/// text keeps the defaults of ECMA-376 and not those of Word's template.
pub(crate) fn package(body: &str, header: &str, last: &str) -> Vec<u8> {
    testing::docx(&format!("{body}<w:sectPr>{last}</w:sectPr>"))
        .word_part("h1", "header", "h1.xml", "hdr", header)
        .plain_styles()
        .zip()
        .unwrap()
}

#[test]
fn reads_a_header_part_once_for_the_sections_that_share_it() {
    let bytes = package("", "<w:p><w:r><w:t>h</w:t></w:r></w:p>", HEADER_REFERENCE);
    let package = Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
    let relationships = &package
        .relationships(&package.main_document().unwrap())
        .unwrap()
        .relationships;
    let (_, _, reader, _) = quirks_and_device(&Options::default().profile().unwrap()).unwrap();
    let styles = Styles::default();
    let theme = Theme::default();
    let image_loader = crate::image_loader::PackageImageLoader::new(&package);
    let mut stories = StoryParts::new(
        &package,
        &image_loader,
        relationships,
        (reader, &styles, &theme, crate::props::SmallFractions::Off),
        &crate::body::TEST_PARAGRAPH_IDS,
    );
    let refs = References {
        default: Some("h1".to_owned()),
        ..References::default()
    };
    let sections: Vec<Stories> = (0..3)
        .map(|_| stories.stories(&refs, &mut Warnings::new()).unwrap())
        .collect();
    // The blocks of one read sit at one place in memory, with the name of their part.
    let blocks = |s: &Stories| s.default.as_ref().map(|story| story.blocks.as_ptr());
    assert!(blocks(&sections[0]).is_some());
    assert!(sections.iter().all(|s| blocks(s) == blocks(&sections[0])));
    let part = |s: &Stories| s.default.as_ref().map(|story| story.part.to_string());
    assert!(
        sections
            .iter()
            .all(|s| part(s).as_deref() == Some("word/h1.xml"))
    );
}

#[test]
fn a_picture_takes_the_conversion_of_inks_that_the_target_names() {
    use crate::quirks::FourComponentInks;
    use f_office_paint::InkConversion;

    let bytes = testing::docx("")
        .relationship(testing::DOCUMENT, "p1", "image", "media/p.jpeg")
        .part("word/media/p.jpeg", vec![0xFF, 0xD8, 0xFF])
        .zip()
        .unwrap();
    let package = Package::open(&bytes, &f_office_opc::Limits::default()).unwrap();
    let main = package.main_document().unwrap();
    let relationships = &package.relationships(&main).unwrap().relationships;
    let mut seen = Vec::new();
    for target in crate::Target::all() {
        let options = Options {
            configuration: crate::Configuration::of(target),
            ..Options::default()
        };
        let (quirks, _, reader, _) = quirks_and_device(&options.profile().unwrap()).unwrap();
        let mut warnings = Warnings::new();
        let inks = reader.ink_conversion();
        let image_loader = crate::image_loader::PackageImageLoader::new(&package);
        let image = crate::images(
            &package,
            &image_loader,
            (&main, relationships),
            inks,
            &mut warnings,
        )("p1")
        .unwrap();
        let expected = match quirks.four_component_inks {
            FourComponentInks::ThroughEmbeddedProfile => InkConversion::EmbeddedProfile,
            FourComponentInks::ThroughInkTable => InkConversion::InkTable,
        };
        assert_eq!(image.inks, expected, "{target:?}");
        seen.push(image.inks);
    }
    // Some target converts through the ink table, and some through the embedded profile.
    assert!(seen.contains(&InkConversion::InkTable));
    assert!(seen.contains(&InkConversion::EmbeddedProfile));
}