Skip to content

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

fix(comments): the eraser's size sets how wide it rubs (#3747)

Lombardi200315a0b696c51f
diff --git a/crates/ui-egui/src/comments.rs b/crates/ui-egui/src/comments.rs
index 7fd9b7a62..ee5214fca 100644
--- a/crates/ui-egui/src/comments.rs
+++ b/crates/ui-egui/src/comments.rs
@@ -1268,7 +1268,9 @@ fn paint_draw(painter: &egui::Painter, cx: &PageCx<'_>, tool: &CommentTool, poin
painter.add(egui::Shape::line(pts, stroke));
}
CommentTool::Eraser => {
- painter.add(egui::Shape::line(pts, Stroke::new(12.0, Color32::from_gray(128).gamma_multiply(0.35))));
+ // As wide as what it rubs out (see `erase`).
+ let width = (2.0 * eraser_radius_px(style.width)) as f32;
+ painter.add(egui::Shape::line(pts, Stroke::new(width, Color32::from_gray(128).gamma_multiply(0.35))));
}
CommentTool::Line | CommentTool::Arrow if pts.len() == 2 => {
painter.line_segment([pts[0], pts[1]], stroke);
@@ -1334,10 +1336,18 @@ fn drawn_shape(tool: CommentTool, points: &[[f64; 2]]) -> Option<Shape> {
}
}
-/// Eraser: rub out drawings along `path` (user space); one undo step for all of them.
+/// The eraser's radius on screen, in pixels, from its thickness setting (#3747): three pixels
+/// per point, so the default 2 pt keeps the 12-pixel eraser it always had, and the thickness
+/// slider's 0.5–12 pt gives 3 to 72 pixels across. A setting that isn't a number is the default.
+fn eraser_radius_px(width: f64) -> f64 {
+ 3.0 * if width.is_finite() { width.clamp(0.5, 12.0) } else { 2.0 }
+}
+
+/// Eraser: rub out drawings along `path` (user space); one undo step for all of them. It rubs
+/// as wide on screen as its preview, whatever the zoom.
fn erase(cx: &PageCx<'_>, path: &[[f64; 2]]) -> Option<Edit> {
let zoom = (cx.xf.rect.width() / cx.xf.pw.max(1.0)) as f64;
- let radius = (6.0 / zoom).max(1.0);
+ let radius = (eraser_radius_px(cx.prefs.style(CommentTool::Eraser).width) / zoom).max(1.0);
let b = path.iter().fold([f64::MAX, f64::MAX, f64::MIN, f64::MIN], |r, p| [r[0].min(p[0]), r[1].min(p[1]), r[2].max(p[0]), r[3].max(p[1])]);
let mut hits: Vec<usize> = cx
.comments()
diff --git a/crates/ui-egui/tests/comments.rs b/crates/ui-egui/tests/comments.rs
index 01166a41d..55d699bd4 100644
--- a/crates/ui-egui/tests/comments.rs
+++ b/crates/ui-egui/tests/comments.rs
@@ -1395,6 +1395,27 @@ fn erasing_part_of_a_drawing() {
assert!(comments(&h).is_empty(), "{:?}", comments(&h));
}
+#[test]
+fn the_eraser_rubs_as_wide_as_its_thickness_setting() {
+ // #3747: the eraser's size changed nothing; it always rubbed 6 pixels either side.
+ let stroke_near = |width: f64| {
+ let mut h = harness(|app| app.set_option("quick", "ink").unwrap());
+ drag_pt(&mut h, (40.0, 60.0), (260.0, 60.0));
+ assert_eq!(comments(&h).len(), 1);
+ let zoom = h.state().views[0].page_screen_rect(0).expect("page 1 on screen").width() / 300.0;
+ let app = h.state_mut();
+ app.comment_prefs.set_width(pdfcraft_ui_egui::comments::CommentTool::Eraser, width);
+ app.set_option("quick", "eraser").unwrap();
+ // Along the whole drawing, 20 pixels above it: out of reach of the default eraser
+ // (6 pixels), within reach of the largest (36).
+ let y = 60.0 + 20.0 / zoom;
+ drag_pt(&mut h, (30.0, y), (270.0, y));
+ comments(&h).len()
+ };
+ assert_eq!(stroke_near(2.0), 1, "the default eraser passes by");
+ assert_eq!(stroke_near(12.0), 0, "the largest rubs the drawing out");
+}
+
#[test]
fn hovering_a_comment_shows_its_author_and_text() {
// A square comment by Ada over the first page's text.
diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml
index 7aca7d5c8..c6a9a0689 100644
--- a/parity/acrobat-features.toml
+++ b/parity/acrobat-features.toml
@@ -3704,8 +3704,8 @@ milestone = "M5"
status = "shipped"
commands = ["comment.eraser"]
tools = []
-evidence = ["crates/annot/src/tests.rs::the_eraser_cuts_strokes_and_removes_empty_drawings", "crates/ui-egui/tests/comments.rs::erasing_part_of_a_drawing"]
-notes = "cuts strokes where the eraser passes; an emptied drawing is deleted; one undo step"
+evidence = ["crates/annot/src/tests.rs::the_eraser_cuts_strokes_and_removes_empty_drawings", "crates/ui-egui/tests/comments.rs::erasing_part_of_a_drawing", "crates/ui-egui/tests/comments.rs::the_eraser_rubs_as_wide_as_its_thickness_setting"]
+notes = "cuts strokes where the eraser passes; an emptied drawing is deleted; one undo step. Its thickness setting sizes it on screen (3 to 72 pixels across; the default 2 pt is 12), preview included (#3747)"
[[feature]]
id = "comment.line"