use super::package::P_NS;
use crate::word::xml::{DRAWING_NS, child};
use roxmltree::Node;
pub(crate) const FURNITURE: [&str; 5] = ["dt", "ftr", "sldNum", "hdr", "sldImg"];
const MAX_LEVEL: u8 = 8;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Placeholder<'a> {
pub(crate) kind: &'a str,
pub(crate) idx: Option<&'a str>,
}
impl Placeholder<'_> {
pub(crate) fn is_title(&self) -> bool {
matches!(self.kind, "title" | "ctrTitle")
}
pub(crate) fn is_furniture(&self) -> bool {
FURNITURE.contains(&self.kind)
}
fn style_name(&self) -> &'static str {
if self.is_title() {
"titleStyle"
} else if self.is_furniture() {
"otherStyle"
} else {
"bodyStyle"
}
}
fn master_kind(&self) -> &str {
match self.kind {
"ctrTitle" => "title",
"obj" | "subTitle" | "tbl" | "chart" | "pic" | "clipArt" | "dgm" | "media" => "body",
other => other,
}
}
}
pub(crate) fn placeholder<'a>(shape: Node<'a, '_>) -> Option<Placeholder<'a>> {
let ph = shape
.children()
.find(|node| node.is_element() && node.tag_name().name().starts_with("nv"))
.and_then(|nv| child(nv, P_NS, "nvPr"))
.and_then(|nv| child(nv, P_NS, "ph"))?;
Some(Placeholder {
kind: ph.attribute("type").unwrap_or("obj"),
idx: ph.attribute("idx"),
})
}
#[derive(Clone, Copy, Default)]
pub(crate) struct Inheritance<'a, 'input> {
pub(crate) layout: Option<Node<'a, 'input>>,
pub(crate) master: Option<Node<'a, 'input>>,
pub(crate) styles: Option<Node<'a, 'input>>,
pub(crate) defaults: Option<Node<'a, 'input>>,
}
impl<'a, 'input> Inheritance<'a, 'input> {
pub(crate) fn kind_of(&self, ph: Placeholder<'a>) -> &'a str {
if ph.kind != "obj" || ph.idx.is_none() {
return ph.kind;
}
self.layout_match(ph)
.and_then(placeholder)
.map_or(ph.kind, |layout| layout.kind)
}
pub(crate) fn layout_match(&self, ph: Placeholder<'_>) -> Option<Node<'a, 'input>> {
let tree = self.layout?;
let shapes: Vec<(Node<'a, 'input>, Placeholder<'a>)> = tree
.descendants()
.filter_map(|shape| placeholder(shape).map(|own| (shape, own)))
.collect();
ph.idx
.and_then(|idx| shapes.iter().find(|(_, own)| own.idx == Some(idx)))
.or_else(|| shapes.iter().find(|(_, own)| own.master_kind() == ph.master_kind()))
.map(|(shape, _)| *shape)
}
pub(crate) fn master_match(&self, ph: Placeholder<'_>) -> Option<Node<'a, 'input>> {
let kind = ph.master_kind();
self.master?
.descendants()
.find(|shape| placeholder(*shape).is_some_and(|own| own.master_kind() == kind))
}
pub(crate) fn list_styles(&self, shape: Node<'a, 'input>) -> Vec<Node<'a, 'input>> {
let mut styles: Vec<Node<'a, 'input>> = own_list_style(shape).into_iter().collect();
if let Some(ph) = placeholder(shape) {
let ph = Placeholder {
kind: self.kind_of(ph),
..ph
};
let layout = self.layout_match(ph);
styles.extend(layout.and_then(own_list_style));
let master = layout
.and_then(placeholder)
.map_or_else(|| self.master_match(ph), |own| self.master_match(own));
styles.extend(master.and_then(own_list_style));
styles.extend(self.styles.and_then(|styles| child(styles, P_NS, ph.style_name())));
}
styles.extend(self.defaults);
styles
}
}
pub(crate) fn level_props<'a, 'input>(
para_props: Option<Node<'a, 'input>>,
styles: &[Node<'a, 'input>],
level: u8,
) -> Vec<Node<'a, 'input>> {
let name = format!("lvl{}pPr", level.min(MAX_LEVEL) + 1);
para_props
.into_iter()
.chain(styles.iter().filter_map(|style| child(*style, DRAWING_NS, &name)))
.collect()
}
fn own_list_style<'a, 'input>(shape: Node<'a, 'input>) -> Option<Node<'a, 'input>> {
shape
.children()
.find(|node| node.is_element() && node.tag_name().name() == "txBody")
.and_then(|body| child(body, DRAWING_NS, "lstStyle"))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::word::docx::parse;
const NS: &str = "xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\" \
xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"";
fn tree(shapes: &[(Option<&str>, Option<&str>, &str)]) -> String {
let mut out = format!("<p:spTree {NS}>");
for (kind, idx, mark) in shapes {
let kind = kind.map(|kind| format!(" type=\"{kind}\"")).unwrap_or_default();
let idx = idx.map(|idx| format!(" idx=\"{idx}\"")).unwrap_or_default();
out += &format!(
"<p:sp><p:nvSpPr><p:cNvPr id=\"1\" name=\"{mark}\"/><p:cNvSpPr/><p:nvPr><p:ph{kind}{idx}/></p:nvPr></p:nvSpPr>\
<p:txBody><a:bodyPr/><a:lstStyle><a:lvl1pPr marL=\"{mark}\"/></a:lstStyle></p:txBody></p:sp>"
);
}
out + "</p:spTree>"
}
fn marks(slide: &str, layout: &str, master: &str) -> Vec<String> {
let (slide, layout, master) = (parse(slide).unwrap(), parse(layout).unwrap(), parse(master).unwrap());
let inheritance = Inheritance {
layout: Some(layout.root_element()),
master: Some(master.root_element()),
..Default::default()
};
let shape = slide.root_element().first_element_child().unwrap();
let styles = inheritance.list_styles(shape);
level_props(None, &styles, 0)
.iter()
.filter_map(|props| props.attribute("marL").map(str::to_string))
.collect()
}
#[test]
fn placeholder_by_idx_then_type() {
let master = tree(&[(Some("title"), None, "mt"), (Some("body"), Some("1"), "mb")]);
let layout = tree(&[
(Some("title"), None, "lt"),
(None, Some("1"), "l1"),
(None, Some("13"), "l13"),
]);
assert_eq!(
marks(&tree(&[(None, Some("13"), "s")]), &layout, &master),
["s", "l13", "mb"]
);
assert_eq!(
marks(&tree(&[(Some("title"), None, "s")]), &layout, &master),
["s", "lt", "mt"]
);
}
#[test]
fn body_stands_for_obj_and_untyped() {
let master = tree(&[(Some("body"), Some("1"), "mb")]);
let layout = tree(&[(Some("subTitle"), Some("1"), "ls")]);
assert_eq!(
marks(&tree(&[(None, Some("1"), "s")]), &layout, &master),
["s", "ls", "mb"]
);
let slide = tree(&[(None, Some("1"), "s")]);
let doc = parse(&slide).unwrap();
let layout = parse(&layout).unwrap();
let inheritance = Inheritance {
layout: Some(layout.root_element()),
..Default::default()
};
let ph = placeholder(doc.root_element().first_element_child().unwrap()).unwrap();
assert_eq!(inheritance.kind_of(ph), "subTitle");
}
#[test]
fn orphan_placeholder_keeps_its_own_text() {
let layout = tree(&[(Some("title"), None, "lt")]);
let slide = tree(&[(Some("body"), Some("7"), "s")]);
let (slide, layout) = (parse(&slide).unwrap(), parse(&layout).unwrap());
let inheritance = Inheritance {
layout: Some(layout.root_element()),
..Default::default()
};
let shape = slide.root_element().first_element_child().unwrap();
let styles = inheritance.list_styles(shape);
assert_eq!(styles.len(), 1);
}
#[test]
fn levels_merge_from_paragraph_to_default_style() {
let master = format!(
"<p:sldMaster {NS}><p:txStyles><p:bodyStyle><a:lvl2pPr marL=\"m2\"/></p:bodyStyle></p:txStyles></p:sldMaster>"
);
let defaults = format!("<p:defaultTextStyle {NS}><a:lvl2pPr marL=\"d2\"/></p:defaultTextStyle>");
let slide = tree(&[(Some("body"), Some("1"), "s")]);
let (master, defaults, slide) = (
parse(&master).unwrap(),
parse(&defaults).unwrap(),
parse(&slide).unwrap(),
);
let inheritance = Inheritance {
styles: child(master.root_element(), P_NS, "txStyles"),
defaults: Some(defaults.root_element()),
..Default::default()
};
let shape = slide.root_element().first_element_child().unwrap();
let styles = inheritance.list_styles(shape);
let marks: Vec<&str> = level_props(None, &styles, 1)
.iter()
.filter_map(|props| props.attribute("marL"))
.collect();
assert_eq!(marks, ["m2", "d2"]);
}
}