Compare commits

..
7 changed files with 77 additions and 30 deletions
+2 -2
View File
File diff suppressed because one or more lines are too long
+5 -25
View File
@@ -353,11 +353,11 @@ impl super::Writer<'_> {
};
self.embeds.markup_bytes = left;
let mut root = xml::parse(src.xml.as_bytes()).ok()?;
let mut used = HashSet::new();
let mut used = Vec::new();
rel_attr_values(&root, &mut used, 0);
// Every relationship the markup uses must be writable, or the object would be broken.
let writable: HashSet<&str> = src.refs.iter().filter(|r| r.external || self.embeds.available(&r.target)).map(|r| r.id.as_str()).collect();
if !used.iter().all(|id| writable.contains(id.as_str())) {
let writable = |id: &String| src.refs.iter().any(|r| r.id == *id && (r.external || self.embeds.available(&r.target)));
if !used.iter().all(writable) {
log::warn!("docx: an embedded object's parts are missing; it is left out");
return None;
}
@@ -637,15 +637,13 @@ fn set_attr(e: &mut El, k: &str, v: &str) {
}
}
/// The distinct relationship ids `e` uses: a set, so a large tree (a native document's markup
/// never went through the reader's id limit) costs no scan per attribute.
fn rel_attr_values(e: &El, out: &mut HashSet<String>, depth: usize) {
fn rel_attr_values(e: &El, out: &mut Vec<String>, depth: usize) {
if depth > MAX_DEPTH {
return;
}
for (k, v) in &e.attrs {
if xml::is_rel_attr(&e.name, k) && !out.contains(v) {
out.insert(v.clone());
out.push(v.clone());
}
}
for c in e.els() {
@@ -853,24 +851,6 @@ fn valid_part_name(name: &str) -> String {
mod tests {
use super::*;
#[test]
fn relationship_ids_are_collected_once_each() {
// A native document's embedded markup skips the reader's id limit: 40,000 distinct ids
// (each named twice) were a scan of every id collected so far, per attribute.
let mut xml = String::from(
r#"<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">"#,
);
for i in 0..40_000 {
xml.push_str(&format!(r#"<w:r r:id="rId{i}"/><w:r r:id="rId{i}"/>"#));
}
xml.push_str("</w:p>");
let root = xml::parse(xml.as_bytes()).unwrap();
let mut used = HashSet::new();
rel_attr_values(&root, &mut used, 0);
assert_eq!(used.len(), 40_000);
assert!(used.contains("rId39999"));
}
#[test]
fn charts_cut_to_the_limits_on_save_say_so_once() {
// A model deserialised as is can hold more than a chart part keeps; the save left the rest
+1
View File
@@ -77,6 +77,7 @@ fn write_attempt(doc: &Document, lay: &DocLayout, opts: &PdfOptions, pages: &[us
cmaps: HashMap::new(),
images: HashMap::new(),
vectors: HashMap::new(),
vector_bytes_left: crate::exporter::MAX_CACHED_VECTOR_BYTES,
tags: Vec::new(),
tag_index: HashMap::new(),
links: Vec::new(),
+16 -2
View File
@@ -15,6 +15,9 @@ mod draw;
mod nav;
mod text;
/// Most bytes of parsed metafiles (geometry, text and pixels) one export keeps between pages.
pub(crate) const MAX_CACHED_VECTOR_BYTES: usize = 256 << 20;
pub(crate) struct Exporter<'a> {
pub(crate) doc: &'a Document,
pub(crate) lay: &'a DocLayout,
@@ -30,6 +33,8 @@ pub(crate) struct Exporter<'a> {
pub(crate) images: HashMap<String, Option<Image>>,
/// Parsed metafiles by media key (`None`: not readable).
pub(crate) vectors: HashMap<String, Option<Arc<Vector>>>,
/// Bytes of parsed metafiles `vectors` may still keep ([`MAX_CACHED_VECTOR_BYTES`]).
pub(crate) vector_bytes_left: usize,
pub(crate) tags: Vec<TagEntry>,
pub(crate) tag_index: HashMap<(StoryRef, DocPath), usize>,
/// Link rectangles on the current page: (x0, y0, x1, y1, target).
@@ -84,7 +89,7 @@ impl Exporter<'_> {
/// The metafile picture for a media key, parsed once. None when the bytes are not a metafile
/// (the raster path takes them) or do not parse (the raster path then draws the grey box).
fn vector(&mut self, key: &str) -> Option<Arc<Vector>> {
pub(crate) fn vector(&mut self, key: &str) -> Option<Arc<Vector>> {
let doc = self.doc;
let bytes = doc.media.get(key)?;
if !wordcraft_metafile::is_metafile(bytes) {
@@ -97,7 +102,16 @@ impl Exporter<'_> {
if v.is_none() {
log::warn!("PDF: metafile `{key}` could not be read");
}
self.vectors.insert(key.to_string(), v.clone());
// Kept for the pages that draw it again while the cache has room; past that it is parsed
// again when it is next drawn, rather than every metafile's geometry piling up.
let size = v.as_ref().map_or(0, |v| v.heap_bytes());
match self.vector_bytes_left.checked_sub(size) {
Some(left) => {
self.vector_bytes_left = left;
self.vectors.insert(key.to_string(), v.clone());
}
None => log::debug!("PDF: metafile `{key}` ({size} bytes parsed) is not kept between pages"),
}
v
}
+17
View File
@@ -14,6 +14,23 @@ pub(crate) struct Vector {
images: Vec<Option<Image>>,
}
impl Vector {
/// Bytes the parsed picture holds: its paths' segments, its text and its bitmaps' pixels.
pub(crate) fn heap_bytes(&self) -> usize {
let item = |it: &wordcraft_metafile::Item| match it {
wordcraft_metafile::Item::Path { segs, .. } => segs.len().saturating_mul(std::mem::size_of::<wordcraft_metafile::Seg>()),
wordcraft_metafile::Item::Bitmap { width, height, .. } => (*width as usize).saturating_mul(*height as usize).saturating_mul(4),
wordcraft_metafile::Item::Text(t) => t.text.len().saturating_add(t.font.len()),
};
let items = self
.pic
.items
.iter()
.fold(self.pic.items.len().saturating_mul(std::mem::size_of::<wordcraft_metafile::Item>()), |n, it| n.saturating_add(item(it)));
items.saturating_add(self.images.len().saturating_mul(std::mem::size_of::<Option<Image>>()))
}
}
/// Parses a metafile and embeds its bitmaps. None when the picture has no usable size.
pub(crate) fn build_vector(bytes: &[u8]) -> Option<Vector> {
let mut pic = wordcraft_metafile::parse(bytes).ok()?;
+35
View File
@@ -1367,6 +1367,7 @@ fn bookmark_destination_is_the_page_of_its_start() {
cmaps: HashMap::new(),
images: HashMap::new(),
vectors: HashMap::new(),
vector_bytes_left: crate::exporter::MAX_CACHED_VECTOR_BYTES,
tags: Vec::new(),
tag_index: HashMap::new(),
links: Vec::new(),
@@ -2488,3 +2489,37 @@ fn baseline_is_the_most_shared_y() {
assert_eq!(baseline(&[(1, 0.0, 9.0), (2, 3.0, 4.0)]), Some(4.0));
assert_eq!(baseline(&[(1, 0.0, -0.0), (2, 3.0, 0.0)]), Some(0.0));
}
#[test]
fn parsed_metafiles_are_kept_only_within_the_cache_budget() {
let mut d = Document::new();
let media = d.add_media(red_right_half_emf(), "emf");
let lay = layout(&d, &mut LayoutCache::new(), &LayoutOptions::default());
let outlined = HashSet::new();
let mut ex = Exporter {
doc: &d,
lay: &lay,
opts: &PdfOptions::default(),
out_index: HashMap::new(),
fonts: HashMap::new(),
font_error: None,
outlined: &outlined,
cmaps: HashMap::new(),
images: HashMap::new(),
vectors: HashMap::new(),
vector_bytes_left: crate::exporter::MAX_CACHED_VECTOR_BYTES,
tags: Vec::new(),
tag_index: HashMap::new(),
links: Vec::new(),
bookmarks: std::cell::OnceCell::new(),
};
let v = ex.vector(&media).expect("parses");
assert!(v.heap_bytes() > 0);
assert_eq!(ex.vectors.len(), 1, "kept while the budget has room");
assert_eq!(ex.vector_bytes_left, crate::exporter::MAX_CACHED_VECTOR_BYTES - v.heap_bytes());
// With the budget spent, a metafile is still drawn, just not kept.
ex.vectors.clear();
ex.vector_bytes_left = 0;
assert!(ex.vector(&media).is_some());
assert!(ex.vectors.is_empty());
}
+1 -1
View File
File diff suppressed because one or more lines are too long