mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-04 01:10:37 -06:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b48e4c7a47 |
+2
-2
File diff suppressed because one or more lines are too long
@@ -179,9 +179,18 @@ fn char_prop(w: &mut W, name: &str, c: Option<char>) {
|
||||
val(w, name, &s);
|
||||
}
|
||||
|
||||
/// Most cells a matrix is padded out to (rows × its widest row). A ragged matrix past it (a
|
||||
/// native document's, or one parsed from a few bytes of text) writes each row as it is, rather
|
||||
/// than output that grows with the square of its size.
|
||||
const MAX_PADDED_CELLS: usize = 1 << 20;
|
||||
|
||||
fn write_matrix(w: &mut W, n: &MNode, depth: usize) {
|
||||
let MNode::Matrix { rows, col_jc } = n else { return };
|
||||
let cols = rows.iter().map(|r| r.len()).max().unwrap_or(0).max(1);
|
||||
let pad = rows.len().saturating_mul(cols) <= MAX_PADDED_CELLS;
|
||||
if !pad {
|
||||
log::warn!("docx: a {} × {cols} matrix is too large to pad; its rows are written as they are", rows.len());
|
||||
}
|
||||
w.open("m:m", &[]);
|
||||
w.open("m:mPr", &[]);
|
||||
w.open("m:mcs", &[]);
|
||||
@@ -203,7 +212,7 @@ fn write_matrix(w: &mut W, n: &MNode, depth: usize) {
|
||||
w.close("m:mPr");
|
||||
for row in rows {
|
||||
w.open("m:mr", &[]);
|
||||
for c in 0..cols {
|
||||
for c in 0..if pad { cols } else { row.len().max(1) } {
|
||||
arg(w, "m:e", row.get(c).map(|v| v.as_slice()).unwrap_or(&[]), depth);
|
||||
}
|
||||
w.close("m:mr");
|
||||
@@ -480,3 +489,24 @@ fn run(w: &mut W, r: &MRun) {
|
||||
w.leaf("m:t", &[("xml:space", "preserve")], &r.text);
|
||||
w.close("m:r");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn cells(rows: Vec<Vec<Arg>>) -> usize {
|
||||
let mut w = W::new();
|
||||
write_matrix(&mut w, &MNode::Matrix { rows, col_jc: Vec::new() }, 0);
|
||||
w.s.matches("<m:e/>").count() + w.s.matches("<m:e>").count()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_ragged_matrix_is_padded_within_a_cell_budget() {
|
||||
// A small ragged matrix is padded to a rectangle, as before.
|
||||
assert_eq!(cells(vec![vec![Vec::new(); 3], vec![Vec::new()]]), 6);
|
||||
// 2,000 one-cell rows and one 2,000-cell row: 4 million cells padded, past the budget.
|
||||
let mut rows = vec![vec![Vec::new()]; 2000];
|
||||
rows.push(vec![Vec::new(); 2000]);
|
||||
assert_eq!(cells(rows), 4000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,6 @@ 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(),
|
||||
|
||||
@@ -15,9 +15,6 @@ 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,
|
||||
@@ -33,8 +30,6 @@ 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).
|
||||
@@ -89,7 +84,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).
|
||||
pub(crate) fn vector(&mut self, key: &str) -> Option<Arc<Vector>> {
|
||||
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) {
|
||||
@@ -102,16 +97,7 @@ impl Exporter<'_> {
|
||||
if v.is_none() {
|
||||
log::warn!("PDF: metafile `{key}` could not be read");
|
||||
}
|
||||
// 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"),
|
||||
}
|
||||
self.vectors.insert(key.to_string(), v.clone());
|
||||
v
|
||||
}
|
||||
|
||||
|
||||
@@ -14,23 +14,6 @@ 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()?;
|
||||
|
||||
@@ -1367,7 +1367,6 @@ 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(),
|
||||
@@ -2489,37 +2488,3 @@ 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
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user