pptx: round-trip p:video and p:audio media timing nodes and autoplay conditions

This commit is contained in:
Neil Cawse committed 2026-11-08 13:41:07 +00:00
1 parent 48e2203123
commit 8c85789cb1
4 files changed
+118 -7

No files matched your search

+38 -4
View File
@@ -207,11 +207,41 @@ fn behaviour_auto_reverses(c: &El) -> bool {
/// Volume, mute, Loop until Stopped, Hide While Not Playing and Play Full Screen live in the timing tree
/// (`p:video`/`p:audio` > `p:cMediaNode`), not on the picture: copy them onto the target's `MediaClip`.
pub fn media_settings(timing: &El, ctx: &IdCtx, shapes: &mut [Shape]) {
let mut nodes = vec![];
for tag in ["video", "audio"] {
timing.find_all(tag, &mut nodes);
fn collect_media<'a>(e: &'a El, deferred: bool, out: &mut Vec<(&'a El, bool)>, depth: usize) {
if depth > 64 || out.len() >= 10_000 {
return;
}
let mut deferred = deferred;
if e.is("cTn")
&& e.attr("nodeType") != Some("tmRoot")
&& let Some(st) = e.child("stCondLst")
{
let is_auto_begin = |c: &El| {
c.attr("evt") == Some("onBegin")
&& c.attr("delay").map(str::trim) == Some("0")
&& c.child("tgtEl").is_none()
&& c.child("tn").is_none()
&& c.child("rtn").is_none()
};
let has_on_begin = st.children_named("cond").any(is_auto_begin);
if st
.children_named("cond")
.any(|c| (c.attr("evt").is_some() && !is_auto_begin(c)) || (!has_on_begin && c.attr("delay").map(str::trim) != Some("0")))
{
deferred = true;
}
}
if e.is("video") || e.is("audio") {
out.push((e, deferred));
return;
}
for c in e.elements() {
collect_media(c, deferred, out, depth + 1);
}
}
for node in nodes.into_iter().take(10_000) {
let mut nodes = vec![];
collect_media(timing, false, &mut nodes, 0);
for (node, deferred) in nodes {
let Some(cm) = node.child("cMediaNode") else { continue };
let Some(id) = cm.path(&["tgtEl", "spTgt"]).and_then(|t| t.u32("spid")).and_then(|f| ctx.lookup(f)) else { continue };
let Some(ShapeKind::Media(clip)) = find_shape_mut(shapes, id).map(|s| &mut s.kind) else { continue };
@@ -227,6 +257,10 @@ pub fn media_settings(timing: &El, ctx: &IdCtx, shapes: &mut [Shape]) {
if cm.child("cTn").and_then(|c| c.attr("repeatCount")) == Some("indefinite") {
clip.loop_play = true;
}
if !deferred && cm.path(&["cTn", "stCondLst", "cond"]).is_some_and(|c| c.attr("evt").is_none() && c.attr("delay").map(str::trim) == Some("0"))
{
clip.autoplay = true;
}
if node.bool("fullScrn") == Some(true) {
clip.full_screen = true;
}
+14 -1
View File
@@ -813,7 +813,20 @@ fn slide_xml(x: &mut Exp, o: &mut Out, s: &Slide, comment_rel: Option<&str>) ->
if let Some(t) = &s.transition {
timing::transition(&mut w, x, o, t);
}
if !s.animations.is_empty() {
if !s.animations.is_empty()
|| s.shapes.iter().any(|shape| {
fn has_nondefault(shape: &deckcraft_model::Shape) -> bool {
match &shape.kind {
deckcraft_model::ShapeKind::Media(c) => {
c.autoplay || (c.volume - 1.0).abs() > 1e-6 || c.hide_while_not_playing || c.loop_play || c.full_screen
}
deckcraft_model::ShapeKind::Group { children, .. } => children.iter().any(has_nondefault),
_ => false,
}
}
has_nondefault(shape)
})
{
timing::timing(&mut w, x, &s.animations, &s.shapes, &ids);
}
slide_ext_list(&mut w, s.raw_ext.as_deref(), comment_rel);
+42 -2
View File
@@ -592,7 +592,7 @@ fn shape_index<'a>(shapes: &'a [Shape], depth: usize, out: &mut std::collections
}
}
/// Write `p:timing` for a slide's animations.
/// Write `p:timing` for a slide's animations and media settings.
pub fn timing(w: &mut W, x: &mut Exp, anims: &[Animation], shapes: &[Shape], idmap: &IdMap) {
let mut grp_count: std::collections::HashMap<u32, u32> = Default::default();
let mut main: Vec<Eff> = vec![];
@@ -686,7 +686,8 @@ pub fn timing(w: &mut W, x: &mut Exp, anims: &[Animation], shapes: &[Shape], idm
None => main.extend(effs),
}
}
if main.is_empty() && triggered.is_empty() {
let media = collect_media(shapes, idmap);
if main.is_empty() && triggered.is_empty() && media.is_empty() {
return;
}
let mut ids = Ids(0);
@@ -757,6 +758,9 @@ pub fn timing(w: &mut W, x: &mut Exp, anims: &[Animation], shapes: &[Shape], idm
w.close("p:nextCondLst");
w.close("p:seq");
}
for (spid, clip) in &media {
write_media_node(w, &mut ids, *spid, clip);
}
w.close("p:childTnLst");
w.close("p:cTn");
w.close("p:par");
@@ -770,3 +774,39 @@ pub fn timing(w: &mut W, x: &mut Exp, anims: &[Animation], shapes: &[Shape], idm
}
w.close("p:timing");
}
fn collect_media<'a>(shapes: &'a [Shape], idmap: &IdMap) -> Vec<(u32, &'a deckcraft_model::MediaClip)> {
let mut media = Vec::new();
deckcraft_model::walk(shapes, &mut |shape, _| {
if let ShapeKind::Media(clip) = &shape.kind
&& (clip.autoplay || (clip.volume - 1.0).abs() > 1e-6 || clip.hide_while_not_playing || clip.loop_play || clip.full_screen)
&& let Some(spid) = idmap.get(shape.id)
{
media.push((spid, clip));
}
});
media
}
fn write_media_node(w: &mut W, ids: &mut Ids, spid: u32, clip: &deckcraft_model::MediaClip) {
let tag = if clip.video { "p:video" } else { "p:audio" };
w.open(tag, A::new().t("fullScrn", clip.full_screen));
let mut cm = A::new().a("vol", (clip.volume.clamp(0.0, 1.0) * 100_000.0).round() as u32);
if clip.volume <= 1e-6 {
cm = cm.t("mute", true);
}
if clip.hide_while_not_playing {
cm = cm.a("showWhenStopped", 0);
}
w.open("p:cMediaNode", cm);
w.open("p:cTn", A::new().a("id", ids.next()).a("fill", "hold").a("display", 0).o("repeatCount", clip.loop_play.then_some("indefinite")));
w.open0("p:stCondLst");
w.empty("p:cond", A::new().a("delay", if clip.autoplay { "0" } else { "indefinite" }));
w.close("p:stCondLst");
w.close("p:cTn");
w.open0("p:tgtEl");
w.empty("p:spTgt", A::new().a("spid", spid));
w.close("p:tgtEl");
w.close("p:cMediaNode");
w.close(tag);
}
+24
View File
@@ -74,3 +74,27 @@ fn muted_audio_has_no_volume_and_other_clips_keep_theirs() {
assert_eq!(c[0].volume, 1.0);
assert_eq!((c[1].volume, c[1].loop_play, c[1].full_screen), (0.0, false, false));
}
#[test]
fn media_autoplay_respects_ancestor_conditions_and_delays() {
let immediate = clips(|v, _| {
format!(
r#"<p:par><p:cTn id="2" fill="hold"><p:stCondLst><p:cond delay="0"/></p:stCondLst><p:childTnLst><p:video><p:cMediaNode><p:cTn id="3" fill="hold"><p:stCondLst><p:cond delay="0"/></p:stCondLst></p:cTn><p:tgtEl><p:spTgt spid="{v}"/></p:tgtEl></p:cMediaNode></p:video></p:childTnLst></p:cTn></p:par>"#
)
});
assert!(immediate[0].autoplay);
let delayed_ancestor = clips(|v, _| {
format!(
r#"<p:par><p:cTn id="2" fill="hold"><p:stCondLst><p:cond delay="2000"/></p:stCondLst><p:childTnLst><p:video><p:cMediaNode><p:cTn id="3" fill="hold"><p:stCondLst><p:cond delay="0"/></p:stCondLst></p:cTn><p:tgtEl><p:spTgt spid="{v}"/></p:tgtEl></p:cMediaNode></p:video></p:childTnLst></p:cTn></p:par>"#
)
});
assert!(!delayed_ancestor[0].autoplay);
let targeted_tn_ancestor = clips(|v, _| {
format!(
r#"<p:par><p:cTn id="2" fill="hold"><p:stCondLst><p:cond evt="onBegin" delay="0"><p:tn val="2"/></p:cond></p:stCondLst><p:childTnLst><p:video><p:cMediaNode><p:cTn id="3" fill="hold"><p:stCondLst><p:cond delay="0"/></p:stCondLst></p:cTn><p:tgtEl><p:spTgt spid="{v}"/></p:tgtEl></p:cMediaNode></p:video></p:childTnLst></p:cTn></p:par>"#
)
});
assert!(!targeted_tn_ancestor[0].autoplay);
}