CDR import: bound the walks of clipped groups and stop copying refused dashes

Every chunk walked inside a clipped group now counts toward a new
clipped_chunks limit (2,000,000), each time a container names the group,
empty groups included. Containers that all name one group of empty
groups took time in the product of the two counts; past the limit a
container's group is left out, with its guidelines, and the container
stays as a plain shape with the limit warning.

An outline record refused at the dash limit drops its dashes from the
cache, so later shapes that use it take it solid without copying the
dash list.
This commit is contained in:
dotCooCoo
2026-11-12 19:04:31 -08:00
parent c2789fff2a
commit daeb83d2ee
3 changed files with 100 additions and 9 deletions
+58 -8
View File
@@ -55,6 +55,8 @@ struct Ctx<'a, 'b> {
clips: HashMap<u32, &'b Chunk>,
/// The clipped groups being read, innermost last.
open_clips: Vec<u32>,
/// Whether the walk of a clipped group stopped at the clipped chunk limit.
clip_cut: bool,
}
/// The most clipped groups read one inside another: VectorCraft's own limit.
@@ -93,6 +95,7 @@ pub(crate) fn drawing(ix: &Index, budget: &mut Budget, warn: &mut Vec<Warning>)
styles,
clips: HashMap::new(),
open_clips: Vec::new(),
clip_cut: false,
};
for g in found.clpt.iter().flat_map(|c| ix.children(c)).filter(|g| g.is_list(b"grp ")) {
if let Some(id) = ix.children(g).iter().find(|c| &c.id == b"spnd").and_then(|c| Rd::new(ix.data(c)).u32()) {
@@ -366,6 +369,13 @@ impl<'a, 'b> Ctx<'a, 'b> {
continue;
};
top.next += 1;
// Every chunk of a clipped group counts, empty groups included, each time a container
// names the group; past the limit the group is left out.
if !self.open_clips.is_empty() && !self.budget.take_clipped_chunk() {
push(self.warn, Warning::TooDeep);
self.clip_cut = true;
return Vec::new();
}
if c.is_list(b"obj ") {
if let Some(o) = self.object(c, guides, &outer) {
top.objects.push(o);
@@ -471,8 +481,9 @@ impl<'a, 'b> Ctx<'a, 'b> {
}
/// `container` and the objects of the clipped group `id`, which keep their own transforms.
/// A group that isn't there is damage; one already being read, or past the depth limit, is
/// left out with the limit warning. Either way the container stays, as a plain shape.
/// A group that isn't there is damage; one already being read, past the depth limit or past
/// the chunks clipped groups may walk is left out with the limit warning, the guidelines it
/// holds included. Either way the container stays, as a plain shape.
fn clip(&mut self, container: Box<Shape>, id: u32, guides: &mut Vec<Guideline>) -> Object {
let Some(&group) = self.clips.get(&id) else {
push(self.warn, Warning::Damaged);
@@ -483,9 +494,13 @@ impl<'a, 'b> Ctx<'a, 'b> {
return Object::Shape(container);
}
self.open_clips.push(id);
let ix = self.ix;
let (ix, mark) = (self.ix, guides.len());
let objects = self.objects(ix.children(group), guides);
self.open_clips.pop();
if std::mem::take(&mut self.clip_cut) {
guides.truncate(mark);
return Object::Shape(container);
}
if objects.is_empty() {
return Object::Shape(container);
}
@@ -616,12 +631,14 @@ impl<'a, 'b> Ctx<'a, 'b> {
});
self.outlines.insert(id, read);
}
let o = self.outlines.get(&id)?.as_ref()?;
if o.dashes.is_empty() || self.budget.take_dashes(o.dashes.len()) {
return Some(o.clone());
let o = self.outlines.get_mut(&id)?.as_mut()?;
// The dashes left only shrink, so an outline refused once is refused for every later
// shape: its dashes are dropped from the cache, and later shapes take it solid.
if !o.dashes.is_empty() && !self.budget.take_dashes(o.dashes.len()) {
o.dashes = Vec::new();
push(self.warn, Warning::TooDeep);
}
push(self.warn, Warning::TooDeep);
Some(Outline { dashes: Vec::new(), ..o.clone() })
Some(o.clone())
}
}
@@ -659,4 +676,37 @@ mod tests {
assert_eq!(layer_kind([0x48, 0, 1, 0x98], false), LayerKind::Desktop);
assert_eq!(layer_kind([0x4a, 1, 1, 0x98], true), LayerKind::Guides);
}
/// An outline record refused at the dash limit keeps no dashes in the cache, so the shapes
/// that use it later copy none.
#[test]
fn an_outline_past_the_dash_limit_keeps_no_dashes() {
let v = crate::synth::V(1100);
let pen = crate::synth::Pen { line_type: 4, width: 1.0, cap: 0, join: 0 };
let record = v.outline(1, pen, &crate::synth::color(5, [0, 0, 0, 255]), &[3, 1, 3]);
let file = crate::synth::riff(1100, &[]);
let mut budget = Budget::new(crate::Limits { dashes: 4, ..crate::Limits::default() });
let mut warn = Vec::new();
let ix = crate::riff::Index::build(&file, None, &mut budget, &mut warn).unwrap();
let mut cx = Ctx {
ix: &ix,
p: layout::for_version(1100).unwrap(),
budget: &mut budget,
warn: &mut warn,
fill_chunks: HashMap::new(),
outline_chunks: HashMap::from([(1, record.as_slice())]),
fills: HashMap::new(),
outlines: HashMap::new(),
styles: Ok(Styles::default()),
clips: HashMap::new(),
open_clips: Vec::new(),
clip_cut: false,
};
assert_eq!(cx.outline(1).map(|o| o.dashes.len()), Some(3));
for _ in 0..3 {
assert_eq!(cx.outline(1).map(|o| o.dashes.len()), Some(0));
assert_eq!(cx.outlines.get(&1).and_then(|o| o.as_ref()).map(|o| o.dashes.len()), Some(0));
}
assert_eq!(*cx.warn, vec![Warning::TooDeep]);
}
}
+16 -1
View File
@@ -26,6 +26,9 @@ pub struct Limits {
/// Dashes of all outlines together, an outline's counted again for every shape that uses it.
/// Past them a shape's outline is solid, with a warning.
pub dashes: usize,
/// Chunks walked inside clipped groups, all containers together, a group's counted again for
/// every container that names it. Past them a container's group is left out, with a warning.
pub clipped_chunks: usize,
}
#[cfg(not(target_arch = "wasm32"))]
@@ -45,6 +48,7 @@ impl Default for Limits {
coordinates: 16_000_000,
arguments: 64,
dashes: 16_000_000,
clipped_chunks: 2_000_000,
}
}
@@ -61,6 +65,7 @@ impl Default for Limits {
coordinates: 16_000_000,
arguments: 64,
dashes: 8_000_000,
clipped_chunks: 2_000_000,
}
}
}
@@ -74,13 +79,14 @@ pub(crate) struct Budget {
objects: usize,
coordinates: usize,
dashes: usize,
clipped_chunks: usize,
/// A request for coordinates was refused since [`Budget::coordinates_refused`] last asked.
refused: bool,
}
impl Budget {
pub(crate) fn new(limits: Limits) -> Self {
Budget { limits, inflated: 0, chunks: 0, objects: 0, coordinates: 0, dashes: 0, refused: false }
Budget { limits, inflated: 0, chunks: 0, objects: 0, coordinates: 0, dashes: 0, clipped_chunks: 0, refused: false }
}
/// Whether a request for coordinates was refused since the last call, which tells a shape
@@ -147,4 +153,13 @@ impl Budget {
None => false,
}
}
/// One more chunk walked inside a clipped group, or `false` past the limit.
pub(crate) fn take_clipped_chunk(&mut self) -> bool {
if self.clipped_chunks >= self.limits.clipped_chunks {
return false;
}
self.clipped_chunks += 1;
true
}
}
+26
View File
@@ -1326,6 +1326,32 @@ fn a_missing_or_repeating_clipped_group_keeps_the_container() {
assert_eq!(d.warnings, vec![Warning::TooDeep]);
}
/// Each chunk of a clipped group counts toward the clipped chunk limit every time a container
/// names the group, empty groups included; past it the containers stay as plain shapes, and a
/// group cut off at the limit leaves out its guidelines too.
#[test]
fn containers_naming_one_group_stop_at_the_clipped_chunk_limit() {
let v = V(1100);
// The group walks 53 chunks: its `spnd`, a guideline, 50 empty groups and a rectangle.
let mut guide = vec![0; 12];
guide.extend(90_000_000i32.to_le_bytes());
guide.extend([v.coord(36.0), v.coord(0.0)].concat());
let mut contents = vec![synth::object(v, 0x16, &[(16000, guide)], None)];
contents.extend((0..50).map(|_| list(b"grp ", vec![])));
contents.push(clip_rect(v, 10.0, &[], [0.0, 0.0]));
let containers: Vec<synth::Node> = (0..20).map(|_| clip_rect(v, 40.0, &[synth::container(0x77)], [0.0, 0.0])).collect();
let file = clip_drawing(1100, vec![(0x77, contents)], containers);
let count = |objects: &[Object]| objects.iter().filter(|o| matches!(o, Object::Clip { .. })).count();
let d = read(&file, Limits::default()).unwrap();
assert_eq!((count(&d.pages[0].layers[0].objects), d.pages[0].layers[0].guides.len()), (20, 20));
assert!(d.warnings.is_empty(), "{:?}", d.warnings);
// The sixth container's walk stops after its guideline and 8 empty groups.
let d = read(&file, Limits { clipped_chunks: 53 * 5 + 10, ..Limits::default() }).unwrap();
let layer = &d.pages[0].layers[0];
assert_eq!((layer.objects.len(), count(&layer.objects), layer.guides.len()), (20, 5, 5));
assert_eq!(d.warnings, vec![Warning::TooDeep]);
}
/// A container, fill, outline or style argument too short for the id it names is damage: the
/// shape comes in as a plain shape without that id.
#[test]