f-office-docx 0.2.1

DOCX model and Word's layout rules.
Documentation
//! The drawing walk: the one walk over the drawings of a story's blocks, through which the
//! readers give the pictures their images, the charts their parts and the SmartArt graphics
//! their saved drawings.

use std::rc::Rc;

use f_office_units::Length;

use super::{Block, Content};
use crate::floating::Floating;
use crate::picture::Picture;

/// A drawing that the drawing walk reaches.
pub(crate) enum DrawingMut<'a> {
    /// The picture of an inline object (`Content::Object`), with the object's room on its
    /// line, its width and its height.
    Object {
        /// The picture.
        picture: &'a mut Picture,
        /// The object's width and height.
        room: (&'a mut Length, &'a mut Length),
    },
    /// A floating drawing, the shape of an inline drawing, or a part of a group.
    Floating(&'a mut Floating),
}

/// Calls `visit` for each drawing of `blocks`, in the order of the document: the drawings of
/// the paragraphs and of the table cells, and for each floating drawing, after the drawing
/// itself, the drawings of its text box and then each part of its group with its own.
pub(crate) fn visit_drawings(blocks: &mut [Block], visit: &mut dyn FnMut(DrawingMut<'_>)) {
    for block in blocks {
        match block {
            Block::Paragraph(paragraph) => {
                for content in paragraph
                    .runs
                    .iter_mut()
                    .flat_map(|run| run.content.iter_mut())
                {
                    match content {
                        Content::Object {
                            width,
                            height,
                            picture: Some(picture),
                            ..
                        } => visit(DrawingMut::Object {
                            picture,
                            room: (width, height),
                        }),
                        Content::Floating(floating) => visit_floating(floating, visit),
                        Content::Drawing(drawing) => {
                            visit_floating(&mut Rc::make_mut(drawing).shape, visit);
                        }
                        _ => {}
                    }
                }
            }
            Block::Table(table) => {
                for cell in table.rows.iter_mut().flat_map(|row| row.cells.iter_mut()) {
                    visit_drawings(&mut cell.blocks, visit);
                }
            }
        }
    }
}

/// Visits `floating`, then the drawings of its text box, then each part of its group.
fn visit_floating(floating: &mut Floating, visit: &mut dyn FnMut(DrawingMut<'_>)) {
    visit(DrawingMut::Floating(floating));
    visit_drawings(&mut floating.blocks, visit);
    for (_, _, part) in &mut floating.parts {
        visit_floating(part, visit);
    }
}

#[cfg(test)]
mod tests;