mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-20 19:18:41 +00:00
Merge pull request #7830 from storytold/fix/pptx-parity-20261119
Restore plain PPTX tables and shared-writer regression checks
This commit is contained in:
4 files changed
+62
-23
No files matched your search
@@ -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<u8> {
|
||||
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",
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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("<p:pic>"));
|
||||
assert!(slide_xml.contains("<a:blip r:embed=\"rIdImg1\"/>"));
|
||||
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("<p:ph"), "pure text boxes avoid placeholder inheritance repair warnings: {slide_xml}");
|
||||
|
||||
let rels_xml = part(&bytes, "ppt/slides/_rels/slide1.xml.rels");
|
||||
@@ -250,7 +272,7 @@ fn pptx_package_parts_are_complete_and_consistent() {
|
||||
let inner = theme.split(&format!("<a:{font}>")).nth(1).and_then(|r| r.split(&format!("</a:{font}>")).next()).unwrap_or_default();
|
||||
assert!(inner.contains("<a:latin ") && inner.contains("<a:ea ") && inner.contains("<a:cs "), "{font}: {inner}");
|
||||
}
|
||||
assert!(text("ppt/presentation.xml").contains("r:id=\"rIdSld2\""));
|
||||
assert_slide_targets(&bytes, &["slides/slide1.xml", "slides/slide2.xml"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -292,8 +314,7 @@ fn hostile_sizes_do_not_overflow_the_slide_position() {
|
||||
pub fn pptx_empty_pages_produce_valid_slide() {
|
||||
let bytes = pptx(&[], "Blank");
|
||||
assert!(bytes.starts_with(b"PK"));
|
||||
let pres_xml = part(&bytes, "ppt/presentation.xml");
|
||||
assert!(pres_xml.contains("<p:sldId id=\"256\" r:id=\"rIdSld1\"/>"));
|
||||
assert_slide_targets(&bytes, &["slides/slide1.xml"]);
|
||||
let slide_xml = part(&bytes, "ppt/slides/slide1.xml");
|
||||
assert!(slide_xml.contains("<p:spTree>"));
|
||||
}
|
||||
@@ -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("<a:tbl>"));
|
||||
assert!(slide_xml.contains("+99%"));
|
||||
assert!(slide_xml.contains("<a:tcPr><a:lnL w=\"12700\" cmpd=\"s\">"));
|
||||
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("<p:pic>").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");
|
||||
|
||||
@@ -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("<a:p><a:r><a:rPr sz=\"1200\"></a:rPr><a:t>Before </a:t></a:r><a:r><a:rPr sz=\"1200\" i=\"1\"></a:rPr><a:t>Chevron </a:t></a:r><a:r><a:rPr sz=\"1200\"></a:rPr><a:t>after.</a:t></a:r></a:p>"),
|
||||
"{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
|
||||
|
||||
Reference in new issue
Block a user