use f_office_units::Length;
use f_office_xml::NodeId;
use f_office_xml::enumeration::{Values, another, own};
use super::place::{ANCHOR, Align, Anchor, X_ALIGN, XAlign, Y_ALIGN, YAlign};
use crate::reader::PartReader;
use crate::table::{HEIGHT_RULE, HeightRule};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct FramePr {
pub width: Option<Length>,
pub x: Option<Length>,
pub y: Option<Length>,
pub x_align: Option<Align>,
pub h_anchor: Anchor,
pub v_anchor: Anchor,
pub inline: bool,
pub y_align: Option<Align>,
pub v_anchor_named: bool,
pub wrap: FrameWrap,
pub spacing: (Length, Length),
pub height: Option<(Length, bool)>,
pub drop_cap: bool,
pub mark: FrameMark,
attributes: Attributes,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
struct Attributes {
width: Option<Length>,
height: Option<Length>,
x: Option<Length>,
y: Option<Length>,
h_space: Option<Length>,
v_space: Option<Length>,
x_align: Option<XAlign>,
y_align: Option<YAlign>,
h_anchor: Option<Anchor>,
v_anchor: Option<Anchor>,
wrap: Option<Wrap>,
drop_cap: Option<bool>,
rule: Option<HeightRule>,
}
impl Attributes {
fn over(self, under: Self) -> Self {
Self {
width: self.width.or(under.width),
height: self.height.or(under.height),
x: self.x.or(under.x),
y: self.y.or(under.y),
h_space: self.h_space.or(under.h_space),
v_space: self.v_space.or(under.v_space),
x_align: self.x_align.or(under.x_align),
y_align: self.y_align.or(under.y_align),
h_anchor: self.h_anchor.or(under.h_anchor),
v_anchor: self.v_anchor.or(under.v_anchor),
wrap: self.wrap.or(under.wrap),
drop_cap: self.drop_cap.or(under.drop_cap),
rule: self.rule.or(under.rule),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FrameMark {
#[default]
Frame,
Plain,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FrameWrap {
#[default]
Around,
NotBeside,
None,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Wrap {
Auto,
NotBeside,
Around,
Tight,
Through,
None,
}
impl Wrap {
const fn flow(self) -> FrameWrap {
match self {
Self::None => FrameWrap::None,
Self::NotBeside => FrameWrap::NotBeside,
Self::Auto | Self::Around | Self::Tight | Self::Through => FrameWrap::Around,
}
}
}
pub(super) const WRAP: &Values<Wrap> = &Values::new(&[
another("auto", Wrap::Auto),
own("notBeside", Wrap::NotBeside),
own("around", Wrap::Around),
another("tight", Wrap::Tight),
another("through", Wrap::Through),
own("none", Wrap::None),
]);
pub(super) const DROP_CAP: &Values<bool> = &Values::new(&[
own("none", false),
own("drop", true),
another("margin", false),
]);
impl FramePr {
#[must_use]
pub fn in_place(&self) -> bool {
self.inline
&& self.h_anchor == Anchor::Text
&& match self.x_align {
Some(align) => align == Align::Start,
None => self.x.is_none_or(|x| x == Length::ZERO),
}
}
fn mark(named: bool, lengths: [Option<Length>; 4]) -> FrameMark {
let sized = lengths
.iter()
.any(|length| length.is_some_and(|length| length != Length::ZERO));
if named || sized {
FrameMark::Frame
} else {
FrameMark::Plain
}
}
pub(super) fn read(reader: &mut PartReader, node: NodeId) -> Self {
let [width, height, x, y] = ["w", "h", "x", "y"].map(|name| reader.twips(node, name));
let [h_space, v_space] = ["hSpace", "vSpace"].map(|name| reader.twips(node, name));
Self::of(Attributes {
width,
height,
x,
y,
h_space,
v_space,
x_align: reader.enumeration(node, "xAlign", X_ALIGN),
y_align: reader.enumeration(node, "yAlign", Y_ALIGN),
h_anchor: reader.enumeration(node, "hAnchor", ANCHOR),
v_anchor: reader.enumeration(node, "vAnchor", ANCHOR),
wrap: reader.enumeration(node, "wrap", WRAP),
drop_cap: reader.enumeration(node, "dropCap", DROP_CAP),
rule: reader.enumeration(node, "hRule", HEIGHT_RULE),
})
}
pub(super) fn overlay(frame: &mut Option<Box<Self>>, nearer: Option<&Self>) {
if let Some(nearer) = nearer {
let attributes = frame.as_deref().map_or(nearer.attributes, |under| {
nearer.attributes.over(under.attributes)
});
*frame = Some(Box::new(Self::of(attributes)));
}
}
fn of(attributes: Attributes) -> Self {
let Attributes {
width,
height,
x,
y,
h_space,
v_space,
x_align,
y_align,
h_anchor,
v_anchor,
wrap,
drop_cap,
rule,
} = attributes;
let named = wrap.is_some_and(|wrap| wrap != Wrap::Auto)
|| h_anchor.is_some_and(|anchor| anchor != Anchor::Text)
|| v_anchor == Some(Anchor::Text)
|| x_align.is_some_and(|align| align != XAlign::Left)
|| y_align.is_some_and(|align| align != YAlign::Inline);
Self {
width: width.filter(|w| *w > Length::ZERO),
x,
y,
x_align: x_align.map(XAlign::align),
h_anchor: h_anchor.unwrap_or_default(),
v_anchor: v_anchor.unwrap_or_default(),
v_anchor_named: v_anchor.is_some(),
inline: y_align == Some(YAlign::Inline),
y_align: y_align.and_then(YAlign::align),
wrap: wrap.map_or_else(FrameWrap::default, Wrap::flow),
spacing: (
h_space.unwrap_or(Length::ZERO),
v_space.unwrap_or(Length::ZERO),
),
height: height
.filter(|h| *h > Length::ZERO)
.and_then(|h| match rule {
Some(HeightRule::Exact) => Some((h, true)),
Some(HeightRule::Auto) => None,
Some(HeightRule::AtLeast) | None => Some((h, false)),
}),
drop_cap: drop_cap.unwrap_or(false),
mark: Self::mark(named, [width, height, x, y]),
attributes,
}
}
}