Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Measure: snap to the implicit closing edge of filled paths (#803) (#862)

MarekSpodBiedry8ae9f6be8b67
diff --git a/crates/measure/src/snap.rs b/crates/measure/src/snap.rs
index f9eeb92d4..46455dcf4 100644
--- a/crates/measure/src/snap.rs
+++ b/crates/measure/src/snap.rs
@@ -321,7 +321,8 @@ impl Walker<'_> {
}
}
"S" | "s" | "f" | "F" | "f*" | "B" | "B*" | "b" | "b*" => {
- if matches!(String::from_utf8_lossy(&op.op).as_ref(), "s" | "b" | "b*")
+ // Fills close open subpaths implicitly (ISO 32000-2 8.5.3.3); only a bare stroke leaves the path open.
+ if String::from_utf8_lossy(&op.op).as_ref() != "S"
&& let (Some(a), Some(b)) = (current, start)
{
path.edge(a, b);
diff --git a/crates/measure/src/tests.rs b/crates/measure/src/tests.rs
index 8cd701d05..1348a3ec3 100644
--- a/crates/measure/src/tests.rs
+++ b/crates/measure/src/tests.rs
@@ -120,6 +120,16 @@ fn rejects_invalid_geometry_and_unsupported_scales() {
assert!(set_scale(&mut d, 0, [0.0; 4], "bad", &Scale::default()).is_err());
}
#[test]
+fn filled_open_triangle_snaps_to_closing_edge() {
+ for (op, closed) in [("f", true), ("F", true), ("f*", true), ("B", true), ("B*", true), ("h f", true), ("S", false)] {
+ let d = fixture(&format!("40 50 m 160 50 l 100 170 l {op}"), "", &[]);
+ let paths = snap::geometry(&d, 0).unwrap();
+ assert_eq!(paths.segments.len(), if closed { 3 } else { 2 }, "{op}");
+ let hit = paths.snap([70.0, 110.0], 5.0, snap::SnapOptions::default()).unwrap();
+ assert_eq!(hit.is_some(), closed, "{op}");
+ }
+}
+#[test]
fn snap_paths_endpoints_midpoints_and_intersections() {
let d = fixture("10 20 m 110 20 l S 60 0 m 60 80 l S 10 100 100 40 re S", "", &[]);
let paths = snap::geometry(&d, 0).unwrap();