Compare commits

..
Author SHA1 Message Date
Dylan M. Taylor c5a8c92c40 style: rustfmt 2026-11-03 16:31:02 +00:00
Dylan M. Taylor ba61262eb0 fix(metafile): hold a placed pen width to what covers the frame 2026-11-03 11:50:57 +00:00
5 changed files with 40 additions and 36 deletions
+2 -2
View File
File diff suppressed because one or more lines are too long
+1 -31
View File
@@ -179,18 +179,9 @@ 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", &[]);
@@ -212,7 +203,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..if pad { cols } else { row.len().max(1) } {
for c in 0..cols {
arg(w, "m:e", row.get(c).map(|v| v.as_slice()).unwrap_or(&[]), depth);
}
w.close("m:mr");
@@ -489,24 +480,3 @@ 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);
}
}
+8 -2
View File
@@ -136,8 +136,14 @@ impl Picture {
match item {
Item::Path { segs, fill, stroke, even_odd } => {
let path = tf * bez(segs);
if in_range(path.bounding_box()) {
let stroke = stroke.map(|(c, w)| (c, f64::from(w) * k));
let bounds = path.bounding_box();
if in_range(bounds) {
// A pen wider than twice the distance across the frame and the path
// already covers the whole frame: wider adds nothing the frame shows, only
// work for the renderer flattening its round caps (the width is file data).
let u = bounds.union(r);
let widest = 2.0 * u.width().hypot(u.height());
let stroke = stroke.map(|(c, w)| (c, (f64::from(w) * k).min(widest)));
out.push(PlacedItem::Path { path, fill: *fill, stroke, even_odd: *even_odd });
}
}
+28
View File
@@ -891,6 +891,34 @@ fn place_fits_the_frame_scales_strokes_and_drops_far_paths() {
assert_eq!(stroke.map(|s| s.1), Some(8.0));
}
#[test]
fn a_huge_pen_is_held_to_what_covers_the_frame() {
// A pen of 1e30 units on a short path: round caps that wide took the renderer forever to
// flatten. Past twice the distance across the frame and the path it covers no more.
let path = |w: f32| crate::Picture {
width: 100.0,
height: 50.0,
items: vec![Item::Path {
segs: vec![Seg::Move(0.0, 0.0), Seg::Line(10.0, 0.0)],
fill: None,
stroke: Some(([0, 0, 0, 255], w)),
even_odd: true,
}],
};
let width = |w: f32| match pic_place(&path(w)).first() {
Some(crate::PlacedItem::Path { stroke, .. }) => stroke.map(|s| s.1),
_ => None,
};
let cap = 2.0 * 200f64.hypot(100.0);
assert_eq!(width(1e30), Some(cap));
assert_eq!(width(f32::MAX), Some(cap));
assert_eq!(width(4.0), Some(8.0), "an ordinary pen is untouched");
}
fn pic_place(pic: &crate::Picture) -> Vec<crate::PlacedItem> {
pic.place(kurbo::Rect::new(0.0, 0.0, 200.0, 100.0), [0.0; 4])
}
/// #1899: a crop keeps what the file says down to a sliver (a header's bar is 2.7% of a page-sized
/// metafile), and junk sides give a finite part of the source with some area.
#[test]
+1 -1
View File
File diff suppressed because one or more lines are too long