pdf-edit-w12: net change of 3 commits

- pdf-edit-w12: written from its sheet
- pdf-edit-w12: repair 1
- pdf-edit-w12: review fixes (review-1)
This commit is contained in:
Dylan M. Taylor
2026-11-18 16:15:27 -05:00
parent 6533a3c903
commit b5af869b80
6 changed files with 332 additions and 989 deletions
File diff suppressed because it is too large Load Diff
+25 -12
View File
@@ -5,6 +5,8 @@ use egui_kittest::kittest::Queryable;
use pdfcraft_engine::{AddedText, Edit, EditableObject};
use pdfcraft_ui_egui::PdfCraftApp;
use crate::editing::{Copies, clipboard_app, copied_text};
fn fixture(clipping: bool) -> Vec<u8> {
fixture_with_background(clipping, false)
}
@@ -55,8 +57,15 @@ fn harness_with(clipping: bool) -> Harness<'static, PdfCraftApp> {
}
fn harness_document(clipping: bool, background: bool) -> Harness<'static, PdfCraftApp> {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| {
let mut app = PdfCraftApp::new();
harness_copying(clipping, background).0
}
/// The harness with the clipboard its app copies to: each copy's flavours are recorded in the list.
fn harness_copying(clipping: bool, background: bool) -> (Harness<'static, PdfCraftApp>, Copies) {
let copies = Copies::default();
let sink = copies.clone();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
let mut app = clipboard_app(&cc.egui_ctx, &sink);
app.session = pdfcraft_engine::Session::new().with_clock(|| 1_700_000_000);
app.set_option("language", "en").unwrap();
app.open_bytes("group.pdf", None, fixture_with_background(clipping, background)).unwrap();
@@ -74,7 +83,7 @@ fn harness_document(clipping: bool, background: bool) -> Harness<'static, PdfCra
app
});
crate::support::settle(&mut h);
h
(h, copies)
}
fn at(h: &Harness<'static, PdfCraftApp>, x: f32, y: f32) -> Pos2 {
@@ -520,6 +529,13 @@ fn keys(h: &mut Harness<'static, PdfCraftApp>, modifiers: Modifiers, key: Key) {
h.run_steps(4);
}
/// The paste chord: the platform answers the request with the text of the latest copy.
fn paste(h: &mut Harness<'static, PdfCraftApp>, copies: &Copies) {
h.key_press_modifiers(Modifiers::COMMAND, Key::V);
h.event(egui::Event::Paste(copied_text(copies).unwrap_or_default()));
h.run_steps(4);
}
/// ⌘A in Edit a PDF selects every object on the page, as Acrobat does: paragraphs, artwork and
/// added content. The arrow keys then move them all and Delete removes them all, each as one step.
#[test]
@@ -559,14 +575,13 @@ fn command_a_in_an_open_paragraph_selects_its_text_not_the_objects() {
/// and right, selected; a paste into another document draws them there; a cut is one step.
#[test]
fn objects_copy_cut_and_paste_with_the_keys_on_the_page_and_into_another_document() {
let mut h = harness();
h.state_mut().system_clipboard = false;
let (mut h, copies) = harness_copying(false, false);
let unchanged = can_undo(&h, 0);
keys(&mut h, Modifiers::COMMAND, Key::A);
keys(&mut h, Modifiers::COMMAND, Key::C);
assert_eq!(can_undo(&h, 0), unchanged, "a copy changes nothing");
assert!(h.state().object_clipboard.is_some());
keys(&mut h, Modifiers::COMMAND, Key::V);
paste(&mut h, &copies);
assert_eq!(can_undo(&h, 0).as_deref(), Some("Paste"));
let pasted = texts(&h, 0);
for text in ["Group headline", "Keep here", "Added note"] {
@@ -583,7 +598,7 @@ fn objects_copy_cut_and_paste_with_the_keys_on_the_page_and_into_another_documen
assert_eq!(other, 1);
h.state_mut().quick_tool = pdfcraft_ui_egui::QuickTool::EditText;
h.run_steps(2);
keys(&mut h, Modifiers::COMMAND, Key::V);
paste(&mut h, &copies);
assert_eq!(can_undo(&h, other).as_deref(), Some("Paste"));
let there = texts(&h, other);
assert!(["Group headline", "Keep here", "Added note"].iter().all(|t| there.iter().any(|x| x.contains(t))), "{there:?}");
@@ -593,7 +608,7 @@ fn objects_copy_cut_and_paste_with_the_keys_on_the_page_and_into_another_documen
keys(&mut h, Modifiers::COMMAND, Key::X);
assert!(texts(&h, other).is_empty(), "{:?}", texts(&h, other));
assert_eq!(can_undo(&h, other).as_deref(), Some("Delete content"));
keys(&mut h, Modifiers::COMMAND, Key::V);
paste(&mut h, &copies);
assert!(texts(&h, other).iter().any(|t| t.contains("Added note")));
}
@@ -601,8 +616,7 @@ fn objects_copy_cut_and_paste_with_the_keys_on_the_page_and_into_another_documen
/// paste only while it still holds the text their copy put there.
#[test]
fn a_newer_copy_elsewhere_wins_over_the_copied_objects() {
let mut h = harness();
h.state_mut().system_clipboard = false;
let (mut h, _copies) = harness_copying(false, false);
keys(&mut h, Modifiers::COMMAND, Key::A);
keys(&mut h, Modifiers::COMMAND, Key::C);
let copied = h.state().object_clipboard.as_ref().unwrap().clip().text();
@@ -621,8 +635,7 @@ fn a_newer_copy_elsewhere_wins_over_the_copied_objects() {
/// objects were.
#[test]
fn a_picture_for_a_native_edit_paste_with_a_context_menu_open_lands_on_the_page() {
let mut h = harness();
h.state_mut().system_clipboard = false;
let (mut h, _copies) = harness_copying(false, false);
keys(&mut h, Modifiers::COMMAND, Key::A);
keys(&mut h, Modifiers::COMMAND, Key::C);
assert!(h.state().object_clipboard.is_some(), "the objects are copied, so Edit ▸ Paste is on");
+34 -51
View File
@@ -5,7 +5,7 @@ use egui_kittest::{
kittest::{NodeT, Queryable},
};
use pdfcraft_engine::{AddedContent, AddedImage, Edit};
use pdfcraft_ui_egui::{Clip, PdfCraftApp};
use pdfcraft_ui_egui::PdfCraftApp;
fn fixture(rotation: u16, cropped: bool, shared: bool) -> Vec<u8> {
fixture_at(rotation, cropped, shared, false)
@@ -40,18 +40,24 @@ fn fixture_at(rotation: u16, cropped: bool, shared: bool, collision: bool) -> Ve
}
fn harness(bytes: Vec<u8>) -> Harness<'static, PdfCraftApp> {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1000.0)).build_eframe(move |_cc| {
let mut app = PdfCraftApp::new();
let copies = crate::editing::Copies::default();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1000.0)).build_eframe(move |cc| {
let mut app = crate::editing::clipboard_app(&cc.egui_ctx, &copies);
app.set_option("language", "en").unwrap();
app.system_clipboard = false;
app.open_bytes("ownership.pdf", None, bytes.clone()).unwrap();
app.clipboard_override = Some(Clip::Image { width: 40, height: 20, rgba: [0, 0, 255, 255].repeat(800) });
app
});
h.run_steps(4);
h
}
/// Answers the paste the app asked the platform for with a 40×20 blue image.
fn deliver_image(h: &mut Harness<'static, PdfCraftApp>) {
h.step();
h.event(egui::Event::PasteImage(std::sync::Arc::new(egui::ColorImage::filled([40, 20], egui::Color32::BLUE))));
h.run_steps(3);
}
fn point(h: &Harness<'static, PdfCraftApp>, user: [f64; 2]) -> Pos2 {
let app = h.state();
let info = &app.session.get(app.views[0].id).unwrap().info.pages[0];
@@ -77,7 +83,7 @@ fn reopened(app: &mut PdfCraftApp) -> PdfCraftApp {
saved
}
fn check_placement(rotation: u16, cropped: bool, view_rotation: u16, shared: bool, existing_added: bool, collision: bool) {
fn check_placement(case: &str, rotation: u16, cropped: bool, view_rotation: u16, shared: bool, existing_added: bool, collision: bool) {
let mut h = harness(fixture_at(rotation, cropped, shared, collision));
if existing_added {
let mut png = Vec::new();
@@ -95,7 +101,7 @@ fn check_placement(rotation: u16, cropped: bool, view_rotation: u16, shared: boo
let at = if cropped { [250.0, 400.0] } else { [210.0, 80.0] };
right_click(&mut h, at);
h.get_by_label_contains("Paste").click();
h.run_steps(3);
deliver_image(&mut h);
let doc = h.state().session.get(id).unwrap();
let inserted = doc
.added()
@@ -107,9 +113,9 @@ fn check_placement(rotation: u16, cropped: bool, view_rotation: u16, shared: boo
.expect("a new image XObject was inserted");
assert!(!originals.contains(&inserted), "embedding must allocate a fresh image identity");
let selected = &doc.page_images(0)[h.state().views[0].image_selection.as_ref().unwrap().index];
assert_eq!(selected.object, Some(inserted), "the visible selection must name the inserted XObject");
assert_eq!(selected.object, Some(inserted), "{case}: the visible selection must name the inserted XObject");
let center = [(selected.rect[0] + selected.rect[2]) * 0.5, (selected.rect[1] + selected.rect[3]) * 0.5];
assert!((center[0] - at[0]).abs() < 0.001 && (center[1] - at[1]).abs() < 0.001, "rotation={rotation}, crop={cropped}: {center:?} vs {at:?}");
assert!((center[0] - at[0]).abs() < 0.001 && (center[1] - at[1]).abs() < 0.001, "{case}: rotation={rotation}, crop={cropped}: {center:?} vs {at:?}");
let saved = reopened(h.state_mut());
let placed = saved.session.get(saved.views[0].id).unwrap().page_images(0).into_iter().find(|image| image.object == Some(inserted)).unwrap();
assert!(((placed.rect[0] + placed.rect[2]) * 0.5 - at[0]).abs() < 0.001);
@@ -118,9 +124,9 @@ fn check_placement(rotation: u16, cropped: bool, view_rotation: u16, shared: boo
h.run_steps(3);
let saved = reopened(h.state_mut());
let doc = saved.session.get(saved.views[0].id).unwrap();
assert_eq!(doc.added().len(), usize::from(existing_added));
assert_eq!(doc.added().len(), usize::from(existing_added), "{case}");
let remaining = doc.page_images(0);
assert_eq!(remaining.len(), originals.len());
assert_eq!(remaining.len(), originals.len(), "{case}");
assert!(remaining.iter().any(|image| image.name == "Red"));
if shared {
assert!(remaining.iter().any(|image| image.name == "RedAgain"));
@@ -128,46 +134,23 @@ fn check_placement(rotation: u16, cropped: bool, view_rotation: u16, shared: boo
}
#[test]
fn context_paste_ordinary_page_control() {
check_placement(0, false, 0, false, false, false);
}
#[test]
fn context_paste_rotate90_places_and_selects_inserted_image() {
check_placement(90, false, 0, false, false, false);
}
#[test]
fn context_paste_rotate180_places_and_selects_inserted_image() {
check_placement(180, false, 0, false, false, false);
}
#[test]
fn context_paste_rotate270_places_and_selects_inserted_image() {
check_placement(270, false, 0, false, false, false);
}
#[test]
fn context_paste_crop_offset_places_and_selects_inserted_image() {
check_placement(0, true, 0, false, false, false);
}
#[test]
fn context_paste_rotated_crop_places_and_selects_inserted_image() {
check_placement(90, true, 0, false, false, false);
}
#[test]
fn context_paste_view_rotation_control() {
check_placement(0, false, 90, false, false, false);
}
#[test]
fn context_paste_shared_original_xobject_control() {
check_placement(90, false, 0, true, false, false);
}
#[test]
fn context_paste_with_existing_added_image_control() {
check_placement(90, false, 0, false, true, false);
}
#[test]
fn context_paste_identity_ignores_original_at_display_rectangle() {
check_placement(90, false, 0, false, false, true);
fn context_paste_places_and_selects_the_inserted_image() {
// (case, rotation, cropped, view_rotation, shared, existing_added, collision)
let cases: [(&str, u16, bool, u16, bool, bool, bool); 10] = [
("ordinary page", 0, false, 0, false, false, false),
("page rotation 90", 90, false, 0, false, false, false),
("page rotation 180", 180, false, 0, false, false, false),
("page rotation 270", 270, false, 0, false, false, false),
("crop offset", 0, true, 0, false, false, false),
("rotated crop", 90, true, 0, false, false, false),
("view rotation 90", 0, false, 90, false, false, false),
("shared original XObject", 90, false, 0, true, false, false),
("existing added image", 90, false, 0, false, true, false),
("original at the display rectangle", 90, false, 0, false, false, true),
];
for (case, rotation, cropped, view_rotation, shared, existing_added, collision) in cases {
check_placement(case, rotation, cropped, view_rotation, shared, existing_added, collision);
}
}
fn check_delete_owner(original: bool, shared_added: bool) {
+52 -46
View File
@@ -1,10 +1,11 @@
//! Edit a PDF ▸ Link: drawing a link, Link Properties, editing and deleting links.
use egui::Pos2;
use egui::accesskit::Role;
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
use pdfcraft_engine::LinkAction;
use pdfcraft_ui_egui::{Dialog, PdfCraftApp};
use pdfcraft_ui_egui::PdfCraftApp;
fn harness() -> Harness<'static, PdfCraftApp> {
// 60 fps steps, so two clicks fall inside egui's double-click window.
@@ -38,27 +39,7 @@ fn drag(h: &mut Harness<'static, PdfCraftApp>, a: Pos2, b: Pos2) {
h.run_steps(3);
}
fn links(h: &Harness<'static, PdfCraftApp>) -> Vec<pdfcraft_engine::LinkItem> {
let s = h.state();
s.session.get(s.views[0].id).unwrap().links().to_vec()
}
#[test]
fn drawing_editing_and_deleting_links() {
let mut h = harness();
assert!(h.state_mut().execute("edit.link"));
let (a, b) = (at(&h, 40.0, 300.0), at(&h, 160.0, 320.0));
drag(&mut h, a, b);
assert_eq!(h.state().dialog, Some(Dialog::LinkProps));
h.get_by_label("Create Link");
h.state_mut().link_draft.as_mut().unwrap().url = "https://example.org".into();
h.get_by_label("OK").click();
h.run_steps(3);
let l = links(&h);
assert_eq!(l.len(), 1);
assert_eq!(l[0].action, LinkAction::Uri("https://example.org".into()));
// Double-click opens Link Properties; switch to a page action.
let c = at(&h, 100.0, 310.0);
fn double_click(h: &mut Harness<'static, PdfCraftApp>, c: Pos2) {
h.hover_at(c);
h.run_steps(1);
for _ in 0..2 {
@@ -68,13 +49,45 @@ fn drawing_editing_and_deleting_links() {
h.step();
}
h.run_steps(2);
assert_eq!(h.state().dialog, Some(Dialog::LinkProps));
}
fn links(h: &Harness<'static, PdfCraftApp>) -> Vec<pdfcraft_engine::LinkItem> {
let s = h.state();
s.session.get(s.views[0].id).unwrap().links().to_vec()
}
/// Types the address into Link Properties' URL field.
fn type_url(h: &mut Harness<'static, PdfCraftApp>) {
h.get_by_role_and_label(Role::TextInput, "URL").click();
h.run_steps(1);
h.get_by_role_and_label(Role::TextInput, "URL").type_text("https://example.org");
h.run_steps(2);
}
/// Draws a link over `a`..`b` with the Link tool and confirms the dialog with a web address.
fn draw_web_link(h: &mut Harness<'static, PdfCraftApp>, a: Pos2, b: Pos2) {
assert!(h.state_mut().execute("edit.link"));
drag(h, a, b);
h.get_by_label("Create Link");
type_url(h);
h.get_by_label("OK").click();
h.run_steps(3);
}
#[test]
fn drawing_editing_and_deleting_links() {
let mut h = harness();
let (a, b) = (at(&h, 40.0, 300.0), at(&h, 160.0, 320.0));
draw_web_link(&mut h, a, b);
let l = links(&h);
assert_eq!(l.len(), 1);
assert_eq!(l[0].action, LinkAction::Uri("https://example.org".into()));
// Double-click opens Link Properties; switch to a page action.
let c = at(&h, 100.0, 310.0);
double_click(&mut h, c);
h.get_by_label("Link Properties");
{
let d = h.state_mut().link_draft.as_mut().unwrap();
d.web = false;
d.target = 1;
}
h.get_by_label("Go to a page view").click();
h.run_steps(3);
h.get_by_label("OK").click();
h.run_steps(3);
assert_eq!(links(&h)[0].action, LinkAction::Page { page: 0, zoom: pdfcraft_engine::LinkZoom::FitPage });
@@ -86,7 +99,6 @@ fn drawing_editing_and_deleting_links() {
#[test]
fn link_properties_sets_the_page_view_zoom() {
use egui::accesskit::Role;
use pdfcraft_engine::LinkZoom;
let mut h = harness();
assert!(h.state_mut().execute("edit.link"));
@@ -95,8 +107,7 @@ fn link_properties_sets_the_page_view_zoom() {
h.get_by_label("Create Link");
h.get_by_label("Go to a page view").click();
h.run_steps(3);
// The toolbar has a zoom box of its own; the link's shows Fit Page until changed.
h.get_by(|n| n.role() == Role::ComboBox && n.value().as_deref() == Some("Fit Page")).click();
h.get_by_label("Zoom").click();
h.run_steps(3);
h.get_by_role_and_label(Role::Button, "200%").scroll_to_me();
h.run_steps(3);
@@ -119,7 +130,7 @@ trailer << /Root 1 0 R >>
#[test]
fn an_appearance_edit_keeps_the_destination_as_written() {
use pdfcraft_render::{DestView, LinkTarget};
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_cc| {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 60.0).build_eframe(|_cc| {
let mut app = PdfCraftApp::new();
app.set_option("language", "en").unwrap();
app.open_bytes("positioned.pdf", None, POSITIONED.to_vec()).unwrap();
@@ -132,10 +143,13 @@ fn an_appearance_edit_keeps_the_destination_as_written() {
s.session.get(s.views[0].id).unwrap().info.links[0].target.clone()
};
assert_eq!(target(&h), positioned);
h.state_mut().views[0].links.open_existing = Some((0, 0));
h.run_steps(3);
assert_eq!(h.state().dialog, Some(Dialog::LinkProps));
h.state_mut().link_draft.as_mut().unwrap().style.color = [1.0, 0.0, 0.0];
assert!(h.state_mut().execute("edit.link"));
h.run_steps(2);
// The link lies at y 340..360 from the top of the 400 pt page.
let c = at(&h, 100.0, 350.0);
double_click(&mut h, c);
h.get_by_label("Link Properties");
h.get_by_label("Red").click();
h.run_steps(3);
h.get_by_label("OK").click();
h.run_steps(3);
@@ -146,12 +160,8 @@ fn an_appearance_edit_keeps_the_destination_as_written() {
#[test]
fn removing_all_links_and_undo() {
let mut h = harness();
assert!(h.state_mut().execute("edit.link"));
let (a, b) = (at(&h, 40.0, 300.0), at(&h, 160.0, 320.0));
drag(&mut h, a, b);
h.state_mut().link_draft.as_mut().unwrap().url = "https://example.org".into();
h.get_by_label("OK").click();
h.run_steps(3);
draw_web_link(&mut h, a, b);
assert_eq!(links(&h).len(), 1);
// Execute remove all links command
@@ -176,12 +186,8 @@ fn right_click(h: &mut Harness<'static, PdfCraftApp>, p: Pos2) {
#[test]
fn the_link_context_menu_shows_only_for_a_right_click_on_a_link() {
let mut h = harness();
assert!(h.state_mut().execute("edit.link"));
let (a, b) = (at(&h, 40.0, 100.0), at(&h, 160.0, 120.0));
drag(&mut h, a, b);
h.state_mut().link_draft.as_mut().unwrap().url = "https://example.org".into();
h.get_by_label("OK").click();
h.run_steps(3);
draw_web_link(&mut h, a, b);
let (on_link, away) = (at(&h, 100.0, 110.0), at(&h, 250.0, 200.0));
// Select the link with the Link tool, then right-click away from it: the menu there is
// not the link's.
+6 -3
View File
@@ -60,9 +60,12 @@ fn unsupported_script_buttons_leave_hidden_text_bytes_history_and_saved_pixels_u
h.run_steps(1);
h.drop_at(at);
h.run_steps(3);
let editor = h.state_mut().views[0].line_editor.as_mut().expect("hidden text editor opens");
editor.selection = Some(1..2);
editor.focus = true;
// Select the second character the way a person does: Home, one step right, Shift+Right.
h.key_press(egui::Key::Home);
h.run_steps(1);
h.key_press(egui::Key::ArrowRight);
h.run_steps(1);
h.key_press_modifiers(egui::Modifiers::SHIFT, egui::Key::ArrowRight);
h.run_steps(1);
let before = h.state().session.get(id).unwrap().bytes.clone();
assert!(h.state().session.get(id).unwrap().can_undo().is_none());
+11 -14
View File
@@ -20,7 +20,9 @@ use std::fs;
use std::path::{Path, PathBuf};
use egui::{Key, Modifiers};
use egui::accesskit::Role;
use egui_kittest::Harness;
use egui_kittest::kittest::{NodeT, Queryable};
use pdfcraft_ui_egui::PdfCraftApp;
use serde::Deserialize;
@@ -71,10 +73,6 @@ enum Step {
Type {
text: String,
},
/// Replace the open paragraph's text without keystrokes. Prefer `type`.
Editor {
text: String,
},
Key {
key: String,
#[serde(default)]
@@ -170,13 +168,6 @@ impl Step {
}
Step::ClickPage { x, y } => click_page(h, session, *x, *y)?,
Step::Type { text } => type_into_paragraph(h, text)?,
Step::Editor { text } => {
let Some(editor) = h.state_mut().views.first_mut().and_then(|v| v.line_editor.as_mut()) else {
return Err("no paragraph is open for typing".into());
};
editor.text.clone_from(text);
h.run_steps(1);
}
Step::Key { key, modifiers } => {
let Some(key) = Key::from_name(key) else { return Err(format!("unknown key {key:?}")) };
h.key_press_modifiers(modifiers_of(modifiers)?, key);
@@ -187,8 +178,8 @@ impl Step {
// The open editor is what a click just showed. Otherwise take the shortest
// paragraph that contains the words, so a heading that merely mentions them
// does not win.
let actual = if let Some(open) = h.state().views.first().and_then(|v| v.line_editor.as_ref()) {
open.text.clone()
let actual = if let Some(open) = open_editor_text(h) {
open
} else {
let mut matches: Vec<_> =
paragraphs_full(h).into_iter().map(|p| p.editor_text).filter(|p| p.contains(contains.as_str())).collect();
@@ -235,8 +226,14 @@ impl Step {
}
}
/// The text of the open paragraph editor: the focused multiline input's line runs, in order.
fn open_editor_text(h: &Harness<'static, PdfCraftApp>) -> Option<String> {
let editor = h.query_by(|n| n.role() == Role::MultilineTextInput && n.is_focused())?;
Some(editor.children().filter_map(|line| line.value()).collect())
}
fn type_into_paragraph(h: &mut Harness<'static, PdfCraftApp>, text: &str) -> Result<(), String> {
if h.state().views.first().and_then(|v| v.line_editor.as_ref()).is_none() {
if open_editor_text(h).is_none() {
return Err("no paragraph is open for typing".into());
}
h.key_press_modifiers(Modifiers::COMMAND, Key::A);