diff --git a/crates/pptx/src/read/timing.rs b/crates/pptx/src/read/timing.rs index 1b6aa8b1b..37a7ccb53 100644 --- a/crates/pptx/src/read/timing.rs +++ b/crates/pptx/src/read/timing.rs @@ -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; } diff --git a/crates/pptx/src/write.rs b/crates/pptx/src/write.rs index e4c0858c9..888703b97 100644 --- a/crates/pptx/src/write.rs +++ b/crates/pptx/src/write.rs @@ -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); diff --git a/crates/pptx/src/write/timing.rs b/crates/pptx/src/write/timing.rs index 6376e3ca0..766e3abad 100644 --- a/crates/pptx/src/write/timing.rs +++ b/crates/pptx/src/write/timing.rs @@ -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 = Default::default(); let mut main: Vec = 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); +} diff --git a/crates/pptx/tests/it/media_timing.rs b/crates/pptx/tests/it/media_timing.rs index 8fe325964..ba1fb6174 100644 --- a/crates/pptx/tests/it/media_timing.rs +++ b/crates/pptx/tests/it/media_timing.rs @@ -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#""# + ) + }); + assert!(immediate[0].autoplay); + + let delayed_ancestor = clips(|v, _| { + format!( + r#""# + ) + }); + assert!(!delayed_ancestor[0].autoplay); + + let targeted_tn_ancestor = clips(|v, _| { + format!( + r#""# + ) + }); + assert!(!targeted_tn_ancestor[0].autoplay); +}