PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(edit): resizing an image under a shared clip scales its own clip too (#4639)
Hitesh6b9785eb004b
diff --git a/crates/edit/src/images.rs b/crates/edit/src/images.rsindex 6f14271be..5180e7601 100644--- a/crates/edit/src/images.rs+++ b/crates/edit/src/images.rs@@ -539,6 +539,11 @@ pub(crate) fn selectable_images(/// Find the saved graphics-state scope containing just this image and its crop. Applying/// the transform before that scope moves the actual clipping path as well as the pixels./// A shared clip must never be moved along with unrelated artwork or text.+///+/// Under a clip set outside every scope that holds only this image (a report or table drawing+/// each picture as `q re W n cm Do Q` inside a section's clip), no scope contains every active+/// clip. The outermost scope holding only the image is used then: it contains all of the+/// image's private clips, which move with it, while the outer clip is shared and stays.fn image_transform_scope(ops: &[Op], target: usize) -> Result<Option<(usize, usize, Matrix)>, EditError> {struct Scope {start: usize,@@ -556,6 +561,7 @@ fn image_transform_scope(ops: &[Op], target: usize) -> Result<Option<(usize, usilet mut text = false;let mut at_image = None;let mut isolated = None;+ let mut outermost = None;for (index, op) in ops.iter().enumerate() {match op.op.as_slice() {b"q" => {@@ -566,9 +572,13 @@ fn image_transform_scope(ops: &[Op], target: usize) -> Result<Option<(usize, usi}b"Q" => {if let Some(scope) = stack.pop() {- if scope.selected && !scope.other_paint && !scope.inherited_clip && !scope.inherited_path {- // Prefer the innermost scope that contains every active clipping path.- isolated.get_or_insert((scope.start, index, scope.matrix));+ if scope.selected && !scope.other_paint && !scope.inherited_path {+ if !scope.inherited_clip {+ // Prefer the innermost scope that contains every active clipping path.+ isolated.get_or_insert((scope.start, index, scope.matrix));+ }+ // Scopes close inner first, so the last one recorded is the outermost.+ outermost = Some((scope.start, index, scope.matrix));}matrix = scope.matrix;clipped = scope.inherited_clip;@@ -611,7 +621,7 @@ fn image_transform_scope(ops: &[Op], target: usize) -> Result<Option<(usize, usiif !clipped {return Ok(Some((target, target, placement)));}- Ok(isolated)+ Ok(isolated.or(outermost))}fn transform_image(diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rsindex 3d8752d5a..5e84cfc33 100644--- a/crates/edit/src/tests.rs+++ b/crates/edit/src/tests.rs@@ -4979,6 +4979,40 @@ fn cropped_image_resizing_moves_private_clips_and_keeps_shared_ones() {assert_eq!(text::text_lines(&doc, 0).unwrap().len(), 1);}+/// Report generators draw each picture as `q re W n cm Do Q` inside a section's clip that the+/// section's text shares. Resizing the picture scales its own clip with it (the enlarged picture+/// used to show only through its old box) while the section's clip and text stay put.+#[test]+fn resizing_an_image_under_a_shared_clip_moves_its_own_clip() {+ // The picture's own clip as wide as the picture, and wider (both seen in one report).+ for clip_width in [61.2, 85.0] {+ let mut doc = image_page_with(&format!(+ "q 20 20 560 760 re W n q 162 79 {clip_width} 28.3 re W n 61.2 0 0 28.3 162 79 cm /Im0 Do Q BT /F1 12 Tf 30 50 Td (Section text) Tj ET Q"+ ));+ let before = images::page_images(&doc, 0).unwrap()[0].clone();+ assert!(close(before.visible_rect.unwrap(), [162.0, 79.0, 223.2, 107.3]), "{:?}", before.visible_rect);+ let caption = text::text_lines(&doc, 0).unwrap();+ let target = [162.0, 79.0, 284.4, 135.6];+ let transform = images::rect_to_rect(before.visible_rect.unwrap(), target);+ images::change_image(&mut doc, 0, 0, &images::ImageChange::Transform(transform)).unwrap();+ doc = reopen(&doc);+ let after = images::page_images(&doc, 0).unwrap()[0].clone();+ let near = |a: [f64; 4], b: [f64; 4]| a.iter().zip(b).all(|(x, y)| (x - y).abs() < 0.05);+ assert!(near(after.visible_rect.unwrap(), target), "clip {clip_width}: the whole enlarged picture shows, {:?}", after.visible_rect);+ assert_eq!(after.object, before.object, "pixels are never resampled or replaced");+ assert_eq!(+ text::text_lines(&doc, 0).unwrap().iter().map(|l| (&l.text, l.rect, l.baseline())).collect::<Vec<_>>(),+ caption.iter().map(|l| (&l.text, l.rect, l.baseline())).collect::<Vec<_>>(),+ "the section's text stays where it was"+ );+ // The section's clip is shared, so it still bounds the picture: past it, nothing shows.+ let past = images::rect_to_rect(after.visible_rect.unwrap(), [500.0, 79.0, 700.0, 135.6]);+ images::change_image(&mut doc, 0, 0, &images::ImageChange::Transform(past)).unwrap();+ let clipped = images::page_images(&reopen(&doc), 0).unwrap()[0].visible_rect.unwrap();+ assert!(clipped[2] <= 580.05, "the section's clip stays in force: {clipped:?}");+ }+}+#[test]fn private_image_crop_with_intervening_state_resizes_across_streams() {let variants: Vec<Vec<&[u8]>> = vec![diff --git a/docs/editing-images.md b/docs/editing-images.mdindex 67d3613bf..4ebafd115 100644--- a/docs/editing-images.md+++ b/docs/editing-images.md@@ -38,7 +38,9 @@ the full placement by the same change; resizing keeps the opposite corner fixed,horizontal-only or vertical-only corner drags, and grabbing a grip slightly off-centre does notjump. A selected corner keeps control of its drag even over a text label; the drag previews oneframe and applies one undoable edit on release. Exclusive crops and their holes move-with the selected image; shared clips stay fixed without moving neighbouring text or artwork.+with the selected image, also when the picture sits inside a clip it shares with its section+(a report drawing each picture as `q re W n cm Do Q` within a table's clip): the picture's own+clip scales with it, and shared clips stay fixed without moving neighbouring text or artwork.`image_hit` applies the same path and hole checks headlessly. `page_images` retains `rect` asthe full placement rectangle used by `image_edit` and adds `visible_rect` for selection (nulldiff --git a/parity/acrobat-features.toml b/parity/acrobat-features.tomlindex 1f978a221..e48831625 100644--- a/parity/acrobat-features.toml+++ b/parity/acrobat-features.toml@@ -2153,8 +2153,8 @@ milestone = "M7"status = "shipped"commands = ["edit.edit_text"]tools = ["image_edit", "image_hit"]-evidence = ["crates/edit/src/tests.rs::private_image_crop_with_intervening_state_resizes_across_streams", "crates/automation/tests/it/automation.rs::private_crops_with_nested_state_resize_save_reopen_through_tools", "crates/edit/src/tests.rs::form_artwork_edits_only_the_selected_placement", "crates/ui-egui/tests/it/editing.rs::editing_existing_form_artwork_on_the_page", "crates/automation/tests/it/automation.rs::editing_form_artwork_through_tools", "crates/edit/src/tests.rs::page_images_move_turn_replace_and_delete", "crates/ui-egui/tests/it/editing.rs::editing_existing_images_on_the_page", "crates/automation/tests/it/automation.rs::editing_page_images_through_tools", "crates/edit/src/tests.rs::curved_image_clips_have_analytic_bounds_and_holes_do_not_capture_points", "crates/edit/src/tests.rs::cropped_image_resizing_moves_private_clips_and_keeps_shared_ones", "crates/ui-egui/tests/it/editing.rs::cropped_image_corner_resize_keeps_opposite_corner_and_scales_crop", "crates/edit/src/tests.rs::image_selection_bounds_respect_clips_and_restore_graphics_state", "crates/ui-egui/tests/it/editing.rs::cropped_images_do_not_intercept_text_and_stay_inside_each_page", "crates/automation/tests/it/automation.rs::clipped_image_bounds_and_text_edit_save_reopen_through_tools"]-notes = "existing raster images and grouped Form artwork: select, drag to move, drag a corner to resize keeping proportions; Delete removes one placement. Forms include their nested content as a group; individual internal elements and inline images are not supported. See docs/editing-images.md; added images too Since #1116: raster images and grouped Forms move/resize/turn as placements; selected grips own the gesture and opposite anchor, with no PDF writes during movement. Curved path frames use analytic Bezier extrema and hit testing evaluates actual nonzero/even-odd fill rules, excluding holes; intersections can retain a loose frame. Selection and resize use the visible (clipped) frame; exclusive image clips move with their pixels across nested states, intervening operators and content streams; shared clips stay fixed. Inline images and Form internals remain unsupported; see docs/editing-images.md"+evidence = ["crates/edit/src/tests.rs::private_image_crop_with_intervening_state_resizes_across_streams", "crates/automation/tests/it/automation.rs::private_crops_with_nested_state_resize_save_reopen_through_tools", "crates/edit/src/tests.rs::form_artwork_edits_only_the_selected_placement", "crates/ui-egui/tests/it/editing.rs::editing_existing_form_artwork_on_the_page", "crates/automation/tests/it/automation.rs::editing_form_artwork_through_tools", "crates/edit/src/tests.rs::page_images_move_turn_replace_and_delete", "crates/ui-egui/tests/it/editing.rs::editing_existing_images_on_the_page", "crates/automation/tests/it/automation.rs::editing_page_images_through_tools", "crates/edit/src/tests.rs::curved_image_clips_have_analytic_bounds_and_holes_do_not_capture_points", "crates/edit/src/tests.rs::cropped_image_resizing_moves_private_clips_and_keeps_shared_ones", "crates/ui-egui/tests/it/editing.rs::cropped_image_corner_resize_keeps_opposite_corner_and_scales_crop", "crates/edit/src/tests.rs::image_selection_bounds_respect_clips_and_restore_graphics_state", "crates/ui-egui/tests/it/editing.rs::cropped_images_do_not_intercept_text_and_stay_inside_each_page", "crates/automation/tests/it/automation.rs::clipped_image_bounds_and_text_edit_save_reopen_through_tools", "crates/edit/src/tests.rs::resizing_an_image_under_a_shared_clip_moves_its_own_clip"]+notes = "existing raster images and grouped Form artwork: select, drag to move, drag a corner to resize keeping proportions; Delete removes one placement. Forms include their nested content as a group; individual internal elements and inline images are not supported. See docs/editing-images.md; added images too Since #1116: raster images and grouped Forms move/resize/turn as placements; selected grips own the gesture and opposite anchor, with no PDF writes during movement. Curved path frames use analytic Bezier extrema and hit testing evaluates actual nonzero/even-odd fill rules, excluding holes; intersections can retain a loose frame. Selection and resize use the visible (clipped) frame; exclusive image clips move with their pixels across nested states, intervening operators and content streams, also inside a clip the picture shares with other content (its own clip scales, the shared one stays); shared clips stay fixed. Inline images and Form internals remain unsupported; see docs/editing-images.md"[[feature]]id = "edit.image-extract"