f-office-docx 0.2.4

DOCX model and Word's layout rules.
Documentation
use std::rc::Rc;

use f_office_units::{Edges, Length};

use super::{DrawingMut, visit_drawings};
use crate::body::{Block, Content, InlineDrawing, Paragraph, Run};
use crate::floating::Floating;
use crate::picture::Picture;
use crate::table::{Cell, Row, Table};

/// A picture whose relationship ID is `label`, which the test reads back as the drawing's name.
fn picture(label: &str) -> Picture {
    Picture::new(
        label,
        Edges::sized(Length::ZERO, Length::ZERO),
        Edges::default(),
    )
}

fn object(label: &str) -> Content {
    Content::Object {
        width: Length::ZERO,
        height: Length::ZERO,
        picture: Some(Box::new(picture(label))),
        extent: Some(Length::ZERO),
    }
}

/// A floating drawing named `label`, with a text box of `blocks` and the group parts `parts`.
fn floating(label: &str, blocks: Vec<Block>, parts: Vec<Floating>) -> Floating {
    Floating {
        picture: Some(picture(label)),
        blocks,
        parts: parts
            .into_iter()
            .map(|part| (Length::ZERO, Length::ZERO, part))
            .collect(),
        ..Floating::default()
    }
}

fn paragraph(content: Vec<Content>) -> Block {
    Block::Paragraph(Paragraph {
        runs: vec![Run {
            content,
            ..Run::default()
        }],
        ..Paragraph::empty()
    })
}

fn inline(shape: Floating) -> Content {
    Content::Drawing(Rc::new(InlineDrawing::of_shape(shape)))
}

#[test]
fn visits_each_drawing_after_the_drawing_that_holds_it_in_the_order_of_the_document() {
    // A text box with an inline picture, and a group whose part has a text box and a group of
    // its own; then a table cell with an inline drawing whose shape has a text box.
    let group = floating(
        "d",
        vec![paragraph(vec![object("e")])],
        vec![floating("f", Vec::new(), Vec::new())],
    );
    let cell = Cell {
        blocks: vec![paragraph(vec![inline(floating(
            "g",
            vec![paragraph(vec![object("h")])],
            Vec::new(),
        ))])],
        ..Cell::default()
    };
    let mut blocks = vec![
        paragraph(vec![
            object("a"),
            Content::Floating(Box::new(floating(
                "b",
                vec![paragraph(vec![object("c")])],
                vec![group],
            ))),
        ]),
        Block::Table(Table {
            rows: vec![Row {
                cells: vec![cell],
                ..Row::default()
            }],
            ..Table::default()
        }),
    ];
    let mut visited = Vec::new();
    visit_drawings(&mut blocks, &mut |drawing| {
        visited.push(match drawing {
            DrawingMut::Object { picture, .. } => format!("object {}", picture.embed),
            DrawingMut::Floating(floating) => {
                format!("floating {}", floating.picture.as_ref().unwrap().embed)
            }
        });
    });
    assert_eq!(
        visited,
        [
            "object a",
            "floating b",
            "object c",
            "floating d",
            "object e",
            "floating f",
            "floating g",
            "object h",
        ]
    );
}