mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-06 02:09:46 -06:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c5a8c92c40 | ||
|
|
ba61262eb0 |
+2
-2
File diff suppressed because one or more lines are too long
@@ -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
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user