diff --git a/crates/export/src/as_docx.rs b/crates/export/src/as_docx.rs
index 0dd395d08a..24b1c5eb98 100644
--- a/crates/export/src/as_docx.rs
+++ b/crates/export/src/as_docx.rs
@@ -150,7 +150,7 @@ pub(crate) const MAX_MEDIA: usize = 1000;
const MAX_MEDIA_BYTES: usize = 512 << 20;
/// Whether `im` can still be embedded: it has data, and the package stays within its limits.
-fn room_for(media: &[(String, &Image)], im: &Image) -> bool {
+pub(crate) fn room_for(media: &[(String, &Image)], im: &Image) -> bool {
!im.bytes.is_empty()
&& media.len() < MAX_MEDIA
&& media.iter().map(|(_, m)| m.bytes.len()).fold(im.bytes.len(), usize::saturating_add) <= MAX_MEDIA_BYTES
diff --git a/crates/export/src/as_html.rs b/crates/export/src/as_html.rs
index 7d70359edd..5d51e3b6fe 100644
--- a/crates/export/src/as_html.rs
+++ b/crates/export/src/as_html.rs
@@ -1,5 +1,5 @@
-use super::as_docx::{hex, rgb8};
-use super::layout::{Item, Page, Run, Table, items};
+use super::as_docx::{hex, rgb8, room_for};
+use super::layout::{Image, Item, Page, Run, Table, items};
/// Whether a character may appear in the output. XML 1.0 forbids the C0 controls other than tab,
/// line feed and carriage return, and U+FFFE/U+FFFF. Text extracted from PDFs often holds them
@@ -152,6 +152,7 @@ fn html_with_mode(pages: &[Page], title: &str, simple: bool, language: Option<&s
""
}
);
+ let mut embedded: Vec<(String, &Image)> = Vec::new();
for it in items(pages) {
match it {
Item::Para(runs, lvl) => {
@@ -160,10 +161,13 @@ fn html_with_mode(pages: &[Page], title: &str, simple: bool, language: Option<&s
}
}
Item::Table(t) => s.push_str(&table_html(&t, simple)),
- Item::Img(im) => {
+ // HTML embeds each placement in full, so repeated pictures also consume the budget.
+ Item::Img(im) if room_for(&embedded, im) => {
let mime = if im.ext == "jpg" { "image/jpeg" } else { "image/png" };
s.push_str(&format!("

\n", base64(&im.bytes)));
+ embedded.push((String::new(), im));
}
+ Item::Img(_) => {}
Item::PageBreak => s.push_str("
\n"),
}
}
diff --git a/crates/export/src/pptx.rs b/crates/export/src/pptx.rs
index abfdb833c4..59d9f858d9 100644
--- a/crates/export/src/pptx.rs
+++ b/crates/export/src/pptx.rs
@@ -1,7 +1,9 @@
//! PowerPoint presentation (.pptx, Office Open XML PresentationML): slides per page,
//! largest text as slide title, remaining paragraphs as body text, images and tables between them.
-use crate::as_docx::{core_properties_xml, image_mime};
+use std::collections::HashMap;
+
+use crate::as_docx::{core_properties_xml, image_mime, room_for};
use crate::{Image, Page, Run, Table, hex};
use craft_opc::{PackageOut, RT_BASE, RelOut, RelsOut};
use craft_xml::{DECL_STANDALONE, esc};
@@ -127,6 +129,8 @@ pub fn pptx(pages: &[Page], title: &str) -> Vec {
let content_w = sw - 2 * mx;
let mut global_media: Vec<(String, &Image)> = Vec::new();
+ // Shared picture bytes remain alive throughout export; only admitted media enter this map.
+ let mut media_of: HashMap<*const u8, usize> = HashMap::new();
let mut slides: Vec<(String, RelsOut)> = Vec::new(); // (slide_xml, slide_rels)
let body = crate::layout::body_size(pages);
@@ -201,9 +205,19 @@ pub fn pptx(pages: &[Page], title: &str) -> Vec {
current_y = current_y.saturating_add(box_h).saturating_add(height_emu(12.0));
}
SlideBodyItem::Img(im) => {
- let global_img_idx = global_media.len() + 1;
- let img_name = format!("image{global_img_idx}.{}", im.ext);
- global_media.push((img_name, *im));
+ let global_img_idx = match media_of.get(&im.bytes.as_ptr()) {
+ Some(&i) => i,
+ None if room_for(&global_media, im) => {
+ let n = global_media.len() + 1;
+ global_media.push((format!("image{n}.{}", im.ext), *im));
+ media_of.insert(im.bytes.as_ptr(), n);
+ n
+ }
+ None => {
+ i += 1;
+ continue;
+ }
+ };
let r_id = format!("rIdImg{}", slide_media_rels.len() + 1);
slide_media_rels.push((r_id.clone(), global_img_idx));
diff --git a/crates/export/src/pptx/tests.rs b/crates/export/src/pptx/tests.rs
index 2ed0235778..d703c682f3 100644
--- a/crates/export/src/pptx/tests.rs
+++ b/crates/export/src/pptx/tests.rs
@@ -403,3 +403,40 @@ fn pptx_opens_in_libreoffice() {
}
let _ = std::fs::remove_dir_all(&dir);
}
+
+/// A picture drawn in many places is one media part, referred to from each place; the package
+/// stays within the media limits however many places there are.
+#[test]
+fn pptx_packages_a_repeated_picture_once() {
+ let mut p = sample_page();
+ let one = p.images[0].clone();
+ 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");
+ 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");
+}
+
+/// Distinct allocations remain distinct media, even when their bytes match. Once the media
+/// budget is full, an existing picture remains usable on a later slide, but a new one is omitted.
+#[test]
+fn pptx_bounds_distinct_pictures_and_reuses_media_across_slides() {
+ let mut first = sample_page();
+ let one = first.images[0].clone();
+ first.images = (0..crate::as_docx::MAX_MEDIA).map(|_| Image { bytes: one.bytes.as_ref().to_vec().into(), ..one.clone() }).collect();
+ // Media names follow reading order, not the input vector. Give the reused picture a
+ // distinct top edge so it is unambiguously image1 rather than tying every rectangle.
+ first.images[0].rect[3] += 1.0;
+ let mut second = sample_page();
+ second.images =
+ vec![first.images[0].clone(), Image { bytes: one.bytes.as_ref().to_vec().into(), ..one.clone() }, Image { bytes: Vec::new().into(), ..one }];
+ let bytes = pptx(&[first, second], "Deck");
+ assert_eq!(part(&bytes, "ppt/slides/slide1.xml").matches("").count(), crate::as_docx::MAX_MEDIA);
+ assert_eq!(part(&bytes, "ppt/slides/slide2.xml").matches("").count(), 1);
+ let rels = part(&bytes, "ppt/slides/_rels/slide2.xml.rels");
+ assert_eq!(rels.matches("relationships/image\"").count(), 1);
+ assert!(rels.contains("Target=\"../media/image1.png\""));
+ assert!(part(&bytes, "ppt/slides/_rels/slide1.xml.rels").contains("../media/image1000.png"));
+}
diff --git a/crates/export/src/tests.rs b/crates/export/src/tests.rs
index 68d3a569ee..3bae8571d3 100644
--- a/crates/export/src/tests.rs
+++ b/crates/export/src/tests.rs
@@ -1442,3 +1442,15 @@ fn shared_base64_keeps_padded_unwrapped_export_bytes() {
assert!(!encoded.contains(['\r', '\n']));
assert!(encoded.ends_with("/w=="));
}
+
+/// Each place a picture is drawn embeds it whole in the HTML, so the places count against the
+/// media limits: past them, pictures are left out instead of multiplying one into gigabytes.
+#[test]
+fn html_bounds_the_pictures_it_embeds() {
+ let mut p = page();
+ let one = p.images[0].clone();
+ p.images = std::iter::repeat_n(one, MAX_MEDIA + 10).collect();
+ for h in [html(std::slice::from_ref(&p), "Report"), html_simple(std::slice::from_ref(&p), "Report")] {
+ assert_eq!(h.matches("![]()