Merge main into onee/fix-annot-tests

This commit is contained in:
FredApps committed 2026-11-18 17:14:34 +07:00
commit f8211b6aaf
5 files changed
+75 -8

No files matched your search

+1 -1
View File
@@ -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
+7 -3
View File
@@ -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
"<style>body{max-width:46em;margin:2em auto;font-family:sans-serif;line-height:1.45}img{max-width:100%}hr{border:0;border-top:1px solid #ccc;margin:2em 0}table{border-collapse:collapse;margin:1em 0}td,th{border:1px solid #999;padding:2px 6px;vertical-align:top}</style>"
}
);
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!("<p><img alt=\"\" src=\"data:{mime};base64,{}\"></p>\n", base64(&im.bytes)));
embedded.push((String::new(), im));
}
Item::Img(_) => {}
Item::PageBreak => s.push_str("<hr>\n"),
}
}
+18 -4
View File
@@ -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<u8> {
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<u8> {
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));
+37
View File
@@ -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("<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");
}
/// 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("<p:pic>").count(), crate::as_docx::MAX_MEDIA);
assert_eq!(part(&bytes, "ppt/slides/slide2.xml").matches("<p:pic>").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"));
}
+12
View File
@@ -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("<img").count(), MAX_MEDIA);
}
}