diff --git a/crates/export/src/as_docx.rs b/crates/export/src/as_docx.rs index bfb0ba4bc4..39e2f89934 100644 --- a/crates/export/src/as_docx.rs +++ b/crates/export/src/as_docx.rs @@ -4,18 +4,6 @@ use super::as_word::{Meta, word_doc}; use super::layout::{Image, Page}; -use craft_xml::Writer; - -pub(crate) fn core_properties_xml(title: &str) -> Vec { - let mut w = Writer::document(); - w.open( - "cp:coreProperties", - &[("xmlns:cp", "http://schemas.openxmlformats.org/package/2006/metadata/core-properties"), ("xmlns:dc", "http://purl.org/dc/elements/1.1/")], - ); - w.leaf("dc:title", &[], title); - w.into_bytes() -} - pub(crate) fn image_mime(ext: &str) -> &'static str { match ext.to_ascii_lowercase().as_str() { "png" => "image/png", diff --git a/crates/export/src/pptx.rs b/crates/export/src/pptx.rs index be66397190..38814cf58e 100644 --- a/crates/export/src/pptx.rs +++ b/crates/export/src/pptx.rs @@ -116,7 +116,8 @@ fn pptx_table(shape_id: u32, t: &Table, x: f64, y: f64, content_w: f64) -> (Shap id: ShapeId(shape_id), name: format!("Table {shape_id}"), xfrm: Some(Xfrm { x, y, w: table_w, h: table_h, ..Default::default() }), - kind: ShapeKind::Table(DmlTable { cols: widths, rows, ..Default::default() }), + // PDF cells already carry their formatting; do not invent a themed header or banding. + kind: ShapeKind::Table(DmlTable { cols: widths, rows, style: "none".into(), first_row: false, band_row: false, ..Default::default() }), ..Default::default() }; (shape, table_h) diff --git a/crates/export/src/pptx/tests.rs b/crates/export/src/pptx/tests.rs index d703c682f3..ff8157c3f0 100644 --- a/crates/export/src/pptx/tests.rs +++ b/crates/export/src/pptx/tests.rs @@ -2,6 +2,25 @@ use super::*; use crate::Block; use crate::tests::part; +fn xml(text: &str) -> craft_xml::El { + craft_xml::parse(text.as_bytes()).expect("valid package XML") +} + +fn relationship_target<'a>(rels: &'a craft_xml::El, id: &str) -> &'a str { + rels.children("Relationship").find(|r| r.attr("Id") == Some(id)).and_then(|r| r.attr("Target")).expect("referenced relationship") +} + +fn assert_slide_targets(bytes: &[u8], targets: &[&str]) { + let pres = xml(&part(bytes, "ppt/presentation.xml")); + let rels = xml(&part(bytes, "ppt/_rels/presentation.xml.rels")); + let ids: Vec<_> = pres.find("p:sldIdLst").expect("slide list").children("p:sldId").collect(); + assert_eq!(ids.len(), targets.len()); + for (slide, target) in ids.iter().zip(targets) { + assert!(slide.attr_u32("id").expect("numeric slide id") >= 256); + assert_eq!(relationship_target(&rels, slide.attr("r:id").expect("slide relationship")), *target); + } +} + fn sample_page() -> Page { Page { width: 720.0, @@ -85,7 +104,10 @@ pub fn pptx_slide_has_title_body_and_images_in_order() { assert!(slide_xml.contains("name=\"TextBox 3\"")); assert!(slide_xml.contains("Revenue grew 25%")); assert!(slide_xml.contains("")); - assert!(slide_xml.contains("")); + let slide = xml(&slide_xml); + let rels = xml(&part(&bytes, "ppt/slides/_rels/slide1.xml.rels")); + let id = slide.find("a:blip").and_then(|n| n.attr("r:embed")).expect("embedded picture"); + assert_eq!(relationship_target(&rels, id), "../media/image1.png"); assert!(!slide_xml.contains("")).nth(1).and_then(|r| r.split(&format!("")).next()).unwrap_or_default(); assert!(inner.contains("")); + assert_slide_targets(&bytes, &["slides/slide1.xml"]); let slide_xml = part(&bytes, "ppt/slides/slide1.xml"); assert!(slide_xml.contains("")); } @@ -372,7 +393,20 @@ fn pptx_table_cell_with_largest_font_stays_in_table_and_not_in_title() { // The table must contain both the cell and explicit borders assert!(slide_xml.contains("")); assert!(slide_xml.contains("+99%")); - assert!(slide_xml.contains("")); + let slide = xml(&slide_xml); + let table = slide.find("a:tbl").expect("table"); + let props = table.child("a:tblPr").expect("table properties"); + assert_eq!(props.child_text("a:tableStyleId").as_deref(), Some("{2D5ABB26-0587-4C30-8999-92F81FD0307C}")); + assert!(!props.flag("firstRow", false) && !props.flag("bandRow", false), "no invented header/banding"); + let cells: Vec<_> = table.descendants().filter(|n| n.is("a:tcPr")).collect(); + assert_eq!(cells.len(), 6); + for cell in cells { + for edge in ["a:lnL", "a:lnR", "a:lnT", "a:lnB"] { + let line = cell.child(edge).expect("explicit border"); + assert_eq!(line.attr_u32("w"), Some(12700)); + assert_eq!(line.find("a:srgbClr").and_then(|n| n.attr("val")), Some("999999")); + } + } } #[test] @@ -413,7 +447,13 @@ fn pptx_packages_a_repeated_picture_once() { p.images = std::iter::repeat_n(one, 2 * crate::as_docx::MAX_MEDIA).collect(); let bytes = pptx(&[p], "Deck"); let rels = part(&bytes, "ppt/slides/_rels/slide1.xml.rels"); - assert_eq!(rels.matches("relationships/image\"").count(), 2 * crate::as_docx::MAX_MEDIA, "every place refers to the picture"); + let relationships = xml(&rels); + let slide = xml(&part(&bytes, "ppt/slides/slide1.xml")); + let blips: Vec<_> = slide.descendants().filter(|n| n.is("a:blip")).collect(); + assert_eq!(blips.len(), 2 * crate::as_docx::MAX_MEDIA, "every placement has an image reference"); + for blip in blips { + assert_eq!(relationship_target(&relationships, blip.attr("r:embed").expect("picture relationship")), "../media/image1.png"); + } assert!(rels.contains("Target=\"../media/image1.png\"") && !rels.contains("image2."), "one media part"); assert_eq!(part(&bytes, "ppt/slides/slide1.xml").matches("").count(), 2 * crate::as_docx::MAX_MEDIA); assert_eq!(bytes.windows(20).filter(|w| *w == b"ppt/media/image1.png").count(), 2, "its local header and central directory entry"); diff --git a/crates/export/src/tests.rs b/crates/export/src/tests.rs index d2defc87ff..76da4c6981 100644 --- a/crates/export/src/tests.rs +++ b/crates/export/src/tests.rs @@ -924,10 +924,20 @@ fn an_italic_word_stays_in_its_sentence() { ); // The longest line is the slide's title; the sentence is one paragraph of the body. let pxml = part(&pptx(std::slice::from_ref(&p), "Opinion"), "ppt/slides/slide1.xml"); - assert!( - pxml.contains("Before Chevron after."), - "{pxml}" - ); + let slide = craft_xml::parse(pxml.as_bytes()).expect("slide XML"); + let paragraph = slide + .descendants() + .filter(|n| n.is("a:p")) + .find(|p| p.children("a:r").any(|r| r.child_text("a:t").as_deref() == Some("Before "))) + .expect("body paragraph"); + let actual: Vec<_> = paragraph + .children("a:r") + .map(|r| { + let props = r.child("a:rPr").expect("run properties"); + (r.child_text("a:t").expect("run text").into_owned(), props.attr_u32("sz"), props.flag("i", false)) + }) + .collect(); + assert_eq!(actual, vec![("Before ".into(), Some(1200), false), ("Chevron ".into(), Some(1200), true), ("after.".into(), Some(1200), false)]); } /// #4158: the runs keep their order along the line whatever order the blocks come in, and the