diff --git a/crates/pptx/src/lib.rs b/crates/pptx/src/lib.rs index d916f10db..c395209b2 100644 --- a/crates/pptx/src/lib.rs +++ b/crates/pptx/src/lib.rs @@ -539,7 +539,7 @@ fn media_needs_timing(clip: &deckcraft_model::MediaClip) -> bool { || (clip.video && clip.full_screen) } -fn collect_export_media<'a>(shapes: &'a [Shape]) -> Vec<(u32, &'a deckcraft_model::MediaClip)> { +fn assign_spids<'a>(shapes: &'a [Shape], mut visit: impl FnMut(u32, &'a Shape)) { let mut used = std::collections::HashSet::new(); let mut max = 1u32; deckcraft_model::walk(shapes, &mut |s, _| { @@ -548,7 +548,6 @@ fn collect_export_media<'a>(shapes: &'a [Shape]) -> Vec<(u32, &'a deckcraft_mode } }); let mut next = max; - let mut media = Vec::new(); deckcraft_model::walk(shapes, &mut |s, _| { let spid = if s.id.0 >= 2 && s.id.0 < i32::MAX as u32 && !used.contains(&s.id.0) { s.id.0 @@ -557,6 +556,13 @@ fn collect_export_media<'a>(shapes: &'a [Shape]) -> Vec<(u32, &'a deckcraft_mode next }; used.insert(spid); + visit(spid, s); + }); +} + +fn collect_export_media(shapes: &[Shape]) -> Vec<(u32, &deckcraft_model::MediaClip)> { + let mut media = Vec::new(); + assign_spids(shapes, |spid, s| { if let ShapeKind::Media(clip) = &s.kind && media_needs_timing(clip) { @@ -566,6 +572,18 @@ fn collect_export_media<'a>(shapes: &'a [Shape]) -> Vec<(u32, &'a deckcraft_mode media } +fn collect_grp_fill_pic_spids(shapes: &[Shape]) -> std::collections::HashSet { + let mut spids = std::collections::HashSet::new(); + assign_spids(shapes, |spid, s| { + if s.fill == Some(deckcraft_model::style::Fill::Group) + && matches!(s.kind, ShapeKind::Picture { .. } | ShapeKind::Opaque { preview: Some(_), .. }) + { + spids.insert(spid); + } + }); + spids +} + fn write_media_node(w: &mut craft_xml::Writer, id: &mut u32, spid: u32, clip: &deckcraft_model::MediaClip) { let tag = if clip.video { "p:video" } else { "p:audio" }; let mut t = w.tag(tag); @@ -642,9 +660,107 @@ fn patch_slide_media_timing(xml_bytes: &[u8], media: &[(u32, &deckcraft_model::M xml.into_bytes() } -fn patch_exported_media_timing(p: &Presentation, bytes: Vec) -> Result, PptxError> { +fn patch_pic_grp_fill(xml_bytes: &[u8], spids: &std::collections::HashSet) -> Vec { + let Ok(mut xml) = String::from_utf8(xml_bytes.to_vec()) else { + return xml_bytes.to_vec(); + }; + for &spid in spids { + let needle = format!(r#"')); + if boundary_ok + && let Some(pic_end_rel) = xml[after..].find("") + && let Some(sp_pr_rel) = xml[after..after + pic_end_rel].find("") + { + let sp_pr_start = after + sp_pr_rel + "".len(); + if let Some(sp_pr_end_rel) = xml[sp_pr_start..after + pic_end_rel].find("") { + let sp_pr_end = sp_pr_start + sp_pr_end_rel; + let inner = &xml[sp_pr_start..sp_pr_end]; + if !inner.contains(""); + } + break; + } + } + search_from = after; + } + } + xml.into_bytes() +} + +fn collect_large_geom_adjs(shapes: &[Shape]) -> Vec<(u32, &[f64])> { + let mut out = Vec::new(); + assign_spids(shapes, |spid, s| { + if let deckcraft_model::Geom::Preset { adj, .. } = &s.geom + && adj.iter().any(|v| v.is_finite() && (v.round() < -2.0e9 || v.round() > 2.0e9)) + { + out.push((spid, adj.as_slice())); + } + }); + out +} + +fn patch_shape_av_lst(xml: &mut String, after: usize, adj: &[f64]) { + let shape_end_rel = ["", "", ""].iter().filter_map(|t| xml[after..].find(t)).min(); + let Some(shape_end_rel) = shape_end_rel else { return }; + let Some(av_rel) = xml[after..after + shape_end_rel].find("") else { return }; + let av_start = after + av_rel + "".len(); + let Some(av_end_rel) = xml[av_start..after + shape_end_rel].find("") else { return }; + let mut av_end = av_start + av_end_rel; + for (i, &v) in adj.iter().take(16).enumerate() { + if !v.is_finite() || (v.round() >= -2.0e9 && v.round() <= 2.0e9) { + continue; + } + let clamped = v.round().clamp(-2.0e9, 2.0e9) as i64; + let actual = v.round() as i64; + let single_name = format!(r#""#); + let idx_name = format!(r#""#, i + 1); + let (target, repl) = if i == 0 && xml[av_start..av_end].contains(&single_name) { + (single_name, format!(r#""#)) + } else { + (idx_name, format!(r#""#, i + 1)) + }; + if let Some(rel) = xml[av_start..av_end].find(&target) { + let pos = av_start + rel; + xml.replace_range(pos..pos + target.len(), &repl); + av_end = av_end + repl.len() - target.len(); + } + } +} + +fn patch_large_geom_adjs(xml_bytes: &[u8], adjs: &[(u32, &[f64])]) -> Vec { + let Ok(mut xml) = String::from_utf8(xml_bytes.to_vec()) else { + return xml_bytes.to_vec(); + }; + for &(spid, adj) in adjs { + let needle = format!(r#"')) { + patch_shape_av_lst(&mut xml, after, adj); + break; + } + search_from = after; + } + } + xml.into_bytes() +} + +fn patch_exported_slides(p: &Presentation, bytes: Vec) -> Result, PptxError> { let slide_media: Vec> = p.slides.iter().map(|s| collect_export_media(&s.shapes)).collect(); - if slide_media.iter().all(Vec::is_empty) { + let slide_grp_pics: Vec> = p.slides.iter().map(|s| collect_grp_fill_pic_spids(&s.shapes)).collect(); + let slide_adjs: Vec> = p.slides.iter().map(|s| collect_large_geom_adjs(&s.shapes)).collect(); + if slide_media.iter().all(Vec::is_empty) && slide_grp_pics.iter().all(std::collections::HashSet::is_empty) && slide_adjs.iter().all(Vec::is_empty) + { return Ok(bytes); } let pkg = craft_opc::Package::open(&bytes, &craft_pptx::LIMITS).map_err(|e| PptxError::Write(e.to_string()))?; @@ -657,14 +773,26 @@ fn patch_exported_media_timing(p: &Presentation, bytes: Vec) -> Result) -> Result Result, PptxError> { check_table_caps(p)?; let bytes = craft_pptx::export(&convert::pres_to_craft(p), &DeckDefaults)?; - patch_exported_media_timing(p, bytes) + patch_exported_slides(p, bytes) } /// Write a package for `ext`: `potx` (template) and `ppsx` (slide show) differ from `pptx` only in @@ -693,7 +821,7 @@ pub fn export(p: &Presentation) -> Result, PptxError> { pub fn export_as(p: &Presentation, ext: &str) -> Result, PptxError> { check_table_caps(p)?; let bytes = craft_pptx::export_as(&convert::pres_to_craft(p), &DeckDefaults, ext)?; - patch_exported_media_timing(p, bytes) + patch_exported_slides(p, bytes) } #[cfg(test)]