diff --git a/crates/metafile/src/import.rs b/crates/metafile/src/import.rs index dce77b2d7a..c87dd83d72 100644 --- a/crates/metafile/src/import.rs +++ b/crates/metafile/src/import.rs @@ -46,7 +46,8 @@ fn sane(r: Rect) -> bool { /// Is `path` one upright rectangle around the whole of `frame`? Writers clip to one first; it clips /// nothing in the picture. A path of more than one figure is not one rectangle, even when its -/// figures run along the sides of a box. +/// figures run along the sides of a box. Nor is one figure that runs back over its own edges, so +/// that it encloses less than the whole box. fn around(path: &BezPath, frame: Rect) -> bool { if path.elements().len() > 6 { return false; @@ -58,6 +59,7 @@ fn around(path: &BezPath, frame: Rect) -> bool { // neighbor (corners are in order around the box), never across it. let along = |i: usize, j: usize| (i + 4 - j) % 4 != 2; let mut seen = [false; 4]; + let mut points = Vec::with_capacity(6); let (mut first, mut last) = (None, None); for e in path.elements() { let (p, start) = match e { @@ -77,12 +79,19 @@ fn around(path: &BezPath, frame: Rect) -> bool { } last = Some(i); seen[i] = true; + points.push(corners[i]); } if let (Some(f), Some(l)) = (first, last) && !along(f, l) { return false; } + // Every edge runs along a side, so the figure winds the same number of times around every + // point inside the box: once, when its area is the box's. + let twice_area: f64 = points.iter().zip(points.iter().cycle().skip(1)).map(|(p, q)| (*p - corners[0]).cross(*q - corners[0])).sum(); + if (twice_area.abs() - 2.0 * b.area()).abs() > 1e-6 * (1.0 + 2.0 * b.area()) { + return false; + } let b = b.inflate(0.5, 0.5); seen.iter().all(|s| *s) && b.contains(frame.origin()) && b.contains(Point::new(frame.x1, frame.y1)) } diff --git a/crates/metafile/src/tests/import.rs b/crates/metafile/src/tests/import.rs index a1ddcc335e..55714c1f71 100644 --- a/crates/metafile/src/tests/import.rs +++ b/crates/metafile/src/tests/import.rs @@ -539,7 +539,11 @@ fn a_clip_whose_box_holds_the_picture_still_clips() { // Two open figures through the four corners, the top edge and the bottom edge, which enclose // nothing: MOVETOEX, LINETO, MOVETOEX, LINETO. let edges = vec![(27, le(&[-500, -500])), (54, le(&[5500, -500])), (27, le(&[5500, 3500])), (54, le(&[-500, 3500]))]; - for clip in [vec![(3, diamond)], vec![(3, bow_tie)], edges] { + // One figure through the four corners that runs back over its edges and encloses nothing: + // POLYLINE top-left, top-right, top-left, bottom-left, bottom-right, bottom-left. + let mut retraced = le(&[0, 0, 0, 0, 6]); + retraced.extend(le(&[-500, -500, 5500, -500, -500, -500, -500, 3500, 5500, 3500, -500, 3500])); + for clip in [vec![(3, diamond)], vec![(3, bow_tie)], edges, vec![(4, retraced)]] { let records = [vec![(59, vec![])], clip, vec![(60, vec![]), (67, le(&[5])), (43, le(&[0, 0, 5000, 3000]))]].concat(); let back = import(&hand_emf(5000, 3000, &records)).unwrap(); let layer = back.document.layers[0].children().unwrap();