f-office-docx 0.2.1

DOCX model and Word's layout rules.
Documentation
//! What the place of a frame (`w:framePr`) or of a floating table (`w:tblpPr`) is measured
//! from, and where the object lies in that area: the simple types `ST_HAnchor`, `ST_VAnchor`,
//! `ST_XAlign` and `ST_YAlign` of ECMA-376 Part 1, which both elements use. Floating drawings
//! and VML shapes use the same alignment, with the readers of their own formats.

use f_office_xml::enumeration::{Values, another, own};

/// What the position of a frame or a floating table is measured from (`ST_HAnchor`,
/// `ST_VAnchor`).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Anchor {
    /// The edge of the page.
    Page,
    /// The page margins.
    Margin,
    /// The text: the column across, and the place of the paragraph or the table in the flow
    /// down.
    #[default]
    Text,
}

/// `ST_HAnchor` and `ST_VAnchor`, which have the same names; a missing anchor is the text.
pub(crate) const ANCHOR: &Values<Anchor> = &Values::new(&[
    own("text", Anchor::Text),
    own("margin", Anchor::Margin),
    own("page", Anchor::Page),
]);

/// A horizontal place of `ST_XAlign`, with `inside` and `outside` apart from `left` and
/// `right`, which the mark of a frame tells apart.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum XAlign {
    Left,
    Center,
    Right,
    Inside,
    Outside,
}

impl XAlign {
    /// The alignment in the area: `inside` at the left and `outside` at the right, on every
    /// page.
    pub(crate) const fn align(self) -> Align {
        match self {
            Self::Left | Self::Inside => Align::Start,
            Self::Center => Align::Center,
            Self::Right | Self::Outside => Align::End,
        }
    }
}

/// `ST_XAlign`: `inside` is drawn at the left and `outside` at the right, on every page.
pub(crate) const X_ALIGN: &Values<XAlign> = &Values::new(&[
    own("left", XAlign::Left),
    own("center", XAlign::Center),
    own("right", XAlign::Right),
    another("inside", XAlign::Inside),
    another("outside", XAlign::Outside),
]);

/// A vertical place of `ST_YAlign`: where the object's own text would lie, or an alignment in
/// its area.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum YAlign {
    /// Where the object's text would lie (`inline`).
    Inline,
    /// At the top, in the middle or at the bottom of the area.
    Align(Align),
}

/// `ST_YAlign`: `inside` is drawn at the top and `outside` at the bottom, on every page.
pub(crate) const Y_ALIGN: &Values<YAlign> = &Values::new(&[
    own("inline", YAlign::Inline),
    own("top", YAlign::Align(Align::Start)),
    own("center", YAlign::Align(Align::Center)),
    own("bottom", YAlign::Align(Align::End)),
    another("inside", YAlign::Align(Align::Start)),
    another("outside", YAlign::Align(Align::End)),
]);

impl YAlign {
    /// The alignment, or `None` for `inline`.
    pub(crate) const fn align(self) -> Option<Align> {
        match self {
            Self::Inline => None,
            Self::Align(align) => Some(align),
        }
    }
}

/// Where a frame, a floating table or a floating drawing lies in its area when no offset is
/// given.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Align {
    /// At the start: left or top.
    Start,
    /// In the middle.
    Center,
    /// At the end: right or bottom.
    End,
}