Files
pdfcraft/crates/ui-egui/tests/it/editing.rs
T

11657 lines
544 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Headless UI tests for editing and saving: the organize toolbar, selection, undo/redo keys,
//! save/save-as, the unsaved-changes prompt and editable document properties.
use crate::support;
use egui::accesskit::Role;
use egui::{Key, Modifiers};
use egui_kittest::Harness;
use egui_kittest::kittest::{NodeT, Queryable};
use pdfcraft_render::{PageRenderer, RenderRequest, RequestKind};
use pdfcraft_ui_egui::{OpenOutcome, PdfCraftApp};
use crate::gpu;
use crate::support::AppHarness as _;
/// An `n`-page document with a proper xref table; page `i` shows "Page i+1".
fn fixture(n: usize) -> Vec<u8> {
let mut objs: Vec<String> = vec!["<< /Type /Catalog /Pages 2 0 R >>".into()];
let kids: Vec<String> = (0..n).map(|i| format!("{} 0 R", 4 + 2 * i)).collect();
objs.push(format!("<< /Type /Pages /Kids [{}] /Count {n} /MediaBox [0 0 200 300] >>", kids.join(" ")));
objs.push("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".into());
for i in 0..n {
objs.push(format!("<< /Type /Page /Parent 2 0 R /Contents {} 0 R /Resources << /Font << /F1 3 0 R >> >> >>", 5 + 2 * i));
let body = format!("BT /F1 24 Tf 20 150 Td (Page {}) Tj ET", i + 1);
objs.push(format!("<< /Length {} >>\nstream\n{body}\nendstream", body.len()));
}
let mut out = b"%PDF-1.7\n".to_vec();
let mut offsets = Vec::new();
for (i, o) in objs.iter().enumerate() {
offsets.push(out.len());
out.extend_from_slice(format!("{} 0 obj\n{o}\nendobj\n", i + 1).as_bytes());
}
let xref = out.len();
out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
for o in offsets {
out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
}
out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes());
out
}
thread_local! {
/// The file service of this test thread's apps: the files an app saves land in it, and a Save's picker
/// answers from it.
static FILES: std::sync::Arc<craft::host::ScriptedFiles> = std::sync::Arc::default();
}
/// An app whose pickers and writes go to this thread's [`FILES`].
fn scripted_app() -> PdfCraftApp {
crate::control::app_with_files(FILES.with(std::sync::Arc::clone))
}
/// The unsaved-changes question's Save answer in the platform's words (production's wording, on every platform).
fn save_answer() -> &'static str {
"Save"
}
/// The unsaved-changes question's Don't Save answer in the platform's words.
fn dont_save() -> &'static str {
"Don't save"
}
/// The next Save's picker answers `path`.
fn queue_save(path: &std::path::Path) {
FILES.with(|f| f.queue(craft::host::Picked::Paths(vec![path.to_path_buf()])));
}
/// The next picker answers `paths`; a file among them is also put in the file service's memory, for what
/// reads through it.
fn queue_pick(paths: &[&std::path::Path]) {
use craft::host::Files as _;
FILES.with(|f| {
for path in paths {
if let Ok(bytes) = std::fs::read(path) {
let _ = f.write(path, &bytes, craft::host::WriteIntent::SaveAs);
}
}
f.queue(craft::host::Picked::Paths(paths.iter().map(|p| p.to_path_buf()).collect()));
});
}
/// What a save wrote to `path`.
fn saved_file(path: &std::path::Path) -> Vec<u8> {
FILES.with(|f| f.file(path)).expect("file written")
}
fn harness(pages: usize, setup: impl FnOnce(&mut PdfCraftApp) + 'static) -> Harness<'static, PdfCraftApp> {
let mut builder = Harness::builder().with_size(egui::vec2(1400.0, 900.0));
if std::env::var_os("PDFCRAFT_ORGANIZE_SNAPSHOT").is_some() {
builder = builder.wgpu();
}
let mut h = builder.build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = scripted_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "doc.pdf", None, fixture(pages)).expect("fixture opens");
setup(&mut app);
app
}
});
h.run_steps(4);
h
}
/// What an app writes to its injected clipboard: the flavours of each copy, in order.
pub(crate) type Copies = std::sync::Arc<std::sync::Mutex<Vec<Vec<craft::platform::clipboard::Flavor>>>>;
/// The text of the latest copy in `copies`.
pub(crate) fn copied_text(copies: &Copies) -> Option<String> {
let copies = copies.lock().ok()?;
copies.last()?.iter().find(|f| f.mime == "text/plain").map(|f| String::from_utf8_lossy(&f.data).into_owned())
}
/// An app over a host and an `EventClipboard`: a paste reaches it as the frame's raw paste event, and what
/// it copies is recorded in `copies`.
pub(crate) fn clipboard_app(ctx: &egui::Context, copies: &Copies) -> PdfCraftApp {
use craft::platform::clipboard::{Clipboard, EventClipboard};
let pass = ctx.clone();
let sink = copies.clone();
let clipboard = Clipboard::new(EventClipboard::new(
move || pass.cumulative_pass_nr(),
move |flavors| {
if let Ok(mut held) = sink.lock() {
held.push(flavors.to_vec());
}
Ok(0)
},
));
let services = pdfcraft_ui_egui::AppServices::new(
craft::host::Wake::none(),
clipboard,
None,
None,
craft::platform::open::FileIntake::new(craft::host::Wake::none()).0,
craft::platform::watch::FileWatch::new(craft::host::Wake::none()).inline(),
);
PdfCraftApp::with_host(std::sync::Arc::new(craft::host::Host::builder(pdfcraft_ui_egui::APP_INFO).build()), services)
}
/// A harness over `bytes` opened as `name`, with the clipboard of [`clipboard_app`].
pub(crate) fn clipboard_harness(name: &'static str, bytes: Vec<u8>) -> (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| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = clipboard_app(&cc.egui_ctx, &sink);
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), name, None, bytes.clone()).expect("opens");
app
}
});
h.run_steps(4);
(h, copies)
}
/// The eight grips of a picture from its corners: the corners, then the midpoints of the edges from each
/// corner to the next. Grip `k` faces grip `(k % 4 + 2) % 4 + 4 * (k / 4)`.
fn grip_points(c: [egui::Pos2; 4]) -> [egui::Pos2; 8] {
[c[0], c[1], c[2], c[3], c[0].lerp(c[1], 0.5), c[1].lerp(c[2], 0.5), c[2].lerp(c[3], 0.5), c[3].lerp(c[0], 0.5)]
}
/// The screen corners of picture `index` on `page`, as `[s00, s10, s11, s01]`: the unit square's corners
/// through the picture's matrix (an upright picture uses its selectable bounds). `s11` is the top right
/// and `s00` the bottom left of an upright picture.
fn picture_corners(h: &Harness<'static, PdfCraftApp>, page: usize, index: usize) -> [egui::Pos2; 4] {
let app = h.state();
let doc = app.session.get(app.views[0].id).unwrap();
let image = &doc.page_images(page)[index];
let [a, b, c, d, ..] = image.matrix;
let turned = b.abs() > 1e-4 || c.abs() > 1e-4 || a < 0.0 || d < 0.0;
let bounds = pdfcraft_ui_egui::canvas::image_selectable_bounds(image);
let matrix = if !turned && bounds[2] > bounds[0] && bounds[3] > bounds[1] {
[bounds[2] - bounds[0], 0.0, 0.0, bounds[3] - bounds[1], bounds[0], bounds[1]]
} else {
image.matrix
};
let xf = app.views[0].page_xform(page).expect("page on screen");
let info = &doc.info.pages[page];
[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)].map(|(u, v)| {
let x = matrix[0] * u + matrix[2] * v + matrix[4];
let y = matrix[1] * u + matrix[3] * v + matrix[5];
let p = info.user_to_view(x as f32, y as f32);
xf.norm_to_screen(p[0] / info.width, p[1] / info.height)
})
}
/// The centre of the selected picture's rotate knob, found by its access node.
fn knob_point(h: &Harness<'static, PdfCraftApp>) -> egui::Pos2 {
use egui::accesskit::Role;
h.get_by_role_and_label(Role::Button, "Rotate").rect().center()
}
/// The value of the number field in a dialog's row named `label`: the row is a group under that name, with its
/// caption, and the field inside it.
fn row_number(h: &Harness<'static, PdfCraftApp>, label: &str) -> Option<String> {
h.get_by_role_and_label(Role::Group, label).query_all_by(|n| n.role() == Role::SpinButton).next().and_then(|n| n.value())
}
/// The page grid's own Close, in its Organize pages bar: the window's title bar has a Close too.
fn close_grid(h: &mut Harness<'static, PdfCraftApp>) {
h.get_by_role_and_label(Role::Toolbar, "Organize pages").get_by_role_and_label(Role::Button, "Close").click();
}
fn organize(pages: usize) -> Harness<'static, PdfCraftApp> {
harness(pages, |app| app.set_option(&egui::Context::default(), "organize", "on").unwrap())
}
/// Organize pages ▸ Insert ▸ `item`: the panel lists the Insert rows under their heading.
fn insert_menu(h: &mut Harness<'static, PdfCraftApp>, item: &str) {
// Insert is one row that opens a menu of its ways.
h.get_by_label("Insert").click();
h.run_steps(2);
h.get_by_label(item).click();
}
fn type_selection(h: &mut Harness<'static, PdfCraftApp>, text: &str) {
use egui::accesskit::Role;
h.get_by_role_and_label(Role::TextInput, "Selected pages").click();
h.run_steps(1);
h.key_press_modifiers(Modifiers::COMMAND, Key::A);
h.get_by_role_and_label(Role::TextInput, "Selected pages").type_text(text);
h.run_steps(2);
}
#[test]
fn organize_page_range_tracks_thumbnails_and_applies_typed_selection() {
use egui::accesskit::Role;
let _gpu = gpu();
let mut h = organize(100);
// A tile's check toggles its page as ⌘-click does; the check is the pointer's only, so the test ⌘-clicks.
h.get_by_label("Page 2").click();
h.run_steps(2);
assert_eq!(h.get_by_role_and_label(Role::TextInput, "Selected pages").value().as_deref(), Some("2"));
h.get_by_label("Page 3").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
assert_eq!(h.get_by_role_and_label(Role::TextInput, "Selected pages").value().as_deref(), Some("2-3"));
let old = h.state().views[0].selected.clone();
type_selection(&mut h, "1,12,16-18,89,92,93,97,99");
assert_eq!(h.state().views[0].selected, old, "typing is not committed until Enter or focus loss");
h.key_press(Key::Enter);
h.run_steps(2);
assert_eq!(h.state().views[0].target_pages(), [0, 11, 15, 16, 17, 88, 91, 92, 96, 98]);
assert_eq!(h.get_by_role_and_label(Role::TextInput, "Selected pages").value().as_deref(), Some("1, 12, 16-18, 89, 92-93, 97, 99"));
if let Some(path) = std::env::var_os("PDFCRAFT_ORGANIZE_SNAPSHOT") {
h.render().unwrap().save(std::path::Path::new(&path)).unwrap();
}
h.get_by_label("Rotate clockwise").click();
h.run_steps(3);
let doc = h.state().session.get(h.state().views[0].id).unwrap();
for (i, page) in doc.info.pages.iter().enumerate() {
assert_eq!(page.rotation, if h.state().views[0].selected.contains(&i) { 90 } else { 0 });
}
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert!(h.state().session.get(h.state().views[0].id).unwrap().info.pages.iter().all(|p| p.rotation == 0));
}
#[test]
fn organize_page_range_rejects_invalid_input_and_escape_cancels_without_clearing() {
use egui::accesskit::Role;
let mut h = organize(5);
h.set_option("select", "2-3").unwrap();
h.run_steps(2);
for text in ["0", "6", "1,", "no"] {
type_selection(&mut h, text);
h.key_press(Key::Enter);
h.run_steps(2);
assert_eq!(h.state().views[0].target_pages(), [1, 2]);
}
type_selection(&mut h, "1-5");
h.key_press(Key::Escape);
h.run_steps(2);
assert_eq!(h.state().views[0].target_pages(), [1, 2]);
assert_eq!(h.get_by_role_and_label(Role::TextInput, "Selected pages").value().as_deref(), Some("2-3"));
type_selection(&mut h, "5, 4, 4");
h.key_press(Key::Enter);
h.run_steps(2);
assert_eq!(h.state().views[0].target_pages(), [3, 4]);
type_selection(&mut h, " ");
h.key_press(Key::Enter);
h.run_steps(2);
assert!(h.state().views[0].selected.items.is_empty());
assert!(h.set_option("select", "0").is_err());
assert!(h.set_option("select", "6").is_err());
}
/// Issue #1910: the page grid opens on the page the user was reading: it is selected and
/// scrolled into view, whether the document was on a middle page, its last page or a page
/// other than an earlier grid selection.
#[test]
fn organize_opens_on_the_current_page() {
let mut h = harness(60, |_| {});
let in_view = |h: &Harness<'static, PdfCraftApp>, page: usize| {
let viewport = h.state().views[0].viewport_rect();
let label = format!("Page {}", page + 1);
// A tile aligned with the viewport edge can differ by a fraction of a pixel after layout rounding.
h.query_by_label(&label).is_some_and(|n| viewport.expand(0.001).contains_rect(n.rect()))
};
// From a middle page.
h.state_mut().views[0].go_to_page(30);
h.run_steps(2);
assert!(h.execute("page.organize"));
h.run_steps(4);
assert!(h.state().views[0].organize);
assert_eq!(h.state().views[0].target_pages(), [30]);
assert!(h.state().views[0].selected.items.is_empty(), "an empty selection is the current page, so checkboxes still toggle");
assert!(in_view(&h, 30), "the current page is scrolled into view");
assert!(!in_view(&h, 0), "the grid does not open on page 1");
// An earlier grid selection on another page does not win over the page the user went to.
h.get_by_label("Page 32").click();
h.run_steps(2);
assert_eq!(h.state().views[0].target_pages(), [31]);
close_grid(&mut h);
h.run_steps(2);
assert!(!h.state().views[0].organize);
h.state_mut().views[0].go_to_page(45);
h.run_steps(2);
h.set_option("tool", "organize").unwrap();
h.run_steps(4);
assert_eq!(h.state().views[0].target_pages(), [45]);
assert!(in_view(&h, 45));
assert!(!in_view(&h, 1));
// From the last page.
close_grid(&mut h);
h.run_steps(2);
h.state_mut().views[0].go_to_page(59);
h.run_steps(2);
h.set_option("organize", "on").unwrap();
h.run_steps(4);
assert_eq!(h.state().views[0].target_pages(), [59]);
assert!(in_view(&h, 59));
assert!(!in_view(&h, 0));
}
#[test]
fn organize_tool_chosen_before_opening_a_document_shows_its_pages() {
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "tool", "organize").unwrap();
app.open_bytes(&egui::Context::default(), "doc.pdf", None, fixture(3)).unwrap();
assert!(app.views[0].organize);
}
#[test]
fn organize_page_actions_rotate_only_the_named_page_and_undo() {
let mut h = organize(3);
h.get_by_label("Page 1").click();
h.run_steps(2);
h.get_by_label("Page 3").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
h.hover_at(h.get_by_label("Page 1").rect().center());
h.run_steps(2);
h.get_by_label("Rotate page 1 clockwise").click();
h.run_steps(3);
let rotations = |h: &Harness<'static, PdfCraftApp>| {
h.state().session.get(h.state().views[0].id).unwrap().info.pages.iter().map(|p| p.rotation).collect::<Vec<_>>()
};
assert_eq!(rotations(&h), [90, 0, 0]);
assert_eq!(h.state().views[0].target_pages(), [0, 2], "a local action preserves the multi-selection");
h.get_by_label("Rotate counterclockwise").click();
h.run_steps(3);
assert_eq!(rotations(&h), [0, 0, 270], "toolbar actions apply to the selection");
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(rotations(&h), [90, 0, 0]);
h.hover_at(h.get_by_label("Page 1").rect().center());
h.run_steps(2);
h.get_by_label("Rotate page 1 counterclockwise").click();
h.run_steps(3);
assert_eq!(rotations(&h), [0, 0, 0]);
}
#[test]
fn organize_local_actions_respect_document_permissions() {
let mut h = harness(3, |app| {
app.apply_edit(
&egui::Context::default(),
pdfcraft_engine::Edit::Protect(pdfcraft_engine::Protection {
permissions_password: Some("owner".into()),
changes: pdfcraft_engine::Changes::None,
..Default::default()
}),
);
let bytes = app.session.save_bytes(app.views[0].id).unwrap();
app.open_bytes(&egui::Context::default(), "restricted.pdf", None, bytes.as_ref().clone()).unwrap();
app.set_option(&egui::Context::default(), "tool", "organize").unwrap();
});
h.get_by_label("Page 2").click();
h.run_steps(2);
assert!(h.get_by_label("Rotate page 2 clockwise").accesskit_node().is_disabled());
assert!(h.get_by_label("Rotate page 2 counterclockwise").accesskit_node().is_disabled());
assert!(h.get_by_label("Delete page 2").accesskit_node().is_disabled());
let app = h.state();
let view = &app.views[app.active.unwrap()];
assert_eq!(view.target_pages(), [1]);
assert!(!app.session.get(view.id).unwrap().dirty);
}
/// Page labels of the active document, read back from its current bytes.
fn page_texts(app: &PdfCraftApp) -> Vec<String> {
let doc = app.session.get(app.views[0].id).unwrap();
let mut r = PageRenderer::new(doc.bytes.clone(), Default::default());
(0..r.page_count())
.map(|p| {
let out = r.render(RenderRequest { page: p, kind: RequestKind::Text, scale: 1.0, ..Default::default() });
out.text.map(|t| t.plain_text().trim().to_string()).unwrap_or_default()
})
.collect()
}
fn dirty(h: &Harness<'static, PdfCraftApp>) -> bool {
let app = h.state();
app.session.get(app.views[0].id).is_some_and(|d| d.dirty)
}
fn temp_path(name: &str) -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!("pdfcraft-ui-tests-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
dir.join(name)
}
#[test]
fn shift_click_selects_a_range_and_moves_it() {
let mut h = organize(4);
h.get_by_label("Page 1").click();
h.run_steps(1);
h.get_by_label("Page 2").click_modifiers(Modifiers::SHIFT);
h.run_steps(2);
h.get_by_label_contains("2 pages selected");
h.get_by_label("Move later").click();
h.run_steps(3);
assert_eq!(page_texts(h.state()), ["Page 3", "Page 1", "Page 2", "Page 4"]);
// The moved pages stay selected at their new position, so repeated clicks keep moving them.
assert_eq!(h.state().views[0].selected.items.iter().copied().collect::<Vec<_>>(), [1, 2]);
// The pointer still rests on the button, whose tooltip carries its name too.
h.get_by_role_and_label(Role::Button, "Move later").click();
h.run_steps(3);
assert_eq!(page_texts(h.state()), ["Page 3", "Page 4", "Page 1", "Page 2"]);
}
#[test]
fn reverse_pages_reverses_the_selection_or_else_the_whole_document() {
let mut h = organize(4);
assert!(h.execute("page.reverse"));
h.run_steps(3);
assert_eq!(page_texts(h.state()), ["Page 4", "Page 3", "Page 2", "Page 1"]);
h.get_by_label("Page 1").click();
h.run_steps(1);
h.get_by_label("Page 3").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
assert!(h.execute("page.reverse"));
h.run_steps(3);
assert_eq!(page_texts(h.state()), ["Page 2", "Page 3", "Page 4", "Page 1"]);
}
#[test]
fn command_click_toggles_and_rotation_applies_to_selection() {
let mut h = organize(3);
h.get_by_label("Page 1").click();
h.run_steps(1);
h.get_by_label("Page 3").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
h.get_by_label("Rotate clockwise").click();
h.run_steps(3);
let rot: Vec<u16> = {
let app = h.state();
app.session.get(app.views[0].id).unwrap().info.pages.iter().map(|p| p.rotation).collect()
};
assert_eq!(rot, [90, 0, 90]);
}
#[test]
fn delete_is_refused_when_it_would_remove_every_page() {
let mut h = organize(2);
h.state_mut().views[0].select_pages(&[0, 1]);
h.run_steps(2);
h.key_press(Key::Delete);
h.run_steps(3);
assert_eq!(page_texts(h.state()).len(), 2, "a document keeps at least one page");
assert!(!dirty(&h));
}
#[test]
fn insert_blank_page_after_selection() {
let mut h = organize(2);
h.get_by_label("Page 1").click();
h.run_steps(1);
insert_menu(&mut h, "Insert a blank page after the selection");
h.run_steps(3);
assert_eq!(page_texts(h.state()), ["Page 1", "", "Page 2"]);
let app = h.state();
let info = &app.session.get(app.views[0].id).unwrap().info;
assert_eq!((info.pages[1].width, info.pages[1].height), (200.0, 300.0), "matches the neighbouring page");
}
#[test]
fn save_writes_an_incremental_update_and_clears_dirty() {
let out = temp_path("saved.pdf");
let original = fixture(3);
let mut h = organize(3);
queue_save(&out);
h.get_by_label("Page 3").click();
h.run_steps(1);
h.get_by_label("Rotate counterclockwise").click();
h.run_steps(3);
h.key_press_modifiers(Modifiers::COMMAND, Key::S);
h.run_steps(3);
let saved = saved_file(&out);
assert_eq!(&saved[..original.len()], &original[..], "original bytes untouched");
assert!(!dirty(&h));
let _ = h.get_all_by_label("saved.pdf"); // the tab takes the new name, no edited marker
// What was written is what the app now shows.
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().bytes.as_slice(), saved.as_slice());
let reopened = pdfcraft_render::inspect(std::sync::Arc::new(saved), None).unwrap();
assert_eq!(reopened.pages[2].rotation, 270);
let _ = std::fs::remove_file(out);
}
#[test]
fn save_as_to_a_new_file_drops_earlier_revisions() {
// #2165: Save As appended a revision, so a deleted page stayed in the new file's first one.
let out = temp_path("save-as-clean.pdf");
let mut h = organize(3);
h.get_by_label("Page 2").hover();
h.run_steps(2);
h.get_by_label("Delete page 2").click();
h.run_steps(3);
queue_save(&out);
let ctx_h = h.ctx.clone();
assert!(!h.state_mut().save_active(&ctx_h, pdfcraft_ui_egui::SaveTarget::As), "Save As awaits the picker");
h.run_steps(3);
let saved = saved_file(&out);
let count = |needle: &[u8]| saved.windows(needle.len()).filter(|w| *w == needle).count();
assert_eq!(count(b"%%EOF"), 1, "one revision only");
assert_eq!((count(b"(Page 1)"), count(b"(Page 2)")), (1, 0), "the deleted page's content is gone");
assert!(!dirty(&h));
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().bytes.as_slice(), saved.as_slice());
assert_eq!(page_texts(h.state()), ["Page 1", "Page 3"]);
let _ = std::fs::remove_file(out);
}
#[test]
fn closing_a_dirty_tab_asks_and_cancel_keeps_it() {
let mut h = organize(2);
h.get_by_label("Rotate clockwise").click();
h.run_steps(3);
h.key_press_modifiers(Modifiers::COMMAND, Key::W);
h.run_steps(3);
assert!(h.query_all_by_label_contains("Save changes to “doc.pdf”").next().is_some());
h.get_by_label("Cancel").click();
h.run_steps(3);
assert_eq!(h.state().views.len(), 1, "cancel keeps the document open");
assert!(dirty(&h));
assert!(h.query_by_label(dont_save()).is_none(), "cancel closes the prompt");
h.key_press_modifiers(Modifiers::COMMAND, Key::W);
h.run_steps(3);
h.get_by_label(dont_save()).click();
h.run_steps(3);
assert!(h.state().views.is_empty(), "discarding closes the tab");
}
#[test]
fn closing_a_dirty_tab_can_save_first() {
let out = temp_path("closed.pdf");
let mut h = organize(2);
queue_save(&out);
h.get_by_label("Rotate clockwise").click();
h.run_steps(3);
let ctx_h = h.ctx.clone();
h.state_mut().request_close_tab(&ctx_h, 0);
h.run_steps(2);
h.query_all_by_label(save_answer()).last().expect("the prompt's Save button").click();
h.run_steps(3);
assert!(h.state().views.is_empty());
let saved = pdfcraft_render::inspect(std::sync::Arc::new(saved_file(&out)), None).unwrap();
assert_eq!(saved.pages[0].rotation, 90);
let _ = std::fs::remove_file(out);
}
#[test]
fn the_save_prompt_answers_to_the_keyboard() {
// Issue #8: Enter saves; ⌘D / Ctrl+D and Alt+D (the Don't save button's letter) don't save;
// Escape cancels. While the prompt is open, ⌘D is not Document properties.
for (m, key) in [(Modifiers::COMMAND, Key::D), (Modifiers::ALT, Key::D)] {
let mut h = organize(2);
h.get_by_label("Rotate clockwise").click();
h.run_steps(3);
h.key_press_modifiers(Modifiers::COMMAND, Key::W);
h.run_steps(3);
assert!(h.query_all_by_label_contains("Save changes to “doc.pdf”").next().is_some());
h.key_press_modifiers(m, key);
h.run_steps(3);
assert!(h.state().views.is_empty(), "{m:?}+{key:?} doesn't save and closes");
assert!(h.state().dialogs.is_empty(), "{m:?}+{key:?} ran no command underneath");
}
let mut h = organize(2);
h.get_by_label("Rotate clockwise").click();
h.run_steps(3);
h.key_press_modifiers(Modifiers::COMMAND, Key::W);
h.run_steps(3);
h.key_press(Key::Escape);
h.run_steps(3);
assert_eq!(h.state().views.len(), 1, "Escape cancels");
assert!(h.query_by_label(dont_save()).is_none(), "Escape closes the prompt");
let out = temp_path("enter-saves.pdf");
queue_save(&out);
h.key_press_modifiers(Modifiers::COMMAND, Key::W);
h.run_steps(3);
h.key_press(Key::Enter);
h.run_steps(3);
assert!(h.state().views.is_empty(), "Enter saves and closes");
let saved = pdfcraft_render::inspect(std::sync::Arc::new(saved_file(&out)), None).unwrap();
assert_eq!(saved.pages[0].rotation, 90);
let _ = std::fs::remove_file(out);
}
/// An organize harness with a dirty document and the "Save changes?" prompt open.
fn dirty_prompt() -> Harness<'static, PdfCraftApp> {
let mut h = organize(2);
h.get_by_label("Rotate clockwise").click();
h.run_steps(3);
h.key_press_modifiers(Modifiers::COMMAND, Key::W);
h.run_steps(3);
assert!(h.query_all_by_label_contains("Save changes to “doc.pdf”").next().is_some());
h
}
/// The prompt has Save chosen on arrival; `keys` then move the choice and Enter or Space presses it.
fn press_after(keys: &[(Modifiers, Key)], activate: Key) -> Harness<'static, PdfCraftApp> {
let mut h = dirty_prompt();
for (m, k) in keys {
h.key_press_modifiers(*m, *k);
h.run_steps(2);
}
h.key_press(activate);
h.run_steps(3);
h
}
#[test]
fn the_save_prompt_escape_still_cancels_whatever_is_chosen() {
let h = press_after(&[(Modifiers::NONE, Key::ArrowLeft), (Modifiers::NONE, Key::ArrowLeft)], Key::Escape);
assert_eq!(h.state().views.len(), 1, "Escape cancels even with Don't save chosen");
assert!(dirty(&h));
}
#[test]
fn the_save_prompt_starts_on_save_every_time_it_opens() {
// Move to Don't save, cancel with Escape, and ask again: the choice is back on Save.
let out = temp_path("reopened.pdf");
let mut h = dirty_prompt();
queue_save(&out);
h.key_press(Key::ArrowLeft);
h.key_press(Key::ArrowLeft);
h.run_steps(2);
h.key_press(Key::Escape);
h.run_steps(3);
assert_eq!(h.state().views.len(), 1);
h.key_press_modifiers(Modifiers::COMMAND, Key::W);
h.run_steps(3);
h.key_press(Key::Enter);
h.run_steps(3);
assert!(h.state().views.is_empty(), "Enter saved and closed");
// Saves go through the file service, which keeps them in memory.
assert!(FILES.with(|f| f.file(&out)).is_some(), "the second prompt opened on Save");
}
#[test]
fn keys_leave_the_document_under_a_dialog_alone() {
// Issue #870: ⌘W with Preferences open closed the file underneath it, and in the page grid
// Delete deleted the selected page. While a dialog is open its keys are its own.
let mut h = organize(3);
h.get_by_label("Page 2").click();
h.run_steps(2);
h.get_by_label("Rotate clockwise").click();
h.run_steps(3);
assert!(h.execute("app.preferences"));
h.run_steps(3);
let shift = Modifiers::COMMAND | Modifiers::SHIFT;
for (m, key) in [
(Modifiers::COMMAND, Key::W),
(shift, Key::W),
(Modifiers::COMMAND, Key::Z),
(Modifiers::COMMAND, Key::D),
(Modifiers::COMMAND, Key::F),
(Modifiers::NONE, Key::Delete),
(Modifiers::NONE, Key::Backspace),
] {
h.key_press_modifiers(m, key);
h.run_steps(3);
let app = h.state();
assert_eq!(app.views.len(), 1, "{m:?}+{key:?} closed the file");
assert!(h.query_by_label(dont_save()).is_none(), "{m:?}+{key:?} asked to close the file");
assert!(app.session.get(app.views[0].id).unwrap().can_undo().is_some(), "{m:?}+{key:?} undid the rotation");
assert!(app.views[0].find.is_none(), "{m:?}+{key:?} opened Find");
// The dialog's heading carries its name too: the dialog is the node with its role.
assert!(h.query_by_role_and_label(Role::Dialog, "Preferences").is_some(), "{m:?}+{key:?} replaced Preferences");
}
assert_eq!(page_texts(h.state()), ["Page 1", "Page 2", "Page 3"], "a page was deleted under the dialog");
h.key_press(Key::Escape);
h.run_steps(3);
assert!(h.query_by_role_and_label(Role::Dialog, "Preferences").is_none(), "Escape closes Preferences");
assert_eq!(h.state().views[0].target_pages(), [1], "Escape went to the dialog, not the page selection");
h.key_press_modifiers(Modifiers::COMMAND, Key::W);
h.run_steps(3);
assert!(h.query_all_by_label_contains("Save changes to “doc.pdf”").next().is_some()); // with the dialog closed, ⌘W asks to close the file again
}
#[test]
fn clean_tabs_close_without_asking() {
let mut h = harness(1, |_| {});
h.key_press_modifiers(Modifiers::COMMAND, Key::W);
h.run_steps(3);
assert!(h.state().views.is_empty());
assert!(h.query_by_label(dont_save()).is_none());
}
#[test]
fn quitting_with_unsaved_changes_asks_for_each_document() {
let mut h = harness(2, |app| {
app.open_bytes(&egui::Context::default(), "second.pdf", None, fixture(1)).unwrap();
});
// Edit both documents.
for tab in 0..2 {
h.state_mut().active = Some(tab);
h.state_mut().views[tab].select_pages(&[0]);
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(&ctx_h, pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 });
}
h.key_press_modifiers(Modifiers::COMMAND, Key::Q);
h.run_steps(3);
assert!(h.query_all_by_label_contains("Save changes to “doc.pdf”").next().is_some());
h.get_by_label(dont_save()).click();
h.run_steps(3);
assert!(h.query_all_by_label_contains("Save changes to “second.pdf”").next().is_some());
h.get_by_label(dont_save()).click();
h.run_steps(3);
assert!(h.state().views.is_empty());
assert!(h.query_by_label(dont_save()).is_none(), "nothing more is asked");
}
/// PDFs written to a fresh temp folder, removed when the test ends.
struct TempPdfs(std::path::PathBuf);
impl TempPdfs {
fn new(test: &str) -> Self {
let dir = std::env::temp_dir().join(format!("pdfcraft-session-{test}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
Self(dir)
}
fn file(&self, name: &str, pages: usize) -> String {
let path = self.0.join(name);
std::fs::write(&path, fixture(pages)).unwrap();
path.to_string_lossy().into_owned()
}
}
impl Drop for TempPdfs {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
/// The app with Preferences ▸ Reopen the files that were open when PdfCraft last closed on.
fn session_harness(setup: impl FnOnce(&mut PdfCraftApp) + 'static) -> Harness<'static, PdfCraftApp> {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.reopen_last_session = true;
setup(&mut app);
app
}
});
h.run_steps(4);
h
}
/// [`session_harness`] over a host that runs its jobs on the calling thread: an open finishes within the call
/// that asked for it.
fn inline_session_harness() -> Harness<'static, PdfCraftApp> {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let host = craft::host::Host::builder(crate::control::info()).jobs(std::sync::Arc::new(craft::host::InlineJobs)).build();
let mut app = crate::control::app_on(host);
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.reopen_last_session = true;
app
}
});
h.run_steps(4);
h
}
fn open_paths(app: &PdfCraftApp) -> Vec<String> {
app.views.iter().map(|v| app.session.get(v.id).and_then(|d| d.path.clone()).unwrap_or_default()).collect()
}
#[test]
fn the_last_session_reopens_its_files_pages_and_active_tab() {
let dir = TempPdfs::new("reopen");
let files = [dir.file("a.pdf", 1), dir.file("b.pdf", 5), dir.file("c.pdf", 2)];
let opened = files.clone();
let mut h = session_harness(move |app| {
opened.iter().for_each(|f| {
app.open_path(f);
})
});
h.state_mut().views[1].go_to_page(3);
h.state_mut().active = Some(1);
h.run_steps(4);
assert_eq!(h.state().views[1].current(), 3, "b.pdf shows page 4");
let settings = h.state().settings_snapshot();
let mut h = session_harness(move |app| {
app.apply_settings(&settings);
app.reopen_last_files(&[]);
});
h.run_steps(4);
let app = h.state();
assert_eq!(open_paths(app), files, "the same files, in the same order");
assert_eq!(app.active, Some(1), "b.pdf is in front again");
assert_eq!(app.views[1].current(), 3, "at the page it showed");
}
#[test]
fn quitting_with_unsaved_changes_still_remembers_every_file() {
let dir = TempPdfs::new("quit");
let files = [dir.file("a.pdf", 1), dir.file("b.pdf", 1)];
let opened = files.clone();
let mut h = session_harness(move |app| {
opened.iter().for_each(|f| {
app.open_path(f);
})
});
for tab in 0..2 {
h.state_mut().active = Some(tab);
h.state_mut().views[tab].select_pages(&[0]);
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(&ctx_h, pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 });
}
h.state_mut().active = Some(1);
h.key_press_modifiers(Modifiers::COMMAND, Key::Q);
h.run_steps(3);
// Each unsaved tab closes as it is answered, before PdfCraft quits.
for _ in 0..2 {
h.get_by_label(dont_save()).click();
h.run_steps(3);
}
assert!(h.state().views.is_empty());
let settings = h.state().settings_snapshot();
let mut h = session_harness(move |app| {
app.apply_settings(&settings);
app.reopen_last_files(&[]);
});
support::wait_until(&mut h, "reopened files", |app| app.views.len() == 2);
assert_eq!(open_paths(h.state()), files, "both files come back");
assert_eq!(h.state().active, Some(1), "with b.pdf in front, as when the quit began");
}
#[test]
fn the_last_session_is_off_by_default_and_skips_missing_and_duplicate_files() {
let dir = TempPdfs::new("skip");
let (a, b) = (dir.file("a.pdf", 1), dir.file("b.pdf", 1));
let mut app = crate::control::new_app();
app.open_path(&a);
app.open_path(&b);
assert!(app.settings_snapshot().session.last_session.is_none(), "nothing is kept while the preference is off");
app.reopen_last_session = true;
let settings = app.settings_snapshot();
let mut later = crate::control::new_app();
later.apply_settings(&settings);
later.reopen_last_session = false;
later.reopen_last_files(&[]);
assert!(later.views.is_empty(), "turned off again: nothing reopens");
// b.pdf is gone, and a.pdf is about to open from the command line.
std::fs::remove_file(&b).unwrap();
let mut later = crate::control::new_app();
later.apply_settings(&settings);
later.reopen_last_files(&[a]);
assert!(later.views.is_empty(), "neither opens from the session");
}
#[test]
fn opening_the_same_path_again_focuses_its_tab() {
let pdfs = TempPdfs::new("dedup-open");
let a = pdfs.file("a.pdf", 1);
let b = pdfs.file("b.pdf", 1);
// Every open goes through the open queue and answers `Pending`; its tab appears when the queue is polled. A file
// already in a tab is focused at once.
let mut h = inline_session_harness();
assert_eq!(h.state_mut().open_path(&a), OpenOutcome::Pending);
assert_eq!(h.state_mut().open_path(&b), OpenOutcome::Pending);
h.run_steps(2);
assert_eq!(h.state().views.len(), 2, "two different files open two tabs");
assert_eq!(h.state_mut().open_path(&a), OpenOutcome::Focused, "opening an open file focuses, it doesn't reopen");
h.run_steps(2);
assert_eq!(h.state().views.len(), 2, "opening an open file again adds no tab");
assert_eq!(h.state().active, Some(0), "the existing tab comes to the front");
assert_eq!(open_paths(h.state()), vec![a, b], "the tab order is untouched");
}
#[test]
fn opening_a_missing_file_fails_and_tells_the_user() {
let mut h = session_harness(|_| {});
let missing = std::env::temp_dir().join(format!("pdfcraft-no-such-file-{}.pdf", std::process::id()));
// The open is queued; its failure is reported once the read fails.
assert_eq!(h.state_mut().open_path(&missing.to_string_lossy()), OpenOutcome::Pending);
h.run_steps(4);
assert!(h.state().views.is_empty());
assert!(support::shown_notice(&mut h).is_some(), "the failure is still reported to the user");
}
#[test]
fn opening_the_same_file_with_a_dot_component_focuses_its_tab() {
let pdfs = TempPdfs::new("dedup-dot");
let a = pdfs.file("a.pdf", 1);
let dotted = format!("{}/./a.pdf", pdfs.0.display());
let mut h = inline_session_harness();
h.state_mut().open_path(&a);
h.state_mut().open_path(&dotted);
h.run_steps(2);
assert_eq!(h.state().views.len(), 1, "another spelling of the same file matches");
assert_eq!(h.state().active, Some(0));
}
#[test]
fn opening_a_file_whose_copy_vanished_from_disk_focuses_its_tab() {
let pdfs = TempPdfs::new("dedup-vanished");
let a = pdfs.file("a.pdf", 1);
let mut h = session_harness(|_| {});
h.state_mut().open_path(&a);
// A live window opens through the background worker; the tab has to exist before the file
// is deleted, or the worker itself fails to read what this test is about to remove.
support::wait_until(&mut h, "the vanished file's tab", |app| app.views.len() == 1);
std::fs::remove_file(&a).unwrap();
h.state_mut().open_path(&a);
h.run_steps(2);
assert_eq!(h.state().views.len(), 1, "no second tab");
assert_eq!(h.state().active, Some(0), "the open tab is focused");
assert!(support::shown_notice(&mut h).is_none(), "no read error: the file is never read again");
}
#[test]
fn opening_an_encrypted_document_twice_keeps_one_prompt_and_one_tab() {
let pdfs = TempPdfs::new("dedup-encrypted");
let secret = pdfs.0.join("secret.pdf");
std::fs::write(&secret, protected("pw", "owner", -1)).unwrap();
let s = secret.to_string_lossy().into_owned();
let mut h = inline_session_harness();
h.state_mut().open_path(&s);
h.state_mut().open_path(&s);
h.run_steps(2);
assert!(h.state().views.is_empty(), "still locked");
// One prompt: a second would add a second field.
let field = password_field(&h);
field.focus();
field.type_text("pw");
h.run_steps(2);
h.key_press(Key::Enter);
h.run_steps(4);
assert_eq!(h.state().views.len(), 1, "unlocking opens one tab, not two");
}
#[test]
fn opening_documents_without_a_path_still_open_twice() {
let bytes = fixture(1);
let mut h = session_harness(|_| {});
let ctx_h = h.ctx.clone();
h.state_mut().open_bytes(&ctx_h, "copy.pdf", None, bytes.clone()).unwrap();
let ctx_h = h.ctx.clone();
h.state_mut().open_bytes(&ctx_h, "copy.pdf", None, bytes).unwrap();
h.run_steps(2);
assert_eq!(h.state().views.len(), 2, "bytes have no path to compare (attachments, conversions)");
}
#[test]
fn save_prompt_stays_inside_the_screen_for_a_long_filename() {
// Issue #161: an unwrapped title carrying a long filename widened the centered modal past
// the viewport, clipping the message and pushing the Save/Cancel buttons off-screen.
let name = "Psychology_ The Science of Mind and Behaviour, -- Nigel Holt, Andy Bremner, Michael \
Vliek, Ed Sutherland, -- 5, 2024 -- McGraw-Hill Education (UK) Ltd -- isbn13 97815268.pdf";
let mut h = Harness::builder().with_size(egui::vec2(1365.0, 719.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&cc.egui_ctx, "language", "en").unwrap();
app.open_bytes(&cc.egui_ctx, name, None, fixture(1)).expect("fixture opens");
app.apply_edit(&cc.egui_ctx, pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 });
app.request_close_tab(&cc.egui_ctx, 0);
app
}
});
h.run_steps(4);
let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1365.0, 719.0));
let inside = |r: egui::Rect| screen.contains(r.min) && screen.contains(r.max);
let title = h.query_all_by_label_contains("Save changes to").last().expect("the prompt's title");
let title_rect = title.rect();
assert!(inside(title_rect), "the title rect {title_rect:?} leaves the screen");
for button in [save_answer(), "Cancel", dont_save()] {
let rect = h.query_all_by_label(button).last().unwrap_or_else(|| panic!("no {button} button")).rect();
assert!(inside(rect), "the {button} button rect {rect:?} leaves the screen");
}
}
#[test]
fn save_prompt_fits_the_smallest_window_whatever_the_name() {
// Issue #236: wrapping (#161) kept the prompt narrow, but a long enough name still grew it
// taller than the window and pushed the buttons off-screen. On the web a `?file=` URL names
// the document, so the name has no length limit. Long names now give way in the middle.
let names = [
format!("{}.pdf", "a".repeat(400)),
format!("{}.pdf", "Quarterly_Report_FY2026_Final_v3_".repeat(12)),
format!("{}.pdf", "รายงานประจำปีงบประมาณ".repeat(15)),
format!("{}.pdf", "年".repeat(251)),
format!("{}.pdf", "W".repeat(2000)),
];
// The desktop window's minimum inner size (apps/pdfcraft/src/main.rs).
let size = egui::vec2(820.0, 520.0);
for name in names {
let start: String = name.chars().take(10).collect();
if support::skip_without_glyphs(&format!("save_prompt_fits_the_smallest_window_whatever_the_name ({start}…)"), "en", &name) {
continue;
}
let mut h = Harness::builder().with_size(size).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&cc.egui_ctx, "language", "en").unwrap();
app.open_bytes(&cc.egui_ctx, &name, None, fixture(1)).expect("fixture opens");
app.apply_edit(&cc.egui_ctx, pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 });
app.request_close_tab(&cc.egui_ctx, 0);
app
}
});
h.run_steps(4);
let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, size);
let inside = |r: egui::Rect| screen.contains(r.min) && screen.contains(r.max);
let title = h.query_all_by_label_contains("Save changes to").last().expect("the prompt's title");
let title_rect = title.rect();
assert!(inside(title_rect), "{start}…: the title rect {title_rect:?} leaves the screen");
let opening = format!("Save changes to “{start}");
assert!(
h.query_all_by(|n| n
.label()
.or(n.value())
.is_some_and(|l| l.starts_with(&opening) && l.contains('…') && l.contains(".pdf” before closing?")))
.next()
.is_some(),
"{start}…: the title keeps the start and the extension"
);
for button in [save_answer(), "Cancel", dont_save()] {
let rect = h.query_all_by_label(button).last().unwrap_or_else(|| panic!("no {button} button")).rect();
assert!(inside(rect), "{start}…: the {button} button rect {rect:?} leaves the screen");
}
}
}
#[test]
fn document_properties_edit_is_one_undoable_step() {
let mut h = harness(1, |app| app.set_option(&egui::Context::default(), "dialog", "properties").unwrap());
let title = h.get_by_role_and_label(Role::TextInput, "Title");
title.focus();
title.type_text("Annual report");
h.run_steps(2);
let author = h.get_by_role_and_label(Role::TextInput, "Author");
author.focus();
author.type_text("Finance team");
h.run_steps(2);
h.get_by_label("OK").click();
h.run_steps(3);
let app = h.state();
let doc = app.session.get(app.views[0].id).unwrap();
assert_eq!(doc.info.title.as_deref(), Some("Annual report"), "inspection sees the new title");
assert_eq!(doc.info_value("Author").as_deref(), Some("Finance team"));
assert_eq!(doc.can_undo(), Some("Change document properties"));
assert!(h.query_by_label("OK").is_none(), "the dialog is closed");
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
let app = h.state();
let doc = app.session.get(app.views[0].id).unwrap();
assert_eq!(doc.info.title, None);
assert_eq!(doc.info_value("Author"), None);
}
#[test]
fn cancelling_properties_discards_the_draft() {
let mut h = harness(1, |app| app.set_option(&egui::Context::default(), "dialog", "properties").unwrap());
let title = h.get_by_role_and_label(Role::TextInput, "Title");
title.focus();
title.type_text("Draft");
h.run_steps(2);
h.get_by_label("Cancel").click();
h.run_steps(3);
assert!(!dirty(&h));
assert!(h.query_by_label("OK").is_none(), "the dialog and its draft are gone");
}
fn custom_properties(h: &Harness<'static, PdfCraftApp>) -> Vec<(String, String)> {
let app = h.state();
app.session.get(app.views[0].id).unwrap().custom_info()
}
fn type_into(h: &mut Harness<'static, PdfCraftApp>, label: &str, text: &str) {
let field = h.get_by_role_and_label(Role::TextInput, label);
field.focus();
field.type_text(text);
h.run_steps(2);
}
fn clear(h: &mut Harness<'static, PdfCraftApp>, label: &str) {
h.get_by_role_and_label(Role::TextInput, label).focus();
h.run_steps(1);
h.key_press_modifiers(Modifiers::COMMAND, Key::A);
h.key_press(Key::Backspace);
h.run_steps(2);
}
#[test]
fn custom_properties_are_added_in_the_custom_tab_as_one_undoable_step() {
let mut h = harness(1, |app| app.set_option(&egui::Context::default(), "dialog", "custom").unwrap());
h.get_by_label("None");
type_into(&mut h, "Name:", "Project");
type_into(&mut h, "Value:", "Apollo");
h.get_by_role_and_label(Role::Button, "Add").click();
h.run_steps(3);
assert!(custom_properties(&h).is_empty(), "nothing is written before OK");
h.get_by_label_contains("Project");
type_into(&mut h, "Name:", "Revision");
type_into(&mut h, "Value:", "4");
h.get_by_role_and_label(Role::Button, "Add").click();
h.run_steps(3);
h.get_by_label("OK").click();
h.run_steps(3);
assert_eq!(custom_properties(&h), [("Project".to_string(), "Apollo".to_string()), ("Revision".to_string(), "4".to_string())]);
let app = h.state();
assert_eq!(app.session.get(app.views[0].id).unwrap().can_undo(), Some("Change document properties"));
assert!(h.query_by_label("OK").is_none(), "the dialog and its draft are gone");
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert!(custom_properties(&h).is_empty());
}
#[test]
fn custom_properties_are_changed_and_deleted_in_the_custom_tab() {
let mut h = harness(1, |app| {
for (key, value) in [("Project", "Apollo"), ("Revision", "4")] {
assert!(app.apply_edit(&egui::Context::default(), pdfcraft_engine::Edit::SetInfo { key: key.into(), value: value.into() }));
}
app.set_option(&egui::Context::default(), "dialog", "custom").unwrap();
});
h.get_by_label_contains("Revision").click();
h.run_steps(3);
h.get_by_role_and_label(Role::Button, "Delete").click();
h.run_steps(3);
h.get_by_label_contains("Project").click();
h.run_steps(3);
assert!(h.get_by_role_and_label(Role::Button, "Update").accesskit_node().is_disabled(), "the value is unchanged");
clear(&mut h, "Value:");
type_into(&mut h, "Value:", "Apollo 2");
h.get_by_role_and_label(Role::Button, "Update").click();
h.run_steps(3);
h.get_by_label("OK").click();
h.run_steps(3);
assert_eq!(custom_properties(&h), [("Project".to_string(), "Apollo 2".to_string())]);
let app = h.state();
let doc = app.session.get(app.views[0].id).unwrap();
assert_eq!(doc.info_value("Revision"), None);
assert_eq!(doc.can_undo(), Some("Change document properties"));
}
#[test]
fn the_custom_tab_refuses_names_a_custom_property_cannot_have() {
let mut h = harness(1, |app| {
assert!(app.apply_edit(&egui::Context::default(), pdfcraft_engine::Edit::SetInfo { key: "Project".into(), value: "Apollo".into() }));
app.set_option(&egui::Context::default(), "dialog", "custom").unwrap();
});
type_into(&mut h, "Value:", "x");
let add_disabled = |h: &Harness<'static, PdfCraftApp>| h.get_by_role_and_label(Role::Button, "Add").accesskit_node().is_disabled();
assert!(add_disabled(&h), "no name");
// A standard name, a name with a space, a delimiter, and a name already listed.
for name in ["Title", "Client name", "R&D/2", "Project"] {
type_into(&mut h, "Name:", name);
assert!(add_disabled(&h), "{name} is refused");
clear(&mut h, "Name:");
}
type_into(&mut h, "Name:", "Client");
assert!(!add_disabled(&h));
assert!(h.get_by_role_and_label(Role::Button, "Delete").accesskit_node().is_disabled(), "Client is not listed");
}
#[test]
fn a_custom_property_whose_name_the_tools_refuse_is_listed_and_left_alone() {
// `/Client#20name` and `/Project#20` are valid PDF names with a space in them, which a new custom
// property can't have; `/Project#20` is not `/Project`. `/Dept#E9` is not UTF-8 and is listed as
// "Dept\u{FFFD}". `/Revision` is a number, so it isn't listed.
if crate::support::skip_without_glyphs("a_custom_property_whose_name_the_tools_refuse_is_listed_and_left_alone", "en", "Dept\u{FFFD}") {
return;
}
let pdf = String::from_utf8(fixture(1)).unwrap().replace(
"/Root 1 0 R >>\nstartxref",
"/Root 1 0 R /Info << /Client#20name (Acme) /Project#20 (Padded) /Dept#E9 (Sales) /Project (Apollo) /Revision 4 >> >>\nstartxref",
);
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "doc.pdf", None, pdf.into_bytes()).expect("fixture opens");
app.set_option(&egui::Context::default(), "dialog", "custom").unwrap();
app
}
});
h.run_steps(4);
for name in ["Client name", "Project ", "Dept\u{FFFD}"] {
h.get_by_label(name).click();
h.run_steps(3);
for button in ["Update", "Delete"] {
assert!(h.get_by_role_and_label(Role::Button, button).accesskit_node().is_disabled(), "{button} is off for {name:?}");
}
}
clear(&mut h, "Name:");
type_into(&mut h, "Name:", "Revision");
assert!(h.get_by_role_and_label(Role::Button, "Add").accesskit_node().is_disabled(), "Add would overwrite the number");
assert_eq!(h.get_by_role_and_label(Role::TextInput, "Value:").value().as_deref(), Some("Sales"));
h.get_by_label_contains("Project").click();
h.run_steps(3);
clear(&mut h, "Value:");
type_into(&mut h, "Value:", "Gemini");
h.get_by_role_and_label(Role::Button, "Update").click();
h.run_steps(3);
h.get_by_label("OK").click();
h.run_steps(3);
assert_eq!(
custom_properties(&h),
[
("Client name".to_string(), "Acme".to_string()),
("Project ".to_string(), "Padded".to_string()),
("Dept\u{FFFD}".to_string(), "Sales".to_string()),
("Project".to_string(), "Gemini".to_string())
]
);
let app = h.state();
let doc = app.session.get(app.views[0].id).unwrap();
assert!(doc.has_info("Revision") && doc.info_value("Revision").is_none(), "the number is kept");
assert!(h.query_by_label("OK").is_none());
}
#[test]
fn a_custom_property_is_deleted_from_an_info_dictionary_in_the_trailer() {
let pdf = String::from_utf8(fixture(1))
.unwrap()
.replace("/Root 1 0 R >>\nstartxref", "/Root 1 0 R /Info << /Project (Apollo) /Revision (4) >> >>\nstartxref");
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "doc.pdf", None, pdf.into_bytes()).expect("fixture opens");
app.set_option(&egui::Context::default(), "dialog", "custom").unwrap();
app
}
});
h.run_steps(4);
h.get_by_label_contains("Project").click();
h.run_steps(3);
h.get_by_role_and_label(Role::Button, "Delete").click();
h.run_steps(3);
h.get_by_label("OK").click();
h.run_steps(3);
assert_eq!(custom_properties(&h), [("Revision".to_string(), "4".to_string())]);
let app = h.state();
assert_eq!(app.session.get(app.views[0].id).unwrap().can_undo(), Some("Change document properties"));
}
#[test]
fn add_stops_at_the_256_custom_properties_the_tab_lists() {
let with_props = |count: usize| {
let entries: String = (0..count).map(|n| format!("/P{n:03} (v) ")).collect();
let pdf =
String::from_utf8(fixture(1)).unwrap().replace("/Root 1 0 R >>\nstartxref", &format!("/Root 1 0 R /Info << {entries}>> >>\nstartxref"));
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "doc.pdf", None, pdf.into_bytes()).expect("fixture opens");
app.set_option(&egui::Context::default(), "dialog", "custom").unwrap();
app
}
});
h.run_steps(4);
h
};
let add_disabled = |h: &Harness<'static, PdfCraftApp>| h.get_by_role_and_label(Role::Button, "Add").accesskit_node().is_disabled();
// 255 in the file: one more fits, a second does not.
let mut h = with_props(255);
type_into(&mut h, "Name:", "Client");
type_into(&mut h, "Value:", "Acme");
assert!(!add_disabled(&h));
h.get_by_role_and_label(Role::Button, "Add").click();
h.run_steps(3);
type_into(&mut h, "Name:", "Other");
type_into(&mut h, "Value:", "x");
assert!(add_disabled(&h), "the draft holds 256");
h.get_by_label("OK").click();
h.run_steps(3);
assert!(custom_properties(&h).contains(&("Client".to_string(), "Acme".to_string())));
// 257 in the file: after a Delete a new name would still be past the 256 listed.
let mut h = with_props(257);
h.get_by_label_contains("P000").click();
h.run_steps(3);
h.get_by_role_and_label(Role::Button, "Delete").click();
h.run_steps(3);
type_into(&mut h, "Name:", "Client");
type_into(&mut h, "Value:", "Acme");
assert!(add_disabled(&h), "the file lists 256");
}
#[test]
fn edit_menu_names_the_step_to_undo() {
let mut h = harness(2, |app| {
app.apply_edit(&egui::Context::default(), pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: 90 });
});
support::open_menu(&mut h, "Edit");
support::menu_row(&h, "Undo Rotate page").click();
h.run_steps(3);
let app = h.state();
let doc = app.session.get(app.views[0].id).unwrap();
assert_eq!(doc.can_redo(), Some("Rotate page"));
assert_eq!(doc.info.pages[0].rotation, 0);
}
// ── Combine / insert from file / extract / split ──────────────────────────────────────────────
/// A one-page PDF whose form holds one text field named `name`.
fn form_pdf(name: &str) -> Vec<u8> {
form_pdf_pages(name, 1)
}
/// A PDF of `pages` pages whose form holds one text field named `name` (on the first page).
fn form_pdf_pages(name: &str, pages: usize) -> Vec<u8> {
let kids: Vec<String> = (0..pages).map(|i| format!("{} 0 R", 3 + i)).collect();
let mut objs: Vec<String> = vec![
"<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [4 0 R] >> >>".into(),
format!("<< /Type /Pages /Kids [{}] /Count {pages} /MediaBox [0 0 300 400] >>", kids.join(" ")),
"<< /Type /Page /Parent 2 0 R /Annots [4 0 R] >>".into(),
format!("<< /Type /Annot /Subtype /Widget /FT /Tx /T ({name}) /V (x) /Rect [60 60 200 80] /P 3 0 R >>"),
];
for _ in 1..pages {
objs.push("<< /Type /Page /Parent 2 0 R >>".to_string()); // objects after the field's number
}
let mut out = b"%PDF-1.7\n".to_vec();
let mut offsets = Vec::new();
for (i, o) in objs.iter().enumerate() {
offsets.push(out.len());
out.extend_from_slice(format!("{} 0 obj\n{o}\nendobj\n", i + 1).as_bytes());
}
let xref = out.len();
out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
for o in offsets {
out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
}
out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes());
out
}
/// organize.field-name-conflicts: the Combine tab warns, before anything is combined, that a
/// file's form fields collide with earlier files' and will be renamed, naming the new names.
#[test]
fn combine_list_warns_that_colliding_field_names_will_be_renamed() {
let mut h = harness(1, |app| {
app.set_option(&egui::Context::default(), "combine-view", "list").unwrap();
app.use_files(
&egui::Context::default(),
pdfcraft_ui_egui::FilePurpose::Combine,
vec![("one.pdf".into(), form_pdf("Text1")), ("two.pdf".into(), form_pdf("Text1"))],
);
});
h.run_steps(3);
h.get_by_label_contains("will be renamed");
h.get_by_label_contains("Text1 → Text1-1");
}
/// A file split into parts (its pages moved among other files') is combined as one file, once —
/// so the preview must not warn that its field names collide with their own.
#[test]
fn combine_list_does_not_warn_that_a_split_file_collides_with_itself() {
let mut h = harness(1, |app| {
app.set_option(&egui::Context::default(), "combine-view", "list").unwrap();
app.use_files(&egui::Context::default(), pdfcraft_ui_egui::FilePurpose::Combine, vec![("one.pdf".into(), form_pdf_pages("Text1", 2))]);
});
h.run_steps(3);
// one.pdf in two parts — pages 1 and 2, sharing a group, as moving its pages apart leaves it.
let mut part = h.state().combine_draft[0].clone();
part.id += 1000;
h.state_mut().combine_draft[0].range = "1".into();
part.range = "2".into();
h.state_mut().combine_draft.push(part);
h.run_steps(3);
assert!(h.query_by_label_contains("will be renamed").is_none(), "one file, however many parts, cannot collide with itself");
}
/// Copying a page with a field and pasting it back into the same document is a duplicate: the
/// copy keeps the field's name rather than becoming Text1-1.
#[test]
fn pasting_pages_into_their_own_document_keeps_field_names() {
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "form.pdf", None, form_pdf("Text1")).unwrap();
app.views[0].select_pages(&[0]);
assert!(app.execute(&egui::Context::default(), "page.copy"));
assert!(app.execute(&egui::Context::default(), "page.paste"));
let doc = app.session.get(app.views[0].id).unwrap();
assert_eq!(doc.info.pages.len(), 2, "pasted");
assert!(doc.form.iter().all(|f| f.name == "Text1"), "{:?}", doc.form.iter().map(|f| &f.name).collect::<Vec<_>>());
}
fn texts_of(app: &PdfCraftApp, tab: usize) -> Vec<String> {
let doc = app.session.get(app.views[tab].id).unwrap();
let mut r = PageRenderer::new(doc.bytes.clone(), Default::default());
(0..r.page_count())
.map(|p| {
let out = r.render(RenderRequest { page: p, kind: RequestKind::Text, scale: 1.0, ..Default::default() });
out.text.map(|t| t.plain_text().trim().to_string()).unwrap_or_default()
})
.collect()
}
#[test]
fn combining_files_opens_a_new_unsaved_tab() {
let mut h = harness(1, |app| {
app.use_files(
&egui::Context::default(),
pdfcraft_ui_egui::FilePurpose::Combine,
vec![("one.pdf".into(), fixture(2)), ("two.pdf".into(), fixture(1))],
);
});
h.run_steps(3);
h.get_by_role_and_label(Role::Button, "Combine").click();
h.run_steps(3);
let app = h.state();
assert_eq!(app.views.len(), 2);
assert_eq!(app.active, Some(1));
assert_eq!(texts_of(app, 1), ["Page 1", "Page 2", "Page 1"]);
let doc = app.session.get(app.views[1].id).unwrap();
assert_eq!(doc.name, "Combined.pdf");
assert!(doc.dirty && doc.path.is_none(), "unsaved until the user saves it");
assert_eq!(doc.info.outline.iter().map(|o| o.title.as_str()).collect::<Vec<_>>(), ["one", "two"]);
h.get_by_label("Combined.pdf, modified");
}
#[test]
fn combine_files_takes_chosen_pages_in_the_order_listed() {
let mut h = harness(1, |app| {
app.set_option(&egui::Context::default(), "combine-view", "list").unwrap();
app.use_files(
&egui::Context::default(),
pdfcraft_ui_egui::FilePurpose::Combine,
vec![("one.pdf".into(), fixture(3)), ("two.pdf".into(), fixture(2))],
);
});
h.run_steps(3);
h.get_by_label_contains("Files are combined from top to bottom");
// Nothing is selected yet: the toolbar's Move up is there but disabled.
assert!(h.get_by_label("Move up").accesskit_node().is_disabled());
// Select two.pdf with the keyboard and move it up: two.pdf first; one.pdf's pages 3 and 1.
h.key_press(Key::ArrowUp);
h.run_steps(2);
assert_eq!(h.state().combine_selection(), [1]);
h.get_by_label("Move up").click();
h.run_steps(2);
assert_eq!(h.state().combine_draft[0].name, "two.pdf");
assert_eq!(h.state().combine_selection(), [0], "the selection follows the moved file");
h.state_mut().combine_draft[1].range = "3, 1".into();
h.run_steps(2);
h.get_by_label("2 of 3");
h.get_by_role_and_label(Role::Button, "Combine").click();
h.run_steps(3);
let app = h.state();
assert_eq!(texts_of(app, 1), ["Page 1", "Page 2", "Page 3", "Page 1"]);
let doc = app.session.get(app.views[1].id).unwrap();
assert_eq!(doc.info.outline.iter().map(|o| o.title.as_str()).collect::<Vec<_>>(), ["two", "one"]);
assert!(app.combine_draft.is_empty());
assert!(!app.combine_tab.open, "the tab makes way for the result");
}
#[test]
fn combine_files_opens_in_its_own_tab() {
let mut h = harness(1, |_| {});
h.execute("page.combine");
h.run_steps(3);
assert!(h.state().combine_showing() && h.state().active.is_none());
h.get_by_label_contains("Add the files to combine");
// Back to the document, then to Combine through its tab.
h.get_by_label("doc.pdf").click();
h.run_steps(3);
assert_eq!(h.state().active, Some(0));
assert!(!h.state().combine_showing() && h.state().combine_tab.open);
// The tab, in the tab strip (All tools lists Combine files too).
h.get_all_by_label("Combine files").find(|n| n.rect().top() < 40.0).unwrap().click();
h.run_steps(3);
assert!(h.state().combine_showing());
// Home hides it too.
h.get_by_label("Home").click();
h.run_steps(3);
assert!(!h.state().combine_showing() && h.state().combine_tab.open);
}
#[test]
fn closing_the_combine_tab_forgets_its_list() {
let mut h = harness(1, |app| {
app.use_files(&egui::Context::default(), pdfcraft_ui_egui::FilePurpose::Combine, vec![("one.pdf".into(), fixture(1))]);
});
h.run_steps(3);
assert!(h.state().combine_showing());
h.state_mut().close_combine_tab(&egui::Context::default());
h.run_steps(3);
let app = h.state();
assert!(app.combine_draft.is_empty() && !app.combine_tab.open);
assert_eq!(app.active, Some(0), "the document next to it shows");
}
#[test]
fn combine_lists_size_and_warns_before_combining() {
let mut h = harness(1, |app| {
app.set_option(&egui::Context::default(), "combine-view", "list").unwrap();
app.use_files(
&egui::Context::default(),
pdfcraft_ui_egui::FilePurpose::Combine,
vec![
("one.pdf".into(), fixture(3)),
("locked.pdf".into(), protected("", "owner", -1 ^ 1024 ^ 8)),
("secret.pdf".into(), protected("pw", "owner", -1)),
],
);
});
h.run_steps(3);
h.get_by_label("File name");
h.get_by_label("Warnings");
h.get_by_label("Its security settings don't allow copying pages");
h.get_by_label("Password-protected");
h.get_by_label("2 files need attention");
assert!(h.get_by_role_and_label(Role::Button, "Combine").accesskit_node().is_disabled());
// Removing them (Delete on the selection) lets Combine run.
for _ in 0..2 {
h.state_mut().select_combine_rows(&[1]);
h.key_press(Key::Delete);
h.run_steps(2);
}
assert_eq!(h.state().combine_draft.len(), 1);
h.get_by_label_contains("1 file · 3 pages");
}
#[test]
fn a_bad_range_stops_combine_until_fixed() {
let mut h = harness(1, |app| {
app.use_files(
&egui::Context::default(),
pdfcraft_ui_egui::FilePurpose::Combine,
vec![("one.pdf".into(), fixture(3)), ("two.pdf".into(), fixture(2))],
);
});
h.run_steps(3);
h.get_by_label_contains("2 files · 5 pages");
h.state_mut().combine_draft[0].range = "1-99".into();
h.run_steps(2);
h.get_by_label("1 file needs attention");
assert!(h.get_by_role_and_label(Role::Button, "Combine").accesskit_node().is_disabled());
h.state_mut().combine_draft[0].range = "2-3".into();
h.run_steps(2);
h.get_by_label_contains("2 files · 4 pages");
assert!(!h.get_by_role_and_label(Role::Button, "Combine").accesskit_node().is_disabled());
}
fn combine_names(h: &Harness<'static, PdfCraftApp>) -> Vec<String> {
h.state().combine_draft.iter().map(|f| f.name.clone()).collect()
}
/// Files in the Combine tab's list view (the table: headings, page ranges, row links).
fn combine_of(names: &[(&str, usize)]) -> Harness<'static, PdfCraftApp> {
let files: Vec<(String, Vec<u8>)> = names.iter().map(|(n, p)| (n.to_string(), fixture(*p))).collect();
let mut h = harness(1, move |app| {
app.set_option(&egui::Context::default(), "combine-view", "list").unwrap();
app.use_files(&egui::Context::default(), pdfcraft_ui_egui::FilePurpose::Combine, files);
});
h.run_steps(3);
h
}
#[test]
fn column_headings_sort_the_list_and_flip_on_the_second_click() {
let mut h = combine_of(&[("scan10.pdf", 1), ("Scan2.pdf", 3), ("a.pdf", 2)]);
h.get_by_label("File name").click();
h.run_steps(2);
assert_eq!(combine_names(&h), ["a.pdf", "Scan2.pdf", "scan10.pdf"], "case-insensitive, numbers by value");
h.get_by_label("File name").click();
h.run_steps(2);
assert_eq!(combine_names(&h), ["scan10.pdf", "Scan2.pdf", "a.pdf"]);
// The Pages heading: the Pages panel shares its name.
let heading = h.get_by_label("File name").accesskit_node().role();
h.get_by_role_and_label(heading, "Pages").click();
h.run_steps(2);
assert_eq!(combine_names(&h), ["scan10.pdf", "a.pdf", "Scan2.pdf"]);
// Moving a file by hand ends the sorted order.
h.state_mut().select_combine_rows(&[2]);
h.get_by_label("Move up").click();
h.run_steps(2);
assert_eq!(h.state().combine_tab.sort, None);
h.get_by_role_and_label(heading, "Pages");
}
#[test]
fn ctrl_and_shift_clicks_select_several_files_that_move_and_go_together() {
let mut h = combine_of(&[("a.pdf", 1), ("b.pdf", 1), ("c.pdf", 1), ("d.pdf", 1), ("e.pdf", 1)]);
// The widened Pages editor covers the row's center; click the row outside that editor.
let click_row = |h: &mut Harness<'static, PdfCraftApp>, label: &str, modifiers: Modifiers| {
let rect = h.get_by_role_and_label(Role::ListItem, label).rect();
let at = egui::pos2(rect.left() + 20.0, rect.center().y);
h.input_mut().events.extend([
egui::Event::PointerMoved(at),
egui::Event::ModifiersChanged(modifiers),
egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: true, modifiers },
egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: false, modifiers },
egui::Event::ModifiersChanged(Modifiers::NONE),
]);
h.run_steps(2);
};
click_row(&mut h, "b.pdf", Modifiers::NONE);
click_row(&mut h, "d.pdf", Modifiers::SHIFT);
assert_eq!(h.state().combine_selection(), [1, 2, 3]);
click_row(&mut h, "c.pdf", Modifiers::COMMAND);
assert_eq!(h.state().combine_selection(), [1, 3], "Ctrl/⌘ toggles one file");
h.get_by_label("2 selected");
h.get_by_label("Move up").click();
h.run_steps(2);
assert_eq!(combine_names(&h), ["b.pdf", "a.pdf", "d.pdf", "c.pdf", "e.pdf"]);
h.get_by_label("Remove 2 files").click();
h.run_steps(2);
assert_eq!(combine_names(&h), ["a.pdf", "c.pdf", "e.pdf"]);
h.key_press_modifiers(Modifiers::COMMAND, Key::A);
h.run_steps(1);
assert_eq!(h.state().combine_selection(), [0, 1, 2]);
}
#[test]
fn undo_and_redo_restore_the_combine_list() {
let mut h = combine_of(&[("a.pdf", 1), ("b.pdf", 1), ("c.pdf", 1)]);
h.state_mut().select_combine_rows(&[0, 2]);
h.key_press(Key::Delete);
h.run_steps(2);
assert_eq!(combine_names(&h), ["b.pdf"]);
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(2);
assert_eq!(combine_names(&h), ["a.pdf", "b.pdf", "c.pdf"], "⌘Z brings the removed files back");
assert_eq!(h.state().combine_selection(), [0, 2], "…selected as they were");
h.key_press_modifiers(Modifiers::COMMAND | Modifiers::SHIFT, Key::Z);
h.run_steps(2);
assert_eq!(combine_names(&h), ["b.pdf"]);
// The toolbar's buttons do the same; undo also reverses a sort and the files' addition.
h.get_by_label("Undo").click();
h.run_steps(2);
h.get_by_label("File name").click();
h.run_steps(2);
h.get_by_label("File name").click();
h.run_steps(2);
assert_eq!(combine_names(&h), ["c.pdf", "b.pdf", "a.pdf"]);
h.get_by_label("Undo").click();
h.run_steps(2);
assert_eq!(combine_names(&h), ["a.pdf", "b.pdf", "c.pdf"]);
// A page range typed in is one step.
h.state_mut().combine_draft[0].range.clear();
let field = h.get_all_by_role(Role::TextInput).next().unwrap();
field.focus();
h.run_steps(1);
h.get_all_by_role(Role::TextInput).next().unwrap().type_text("1");
h.run_steps(2);
assert_eq!(h.state().combine_draft[0].range, "1");
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(2);
assert_eq!(h.state().combine_draft[0].range, "");
// The document's own history is untouched: it has nothing to undo.
h.get_by_label("doc.pdf").click();
h.run_steps(2);
assert!(h.state().session.get(h.state().views[0].id).unwrap().can_undo().is_none());
}
/// A protected file that still allows page extraction (only printing is withheld) is listed
/// for Combine with a warning that the result won't keep its security, not as a problem.
#[test]
fn protected_files_that_can_be_combined_are_flagged() {
let mut h = harness(1, |app| {
app.set_option(&egui::Context::default(), "combine-view", "list").unwrap();
app.use_files(
&egui::Context::default(),
pdfcraft_ui_egui::FilePurpose::Combine,
vec![("one.pdf".into(), fixture(1)), ("no-print.pdf".into(), protected("", "owner", -1 ^ 4))],
);
});
h.run_steps(3);
let app = h.state();
assert!(app.combine_draft[1].problem.is_none(), "printing is withheld, extracting pages isn't");
h.get_by_label("Protected: the combined file won't keep its security settings");
assert!(!h.get_by_role_and_label(Role::Button, "Combine").accesskit_node().is_disabled(), "a warning, not a blocker");
}
#[test]
fn the_whole_heading_sorts_and_dragging_it_moves_the_column() {
let mut h = combine_of(&[("b.pdf", 1), ("a.pdf", 3)]);
// A click in the empty right part of the heading cell, away from its text.
let cell = h.get_by_label("File name").rect();
let spot = egui::pos2(cell.right() - 30.0, cell.center().y);
h.hover_at(spot);
h.run_steps(1);
h.drag_at(spot);
h.run_steps(1);
h.drop_at(spot);
h.run_steps(2);
assert_eq!(combine_names(&h), ["a.pdf", "b.pdf"]);
// Drag Size before File name.
let size = h.get_by_label("Size").rect().center();
let name = h.get_by_label("File name").rect();
drag(&mut h, size, egui::pos2(name.left() + 10.0, name.center().y));
let order = ["size", "name", "pages", "modified", "warnings"].map(craft::behavior::ColumnKey::new);
assert_eq!(h.state().combine_columns.order, order);
assert_eq!(combine_names(&h), ["a.pdf", "b.pdf"], "moving a column doesn't sort");
let (size, name) = (h.get_by_label("Size").rect(), h.get_by_label("File name").rect());
assert!(size.left() < name.left(), "Size now shows first");
}
#[test]
fn columns_resize_and_the_layout_is_kept_in_the_settings() {
let mut h = combine_of(&[("a.pdf", 1), ("b.pdf", 1)]);
let size = h.get_by_label("Size").rect();
// The separator after Size: drag it right by 60 points.
let edge = egui::pos2(size.right() - 2.0, size.center().y);
drag(&mut h, edge, edge + egui::vec2(60.0, 0.0));
let widths = h.state().combine_columns.widths.clone();
let size_key = craft::behavior::ColumnKey::new("size");
let size_width = widths.iter().find(|(key, _)| *key == size_key).map(|(_, width)| *width).expect("a resized column keeps its width");
assert!(size_width > 120.0, "{widths:?}");
let saved = h.state().settings_snapshot();
let mut fresh = crate::control::new_app();
fresh.set_option(&egui::Context::default(), "language", "en").unwrap();
fresh.apply_settings(&saved);
assert_eq!(fresh.combine_columns, h.state().combine_columns);
// Malformed settings give the default layout.
let (malformed, _) = pdfcraft_ui_egui::settings::import_json(r#"{"combine_columns":{"order":["size","size"],"widths":[1,2]}}"#);
let mut other = crate::control::new_app();
other.apply_settings(&malformed);
assert_eq!(other.combine_columns, Default::default());
}
#[test]
fn a_folder_adds_its_pdfs_images_and_text_in_order_and_optionally_its_subfolders() {
let dir = std::env::temp_dir().join(format!("pdfcraft-combine-folder-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("sub")).unwrap();
for (name, pages) in [("page10.pdf", 1), ("page2.PDF", 2), ("sub/inner.pdf", 3)] {
std::fs::write(dir.join(name), fixture(pages)).unwrap();
}
// #569: images and text files join too; other files are left out.
std::fs::write(dir.join("notes.txt"), b"Some notes").unwrap();
std::fs::write(dir.join("readme.md"), b"# not combinable").unwrap();
let mut h = harness(1, |_| {});
h.execute("page.combine");
h.run_steps(2);
queue_pick(&[dir.as_path()]);
h.get_by_label("Add folder…").click();
h.run_steps(4);
assert_eq!(combine_names(&h), ["notes.txt", "page2.PDF", "page10.pdf"], "numbers by value");
// The toolbar's menu: the folder and its subfolders, as one undo step.
queue_pick(&[dir.as_path()]);
h.get_by_label("More ways to add files").click();
h.run_steps(2);
h.get_by_label("Add folder and subfolders…").click();
h.run_steps(4);
assert_eq!(combine_names(&h), ["notes.txt", "page2.PDF", "page10.pdf", "notes.txt", "page2.PDF", "page10.pdf", "inner.pdf"]);
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(2);
assert_eq!(combine_names(&h).len(), 3);
let _ = std::fs::remove_dir_all(&dir);
}
/// #569: an image or a text file added to Combine files becomes a PDF as it joins the list, an
/// ordinary row with its pages.
#[test]
fn images_and_text_files_join_the_combine_list_as_pdfs() {
let mut png = std::io::Cursor::new(Vec::new());
image::RgbImage::from_pixel(40, 20, image::Rgb([90, 120, 200])).write_to(&mut png, image::ImageFormat::Png).unwrap();
let mut h = combine_of(&[("one.pdf", 2)]);
h.state_mut().use_files(
&egui::Context::default(),
pdfcraft_ui_egui::FilePurpose::Combine,
vec![("scan.png".into(), png.into_inner()), ("notes.txt".into(), b"Some notes".to_vec())],
);
h.run_steps(3);
assert_eq!(combine_names(&h), ["one.pdf", "scan.png", "notes.txt"]);
let draft = &h.state().combine_draft;
assert!(draft.iter().all(|f| f.bytes.starts_with(b"%PDF-") && f.problem.is_none()), "made into PDFs");
assert_eq!(draft.iter().map(|f| f.pages).collect::<Vec<_>>(), [2, 1, 1]);
h.get_by_role_and_label(Role::Button, "Combine").click();
h.run_steps(3);
assert_eq!(texts_of(h.state(), 1), ["Page 1", "Page 2", "", "Some notes"]);
}
/// A file that can't be read (a damaged image, a type that can't be made into a PDF) joins the
/// list all the same, with why; Combine asks before leaving it out, and Cancel changes nothing.
#[test]
fn files_that_cannot_be_read_are_listed_and_left_out_once_the_user_agrees() {
let mut h = combine_of(&[("one.pdf", 2)]);
let bad = vec![("broken.jpg".to_string(), b"\xff\xd8\xff\xe0 not a JPEG".to_vec()), ("archive.zip".to_string(), b"PK\x03\x04".to_vec())];
let ctx_h = h.ctx.clone();
h.state_mut().use_files(&ctx_h, pdfcraft_ui_egui::FilePurpose::Combine, bad);
h.run_steps(3);
assert_eq!(combine_names(&h), ["one.pdf", "broken.jpg", "archive.zip"]);
for f in &h.state().combine_draft[1..] {
let why = f.problem.as_deref().expect("why it can't be combined");
assert!(why.starts_with("Can't be read: ") && !why.contains(&f.name), "{why}");
assert!(f.lock.is_none() && f.pages == 0);
}
h.get_by_label_contains("this file type can't be converted");
// One readable file is not enough to combine.
assert!(h.get_by_role_and_label(Role::Button, "Combine").accesskit_node().is_disabled());
let ctx_h = h.ctx.clone();
h.state_mut().use_files(&ctx_h, pdfcraft_ui_egui::FilePurpose::Combine, vec![("two.pdf".into(), fixture(1))]);
h.run_steps(3);
assert!(!h.get_by_role_and_label(Role::Button, "Combine").accesskit_node().is_disabled());
// Combine asks first, naming the files left out; Cancel leaves everything as it was.
h.get_by_role_and_label(Role::Button, "Combine").click();
h.run_steps(3);
h.get_by_role_and_label(Role::Dialog, "Leave out files that can't be read?");
h.get_by_label_contains("• broken.jpg");
h.get_by_label_contains("• archive.zip");
h.get_by_label("Cancel").click();
h.run_steps(3);
assert!(h.query_all_by_label("Leave out files that can't be read?").next().is_none());
assert_eq!(h.state().views.len(), 1, "nothing combined");
assert_eq!(combine_names(&h).len(), 4);
// Asked again: Combine the rest.
h.get_by_role_and_label(Role::Button, "Combine").click();
h.run_steps(3);
h.get_by_label("Combine the rest").click();
h.run_steps(3);
assert_eq!(texts_of(h.state(), 1), ["Page 1", "Page 2", "Page 1"]);
}
/// The password dialog's field. Its text widget's own node is hidden in the field's, so the role alone finds two.
fn password_field<'h>(h: &'h Harness<'static, PdfCraftApp>) -> egui_kittest::Node<'h> {
h.get_by_role_and_label(Role::PasswordInput, "Password")
}
/// The password dialog is open and asks for the permissions password (its OK is Unlock), or for the one that opens
/// the file (Open).
fn password_dialog_asks(h: &Harness<'static, PdfCraftApp>, permissions: bool) {
h.get_by_role(Role::Dialog);
let _ = password_field(h);
h.get_by_role_and_label(Role::Button, if permissions { "Unlock" } else { "Open" });
}
fn type_password(h: &mut Harness<'static, PdfCraftApp>, password: &str) {
let field = password_field(h);
field.focus();
field.type_text(password);
h.run_steps(1);
h.key_press(Key::Enter);
h.run_steps(3);
}
#[test]
fn a_password_protected_file_is_unlocked_with_its_password_and_combined() {
let mut h = combine_of(&[("one.pdf", 1)]);
let ctx_h = h.ctx.clone();
h.state_mut().use_files(&ctx_h, pdfcraft_ui_egui::FilePurpose::Combine, vec![("secret.pdf".into(), protected("pw", "owner", -1))]);
h.run_steps(3);
h.get_by_label("Password-protected");
assert!(h.get_by_role_and_label(Role::Button, "Combine").accesskit_node().is_disabled());
// The row's link (the toolbar's Unlock… comes first).
h.get_all_by_label("Unlock…").last().unwrap().click();
h.run_steps(3);
password_dialog_asks(&h, false);
type_password(&mut h, "nope");
h.get_by_label("Wrong password");
assert!(h.state().combine_draft[1].lock.is_some());
// The box stays open for another try.
let field = password_field(&h);
field.focus();
h.key_press_modifiers(Modifiers::COMMAND, Key::A);
h.run_steps(1);
type_password(&mut h, "pw");
let f = &h.state().combine_draft[1];
assert!(f.lock.is_none() && f.problem.is_none());
assert_eq!(f.pages, 2);
h.get_by_label("Unlocked: the combined file won't be password-protected");
// Never printed: debug output ends up in logs.
assert!(!format!("{:?} {:?}", h.state().combine_draft, h.state().combine_tab).contains("pw\""));
h.get_by_role_and_label(Role::Button, "Combine").click();
h.run_steps(3);
assert_eq!(texts_of(h.state(), 1), ["Page 1", "Page 1", "Page 2"]);
}
#[test]
fn one_password_unlocks_several_selected_files_and_undo_locks_them_again() {
let mut h = combine_of(&[("one.pdf", 1)]);
let ctx_h = h.ctx.clone();
h.state_mut().use_files(
&ctx_h,
pdfcraft_ui_egui::FilePurpose::Combine,
vec![
("a.pdf".into(), protected("same", "o1", -1)),
("b.pdf".into(), protected("same", "o2", -1)),
("c.pdf".into(), protected("other", "o3", -1)),
],
);
h.run_steps(3);
h.state_mut().select_combine_rows(&[1, 2, 3]);
h.run_steps(1);
h.get_all_by_label("Unlock…").next().unwrap().click(); // the toolbar's (the rows' links come after it)
h.run_steps(3);
// One dialog asks for the three files' password.
password_dialog_asks(&h, false);
assert!(h.query_all_by_label_contains("3 files").next().is_some(), "the dialog names the three files");
type_password(&mut h, "same");
let locked: Vec<bool> = h.state().combine_draft.iter().map(|f| f.lock.is_some()).collect();
assert_eq!(locked, [false, false, false, true]);
h.get_by_label_contains("Unlocked 2 of 3 files");
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(2);
assert!(h.state().combine_draft[1..].iter().all(|f| f.lock.is_some()), "undo locks them again");
}
#[test]
fn a_file_that_opens_but_forbids_combining_takes_the_permissions_password() {
let mut h = combine_of(&[("one.pdf", 1)]);
// Opens with "pw"; only the owner may assemble pages.
let ctx_h = h.ctx.clone();
h.state_mut().use_files(&ctx_h, pdfcraft_ui_egui::FilePurpose::Combine, vec![("both.pdf".into(), protected("pw", "owner", -1 ^ 1024 ^ 8))]);
h.run_steps(3);
assert!(h.state_mut().combine_unlock_rows(&[1], "pw"));
h.run_steps(2);
assert_eq!(h.state().combine_draft[1].lock, Some(pdfcraft_ui_egui::CombineLock::Permissions));
// The row's message, which the list's table also shows as the row's cell.
let _ = h.get_all_by_label_contains("Enter the permissions password");
h.get_all_by_label("Unlock…").last().unwrap().click();
h.run_steps(3);
password_dialog_asks(&h, true);
type_password(&mut h, "owner");
assert!(h.state().combine_draft[1].lock.is_none());
assert!(!h.get_by_role_and_label(Role::Button, "Combine").accesskit_node().is_disabled());
}
#[test]
fn an_rc4_file_unlocks_with_its_owner_password_too() {
// The engine reads RC4 files with their owner password; the inspector may not, and the
// engine's answer is the one that counts.
let mut h = combine_of(&[("one.pdf", 1)]);
let ctx_h = h.ctx.clone();
h.state_mut().use_files(
&ctx_h,
pdfcraft_ui_egui::FilePurpose::Combine,
vec![("rc4.pdf".into(), protected_with(pdfcraft_cos::Algorithm::Rc4_128, "user", "boss", -1))],
);
h.run_steps(3);
assert!(h.state_mut().combine_unlock_rows(&[1], "boss"));
let f = &h.state().combine_draft[1];
assert!(f.lock.is_none());
assert_eq!(f.pages, 2);
}
#[test]
fn open_documents_can_be_added_to_combine() {
let mut h = harness(2, |_| {});
h.execute("page.combine");
h.run_steps(3);
h.get_by_label("Add open documents").click();
h.run_steps(2);
// The menu's entry, after the tab of the same name.
h.get_all_by_label("doc.pdf").last().unwrap().click();
h.run_steps(3);
let app = h.state();
assert_eq!(app.combine_draft.len(), 1);
assert_eq!(app.combine_draft[0].pages, 2);
assert!(app.combine_showing());
}
#[test]
fn extract_button_copies_selected_pages_to_a_new_tab() {
let mut h = organize(3);
h.get_by_label("Page 2").click();
h.run_steps(1);
h.get_by_label("Page 3").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
h.get_by_label("Extract pages to a new document").click();
h.run_steps(3);
h.get_by_label("2 pages selected.");
h.get_all_by_label("Extract").last().unwrap().click();
h.run_steps(3);
let app = h.state();
assert_eq!(app.views.len(), 2);
assert_eq!(texts_of(app, 1), ["Page 2", "Page 3"]);
assert_eq!(texts_of(app, 0).len(), 3, "the original is unchanged");
assert!(!app.session.get(app.views[0].id).unwrap().dirty);
}
#[test]
fn replace_button_replaces_selected_pages_in_organize() {
let dir = temp_path("replace-btn");
std::fs::create_dir_all(&dir).unwrap();
let other = dir.join("other.pdf");
std::fs::write(&other, fixture(5)).unwrap();
let mut h = organize(3);
h.get_by_label("Page 2").click();
h.run_steps(1);
h.get_by_label("Page 3").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
queue_pick(&[other.as_path()]);
h.get_by_label("Replace").click();
h.run_steps(3);
h.get_by_role_and_label(Role::Dialog, "Replace Pages");
assert_eq!(row_number(&h, "Original: replace pages").as_deref(), Some("2"));
assert_eq!(row_number(&h, "to").as_deref(), Some("3"));
egui_kittest::kittest::Queryable::get_by_label(&h, "OK").click();
h.run_steps(3);
let s = h.state();
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!(doc.info.pages.len(), 3);
assert_eq!(doc.can_undo(), Some("Replace pages"));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn replace_pages_from_thumbnail_context_menu() {
let dir = temp_path("replace-ctx");
std::fs::create_dir_all(&dir).unwrap();
let other = dir.join("other.pdf");
std::fs::write(&other, fixture(5)).unwrap();
let mut h = organize(3);
queue_pick(&[other.as_path()]);
h.get_by_label("Page 2").click_button(egui::PointerButton::Secondary);
h.run_steps(2);
h.get_all_by_label("Replace pages…").last().unwrap().click();
h.run_steps(3);
h.get_by_role_and_label(Role::Dialog, "Replace Pages");
assert_eq!(row_number(&h, "Original: replace pages").as_deref(), Some("2"));
assert_eq!(row_number(&h, "to").as_deref(), Some("2"));
egui_kittest::kittest::Queryable::get_by_label(&h, "OK").click();
h.run_steps(3);
let s = h.state();
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!(doc.info.pages.len(), 3);
assert_eq!(doc.can_undo(), Some("Replace page"));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn inserting_a_file_goes_after_the_selection_and_undoes() {
let mut h = organize(2);
h.get_by_label("Page 1").click();
h.run_steps(2);
let ctx_h = h.ctx.clone();
h.state_mut().use_files(&ctx_h, pdfcraft_ui_egui::FilePurpose::InsertPages, vec![("extra.pdf".into(), fixture(2))]);
h.run_steps(3);
assert_eq!(texts_of(h.state(), 0), ["Page 1", "Page 1", "Page 2", "Page 2"]);
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(texts_of(h.state(), 0), ["Page 1", "Page 2"]);
}
#[test]
fn plus_between_pages_inserts_picked_files_there_in_order() {
let dir = temp_path("insert-gap");
std::fs::create_dir_all(&dir).unwrap();
let (extra, note) = (dir.join("extra.pdf"), dir.join("note.txt"));
std::fs::write(&extra, fixture(2)).unwrap();
std::fs::write(&note, "a note").unwrap();
let mut h = organize(3);
// Page 3 is selected, yet the files go where the "+" is: between pages 1 and 2.
h.get_by_label("Page 3").click();
h.run_steps(2);
queue_pick(&[extra.as_path(), note.as_path()]);
h.get_by_label("Insert a file before page 2").click();
h.run_steps(4);
assert_eq!(texts_of(h.state(), 0), ["Page 1", "Page 1", "Page 2", "a note", "Page 2", "Page 3"]);
assert_eq!(picked(&h), [1, 2, 3], "the inserted pages are selected");
// Each file is one undo step.
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(texts_of(h.state(), 0), ["Page 1", "Page 2", "Page 3"]);
// Both ends.
queue_pick(&[note.as_path()]);
h.get_by_label("Insert a file before page 1").click();
h.run_steps(4);
queue_pick(&[note.as_path()]);
h.get_by_label("Insert a file at the end").click();
h.run_steps(4);
assert_eq!(texts_of(h.state(), 0), ["a note", "Page 1", "Page 2", "Page 3", "a note"]);
// The toolbar button still inserts after the selection, not at the last "+" used.
h.get_by_label("Page 2").click();
h.run_steps(2);
queue_pick(&[note.as_path()]);
insert_menu(&mut h, "Insert pages from a file…");
h.run_steps(4);
assert_eq!(texts_of(h.state(), 0), ["a note", "Page 1", "a note", "Page 2", "Page 3", "a note"]);
// A file that can't be converted changes nothing.
let before = texts_of(h.state(), 0);
let ctx_h = h.ctx.clone();
h.state_mut().use_files(&ctx_h, pdfcraft_ui_egui::FilePurpose::InsertPages, vec![("archive.zip".into(), b"PK\x03\x04".to_vec())]);
h.run_steps(2);
assert_eq!(texts_of(h.state(), 0), before);
}
/// A file dropped on the window, as the windowing layer hands it over.
#[derive(Debug)]
struct Dropped(&'static str, Vec<u8>);
impl egui::DroppedFile for Dropped {
fn path(&self) -> &std::path::Path {
std::path::Path::new(self.0)
}
fn bytes(&self) -> Result<Vec<u8>, String> {
Ok(self.1.clone())
}
}
/// Drops `files` as a platform does: held over the window for a frame, then dropped.
fn drop_files(h: &mut Harness<'static, PdfCraftApp>, files: Vec<Dropped>) {
h.input_mut().hovered_files = files.iter().map(|f| egui::HoveredFile { path: Some(f.0.into()), mime: String::new() }).collect();
h.step();
h.input_mut().hovered_files.clear();
for f in files {
h.input_mut().dropped_files.push(std::sync::Arc::new(f));
}
}
#[test]
fn files_dropped_between_pages_are_inserted_there() {
let mut h = organize(3);
// Over the gap between pages 1 and 2.
let gap = h.get_by_label("Insert a file before page 2").rect().center();
h.hover_at(gap + egui::vec2(0.0, 40.0));
h.run_steps(2);
drop_files(&mut h, vec![Dropped("extra.pdf", fixture(2)), Dropped("note.txt", b"a note".to_vec())]);
h.run_steps(4);
assert_eq!(h.state().views.len(), 1, "dropped files are not opened as documents");
assert_eq!(texts_of(h.state(), 0), ["Page 1", "Page 1", "Page 2", "a note", "Page 2", "Page 3"]);
assert_eq!(picked(&h), [1, 2, 3], "the inserted pages are selected");
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(texts_of(h.state(), 0), ["Page 1", "Page 2", "Page 3"]);
// Right of the last page: the end.
let last = h.get_by_label("Page 3").rect();
h.hover_at(last.center() + egui::vec2(last.width() * 0.4, 0.0));
h.run_steps(2);
drop_files(&mut h, vec![Dropped("note.txt", b"a note".to_vec())]);
h.run_steps(4);
assert_eq!(texts_of(h.state(), 0), ["Page 1", "Page 2", "Page 3", "a note"]);
}
#[test]
fn files_dropped_before_the_pointer_is_known_wait_for_it() {
// Some platforms don't report the pointer while files are dragged over the window.
let mut h = organize(2);
drop_files(&mut h, vec![Dropped("note.txt", b"a note".to_vec())]);
h.run_steps(1);
assert_eq!(texts_of(h.state(), 0).len(), 2, "not placed yet");
let gap = h.get_by_label("Insert a file before page 2").rect().center();
h.hover_at(gap + egui::vec2(0.0, 40.0));
h.run_steps(3);
assert_eq!(texts_of(h.state(), 0), ["Page 1", "a note", "Page 2"]);
}
#[test]
fn files_dropped_outside_the_page_grid_still_open_as_documents() {
let mut h = harness(2, |_| {});
let original = h.state().views[0].id;
drop_files(&mut h, vec![Dropped("other.pdf", fixture(1))]);
support::wait_until(&mut h, "dropped document", |app| app.views.len() == 2 && app.password_prompt.is_none());
assert_eq!(h.state().views.len(), 2);
assert_eq!(texts_of(h.state(), 0), ["Page 1", "Page 2"]);
assert!(h.state().views.iter().any(|view| view.id == original));
assert_eq!(h.state().session.get(original).unwrap().bytes.as_slice(), fixture(2).as_slice());
let (_, id) = h.state().active_ids().unwrap();
let doc = h.state().session.get(id).unwrap();
assert_eq!(doc.name, "other.pdf");
assert_eq!(doc.info.pages.len(), 1);
assert_eq!(doc.bytes.as_slice(), fixture(1).as_slice());
}
/// The Pages panel's thumbnail of page `label` (the panel is right of the page view).
fn panel_thumb_rect(h: &Harness<'static, PdfCraftApp>, label: &str) -> egui::Rect {
h.get_all_by_label(label).map(|n| n.rect()).max_by(|a, b| a.left().total_cmp(&b.left())).unwrap()
}
#[test]
fn files_dropped_on_the_pages_panel_are_inserted_there() {
let mut h = harness(3, |app| app.set_option(&egui::Context::default(), "panel", "pages").unwrap());
// The top half of page 2's thumbnail: between pages 1 and 2.
let thumb = panel_thumb_rect(&h, "Page 2");
h.hover_at(thumb.center() - egui::vec2(0.0, thumb.height() * 0.25));
h.run_steps(2);
h.input_mut().hovered_files = vec![egui::HoveredFile { path: Some("extra.pdf".into()), mime: String::new() }];
h.step();
assert_eq!(h.state().views[0].panel_gap, Some(1));
drop_files(&mut h, vec![Dropped("extra.pdf", fixture(2)), Dropped("note.txt", b"a note".to_vec())]);
h.run_steps(4);
assert_eq!(h.state().views.len(), 1, "dropped files are not opened as documents");
assert_eq!(texts_of(h.state(), 0), ["Page 1", "Page 1", "Page 2", "a note", "Page 2", "Page 3"]);
assert_eq!(picked(&h), [1, 2, 3], "the inserted pages are selected");
// The bottom half of the last thumbnail: the end.
let mut h = harness(2, |app| app.set_option(&egui::Context::default(), "panel", "pages").unwrap());
let last = panel_thumb_rect(&h, "Page 2");
h.hover_at(last.center() + egui::vec2(0.0, last.height() * 0.25));
h.run_steps(2);
drop_files(&mut h, vec![Dropped("note.txt", b"a note".to_vec())]);
h.run_steps(4);
assert_eq!(h.state().views.len(), 1);
assert_eq!(texts_of(h.state(), 0), ["Page 1", "Page 2", "a note"]);
}
#[test]
fn files_dropped_beside_the_pages_panel_still_open_as_documents() {
let mut h = harness(2, |app| app.set_option(&egui::Context::default(), "panel", "pages").unwrap());
// Over the page view, not the panel.
let thumb = panel_thumb_rect(&h, "Page 1");
h.hover_at(egui::pos2(thumb.left() - 300.0, thumb.center().y));
h.run_steps(2);
drop_files(&mut h, vec![Dropped("other.pdf", fixture(1))]);
// Native opening prepares the document on a worker; three UI frames need not give it
// any CPU time. Wait for the result, as the other dropped-document regression does.
support::wait_until(&mut h, "document dropped beside the Pages panel", |app| app.views.len() == 2 && app.password_prompt.is_none());
assert_eq!(h.state().views.len(), 2);
assert_eq!(texts_of(h.state(), 0), ["Page 1", "Page 2"]);
assert_eq!(texts_of(h.state(), 1), ["Page 1"], "the dropped PDF opens as its own document");
}
/// The grid's size: one for the app, shared by every document.
fn grid_zoom(h: &Harness<'static, PdfCraftApp>) -> f64 {
h.state().organize_zoom
}
/// As in Acrobat, Organize pages keeps the size last chosen: every document's grid opens at it,
/// and so does the next start of the app.
#[test]
fn page_grid_size_is_remembered_for_every_document_and_restart() {
let mut h = organize(3);
h.get_by_label("Larger Thumbnails").click();
h.run_steps(3);
assert_eq!(grid_zoom(&h), 1.25);
let ctx_h = h.ctx.clone();
h.state_mut().open_bytes(&ctx_h, "other.pdf", None, fixture(2)).expect("second fixture opens");
h.set_option("organize", "on").unwrap();
h.run_steps(4);
let other = h.state().active.expect("the new document is active");
assert_ne!(other, 0);
assert_eq!(grid_zoom(&h), 1.25, "the other document");
let settings = h.state().settings_snapshot();
let h = harness(3, move |app| {
app.apply_settings(&settings);
app.set_option(&egui::Context::default(), "organize", "on").unwrap();
});
assert_eq!(grid_zoom(&h), 1.25, "after a restart");
}
#[test]
fn zoomed_pages_in_view_are_rendered_at_the_size_drawn() {
// A long document: sharpness must not depend on how many pages there are.
let mut h = harness(400, |app| {
app.set_option(&egui::Context::default(), "organize", "on").unwrap();
app.set_option(&egui::Context::default(), "grid-zoom", "300").unwrap();
});
// The grid keeps each page picture in the shared thumbnail store.
let picture = |h: &Harness<'static, PdfCraftApp>, page: usize| {
use craft::ui::primitives::textures::{Revision, ThumbnailOptions, Thumbnails};
Thumbnails::<usize>::in_context(&h.ctx, "page-thumbnails", ThumbnailOptions::new()).cache().state(&h.ctx, &page, Revision::of(&())).shown()
};
let drawn = |h: &Harness<'static, PdfCraftApp>| {
let texture = picture(h, 0)?;
h.output().shapes.iter().find_map(|s| match &s.shape {
egui::Shape::Mesh(mesh) if mesh.texture_id == texture.id => Some(mesh.calc_bounds().width() * h.ctx.pixels_per_point()),
_ => None,
})
};
for _ in 0..200 {
if picture(&h, 0).zip(drawn(&h)).is_some_and(|(texture, width)| texture.size.width as f32 >= width * 0.9) {
break;
}
std::thread::sleep(std::time::Duration::from_millis(20));
h.run_steps(1);
}
let width = picture(&h, 0).expect("the first page has a sharp render").size.width;
let drawn = drawn(&h).expect("the first page picture is drawn");
assert!((drawn * 0.9..=drawn * 1.6).contains(&(width as f32)), "{width} for {drawn}");
assert!(picture(&h, 399).is_none(), "pages out of view have none");
// Zooming out replaces the sharp picture with a smaller thumbnail.
h.set_option("grid-zoom", "50").unwrap();
for _ in 0..200 {
h.run_steps(1);
if picture(&h, 0).is_some_and(|small| small.size.width < width) {
break;
}
std::thread::sleep(std::time::Duration::from_millis(20));
}
assert!(picture(&h, 0).is_some_and(|small| small.size.width < width), "page 0 still has a sharp render: {:?}", picture(&h, 0));
}
#[test]
fn save_pages_writes_what_the_grid_shows() {
let path = temp_path("grid-save.pdf");
let mut h = organize(3);
h.get_by_label("Page 2").click();
h.run_steps(2);
h.key_press(Key::Delete);
h.run_steps(3);
queue_save(&path);
h.get_by_label("Save pages").click();
h.run_steps(4);
assert!(!dirty(&h));
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "saved.pdf", None, saved_file(&path)).unwrap();
assert_eq!(texts_of(&app, 0), ["Page 1", "Page 3"]);
}
#[test]
fn split_dialog_writes_one_file_per_part() {
let dir = temp_path("split-count");
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let mut h = organize(3);
queue_pick(&[dir.as_path()]);
h.get_all_by_label("Split").last().unwrap().click();
h.run_steps(3);
h.get_by_label_contains("Creates 3 files from 3 pages");
h.get_all_by_label("Split").last().unwrap().click();
h.run_steps(3);
let names = ["doc (page 1).pdf", "doc (page 2).pdf", "doc (page 3).pdf"];
for name in names {
let info = pdfcraft_render::inspect(std::sync::Arc::new(saved_file(&dir.join(name))), None).unwrap();
assert_eq!(info.pages.len(), 1);
}
let _ = std::fs::remove_dir_all(dir);
}
#[test]
fn split_at_bookmarks_with_equal_titles_keeps_every_part() {
let dir = temp_path("split-dup-titles");
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let mut h = organize(3);
queue_pick(&[dir.as_path()]);
for (index, title) in [(0, "Same"), (1, "Same"), (2, "same")] {
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::AddBookmark { parent: vec![], index, title: title.into(), page: index, left: None, top: None },
);
}
assert!(h.execute("page.split"));
h.run_steps(3);
h.get_by_label_contains("Top-level bookmarks").click();
h.run_steps(3);
h.get_by_label_contains("Creates 3 files from 3 pages");
h.get_all_by_label("Split").last().unwrap().click();
h.run_steps(3);
for name in ["doc - Same (2).pdf", "doc - Same.pdf", "doc - same (3).pdf"] {
saved_file(&dir.join(name));
}
let _ = std::fs::remove_dir_all(dir);
}
#[test]
fn split_from_organize_grid_context_menu() {
let dir = temp_path("split-org-ctx");
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let mut h = organize(4);
queue_pick(&[dir.as_path()]);
h.get_by_label("Page 3").click_button(egui::PointerButton::Secondary);
h.run_steps(2);
h.get_all_by_label("Split document…").last().unwrap().click();
h.run_steps(3);
h.get_by_role_and_label(Role::Dialog, "Split document");
// The cut before the selected page is chosen: two parts, not one per page.
h.get_by_label_contains("Creates 2 files from 4 pages");
h.get_all_by_label("Split").last().unwrap().click();
h.run_steps(3);
for name in ["doc (pages 1-2).pdf", "doc (pages 3-4).pdf"] {
saved_file(&dir.join(name));
}
let _ = std::fs::remove_dir_all(dir);
}
#[test]
fn split_from_pages_panel_context_menu_on_page_one_falls_back_to_pages() {
let mut h = harness(4, |app| app.set_option(&egui::Context::default(), "panel", "pages").unwrap());
let thumb = h.get_all_by_label("Page 1").max_by(|a, b| a.rect().left().total_cmp(&b.rect().left())).unwrap();
thumb.click_button(egui::PointerButton::Secondary);
h.run_steps(2);
h.get_all_by_label("Split document…").last().unwrap().click();
h.run_steps(3);
h.get_by_role_and_label(Role::Dialog, "Split document");
h.get_by_label_contains("Creates 4 files from 4 pages"); // by number of pages: the cut is not before page 1
}
#[test]
fn split_from_pages_panel_context_menu_with_multi_selection() {
let mut h = harness(4, |app| app.set_option(&egui::Context::default(), "panel", "pages").unwrap());
// Select pages 2 and 4 (indices 1 and 3)
h.state_mut().views[0].select_pages(&[1, 3]);
h.run_steps(2);
let thumb = h.get_all_by_label("Page 2").max_by(|a, b| a.rect().left().total_cmp(&b.rect().left())).unwrap();
thumb.click_button(egui::PointerButton::Secondary);
h.run_steps(2);
h.get_all_by_label("Split document…").last().unwrap().click();
h.run_steps(3);
h.get_by_role_and_label(Role::Dialog, "Split document");
h.get_by_label_contains("Creates 3 files from 4 pages"); // before each selected page
}
// ── Encrypted documents ───────────────────────────────────────────────────────────────────────
fn protected(user: &str, owner: &str, permissions: i32) -> Vec<u8> {
protected_with(pdfcraft_cos::Algorithm::Aes256, user, owner, permissions)
}
fn protected_with(algorithm: pdfcraft_cos::Algorithm, user: &str, owner: &str, permissions: i32) -> Vec<u8> {
encrypted(fixture(2), algorithm, user, owner, permissions)
}
/// `bytes` rewritten encrypted.
pub(crate) fn encrypted(bytes: Vec<u8>, algorithm: pdfcraft_cos::Algorithm, user: &str, owner: &str, permissions: i32) -> Vec<u8> {
let mut doc = pdfcraft_cos::Document::open(std::sync::Arc::new(bytes)).unwrap();
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
algorithm,
user_password: user,
owner_password: owner,
permissions,
encrypt_metadata: true,
seed: [4; 32],
})
.unwrap();
pdfcraft_cos::write_full(&doc, &Default::default()).unwrap()
}
#[test]
fn launch_display_override_survives_password_retry_without_changing_defaults() {
use pdfcraft_ui_egui::canvas::PageDisplay as PageLayout;
use std::io::Write as _;
let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
let path = temp_path(&format!("startup-layout-{nonce}.pdf"));
let mut file = std::fs::OpenOptions::new().write(true).create_new(true).open(&path).unwrap();
file.write_all(&protected("reader", "owner", -4)).unwrap();
drop(file);
let mut h = session_harness(|app| app.set_option(&egui::Context::default(), "default-layout", "continuous").unwrap());
h.state_mut().open_path_with_display(path.to_str().unwrap(), PageLayout::TwoUp, false);
support::wait_until(&mut h, "password prompt", |app| app.password_prompt.is_some());
assert!(h.state().views.is_empty());
h.state_mut().submit_password(&egui::Context::default(), Some("wrong".into()));
support::wait_until(&mut h, "wrong password", |app| app.password_prompt.as_ref().is_some_and(|p| p.error.is_some()));
assert!(h.state().views.is_empty());
h.state_mut().submit_password(&egui::Context::default(), Some("reader".into()));
support::wait_until(&mut h, "unlocked document", |app| app.views.len() == 1);
assert_eq!(h.state().views[0].view.display, PageLayout::TwoUp);
assert!(!h.state().views[0].continuous());
assert_eq!(h.state().settings_snapshot().layout.default_layout.as_deref(), Some("continuous"));
std::fs::remove_file(path).unwrap();
}
#[test]
fn asynchronous_launch_display_override_survives_retries_and_cancel() {
asynchronous_launch_display(false);
}
#[test]
fn asynchronous_launch_display_explicit_layout_survives_retries_and_cancel() {
asynchronous_launch_display(true);
}
fn asynchronous_launch_display(explicit: bool) {
use pdfcraft_ui_egui::canvas::PageDisplay as PageLayout;
fn wait(h: &mut Harness<'static, PdfCraftApp>, stage: &str, ready: impl Fn(&PdfCraftApp) -> bool) {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
while !ready(h.state()) {
assert!(
std::time::Instant::now() < deadline,
"{stage}: views={}, prompt={}, error={}, notice={}",
h.state().views.len(),
h.state().password_prompt.is_some(),
h.state().password_prompt.as_ref().is_some_and(|p| p.error.is_some()),
support::shown_notice(h).is_some()
);
h.run_steps(2);
std::thread::sleep(std::time::Duration::from_millis(5));
}
}
let path = temp_path(&format!("async-startup-layout-{explicit}.pdf"));
std::fs::write(&path, protected("reader", "owner", -4)).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.set_option(&egui::Context::default(), "default-layout", "continuous").unwrap();
app.open_bytes(&egui::Context::default(), "other.pdf", None, fixture(2)).unwrap();
app
}
});
h.run_steps(2);
let other = h.state().views[0].id;
h.state_mut().open_path_with_display(path.to_str().unwrap(), PageLayout::TwoUp, false);
if explicit {
h.state_mut().set_launch_layout("single").unwrap();
}
wait(&mut h, "password prompt", |app| app.password_prompt.is_some());
// The existing document keeps its default view while this request is pending.
h.state_mut().submit_password(&egui::Context::default(), Some("wrong".into()));
wait(&mut h, "wrong-password result", |app| app.password_prompt.as_ref().is_some_and(|p| p.error.is_some()));
assert_eq!(h.state().views.len(), 1);
h.state_mut().submit_password(&egui::Context::default(), Some("reader".into()));
wait(&mut h, "first unlock", |app| app.views.len() == 2);
assert_eq!(h.state().views[0].id, other);
assert_eq!(h.state().views[0].view.display, PageLayout::Single);
assert!(h.state().views[0].continuous());
assert_eq!(h.state().views[1].view.display, if explicit { PageLayout::Single } else { PageLayout::TwoUp });
assert!(!h.state().views[1].continuous());
let second_path = path.with_extension("second.pdf");
std::fs::copy(&path, &second_path).unwrap();
h.state_mut().open_path_with_display(second_path.to_str().unwrap(), PageLayout::TwoUp, false);
wait(&mut h, "password prompt", |app| app.password_prompt.is_some());
h.state_mut().submit_password(&egui::Context::default(), None);
h.state_mut().open_path(second_path.to_str().unwrap());
wait(&mut h, "password prompt", |app| app.password_prompt.is_some());
h.state_mut().submit_password(&egui::Context::default(), Some("reader".into()));
wait(&mut h, "normal unlock after cancel", |app| app.views.len() == 3);
assert_eq!(h.state().views[2].view.display, PageLayout::Single);
assert!(h.state().views[2].continuous());
assert_eq!(h.state().settings_snapshot().layout.default_layout.as_deref(), Some("continuous"));
std::fs::remove_file(path).unwrap();
std::fs::remove_file(second_path).unwrap();
}
#[test]
fn canceling_launch_display_override_does_not_affect_later_documents() {
use pdfcraft_ui_egui::canvas::PageDisplay as PageLayout;
use std::io::Write as _;
let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
let path = temp_path(&format!("startup-cancel-layout-{nonce}.pdf"));
let mut file = std::fs::OpenOptions::new().write(true).create_new(true).open(&path).unwrap();
file.write_all(&protected("reader", "owner", -4)).unwrap();
drop(file);
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.set_option(&egui::Context::default(), "default-layout", "continuous").unwrap();
app
}
});
h.run_steps(2);
h.state_mut().open_path_with_display(path.to_str().unwrap(), PageLayout::TwoUp, false);
support::wait_until(&mut h, "the password prompt", |app| app.password_prompt.is_some());
assert!(h.state().views.is_empty());
h.state_mut().submit_password(&egui::Context::default(), None);
h.run_steps(2);
assert!(h.state().views.is_empty() && h.state().password_prompt.is_none());
h.state_mut().open_bytes(&egui::Context::default(), "next.pdf", None, fixture(2)).unwrap();
assert_eq!(h.state().views[0].view.display, PageLayout::Single);
assert!(h.state().views[0].continuous());
// Reopening the same protected file normally must not resurrect the canceled override.
h.state_mut().open_path(path.to_str().unwrap());
support::wait_until(&mut h, "the password prompt again", |app| app.password_prompt.is_some());
h.state_mut().submit_password(&egui::Context::default(), Some("reader".into()));
support::wait_until(&mut h, "the unlocked file", |app| app.views.len() == 2);
assert_eq!(h.state().views[1].view.display, PageLayout::Single);
assert!(h.state().views[1].continuous());
assert_eq!(h.state().settings_snapshot().layout.default_layout.as_deref(), Some("continuous"));
std::fs::remove_file(path).unwrap();
}
/// `pdfcraft locked.pdf other.pdf`: the files open before the first frame, and opening the
/// second one inline used to clear the first one's password prompt, so `locked.pdf` was never
/// opened or asked for. The later file now waits behind the prompt.
#[test]
fn a_startup_file_after_a_protected_one_keeps_its_password_prompt() {
for unlock in [true, false] {
let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
let locked = temp_path(&format!("startup-locked-{unlock}-{nonce}.pdf"));
let other = temp_path(&format!("startup-other-{unlock}-{nonce}.pdf"));
std::fs::write(&locked, protected("reader", "owner", -4)).unwrap();
std::fs::write(&other, fixture(2)).unwrap();
let (locked_path, other_path) = (locked.to_str().unwrap().to_string(), other.to_str().unwrap().to_string());
let (first, second) = (locked_path.clone(), other_path.clone());
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_path(&first);
app.open_path(&second);
app
}
});
support::wait_until(&mut h, "the password prompt", |app| app.password_prompt.is_some());
let prompt = h.state().password_prompt.as_ref().map(|p| p.path.clone());
assert_eq!(prompt, Some(Some(locked_path.clone())), "the protected file is asked for");
assert!(h.state().views.is_empty(), "the later file waits behind the prompt");
if unlock {
h.state_mut().submit_password(&egui::Context::default(), Some("reader".into()));
support::wait_until(&mut h, "both files", |app| app.views.len() == 2);
// The password retry has worker priority, so either file can finish first.
// Both must open exactly once; the last admitted tab is the active one.
let mut paths = open_paths(h.state());
paths.sort();
let mut expected = vec![other_path, locked_path];
expected.sort();
assert_eq!(paths, expected);
assert_eq!(h.state().active_ids().map(|(i, _)| i), Some(1));
assert!(h.state().password_prompt.is_none(), "successful unlock leaves no stale prompt");
} else {
h.state_mut().submit_password(&egui::Context::default(), None);
support::wait_until(&mut h, "the other file", |app| app.views.len() == 1);
assert_eq!(open_paths(h.state()), vec![other_path]);
}
std::fs::remove_file(locked).unwrap();
std::fs::remove_file(other).unwrap();
}
}
/// `pdfcraft locked.pdf locked.pdf other.pdf`: the repeated file joins its open password
/// prompt. It used to be queued again behind the prompt, so the unlocked file asked for its
/// password a second time.
#[test]
fn a_protected_file_named_twice_at_startup_asks_for_its_password_once() {
let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
let locked = temp_path(&format!("startup-twice-locked-{nonce}.pdf"));
let other = temp_path(&format!("startup-twice-other-{nonce}.pdf"));
std::fs::write(&locked, protected("reader", "owner", -4)).unwrap();
std::fs::write(&other, fixture(2)).unwrap();
let (locked_path, other_path) = (locked.to_str().unwrap().to_string(), other.to_str().unwrap().to_string());
let (first, second) = (locked_path.clone(), other_path.clone());
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_path(&first);
app.open_path(&first);
app.open_path(&second);
app
}
});
support::wait_until(&mut h, "the password prompt", |app| app.password_prompt.is_some());
assert!(h.state().password_prompt.is_some(), "the protected file is asked for");
h.state_mut().submit_password(&egui::Context::default(), Some("reader".into()));
support::wait_until(&mut h, "both files, or a second prompt", |app| app.views.len() == 2 || app.password_prompt.is_some());
assert!(h.state().password_prompt.is_none(), "the password is asked for once");
let mut paths = open_paths(h.state());
paths.sort();
let mut expected = vec![locked_path, other_path];
expected.sort();
assert_eq!(paths, expected);
std::fs::remove_file(locked).unwrap();
std::fs::remove_file(other).unwrap();
}
/// `pdfcraft locked-a.pdf locked-b.pdf`: B's reply is ready while A's password is asked for.
/// Once A's password is given, A's retry comes first: A opens (or asks again) before B's prompt
/// can take the dialog. B's prompt used to replace A's retry, so A waited for B's answer.
#[test]
fn a_password_retry_finishes_before_the_next_protected_files_prompt() {
let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
let a = temp_path(&format!("startup-retry-a-{nonce}.pdf"));
let b = temp_path(&format!("startup-retry-b-{nonce}.pdf"));
std::fs::write(&a, protected("reader", "owner", -4)).unwrap();
std::fs::write(&b, protected("reader", "owner", -4)).unwrap();
let (a_path, b_path) = (a.to_str().unwrap().to_string(), b.to_str().unwrap().to_string());
let (first, second) = (a_path.clone(), b_path.clone());
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_path(&first);
app.open_path(&second);
app
}
});
support::wait_until(&mut h, "A's password prompt", |app| app.password_prompt.is_some());
// B is prepared in the background meanwhile; its reply waits behind A's prompt.
std::thread::sleep(std::time::Duration::from_millis(300));
h.run_steps(2);
assert_eq!(h.state().password_prompt.as_ref().and_then(|p| p.path.clone()), Some(a_path.clone()));
h.state_mut().submit_password(&egui::Context::default(), Some("reader".into()));
support::wait_until(&mut h, "the next prompt", |app| app.password_prompt.is_some());
assert_eq!(open_paths(h.state()), vec![a_path], "A's retry finished first");
assert_eq!(h.state().password_prompt.as_ref().and_then(|p| p.path.clone()), Some(b_path));
std::fs::remove_file(a).unwrap();
std::fs::remove_file(b).unwrap();
}
/// `pdfcraft locked.pdf` followed by more files than the open worker queues at once: the files
/// past its bound wait in order and open as it drains, instead of failing to open.
#[test]
fn a_long_startup_list_behind_a_password_prompt_opens_every_file() {
const OTHERS: usize = 24;
let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
let locked = temp_path(&format!("startup-long-locked-{nonce}.pdf"));
std::fs::write(&locked, protected("reader", "owner", -4)).unwrap();
let others: Vec<std::path::PathBuf> = (0..OTHERS).map(|i| temp_path(&format!("startup-long-{i:02}-{nonce}.pdf"))).collect();
for path in &others {
std::fs::write(path, fixture(1)).unwrap();
}
let locked_path = locked.to_str().unwrap().to_string();
let other_paths: Vec<String> = others.iter().map(|p| p.to_str().unwrap().to_string()).collect();
let (first, rest) = (locked_path.clone(), other_paths.clone());
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_path(&first);
for path in &rest {
app.open_path(path);
}
app
}
});
support::wait_until(&mut h, "the password prompt", |app| app.password_prompt.is_some());
assert!(h.state().password_prompt.is_some(), "the protected file is asked for");
h.state_mut().submit_password(&egui::Context::default(), Some("reader".into()));
support::wait_until(&mut h, "every file", |app| app.views.len() == OTHERS + 1);
let mut paths = open_paths(h.state());
paths.sort();
let mut expected = other_paths;
expected.push(locked_path);
expected.sort();
assert_eq!(paths, expected);
std::fs::remove_file(locked).unwrap();
for path in others {
std::fs::remove_file(path).unwrap();
}
}
#[test]
fn password_prompt_opens_and_security_tab_reports_the_details() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "secret.pdf", None, protected("pw", "owner", -1)).unwrap();
app
}
});
h.run_steps(4);
h.get_by_label_contains("is protected");
let field = password_field(&h);
field.focus();
field.type_text("pw");
h.run_steps(2);
h.key_press(Key::Enter);
support::wait_until(&mut h, "unlocked document", |app| app.views.len() == 1);
assert_eq!(h.state().views.len(), 1);
h.set_option("dialog", "properties").unwrap();
h.run_steps(2);
h.get_by_label("Security").click();
h.run_steps(3);
h.get_by_label("AES, 256-bit");
h.get_by_label("User password");
}
/// #785: the notice's message used to be laid out before its buttons, so in a narrow document
/// area (a side panel open) it ran under "Security settings" and on into the panel, and it sat
/// off the buttons' centre line.
#[test]
fn the_security_notice_wraps_beside_its_buttons_and_lines_up_with_them() {
fn message_rect(shape: &egui::Shape) -> Option<(egui::Rect, f32)> {
match shape {
egui::Shape::Vec(shapes) => shapes.iter().find_map(message_rect),
egui::Shape::Text(text) if text.galley.text().starts_with("This document is secured") => {
let first = text.galley.rows.first()?;
Some((egui::Rect::from_min_size(text.pos, text.galley.size()), first.pos.y + first.row.size.y / 2.0))
}
_ => None,
}
}
// Wide: one line. Narrower: wrapped. Narrowest: actions stack below the message.
for (width, wraps) in [(1700.0, false), (1600.0, true), (1250.0, false), (900.0, false)] {
let mut h = Harness::builder().with_size(egui::vec2(width, 800.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "locked.pdf", None, protected("", "owner", 0b0100)).unwrap();
app.set_option(&egui::Context::default(), "panel", "bookmarks").unwrap();
app
}
});
h.run_steps(4);
let (msg, first_line_offset) =
h.output().shapes.iter().find_map(|shape| message_rect(&shape.shape)).expect("the security message is painted");
let button = h.get_by_label("Security settings").rect();
let dismiss = h.get_by_label("Dismiss").rect();
if width <= 1250.0 {
assert!(button.top() >= msg.bottom(), "{width}: stacked actions stay below the message: {msg:?} vs {button:?}");
assert!(!button.intersects(dismiss), "{width}: the actions stay apart: {button:?} vs {dismiss:?}");
continue;
}
assert!(msg.right() <= button.left() + 0.5, "{width}: the message stops before the buttons: {msg:?} vs {button:?}");
assert!(button.right() <= dismiss.left() + 0.5, "{width}: {button:?} vs {dismiss:?}");
assert!((button.center().y - dismiss.center().y).abs() <= 0.5, "{width}: the buttons share a centre line");
assert!(msg.top() >= button.top(), "{width}: the message stays inside the bar: {msg:?} vs {button:?}");
let first_line = msg.top() + first_line_offset;
assert!((first_line - button.center().y).abs() <= 1.5, "{width}: first line centred on the buttons: {msg:?} vs {button:?}");
assert_eq!(msg.height() > button.height(), wraps, "{width}: {msg:?}");
}
}
/// #995, #1077: a file with only a permissions password opens without asking, restricted. Its
/// notice offers Unlock…, which takes the permissions password and lifts the restrictions.
#[test]
fn the_security_notice_unlocks_with_the_permissions_password() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "locked.pdf", None, protected("", "owner", 0b0100)).unwrap();
app
}
});
h.run_steps(4);
h.get_by_label_contains("This document is secured");
h.get_by_label("Unlock…").click();
h.run_steps(2);
h.get_by_label_contains("Enter its permissions password");
type_password(&mut h, "wrong");
h.get_by_label("Incorrect password. Try again.");
type_password(&mut h, "owner");
assert!(h.state().unlock_prompt.is_none());
assert!(h.query_by_label_contains("This document is secured").is_none(), "the restrictions are lifted");
h.set_option("dialog", "properties").unwrap();
h.run_steps(2);
h.get_by_label("Security").click();
h.run_steps(3);
h.get_by_label("Owner password (no restrictions apply)");
assert!(!h.state().session.get(h.state().active_ids().unwrap().1).unwrap().dirty, "unlocking is not an edit");
}
/// #4035: the secured-document notice can be turned off for good. "Don't show again" hides it
/// in every document, the choice is kept in the settings and turned back on in Preferences (or
/// `--secured-notice`); the document stays restricted and Properties ▸ Security still opens.
#[test]
fn the_security_notice_can_be_turned_off_for_good() {
let open = |settings: Option<pdfcraft_ui_egui::settings::PdfCraftSettings>| {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
if let Some(s) = &settings {
app.apply_settings(s);
}
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "locked.pdf", None, protected("", "owner", 0b0100)).unwrap();
app
}
});
h.run_steps(4);
h
};
let mut h = open(None);
assert!(h.state().show_secured_notice, "on by default");
h.get_by_label_contains("This document is secured");
h.get_by_label("Don't show again").click();
h.run_steps(2);
assert!(h.query_by_label_contains("This document is secured").is_none());
assert!(!h.state().show_secured_notice);
let id = h.state().active_ids().unwrap().1;
assert!(h.state().session.get(id).unwrap().security_summary().is_some_and(|s| !s.owner), "still restricted");
h.set_option("dialog", "properties").unwrap();
h.run_steps(2);
h.get_by_label("Security").click();
h.run_steps(3);
h.get_by_label("Password security");
h.get_by_label("User password");
// Kept in the settings: a later session opens the same file without the notice.
let saved = h.state().settings_snapshot();
let mut h = open(Some(saved));
assert!(h.query_by_label_contains("This document is secured").is_none(), "remembered across a restart");
// Back on from Preferences.
assert!(h.execute("app.preferences"));
h.run_steps(2);
h.get_by_role_and_label(Role::CheckBox, "Show a notice when a document is secured").scroll_to_me();
h.run_steps(4);
h.get_by_role_and_label(Role::CheckBox, "Show a notice when a document is secured").click();
h.run_steps(2);
assert!(h.state().show_secured_notice);
h.key_press(Key::Escape);
h.run_steps(2);
assert!(h.state().dialogs.is_empty(), "Escape closes Preferences");
h.get_by_label_contains("This document is secured");
// Scriptable, with a clear refusal for anything else.
let app = h.state_mut();
app.set_option(&egui::Context::default(), "secured-notice", "false").unwrap();
assert!(!app.show_secured_notice);
assert_eq!(app.set_option(&egui::Context::default(), "secured-notice", "maybe"), Err("secured-notice must be true or false".into()));
}
/// Changing a restricted document's security asks for its permissions password first (Acrobat
/// does too), then carries on.
#[test]
fn remove_security_asks_for_the_permissions_password_first() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "locked.pdf", None, protected("", "owner", 0b0100)).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("protect.remove"));
h.run_steps(2);
assert!(h.state().unlock_prompt.is_some(), "asks for the password");
// Cancelling changes nothing.
h.key_press(Key::Escape);
h.run_steps(2);
assert!(h.state().unlock_prompt.is_none());
let id = h.state().active_ids().unwrap().1;
assert!(!h.state().session.get(id).unwrap().dirty);
assert!(h.execute("protect.remove"));
h.run_steps(2);
type_password(&mut h, "owner");
let doc = h.state().session.get(id).unwrap();
assert!(doc.dirty && doc.security_summary().is_none(), "security is removed (written by the next save)");
}
/// A secured form still says it is one and offers Highlight fields: the security notice joins
/// the form's instead of replacing it.
#[test]
fn a_secured_form_shows_its_form_notice_and_its_security() {
// Changing and assembling withheld, filling allowed (as government forms are often set).
let bytes = encrypted(include_bytes!("../data/form.pdf").to_vec(), pdfcraft_cos::Algorithm::Aes256, "", "owner", -1036);
let mut h = Harness::builder().with_size(egui::vec2(1600.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "form.pdf", None, bytes).unwrap();
app
}
});
h.run_steps(4);
h.get_by_label_contains("interactive form fields. This document is secured. Some changes are restricted by its security settings.");
h.get_by_label("Hide field highlights");
h.get_by_label("Security settings");
h.get_by_label("Unlock…");
}
#[test]
fn restricted_documents_show_a_notice_and_block_page_changes() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "locked.pdf", None, protected("", "owner", 0b0100)).unwrap(); // opens without a password
app.set_option(&egui::Context::default(), "organize", "on").unwrap();
app
}
});
h.run_steps(4);
h.get_by_label_contains("This document is secured");
h.get_by_label("Page 1").click();
h.run_steps(1);
h.get_by_label("Delete pages (Delete)").click();
h.key_press(Key::Delete);
h.run_steps(3);
assert_eq!(texts_of(h.state(), 0).len(), 2, "page changes are blocked");
assert_eq!(h.query_all_by_label_contains("Insert a file").count(), 0, "nothing to insert into");
// Looking closer is not a page change.
h.get_by_label("Larger Thumbnails").click();
h.run_steps(2);
assert_eq!(grid_zoom(&h), 1.25);
assert!(!dirty(&h));
h.get_by_label("Security settings").click();
h.run_steps(3);
assert!(h.query_all_by_label("Not allowed").count() >= 4);
}
/// Table 22, bit 5 (#568): a document whose security withholds copying keeps its text and images
/// in. ⌘C, snapshots and exports are refused with a notice; the owner password lifts that.
#[test]
fn copying_out_of_a_document_honours_its_security() {
fn open(password: &'static str) -> (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| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = clipboard_app(&cc.egui_ctx, &sink);
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "no-copy.pdf", None, protected("pw", "owner", -1 ^ 0b1_0000)).unwrap();
app.submit_password(&cc.egui_ctx, Some(password.into()));
app
}
});
support::wait_until(&mut h, "unlocked document", |app| !app.views.is_empty());
// One page at a time: Select All takes that page's text (scrolling continuously it would
// take both pages', #2639; what is tested here is the security of copying it).
h.state_mut().views[0].view.continuous = false;
// Select the page's text once its text layer is loaded (up to ~4 s: a loaded test machine
// can take far longer than the 0.2 s this used to allow).
for _ in 0..200 {
h.run_steps(2);
if h.state_mut().views[0].select_all() {
break;
}
std::thread::sleep(std::time::Duration::from_millis(20));
}
assert_eq!(h.state().views[0].selected_text().as_deref(), Some("Page 1"));
(h, copies)
}
fn copy(h: &mut Harness<'static, PdfCraftApp>, copies: &Copies) -> bool {
let before = copies.lock().map_or(0, |c| c.len());
h.event(egui::Event::Copy);
h.run_steps(4);
copies.lock().map_or(0, |c| c.len()) > before && copied_text(copies).as_deref() == Some("Page 1")
}
fn snapshot(h: &mut Harness<'static, PdfCraftApp>) {
// Around "Page 1", at (20, 150) on the 200 × 300 pt page.
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let at = |x: f32, y: f32| egui::pos2(r.left() + x / 200.0 * r.width(), r.top() + (300.0 - y) / 300.0 * r.height());
let (a, b) = (at(15.0, 175.0), at(120.0, 140.0));
h.hover_at(a);
h.run_steps(1);
h.drag_at(a);
h.run_steps(1);
h.hover_at(b);
h.run_steps(1);
h.drop_at(b);
h.run_steps(3);
}
let refused = Some("The document's security settings don't allow copying text and images");
let (mut h, copies) = open("pw");
assert!(!copy(&mut h, &copies), "⌘C copies nothing");
assert_eq!(support::shown_notice(&mut h).as_deref(), refused);
for command in ["edit.snapshot", "export.image", "export.text", "export.docx", "export.pptx", "export.html", "export.rtf", "export.all_images"] {
// A fresh window per command: the notice of the one before would still be showing.
let (mut h, _) = open("pw");
assert!(!h.execute(command), "{command} is refused");
h.run_steps(2);
assert_eq!(support::shown_notice(&mut h).as_deref(), refused, "{command} says why");
assert!(h.state().dialogs.is_empty(), "no export dialog");
}
// The snapshot tool, chosen on another tab, takes nothing here either.
h.set_option("quick", "snapshot").unwrap();
snapshot(&mut h);
assert!(h.state().last_snapshot.is_none());
assert_eq!(support::shown_notice(&mut h).as_deref(), refused);
let (mut h, copies) = open("owner");
assert!(copy(&mut h, &copies), "the owner may copy");
assert!(h.execute("edit.snapshot"));
snapshot(&mut h);
assert!(h.state().last_snapshot.is_some());
}
#[test]
fn replace_pages_dialog_swaps_page_content() {
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "doc.pdf", None, fixture(3)).unwrap();
app.views[0].select_pages(&[1]);
app.start_replace(&egui::Context::default(), "other.pdf".into(), fixture(5));
let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
app
});
h.run_steps(3);
h.get_by_role_and_label(Role::Dialog, "Replace Pages");
type_spin(&mut h, "Replacement: pages of other.pdf", "5");
egui_kittest::kittest::Queryable::get_by_label(&h, "OK").click();
h.run_steps(3);
let s = h.state();
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!(doc.info.pages.len(), 3);
assert_eq!(doc.can_undo(), Some("Replace page"));
}
/// Types `text` into the number field `label` of the open dialog, as events (a key press's `run` checks the
/// dialog's unnamed combo boxes), and leaves the field with its end still being typed.
fn type_spin(h: &mut Harness<'static, PdfCraftApp>, label: &str, text: &str) {
h.get_by_role_and_label(Role::SpinButton, label).click();
h.run_steps(2);
h.event(egui::Event::Key { key: egui::Key::A, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::COMMAND });
for c in text.chars() {
h.event(egui::Event::Text(c.to_string()));
h.run_steps(2);
}
}
/// Pages 1 and 3 are selected. OK replaces those two and leaves the unchecked page between them.
#[test]
fn replace_split_selection_keeps_the_unchecked_page() {
fn sized(n: usize, width: u32, height: u32, label: &str) -> Vec<u8> {
let mut objs: Vec<String> = vec!["<< /Type /Catalog /Pages 2 0 R >>".into()];
let kids: Vec<String> = (0..n).map(|i| format!("{} 0 R", 4 + 2 * i)).collect();
objs.push(format!("<< /Type /Pages /Kids [{}] /Count {n} /MediaBox [0 0 {width} {height}] >>", kids.join(" ")));
objs.push("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".into());
for i in 0..n {
objs.push(format!("<< /Type /Page /Parent 2 0 R /Contents {} 0 R /Resources << /Font << /F1 3 0 R >> >> >>", 5 + 2 * i));
let body = format!("BT /F1 24 Tf 20 150 Td ({label} {}) Tj ET", i + 1);
objs.push(format!("<< /Length {} >>\nstream\n{body}\nendstream", body.len()));
}
let mut out = b"%PDF-1.7\n".to_vec();
let mut offsets = Vec::new();
for (i, o) in objs.iter().enumerate() {
offsets.push(out.len());
out.extend_from_slice(format!("{} 0 obj\n{o}\nendobj\n", i + 1).as_bytes());
}
let xref = out.len();
out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
for o in offsets {
out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
}
out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes());
out
}
let open = |selected: &[usize]| {
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "original.pdf", None, sized(3, 200, 300, "Original")).unwrap();
app.views[0].select_pages(selected);
app.start_replace(&egui::Context::default(), "other.pdf".into(), sized(5, 400, 500, "Replacement"));
let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
app
});
h.run_steps(2);
h
};
let sizes = |app: &PdfCraftApp| {
let doc = app.session.get(app.views[0].id).unwrap();
doc.info.pages.iter().map(|p| (p.width.round() as i32, p.height.round() as i32)).collect::<Vec<_>>()
};
let mut split = open(&[0, 2]);
assert_eq!(row_number(&split, "Original: replace pages").as_deref(), Some("1"));
assert_eq!(row_number(&split, "to").as_deref(), Some("3"));
egui_kittest::kittest::Queryable::get_by_label(&split, "OK").click();
split.run_steps(3);
assert_eq!(texts_of(split.state(), 0), ["Replacement 1", "Original 2", "Replacement 2"]);
assert_eq!(sizes(split.state()), [(400, 500), (200, 300), (400, 500)]);
assert_eq!(split.state().session.get(split.state().views[0].id).unwrap().can_undo(), Some("Replace pages"));
split.state_mut().undo(&egui::Context::default());
split.run_steps(2);
assert_eq!(texts_of(split.state(), 0), ["Original 1", "Original 2", "Original 3"]);
assert_eq!(sizes(split.state()), [(200, 300), (200, 300), (200, 300)]);
// A span typed over the selection replaces that span, whatever was selected.
let mut span = open(&[0, 2]);
type_spin(&mut span, "to", "2");
egui_kittest::kittest::Queryable::get_by_label(&span, "OK").click();
span.run_steps(3);
assert_eq!(texts_of(span.state(), 0), ["Replacement 1", "Replacement 2", "Original 3"]);
let mut adjacent = open(&[1, 2]);
egui_kittest::kittest::Queryable::get_by_label(&adjacent, "OK").click();
adjacent.run_steps(3);
assert_eq!(texts_of(adjacent.state(), 0), ["Original 1", "Replacement 1", "Replacement 2"]);
assert_eq!(sizes(adjacent.state()), [(200, 300), (400, 500), (400, 500)]);
}
#[test]
fn extract_options_and_rotate_pages_dialog() {
let dir = temp_path("extract-sep");
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let mut h = organize(4);
queue_pick(&[dir.as_path()]);
h.state_mut().views[0].select_pages(&[1, 2]);
h.state_mut().run_command(&egui::Context::default(), "page.extract");
h.run_steps(2);
h.get_by_label("Delete pages after extracting").click();
h.get_by_label("Extract pages as separate files").click();
h.run_steps(2);
h.get_all_by_label("Extract").last().unwrap().click();
h.run_steps(3);
for name in ["doc (page 2).pdf", "doc (page 3).pdf"] {
saved_file(&dir.join(name));
}
assert_eq!(texts_of(h.state(), 0), ["Page 1", "Page 4"], "deleted after extracting");
let _ = std::fs::remove_dir_all(dir);
// Rotate Pages: odd page numbers only.
h.state_mut().run_command(&egui::Context::default(), "page.rotate_dialog");
h.run_steps(2);
let odd = craft::pages::PageRange { scope: craft::pages::PageScope::All, subset: craft::pages::Subset::Odd, ..Default::default() };
let ctx = h.ctx.clone();
let rotate = craft::ui::dialog::dialog_id("page.rotate_dialog");
h.state_mut().dialogs.set_json_field(&ctx, rotate, "pages", serde_json::to_value(odd).unwrap()).unwrap();
h.run_steps(1);
h.get_by_label("OK").click();
h.run_steps(3);
let s = h.state();
let d = s.session.get(s.views[0].id).unwrap();
assert_eq!(d.info.pages.iter().map(|p| p.rotation).collect::<Vec<_>>(), [90, 0]);
}
/// #5579: typing the last page of Rotate Pages' range keeps what is typed (croquete1): "12" after
/// a first page of 5 rotates pages 5–12, not page 5 alone. An end typed below the start and left
/// by clicking OK is raised to the start as the field lets go: page 5, not nothing.
#[test]
fn rotate_pages_keeps_a_typed_last_page() {
use egui::accesskit::Role;
for (typed, rotated) in [("12", 5..13), ("3", 5..6)] {
let mut h = organize(20);
h.state_mut().run_command(&egui::Context::default(), "page.rotate_dialog");
h.run_steps(2);
// Pages 5 to 5, as the range's From and To show them.
let range = craft::pages::PageRange { scope: craft::pages::PageScope::Range, from: 5, to: Some(5), ..Default::default() };
let ctx = h.ctx.clone();
let rotate = craft::ui::dialog::dialog_id("page.rotate_dialog");
h.state_mut().dialogs.set_json_field(&ctx, rotate, "pages", serde_json::to_value(range).unwrap()).unwrap();
h.run_steps(1);
h.get_by_role_and_label(Role::SpinButton, "to").click();
h.run_steps(2);
// As events, not `key_press`: its `run` checks the dialog's unnamed combo boxes.
h.event(egui::Event::Key { key: egui::Key::A, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::COMMAND });
for c in typed.chars() {
h.event(egui::Event::Text(c.to_string()));
h.run_steps(2);
}
// OK at once, the end still being typed.
h.get_by_label("OK").click();
h.run_steps(3);
let s = h.state();
let d = s.session.get(s.views[0].id).unwrap();
let turned: Vec<usize> = d.info.pages.iter().enumerate().filter(|(_, p)| p.rotation != 0).map(|(i, _)| i + 1).collect();
assert_eq!(turned, rotated.collect::<Vec<usize>>(), "typed {typed:?}");
}
}
#[test]
fn dragging_thumbnails_reorders_pages() {
let mut h = organize(4);
let before = page_texts(h.state());
let grab = |h: &Harness<'static, PdfCraftApp>, label: &str| h.get_by_label(label).rect();
let (from, to) = (grab(&h, "Page 1").center(), grab(&h, "Page 3").right_center() - egui::vec2(10.0, 0.0));
h.hover_at(from);
h.run_steps(1);
h.drag_at(from);
h.run_steps(1);
for k in 1..=5 {
h.hover_at(from + (to - from) * (k as f32 / 5.0));
h.run_steps(1);
}
h.drop_at(to);
h.run_steps(4);
// Page 1 now sits after page 3.
let after = page_texts(h.state());
assert_eq!(after, vec![before[1].clone(), before[2].clone(), before[0].clone(), before[3].clone()], "{after:?}");
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), Some("Move page"));
let sel: Vec<usize> = h.state().views[0].selected.items.iter().copied().collect();
assert_eq!(sel, vec![2], "the moved page stays selected");
}
/// A page drag with the copy modifier held — Ctrl (Option on macOS) — instead of none.
/// Modifiers are input state: `ModifiersChanged` once, as the column-select drag does.
fn copy_drag(h: &mut Harness<'static, PdfCraftApp>, from: egui::Pos2, to: egui::Pos2) {
page_drag(h, from, to, copy_modifiers(h));
}
/// The copy modifier of the OS the app reports: Option on macOS, Ctrl elsewhere. That is the OS
/// the gesture itself reads, so a Mac browser — a wasm build, where `cfg!(target_os = "macos")` is
/// false — is covered by asking the context rather than the target.
fn copy_modifiers(h: &Harness<'static, PdfCraftApp>) -> Modifiers {
if h.ctx.os() == egui::os::OperatingSystem::Mac { Modifiers::ALT } else { Modifiers::CTRL }
}
/// Drag the page under `from` to `to` with `modifiers` held for the whole gesture.
fn page_drag(h: &mut Harness<'static, PdfCraftApp>, from: egui::Pos2, to: egui::Pos2, modifiers: Modifiers) {
h.event(egui::Event::ModifiersChanged(modifiers));
h.run_steps(1);
h.hover_at(from);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers });
h.run_steps(1);
for k in 1..=5 {
h.hover_at(from + (to - from) * (k as f32 / 5.0));
h.run_steps(1);
}
h.event(egui::Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers });
h.run_steps(4);
h.event(egui::Event::ModifiersChanged(Modifiers::NONE));
h.run_steps(1);
}
#[test]
fn dragging_with_the_copy_modifier_copies_the_pages_into_the_gap() {
let mut h = organize(3);
let before = page_texts(h.state());
let grab = |h: &Harness<'static, PdfCraftApp>, label: &str| h.get_by_label(label).rect();
let (from, to) = (grab(&h, "Page 1").center(), grab(&h, "Page 3").right_center() - egui::vec2(10.0, 0.0));
copy_drag(&mut h, from, to);
// The original stays first; a copy of page 1 lands after page 3 (#2997).
let after = page_texts(h.state());
assert_eq!(after, vec![before[0].clone(), before[1].clone(), before[2].clone(), before[0].clone()], "{after:?}");
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), Some("Duplicate page"));
let sel: Vec<usize> = h.state().views[0].selected.items.iter().copied().collect();
assert_eq!(sel, vec![3], "the copy is selected");
}
/// macOS is not only `cfg!(target_os = "macos")`: the wasm build runs in a Mac browser, where the
/// cfg is false but the context says Mac. The gesture must follow the context, or Option-drag
/// there moves the pages instead of copying them (#3092 review).
#[test]
fn option_copies_and_ctrl_moves_when_the_context_reports_macos() {
let grab = |h: &Harness<'static, PdfCraftApp>, label: &str| h.get_by_label(label).rect();
let points = |h: &Harness<'static, PdfCraftApp>| (grab(h, "Page 1").center(), grab(h, "Page 3").right_center() - egui::vec2(10.0, 0.0));
// ⌘, where Ctrl copies elsewhere, is not the copy modifier on macOS, so that drag moves the page. (Control-press is
// the context click there.)
let mut h = organize(3);
craft::ui::testing::windows::set_os(&h.ctx, craft::host::target::Os::Mac);
let before = page_texts(h.state());
let (from, to) = points(&h);
page_drag(&mut h, from, to, Modifiers { mac_cmd: true, command: true, ..Modifiers::NONE });
assert_eq!(page_texts(h.state()), vec![before[1].clone(), before[2].clone(), before[0].clone()], "⌘ moves on macOS");
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), Some("Move page"));
// Option is, so the same drag drops a copy at the gap.
let mut h = organize(3);
craft::ui::testing::windows::set_os(&h.ctx, craft::host::target::Os::Mac);
let before = page_texts(h.state());
let (from, to) = points(&h);
page_drag(&mut h, from, to, Modifiers::ALT);
assert_eq!(page_texts(h.state()), vec![before[0].clone(), before[1].clone(), before[2].clone(), before[0].clone()], "Option copies on macOS");
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), Some("Duplicate page"));
let sel: Vec<usize> = h.state().views[0].selected.items.iter().copied().collect();
assert_eq!(sel, vec![3], "the copy is selected");
}
#[test]
fn copying_cutting_and_pasting_pages() {
let mut h = organize(4);
let before = page_texts(h.state());
h.state_mut().views[0].select_pages(&[0]);
assert!(h.execute("page.copy"));
h.state_mut().views[0].select_pages(&[2]);
assert!(h.execute("page.paste"));
h.run_steps(2);
let after = page_texts(h.state());
assert_eq!(after, vec![before[0].clone(), before[1].clone(), before[2].clone(), before[0].clone(), before[3].clone()], "pasted after page 3");
let sel: Vec<usize> = h.state().views[0].selected.items.iter().copied().collect();
assert_eq!(sel, vec![3], "the pasted page is selected");
// Cut page 2 and paste it at the end.
h.state_mut().views[0].select_pages(&[1]);
assert!(h.execute("page.cut"));
h.state_mut().views[0].select_pages(&[3]);
assert!(h.execute("page.paste"));
let after = page_texts(h.state());
assert_eq!(after, vec![before[0].clone(), before[2].clone(), before[0].clone(), before[3].clone(), before[1].clone()]);
// Every page can be copied but not cut.
h.state_mut().views[0].select_pages(&[0, 1, 2, 3, 4]);
h.execute("page.cut");
assert_eq!(page_texts(h.state()).len(), 5);
}
fn source_font_fixture() -> Vec<u8> {
source_font_fixture_with_text("BT /F1 18 Tf 20 220 Td (Serif) Tj ET BT /F2 18 Tf 20 120 Td (Mono) Tj ET")
}
fn source_font_fixture_with_text(body: &str) -> Vec<u8> {
let objs: Vec<String> = vec![
"<< /Type /Catalog /Pages 2 0 R >>".into(),
"<< /Type /Pages /Kids [4 0 R] /Count 1 /MediaBox [0 0 300 300] >>".into(),
"<< /Type /Font /Subtype /Type1 /BaseFont /Times-BoldItalic >>".into(),
"<< /Type /Page /Parent 2 0 R /Contents 7 0 R /Resources << /Font << /F1 3 0 R /F2 5 0 R >> >> >>".into(),
"<< /Type /Font /Subtype /Type1 /BaseFont /Courier-Oblique >>".into(),
"<< /Producer (PdfCraft) >>".into(),
format!("<< /Length {} >>\nstream\n{body}\nendstream", body.len()),
];
let mut out = b"%PDF-1.7\n".to_vec();
let mut offsets = Vec::new();
for (i, o) in objs.iter().enumerate() {
offsets.push(out.len());
out.extend_from_slice(format!("{} 0 obj\n{o}\nendobj\n", i + 1).as_bytes());
}
let xref = out.len();
out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
for o in offsets {
out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
}
out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes());
out
}
fn open_source_font_fixture() -> Harness<'static, PdfCraftApp> {
Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "fonts.pdf", None, source_font_fixture()).expect("font fixture opens");
app
}
})
}
#[test]
fn clicking_existing_text_selects_its_source_font_style() {
let mut h = open_source_font_fixture();
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let page = h.state().views[0].page_screen_rect(0).expect("on screen");
let click = |x: f32, y: f32| egui::pos2(page.left() + x / 300.0 * page.width(), page.top() + (300.0 - y) / 300.0 * page.height());
let serif = click(25.0, 228.0);
h.hover_at(serif);
h.run_steps(1);
h.drag_at(serif);
h.run_steps(1);
h.drop_at(serif);
h.run_steps(3);
let ed = h.state().views[0].session.line_editor.clone().expect("serif editor opens");
assert_eq!(ed.look.family, pdfcraft_engine::FontFamily::Times);
assert!(ed.look.bold && ed.look.italic, "source style: {:?}", ed.look);
h.key_press(egui::Key::Escape);
h.run_steps(2);
let mono = click(25.0, 128.0);
h.hover_at(mono);
h.run_steps(1);
h.drag_at(mono);
h.run_steps(1);
h.drop_at(mono);
h.run_steps(3);
let ed = h.state().views[0].session.line_editor.clone().expect("mono editor opens");
assert_eq!(ed.look.family, pdfcraft_engine::FontFamily::Courier);
assert!(!ed.look.bold && ed.look.italic, "source style: {:?}", ed.look);
}
#[test]
fn deleting_a_draft_does_not_show_the_original_text_underneath() {
let _gpu = gpu();
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).unwrap();
let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + 142.0 / 300.0 * r.height());
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(3);
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text.clear();
crate::support::settle(&mut h);
let shot = h.render().unwrap();
if let Ok(path) = std::env::var("PDFCRAFT_TEXT_PREVIEW_SHOT") {
shot.save(path).unwrap();
}
// The right half of the original glyphs, away from the caret and editor border.
let k = r.width() / 200.0;
for y in 152..166 {
for x in 65..82 {
let px = (r.left() + x as f32 * k) as u32;
let py = (r.top() + (300 - y) as f32 * k) as u32;
let [red, green, blue, alpha] = shot.get_pixel(px, py).0;
assert!(
!(red < 100 && green < 100 && blue < 100) && alpha == 255,
"the deleted source is still visible at {x},{y}: {:?}",
[red, green, blue, alpha]
);
}
}
assert!(!dirty(&h), "the document is unchanged until the draft is applied");
assert_eq!(page_texts(h.state()), ["Page 1"]);
h.key_press(egui::Key::Escape);
h.run_steps(3);
assert_eq!(page_texts(h.state()), ["Page 1"]);
}
#[test]
fn switching_from_a_narrow_editor_keeps_one_frame_and_a_wide_heading_on_one_line() {
let _gpu = gpu();
let bytes = source_font_fixture_with_text(
"BT /F1 18 Tf 20 220 Td (Editable PDF heading) Tj ET BT /F2 18 Tf 20 120 Td (Mono) Tj ET BT /F2 18 Tf 20 98 Td (Text) Tj ET",
);
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "editor.pdf", None, bytes).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let page = h.state().views[0].page_screen_rect(0).unwrap();
for y in [128.0, 228.0] {
let point = egui::pos2(page.left() + 25.0 / 300.0 * page.width(), page.top() + (300.0 - y) / 300.0 * page.height());
h.hover_at(point);
h.run_steps(1);
h.drag_at(point);
h.run_steps(1);
h.drop_at(point);
h.run_steps(4);
if y == 128.0 {
assert_eq!(
h.state().views[0].session.line_editor.as_ref().unwrap().text,
"Mono\nText",
"opening a paragraph keeps its source line breaks"
);
h.key_press(Key::Escape);
h.run_steps(2);
}
}
// Unchanged text is painted by the page; an edit exposes the editor's layout.
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text.push('!');
h.run_steps(3);
let heading = h
.output()
.shapes
.iter()
.find_map(|shape| match &shape.shape {
egui::Shape::Text(text) if text.galley.text() == "Editable PDF heading!" => {
Some((text.galley.rect.translate(text.pos.to_vec2()), &text.galley))
}
_ => None,
})
.expect("the heading editor is painted");
assert_eq!(heading.1.rows.len(), 1, "opening a wider block must not reuse the previous editor's narrow width");
let frame_stroke = h
.output()
.shapes
.iter()
.rev()
.find_map(|shape| match &shape.shape {
egui::Shape::Path(path) if path.closed && egui::Rect::from_points(&path.points).contains(heading.0.min) => Some(path.stroke.clone()),
_ => None,
})
.expect("the heading has an outline");
let frames = h
.output()
.shapes
.iter()
.filter_map(|shape| match &shape.shape {
egui::Shape::Path(path) if path.closed && path.stroke == frame_stroke => {
let rect = egui::Rect::from_points(&path.points);
(rect.width() > 100.0 && page.contains(rect.center())).then_some(rect)
}
_ => None,
})
.count();
assert_eq!(frames, 1, "the active block has only the editor frame");
if let Ok(path) = std::env::var("PDFCRAFT_TEXT_FRAME_SHOT") {
h.render().unwrap().save(path).unwrap();
}
assert!(!dirty(&h));
}
/// Clicking a paragraph changes nothing on the page but the editor's frame and caret: the page's
/// own text stays until something is typed. The editor used to cover the paragraph (white, or
/// dark for pale text) until its textless background was ready, and drew it again in its own fonts.
#[test]
fn opening_a_paragraph_leaves_the_page_as_it_is() {
let _gpu = gpu();
let bytes = source_font_fixture_with_text("0 0 0.45 rg 10 180 280 70 re f 1 1 1 rg BT /F2 20 Tf 20 205 Td (White on a banner) Tj ET");
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "banner.pdf", None, bytes).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
crate::support::settle(&mut h);
let page = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = page.width() / 300.0;
let block = h.state().session.get(h.state().views[0].id).unwrap().text_blocks(0)[0].rect;
let r = egui::Rect::from_min_max(
egui::pos2(page.left() + block[0] as f32 * k, page.top() + (300.0 - block[3] as f32) * k),
egui::pos2(page.left() + block[2] as f32 * k, page.top() + (300.0 - block[1] as f32) * k),
);
// Click the text body: the width control owns the band around its edges.
let at = r.center();
h.hover_at(at);
h.run_steps(2);
let before = h.render().unwrap();
// Inside the paragraph, away from the caret (at the click) and the frame around it.
let inside = r.shrink(3.0);
let changed = |img: &image::RgbaImage| {
let mut n = 0;
for y in inside.top() as u32..inside.bottom() as u32 {
for x in inside.left() as u32..inside.right() as u32 {
if (x as f32 - at.x).abs() < 14.0 {
continue;
}
let (a, b) = (img.get_pixel(x, y).0, before.get_pixel(x, y).0);
if a.iter().zip(b.iter()).take(3).any(|(p, q)| (i32::from(*p) - i32::from(*q)).abs() > 40) {
n += 1;
}
}
}
n
};
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
for frame in 0..8 {
h.step();
let n = changed(&h.render().unwrap());
assert!(n < 20, "frame {frame} after the click changed {n} pixels of the paragraph");
}
assert_eq!(h.state().views[0].session.line_editor.as_ref().map(|e| e.text.as_str()), Some("White on a banner"));
}
/// A quick click (press and release in one frame) from an open, unchanged paragraph straight into
/// another one: in no frame does the first paragraph lose its text, and the new editor's frame
/// never spans the first one (it took the first paragraph's measured extent for a frame).
#[test]
fn a_quick_click_into_another_paragraph_keeps_both_in_place() {
let _gpu = gpu();
let bytes = source_font_fixture_with_text("BT /F1 24 Tf 20 220 Td (Top) Tj ET BT /F2 24 Tf 20 120 Td (Bottom) Tj ET");
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "boxes.pdf", None, bytes).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let page = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = page.width() / 300.0;
let mut blocks = {
let s = h.state();
s.session.get(s.views[0].id).unwrap().text_blocks(0)
};
blocks.sort_by(|a, b| b.rect[1].total_cmp(&a.rect[1]));
let screen = |b: &pdfcraft_engine::TextBlock| {
egui::Rect::from_min_max(
egui::pos2(page.left() + b.rect[0] as f32 * k, page.top() + (300.0 - b.rect[3] as f32) * k),
egui::pos2(page.left() + b.rect[2] as f32 * k, page.top() + (300.0 - b.rect[1] as f32) * k),
)
};
let (top, bottom) = (screen(&blocks[0]), screen(&blocks[1]));
click_at(&mut h, top.center());
crate::support::settle(&mut h);
assert_eq!(h.state().views[0].session.line_editor.as_ref().map(|e| e.text.as_str()), Some("Top"));
let ink = |pixels: &image::RgbaImage, r: egui::Rect| {
(r.top() as u32..r.bottom() as u32)
.flat_map(|y| (r.left() as u32..r.right() as u32).map(move |x| (x, y)))
.filter(|(x, y)| pixels.get_pixel(*x, *y).0[..3].iter().all(|v| *v < 90))
.count()
};
h.hover_at(bottom.center());
h.run_steps(1);
let before = ink(&h.render().unwrap(), top.shrink(2.0));
assert!(before > 50, "the top paragraph shows");
for pressed in [true, false] {
h.event(egui::Event::PointerButton { pos: bottom.center(), button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE });
}
for frame in 0..4 {
h.step();
let now = ink(&h.render().unwrap(), top.shrink(2.0));
assert!(now * 2 > before, "frame {frame}: the top paragraph lost its text ({now} of {before} dark pixels)");
for shape in &h.output().shapes {
if let egui::Shape::Rect(r) = &shape.shape
&& r.stroke.width == 1.5
&& r.rect.contains(bottom.center())
{
assert!(!r.rect.intersects(top), "frame {frame}: the bottom editor's frame {:?} spans the top paragraph {top:?}", r.rect);
}
}
}
assert_eq!(h.state().views[0].session.line_editor.as_ref().map(|e| e.text.as_str()), Some("Bottom"));
}
/// #2022: clicking straight into another text box while one is open for typing must apply the
/// open paragraph first; the typed text used to be dropped with the editor that switched away.
#[test]
fn switching_straight_to_another_box_applies_the_open_paragraph() {
let bytes = source_font_fixture_with_text("BT /F1 24 Tf 20 220 Td (Top) Tj ET BT /F2 24 Tf 20 120 Td (Bottom) Tj ET");
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "boxes.pdf", None, bytes).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 300.0;
let mut blocks = {
let s = h.state();
s.session.get(s.views[0].id).unwrap().text_blocks(0)
};
blocks.sort_by(|a, b| b.rect[1].partial_cmp(&a.rect[1]).unwrap_or(std::cmp::Ordering::Equal));
assert_eq!(blocks.iter().map(|b| b.text.as_str()).collect::<Vec<_>>(), ["Top", "Bottom"], "two editable boxes");
let center = |b: &pdfcraft_engine::TextBlock| {
egui::pos2(r.left() + ((b.rect[0] + b.rect[2]) / 2.0) as f32 * k, r.top() + (300.0 - (b.rect[1] + b.rect[3]) / 2.0) as f32 * k)
};
// Open the top box and type into it.
click_at(&mut h, center(&blocks[0]));
let ed = h.state().views[0].session.line_editor.clone().expect("the top box opens for typing");
assert_eq!(ed.text, "Top");
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = "Top edited".into();
h.run_steps(1);
// Click straight into the bottom box: the open paragraph is applied, and the bottom box opens.
click_at(&mut h, center(&blocks[1]));
let s = h.state();
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!(
doc.text_blocks(0).iter().map(|b| b.text.clone()).collect::<Vec<_>>(),
["Top edited", "Bottom"],
"the typed text is applied, not dropped"
);
assert_eq!(doc.can_undo(), Some("Edit text"));
let ed = s.views[0].session.line_editor.clone().expect("the bottom box is now open");
assert_eq!(ed.text, "Bottom");
}
// Regression test for #811: text typed into an open paragraph was not counted as unsaved work, so
// closing the tab or quitting discarded it without the save prompt.
#[test]
fn text_typed_into_an_open_paragraph_counts_as_unsaved_work() {
let bytes = source_font_fixture_with_text("BT /F1 24 Tf 20 220 Td (Top) Tj ET");
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "box.pdf", None, bytes).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 300.0;
let block = {
let s = h.state();
s.session.get(s.views[0].id).unwrap().text_blocks(0).into_iter().next().expect("one editable box")
};
let center = egui::pos2(
r.left() + ((block.rect[0] + block.rect[2]) / 2.0) as f32 * k,
r.top() + (300.0 - (block.rect[1] + block.rect[3]) / 2.0) as f32 * k,
);
click_at(&mut h, center);
assert_eq!(h.state().views[0].session.line_editor.as_ref().map(|e| e.text.as_str()), Some("Top"), "the paragraph is open for typing");
assert_eq!(h.state().first_dirty(), None, "opening a paragraph changes nothing");
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = "Top, and a draft not applied yet".into();
h.run_steps(1);
assert!(!h.state().session.get(h.state().views[0].id).unwrap().dirty, "the document itself is untouched until the draft is applied");
assert_eq!(h.state().first_dirty(), Some(0), "the draft in the open paragraph is unsaved work");
// Typing the original text back leaves nothing to lose.
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = "Top".into();
h.run_steps(1);
assert_eq!(h.state().first_dirty(), None, "a draft equal to the page text is not unsaved work");
}
/// Dark pixels in `r`: glyph ink on a white page.
fn ink(pixels: &image::RgbaImage, r: egui::Rect) -> usize {
let (x0, y0, x1, y1) = (r.left().max(0.0) as u32, r.top().max(0.0) as u32, r.right() as u32, r.bottom() as u32);
(y0..y1.min(pixels.height()))
.flat_map(|y| (x0..x1.min(pixels.width())).map(move |x| (x, y)))
.filter(|(x, y)| pixels.get_pixel(*x, *y).0[..3].iter().all(|v| *v < 90))
.count()
}
/// Applying a paragraph changes the page, whose raster still has the old text until it is
/// rendered again. Until then the paragraph keeps the look it had in the editor; the old text
/// used to come back under the new outline for those frames and then jump to the new one.
#[test]
fn an_applied_paragraph_keeps_its_edited_look_until_the_page_is_redrawn() {
let _gpu = gpu();
let bytes = source_font_fixture_with_text("BT /F1 24 Tf 20 220 Td (Topmost) Tj ET BT /F2 24 Tf 20 120 Td (Bottom) Tj ET");
// The jobs, the page renders among them, wait until the test runs them.
let (app, jobs) = crate::control::app_with_manual_jobs();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = app;
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "boxes.pdf", None, bytes).expect("opens");
app
}
});
h.run_steps(4);
support::settle_held(&mut h, &jobs);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 300.0;
let top = {
let s = h.state();
let blocks = s.session.get(s.views[0].id).unwrap().text_blocks(0);
let b = blocks.iter().find(|b| b.text == "Topmost").expect("the top box").rect;
egui::Rect::from_min_max(
egui::pos2(r.left() + b[0] as f32 * k, r.top() + (300.0 - b[3] as f32) * k),
egui::pos2(r.left() + b[2] as f32 * k, r.top() + (300.0 - b[1] as f32) * k),
)
};
// "T" stays; the rest of the word goes.
let head = egui::Rect::from_min_max(top.min, egui::pos2(top.left() + top.width() * 0.12, top.bottom()));
let tail = egui::Rect::from_min_max(egui::pos2(top.left() + top.width() * 0.3, top.top()), top.max).shrink(2.0);
click_at(&mut h, top.center());
// The editor's textless page.
support::settle_held(&mut h, &jobs);
h.state_mut().views[0].session.line_editor.as_mut().expect("the top box is open").text = "T".into();
h.run_steps(2);
// From here the renders wait: the frames below see the page as it was before applying.
h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);
for frame in 0..4 {
h.step();
assert!(h.state().views[0].session.line_editor.is_none(), "applied");
let pixels = h.render().unwrap();
assert!(ink(&pixels, head) > 20, "frame {frame} after applying shows no text");
assert_eq!(ink(&pixels, tail), 0, "frame {frame} after applying shows the old text again");
}
// The page drawn with the edit.
support::settle_held(&mut h, &jobs);
h.run_steps(2);
let pixels = h.render().unwrap();
assert!(ink(&pixels, head) > 20, "the page draws the applied text");
assert_eq!(ink(&pixels, tail), 0, "and not the old one");
let s = h.state();
assert_eq!(s.session.get(s.views[0].id).unwrap().text_blocks(0).iter().map(|b| b.text.clone()).collect::<Vec<_>>(), ["T", "Bottom"]);
}
/// A paragraph whose edit fails (here a letter no font can draw) stays open with what was typed,
/// as a form field does; the editor used to close with the error and the typing was lost.
#[test]
fn a_paragraph_whose_edit_fails_stays_open_with_its_typing() {
if crate::support::skip_without_glyphs("a_paragraph_whose_edit_fails_stays_open_with_its_typing", "en", "Top ก") {
return;
}
let bytes = source_font_fixture_with_text("BT /F1 24 Tf 20 220 Td (Top) Tj ET BT /F2 24 Tf 20 120 Td (Bottom) Tj ET");
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "boxes.pdf", None, bytes).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 300.0;
let top = egui::pos2(r.left() + 30.0 * k, r.top() + (300.0 - 228.0) * k);
click_at(&mut h, top);
assert_eq!(h.state().views[0].session.line_editor.as_ref().map(|e| e.text.as_str()), Some("Top"));
// U+0E01 (Thai): neither the page's font nor a fallback face draws it.
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = "Top ก".into();
h.run_steps(1);
h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);
h.run_steps(3);
let notice = support::shown_notice(&mut h);
let s = h.state();
assert!(notice.as_ref().is_some_and(|m| m.starts_with("Edit text failed")), "the failure is said: {notice:?}");
assert_eq!(s.session.get(s.views[0].id).unwrap().can_undo(), None, "nothing was applied");
let ed = s.views[0].session.line_editor.as_ref().expect("the paragraph is still open");
assert_eq!((ed.block, ed.text.as_str()), (0, "Top ก"), "with what was typed");
// Taking the letter out applies it.
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = "Top edited".into();
h.run_steps(1);
h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);
h.run_steps(3);
let s = h.state();
assert!(s.views[0].session.line_editor.is_none());
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!(doc.text_blocks(0).iter().map(|b| b.text.clone()).collect::<Vec<_>>(), ["Top edited", "Bottom"]);
}
/// Two boxes, "Top" and "Bottom", in Edit text, with egui's animations on: the window animates
/// unless the system asks for less motion, and the test harness turns them off.
fn two_boxes_animated() -> (Harness<'static, PdfCraftApp>, [egui::Rect; 2]) {
let bytes = source_font_fixture_with_text("BT /F1 24 Tf 20 220 Td (Top) Tj ET BT /F2 24 Tf 20 120 Td (Bottom) Tj ET");
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 60.0).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.set_option(&egui::Context::default(), "theme", "light").unwrap();
app.open_bytes(&egui::Context::default(), "boxes.pdf", None, bytes).expect("opens");
app
}
});
h.ctx.all_styles_mut(|s| s.animation_time = 0.2);
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 300.0;
let mut blocks = {
let s = h.state();
s.session.get(s.views[0].id).unwrap().text_blocks(0)
};
blocks.sort_by(|a, b| b.rect[1].partial_cmp(&a.rect[1]).unwrap_or(std::cmp::Ordering::Equal));
let on_screen = |b: &pdfcraft_engine::TextBlock| {
egui::Rect::from_min_max(
egui::pos2(r.left() + b.rect[0] as f32 * k, r.top() + (300.0 - b.rect[3] as f32) * k),
egui::pos2(r.left() + b.rect[2] as f32 * k, r.top() + (300.0 - b.rect[1] as f32) * k),
)
};
(h, [on_screen(&blocks[0]), on_screen(&blocks[1])])
}
/// The colour of the editor's frame (the accent stroke around `r`) this frame, if it is painted.
fn editor_frame_color(h: &Harness<'static, PdfCraftApp>, r: egui::Rect, line: egui::Color32) -> Option<egui::Color32> {
h.output().shapes.iter().find_map(|shape| match &shape.shape {
egui::Shape::Rect(rect) if rect.stroke.width == 1.5 && rect.rect.contains(r.center()) => Some(rect.stroke.color),
egui::Shape::Path(path) if path.closed && egui::Rect::from_points(&path.points).contains(r.center()) => match &path.stroke.color {
egui::epaint::ColorMode::Solid(color) if (color.r(), color.g(), color.b()) == (line.r(), line.g(), line.b()) => Some(*color),
_ => None,
},
_ => None,
})
}
/// A paragraph opens at once: it used to fade in over a fifth of a second, its frame dimmed and
/// the page's own copy of the text showing through the editor's cover.
#[test]
fn a_paragraph_editor_opens_at_full_opacity() {
let (mut h, [top, _]) = two_boxes_animated();
h.hover_at(top.center());
h.run_steps(1);
h.drag_at(top.center());
h.run_steps(1);
h.drop_at(top.center());
let theme = craft::theme::builtins().iter().find(|theme| theme.id.as_str() == "craft.graphite").unwrap();
let appearance = craft::theme::Appearance {
mode: craft::theme::Mode::Light,
contrast: craft::theme::Contrast::Standard,
size: Default::default(),
text_size: Default::default(),
reduced_motion: false,
};
let [r, g, b, a] = craft::theme::resolve(theme, appearance, None).tokens.color.overlay.line.0;
assert_eq!(a, 255, "the theme's outline is opaque");
let accent = egui::Color32::from_rgba_unmultiplied(r, g, b, a);
for frame in 0..8 {
h.step();
// The click's own frame opens it: the editor's frame is there from then on, at full colour.
assert_eq!(editor_frame_color(&h, top, accent), Some(accent), "frame {frame} after the click");
}
}
/// One US Letter page of Helvetica, for driving the live editor on a known baseline.
fn letter_text(body: &str) -> Vec<u8> {
let objs = [
"<< /Type /Catalog /Pages 2 0 R >>".to_string(),
"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_string(),
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>".to_string(),
format!("<< /Length {} >>\nstream\n{body}\nendstream", body.len()),
"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".to_string(),
];
let mut out = b"%PDF-1.7\n".to_vec();
let mut offsets = Vec::new();
for (i, object) in objs.iter().enumerate() {
offsets.push(out.len());
out.extend_from_slice(format!("{} 0 obj\n{object}\nendobj\n", i + 1).as_bytes());
}
let xref = out.len();
out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
for offset in offsets {
out.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes());
}
out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes());
out
}
fn open_letter(name: &'static str, bytes: Vec<u8>) -> Harness<'static, PdfCraftApp> {
let mut h = Harness::builder().with_size(egui::vec2(1100.0, 1400.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), name, None, bytes).unwrap();
app
}
});
h.run_steps(4);
h
}
fn click_page(h: &mut Harness<'static, PdfCraftApp>, x: f32, y: f32) {
let page = h.state().views[0].page_screen_rect(0).expect("the page is on screen");
let at = egui::pos2(page.left() + x / 612.0 * page.width(), page.top() + (792.0 - y) / 792.0 * page.height());
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(4);
}
fn save_live_shot(h: &mut Harness<'static, PdfCraftApp>, name: &str) {
for _ in 0..40 {
h.run_steps(2);
if !h.state().render_pending() {
break;
}
}
let dir = std::path::Path::new("/tmp/pdfcraft-visual");
let _ = std::fs::create_dir_all(dir);
if let Ok(image) = h.render() {
let _ = image.save(dir.join(name));
}
}
/// Live editor: a repeated step with no TL opens as one paragraph, and adding a row
/// beside a kept first row leaves the neighbour as its own paragraph on screen.
#[test]
fn the_live_editor_groups_a_repeated_step_and_keeps_the_neighbour() {
let step = 27.6_f32;
let repeated = format!(
"BT /F1 12 Tf 72 700 Td (One) Tj ET BT /F1 12 Tf 72 {} Td (Two) Tj ET BT /F1 12 Tf 72 {} Td (Three) Tj ET",
700.0 - step,
700.0 - 2.0 * step
);
let mut h = open_letter("repeated.pdf", letter_text(repeated.as_str()));
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
click_page(&mut h, 90.0, 700.0 - step + 4.0);
let editor = h.state().views[0].session.line_editor.clone().expect("the middle row opens");
assert!(editor.text.contains("One") && editor.text.contains("Two") && editor.text.contains("Three"), "{}", editor.text);
save_live_shot(&mut h, "live-repeated-step.png");
let partial = "BT /F1 12 Tf 14.4 TL 1 0 0 1 72 700 Tm (Alpha first row.) Tj T* (Alpha next row.) Tj ET \
BT /F1 12 Tf 14.4 TL 1 0 0 1 72 656.8 Tm (Neighbor stays.) Tj ET";
let mut h = open_letter("partial.pdf", letter_text(partial));
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
click_page(&mut h, 120.0, 704.0);
let editor = h.state().views[0].session.line_editor.clone().expect("the first paragraph opens");
assert!(editor.text.contains("Alpha first row.") && editor.text.contains("Alpha next row."), "{}", editor.text);
assert!(!editor.text.contains("Neighbor"), "{}", editor.text);
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = "Alpha first row.\nTwo\nThree".into();
h.run_steps(1);
h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);
h.run_steps(6);
let blocks = {
let s = h.state();
let doc = s.session.get(s.views[0].id).unwrap();
doc.text_blocks(0).iter().map(|b| b.text.clone()).collect::<Vec<_>>()
};
assert_eq!(blocks, ["Alpha first row. Two Three", "Neighbor stays."]);
save_live_shot(&mut h, "live-after-adding-a-row.png");
click_page(&mut h, 120.0, 660.0);
let neighbour = h.state().views[0].session.line_editor.clone().expect("the neighbour opens on its own");
assert_eq!(neighbour.text, "Neighbor stays.");
save_live_shot(&mut h, "live-neighbour-open.png");
}
/// Widening example 1 must not pull the heading into that box or split the paragraph under example 2.
#[test]
fn widening_example_one_keeps_the_heading_and_the_next_paragraph() {
let body = "BT /F1 14 Tf 72 740 Td (1. Repeated step, no TL. Click Two.) Tj ET \
BT /F1 12 Tf 72 700 Td (One) Tj ET \
BT /F1 12 Tf 72 672.4 Td (Two) Tj ET \
BT /F1 12 Tf 72 644.8 Td (Three) Tj ET \
BT /F1 14 Tf 72 590 Td (2. Blank line. Edit the first paragraph.) Tj ET \
BT /F1 12 Tf 14.4 TL 1 0 0 1 72 550 Tm (First paragraph, first row.) Tj T* (First paragraph, second row.) Tj ET \
BT /F1 12 Tf 14.4 TL 1 0 0 1 72 506.8 Tm (Second paragraph, first row.) Tj T* (Second paragraph, second row.) Tj ET \
BT /F1 14 Tf 72 450 Td (3. Add a row under Alpha. Neighbor stays.) Tj ET \
BT /F1 12 Tf 14.4 TL 1 0 0 1 72 410 Tm (Alpha first row.) Tj T* (Alpha next row.) Tj ET \
BT /F1 12 Tf 14.4 TL 1 0 0 1 72 366.8 Tm (Neighbor stays.) Tj ET";
let mut h = open_letter("letter.pdf", letter_text(body));
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
click_page(&mut h, 90.0, 690.0);
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = "One\nTwo\nThree\nfour".into();
h.run_steps(1);
h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);
h.run_steps(8);
let (edge, page_id) = {
let s = h.state();
let id = s.views[0].id;
let blocks = s.session.get(id).unwrap().text_blocks(0);
let texts: Vec<_> = blocks.iter().map(|b| b.text.clone()).collect();
assert!(texts.iter().any(|t| t == "1. Repeated step, no TL. Click Two."), "commit already joined the heading: {texts:?}");
assert!(
texts.iter().any(|t| t.contains("First paragraph, first row") && t.contains("second row")),
"commit already split the next paragraph: {texts:?}"
);
let block = blocks.iter().find(|b| b.text.contains("four")).expect("the edited paragraph");
let page = s.views[0].page_screen_rect(0).unwrap();
let edge = egui::pos2(
page.left() + block.rect[2] as f32 / 612.0 * page.width(),
page.top() + (792.0 - (block.rect[1] + block.rect[3]) as f32 / 2.0) / 792.0 * page.height(),
);
(edge, id)
};
let to = edge + egui::vec2(280.0, 0.0);
h.hover_at(edge);
h.run_steps(1);
h.drag_at(edge);
h.run_steps(1);
for k in 1..=6 {
h.hover_at(edge + (to - edge) * (k as f32 / 6.0));
h.run_steps(1);
}
h.drop_at(to);
h.run_steps(8);
let texts: Vec<_> = {
let s = h.state();
s.session.get(page_id).unwrap().text_blocks(0).iter().map(|b| b.text.clone()).collect()
};
let edited = {
let s = h.state();
s.session
.get(page_id)
.unwrap()
.text_blocks(0)
.iter()
.find(|b| b.editor_text.contains("four"))
.expect("the widened paragraph")
.editor_text
.clone()
};
assert_eq!(edited, "One\nTwo\nThree\nfour", "the width handle flattened the line breaks: {edited:?}");
assert!(texts.iter().any(|t| t == "1. Repeated step, no TL. Click Two."), "the width handle joined the heading: {texts:?}");
assert!(
texts.iter().any(|t| t.contains("First paragraph, first row") && t.contains("First paragraph, second row")),
"the width handle split the next paragraph: {texts:?}"
);
save_live_shot(&mut h, "live-example-one-widened.png");
}
/// The live width handle, after a row was added on top of the neighbour.
#[test]
fn resizing_the_live_box_keeps_the_neighbour() {
let partial = "BT /F1 12 Tf 14.4 TL 1 0 0 1 72 410 Tm (Alpha first row.) Tj T* (Alpha next row.) Tj ET \
BT /F1 12 Tf 14.4 TL 1 0 0 1 72 366.8 Tm (Neighbor stays.) Tj ET";
let mut h = open_letter("partial.pdf", letter_text(partial));
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
click_page(&mut h, 120.0, 414.0);
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = "Alpha first row.\nthis is another\nalpha test\nAlpha next row.".into();
h.run_steps(1);
h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);
h.run_steps(6);
let before = {
let s = h.state();
s.session.get(s.views[0].id).unwrap().text_blocks(0).iter().map(|b| b.text.clone()).collect::<Vec<_>>()
};
assert!(before.iter().any(|t| t.contains("Neighbor stays.")), "the edit already dropped it: {before:?}");
let page = h.state().views[0].page_screen_rect(0).unwrap();
let rect = h.state().session.get(h.state().views[0].id).unwrap().text_blocks(0)[0].rect;
let edge = egui::pos2(
page.left() + rect[2] as f32 / 612.0 * page.width(),
page.top() + (792.0 - (rect[1] + rect[3]) as f32 / 2.0) / 792.0 * page.height(),
);
let to = edge + egui::vec2(80.0, 0.0);
h.hover_at(edge);
h.run_steps(1);
h.drag_at(edge);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(edge + (to - edge) * (k as f32 / 4.0));
h.run_steps(1);
}
h.drop_at(to);
h.run_steps(6);
let after = {
let s = h.state();
s.session.get(s.views[0].id).unwrap().text_blocks(0).iter().map(|b| b.text.clone()).collect::<Vec<_>>()
};
assert!(after.iter().any(|t| t.contains("Neighbor stays.")), "resize removed the neighbour: {after:?}");
save_live_shot(&mut h, "live-after-resize.png");
}
#[test]
fn editing_existing_text_in_place() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
// The line "Page 1" sits at (20, 150) on a 200 × 300 page, 24 pt.
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height());
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(3);
let ed = h.state().views[0].session.line_editor.clone().expect("the line opens for editing");
assert_eq!((ed.page, ed.block, ed.text.as_str()), (0, 0, "Page 1"));
// The source container stays visible while added text can overflow it.
assert!(ed.growth().is_none(), "opening the editor preserves the source container");
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = "Chapter One".into();
h.run_steps(1);
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::Enter);
h.run_steps(4);
let s = h.state();
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!(doc.can_undo(), Some("Edit text"));
assert_eq!(doc.text_lines(0)[0].text, "Chapter One");
assert_eq!(texts_of(s, 0), ["Chapter One"], "the page shows it");
}
/// Under the Edit tab, opens the line "Page 1" (at (20, 150) on a 200 × 300 page, 24 pt) in the
/// paragraph editor and retypes it as "Chapter One", leaving the editor open.
fn edit_tab_with_an_open_paragraph(h: &mut Harness<'static, PdfCraftApp>) {
use pdfcraft_ui_egui::{Mode, QuickTool};
support::outside_menus(h, "Edit").click();
h.run_steps(3);
assert_eq!((h.state().mode, h.state().quick_tool), (Mode::Edit, QuickTool::EditText));
open_the_first_line(h);
}
/// With Edit text & images on, opens the line "Page 1" in the paragraph editor and retypes it as
/// "Chapter One", leaving the editor open.
fn open_the_first_line(h: &mut Harness<'static, PdfCraftApp>) {
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height());
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(3);
assert_eq!(h.state().views[0].session.line_editor.as_ref().expect("the line opens for editing").text, "Page 1");
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = "Chapter One".into();
h.run_steps(1);
}
/// Edit was left with the paragraph [`edit_tab_with_an_open_paragraph`] opened: Edit text & images
/// is put down, and the paragraph's edit is applied instead of its editor staying open over the page.
fn assert_edit_text_put_down_and_the_paragraph_applied(s: &PdfCraftApp) {
assert_eq!(s.quick_tool, pdfcraft_ui_egui::QuickTool::Select, "Edit text & images is put down");
assert!(s.views[0].session.line_editor.is_none(), "no paragraph editor is left open");
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!(doc.can_undo(), Some("Edit text"), "the open paragraph's edit is applied");
assert_eq!(doc.text_lines(0)[0].text, "Chapter One");
assert_eq!(texts_of(s, 0), ["Chapter One"], "the page shows it");
}
/// The Read mode command (View ▸ Read mode, its shortcut, the palette) leaves Edit as the Edit tab
/// does when another tab is chosen (#2241): Edit text & images is put down, and a paragraph still
/// open in the editor is applied instead of staying open, editable, over the page in Read mode.
#[test]
fn the_read_mode_command_applies_an_open_paragraph_and_puts_edit_text_down() {
let mut h = harness(1, |_| {});
edit_tab_with_an_open_paragraph(&mut h);
assert!(h.execute("view.read_mode"));
h.run_steps(3);
assert_eq!(h.state().mode, pdfcraft_ui_egui::Mode::Read);
assert_edit_text_put_down_and_the_paragraph_applied(h.state());
}
/// The Edit panel's back arrow (it has one when opened from All tools; the Edit tab is its own way
/// back) leaves for All tools as the mode bar does: Edit text & images is put down and a paragraph
/// still open in the editor is applied, instead of every paragraph keeping its box, and the editor
/// staying open, under All tools.
#[test]
fn the_edit_panel_back_arrow_applies_an_open_paragraph_and_puts_edit_text_down() {
use pdfcraft_ui_egui::{LeftPanel, Mode, QuickTool};
let mut h = harness(1, |_| {});
// All tools ▸ Edit a PDF ▸ Edit text & images: the Edit panel, under All tools.
assert!(h.execute("edit.edit_text"));
h.run_steps(3);
assert_eq!((h.state().mode, h.state().quick_tool), (Mode::AllTools, QuickTool::EditText));
open_the_first_line(&mut h);
h.get_by_label("Back to all tools").click();
h.run_steps(3);
assert_eq!((h.state().mode, h.state().left), (Mode::AllTools, LeftPanel::AllTools));
assert_edit_text_put_down_and_the_paragraph_applied(h.state());
}
/// Organize pages, opened under the Edit tab, leaves Edit the same way through its own back arrow.
#[test]
fn the_organize_back_arrow_applies_an_open_paragraph_and_puts_edit_text_down() {
use pdfcraft_ui_egui::{LeftPanel, Mode};
let mut h = harness(1, |_| {});
edit_tab_with_an_open_paragraph(&mut h);
// What the Edit panel's Organize pages row runs.
assert!(h.execute("page.organize"));
h.run_steps(3);
assert!(h.state().views[0].organize, "the page grid opens");
assert!(h.state().views[0].session.line_editor.is_some(), "the paragraph is still open behind the grid");
h.get_by_label("Back to all tools").click();
h.run_steps(3);
let s = h.state();
assert_eq!((s.mode, s.left), (Mode::AllTools, LeftPanel::AllTools));
assert!(!s.views[0].organize, "the grid closes");
assert_edit_text_put_down_and_the_paragraph_applied(s);
}
#[test]
fn text_tool_clicking_source_text_edits_instead_of_adding_a_copy() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.text"));
h.run_steps(2);
let page = h.state().views[0].page_screen_rect(0).unwrap();
let blank = egui::pos2(page.left() + 0.2 * page.width(), page.top() + 0.1 * page.height());
h.hover_at(blank);
h.run_steps(1);
h.drag_at(blank);
h.run_steps(1);
h.drop_at(blank);
h.run_steps(3);
h.event(egui::Event::Text("Keep this note".into()));
h.run_steps(3);
let point = egui::pos2(page.left() + 40.0 / 200.0 * page.width(), page.top() + 142.0 / 300.0 * page.height());
h.hover_at(point);
h.run_steps(1);
h.drag_at(point);
h.run_steps(1);
h.drop_at(point);
h.run_steps(4);
assert!(h.state().views[0].session.content.draft.is_none(), "clicking source text must not start an added layer");
assert_eq!(h.state().views[0].session.line_editor.as_ref().unwrap().text, "Page 1");
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().added().len(), 1, "the previous draft is kept");
h.key_press_modifiers(Modifiers::COMMAND, Key::A);
h.event(egui::Event::Text("Replacement".into()));
h.run_steps(3);
h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);
h.run_steps(3);
assert_eq!(page_texts(h.state()), ["Keep this note\nReplacement"]);
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().added().len(), 1, "editing the source adds no second layer");
}
#[test]
fn text_tool_mixed_style_row_grows_in_the_editor_and_saved_pdf() {
let _gpu = gpu();
let bytes = source_font_fixture_with_text("BT /F1 12 Tf 20 220 Td (Small ) Tj /F2 12 Tf (row) Tj ET");
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "mixed-row.pdf", None, bytes).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let page = h.state().views[0].page_screen_rect(0).unwrap();
let point = egui::pos2(page.left() + 25.0 / 300.0 * page.width(), page.top() + 75.0 / 300.0 * page.height());
h.hover_at(point);
h.run_steps(1);
h.drag_at(point);
h.run_steps(1);
h.drop_at(point);
h.run_steps(4);
let replacement = "Small row with more words";
h.key_press_modifiers(Modifiers::COMMAND, Key::End);
h.event(egui::Event::Text(" with more words".into()));
h.run_steps(4);
let galley = h
.output()
.shapes
.iter()
.find_map(|s| match &s.shape {
egui::Shape::Text(t) if t.galley.text() == replacement => Some(&t.galley),
_ => None,
})
.expect("the draft is painted");
assert_eq!(galley.rows.len(), 1, "mixed source runs grow as one visual row");
if let Ok(path) = std::env::var("PDFCRAFT_MIXED_ROW_SHOT") {
h.render().unwrap().save(path).unwrap();
}
h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);
h.run_steps(3);
let state = h.state();
let doc = state.session.get(state.views[0].id).unwrap();
assert_eq!(doc.text_blocks(0)[0].editor_text, replacement);
assert_eq!(doc.text_blocks(0)[0].rows.len(), 1);
let bytes = state.session.save_bytes(state.views[0].id).unwrap();
let mut reopened = pdfcraft_engine::Session::new();
let id = reopened.open("again.pdf", None, bytes, None).unwrap();
assert_eq!(reopened.get(id).unwrap().text_blocks(0)[0].rows.len(), 1);
}
/// One page whose only line is drawn twice at the same spot (fake bold, as many generated
/// documents do).
fn double_drawn() -> Vec<u8> {
let objs: Vec<String> = vec![
"<< /Type /Catalog /Pages 2 0 R >>".into(),
"<< /Type /Pages /Kids [4 0 R] /Count 1 /MediaBox [0 0 200 300] >>".into(),
"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".into(),
"<< /Type /Page /Parent 2 0 R /Contents 5 0 R /Resources << /Font << /F1 3 0 R >> >> >>".into(),
format!(
"<< /Length {} >>\nstream\nBT /F1 24 Tf 20 150 Td (Page 1) Tj ET BT /F1 24 Tf 20 150 Td (Page 1) Tj ET\nendstream",
"BT /F1 24 Tf 20 150 Td (Page 1) Tj ET BT /F1 24 Tf 20 150 Td (Page 1) Tj ET".len()
),
];
let mut out = b"%PDF-1.7\n".to_vec();
let mut offsets = Vec::new();
for (i, o) in objs.iter().enumerate() {
offsets.push(out.len());
out.extend_from_slice(format!("{} 0 obj\n{o}\nendobj\n", i + 1).as_bytes());
}
let xref = out.len();
out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
for o in offsets {
out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
}
out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes());
out
}
#[test]
fn editing_a_double_drawn_line_replaces_every_copy() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "bold.pdf", None, double_drawn()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height());
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(3);
let ed = h.state().views[0].session.line_editor.clone().expect("the line opens for editing");
assert_eq!(ed.text, "Page 1");
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = "Replaced".into();
h.run_steps(1);
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::Enter);
h.run_steps(4);
let s = h.state();
assert_eq!(s.session.get(s.views[0].id).unwrap().text_lines(0).len(), 1, "one line");
assert_eq!(texts_of(s, 0), ["Replaced"], "no copy of the old text shows under it");
}
#[test]
fn editing_existing_images_on_the_page() {
// A page made from a 40 × 20 image (at 72 dpi: a 40 × 20 pt page filled by it).
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 40, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
check_existing_artwork("picture.png", png);
}
/// Add text places a box with a click anywhere off its own boxes: on a scanned page (one picture
/// filling it) too, and just beside a paragraph, where Edit text would rewrap it.
#[test]
fn add_text_places_a_box_on_a_picture_and_beside_a_paragraph() {
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 40, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
h.state_mut().quick_tool = pdfcraft_ui_egui::QuickTool::AddText;
h.run_steps(2);
let c = h.state().views[0].page_screen_rect(0).expect("on screen").center();
h.hover_at(c);
h.run_steps(2);
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Text, "the I-beam over the picture");
click_at(&mut h, c);
assert!(h.state().views[0].session.content.draft.is_some(), "a click on the picture starts a text box");
let mut h = harness(2, |_| {});
h.state_mut().quick_tool = pdfcraft_ui_egui::QuickTool::AddText;
h.run_steps(2);
let id = h.state().views[0].id;
let block = h.state().session.get(id).unwrap().text_blocks(0)[0].clone();
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
// 6 px left of the text: 4 px past its box, within a closed paragraph's edge grip in Edit text.
let beside = egui::pos2(r.left() + block.rect[0] as f32 * k, r.top() + (300.0 - ((block.rect[1] + block.rect[3]) / 2.0) as f32) * k)
- egui::vec2(6.0, 0.0);
click_at(&mut h, beside);
assert!(h.state().views[0].session.content.draft.is_some(), "a click beside the paragraph starts a text box");
}
/// Add text opens a paragraph with a click anywhere in its box, just inside its edge too, where the
/// edge's grip shows (a drag there rewraps it, as with Edit text). Add text's hits left the grip
/// out there, so the click neither opened the paragraph nor started a box.
#[test]
fn add_text_opens_a_paragraph_from_just_inside_its_edge() {
let mut h = harness(2, |_| {});
h.state_mut().quick_tool = pdfcraft_ui_egui::QuickTool::AddText;
h.run_steps(2);
let id = h.state().views[0].id;
let block = h.state().session.get(id).unwrap().text_blocks(0)[0].clone();
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
// 2 px right of where the text starts: 4 px into its box, within its left edge's grip.
let inside = egui::pos2(r.left() + block.rect[0] as f32 * k, r.top() + (300.0 - ((block.rect[1] + block.rect[3]) / 2.0) as f32) * k)
+ egui::vec2(2.0, 0.0);
h.hover_at(inside);
h.run_steps(2);
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::ResizeHorizontal, "the edge's grip");
click_at(&mut h, inside);
assert!(h.state().views[0].session.line_editor.is_some(), "the click opens the paragraph for typing");
assert!(h.state().views[0].session.content.draft.is_none(), "and starts no text box");
}
/// With Add text a paragraph dragged off its own box still moves to where the button comes up, as
/// with Edit text. Add text acted on the page's paragraphs only while the pointer was over one, so
/// the drag was left hanging, to land wherever the pointer next met a paragraph.
#[test]
fn add_text_finishes_a_paragraph_drag_that_leaves_its_box() {
let mut h = harness(2, |_| {});
h.state_mut().quick_tool = pdfcraft_ui_egui::QuickTool::AddText;
h.run_steps(2);
let id = h.state().views[0].id;
let before = h.state().session.get(id).unwrap().text_blocks(0)[0].rect;
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
let from = egui::pos2(r.left() + ((before[0] + before[2]) / 2.0) as f32 * k, r.top() + (300.0 - ((before[1] + before[3]) / 2.0) as f32) * k);
// The drag starts in the box (8 px down), then goes 60 pt down, onto the blank page under it.
h.hover_at(from);
h.run_steps(1);
h.drag_at(from);
h.run_steps(1);
h.hover_at(from + egui::vec2(0.0, 8.0));
h.run_steps(1);
for i in 1..=4 {
h.hover_at(from + egui::vec2(0.0, 15.0 * k * i as f32));
h.run_steps(1);
}
h.drop_at(from + egui::vec2(0.0, 60.0 * k));
for _ in 0..10 {
h.run_steps(2);
}
let after = h.state().session.get(id).unwrap().text_blocks(0)[0].rect;
// The paragraph follows the pointer by the drag's own distance (60 pt down).
assert!(((before[1] - after[1]) - 60.0).abs() < 2.0, "the paragraph moved down with it: {before:?} to {after:?}");
}
#[test]
fn dragging_the_rotate_knob_turns_a_page_image() {
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 40, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
// A smaller picture in the middle of the page: the knob is over the page, not beside it.
let full = h.state().session.get(id).unwrap().page_images(0)[0].rect;
let (qw, qh) = ((full[2] - full[0]) / 4.0, (full[3] - full[1]) / 4.0);
let (mx, my) = ((full[0] + full[2]) / 2.0, (full[1] + full[3]) / 2.0);
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::EditPageImage { page: 0, index: 0, change: pdfcraft_engine::ImageEdit::Move([mx - qw, my - qh, mx + qw, my + qh]) },
);
h.run_steps(4);
let before = h.state().session.get(id).unwrap().page_images(0)[0].rect;
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let c = r.center();
// Select the picture.
h.hover_at(c);
h.run_steps(1);
h.drag_at(c);
h.run_steps(1);
h.drop_at(c);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_some(), "selected");
// Grab the knob and swing it a quarter turn clockwise (to the right).
let knob = knob_point(&h);
let to = egui::pos2(c.x + 100.0, c.y);
h.hover_at(knob);
h.run_steps(1);
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grab, "the knob offers to turn the picture");
h.drag_at(knob);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(knob + (to - knob) * (k as f32 / 4.0));
h.run_steps(1);
}
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grabbing, "and holds it while it turns");
h.drop_at(to);
h.run_steps(4);
let doc = h.state().session.get(id).unwrap();
assert_eq!(doc.can_undo(), Some("Rotate image"));
let turned = doc.page_images(0)[0].rect;
let (w, hh) = (before[2] - before[0], before[3] - before[1]);
assert!((turned[2] - turned[0] - hh).abs() < 1.5 && (turned[3] - turned[1] - w).abs() < 1.5, "{before:?} -> {turned:?}");
}
#[test]
fn dragging_rotate_knob_on_already_rotated_image_applies_delta_only() {
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 40, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
// Center picture
let full = h.state().session.get(id).unwrap().page_images(0)[0].rect;
let (qw, qh) = ((full[2] - full[0]) / 4.0, (full[3] - full[1]) / 4.0);
let (mx, my) = ((full[0] + full[2]) / 2.0, (full[1] + full[3]) / 2.0);
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::EditPageImage { page: 0, index: 0, change: pdfcraft_engine::ImageEdit::Move([mx - qw, my - qh, mx + qw, my + qh]) },
);
h.run_steps(4);
// Rotate the image by 45° clockwise initially
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(&ctx_h, pdfcraft_engine::Edit::EditPageImage { page: 0, index: 0, change: pdfcraft_engine::ImageEdit::RotateBy(45.0) });
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let c = r.center();
// Select the picture
h.hover_at(c);
h.run_steps(1);
h.drag_at(c);
h.run_steps(1);
h.drop_at(c);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_some(), "selected");
let corners = picture_corners(&h, 0, 0);
let center = corners[0].lerp(corners[2], 0.5);
let knob = knob_point(&h);
let radius = (knob - center).length();
// Hover at the rotated knob position:
h.hover_at(knob);
h.run_steps(1);
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grab, "knob offers Grab cursor on 45° rotated image");
// Rotate clockwise by a small delta of 15° (from 45° to 60°):
let to_angle = (45.0 + 15.0_f32).to_radians();
let to = center + egui::vec2(radius * to_angle.sin(), -radius * to_angle.cos());
h.drag_at(knob);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(knob + (to - knob) * (k as f32 / 4.0));
h.run_steps(1);
}
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grabbing);
h.drop_at(to);
h.run_steps(4);
let doc = h.state().session.get(id).unwrap();
let final_img = &doc.page_images(0)[0];
let angle_deg = (-final_img.matrix[1]).atan2(final_img.matrix[0]).to_degrees();
let angle_deg = (angle_deg % 360.0 + 360.0) % 360.0;
assert!((angle_deg - 60.0).abs() < 2.0, "expected ~60° total rotation, got {angle_deg}°");
}
#[test]
fn editing_existing_form_artwork_on_the_page() {
use pdfcraft_cos::{Dict, Document, Object, SaveOptions, Stream, write_incremental};
let mut doc = Document::open(std::sync::Arc::new(fixture(1))).unwrap();
let page = pdfcraft_model::pages(&doc)[0].clone();
let mut form = Dict::new();
form.set(b"Subtype".to_vec(), Object::name("Form"));
form.set(b"BBox".to_vec(), Object::Array([0, 0, 200, 300].map(Object::Int).to_vec()));
let artwork = doc.add(Object::Stream(Stream::flate(form, b"0.2 0.5 0.9 rg 20 20 160 260 re f")));
let contents = doc.add(Object::Stream(Stream::flate(Dict::new(), b"/Figure Do")));
doc.update_dict(page.obj, |d| {
d.set(b"Contents".to_vec(), Object::Ref(contents));
let mut xo = Dict::new();
xo.set(b"Figure".to_vec(), Object::Ref(artwork));
let mut resources = Dict::new();
resources.set(b"XObject".to_vec(), Object::Dict(xo));
d.set(b"Resources".to_vec(), Object::Dict(resources));
})
.unwrap();
check_existing_artwork("figure.pdf", write_incremental(&doc, &SaveOptions::default()).unwrap());
}
fn check_existing_artwork(name: &'static str, bytes: Vec<u8>) {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), name, None, bytes.clone()).expect("opens");
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
let before = h.state().session.get(id).unwrap().page_images(0)[0].rect;
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
// Select it, then drag it a quarter of the page to the right.
let c = r.center();
h.hover_at(c);
h.run_steps(1);
h.drag_at(c);
h.run_steps(1);
h.drop_at(c);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_some(), "selected");
let to = c + egui::vec2(r.width() / 4.0, 0.0);
h.hover_at(c);
h.run_steps(1);
h.drag_at(c);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(c + (to - c) * (k as f32 / 4.0));
h.run_steps(1);
}
h.drop_at(to);
h.run_steps(4);
let doc = h.state().session.get(id).unwrap();
assert_eq!(doc.can_undo(), Some("Move image"));
let moved = doc.page_images(0)[0].rect;
let dx = moved[0] - before[0];
assert!((dx - (before[2] - before[0]) / 4.0).abs() < 1.5, "moved {dx} pt: {before:?} → {moved:?}");
// Delete with the Delete key.
h.key_press(egui::Key::Delete);
h.run_steps(4);
assert!(h.state().session.get(id).unwrap().page_images(0).is_empty());
}
#[test]
fn image_right_click_context_menu_items_and_actions() {
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 40, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let c = r.center();
// Select image
h.hover_at(c);
h.run_steps(1);
h.drag_at(c);
h.run_steps(1);
h.drop_at(c);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_some(), "selected");
// Right-click image
h.hover_at(c);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_some());
for item in [
"Cut",
"Copy",
"Paste",
"Flip horizontal",
"Flip vertical",
"Arrange image",
"Replace image",
"Draw freehand",
"Redact Image",
"Create Link",
"Recognize Text",
] {
if item == "Paste" || item == "Draw freehand" {
h.get_by_role_and_label(Role::MenuItem, item);
} else {
h.get_by_label_contains(item);
}
}
// Top horizontal icon buttons
h.get_by_label("Rotate Counterclockwise");
h.get_by_label("Rotate Clockwise");
h.get_by_label("Delete");
// Flip horizontal via context menu item
h.get_by_label("Flip horizontal").click();
h.run_steps(3);
let id = h.state().views[0].id;
let doc = h.state().session.get(id).unwrap();
assert_eq!(doc.can_undo(), Some("Flip image"));
// Undo flip
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::Z);
h.run_steps(3);
let doc = h.state().session.get(id).unwrap();
assert_eq!(doc.can_undo(), None);
}
#[test]
fn default_page_context_menu_when_not_in_edit_mode() {
let mut h = harness(1, |_| {});
h.run_steps(4);
assert_eq!(h.state().quick_tool, pdfcraft_ui_egui::QuickTool::Select);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let c = r.center();
// Right click on page in Select mode
h.hover_at(c);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
// Verify default page context menu items
h.get_by_label("Rotate view counterclockwise");
h.get_by_label("Print");
h.get_by_label("Select Tool");
h.get_by_label("Hand Tool");
h.get_by_label("Marquee Zoom");
for item in ["Add bookmark", "Take a snapshot", "Edit this PDF", "Delete page", "Export this PDF", "Find text or tools", "Document properties…"]
{
assert!(menu_row(&h, item), "the page menu offers {item}");
}
assert!(menu_row(&h, "Rotate view clockwise"));
assert!(h.query_all_by_label("Add comment").next().is_some());
assert!(h.query_all_by_label("Add text").next().is_some());
assert!(h.query_all_by_label("Add image").next().is_some());
assert!(h.query_all_by_label_contains("Copy").next().is_some());
assert!(h.query_all_by_label_contains("Paste").next().is_some());
// Clicking "Edit this PDF" enters edit mode
h.get_by_label("Edit this PDF").click();
h.run_steps(2);
assert_eq!(h.state().quick_tool, pdfcraft_ui_egui::QuickTool::EditText);
}
#[test]
fn page_context_menu_items_disabled_on_protected_pdf() {
let bytes = protected("", "owner", 0b0100);
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "protected.pdf", None, bytes.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert_eq!(h.state().quick_tool, pdfcraft_ui_egui::QuickTool::Select);
let doc_id = h.state().views[0].id;
let doc = h.state().session.get(doc_id).unwrap();
assert!(!doc.allows_modification());
assert!(!doc.allows_assembly());
assert!(!doc.allows_annotation());
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let c = r.center();
// Right click on page in Select mode
h.hover_at(c);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
let is_disabled =
|h: &mut Harness<'static, PdfCraftApp>, label: &str| h.query_by(|n| n.label().as_deref() == Some(label) && n.is_disabled()).is_some();
// Context menu is open; check restricted items are greyed out (disabled)
assert!(is_disabled(&mut h, "Edit this PDF"), "Edit this PDF should be disabled on protected document");
assert!(is_disabled(&mut h, "Add text"), "Add text should be disabled");
assert!(is_disabled(&mut h, "Add image"), "Add image should be disabled");
assert!(is_disabled(&mut h, "Delete page"), "Delete page should be disabled");
assert!(is_disabled(&mut h, "Add comment"), "Add comment should be disabled");
assert!(is_disabled(&mut h, "Add bookmark"), "Add bookmark should be disabled");
// View-only items should still be enabled
assert!(!is_disabled(&mut h, "Select Tool"));
assert!(!is_disabled(&mut h, "Hand Tool"));
assert!(!is_disabled(&mut h, "Find text or tools"));
}
#[test]
fn image_edge_handle_stretch_and_crop_mode() {
let mut png = Vec::new();
image::RgbImage::from_pixel(100, 100, image::Rgb([100, 150, 200]))
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
.unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "square.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let c = r.center();
// Select image
h.hover_at(c);
h.run_steps(1);
h.drag_at(c);
h.run_steps(1);
h.drop_at(c);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_some());
// Enter crop mode via context menu
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
h.get_by_label("Crop").click();
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.crop.is_some()));
// Set smaller crop rect
let smaller = egui::Rect::from_center_size(c, egui::vec2(50.0, 50.0));
set_crop_box(&mut h, smaller);
// Confirm crop with Enter key
h.key_press(egui::Key::Enter);
h.run_steps(4);
assert!(!h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.crop.is_some()));
let doc_id = h.state().views[0].id;
let images = h.state().session.get(doc_id).unwrap().page_images(0);
assert_eq!(images.len(), 1, "image still exists: {images:?}");
assert!(images[0].visible_rect.is_some(), "cropped image is visible");
assert_ne!(images[0].visible_rect, Some(images[0].rect), "image is cropped");
// Remove crop via context menu
h.hover_at(c);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
h.get_by_label("Remove crop").click();
h.run_steps(4);
let images = h.state().session.get(doc_id).unwrap().page_images(0);
assert_eq!(images[0].visible_rect, Some(images[0].rect), "crop is removed");
}
#[test]
fn tiny_image_crop_handle_drag_does_not_panic() {
let mut png = Vec::new();
image::RgbImage::from_pixel(5, 5, image::Rgb([100, 150, 200])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(800.0, 600.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "tiny.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
// Select the picture, then choose Crop from its menu.
let corners = picture_corners(&h, 0, 0);
let centre = corners[0].lerp(corners[2], 0.5);
click_at(&mut h, centre);
assert!(h.state().views[0].session.image_selection.is_some(), "selected");
h.hover_at(centre);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: centre, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: centre, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
h.query_all_by_label("Crop").last().expect("the picture menu offers Crop").click();
h.run_steps(3);
// Drag a grip across a 5 × 5 picture, past the opposite corner, to ensure the frame's bounds do not panic.
let corners = picture_corners(&h, 0, 0);
let (from, to) = (corners[2], corners[0] - (corners[2] - corners[0]));
h.hover_at(from);
h.run_steps(1);
h.drag_at(from);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(from + (to - from) * (k as f32 / 4.0));
h.run_steps(1);
}
h.drop_at(to);
h.run_steps(3);
assert_eq!(h.state().views.len(), 1, "the app is still running with its document");
}
#[test]
fn text_selection_action_icons() {
let mut h = harness(1, |_| {});
// Dragging selects only once the page's text layer is in, and it comes from the render pool
// in the background: under load a fixed number of frames was sometimes not enough.
support::wait_until(&mut h, "page 1's text layer", |app| app.views[0].has_page_text(0));
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let p0 = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height());
let p1 = egui::pos2(r.left() + 100.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height());
// Drag to select text
h.hover_at(p0);
h.run_steps(1);
h.drag_at(p0);
h.run_steps(1);
h.hover_at(p1);
h.run_steps(1);
h.drop_at(p1);
h.run_steps(2);
assert!(h.state().views[0].selected_text().is_some());
// Verify all 6 minimalist action icons are present in the floating menu
assert!(h.query_all_by_label("Add comment").next().is_some());
assert!(h.query_all_by_label("Highlight").next().is_some());
assert!(h.query_all_by_label("Underline").next().is_some());
assert!(h.query_all_by_label("Strikethrough").next().is_some());
assert!(h.query_all_by_label("Replace text").next().is_some());
assert!(h.query_all_by_label("Copy").next().is_some());
}
#[test]
fn the_format_panel_restyles_the_paragraph_being_edited() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height());
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(3);
assert!(h.state().views[0].session.line_editor.is_some());
// The Format text panel shows the paragraph's look; making it bold applies at once.
assert!(h.query_all_by_label("Format text").next().is_some(), "the Format text section shows");
h.get_by_label("Bold").click();
h.run_steps(4);
let s = h.state();
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!(doc.can_undo(), Some("Edit text"));
assert_eq!(doc.text_blocks(0)[0].base_font, "Helvetica-Bold");
assert!(s.views[0].session.line_editor.is_some(), "still editing");
// Underline: another step, the text keeps its bold.
h.get_by_label("Underline").click();
h.run_steps(4);
let s = h.state();
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!(doc.text_blocks(0)[0].base_font, "Helvetica-Bold");
assert!(s.views[0].session.line_editor.as_ref().is_some_and(|e| e.extras.underline));
}
#[test]
fn the_format_panel_toggles_superscript_and_subscript_on_selection() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height());
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(3);
assert!(h.state().views[0].session.line_editor.is_some());
// In line editor, select "1" in "Page 1":
let ed = h.state_mut().views[0].session.line_editor.as_mut().unwrap();
ed.selection = Some(craft::text::ui::editor::TextSelection::new(5, 6));
ed.focus = true;
h.run_steps(1);
// Click Superscript: "1" becomes "¹"
assert!(h.query_all_by_label("Format text").next().is_some(), "the Format text section shows");
let btn = h.get_by_role_and_label(Role::Button, "Superscript");
btn.click();
h.run_steps(4);
let s = h.state();
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!(doc.can_undo(), Some("Edit text"));
assert_eq!(doc.text_blocks(0)[0].text, "Page ¹");
// Select "¹" in "Page ¹" to toggle it back:
let ed = h.state_mut().views[0].session.line_editor.as_mut().unwrap();
ed.selection = Some(craft::text::ui::editor::TextSelection::new(5, 7));
ed.focus = true;
h.run_steps(1);
// Click Superscript again: "¹" toggles back to "1"
h.get_by_role_and_label(Role::Button, "Superscript").click();
h.run_steps(4);
let s2 = h.state();
let doc2 = s2.session.get(s2.views[0].id).unwrap();
assert_eq!(doc2.text_blocks(0)[0].text, "Page 1");
// Subscript toggle in added text:
h.state_mut().views[0].session.line_editor = None;
h.state_mut().quick_tool = pdfcraft_ui_egui::QuickTool::AddText;
let mut draft = pdfcraft_ui_egui::TextDraft::new(0, None, [20.0, 20.0, 120.0, 40.0], "H2O".into(), Default::default());
draft.selection = Some(craft::text::ui::editor::TextSelection::new(1, 2));
h.state_mut().views[0].session.content.draft = Some(draft);
h.run_steps(2);
h.get_by_role_and_label(Role::Button, "Subscript").click();
h.run_steps(2);
assert_eq!(h.state().views[0].session.content.draft.as_ref().map(|d| d.text.as_str()), Some("H₂O"));
h.get_by_role_and_label(Role::Button, "Subscript").click();
h.run_steps(2);
assert_eq!(h.state().views[0].session.content.draft.as_ref().map(|d| d.text.as_str()), Some("H2O"));
}
/// Horizontal scale and Character spacing take any value in their ranges, not only the listed
/// ones: values between the listed ones and beyond them, typed in Format text, reach the saved
/// file as the engine writes them.
#[test]
fn the_format_panel_takes_any_scale_and_character_spacing() {
let mut h = harness(1, |_| {});
let id = h.state().views[0].id;
let width = |h: &Harness<'static, PdfCraftApp>, view: usize| {
let s = h.state();
let r = s.session.get(s.views[view].id).unwrap().text_blocks(0)[0].rect;
r[2] - r[0]
};
let plain = width(&h, 0);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height());
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(3);
assert!(h.state().views[0].session.line_editor.is_some());
// Typed the way a person does: click the field, type, Enter.
let type_in = |h: &mut Harness<'static, PdfCraftApp>, label: &str, value: &str| {
h.get_by_role_and_label(Role::SpinButton, label).click();
h.run_steps(2);
h.event(egui::Event::Text(value.into()));
h.run_steps(1);
h.key_press(Key::Enter);
h.run_steps(4);
};
let extras = |h: &Harness<'static, PdfCraftApp>| h.state().views[0].session.line_editor.as_ref().map(|e| (e.extras.scale, e.extras.char_spacing));
// Between the listed values (50…200 %, −1…5 pt) and beyond them, within what was always allowed.
type_in(&mut h, "Horizontal scale", "80");
assert_eq!(extras(&h), Some((80.0, 0.0)));
type_in(&mut h, "Horizontal scale", "250");
assert_eq!(extras(&h), Some((250.0, 0.0)));
type_in(&mut h, "Character spacing", "0.25");
assert_eq!(extras(&h), Some((250.0, 0.25)));
type_in(&mut h, "Character spacing", "-2");
assert_eq!(extras(&h), Some((250.0, -2.0)));
assert_eq!(h.state().session.get(id).unwrap().can_undo(), Some("Edit text"));
// Saved and reopened, the paragraph is as wide as the engine draws it with those values.
let bytes = h.state().session.save_bytes(id).unwrap();
let ctx_h = h.ctx.clone();
h.state_mut().open_bytes(&ctx_h, "reference.pdf", None, fixture(1)).expect("opens");
let style = pdfcraft_engine::BlockStyle { scale: Some(250.0), char_spacing: Some(-2.0), ..Default::default() };
let ctx_h = h.ctx.clone();
assert!(h.state_mut().apply_edit(&ctx_h, pdfcraft_engine::Edit::EditTextBlock { page: 0, block: 0, text: "Page 1".into(), style }));
let ctx_h = h.ctx.clone();
h.state_mut().open_bytes(&ctx_h, "saved.pdf", None, bytes.as_ref().clone()).expect("the saved file opens");
h.run_steps(3);
let (reference, saved) = (width(&h, 1), width(&h, 2));
assert!(saved > plain * 1.5, "250 % widens it: {plain} → {saved}");
assert!((saved - reference).abs() < 0.5, "saved {saved}, the engine's own {reference}");
}
#[test]
fn typed_small_coloured_text_size_survives_save_and_reopen() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).unwrap();
let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height());
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(3);
assert!(h.state().views[0].session.line_editor.is_some());
h.get_by_role_and_label(Role::SpinButton, "Font size").focus();
h.run_steps(2);
h.key_press_modifiers(Modifiers::COMMAND, Key::A);
h.event(egui::Event::Text("6.5".into()));
h.run_steps(1);
h.key_press(Key::Enter);
h.run_steps(4);
h.get_by_label("Text color").click();
h.run_steps(2);
h.get_by_role_and_label(Role::SpinButton, "Blue").click();
h.key_press_modifiers(Modifiers::COMMAND, Key::A);
h.event(egui::Event::Text("255".into()));
h.run_steps(2);
h.get_by_role_and_label(Role::Button, "OK").click();
h.run_steps(4);
let id = h.state().views[0].id;
let lines = h.state().session.get(id).unwrap().text_lines(0);
assert!(!lines.is_empty());
for line in &lines {
assert!((line.size - 6.5).abs() < 0.01, "{line:?}");
assert!(line.color[2] > line.color[0], "the small text stays blue: {line:?}");
}
let saved = h.state().session.save_bytes(id).unwrap();
let mut reopened = pdfcraft_engine::Session::new();
let rid = reopened.open("small-blue.pdf", None, saved, None).unwrap();
assert_eq!(reopened.get(rid).unwrap().text_lines(0), lines);
}
#[test]
fn selected_text_formatting_keeps_the_rest_of_the_paragraph_and_reopens() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).unwrap();
let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height());
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(4);
h.key_press_modifiers(Modifiers::COMMAND, Key::ArrowRight);
h.run_steps(1);
h.key_press_modifiers(Modifiers::SHIFT, Key::ArrowLeft);
h.run_steps(2);
assert_eq!(h.state().views[0].session.line_editor.as_ref().unwrap().selection, Some(craft::text::ui::editor::TextSelection::new(6, 5)));
h.get_by_label("Bold").click();
h.run_steps(5);
let id = h.state().views[0].id;
let doc = h.state().session.get(id).unwrap();
let lines = doc.text_lines(0);
assert!(lines.iter().any(|l| l.text == "1" && l.base_font == "Helvetica-Bold"), "{lines:?}");
assert!(lines.iter().any(|l| l.text.trim_end() == "Page" && l.base_font == "Helvetica"));
let saved = h.state_mut().session.save_bytes(id).unwrap();
let mut reopen = pdfcraft_engine::Session::new();
let rid = reopen.open("saved.pdf", None, saved, None).unwrap();
assert_eq!(reopen.get(rid).unwrap().text_lines(0), lines);
}
/// Drag with the pointer from `from` to `to` in a few steps.
fn drag(h: &mut Harness<'static, PdfCraftApp>, from: egui::Pos2, to: egui::Pos2) {
h.hover_at(from);
h.run_steps(1);
h.drag_at(from);
h.run_steps(1);
for k in 1..=5 {
h.hover_at(from + (to - from) * (k as f32 / 5.0));
h.run_steps(1);
}
h.drop_at(to);
h.run_steps(4);
}
#[test]
fn dragging_a_paragraph_moves_it_and_its_edge_rewraps_it() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let block = |h: &Harness<'static, PdfCraftApp>| {
let s = h.state();
s.session.get(s.views[0].id).unwrap().text_blocks(0)[0].clone()
};
// User space (200 × 300 page) → screen.
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
let screen = |x: f64, y: f64| egui::pos2(r.left() + x as f32 * k, r.top() + (300.0 - y as f32) * k);
// Move "Page 1" 30 pt right and 50 pt up.
let before = block(&h);
let mid = screen((before.rect[0] + before.rect[2]) / 2.0, (before.rect[1] + before.rect[3]) / 2.0);
drag(&mut h, mid, mid + egui::vec2(30.0 * k, -50.0 * k));
assert!(h.state().views[0].session.line_editor.is_none(), "dragging moves the box; it doesn't open it for typing");
let moved = block(&h);
assert_eq!(moved.text, "Page 1");
let near = |a: f64, b: f64| (a - b).abs() < 1.5;
assert!(near(moved.rect[0], before.rect[0] + 30.0) && near(moved.rect[1], before.rect[1] + 50.0), "{:?} → {:?}", before.rect, moved.rect);
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), Some("Move object"));
// Drag the handle on its right edge in to about 45 pt wide: "Page" and "1" rewrap onto two lines.
let edge = screen(moved.rect[2], (moved.rect[1] + moved.rect[3]) / 2.0) + egui::vec2(2.0, 0.0);
let narrower = (moved.rect[2] - moved.rect[0] - 45.0) as f32 * k;
drag(&mut h, edge, edge - egui::vec2(narrower, 0.0));
let s = h.state();
let doc = s.session.get(s.views[0].id).unwrap();
let lines: Vec<String> = doc.text_lines(0).iter().map(|l| l.text.clone()).collect();
assert_eq!(lines, ["Page", "1"], "rewrapped to the narrower box");
assert!(near(doc.text_lines(0)[0].rect[0], moved.rect[0]), "it keeps its place");
assert_eq!(doc.can_undo(), Some("Edit text"), "edge drags still rewrap text");
assert!(h.state().views[0].session.line_editor.is_none());
}
/// Press and release at `at` without moving.
fn click_at(h: &mut Harness<'static, PdfCraftApp>, at: egui::Pos2) {
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(4);
}
/// Sets the open crop's box on page 0 to the screen rectangle `r`, as dragging its handles would.
fn set_crop_box(h: &mut Harness<'static, PdfCraftApp>, r: egui::Rect) {
let view = &h.state().views[0];
let page = &h.state().session.get(view.id).unwrap().info.pages[0];
let xf =
pdfcraft_ui_egui::canvas::PageXform { rect: view.page_screen_rect(0).unwrap(), rot: view.view.rotation, pw: page.width, ph: page.height };
let (a, b) = (xf.screen_to_view(r.min), xf.screen_to_view(r.max));
let (u, v) = (page.view_to_user(a.0, a.1), page.view_to_user(b.0, b.1));
let keep = [u[0].min(v[0]), u[1].min(v[1]), u[0].max(v[0]), u[1].max(v[1])].map(f64::from);
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop.as_mut().unwrap().keep = keep;
}
fn press(h: &mut Harness<'static, PdfCraftApp>, modifiers: Modifiers, key: Key) {
h.key_press_modifiers(modifiers, key);
h.run_steps(4);
}
/// A two-page document whose first page draws one picture (a Form XObject), open in Edit text
/// & images with the picture selected.
fn selected_figure() -> Harness<'static, PdfCraftApp> {
use pdfcraft_cos::{Dict, Document, Object, SaveOptions, Stream, write_incremental};
let mut doc = Document::open(std::sync::Arc::new(fixture(2))).unwrap();
let page = pdfcraft_model::pages(&doc)[0].clone();
let mut form = Dict::new();
form.set(b"Subtype".to_vec(), Object::name("Form"));
form.set(b"BBox".to_vec(), Object::Array([0, 0, 200, 300].map(Object::Int).to_vec()));
let artwork = doc.add(Object::Stream(Stream::flate(form, b"0.2 0.5 0.9 rg 40 200 120 80 re f")));
let contents = doc.add(Object::Stream(Stream::flate(Dict::new(), b"/Figure Do")));
doc.update_dict(page.obj, |d| {
d.set(b"Contents".to_vec(), Object::Ref(contents));
let mut xo = Dict::new();
xo.set(b"Figure".to_vec(), Object::Ref(artwork));
let mut resources = Dict::new();
resources.set(b"XObject".to_vec(), Object::Dict(xo));
d.set(b"Resources".to_vec(), Object::Dict(resources));
})
.unwrap();
let bytes = write_incremental(&doc, &SaveOptions::default()).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "figure.pdf", None, bytes.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
click_at(&mut h, egui::pos2(r.left() + 100.0 / 200.0 * r.width(), r.top() + (300.0 - 240.0) / 300.0 * r.height()));
assert!(h.state().views[0].session.image_selection.is_some(), "selected");
h
}
#[test]
fn arrow_keys_nudge_a_selected_image_instead_of_turning_the_page() {
let mut h = selected_figure();
let id = h.state().views[0].id;
let rect = |h: &Harness<'static, PdfCraftApp>| h.state().session.get(id).unwrap().page_images(0)[0].rect;
let before = rect(&h);
// As in Acrobat: an arrow moves it a point, Shift and an arrow ten.
press(&mut h, Modifiers::NONE, Key::ArrowRight);
press(&mut h, Modifiers::SHIFT, Key::ArrowDown);
let moved = rect(&h);
let near = |a: f64, b: f64| (a - b).abs() < 0.01;
assert!(near(moved[0], before[0] + 1.0) && near(moved[1], before[1] - 10.0), "{before:?} → {moved:?}");
assert!(near(moved[2] - moved[0], before[2] - before[0]), "moved, not resized");
assert_eq!(h.state().session.get(id).unwrap().can_undo(), Some("Move image"));
assert_eq!(h.state().views[0].current(), 0, "the arrows moved the image, not the page");
assert!(h.state().views[0].session.image_selection.is_some(), "it stays selected for the next nudge");
}
/// #3632: with a picture selected, Alt+← / Alt+→ still go back and forward through the view
/// history. They don't nudge it (the nudge patterns also match a press with Alt), so nothing moves
/// and the document isn't edited.
#[test]
fn alt_arrows_navigate_instead_of_nudging_a_selected_image() {
let mut h = selected_figure();
let id = h.state().views[0].id;
let rect = |h: &Harness<'static, PdfCraftApp>| h.state().session.get(id).unwrap().page_images(0)[0].rect;
let before = rect(&h);
// A followed link's jump to page 2 and back, as `go_to_dest` makes it.
h.state_mut().views[0].go_to_page(1);
h.run_steps(2);
h.state_mut().views[0].go_to_page(0);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_some(), "still selected, so a nudge could take the keys");
press(&mut h, Modifiers::ALT, Key::ArrowLeft);
assert_eq!(h.state().views[0].current(), 1, "Alt+← went back");
press(&mut h, Modifiers::ALT, Key::ArrowRight);
assert_eq!(h.state().views[0].current(), 0, "Alt+→ went forward");
// Each press is judged by its own modifiers, not the frame's: Alt let go in the same frame
// as Alt+← leaves the frame with none, and the press still goes back.
let arrow = |key, modifiers| egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers };
h.event(arrow(Key::ArrowLeft, Modifiers::ALT));
h.run_steps(4);
assert_eq!(h.state().views[0].current(), 1, "an Alt+← press went back");
assert_eq!(rect(&h), before, "the picture didn't move");
let doc = h.state().session.get(id).unwrap();
assert!(!doc.dirty && doc.can_undo().is_none(), "nothing was edited");
// And a plain → pressed before Alt goes down in the same frame still nudges.
h.state_mut().views[0].go_to_page(0);
h.run_steps(2);
h.event(arrow(Key::ArrowRight, Modifiers::NONE));
h.event(egui::Event::ModifiersChanged(Modifiers::ALT));
h.run_steps(4);
h.event(egui::Event::ModifiersChanged(Modifiers::NONE));
h.run_steps(2);
let moved = rect(&h);
assert!((moved[0] - before[0] - 1.0).abs() < 0.01, "a plain → nudged a point: {before:?} → {moved:?}");
}
/// A paragraph's edge and an open paragraph's frame are caught a few pixels either side of
/// their hairline, and the cursor shows exactly where a press takes hold (#2203).
#[ignore = "known gap of the egui-to-craft port: see docs/port-known-gaps.md"]
#[test]
fn paragraph_edges_and_frames_are_caught_without_aiming_for_the_hairline() {
let start = |h: &mut Harness<'static, PdfCraftApp>| {
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let b = h.state().session.get(id).unwrap().text_blocks(0)[0].clone();
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
// The drawn box: the text on screen, 2 px larger each side.
let bx = egui::Rect::from_min_max(
egui::pos2(r.left() + b.rect[0] as f32 * k, r.top() + (300.0 - b.rect[3] as f32) * k),
egui::pos2(r.left() + b.rect[2] as f32 * k, r.top() + (300.0 - b.rect[1] as f32) * k),
)
.expand(2.0);
(id, bx)
};
// A closed paragraph's right edge, 7 px inside and 7 px outside: the rewrap arrow, and a
// drag from there changes the width rather than moving the box.
for dx in [-7.0f32, 7.0] {
let mut h = harness(1, |_| {});
let (id, bx) = start(&mut h);
let p = egui::pos2(bx.right() + dx, bx.center().y);
h.hover_at(p);
h.run_steps(2);
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::ResizeHorizontal, "{dx} px from the edge");
drag(&mut h, p, p + egui::vec2(40.0, 0.0));
assert_eq!(h.state().session.get(id).unwrap().can_undo(), Some("Edit text"), "{dx} px from the edge rewraps");
}
// An open paragraph's frame, 7 px beyond its drawn line: the move arrow, and a press there
// selects the box instead of closing the paragraph.
let mut h = harness(1, |_| {});
let (_, bx) = start(&mut h);
click_at(&mut h, bx.center());
assert!(h.state().views[0].session.line_editor.is_some(), "a click on the text opens it for typing");
let p = egui::pos2(bx.center().x, bx.top() - 7.0);
h.hover_at(p);
h.run_steps(2);
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Move, "the pointer shows the frame takes hold");
click_at(&mut h, p);
assert!(h.state().views[0].session.line_editor.is_none());
h.get_by_label("1 object selected");
// Once the text is changed, a press on the frame doesn't take the box (selecting it would
// drop the typing): it applies the typing like any click off the text, so the pointer doesn't
// offer the move arrow there either.
let mut h = harness(1, |_| {});
let (_, bx) = start(&mut h);
click_at(&mut h, bx.center());
h.state_mut().views[0].session.line_editor.as_mut().expect("open for typing").text = "Page one".into();
h.run_steps(2);
h.hover_at(p);
h.run_steps(2);
assert_ne!(h.output().platform_output.cursor_icon, egui::CursorIcon::Move, "no move arrow where a press can't take the box");
click_at(&mut h, p);
let id = h.state().views[0].id;
assert_eq!(h.state().session.get(id).unwrap().text_blocks(0)[0].text, "Page one", "the click applies the typing, as any click off the text does");
assert!(h.query_all_by_label_contains("object selected").next().is_none(), "and doesn't take the box");
}
/// Leaving All tools ▸ Edit a PDF for another tool applies the paragraph still open, as leaving
/// the Edit tab does, rather than dropping what was typed (review on #4605).
#[test]
fn another_tool_from_edit_a_pdf_applies_the_open_paragraph() {
use pdfcraft_ui_egui::{Mode, QuickTool};
let mut h = harness(1, |_| {});
h.set_option("tool", "edit").unwrap();
h.run_steps(2);
assert_ne!(h.state().mode, Mode::Edit, "Edit a PDF from All tools, not the Edit tab");
assert_eq!(h.state().quick_tool, QuickTool::EditText);
let id = h.state().views[0].id;
let b = h.state().session.get(id).unwrap().text_blocks(0)[0].clone();
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
click_at(&mut h, egui::pos2(r.left() + ((b.rect[0] + b.rect[2]) / 2.0) as f32 * k, r.top() + (300.0 - (b.rect[1] + b.rect[3]) / 2.0) as f32 * k));
assert_eq!(h.state().views[0].session.line_editor.as_ref().map(|e| e.text.as_str()), Some("Page 1"), "the paragraph is open for typing");
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = "Page one".into();
h.set_option("tool", "organize").unwrap();
h.run_steps(3);
assert!(h.state().views[0].session.line_editor.is_none());
assert_eq!(h.state().quick_tool, QuickTool::Select);
assert_eq!(h.state().session.get(id).unwrap().text_blocks(0)[0].text, "Page one", "the typed text was applied");
}
#[test]
fn an_open_paragraph_moves_by_its_frame_and_by_the_arrow_keys() {
let mut h = harness(2, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let block = |h: &Harness<'static, PdfCraftApp>| h.state().session.get(id).unwrap().text_blocks(0)[0].clone();
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
let screen = |x: f64, y: f64| egui::pos2(r.left() + x as f32 * k, r.top() + (300.0 - y as f32) * k);
// Just above the paragraph's frame: on the frame, off its text.
let frame = |b: &pdfcraft_engine::TextBlock| screen((b.rect[0] + b.rect[2]) / 2.0, b.rect[3]) - egui::vec2(0.0, 4.0);
let near = |a: f64, b: f64| (a - b).abs() < 0.01;
let before = block(&h);
click_at(&mut h, screen((before.rect[0] + before.rect[2]) / 2.0, (before.rect[1] + before.rect[3]) / 2.0));
assert!(h.state().views[0].session.line_editor.is_some(), "a click on the text opens it for typing");
// A click on its frame selects the box instead, and the arrows move it.
click_at(&mut h, frame(&before));
assert!(h.state().views[0].session.line_editor.is_none(), "the frame selects the box, it doesn't type");
press(&mut h, Modifiers::NONE, Key::ArrowRight);
press(&mut h, Modifiers::SHIFT, Key::ArrowUp);
let nudged = block(&h);
assert_eq!(nudged.text, "Page 1");
assert!(near(nudged.rect[0], before.rect[0] + 1.0) && near(nudged.rect[1], before.rect[1] + 10.0), "{:?} → {:?}", before.rect, nudged.rect);
assert_eq!(h.state().session.get(id).unwrap().can_undo(), Some("Move object"));
assert_eq!(h.state().views[0].current(), 0, "the arrows moved the paragraph, not the page");
// Open it again and drag it by its frame: it follows the pointer.
press(&mut h, Modifiers::NONE, Key::Escape);
click_at(&mut h, screen((nudged.rect[0] + nudged.rect[2]) / 2.0, (nudged.rect[1] + nudged.rect[3]) / 2.0));
assert!(h.state().views[0].session.line_editor.is_some());
let from = frame(&nudged);
drag(&mut h, from, from + egui::vec2(30.0 * k, -50.0 * k));
assert!(h.state().views[0].session.line_editor.is_none());
let dragged = block(&h);
assert!(
(dragged.rect[0] - nudged.rect[0] - 30.0).abs() < 1.5 && (dragged.rect[1] - nudged.rect[1] - 50.0).abs() < 1.5,
"{:?} → {:?}",
nudged.rect,
dragged.rect
);
assert_eq!(h.state().session.get(id).unwrap().text_lines(0).len(), 1, "moved, not retyped");
}
/// The open paragraph's frame shows the Move cursor as far out as a press there takes hold of the
/// box: the cursor reached 4 px past the frame, the press 6.
#[test]
fn an_open_paragraphs_frame_shows_move_wherever_a_press_takes_the_box() {
let mut h = harness(2, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let before = h.state().session.get(id).unwrap().text_blocks(0)[0].clone();
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
let screen = |x: f64, y: f64| egui::pos2(r.left() + x as f32 * k, r.top() + (300.0 - y as f32) * k);
click_at(&mut h, screen((before.rect[0] + before.rect[2]) / 2.0, (before.rect[1] + before.rect[3]) / 2.0));
assert!(h.state().views[0].session.line_editor.is_some(), "a click on the text opens it for typing");
// 7 px above the text: 5 px past the frame drawn 2 px out.
let far = screen((before.rect[0] + before.rect[2]) / 2.0, before.rect[3]) - egui::vec2(0.0, 7.0);
h.hover_at(far);
h.run_steps(3);
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Move, "the frame offers to move the box there");
click_at(&mut h, far);
assert!(h.state().views[0].session.line_editor.is_none(), "and a click there selects the box");
}
/// The open paragraph's frame takes hold of the box on its sides too: there the edges of a closed
/// paragraph would rewrap it, but around the open one lies its frame.
#[test]
fn an_open_paragraphs_frame_takes_the_box_beside_its_text() {
let mut h = harness(2, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let before = h.state().session.get(id).unwrap().text_blocks(0)[0].clone();
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
let screen = |x: f64, y: f64| egui::pos2(r.left() + x as f32 * k, r.top() + (300.0 - y as f32) * k);
let middle = (before.rect[1] + before.rect[3]) / 2.0;
click_at(&mut h, screen((before.rect[0] + before.rect[2]) / 2.0, middle));
assert!(h.state().views[0].session.line_editor.is_some(), "a click on the text opens it for typing");
// 6 px left of the text: 4 px past the frame drawn 2 px out.
let beside = screen(before.rect[0], middle) - egui::vec2(6.0, 0.0);
h.hover_at(beside);
h.run_steps(3);
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Move, "the frame offers to move the box there");
click_at(&mut h, beside);
assert!(h.state().views[0].session.line_editor.is_none(), "and a click there selects the box");
}
/// Contributor-original text, with an off-origin CropBox. Keep the moving paragraph last in
/// source order so an ordinary hit picks it again when two independent boxes fully overlap.
fn paragraph_drag_fixture(content: &str, rotation: i64, unit: i64) -> Vec<u8> {
use pdfcraft_cos::{Dict, Document, Object, Stream};
let mut doc = Document::open(std::sync::Arc::new(fixture(1))).unwrap();
let page = pdfcraft_model::pages(&doc)[0].obj;
let contents = doc.add(Object::Stream(Stream::flate(Dict::new(), content.as_bytes())));
doc.update_dict(page, |d| {
d.set(b"Contents".to_vec(), Object::Ref(contents));
d.set(b"MediaBox".to_vec(), Object::Array([0, 0, 400, 500].map(Object::Int).to_vec()));
d.set(b"CropBox".to_vec(), Object::Array([20, 30, 380, 470].map(Object::Int).to_vec()));
d.set(b"Rotate".to_vec(), Object::Int(rotation));
d.set(b"UserUnit".to_vec(), Object::Int(unit));
})
.unwrap();
pdfcraft_cos::write_incremental(&doc, &Default::default()).unwrap()
}
fn paragraph_drag_harness(bytes: Vec<u8>, view_rotation: u16) -> Harness<'static, PdfCraftApp> {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = scripted_app();
app.session = pdfcraft_engine::Session::new().with_clock(|| 1_700_000_000);
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "paragraphs.pdf", None, bytes).unwrap();
app.set_option(&egui::Context::default(), "zoom", "75").unwrap();
app.views[0].view.rotation = view_rotation;
assert!(app.execute(&egui::Context::default(), "edit.edit_text"));
app
}
});
h.run_steps(4);
h
}
fn dragged_paragraphs(h: &Harness<'static, PdfCraftApp>) -> Vec<pdfcraft_engine::TextBlock> {
let app = h.state();
app.session.get(app.views[0].id).unwrap().text_blocks(0)
}
/// Resolve the current inventory and transform for every gesture, never an old block index
/// or cached screen position. With identical labels, choose the last (topmost) source box.
fn paragraph_drag_points(h: &Harness<'static, PdfCraftApp>, text: &str, offset: [f64; 2]) -> (egui::Pos2, egui::Pos2) {
let app = h.state();
let view = &app.views[0];
let doc = app.session.get(view.id).unwrap();
let b = doc.text_blocks(0).into_iter().rfind(|b| b.text == text).unwrap();
let xf = pdfcraft_ui_egui::canvas::PageXform {
rect: view.page_screen_rect(0).unwrap(),
rot: view.view.rotation,
pw: doc.info.pages[0].width,
ph: doc.info.pages[0].height,
};
let moved = std::array::from_fn(|i| (b.rect[i] + offset[i % 2]) as f32);
(xf.user_rect(&doc.info, 0, b.rect.map(|v| v as f32)).center(), xf.user_rect(&doc.info, 0, moved).center())
}
fn drag_paragraph(h: &mut Harness<'static, PdfCraftApp>, text: &str, offset: [f64; 2]) {
let (from, to) = paragraph_drag_points(h, text, offset);
assert!(
h.query_all_by_label_contains("object selected").next().is_none() && h.query_all_by_label_contains("objects selected").next().is_none(),
"no objects selected"
);
assert!(h.state().views[0].session.line_editor.is_none());
drag(h, from, to);
assert!(h.state().views[0].session.line_editor.is_none(), "a body drag must not open the typing editor");
assert!(
h.query_all_by_label_contains("object selected").next().is_none() && h.query_all_by_label_contains("objects selected").next().is_none(),
"no objects selected"
);
}
fn assert_dragged_paragraphs(actual: &[pdfcraft_engine::TextBlock], expected: &[pdfcraft_engine::TextBlock]) {
assert_eq!(actual.len(), expected.len(), "independent paragraphs must survive");
// Source order is itself an invariant, including when text and geometry are identical.
for (a, e) in actual.iter().zip(expected) {
assert_eq!((&a.text, &a.base_font, a.size, a.bold, a.italic, a.color), (&e.text, &e.base_font, e.size, e.bold, e.italic, e.color));
assert_eq!(a.lines.len(), e.lines.len(), "moving must not rewrap");
assert!(a.rect.iter().zip(e.rect).all(|(a, e)| (a - e).abs() < 0.01), "{a:?} != {e:?}");
}
}
/// Read only the current page contents, not stale revisions or the renderer's deduplicated
/// text layer. These synthetic fixtures contain each literal exactly once per source draw.
fn paragraph_drag_content(h: &Harness<'static, PdfCraftApp>) -> String {
let app = h.state();
let doc = pdfcraft_cos::Document::open(app.session.get(app.views[0].id).unwrap().bytes.clone()).unwrap();
let page = pdfcraft_model::pages(&doc)[0].clone();
let contents = doc.resolve(page.dict.get(b"Contents").unwrap());
let pieces = match contents.as_array() {
Some(a) => a.clone(),
None => vec![contents.as_ref().clone()],
};
let mut bytes = Vec::new();
for piece in pieces {
let object = doc.resolve(&piece);
let pdfcraft_cos::Object::Stream(stream) = object.as_ref() else { panic!("fixture content must be a stream") };
bytes.extend(stream.decoded().unwrap());
}
String::from_utf8(bytes).unwrap()
}
fn save_paragraph_drag(h: &mut Harness<'static, PdfCraftApp>, name: &str, original: &[u8]) -> Vec<u8> {
let out = temp_path(name);
let id = h.state().views[0].id;
queue_save(&out);
h.key_press_modifiers(Modifiers::COMMAND, Key::S);
h.run_steps(4);
let saved = saved_file(&out);
assert!(saved.starts_with(original), "Save keeps the opened/saved revisions");
assert!(!h.state().session.get(id).unwrap().dirty);
assert_eq!(h.state().session.get(id).unwrap().bytes.as_slice(), saved.as_slice());
saved
}
fn paragraph_drag_shot(h: &mut Harness<'static, PdfCraftApp>, name: &str) {
if let Ok(directory) = std::env::var("PDFCRAFT_PARAGRAPH_DRAG_SHOTS") {
assert!(
crate::support::step_until(h, |h| {
h.run_steps(1);
!h.state().render_pending()
}),
"the screenshot must show the current document generation"
);
let directory = std::path::Path::new(&directory);
std::fs::create_dir_all(directory).unwrap();
h.render().unwrap().save(directory.join(name)).unwrap();
}
}
fn check_paragraph_overlap_drag(identical: bool, reopen_between: bool) {
let _gpu = gpu();
let (stationary, moving) = if identical { ("Repeated", "Repeated") } else { ("Stationary beta", "Moving alpha") };
let content =
format!("BT /F1 12 Tf 60 220 Td ({stationary}) Tj ET BT /F1 12 Tf 250 100 Td (Keep here) Tj ET BT /F1 12 Tf 60 320 Td ({moving}) Tj ET");
let mut saved_base = paragraph_drag_fixture(&content, 0, 1);
let mut h = paragraph_drag_harness(saved_base.clone(), 0);
let before = dragged_paragraphs(&h);
assert_eq!(before.len(), 3);
paragraph_drag_shot(&mut h, &format!("before-{identical}-{reopen_between}.png"));
let moving_index = before.iter().rposition(|b| b.text == moving).unwrap();
let mut overlap = before.clone();
for i in [1, 3] {
overlap[moving_index].rect[i] -= 100.0;
}
drag_paragraph(&mut h, moving, [0.0, -100.0]);
assert_dragged_paragraphs(&dragged_paragraphs(&h), &overlap);
paragraph_drag_shot(&mut h, &format!("overlap-{identical}-{reopen_between}.png"));
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(4);
assert_dragged_paragraphs(&dragged_paragraphs(&h), &before);
assert!(h.state().session.get(h.state().views[0].id).unwrap().can_undo().is_none(), "the first release was exactly one step");
h.key_press_modifiers(Modifiers::COMMAND | Modifiers::SHIFT, Key::Z);
h.run_steps(4);
assert_dragged_paragraphs(&dragged_paragraphs(&h), &overlap);
if reopen_between {
let bytes = save_paragraph_drag(&mut h, &format!("paragraph-overlap-{identical}.pdf"), &saved_base);
saved_base = bytes.clone();
h = paragraph_drag_harness(bytes, 0);
assert_dragged_paragraphs(&dragged_paragraphs(&h), &overlap);
}
// A rewrite here used to remove the stationary box, even if its text was different.
drag_paragraph(&mut h, moving, [150.0, 0.0]);
let mut after = overlap.clone();
for i in [0, 2] {
after[moving_index].rect[i] += 150.0;
}
assert_dragged_paragraphs(&dragged_paragraphs(&h), &after);
paragraph_drag_shot(&mut h, &format!("after-{identical}-{reopen_between}.png"));
let current = paragraph_drag_content(&h);
assert_eq!(current.matches(&format!("({stationary})")).count(), if identical { 2 } else { 1 });
assert_eq!(current.matches(&format!("({moving})")).count(), if identical { 2 } else { 1 });
assert_eq!(current.matches("(Keep here)").count(), 1);
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), Some("Move object"));
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(4);
assert_dragged_paragraphs(&dragged_paragraphs(&h), &overlap);
if !reopen_between {
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(4);
assert_dragged_paragraphs(&dragged_paragraphs(&h), &before);
}
assert!(h.state().session.get(h.state().views[0].id).unwrap().can_undo().is_none(), "each release was exactly one step");
if !reopen_between {
h.key_press_modifiers(Modifiers::COMMAND | Modifiers::SHIFT, Key::Z);
h.run_steps(4);
assert_dragged_paragraphs(&dragged_paragraphs(&h), &overlap);
}
h.key_press_modifiers(Modifiers::COMMAND | Modifiers::SHIFT, Key::Z);
h.run_steps(4);
assert_dragged_paragraphs(&dragged_paragraphs(&h), &after);
assert!(h.state().session.get(h.state().views[0].id).unwrap().can_redo().is_none());
let bytes = save_paragraph_drag(&mut h, &format!("paragraph-separated-{identical}-{reopen_between}.pdf"), &saved_base);
let reopened = paragraph_drag_harness(bytes, 0);
assert_dragged_paragraphs(&dragged_paragraphs(&reopened), &after);
assert_eq!(paragraph_drag_content(&reopened), current);
}
#[test]
fn ordinary_paragraph_drags_preserve_overlapping_text_and_history() {
check_paragraph_overlap_drag(false, false);
}
#[test]
fn ordinary_paragraph_drags_preserve_identical_overlapping_labels() {
check_paragraph_overlap_drag(true, false);
}
#[test]
fn ordinary_paragraph_drags_preserve_overlap_after_save_and_reopen() {
check_paragraph_overlap_drag(false, true);
check_paragraph_overlap_drag(true, true);
}
#[test]
fn ordinary_paragraph_drags_preserve_partial_overlap_and_touching_labels() {
let content = "BT /F1 12 Tf 60 220 Td (Repeated) Tj ET BT /F1 12 Tf 60 320 Td (Repeated) Tj ET";
for fraction in [0.25, 0.75, 1.0] {
let mut h = paragraph_drag_harness(paragraph_drag_fixture(content, 0, 1), 0);
let mut expected = dragged_paragraphs(&h);
assert_eq!(expected.len(), 2);
let width = expected[1].rect[2] - expected[1].rect[0];
// 75% overlap, 25% overlap, then exact touching of the original source rectangles.
for offset in [[width * fraction, -100.0], [150.0, -40.0]] {
drag_paragraph(&mut h, "Repeated", offset);
for (i, coordinate) in expected[1].rect.iter_mut().enumerate() {
*coordinate += offset[i % 2];
}
assert_dragged_paragraphs(&dragged_paragraphs(&h), &expected);
assert_eq!(paragraph_drag_content(&h).matches("(Repeated)").count(), 2);
}
}
}
#[test]
fn ordinary_paragraph_drags_preserve_multiline_kerning_strings() {
let content =
concat!("BT /F1 12 Tf 60 220 Td (Keep here) Tj ET ", "BT /F1 12 Tf 14 TL 60 320 Td [(First) -120 ( line)] TJ T* (Second line) Tj ET");
let mut h = paragraph_drag_harness(paragraph_drag_fixture(content, 0, 1), 0);
let mut expected = dragged_paragraphs(&h);
assert_eq!(expected.len(), 2);
assert_eq!(expected[1].lines.len(), 2);
let text = expected[1].text.clone();
for offset in [[0.0, -100.0], [150.0, 0.0]] {
drag_paragraph(&mut h, &text, offset);
for (i, coordinate) in expected[1].rect.iter_mut().enumerate() {
*coordinate += offset[i % 2];
}
assert_dragged_paragraphs(&dragged_paragraphs(&h), &expected);
let current = paragraph_drag_content(&h);
assert!(current.contains("[(First) -120 ( line)] TJ"), "original encoded strings and kerning stay byte-identical");
for literal in ["(Keep here)", "(Second line)"] {
assert_eq!(current.matches(literal).count(), 1);
}
}
}
#[test]
fn ordinary_paragraph_drag_respects_rotations_crop_and_user_unit() {
let content = "BT /F1 12 Tf 60 220 Td (Keep here) Tj ET BT /F1 12 Tf 60 320 Td (Moving alpha) Tj ET";
for rotation in [0, 90, 180, 270] {
for view_rotation in [0, 90, 180, 270] {
let mut h = paragraph_drag_harness(paragraph_drag_fixture(content, rotation, 2), view_rotation);
let mut expected = dragged_paragraphs(&h);
let moving = expected.iter_mut().find(|b| b.text == "Moving alpha").unwrap();
for (i, coordinate) in moving.rect.iter_mut().enumerate() {
*coordinate += [35.0, -27.0][i % 2];
}
drag_paragraph(&mut h, "Moving alpha", [35.0, -27.0]);
assert_dragged_paragraphs(&dragged_paragraphs(&h), &expected);
}
}
}
#[test]
fn an_ordinary_added_text_drag_keeps_its_content_edit() {
for panel_open in [true, false] {
check_added_text_drag(panel_open);
}
}
fn check_added_text_drag(panel_open: bool) {
let content = "BT /F1 12 Tf 60 220 Td (Keep here) Tj ET";
let mut h = paragraph_drag_harness(paragraph_drag_fixture(content, 0, 1), 0);
let id = h.state().views[0].id;
h.state_mut()
.session
.apply(
id,
pdfcraft_engine::Edit::AddText {
page: 0,
text: pdfcraft_engine::AddedText { text: "Added note".into(), rect: [100.0, 330.0, 230.0, 354.0], ..Default::default() },
},
)
.unwrap();
let original = h.state().session.save_bytes(id).unwrap();
h = paragraph_drag_harness(original.as_ref().clone(), 0);
h.state_mut().set_left_open(panel_open);
h.run_steps(4);
let added = |h: &Harness<'static, PdfCraftApp>| {
let app = h.state();
app.session.get(app.views[0].id).unwrap().added().to_vec()
};
let before_added = added(&h);
assert_eq!(before_added.len(), 1);
let before = dragged_paragraphs(&h);
assert_eq!(before.len(), 2, "added text is also visible in the raw paragraph list");
let mut expected = before.clone();
let moved = expected.iter_mut().find(|b| b.text == "Added note").unwrap();
for (i, coordinate) in moved.rect.iter_mut().enumerate() {
*coordinate += [30.0, -20.0][i % 2];
}
// The same pointer hits added content and its source glyphs. Only content_ui may own it:
// a Text target would be excluded from MoveObjects and overwrite the valid UpdateContent.
let (from, to) = paragraph_drag_points(&h, "Added note", [30.0, -20.0]);
drag(&mut h, from, to);
assert!(h.state().views[0].session.line_editor.is_none());
assert_dragged_paragraphs(&dragged_paragraphs(&h), &expected);
let after_added = added(&h);
assert_eq!(after_added.len(), 1);
let (old_rect, new_rect) = (before_added[0].content.rect(), after_added[0].content.rect());
for i in 0..4 {
assert!((new_rect[i] - old_rect[i] - [30.0, -20.0][i % 2]).abs() < 0.01);
}
assert_eq!(after_added[0].content, before_added[0].content.with_rect(new_rect));
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), Some("Edit content"));
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(4);
assert_eq!(added(&h), before_added);
assert_dragged_paragraphs(&dragged_paragraphs(&h), &before);
assert!(h.state().session.get(h.state().views[0].id).unwrap().can_undo().is_none(), "one content edit, no paragraph rewrite");
h.key_press_modifiers(Modifiers::COMMAND | Modifiers::SHIFT, Key::Z);
h.run_steps(4);
assert_eq!(added(&h), after_added);
assert_dragged_paragraphs(&dragged_paragraphs(&h), &expected);
let bytes = save_paragraph_drag(&mut h, &format!("paragraph-added-content-{panel_open}.pdf"), original.as_slice());
let reopened = paragraph_drag_harness(bytes, 0);
assert_eq!(added(&reopened), after_added);
assert_dragged_paragraphs(&dragged_paragraphs(&reopened), &expected);
}
#[test]
fn an_ordinary_paragraph_drag_refusal_never_rewrites_the_text() {
let content = "q BT 4 Tr /F1 12 Tf 60 320 Td (Clipping text) Tj ET Q";
let mut h = paragraph_drag_harness(paragraph_drag_fixture(content, 0, 1), 0);
let before = dragged_paragraphs(&h);
let id = h.state().views[0].id;
let bytes = h.state().session.get(id).unwrap().bytes.clone();
let generation = h.state().session.get(id).unwrap().edit_generation();
drag_paragraph(&mut h, "Clipping text", [30.0, -20.0]);
let doc = h.state().session.get(id).unwrap();
assert_eq!(doc.bytes, bytes);
assert_eq!(doc.edit_generation(), generation);
assert!(doc.can_undo().is_none());
assert!(!doc.dirty);
assert_dragged_paragraphs(&dragged_paragraphs(&h), &before);
assert!(support::shown_notice(&mut h).unwrap().contains("Text used as a clipping path cannot be moved with this selection."));
}
/// A delete the engine refuses (text used as a clipping path) leaves the document as it was and
/// keeps the selection, so the user can still move or nudge what they had selected.
#[test]
fn a_refused_delete_keeps_the_object_selection() {
let content = "q BT 4 Tr /F1 12 Tf 60 320 Td (Clipping text) Tj ET Q";
let mut h = paragraph_drag_harness(paragraph_drag_fixture(content, 0, 1), 0);
let id = h.state().views[0].id;
let (generation, bytes) = {
let doc = h.state().session.get(id).unwrap();
(doc.edit_generation(), doc.bytes.clone())
};
// An area selection from empty page space around the paragraph.
let (center, _) = paragraph_drag_points(&h, "Clipping text", [0.0, 0.0]);
drag(&mut h, center - egui::vec2(60.0, 25.0), center + egui::vec2(60.0, 25.0));
h.get_by_label("1 object selected");
h.key_press(Key::Delete);
h.run_steps(4);
let doc = h.state().session.get(id).unwrap();
assert_eq!((doc.edit_generation(), &doc.bytes), (generation, &bytes), "the refused delete changed nothing");
assert!(doc.can_undo().is_none());
assert!(support::shown_notice(&mut h).is_some(), "the refusal is reported");
h.get_by_label("1 object selected");
}
#[test]
fn an_ordinary_paragraph_drag_cannot_reuse_an_index_after_an_edit() {
let content = "BT /F1 12 Tf 60 120 Td (Discard) Tj ET BT /F1 12 Tf 60 320 Td (Moving alpha) Tj ET BT /F1 12 Tf 60 220 Td (Keep here) Tj ET";
let mut h = paragraph_drag_harness(paragraph_drag_fixture(content, 0, 1), 0);
let (from, to) = paragraph_drag_points(&h, "Moving alpha", [30.0, -20.0]);
h.hover_at(from);
h.run_steps(1);
h.drag_at(from);
h.run_steps(1);
h.hover_at(to);
h.run_steps(2);
let id = h.state().views[0].id;
h.state_mut()
.session
.apply(id, pdfcraft_engine::Edit::EditTextBlock { page: 0, block: 0, text: String::new(), style: Default::default() })
.unwrap();
let after_edit = dragged_paragraphs(&h);
assert_eq!(after_edit.len(), 2, "the surviving paragraphs get different indexes");
assert_eq!(after_edit[1].text, "Keep here", "the old target index now names an unrelated paragraph");
let generation = h.state().session.get(id).unwrap().edit_generation();
h.run_steps(2);
h.drop_at(to);
h.run_steps(4);
assert_eq!(h.state().session.get(id).unwrap().edit_generation(), generation, "the stale release adds no edit");
assert_dragged_paragraphs(&dragged_paragraphs(&h), &after_edit);
}
/// A quick flick: press at `from`, and the next frame already finds the pointer at `to`.
fn flick(h: &mut Harness<'static, PdfCraftApp>, from: egui::Pos2, to: egui::Pos2) {
h.hover_at(from);
h.run_steps(1);
h.drag_at(from);
h.run_steps(1);
h.hover_at(to);
h.run_steps(1);
h.drop_at(to);
h.run_steps(4);
}
#[test]
fn a_quick_flick_on_either_edge_rewraps_the_paragraph() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let lines = |h: &Harness<'static, PdfCraftApp>| {
let s = h.state();
s.session.get(s.views[0].id).unwrap().text_lines(0)
};
let block = |h: &Harness<'static, PdfCraftApp>| {
let s = h.state();
s.session.get(s.views[0].id).unwrap().text_blocks(0)[0].clone()
};
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
let screen = |x: f64, y: f64| egui::pos2(r.left() + x as f32 * k, r.top() + (300.0 - y as f32) * k);
let near = |a: f64, b: f64| (a - b).abs() < 1.5;
// Grab edges near the box's top: a rewrapped box reaches the window's bottom, where the
// tool's toast sits over the page and takes the press.
let grip_y = |b: &pdfcraft_engine::TextBlock| b.rect[3] - 6.0;
// Narrow "Page 1" from its right edge to about 45 pt: it rewraps onto two lines.
let b = block(&h);
let edge = screen(b.rect[2], grip_y(&b)) + egui::vec2(2.0, 0.0);
flick(&mut h, edge, edge - egui::vec2((b.rect[2] - b.rect[0] - 45.0) as f32 * k, 0.0));
let text: Vec<String> = lines(&h).iter().map(|l| l.text.clone()).collect();
assert_eq!(text, ["Page", "1"], "narrowed from the right edge");
// Flick the right edge outward, past the box and its handle: back onto one line.
let b = block(&h);
// 1 px outside the box: the grab zone's outer limit sits near 2 px, where a pixel of page
// scale (the canvas width depends on the rail) decides whether the press lands on the handle.
let edge = screen(b.rect[2], grip_y(&b)) + egui::vec2(1.0, 0.0);
flick(&mut h, edge, edge + egui::vec2(80.0 * k, 0.0));
let text: Vec<String> = lines(&h).iter().map(|l| l.text.clone()).collect();
assert_eq!(text, ["Page 1"], "widened by a fast drag that left the handle at once");
// Flick the left edge inward: it rewraps and its left side follows the pointer.
let b = block(&h);
let edge = screen(b.rect[0], grip_y(&b)) - egui::vec2(2.0, 0.0);
let inward = (b.rect[2] - b.rect[0] - 45.0) as f32;
flick(&mut h, edge, edge + egui::vec2(inward * k, 0.0));
let after = lines(&h);
let text: Vec<String> = after.iter().map(|l| l.text.clone()).collect();
assert_eq!(text, ["Page", "1"], "narrowed from the left edge");
assert!(near(after[0].rect[0], b.rect[0] + inward as f64), "{:?} → {:?}", b.rect, after[0].rect);
assert!(h.state().views[0].session.line_editor.is_none());
}
/// #2276: a paragraph's rewrap handle is drawn half outside its box, and the resize pointer
/// shows a few pixels further out still. A press there started an area selection instead: its
/// rectangle followed the pointer, and on release the box went back to its old width.
#[test]
fn a_paragraph_rewraps_from_the_outer_half_of_its_edge_handles() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let lines = |h: &Harness<'static, PdfCraftApp>| {
let s = h.state();
s.session.get(s.views[0].id).unwrap().text_lines(0).iter().map(|l| l.text.clone()).collect::<Vec<_>>()
};
let block = |h: &Harness<'static, PdfCraftApp>| {
let s = h.state();
s.session.get(s.views[0].id).unwrap().text_blocks(0)[0].clone()
};
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
let screen = |x: f64, y: f64| egui::pos2(r.left() + x as f32 * k, r.top() + (300.0 - y as f32) * k);
let near = |a: [f64; 4], b: [f64; 4]| a.iter().zip(b).all(|(a, b)| (a - b).abs() < 1.5);
// The box is 2 px outside the text and its handle 7 px wide, centred on the box's edge: press
// 3 px outside the box, on the handle. Near the box's top, clear of the tool's toast.
let outside = 2.0 + 3.0;
let before = block(&h);
let grip = screen(before.rect[2], before.rect[3] - 6.0) + egui::vec2(outside, 0.0);
drag(&mut h, grip, grip - egui::vec2((before.rect[2] - before.rect[0] - 45.0) as f32 * k, 0.0));
assert_eq!(lines(&h), ["Page", "1"], "narrowed from the right handle");
let narrowed = block(&h);
assert!(narrowed.rect[2] < before.rect[2] - 20.0 && (narrowed.rect[0] - before.rect[0]).abs() < 1.5, "{:?} → {:?}", before.rect, narrowed.rect);
let id = h.state().views[0].id;
assert_eq!(h.state().session.get(id).unwrap().can_undo(), Some("Edit text"));
assert!(h.state().views[0].session.line_editor.is_none());
// Saved and reopened, the paragraph keeps its new width.
let saved = h.state_mut().session.save_bytes(id).unwrap();
let mut reopen = pdfcraft_engine::Session::new();
let rid = reopen.open("saved.pdf", None, saved, None).unwrap();
assert!(near(reopen.get(rid).unwrap().text_blocks(0)[0].rect, narrowed.rect));
// Undo gives the old width back, redo the new one.
h.state_mut().undo(&egui::Context::default());
h.run_steps(2);
assert_eq!(lines(&h), ["Page 1"]);
assert!(near(block(&h).rect, before.rect), "{:?} → {:?}", before.rect, block(&h).rect);
h.state_mut().redo(&egui::Context::default());
h.run_steps(2);
assert!(near(block(&h).rect, narrowed.rect), "{:?} → {:?}", narrowed.rect, block(&h).rect);
// The left handle's outer half too.
h.state_mut().undo(&egui::Context::default());
h.run_steps(2);
let grip = screen(before.rect[0], before.rect[3] - 6.0) - egui::vec2(outside, 0.0);
let inward = (before.rect[2] - before.rect[0] - 45.0) as f32;
drag(&mut h, grip, grip + egui::vec2(inward * k, 0.0));
assert_eq!(lines(&h), ["Page", "1"], "narrowed from the left handle");
assert!((block(&h).rect[0] - (before.rect[0] + f64::from(inward))).abs() < 1.5, "{:?} → {:?}", before.rect, block(&h).rect);
}
/// The Pages panel, in a window tall enough to show every thumbnail of a short fixture.
fn pages_panel(pages: usize) -> Harness<'static, PdfCraftApp> {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "doc.pdf", None, fixture(pages)).expect("fixture opens");
app.set_option(&egui::Context::default(), "panel", "pages").unwrap();
app
}
});
h.run_steps(4);
h
}
fn picked(h: &Harness<'static, PdfCraftApp>) -> Vec<usize> {
let mut pages = h.state().views[0].selected.items.clone();
pages.sort_unstable();
pages
}
#[test]
fn pages_panel_command_click_picks_pages_without_moving_the_document() {
let mut h = pages_panel(5);
// The first ⌘-click on another page keeps the current page (1) selected too.
h.get_by_label("Page 3").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
assert_eq!(picked(&h), [0, 2]);
assert_eq!(h.state().views[0].current(), 0, "picking pages does not turn the page");
h.get_by_label_contains("2 pages selected");
h.get_by_label("Page 5").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
assert_eq!(picked(&h), [0, 2, 4]);
// ⌘-click again takes a page back out.
h.get_by_label("Page 3").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
assert_eq!(picked(&h), [0, 4]);
// Page commands act on what is picked, as in the organize grid.
assert_eq!(h.state().views[0].target_pages(), [0, 4]);
}
#[test]
fn pages_panel_shift_click_picks_a_range_and_a_plain_click_starts_over() {
let mut h = pages_panel(6);
// A plain click goes to the page and is the anchor of the next range.
h.get_by_label("Page 2").click_modifiers(Modifiers::NONE);
h.run_steps(2);
assert_eq!(h.state().views[0].current(), 1);
assert!(picked(&h).is_empty());
h.get_by_label("Page 4").click_modifiers(Modifiers::SHIFT);
h.run_steps(2);
assert_eq!(picked(&h), [1, 2, 3]);
assert_eq!(h.state().views[0].current(), 1);
// A second ⇧-click ranges from the same anchor, in either direction.
h.get_by_label("Page 1").click_modifiers(Modifiers::SHIFT);
h.run_steps(2);
assert_eq!(picked(&h), [0, 1]);
// ⇧ with no earlier click ranges from the current page.
h.get_by_label("Page 6").click_modifiers(Modifiers::NONE);
h.run_steps(2);
assert!(picked(&h).is_empty(), "a plain click drops the selection");
assert_eq!(h.state().views[0].current(), 5);
h.get_by_label("Page 5").click_modifiers(Modifiers::SHIFT);
h.run_steps(2);
assert_eq!(picked(&h), [4, 5]);
}
#[test]
fn pages_panel_escape_clears_and_deleted_pages_leave_the_selection() {
let mut h = pages_panel(5);
h.get_by_label("Page 2").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
assert_eq!(picked(&h), [0, 1]);
// The pointer is still over the panel.
h.key_press(Key::Escape);
h.run_steps(2);
assert!(picked(&h).is_empty());
// Pages that no longer exist drop out of the selection.
h.get_by_label("Page 5").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
assert_eq!(picked(&h), [0, 4]);
h.state_mut().views[0].select_pages(&[3, 4]);
assert!(h.execute("page.delete"));
h.run_steps(3);
assert_eq!(page_texts(h.state()).len(), 3);
assert!(picked(&h).iter().all(|p| *p < 3), "{:?}", picked(&h));
}
#[test]
fn print_shortcut_offers_the_pages_picked_in_the_pages_panel() {
let mut h = pages_panel(5);
h.get_by_label("Page 2").click_modifiers(Modifiers::NONE);
h.run_steps(2);
h.get_by_label("Page 4").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
assert_eq!(picked(&h), [1, 3]);
h.key_press_modifiers(Modifiers::COMMAND, Key::P);
h.run_steps(3);
assert_eq!(h.state().print_draft.which, pdfcraft_ui_egui::PrintWhich::Selected);
assert_eq!(h.state().print_draft.selected, [1, 3]);
h.get_by_label("Selected pages (2)");
h.get_by_label("Sheet 1 of 2");
}
#[test]
fn dragging_pages_panel_thumbnails_reorders_pages() {
let mut h = pages_panel(4);
let before = page_texts(h.state());
// The Pages panel is on the right; the document view may label its pages too.
let thumb = |h: &Harness<'static, PdfCraftApp>, label: &str| {
h.get_all_by_label(label).map(|n| n.rect()).max_by(|a, b| a.left().total_cmp(&b.left())).expect("the thumbnail")
};
// Grab page 3 and drop it before page 1.
let (from, to) = (thumb(&h, "Page 3").center(), thumb(&h, "Page 1").left_top() + egui::vec2(4.0, 4.0));
drag(&mut h, from, to);
h.run_steps(4);
let after = page_texts(h.state());
assert_eq!(after, vec![before[2].clone(), before[0].clone(), before[1].clone(), before[3].clone()], "{after:?}");
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), Some("Move page"));
let view = &h.state().views[0];
assert_eq!(view.selected.items.iter().copied().collect::<Vec<_>>(), vec![0], "the moved page stays selected");
}
#[test]
fn resized_pages_panel_thumbnails_reorder_multiple_pages_and_undo() {
let mut h = harness(4, |app| {
app.set_option(&egui::Context::default(), "panel", "pages").unwrap();
app.set_option(&egui::Context::default(), "thumbnail-zoom", "50").unwrap();
});
let before = page_texts(h.state());
h.state_mut().views[0].select_pages(&[1, 2]);
h.run_steps(2);
let from = panel_thumb_rect(&h, "Page 2").center();
let to = panel_thumb_rect(&h, "Page 1").center_top() + egui::vec2(0.0, 4.0);
drag(&mut h, from, to);
h.run_steps(4);
assert_eq!(page_texts(h.state()), vec![before[1].clone(), before[2].clone(), before[0].clone(), before[3].clone()]);
assert_eq!(h.state().views[0].target_pages(), vec![0, 1]);
assert_eq!(h.state().views[0].thumbnail_zoom(), 0.5);
assert!(h.execute("edit.undo"));
h.run_steps(4);
assert_eq!(page_texts(h.state()), before);
assert_eq!(h.state().views[0].thumbnail_zoom(), 0.5);
}
#[test]
fn a_thumbnail_drag_that_ends_off_the_panel_or_outlives_the_pages_is_dropped() {
let mut h = pages_panel(4);
h.run_steps(2);
let before = page_texts(h.state());
// Presses a thumbnail and drags it halfway towards the first one, leaving the button down.
let start_drag = |h: &mut Harness<'static, PdfCraftApp>, label: &str| -> egui::Pos2 {
let (from, to) = (panel_thumb(h, label), panel_thumb(h, "Page 1"));
h.hover_at(from);
h.run_steps(1);
h.drag_at(from);
h.run_steps(1);
for k in 1..=3 {
h.hover_at(from + (to - from) * (k as f32 / 6.0));
h.run_steps(1);
}
to
};
// The panel closed mid-drag: the release, over where the first thumbnail was, moves nothing.
let to = start_drag(&mut h, "Page 3");
h.set_option("panel", "none").unwrap();
h.run_steps(2);
h.hover_at(to);
h.run_steps(1);
h.drop_at(to);
h.run_steps(2);
assert_eq!(page_texts(h.state()), before, "a drag that outlived its panel moves nothing");
// A change to the document drops page indexes taken before it: the release moves nothing.
h.set_option("panel", "pages").unwrap();
h.run_steps(3);
let to = start_drag(&mut h, "Page 4");
let id = h.state().views[0].id;
let info = h.state().session.get(id).unwrap().info.clone();
let ctx = h.ctx.clone();
h.state_mut().views[0].document_changed(&ctx, &info);
h.hover_at(to);
h.run_steps(1);
h.drop_at(to);
h.run_steps(2);
assert_eq!(page_texts(h.state()), before, "a drag that outlived the pages it picked moves nothing");
}
/// A Pages panel thumbnail (the rightmost node with that label).
fn panel_thumb(h: &Harness<'static, PdfCraftApp>, label: &str) -> egui::Pos2 {
h.get_all_by_label(label).map(|n| n.rect()).max_by(|a, b| a.left().total_cmp(&b.left())).expect("the thumbnail").center()
}
fn right_click_at(h: &mut Harness<'static, PdfCraftApp>, at: egui::Pos2) {
h.hover_at(at);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
}
#[test]
fn deleting_pages_from_the_pages_panel_menu() {
let mut h = pages_panel(4);
let at = panel_thumb(&h, "Page 2");
right_click_at(&mut h, at);
h.get_by_label("Delete pages").click();
h.run_steps(4);
assert_eq!(page_texts(h.state()), ["Page 1", "Page 3", "Page 4"]);
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), Some("Delete page"));
assert!(h.state().views[0].selected.items.is_empty(), "the deleted pages leave the selection");
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(page_texts(h.state()), ["Page 1", "Page 2", "Page 3", "Page 4"]);
}
#[test]
fn the_delete_key_deletes_pages_picked_in_the_pages_panel_but_never_every_page() {
let mut h = pages_panel(3);
// ⌘-click picks pages 2 and 3 (and the current page, 1: the first ⌘-click keeps it).
h.get_by_label("Page 2").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
h.get_by_label("Page 3").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
let (p1, p3) = (panel_thumb(&h, "Page 1"), panel_thumb(&h, "Page 3"));
h.hover_at(p3);
h.run_steps(1);
assert_eq!(h.state().views[0].selected.items.iter().copied().collect::<Vec<_>>(), vec![0, 1, 2]);
// Every page picked: Delete does nothing, and the menu item is off.
h.key_press(Key::Delete);
h.run_steps(3);
assert_eq!(page_texts(h.state()).len(), 3, "a document keeps at least one page");
right_click_at(&mut h, p3);
assert!(h.query_by(|n| n.label().as_deref() == Some("Delete pages") && n.is_disabled()).is_some());
h.key_press(Key::Escape);
h.run_steps(2);
// Pages 2 and 3 picked (page 2 current): Delete deletes them, and page 1 becomes current.
h.state_mut().views[0].select_pages(&[1, 2]);
h.hover_at(p1);
h.run_steps(1);
h.key_press(Key::Delete);
h.run_steps(4);
assert_eq!(page_texts(h.state()), ["Page 1"]);
assert_eq!(h.state().views[0].current(), 0);
}
#[test]
fn host_dirty_hears_when_unsaved_work_appears_and_goes() {
let reports = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let sink = reports.clone();
let mut h = harness(2, move |app| {
app.set_option(&egui::Context::default(), "organize", "on").unwrap();
app.host_dirty = Some(Box::new(move |dirty| sink.lock().unwrap().push(dirty)));
});
assert_eq!(reports.lock().unwrap().last(), Some(&false), "a freshly opened document has nothing unsaved");
h.get_by_label("Rotate clockwise").click();
h.run_steps(3);
assert_eq!(reports.lock().unwrap().last(), Some(&true), "an edit is unsaved work");
let out = temp_path("host-dirty.pdf");
queue_save(&out);
h.key_press_modifiers(Modifiers::COMMAND, Key::S);
h.run_steps(3);
assert_eq!(reports.lock().unwrap().last(), Some(&false), "saving clears it");
}
#[test]
fn one_editor_contains_mixed_styles_and_visual_rows() {
let _gpu = gpu();
let bytes = source_font_fixture_with_text(
"BT 0 0.2 0.5 rg /F1 1 Tf 18 0 0 18 20 240 Tm (Heading) Tj ET \
BT 0 g /F2 12 Tf 20 215 Td (Plain ) Tj /F1 12 Tf 0 0.2 0.5 rg (bold) Tj /F2 12 Tf 0 g ( words) Tj ET \
BT /F2 10 Tf 20 195 Td (Last row) Tj ET",
);
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "mixed.pdf", None, bytes).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let page = h.state().views[0].page_screen_rect(0).unwrap();
let point = egui::pos2(page.left() + 30.0 / 300.0 * page.width(), page.top() + 55.0 / 300.0 * page.height());
h.hover_at(point);
h.run_steps(1);
h.drag_at(point);
h.run_steps(1);
h.drop_at(point);
h.run_steps(6);
assert_eq!(h.state().views[0].session.line_editor.as_ref().unwrap().text, "Heading\nPlain bold words\nLast row");
// Unchanged text is painted by the page. An edit makes the editor paint its styled layout.
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text.push('!');
h.run_steps(3);
let galley = h
.output()
.shapes
.iter()
.find_map(|shape| match &shape.shape {
egui::Shape::Text(t) if t.galley.text().starts_with("Heading\n") => Some(&t.galley),
_ => None,
})
.expect("one galley contains the whole region");
assert_eq!(galley.rows.len(), 3);
assert!(galley.job.sections.iter().any(|s| s.format.color == egui::Color32::BLACK));
assert!(galley.job.sections.iter().any(|s| s.format.color == egui::Color32::from_rgb(0, 51, 128)));
assert!(galley.job.sections.iter().any(|s| s.format.font_id.size > galley.job.sections.last().unwrap().format.font_id.size));
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text.pop();
let original_text = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text.push_str("\nAdditional row\nAnother row");
h.run_steps(3);
let visible = h
.output()
.shapes
.iter()
.find_map(|s| match &s.shape {
egui::Shape::Text(t) if t.galley.text().ends_with("Another row") => Some(t.galley.rect.translate(t.pos.to_vec2())),
_ => None,
})
.unwrap();
let frame = h
.output()
.shapes
.iter()
.find_map(|s| match &s.shape {
egui::Shape::Path(path) if path.closed => {
let rect = egui::Rect::from_points(&path.points);
(rect.width() > 50.0 && rect.contains(visible.min)).then_some(rect)
}
_ => None,
})
.unwrap();
assert!(frame.contains_rect(visible), "fallback font rows remain inside the growing frame");
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = original_text;
h.run_steps(3);
if let Ok(path) = std::env::var("PDFCRAFT_RICH_FRAME_SHOT") {
h.render().unwrap().save(path).unwrap();
}
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text.clear();
h.run_steps(3);
h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);
h.run_steps(4);
assert!(h.state().session.get(h.state().views[0].id).unwrap().text_blocks(0).is_empty());
assert!(dirty(&h));
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().text_blocks(0)[0].rows.len(), 3);
}
#[test]
fn the_caret_on_a_small_row_is_as_tall_as_that_row() {
// It was one row of the region's first (18 pt) size tall, reaching into the row below.
let bytes = source_font_fixture_with_text(
"BT 0 0.2 0.5 rg /F1 1 Tf 18 0 0 18 20 240 Tm (Heading) Tj ET \
BT 0 g /F2 12 Tf 20 215 Td (Plain ) Tj /F1 12 Tf 0 0.2 0.5 rg (bold) Tj /F2 12 Tf 0 g ( words) Tj ET \
BT /F2 10 Tf 20 195 Td (Last row) Tj ET",
);
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "mixed.pdf", None, bytes).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let page = h.state().views[0].page_screen_rect(0).unwrap();
let k = page.width() / 300.0;
let screen = |x: f32, y: f32| egui::pos2(page.left() + x * k, page.top() + (300.0 - y) * k);
let click = |h: &mut Harness<'static, PdfCraftApp>, at: egui::Pos2| {
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(4);
};
click(&mut h, screen(30.0, 245.0));
assert!(h.state().views[0].session.line_editor.is_some(), "the region opens");
let row = h.state().session.get(h.state().views[0].id).unwrap().text_lines(0).into_iter().find(|l| l.text == "Last row").unwrap().rect;
click(&mut h, screen(40.0, ((row[1] + row[3]) / 2.0) as f32));
let row_height = (row[3] - row[1]) as f32 * k;
let (left, right) = (screen(row[0] as f32, 0.0).x - 2.0, screen(row[2] as f32, 0.0).x + 2.0);
let middle = screen(0.0, ((row[1] + row[3]) / 2.0) as f32).y;
// The caret: a vertical segment within the row's span, through its middle.
let caret = h
.output()
.shapes
.iter()
.find_map(|s| match &s.shape {
egui::Shape::Path(path) if path.points.len() == 2 => {
let [a, b] = [path.points[0], path.points[1]];
((a.x - b.x).abs() < 0.01 && (left..right).contains(&a.x) && a.y.min(b.y) < middle && a.y.max(b.y) > middle)
.then_some((b.y - a.y).abs())
}
egui::Shape::LineSegment { points: [a, b], .. }
if (a.x - b.x).abs() < 0.01 && (left..right).contains(&a.x) && a.y.min(b.y) < middle && a.y.max(b.y) > middle =>
{
Some((b.y - a.y).abs())
}
_ => None,
})
.expect("a caret on the last row");
assert!(caret > row_height * 0.8 && caret < row_height + 4.0, "caret {caret} px on a {row_height} px row");
}
/// One paragraph whose every word is its own run at its own size, in a font the editor can't load
/// (not embedded, and not a standard font), with glyphs much wider than its stand-in face's: the
/// shape of an OCR layer, and of a Type 1 paragraph from LaTeX. Returns the PDF and its words.
fn stand_in_words_fixture() -> (Vec<u8>, Vec<&'static str>) {
let words: Vec<(&str, f64)> = vec![
("In", 10.0),
("poker", 10.3),
("a", 15.1),
("full", 11.7),
("house", 10.7),
("is", 9.7),
("a", 15.1),
("set", 9.0),
("of", 10.4),
("cards", 10.0),
("that", 9.4),
("make", 9.0),
("a", 12.4),
("pair", 11.7),
];
let mut content = String::new();
let mut x = 20.0;
for (word, size) in &words {
content.push_str(&format!("BT /F1 {size} Tf {x:.2} 200 Td ({word}) Tj ET\n"));
x += word.len() as f64 * 0.7 * size + 4.0;
}
let widths = vec!["700"; 95].join(" ");
let objs: Vec<String> = vec![
"<< /Type /Catalog /Pages 2 0 R >>".into(),
"<< /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 700 300] >>".into(),
"<< /Type /Page /Parent 2 0 R /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>".into(),
format!("<< /Length {} >>\nstream\n{content}\nendstream", content.len()),
format!("<< /Type /Font /Subtype /Type1 /BaseFont /CMR10 /FirstChar 32 /LastChar 126 /Widths [{widths}] >>"),
];
let mut out = b"%PDF-1.7\n".to_vec();
let mut offsets = Vec::new();
for (i, o) in objs.iter().enumerate() {
offsets.push(out.len());
out.extend_from_slice(format!("{} 0 obj\n{o}\nendobj\n", i + 1).as_bytes());
}
let xref = out.len();
out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
for o in offsets {
out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
}
out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes());
(out, words.into_iter().map(|(w, _)| w).collect())
}
/// A drag over a word of an unchanged paragraph selects that word. The editor lays the paragraph
/// out unseen in a stand-in face, and stretching each run to its width let every word boundary
/// drift: over an OCR layer, dragging across "kind" selected "of a", two words before it.
#[test]
fn dragging_over_a_word_selects_that_word_when_the_editor_uses_a_stand_in_face() {
let (bytes, words) = stand_in_words_fixture();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "ocr-like.pdf", None, bytes).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let page = h.state().views[0].page_screen_rect(0).unwrap();
let k = page.width() / 700.0;
let screen = |x: f64, y: f64| egui::pos2(page.left() + x as f32 * k, page.top() + (300.0 - y) as f32 * k);
let lines = h.state().session.get(h.state().views[0].id).unwrap().text_lines(0);
assert_eq!(lines.len(), words.len(), "one run per word: {:?}", lines.iter().map(|l| &l.text).collect::<Vec<_>>());
let first = lines[0].rect;
let at = screen((first[0] + first[2]) / 2.0, (first[1] + first[3]) / 2.0);
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(4);
let opened = h.state().views[0].session.line_editor.as_ref().map(|e| e.text.clone());
assert_eq!(opened.as_deref(), Some(words.join(" ").as_str()), "the words open as one paragraph");
// Drag across each of the last words, just inside its box, as a reader aims at what the page shows.
for target in [words.len() - 1, words.len() - 4, 9] {
let r = lines[target].rect;
let middle = (r[1] + r[3]) / 2.0;
let (from, to) = (screen(r[0] + 0.5, middle), screen(r[2] - 0.5, middle));
h.hover_at(from);
h.run_steps(1);
h.drag_at(from);
h.run_steps(1);
for step in 1..=6 {
h.hover_at(from + (to - from) * (step as f32 / 6.0));
h.run_steps(1);
}
h.drop_at(to);
h.run_steps(3);
let ed = h.state().views[0].session.line_editor.as_ref().expect("the paragraph stays open");
let selected: String = ed
.selection
.map(|s| {
let (from, to) = (s.anchor.min(s.caret), s.anchor.max(s.caret));
ed.text.chars().skip(from).take(to - from).collect()
})
.unwrap_or_default();
assert_eq!(selected, words[target], "dragging over word {target} ({:?})", words[target]);
}
}
/// Opt-in private-document regression. This never captures images or saves the source file.
/// Run with PDFCRAFT_TEXT_REGRESSION_PDF=/path/to/document.pdf and --ignored.
#[test]
#[ignore = "requires an explicitly supplied local PDF"]
fn private_document_regions_open_as_one_editor() {
let path = std::env::var("PDFCRAFT_TEXT_REGRESSION_PDF").expect("supply a local PDF path");
let bytes = std::fs::read(&path).unwrap();
let original = bytes.clone();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "private.pdf", None, bytes).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
h.state_mut().views[0].view.fit = pdfcraft_ui_egui::canvas::Fit::Page;
assert!(h.execute("view.layout.single"));
h.run_steps(3);
let page_count = h.state().session.get(h.state().views[0].id).unwrap().info.pages.len();
let mut checked = 0;
for page_index in 0..page_count {
h.state_mut().views[0].go_to_page(page_index);
h.run_steps(4);
let blocks = h.state().session.get(h.state().views[0].id).unwrap().text_blocks(page_index);
let page = h.state().views[0].page_screen_rect(page_index).unwrap();
let info = h.state().session.get(h.state().views[0].id).unwrap().info.pages[page_index].clone();
let source_lines = h.state().session.get(h.state().views[0].id).unwrap().text_lines(page_index);
let programs = h.state().session.get(h.state().views[0].id).unwrap().text_editor_fonts(page_index);
assert!(!programs.is_empty(), "source sfnt fonts must be usable in the editor");
let scale = page.width() / info.width;
for (index, block) in blocks.iter().enumerate().filter(|(_, b)| b.rows.len() >= 3) {
let line = &source_lines[block.lines[0]];
let v = info.user_to_view((line.rect[0] + 3.0) as f32, (line.rect[1] + line.rect[3]) as f32 / 2.0);
let point = egui::pos2(page.left() + v[0] * scale, page.top() + v[1] * scale);
h.hover_at(point);
h.run_steps(1);
h.drag_at(point);
h.run_steps(1);
h.drop_at(point);
h.run_steps(8);
let editor = h.state().views[0].session.line_editor.as_ref().unwrap();
assert_eq!(editor.block, index);
assert_eq!(editor.text, block.editor_text);
assert!(!editor.overflow, "unchanged source region must fit (page {}, region {})", page_index + 1, index + 1);
let text_shape = h
.output()
.shapes
.iter()
.find_map(|s| match &s.shape {
egui::Shape::Text(t) if t.galley.text() == block.editor_text => Some(t),
_ => None,
})
.expect("the whole source region is editable");
for (section, (_, source)) in text_shape.galley.job.sections.iter().zip(block.edited_spans(&block.editor_text)) {
if programs.iter().any(|font| font.resource == source_lines[source].font) {
assert!(
matches!(&section.format.font_id.family,
egui::FontFamily::Name(name) if name.starts_with("pdf-source-")),
"each available embedded source face is used in the editor"
);
}
}
assert_eq!(text_shape.galley.rows.len(), block.rows.len(), "source rows stay in place");
for (row, source_row) in text_shape.galley.rows.iter().zip(&block.rows) {
let source = &source_lines[source_row[0]];
let baseline = text_shape.pos.y + row.pos.y + row.glyphs[0].pos.y;
let expected = page.top() + info.user_to_view(0.0, source.baseline()[1] as f32)[1] * scale;
assert!((baseline - expected).abs() < 1.5, "editor baseline stays on the source baseline");
}
let frames = h
.output()
.shapes
.iter()
.filter(|s| {
matches!(&s.shape,
egui::Shape::Rect(r) if r.stroke.color==egui::Color32::from_rgb(0x14,0x73,0xE6)
&& r.rect.width()>50.0 && page.contains(r.rect.center()))
})
.count();
assert_eq!(frames, 1, "one stable editor frame");
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text.push_str("\nAdditional text\nAdditional text");
h.run_steps(3);
let editor = h.state().views[0].session.line_editor.as_ref().unwrap();
assert!(editor.overflow, "extra rows extend the block");
let draft =
h.state().session.get(h.state().views[0].id).unwrap().text_edit_layout(page_index, index, &editor.text, &editor.style()).unwrap();
let bounds = draft.draft_rect.unwrap();
assert!((bounds[3] - block.rect[3]).abs() < 0.5, "draft bounds exclude untouched neighboring regions");
h.key_press(Key::Escape);
h.run_steps(2);
checked += 1;
}
}
assert!(checked > 0, "the supplied PDF must contain a multiline region");
assert!(!dirty(&h));
assert_eq!(std::fs::read(path).unwrap(), original);
}
#[test]
fn inline_editor_preserves_source_font_and_previews_live_bounds() {
use pdfcraft_cos::{Dict, Document, Object, Stream, write_incremental};
use skrifa::MetadataProvider as _;
let _gpu = gpu();
let mut cos = Document::open(std::sync::Arc::new(source_font_fixture_with_text(
"BT /F1 18 Tf 20 220 Td (Stable source text) Tj ET BT /F1 12 Tf 20 194 Td (The second row stays here) Tj ET",
)))
.unwrap();
let bytes = include_bytes!("../../../../assets/fonts/Inter-Regular.ttf");
let face = skrifa::FontRef::new(bytes).unwrap();
let metrics = face.metrics(skrifa::instance::Size::unscaled(), skrifa::instance::LocationRef::default());
let glyphs = face.glyph_metrics(skrifa::instance::Size::unscaled(), skrifa::instance::LocationRef::default());
let units = 1000.0 / f64::from(metrics.units_per_em);
let program = cos.add(Object::Stream(Stream::flate(Dict::new(), bytes)));
let mut descriptor = Dict::new();
descriptor.set(b"FontFile2".to_vec(), Object::Ref(program));
descriptor.set(b"Ascent".to_vec(), Object::Real(f64::from(metrics.ascent) * units));
descriptor.set(b"Descent".to_vec(), Object::Real(f64::from(metrics.descent) * units));
let mut font = Dict::new();
font.set(b"Type".to_vec(), Object::name("Font"));
font.set(b"Subtype".to_vec(), Object::name("TrueType"));
font.set(b"BaseFont".to_vec(), Object::name("Inter-Regular"));
font.set(b"Encoding".to_vec(), Object::name("WinAnsiEncoding"));
font.set(b"FirstChar".to_vec(), Object::Int(32));
font.set(b"LastChar".to_vec(), Object::Int(126));
font.set(
b"Widths".to_vec(),
Object::Array(
(32..=126u8)
.map(|c| {
let id = face.charmap().map(char::from(c)).unwrap();
Object::Real(f64::from(glyphs.advance_width(id).unwrap()) * units)
})
.collect(),
),
);
font.set(b"FontDescriptor".to_vec(), Object::Dict(descriptor));
cos.update_dict(pdfcraft_cos::ObjRef::new(3, 0), |d| *d = font).unwrap();
let bytes = write_incremental(&cos, &Default::default()).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "stable.pdf", None, bytes).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let source = h.state().session.get(h.state().views[0].id).unwrap().text_blocks(0)[0].clone();
let lines = h.state().session.get(h.state().views[0].id).unwrap().text_lines(0);
let page = h.state().views[0].page_screen_rect(0).unwrap();
let scale = page.width() / 300.0;
let point = egui::pos2(page.left() + 30.0 * scale, page.top() + 75.0 * scale);
h.hover_at(point);
h.run_steps(1);
h.drag_at(point);
h.run_steps(1);
h.drop_at(point);
h.run_steps(8);
assert!(!h.state().views[0].session.line_editor.as_ref().unwrap().overflow);
// The page paints unchanged text. Change only its colour to exercise the editor's
// source-font layout without changing the text or its geometry.
let original_color = h.state().views[0].session.line_editor.as_ref().unwrap().look.color;
h.state_mut().views[0].session.line_editor.as_mut().unwrap().look.color = [0.2, 0.3, 0.4];
h.run_steps(3);
let shape = h
.output()
.shapes
.iter()
.find_map(|s| match &s.shape {
egui::Shape::Text(t) if t.galley.text() == source.editor_text => Some(t),
_ => None,
})
.unwrap();
assert_eq!(shape.galley.rows.len(), 2);
for (row, source_row) in shape.galley.rows.iter().zip(&source.rows) {
let line = &lines[source_row[0]];
let y = shape.pos.y + row.pos.y + row.glyphs[0].pos.y;
assert!((y - (page.top() + (300.0 - line.baseline()[1] as f32) * scale)).abs() < 1.0);
}
for section in &shape.galley.job.sections {
assert!(matches!(&section.format.font_id.family,egui::FontFamily::Name(name) if name.starts_with("face:pdf-source-")));
}
h.state_mut().views[0].session.line_editor.as_mut().unwrap().look.color = original_color;
h.run_steps(3);
let frame = |h: &Harness<'_, PdfCraftApp>| h.get_by_label("Text editing line").rect().expand(2.0);
let original_frame = frame(&h);
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text.push_str("\nExtra row that does not fit\nAnother row");
h.run_steps(3);
assert!(h.state().views[0].session.line_editor.as_ref().unwrap().overflow);
let expanded = frame(&h);
assert!(expanded.bottom() > original_frame.bottom() + 20.0, "the frame visibly expands around the extra rows");
assert_eq!(expanded.left(), original_frame.left());
assert_eq!(expanded.top(), original_frame.top());
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = "Stable source text".into();
h.run_steps(3);
let reduced = frame(&h);
assert!(reduced.bottom() < original_frame.bottom() - 20.0, "deleting a row visibly shrinks the block");
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text.push_str("\nExtra row that does not fit\nAnother row");
h.run_steps(3);
if let Ok(path) = std::env::var("PDFCRAFT_OVERFLOW_FRAME_SHOT") {
h.render().unwrap().save(path).unwrap();
}
let id = h.state().views[0].id;
let editor = h.state().views[0].session.line_editor.as_ref().unwrap();
let draft_text = editor.text.clone();
let measured = h.state().session.get(id).unwrap().text_edit_layout(0, 0, &draft_text, &editor.style()).unwrap().draft_rect.unwrap();
let expected_frame = egui::Rect::from_min_max(
egui::pos2(page.left() + measured[0] as f32 * scale, page.top() + (300.0 - measured[3] as f32) * scale),
egui::pos2(page.left() + measured[2] as f32 * scale, page.top() + (300.0 - measured[1] as f32) * scale),
)
.expand(2.0);
let live = frame(&h);
assert!(
(live.min - expected_frame.min).length() < 0.1 && (live.max - expected_frame.max).length() < 0.1,
"the live frame shows the same PDF geometry as Apply/Save"
);
assert!(!dirty(&h), "draft measurement does not modify the document");
h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);
h.run_steps(3);
assert!(dirty(&h));
assert_eq!(h.state().session.get(id).unwrap().text_blocks(0)[0].rect, measured);
let bytes = h.state().session.save_bytes(id).unwrap();
let mut reopened = pdfcraft_engine::Session::new();
let reopened_id = reopened.open("again.pdf", None, bytes, None).unwrap();
assert_eq!(reopened.get(reopened_id).unwrap().text_blocks(0)[0].rect, measured);
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().text_blocks(0)[0], source);
}
fn clipped_artwork_with_labels() -> Vec<u8> {
clipped_artwork_with_labels_drawn(false)
}
/// `own_clip` draws the image alone in its clip (`q re W n cm Do Q`), which moves with it (#1049).
fn clipped_artwork_with_labels_drawn(own_clip: bool) -> Vec<u8> {
artwork_drawn(if own_clip {
&b"q 30 170 130 80 re W* n 800 0 0 900 -300 -400 cm /Im0 Do Q BT /F1 12 Tf 30 130 Td (Editable caption) Tj ET BT /F1 12 Tf 40 220 Td (Overlay) Tj ET BT /F1 9 Tf 154 249 Td (Corner label) Tj ET"[..]
} else {
&b"q 30 170 130 80 re W* n q 800 0 0 900 -300 -400 cm /Im0 Do Q Q BT /F1 12 Tf 30 130 Td (Editable caption) Tj ET BT /F1 12 Tf 40 220 Td (Overlay) Tj ET BT /F1 9 Tf 154 249 Td (Corner label) Tj ET"[..]
})
}
/// [`fixture`]'s two pages, each drawn by `content`, with a one-pixel picture as `/Im0`.
fn artwork_drawn(content: &[u8]) -> Vec<u8> {
use pdfcraft_cos::{Dict, Document, Object, Stream, write_incremental};
let mut doc = Document::open(std::sync::Arc::new(fixture(2))).unwrap();
let mut image = Dict::new();
image.set(b"Type".to_vec(), Object::name("XObject"));
image.set(b"Subtype".to_vec(), Object::name("Image"));
image.set(b"Width".to_vec(), Object::Int(1));
image.set(b"Height".to_vec(), Object::Int(1));
image.set(b"BitsPerComponent".to_vec(), Object::Int(8));
image.set(b"ColorSpace".to_vec(), Object::name("DeviceRGB"));
let image = doc.add(Object::Stream(Stream::flate(image, &[220, 235, 245])));
for page in pdfcraft_model::pages(&doc) {
let contents = doc.add(Object::Stream(Stream::flate(Dict::new(), content)));
let mut resources = doc.resolve(page.dict.get(b"Resources").unwrap()).as_dict().unwrap().clone();
let mut xo = Dict::new();
xo.set(b"Im0".to_vec(), Object::Ref(image));
resources.set(b"XObject".to_vec(), Object::Dict(xo));
doc.update_dict(page.obj, |d| {
d.set(b"Resources".to_vec(), Object::Dict(resources));
d.set(b"Contents".to_vec(), Object::Ref(contents));
})
.unwrap();
}
write_incremental(&doc, &Default::default()).unwrap()
}
#[test]
fn cropped_images_do_not_intercept_text_and_stay_inside_each_page() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "clipped.pdf", None, clipped_artwork_with_labels()).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.state_mut().views[0].view.fit = pdfcraft_ui_egui::canvas::Fit::Page;
assert!(h.execute("view.layout.single"));
h.run_steps(3);
for page in 0..2 {
h.state_mut().views[0].go_to_page(page);
h.run_steps(3);
let r = h.state().views[0].page_screen_rect(page).unwrap();
let at = |x: f32, y: f32| egui::pos2(r.left() + x / 200.0 * r.width(), r.top() + (300.0 - y) / 300.0 * r.height());
for (x, y, label) in [(65.0, 134.0, "Editable caption"), (65.0, 224.0, "Overlay")] {
let p = at(x, y);
h.hover_at(p);
h.run_steps(1);
h.drag_at(p);
h.run_steps(1);
h.drop_at(p);
h.run_steps(3);
assert_eq!(h.state().views[0].session.line_editor.as_ref().unwrap().text, label);
assert!(h.state().views[0].session.image_selection.is_none(), "text wins even inside the visible artwork");
h.key_press(Key::Escape);
h.run_steps(2);
}
let p = at(95.0, 195.0);
h.hover_at(p);
h.run_steps(1);
h.drag_at(p);
h.run_steps(1);
h.drop_at(p);
h.run_steps(3);
assert!(h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.page == page));
for shape in &h.output().shapes {
if let egui::Shape::Rect(rect) = &shape.shape
&& rect.stroke.color == egui::Color32::from_rgb(0x14, 0x73, 0xE6)
&& rect.rect.width() > 50.0
{
assert!(r.expand(3.0).contains_rect(rect.rect), "no image frame escapes its own page");
}
}
}
assert!(!dirty(&h));
}
#[test]
fn cropped_image_corner_resize_keeps_opposite_corner_and_scales_crop() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "clipped.pdf", None, clipped_artwork_with_labels_drawn(true)).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.state_mut().views[0].view.fit = pdfcraft_ui_egui::canvas::Fit::Page;
assert!(h.execute("view.layout.single"));
h.run_steps(3);
let page = h.state().views[0].page_screen_rect(0).unwrap();
let at = |x: f32, y: f32| egui::pos2(page.left() + x / 200.0 * page.width(), page.top() + (300.0 - y) / 300.0 * page.height());
let p = at(95.0, 195.0);
h.hover_at(p);
h.run_steps(1);
h.drag_at(p);
h.run_steps(1);
h.drop_at(p);
h.run_steps(3);
let id = h.state().views[0].id;
assert!(h.state().views[0].session.image_selection.is_some());
let text = |doc: &pdfcraft_engine::Document| {
doc.text_lines(0).into_iter().map(|l| (l.text.clone(), l.rect, l.font.clone(), l.size, l.color, l.baseline())).collect::<Vec<_>>()
};
let original_text = text(h.state().session.get(id).unwrap());
// Grow then shrink using the visible top-right grip, never the off-page raw placement.
for (factor, right, top, horizontal_only, off_centre) in [
(1.15, 179.5, 262.0, false, false),
(0.7, 134.65, 234.4, false, false),
(0.75, 108.4875, 218.3, true, false),
(1.1, 116.33625, 223.13, false, true),
] {
let before = h.state().session.get(id).unwrap().page_images(0)[0].visible_rect.unwrap();
let grip = at(before[2] as f32, before[3] as f32);
let offset = if off_centre { egui::vec2(3.0, -3.0) } else { egui::Vec2::ZERO };
let from = grip + offset;
let fixed = at(before[0] as f32, before[1] as f32);
let desired = fixed + (grip - fixed) * factor + offset;
let to = if horizontal_only { egui::pos2(desired.x, from.y) } else { desired };
let generation = h.state().session.get(id).unwrap().edit_generation();
h.hover_at(from);
h.run_steps(1);
h.drag_at(from);
h.run_steps(1);
for k in 1..=5 {
h.hover_at(from + (to - from) * (k as f32 / 5.0));
h.run_steps(1);
}
assert_eq!(h.state().session.get(id).unwrap().edit_generation(), generation, "dragging previews without committing every frame");
h.drop_at(to);
h.run_steps(4);
assert!(h.state().views[0].session.line_editor.is_none(), "a label under a selected image grip cannot take its drag");
let after = h.state().session.get(id).unwrap().page_images(0)[0].visible_rect.unwrap();
for (actual, expected) in after.into_iter().zip([30.0, 170.0, right, top]) {
assert!((actual - expected).abs() < 0.05, "cropped image follows the visible grip: {actual} vs {expected}");
}
assert_eq!(text(h.state().session.get(id).unwrap()), original_text, "image resizing leaves captions in place");
}
let saved = h.state().session.save_bytes(id).unwrap();
let mut reopened = pdfcraft_engine::Session::new();
let re = reopened.open("again.pdf", None, saved, None).unwrap();
assert_eq!(reopened.get(re).unwrap().page_images(0), h.state().session.get(id).unwrap().page_images(0));
}
/// While a corner is dragged, the image's pixels are drawn in the blue frame, not left at their
/// old size until release (#1671).
#[test]
fn image_pixels_follow_the_frame_while_a_corner_is_dragged() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "clipped.pdf", None, clipped_artwork_with_labels_drawn(true)).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.state_mut().views[0].view.zoom = 1.2;
h.run_steps(4);
// The dragged pixels are the page raster's: wait for it, as a slow runner may not have it yet
// (the drag then drew no picture at all).
assert!(
crate::support::step_until(&mut h, |h| {
h.run_steps(1);
!h.state().render_pending()
}),
"the page raster arrives"
);
let page = h.state().views[0].page_screen_rect(0).unwrap();
let at = |x: f32, y: f32| egui::pos2(page.left() + x / 200.0 * page.width(), page.top() + (300.0 - y) / 300.0 * page.height());
let p = at(95.0, 195.0);
h.hover_at(p);
h.run_steps(1);
h.drag_at(p);
h.run_steps(1);
h.drop_at(p);
h.run_steps(3);
assert!(h.state().views[0].session.image_selection.is_some());
let frame = |h: &Harness<'static, PdfCraftApp>| {
let top_right = h.get_by_role_and_label(Role::Button, "Top right corner").rect().center();
let bottom_left = h.get_by_role_and_label(Role::Button, "Bottom left corner").rect().center();
egui::Rect::from_two_pos(top_right, bottom_left)
};
// Textured meshes smaller than the page: the page raster itself always covers the page.
let pictures = |h: &Harness<'static, PdfCraftApp>| -> Vec<egui::Rect> {
h.output()
.shapes
.iter()
.filter_map(|s| match &s.shape {
egui::Shape::Mesh(m) if m.texture_id != egui::TextureId::default() => Some(m.calc_bounds()),
_ => None,
})
.filter(|b| b.width() < page.width() - 1.0)
.collect()
};
// The dragged picture is cut from the page raster, which a render thread delivers after
// however many frames it takes (more on a slow runner): wait for it on screen, by state.
let raster_shown = |h: &Harness<'static, PdfCraftApp>| {
h.output().shapes.iter().any(|s| {
matches!(&s.shape, egui::Shape::Mesh(m) if m.texture_id != egui::TextureId::default() && m.calc_bounds().width() >= page.width() - 1.0)
})
};
let wait_for_raster = |h: &mut Harness<'static, PdfCraftApp>| {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
while h.state().render_pending() || !raster_shown(h) {
assert!(std::time::Instant::now() < deadline, "timed out waiting for the page raster");
h.step();
std::thread::sleep(std::time::Duration::from_millis(5));
}
};
let start = frame(&h);
let (grip, fixed) = (start.right_top(), start.left_bottom());
for factor in [0.6, 1.3] {
wait_for_raster(&mut h);
let to = fixed + (grip - fixed) * factor;
h.hover_at(grip);
h.run_steps(1);
h.drag_at(grip);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(grip + (to - grip) * (k as f32 / 4.0));
h.run_steps(1);
}
let live = frame(&h);
assert!((live.width() - start.width() * factor).abs() < 1.0, "the frame follows the grip: {live:?} from {start:?}");
let drawn = pictures(&h);
assert!(
drawn.iter().any(|b| (b.min - live.min).length() < 0.5 && (b.max - live.max).length() < 0.5),
"the picture is drawn in the frame {live:?}, not only at {drawn:?}"
);
h.drop_at(to);
h.run_steps(4);
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(4);
}
}
/// Red pixels in `r`: the test picture.
fn red(pixels: &image::RgbaImage, r: egui::Rect) -> usize {
let (x0, y0, x1, y1) = (r.left().max(0.0) as u32, r.top().max(0.0) as u32, r.right() as u32, r.bottom() as u32);
(y0..y1.min(pixels.height()))
.flat_map(|y| (x0..x1.min(pixels.width())).map(move |x| (x, y)))
.filter(|(x, y)| matches!(pixels.get_pixel(*x, *y).0, [r, g, b, _] if r > 150 && g < 90 && b < 90))
.count()
}
/// A resized picture is applied when the corner is let go, but the page raster still has it at
/// its old size until the page is rendered again. Until then it stays drawn at its new size and
/// blanked at its old one; the old one used to show again for those frames.
#[test]
fn a_resized_image_keeps_its_new_size_until_the_page_is_redrawn() {
use pdfcraft_cos::{Dict, Document, Object, Stream, write_incremental};
let _gpu = gpu();
let mut doc = Document::open(std::sync::Arc::new(fixture(1))).unwrap();
let mut image = Dict::new();
image.set(b"Type".to_vec(), Object::name("XObject"));
image.set(b"Subtype".to_vec(), Object::name("Image"));
image.set(b"Width".to_vec(), Object::Int(1));
image.set(b"Height".to_vec(), Object::Int(1));
image.set(b"BitsPerComponent".to_vec(), Object::Int(8));
image.set(b"ColorSpace".to_vec(), Object::name("DeviceRGB"));
let image = doc.add(Object::Stream(Stream::flate(image, &[200, 30, 30])));
let page = pdfcraft_model::pages(&doc).remove(0);
let contents = doc.add(Object::Stream(Stream::flate(Dict::new(), b"q 80 0 0 80 60 40 cm /Im0 Do Q")));
let mut resources = doc.resolve(page.dict.get(b"Resources").unwrap()).as_dict().unwrap().clone();
let mut xo = Dict::new();
xo.set(b"Im0".to_vec(), Object::Ref(image));
resources.set(b"XObject".to_vec(), Object::Dict(xo));
doc.update_dict(page.obj, |d| {
d.set(b"Resources".to_vec(), Object::Dict(resources));
d.set(b"Contents".to_vec(), Object::Ref(contents));
})
.unwrap();
let bytes = write_incremental(&doc, &Default::default()).unwrap();
// The jobs, the page renders among them, wait until the test runs them.
let (app, jobs) = crate::control::app_with_manual_jobs();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = app;
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.pdf", None, bytes).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.state_mut().views[0].view.fit = pdfcraft_ui_egui::canvas::Fit::Page;
h.run_steps(4);
// The page raster.
support::settle_held(&mut h, &jobs);
let page = h.state().views[0].page_screen_rect(0).unwrap();
let at = |x: f32, y: f32| egui::pos2(page.left() + x / 200.0 * page.width(), page.top() + (300.0 - y) / 300.0 * page.height());
click_at(&mut h, at(100.0, 80.0));
assert!(h.state().views[0].session.image_selection.is_some(), "the picture is selected");
support::settle_held(&mut h, &jobs);
// From 60..140 × 40..120 to half its size, keeping its lower left corner.
let (grip, to) = (at(140.0, 120.0), at(100.0, 80.0));
let kept = egui::Rect::from_two_pos(at(62.0, 42.0), at(98.0, 78.0));
let gone = egui::Rect::from_two_pos(at(104.0, 84.0), at(138.0, 118.0));
h.hover_at(grip);
h.run_steps(1);
h.drag_at(grip);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(grip + (to - grip) * (k as f32 / 4.0));
h.run_steps(1);
}
// While dragged, no outline stays where the picture was.
let old = egui::Rect::from_two_pos(at(60.0, 40.0), at(140.0, 120.0));
let trailing = h.output().shapes.iter().any(|shape| {
matches!(&shape.shape, egui::Shape::Rect(r) if r.stroke.width == 0.75 && (r.rect.min - old.min).length() < 2.0 && (r.rect.max - old.max).length() < 2.0)
});
assert!(!trailing, "the dragged picture's outline trails behind it");
// From here the renders wait: the frames below see the page as it was before the release.
h.drop_at(to);
for frame in 0..4 {
h.step();
let pixels = h.render().unwrap();
assert!(red(&pixels, kept) > 100, "frame {frame} after the release: the picture is not at its new size");
assert_eq!(red(&pixels, gone), 0, "frame {frame} after the release: the picture shows at its old size");
}
// Resized again before the page is drawn: the raster still has the first size, so that is
// what stays blanked while the corner moves.
let (grip, to) = (to, at(80.0, 60.0));
let again = egui::Rect::from_two_pos(at(62.0, 42.0), at(78.0, 58.0));
h.hover_at(grip);
h.run_steps(1);
h.drag_at(grip);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(grip + (to - grip) * (k as f32 / 4.0));
h.run_steps(1);
}
let pixels = h.render().unwrap();
assert!(red(&pixels, again) > 20, "the second drag draws the picture at its new size");
assert_eq!(red(&pixels, gone), 0, "the second drag shows the picture at its first size");
h.drop_at(to);
h.run_steps(2);
let pixels = h.render().unwrap();
assert_eq!(red(&pixels, gone), 0, "after the second release");
// The page drawn with the resized picture.
support::settle_held(&mut h, &jobs);
h.run_steps(2);
let pixels = h.render().unwrap();
assert!(red(&pixels, again) > 20 && red(&pixels, gone) == 0, "the page raster has the picture at its new size");
}
/// Exercise actual pointer gestures on each cropped placement, without capturing private pixels.
#[test]
#[ignore = "requires an explicitly supplied local PDF with cropped artwork"]
fn private_cropped_image_grips_resize_without_moving_the_opposite_corner() {
let path = std::env::var("PDFCRAFT_CLIPPED_ARTWORK_PDF").expect("supply a local PDF path");
let original = std::fs::read(&path).unwrap();
let bytes = original.clone();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "private.pdf", None, bytes).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.state_mut().views[0].view.fit = pdfcraft_ui_egui::canvas::Fit::Page;
assert!(h.execute("view.layout.single"));
let id = h.state().views[0].id;
let pages = h.state().session.get(id).unwrap().info.pages.len();
let mut checked = 0;
for page_index in 0..pages {
h.state_mut().views[0].go_to_page(page_index);
h.run_steps(4);
let doc = h.state().session.get(id).unwrap();
let info = doc.info.pages[page_index].clone();
let images = doc.page_images(page_index);
let blocks = doc.text_blocks(page_index);
let page = h.state().views[0].page_screen_rect(page_index).unwrap();
let at = |x: f64, y: f64| {
let v = info.user_to_view(x as f32, y as f32);
page.min + egui::vec2(v[0], v[1]) * (page.width() / info.width)
};
for (index, image) in images.iter().enumerate() {
let Some(b) = image.visible_rect else { continue };
let blank = (1..10)
.flat_map(|x| (1..10).map(move |y| (x, y)))
.map(|(x, y)| (b[0] + (b[2] - b[0]) * x as f64 / 10.0, b[1] + (b[3] - b[1]) * y as f64 / 10.0))
.find(|(x, y)| !blocks.iter().any(|l| *x >= l.rect[0] && *x <= l.rect[2] && *y >= l.rect[1] && *y <= l.rect[3]))
.expect("image has an unlabelled selection point");
let corners = [at(b[0], b[3]), at(b[2], b[3]), at(b[0], b[1]), at(b[2], b[1])];
for corner in 0..4 {
for (factor, axis) in [(0.8, 0), (1.04, 0), (0.8, 1), (0.8, 2)] {
let p = at(blank.0, blank.1);
h.hover_at(p);
h.run_steps(1);
h.drag_at(p);
h.run_steps(1);
h.drop_at(p);
h.run_steps(3);
assert_eq!(h.state().views[0].session.image_selection.as_ref().map(|s| s.index), Some(index));
let generation = h.state().session.get(id).unwrap().edit_generation();
let from = corners[corner];
let fixed = corners[3 - corner];
let desired = fixed + (from - fixed) * factor;
let to = match axis {
1 => egui::pos2(desired.x, from.y),
2 => egui::pos2(from.x, desired.y),
_ => desired,
};
h.hover_at(from);
h.run_steps(1);
h.drag_at(from);
h.run_steps(1);
for k in 1..=5 {
h.hover_at(from + (to - from) * (k as f32 / 5.0));
h.run_steps(1);
assert_eq!(h.state().session.get(id).unwrap().edit_generation(), generation, "no PDF rewrite on pointer movement");
}
h.drop_at(to);
h.run_steps(4);
let after = h.state().session.get(id).unwrap().page_images(page_index)[index].visible_rect.unwrap();
let actual = egui::Rect::from_two_pos(at(after[0], after[1]), at(after[2], after[3]));
let expected = egui::Rect::from_two_pos(fixed, desired).intersect(page);
assert!(
(actual.min - expected.min).length() < 0.2 && (actual.max - expected.max).length() < 0.2,
"page {}, image {}, corner {corner}: {actual:?} vs {expected:?}",
page_index + 1,
index + 1
);
assert!(h.state().views[0].session.line_editor.is_none());
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(4);
checked += 1;
}
}
}
}
assert_eq!(h.state().session.get(id).unwrap().bytes.as_slice(), original.as_slice(), "undo restored the source document in memory");
assert_eq!(std::fs::read(path).unwrap(), original);
println!("verified {checked} real-file corner drag gestures on {pages} pages");
}
/// Local acceptance check for PDFs containing cropped artwork. No screenshots or source saves.
#[test]
#[ignore = "requires an explicitly supplied local PDF with cropped artwork"]
fn private_document_cropped_artwork_does_not_block_text() {
let path = std::env::var("PDFCRAFT_CLIPPED_ARTWORK_PDF").expect("supply a local PDF path");
let original = std::fs::read(&path).unwrap();
let bytes = original.clone();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "private.pdf", None, bytes).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.state_mut().views[0].view.fit = pdfcraft_ui_egui::canvas::Fit::Page;
assert!(h.execute("view.layout.single"));
h.run_steps(3);
let pages = h.state().session.get(h.state().views[0].id).unwrap().info.pages.len();
let mut checked = 0;
let mut cropped = 0;
for page in 0..pages {
h.state_mut().views[0].go_to_page(page);
h.run_steps(4);
let doc = h.state().session.get(h.state().views[0].id).unwrap();
let info = doc.info.pages[page].clone();
let blocks = doc.text_blocks(page);
let images = doc.page_images(page);
let r = h.state().views[0].page_screen_rect(page).unwrap();
for image in &images {
let visible = image.visible_rect.unwrap();
assert!(visible[0] >= f64::from(info.crop[0]) - 0.01 && visible[1] >= f64::from(info.crop[1]) - 0.01);
assert!(visible[2] <= f64::from(info.crop[2]) + 0.01 && visible[3] <= f64::from(info.crop[3]) + 0.01);
if image.rect != visible {
cropped += 1;
}
}
for (index, block) in blocks.iter().enumerate() {
let [x0, y0, x1, y1] = block.rect;
let v = info.user_to_view(((x0 + x1) / 2.0) as f32, ((y0 + y1) / 2.0) as f32);
let scale = r.width() / info.width;
let p = egui::pos2(r.left() + v[0] * scale, r.top() + v[1] * scale);
h.hover_at(p);
h.run_steps(1);
h.drag_at(p);
h.run_steps(1);
h.drop_at(p);
h.run_steps(3);
assert_eq!(h.state().views[0].session.line_editor.as_ref().map(|e| e.block), Some(index), "every source block can open directly");
assert!(h.state().views[0].session.image_selection.is_none());
h.key_press(Key::Escape);
h.run_steps(2);
checked += 1;
}
}
assert!(checked > 0 && cropped > 0);
assert!(!dirty(&h));
assert_eq!(std::fs::read(path).unwrap(), original);
println!("checked {pages} pages, {checked} text regions, {cropped} clipped image placements");
}
/// Actual keyboard input into every page of a private Chinese PDF; no private captures.
#[test]
#[ignore = "requires a private Chinese PDF and an approved full source font file"]
fn private_new_chinese_keyboard_input_preserves_font_layout_and_saved_pixels() {
use pdfcraft_engine::{Edit, Session};
use std::sync::Arc;
let path = std::env::var("PDFCRAFT_CHINESE_TEXT_PDF").expect("supply a local PDF path");
let font_path = std::env::var("PDFCRAFT_CHINESE_FULL_FONT").expect("supply the full source face");
let original = std::fs::read(&path).unwrap();
let bytes = original.clone();
let font = Arc::new(std::fs::read(font_path).unwrap());
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "private.pdf", None, bytes).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.state_mut().views[0].view.fit = pdfcraft_ui_egui::canvas::Fit::Page;
assert!(h.execute("view.layout.single"));
let id = h.state().views[0].id;
let pages = h.state().session.get(id).unwrap().info.pages.len();
let all: String = (0..pages).flat_map(|p| h.state().session.get(id).unwrap().text_lines(p)).map(|l| l.text).collect();
let new: String = "测试编辑删除龘".chars().filter(|c| !all.contains(*c)).take(4).collect();
assert!(new.chars().count() >= 2);
let pixels = |bytes: Arc<Vec<u8>>, page| {
let mut r = PageRenderer::new(bytes, Default::default());
let p = r.render(RenderRequest { page, scale: 1.5, ..Default::default() });
assert!(p.error.is_none() && p.warnings.is_empty());
p.rgba.to_vec()
};
for page_index in 0..pages {
h.state_mut().views[0].go_to_page(page_index);
h.state_mut().session.apply(id, Edit::RegisterTextFont { page: page_index, bytes: font.clone() }).unwrap();
h.run_steps(4);
let doc = h.state().session.get(id).unwrap();
let (index, block) = doc
.text_blocks(page_index)
.into_iter()
.enumerate()
.find(|(_, b)| b.rows.len() == 1 && b.editor_text.chars().any(|c| ('\u{4e00}'..='\u{9fff}').contains(&c)))
.unwrap();
let source = doc.text_lines(page_index)[block.lines[0]].clone();
let info = doc.info.pages[page_index].clone();
let page = h.state().views[0].page_screen_rect(page_index).unwrap();
let scale = page.width() / info.width;
let v = info.user_to_view((source.rect[0] + 3.0) as f32, ((source.rect[1] + source.rect[3]) / 2.0) as f32);
let point = page.min + egui::vec2(v[0], v[1]) * scale;
h.hover_at(point);
h.run_steps(1);
h.drag_at(point);
h.run_steps(1);
h.drop_at(point);
h.run_steps(8);
assert_eq!(h.state().views[0].session.line_editor.as_ref().unwrap().block, index);
let draft = format!("{}{new}", block.editor_text);
h.key_press_modifiers(Modifiers::COMMAND, Key::A);
h.event(egui::Event::Text(draft.clone()));
h.run_steps(4);
assert_eq!(h.state().views[0].session.line_editor.as_ref().unwrap().text, draft);
let galley = h
.output()
.shapes
.iter()
.find_map(|s| match &s.shape {
egui::Shape::Text(t) if t.galley.text() == draft => Some(t.galley.clone()),
_ => None,
})
.expect("typed Chinese is in the actual inline editor");
assert!(
galley
.job
.sections
.iter()
.filter(|s| draft
.get(usize::from(s.byte_range.start)..usize::from(s.byte_range.end))
.is_some_and(|text| text.chars().any(|c| ('\u{4e00}'..='\u{9fff}').contains(&c))))
.all(|s| matches!(&s.format.font_id.family, egui::FontFamily::Name(n) if n.starts_with("pdf-source-")))
);
h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);
h.run_steps(5);
assert!(h.state().views[0].session.line_editor.is_none());
let edited = h.state().session.get(id).unwrap();
assert!(edited.text_lines(page_index).iter().any(|l| l.text.contains(&new) && l.base_font == "DroidSansFallback"));
let first = edited.text_lines(page_index).into_iter().find(|l| l.text.starts_with(source.text.trim())).unwrap();
assert!(first.baseline().iter().zip(source.baseline()).all(|(a, b)| (a - b).abs() < 0.01));
let current = pixels(edited.display.clone(), page_index);
let saved = h.state_mut().session.save_bytes(id).unwrap();
let mut reopen = Session::new();
let rid = reopen.open("saved.pdf", None, saved, None).unwrap();
assert_eq!(pixels(reopen.get(rid).unwrap().display.clone(), page_index), current);
// Repeat keyboard input and deletion without recreating the editor via state mutation.
h.hover_at(point);
h.run_steps(1);
h.drag_at(point);
h.run_steps(1);
h.drop_at(point);
h.run_steps(6);
assert!(h.state().views[0].session.line_editor.as_ref().unwrap().text.contains(&new));
h.key_press_modifiers(Modifiers::COMMAND, Key::A);
let shortened = draft.replace(&new, &new.chars().skip(1).collect::<String>());
h.event(egui::Event::Text(shortened.clone()));
h.run_steps(3);
h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);
h.run_steps(4);
let current = pixels(h.state().session.get(id).unwrap().display.clone(), page_index);
let saved = h.state_mut().session.save_bytes(id).unwrap();
let rid = reopen.open("deleted.pdf", None, saved, None).unwrap();
assert_eq!(pixels(reopen.get(rid).unwrap().display.clone(), page_index), current);
let content = |s: &str| s.chars().filter(|c| !c.is_whitespace()).collect::<String>();
assert_eq!(content(&reopen.get(rid).unwrap().text_blocks(page_index)[index].editor_text), content(&shortened));
h.state_mut().session.undo(id).unwrap();
h.state_mut().session.undo(id).unwrap();
h.state_mut().session.undo(id).unwrap();
h.run_steps(4);
}
assert_eq!(std::fs::read(path).unwrap(), original);
println!("verified Chinese keyboard insertion/deletion and save/reopen rendering on {pages} pages");
}
/// Moving text shows the four-way arrow wherever a drag would move it (#1520 follow-up): over a
/// closed paragraph, on the frame of an open one, and while dragging. Inside the open one the
/// pointer reads as text; on a closed one's edge, as the rewrap handle.
#[test]
fn moving_text_offers_the_four_way_arrow() {
let mut h = harness(2, |_| {});
// Keep the moved paragraph clear of the window's lower edge.
h.set_option("zoom", "75").unwrap();
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let block = h.state().session.get(id).unwrap().text_blocks(0)[0].clone();
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
let screen = |x: f64, y: f64| egui::pos2(r.left() + x as f32 * k, r.top() + (300.0 - y as f32) * k);
let centre = screen((block.rect[0] + block.rect[2]) / 2.0, (block.rect[1] + block.rect[3]) / 2.0);
let cursor_at = |h: &mut Harness<'static, PdfCraftApp>, at: egui::Pos2| {
h.hover_at(at);
h.run_steps(2);
h.output().platform_output.cursor_icon
};
// A closed paragraph: a drag moves it; its left edge rewraps it.
assert_eq!(cursor_at(&mut h, centre), egui::CursorIcon::Move);
assert_eq!(cursor_at(&mut h, screen(block.rect[0], (block.rect[1] + block.rect[3]) / 2.0)), egui::CursorIcon::ResizeHorizontal);
// While it is being dragged.
h.hover_at(centre);
h.run_steps(1);
h.drag_at(centre);
h.run_steps(1);
let mid = centre + egui::vec2(20.0 * k, 10.0 * k);
h.hover_at(mid);
h.run_steps(2);
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Move);
h.drop_at(mid);
h.run_steps(4);
let moved = h.state().session.get(id).unwrap().text_blocks(0)[0].clone();
assert!((moved.rect[0] - block.rect[0] - 20.0).abs() < 1.5, "{:?} → {:?}", block.rect, moved.rect);
// Open: text inside, the frame moves it.
let centre = screen((moved.rect[0] + moved.rect[2]) / 2.0, (moved.rect[1] + moved.rect[3]) / 2.0);
click_at(&mut h, centre);
assert!(h.state().views[0].session.line_editor.is_some());
assert_eq!(cursor_at(&mut h, centre), egui::CursorIcon::Text);
let frame = screen((moved.rect[0] + moved.rect[2]) / 2.0, moved.rect[3]) - egui::vec2(0.0, 4.0);
assert_eq!(cursor_at(&mut h, frame), egui::CursorIcon::Move);
}
/// Cropping a picture on the page (Acrobat's crop handles): in crop mode the
/// handles trim, Enter applies what is kept, Esc leaves the picture as it was.
#[test]
fn cropping_a_picture_with_its_handles() {
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 40, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
let before = h.state().session.get(id).unwrap().page_images(0)[0].visible_rect.unwrap();
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let c = r.center();
h.hover_at(c);
h.run_steps(1);
h.drag_at(c);
h.run_steps(1);
h.drop_at(c);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_some(), "selected");
// Crop mode (as the context menu's Crop Image starts it): drag the right-bottom handle a
// quarter of the picture inward, then Enter.
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop = Some(pdfcraft_ui_egui::CropMode::new(before));
h.run_steps(2);
let corner = r.right_bottom();
let to = corner - egui::vec2(r.width() / 4.0, r.height() / 4.0);
h.hover_at(corner);
h.run_steps(1);
h.drag_at(corner);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(corner + (to - corner) * (k as f32 / 4.0));
h.run_steps(1);
}
h.drop_at(to);
h.run_steps(2);
let keep = h.state().views[0].session.image_selection.as_ref().unwrap().crop.as_ref().unwrap().keep;
assert!(keep[2] < before[2] - 1.0 && keep[1] > before[1] + 1.0, "trimmed on the right and the bottom: {keep:?} of {before:?}");
h.key_press(egui::Key::Enter);
h.run_steps(4);
let doc = h.state().session.get(id).unwrap();
assert_eq!(doc.can_undo(), Some("Crop image"));
let im = &doc.page_images(0)[0];
assert_eq!(im.rect, [0.0, 0.0, 80.0, 40.0], "the placement is not scaled");
let v = im.visible_rect.unwrap();
assert!((v[2] - 60.0).abs() < 1.5 && (v[1] - 10.0).abs() < 1.5, "kept {v:?}");
assert!(h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.crop.is_none()), "crop mode ends");
// Esc leaves it.
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop = Some(pdfcraft_ui_egui::CropMode::new([10.0, 10.0, 20.0, 20.0]));
h.run_steps(2);
h.key_press(egui::Key::Escape);
h.run_steps(3);
let doc = h.state().session.get(id).unwrap();
assert_eq!(doc.page_images(0)[0].visible_rect.unwrap(), v);
assert!(h.state().views[0].session.image_selection.as_ref().is_none_or(|s| s.crop.is_none()));
}
/// Both pages of [`clipped_artwork_with_labels`] at 100% in Edit text, the picture on page 0
/// selected (and being cropped, with `crop`).
fn picture_pages(crop: bool) -> Harness<'static, PdfCraftApp> {
picture_pages_of(clipped_artwork_with_labels(), crop)
}
/// [`picture_pages`], of `pdf`.
fn picture_pages_of(pdf: Vec<u8>, crop: bool) -> Harness<'static, PdfCraftApp> {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "clipped.pdf", None, pdf).unwrap();
app.set_option(&egui::Context::default(), "left", "closed").unwrap();
app.set_option(&egui::Context::default(), "zoom", "100").unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(3);
let id = h.state().views[0].id;
let keep = h.state().session.get(id).unwrap().page_images(0)[0].visible_rect.unwrap();
let mut selection = pdfcraft_ui_egui::ImageSelection::at(0, 0);
selection.crop = crop.then(|| pdfcraft_ui_egui::CropMode::new(keep));
h.state_mut().views[0].session.image_selection = Some(selection);
h.run_steps(3);
h
}
/// The screen point at PDF (x, y) of `page` of [`picture_pages`].
fn page_at(h: &Harness<'static, PdfCraftApp>, page: usize, x: f32, y: f32) -> egui::Pos2 {
let r = h.state().views[0].page_screen_rect(page).expect("both pages on screen");
egui::pos2(r.left() + x / 200.0 * r.width(), r.top() + (300.0 - y) / 300.0 * r.height())
}
fn cursor_over(h: &mut Harness<'static, PdfCraftApp>, p: egui::Pos2) -> egui::CursorIcon {
h.hover_at(p);
h.run_steps(2);
h.output().platform_output.cursor_icon
}
/// Crop mode holds every press on its page, and its cursor stays there: the grips show theirs and
/// the box kept the hand that drags it, and on the next page the pointer shows what a press there
/// takes (a paragraph's move arrow). Crop mode's cursor used to win on every page.
#[test]
fn crop_modes_cursor_stays_on_its_page() {
let mut h = picture_pages(true);
// The visible picture is x 30..160, y 170..250.
for (page, x, y, cursor, what) in [
(0, 160.0, 170.0, egui::CursorIcon::ResizeNwSe, "a corner grip"),
(0, 95.0, 210.0, egui::CursorIcon::Grab, "the box kept"),
(0, 65.0, 134.0, egui::CursorIcon::Default, "crop mode holds its page's paragraphs"),
(1, 65.0, 134.0, egui::CursorIcon::Move, "the next page's paragraph"),
] {
let p = page_at(&h, page, x, y);
assert_eq!(cursor_over(&mut h, p), cursor, "{what}");
}
}
/// A press on a crop grip a little outside the picture drags that grip, and Escape in the middle
/// of the drag cancels it: the box goes back and crop mode stays. Escape once the button is up
/// leaves crop mode.
#[test]
fn escape_during_a_crop_drag_keeps_crop_mode() {
let mut h = picture_pages(true);
let before = h.state().views[0].session.image_selection.as_ref().unwrap().crop.as_ref().unwrap().keep;
let preview_before = h.get_by_label("Crop corner, bottom right").rect().center();
let grip = page_at(&h, 0, 160.0, 170.0) + egui::vec2(5.0, 5.0);
h.hover_at(grip);
h.run_steps(1);
h.drag_at(grip);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(grip - egui::vec2(10.0, 10.0) * k as f32);
h.run_steps(1);
}
let keep = |h: &Harness<'static, PdfCraftApp>| h.state().views[0].session.image_selection.as_ref().and_then(|s| s.crop.as_ref()).map(|c| c.keep);
let preview = h.get_by_label("Crop corner, bottom right").rect().center();
assert!(
preview.x < preview_before.x - 5.0 && preview.y < preview_before.y - 5.0,
"the grip outside the picture changes both edges of the preview"
);
assert_eq!(keep(&h), Some(before), "the crop is unchanged until the drag commits");
h.key_press(Key::Escape);
h.run_steps(2);
assert_eq!(keep(&h), Some(before), "Escape cancels the drag, and crop mode stays");
assert_eq!(h.get_by_label("Crop corner, bottom right").rect().center(), preview_before, "Escape restores the preview");
h.drop_at(grip - egui::vec2(40.0, 40.0));
h.run_steps(2);
assert_eq!(keep(&h), Some(before), "the cancelled drag's release crops nothing");
h.key_press(Key::Escape);
h.run_steps(2);
assert_eq!(keep(&h), None, "Escape leaves crop mode");
assert!(!dirty(&h));
}
/// With Edit text, a right-click on text lying over a picture is the picture's, which is selected
/// for its menu: paragraphs have no menu of their own (the user's ruling).
#[test]
fn a_right_click_on_text_over_a_picture_selects_the_picture() {
let mut h = picture_pages(false);
h.state_mut().views[0].session.image_selection = None;
h.run_steps(2);
// "Overlay" lies on the picture, at x 40, y 220.
let overlay = page_at(&h, 0, 50.0, 224.0);
right_click_at(&mut h, overlay);
let selected = h.state().views[0].session.image_selection.as_ref().map(|s| (s.page, s.index));
assert_eq!(selected, Some((0, 0)), "the picture under the text is selected for its menu");
}
/// With Edit text, a right-click on added text over a picture is the picture's, which is selected
/// for its menu: added text has no menu of its own.
#[test]
fn a_right_click_on_added_text_over_a_picture_selects_the_picture() {
let mut h = picture_pages(false);
h.state_mut().views[0].session.image_selection = None;
let ctx_h = h.ctx.clone();
assert!(h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::AddText {
page: 0,
text: pdfcraft_engine::AddedText { rect: [60.0, 190.0, 150.0, 205.0], text: "Added".into(), ..Default::default() },
}
));
h.run_steps(3);
let on_it = page_at(&h, 0, 100.0, 198.0);
right_click_at(&mut h, on_it);
let selected = h.state().views[0].session.image_selection.as_ref().map(|s| (s.page, s.index));
assert_eq!(selected, Some((0, 0)), "the picture under the added text is selected for its menu");
assert!(h.query_by_label("Flip horizontal").is_some(), "its menu opens");
}
/// With Edit text, a right-click on the frame of a paragraph open for typing over a picture is the
/// picture's too.
#[test]
fn a_right_click_on_an_open_paragraphs_frame_over_a_picture_selects_the_picture() {
let mut h = picture_pages(false);
h.state_mut().views[0].session.image_selection = None;
h.run_steps(2);
// "Overlay" lies on the picture, at x 40, y 220.
let overlay = page_at(&h, 0, 50.0, 224.0);
slipping_click(&mut h, overlay, overlay);
assert!(h.state().views[0].session.line_editor.is_some(), "the paragraph is open for typing");
let id = h.state().views[0].id;
let blocks = h.state().session.get(id).unwrap().text_blocks(0);
let block =
blocks.iter().find(|b| b.rect[0] < 50.0 && 50.0 < b.rect[2] && b.rect[1] < 224.0 && 224.0 < b.rect[3]).expect("the paragraph opened").clone();
// Its frame runs just outside its box.
let frame = page_at(&h, 0, block.rect[0] as f32, ((block.rect[1] + block.rect[3]) / 2.0) as f32) - egui::vec2(3.0, 0.0);
right_click_at(&mut h, frame);
let selected = h.state().views[0].session.image_selection.as_ref().map(|s| (s.page, s.index));
assert_eq!(selected, Some((0, 0)), "the picture under the frame is selected for its menu");
assert!(h.query_by_label("Flip horizontal").is_some(), "its menu opens");
}
/// A drag from the selected picture moves it, though another picture lies over it there; a click
/// there selects the one on top.
#[test]
fn a_drag_from_the_selected_picture_under_another_moves_the_selected_one() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1400.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "two.pdf", None, two_image_fixture()).unwrap();
app
}
});
h.run_steps(4);
// The second picture lies over the right half of the first.
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::EditPageImage { page: 0, index: 1, change: pdfcraft_engine::ImageEdit::Move([110.0, 100.0, 130.0, 120.0]) },
);
assert!(h.execute("edit.edit_text"));
h.run_steps(4);
h.state_mut().views[0].session.image_selection = Some(pdfcraft_ui_egui::ImageSelection::at(0, 0));
h.run_steps(2);
let id = h.state().views[0].id;
let rects = |h: &Harness<'static, PdfCraftApp>| {
let images = h.state().session.get(id).unwrap().page_images(0);
(images[0].rect, images[1].rect)
};
let before = rects(&h);
let xf = h.state().views[0].page_xform(0).unwrap();
let doc = h.state().session.get(id).unwrap();
let images = doc.page_images(0);
let frame = |i: usize| pdfcraft_ui_egui::canvas::image_screen_rect(&images[i], &xf, &doc.info, 0);
let overlap = frame(0).intersect(frame(1)).center();
h.hover_at(overlap);
h.run_steps(1);
h.drag_at(overlap);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(overlap + egui::vec2(8.0 * k as f32, 0.0));
h.run_steps(1);
}
h.drop_at(overlap + egui::vec2(32.0, 0.0));
h.run_steps(3);
let after = rects(&h);
assert_ne!(after.0, before.0, "the selected picture moves");
assert_eq!(after.1, before.1, "the one over it stays");
let selected = h.state().views[0].session.image_selection.as_ref().map(|s| s.index);
assert_eq!(selected, Some(0), "and stays selected");
// After the move the two still overlap, a little further along.
let xf = h.state().views[0].page_xform(0).unwrap();
let doc = h.state().session.get(id).unwrap();
let images = doc.page_images(0);
let frame = |i: usize| pdfcraft_ui_egui::canvas::image_screen_rect(&images[i], &xf, &doc.info, 0);
let overlap = frame(0).intersect(frame(1)).center();
slipping_click(&mut h, overlap, overlap);
let selected = h.state().views[0].session.image_selection.as_ref().map(|s| s.index);
assert_eq!(selected, Some(1), "a click selects the one on top");
}
/// A move too short to be a drag of the page still resizes the selected picture from its grip, as
/// the grip takes its drag from the press.
#[test]
fn a_short_drag_from_a_picture_grip_resizes_it() {
let mut h = picture_pages(false);
let id = h.state().views[0].id;
let before = h.state().session.get(id).unwrap().page_images(0)[0].matrix;
// The picture shows at x 30..160, y 170..250: its lower right grip, moved 3 px.
let grip = page_at(&h, 0, 160.0, 170.0);
h.hover_at(grip);
h.run_steps(1);
h.drag_at(grip);
h.run_steps(1);
h.hover_at(grip + egui::vec2(3.0, 0.0));
h.run_steps(1);
h.drop_at(grip + egui::vec2(3.0, 0.0));
h.run_steps(3);
assert_ne!(h.state().session.get(id).unwrap().page_images(0)[0].matrix, before, "the picture is resized");
}
/// With Add text holding text that can't be drawn, a drag from a paragraph moves nothing: the draft
/// stays where it is being typed (#125), as a click there leaves it.
#[test]
fn add_text_moves_no_paragraph_while_it_holds_a_draft_it_cant_draw() {
if crate::support::skip_without_glyphs("add_text_moves_no_paragraph_while_it_holds_a_draft_it_cant_draw", "en", "Hello 世界") {
return;
}
let mut h = edit_page(pdfcraft_ui_egui::QuickTool::AddText);
assert!(h.execute("edit.text"));
h.run_steps(3);
let id = h.state().views[0].id;
let block = h.state().session.get(id).unwrap().text_blocks(0)[0].clone();
let blank = point_on(&h, block.rect[0], block.rect[3] + 12.0);
slipping_click(&mut h, blank, blank);
h.event(egui::Event::Text("Hello 世界".into()));
h.run_steps(2);
assert_eq!(h.state().views[0].session.content.draft.as_ref().map(|d| d.text.as_str()), Some("Hello 世界"), "the draft holds the text");
let rect = h.state().views[0].session.content.draft.as_ref().map(|d| d.rect).unwrap();
let on_text = point_on(&h, (block.rect[0] + block.rect[2]) / 2.0, (block.rect[1] + block.rect[3]) / 2.0);
h.hover_at(on_text);
h.run_steps(1);
h.drag_at(on_text);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(on_text + egui::vec2(10.0 * k as f32, 0.0));
h.run_steps(1);
}
h.drop_at(on_text + egui::vec2(40.0, 0.0));
h.run_steps(3);
assert_eq!(h.state().session.get(id).unwrap().text_blocks(0)[0].rect, block.rect, "the paragraph stays");
let draft = h.state().views[0].session.content.draft.as_ref().map(|d| (d.text.clone(), d.rect));
assert_eq!(draft, Some(("Hello 世界".to_string(), rect)), "and the draft stays where it is typed");
}
/// With Edit text, a right-click on a comment over a picture opens the comment's menu, and the
/// picture is not selected for its own.
#[test]
fn a_right_click_on_a_comment_over_a_picture_opens_its_menu() {
let mut h = picture_pages(false);
h.state_mut().views[0].session.image_selection = None;
let shape = pdfcraft_engine::Shape::Rectangle { rect: [60.0, 190.0, 150.0, 230.0] };
let ctx_h = h.ctx.clone();
assert!(h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::AddAnnotation(pdfcraft_engine::NewAnnotation {
page: 0,
style: pdfcraft_engine::Style::default_for(&shape),
shape,
contents: "Reviewed".into(),
author: "Tester".into(),
})
));
h.state_mut().views[0].session.comments.clear_selection();
h.run_steps(3);
let on_it = page_at(&h, 0, 100.0, 210.0);
right_click_at(&mut h, on_it);
assert!(h.state().views[0].session.comments.primary().is_some(), "the comment is selected");
assert!(h.state().views[0].session.image_selection.is_none(), "the picture is not");
assert!(h.query_by_label("Properties…").is_some(), "the comment's menu opens");
}
/// In crop mode, a right-click on the next page goes to what lies there, though a clipped artwork's
/// whole frame reaches over it: crop mode ends and the page's menu opens.
#[test]
fn a_right_click_on_the_next_page_leaves_crop_mode() {
let mut h = picture_pages(true);
let on_two = page_at(&h, 1, 100.0, 40.0);
right_click_at(&mut h, on_two);
assert!(h.state().views[0].session.image_selection.is_none(), "crop mode ends");
assert!(h.query_by_label("Marquee Zoom").is_some(), "the page's menu opens");
}
/// With Add text, a click on another paragraph keeps both what was typed in the open paragraph and
/// the text being added: neither edit takes the other's place.
#[test]
fn add_text_keeps_a_changed_paragraph_and_a_draft_when_it_moves_on() {
let mut h = picture_pages(false);
h.state_mut().views[0].session.image_selection = None;
assert!(h.execute("edit.text"));
h.run_steps(3);
let id = h.state().views[0].id;
let blocks = h.state().session.get(id).unwrap().text_blocks(0);
assert!(blocks.len() >= 2, "two paragraphs: {blocks:?}");
let centre = |r: [f64; 4]| ((r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0);
let (ax, ay) = centre(blocks[0].rect);
let (bx, by) = centre(blocks[1].rect);
let a = page_at(&h, 0, ax as f32, ay as f32);
let b = page_at(&h, 0, bx as f32, by as f32);
slipping_click(&mut h, a, a);
assert!(h.state().views[0].session.line_editor.is_some(), "the first paragraph is open");
h.event(egui::Event::Text("X".into()));
h.run_steps(3);
// A click on the page's empty part, below the picture, starts a box.
let blank = page_at(&h, 0, 180.0, 20.0);
slipping_click(&mut h, blank, blank);
assert!(h.state().views[0].session.line_editor.is_none(), "that click applied the paragraph");
h.event(egui::Event::Text("Added words".into()));
h.run_steps(3);
slipping_click(&mut h, b, b);
h.run_steps(4);
let doc = h.state().session.get(id).unwrap();
let texts: Vec<String> = doc.text_blocks(0).iter().map(|b| b.text.clone()).collect();
let added: Vec<String> = doc
.added()
.iter()
.filter_map(|a| match &a.content {
pdfcraft_engine::AddedContent::Text(t) => Some(t.text.clone()),
_ => None,
})
.collect();
assert!(added.iter().any(|t| t == "Added words"), "the added text is kept: {added:?}");
assert!(texts.iter().any(|t| t.contains('X')), "the paragraph's typing is kept: {texts:?}");
}
/// In the page grid, Delete deletes the page chosen there. The picture selected before the grid
/// opened is not on screen, and stays.
#[test]
fn delete_in_the_page_grid_deletes_the_page_and_not_the_hidden_picture() {
let mut h = picture_pages(false);
let id = h.state().views[0].id;
let pictures = h.state().session.get(id).unwrap().page_images(0).len();
let pages = h.state().session.get(id).unwrap().info.pages.len();
assert!(h.execute("page.organize"));
h.run_steps(4);
h.get_by_label("Page 2").click();
h.run_steps(2);
h.key_press(egui::Key::Delete);
h.run_steps(4);
let doc = h.state().session.get(id).unwrap();
assert_eq!(doc.info.pages.len(), pages - 1, "the page chosen in the grid is deleted");
assert_eq!(doc.page_images(0).len(), pictures, "the picture stays");
}
/// With the picture's page not on screen, its keys do nothing: Delete removes no picture the reader
/// can't see, and Enter applies no crop.
#[test]
fn a_selected_pictures_keys_wait_for_its_page_to_be_on_screen() {
use pdfcraft_ui_egui::canvas::PageDisplay as PageLayout;
for (crop, key) in [(false, egui::Key::Delete), (true, egui::Key::Enter)] {
let mut h = picture_pages(crop);
let id = h.state().views[0].id;
let before = h.state().session.get(id).unwrap().page_images(0);
if crop {
let keep = before[0].visible_rect.unwrap();
let half = [keep[0], keep[1], (keep[0] + keep[2]) / 2.0, keep[3]];
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop.as_mut().unwrap().keep = half;
}
h.state_mut().views[0].view.display = PageLayout::Single;
h.state_mut().views[0].view.continuous = false;
h.state_mut().views[0].go_to_page(1);
h.run_steps(4);
let viewport = h.state().views[0].viewport_rect();
assert!(h.state().views[0].page_screen_rect(0).is_none_or(|r| !r.intersects(viewport)), "page 1 is off the screen");
h.key_press(key);
h.run_steps(4);
let after = h.state().session.get(id).unwrap().page_images(0);
assert_eq!(after.len(), before.len(), "the picture stays");
assert_eq!(after[0].visible_rect, before[0].visible_rect, "and is not cropped");
}
// A page just off the screen, in a continuous view: it is still run for what lies near it.
let mut h = picture_pages(false);
let id = h.state().views[0].id;
let pictures = h.state().session.get(id).unwrap().page_images(0).len();
h.set_size(egui::vec2(1400.0, 400.0));
h.state_mut().views[0].go_to_page(1);
h.run_steps(6);
// Going to a page leaves a margin above it: a little further, past page 1's foot.
let centre = h.state().views[0].viewport_rect().center();
h.hover_at(centre);
h.run_steps(1);
h.event(egui::Event::MouseWheel {
unit: egui::MouseWheelUnit::Point,
delta: egui::vec2(0.0, -60.0),
phase: egui::TouchPhase::Move,
modifiers: egui::Modifiers::NONE,
source: egui::MouseWheelSource::Trackpad,
});
h.run_steps(6);
assert!(h.state().views[0].continuous(), "a continuous view");
let viewport = h.state().views[0].viewport_rect();
assert!(h.state().views[0].page_screen_rect(0).is_none_or(|r| !r.intersects(viewport)), "page 1 is off the screen");
h.key_press(egui::Key::Delete);
h.run_steps(4);
assert_eq!(h.state().session.get(id).unwrap().page_images(0).len(), pictures, "the picture stays");
}
/// Delete waits for the button to come up: the press held would then name another item, whose drag or
/// click would follow.
#[test]
fn delete_waits_for_the_button_to_come_up() {
let hold_and_delete = |h: &mut Harness<'static, PdfCraftApp>, on: egui::Pos2| {
h.hover_at(on);
h.run_steps(1);
h.drag_at(on);
h.run_steps(1);
h.key_press(egui::Key::Delete);
h.run_steps(2);
h.drop_at(on);
h.run_steps(3);
};
let mut h = picture_pages(false);
let id = h.state().views[0].id;
let pictures = h.state().session.get(id).unwrap().page_images(0).len();
let on_picture = page_at(&h, 0, 100.0, 180.0);
hold_and_delete(&mut h, on_picture);
assert_eq!(h.state().session.get(id).unwrap().page_images(0).len(), pictures, "the picture stays");
let mut h = edit_page(pdfcraft_ui_egui::QuickTool::Select);
let ctx_h = h.ctx.clone();
assert!(h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::AddText {
page: 0,
text: pdfcraft_engine::AddedText { rect: [40.0, 250.0, 140.0, 265.0], text: "Added".into(), ..Default::default() },
}
));
assert!(h.execute("edit.text"));
h.run_steps(3);
h.state_mut().quick_tool = pdfcraft_ui_egui::QuickTool::Select;
h.run_steps(3);
let on_added = point_on(&h, 90.0, 258.0);
slipping_click(&mut h, on_added, on_added);
assert_eq!(h.state().views[0].session.content.selected, Some((0, 0)), "the added text is selected");
hold_and_delete(&mut h, on_added);
let id = h.state().views[0].id;
assert_eq!(h.state().session.get(id).unwrap().added().len(), 1, "the added text stays");
}
/// A drag the page it is over first sees lets the picked picture go, whichever page it began on.
#[test]
fn a_drag_first_seen_on_the_next_page_lets_the_picked_picture_go() {
let mut h = picture_pages(false);
h.state_mut().views[0].session.image_selection = None;
h.state_mut().quick_tool = pdfcraft_ui_egui::QuickTool::Select;
h.run_steps(3);
let on_picture = page_at(&h, 0, 100.0, 180.0);
slipping_click(&mut h, on_picture, on_picture);
assert!(h.state().views[0].session.picked_image.is_some(), "the picture is picked");
let press = page_at(&h, 0, 180.0, 20.0);
let release = page_at(&h, 1, 180.0, 280.0);
h.hover_at(press);
h.run_steps(1);
h.drag_at(press);
h.run_steps(1);
h.hover_at(release);
h.run_steps(1);
h.drop_at(release);
h.run_steps(4);
assert!(h.state().views[0].session.picked_image.is_none(), "the drag lets it go");
}
/// With the pointer on the Pages panel, Delete is the panel's: a page goes, though a picture is
/// selected on the pages.
#[test]
fn delete_with_the_pointer_on_the_pages_panel_deletes_a_page() {
let mut h = picture_pages(false);
h.set_option("panel", "pages").unwrap();
h.run_steps(4);
let id = h.state().views[0].id;
let pages = h.state().session.get(id).unwrap().info.pages.len();
assert!(h.state().views[0].session.image_selection.is_some(), "the picture is selected");
assert!(h.state().views[0].page_screen_rect(0).is_some(), "on a page on screen");
let viewport = h.state().views[0].viewport_rect();
assert!(viewport.right() < 1300.0, "the panel lies right of the pages: {viewport:?}");
h.hover_at(egui::pos2(viewport.right() + 60.0, viewport.center().y));
h.run_steps(2);
h.key_press(egui::Key::Delete);
h.run_steps(4);
assert_eq!(h.state().session.get(id).unwrap().info.pages.len(), pages - 1, "a page goes");
}
/// In crop mode, a right-click on another picture selects it for its menu, as without crop mode. It
/// went to crop mode's whole-page hit, which unselected every picture.
#[test]
fn in_crop_mode_a_right_click_on_another_picture_selects_it() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1500.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "two.pdf", None, two_image_fixture()).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(4);
let id = h.state().views[0].id;
let rect = h.state().session.get(id).unwrap().page_images(0)[0].rect;
let mut selection = pdfcraft_ui_egui::ImageSelection::at(0, 0);
selection.crop = Some(pdfcraft_ui_egui::CropMode::new(rect));
h.state_mut().views[0].session.image_selection = Some(selection);
h.run_steps(2);
let xf = h.state().views[0].page_xform(0).unwrap();
let doc = h.state().session.get(id).unwrap();
let other = pdfcraft_ui_egui::canvas::image_screen_rect(&doc.page_images(0)[1], &xf, &doc.info, 0);
crop_secondary_click(&mut h, other.center());
let selection = h.state().views[0].session.image_selection.clone();
assert!(selection.as_ref().is_some_and(|s| s.index == 1 && s.crop.is_none()), "the other picture is selected: {selection:?}");
assert!(h.query_by_label("Flip horizontal").is_some(), "its menu opens");
}
/// A drag from a hole in the selected picture (drawn through a clip with a hole in it) moves the
/// picture: its frame is its own.
#[test]
fn a_drag_from_a_hole_in_the_selected_picture_moves_it() {
// The picture shows at x 30..160, y 170..250, around a hole at x 70..120, y 190..230.
let mut h = picture_pages_of(artwork_drawn(b"q 30 170 130 80 re 70 190 50 40 re W* n 800 0 0 900 -300 -400 cm /Im0 Do Q"), false);
let id = h.state().views[0].id;
let before = h.state().session.get(id).unwrap().page_images(0)[0].rect;
let hole = page_at(&h, 0, 95.0, 210.0);
h.hover_at(hole);
h.run_steps(1);
h.drag_at(hole);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(hole + egui::vec2(5.0 * k as f32, 0.0));
h.run_steps(1);
}
h.drop_at(hole + egui::vec2(20.0, 0.0));
h.run_steps(4);
let after = h.state().session.get(id).unwrap().page_images(0)[0].rect;
assert!(after[0] - before[0] > 5.0, "the picture moved right: {before:?} → {after:?}");
}
/// A right-click on a crop grip past the page's edge, where those of a picture filling its page lie
/// half, is the crop's, as on the page: crop mode stays and no menu opens (#5835).
#[test]
fn a_right_click_on_a_crop_grip_past_the_page_edge_keeps_crop_mode() {
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 40, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let keep = h.state().session.get(id).unwrap().page_images(0)[0].visible_rect.unwrap();
let mut selection = pdfcraft_ui_egui::ImageSelection::at(0, 0);
selection.crop = Some(pdfcraft_ui_egui::CropMode::new(keep));
h.state_mut().views[0].session.image_selection = Some(selection);
h.run_steps(3);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
crop_secondary_click(&mut h, r.right_bottom() + egui::vec2(4.0, 4.0));
assert!(h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.crop.is_some()), "crop mode stays");
for label in ["Flip horizontal", "Rotate view clockwise"] {
assert!(!menu_row(&h, label), "no menu: {label}");
}
}
/// Crop mode's grips reach past the page's edge, where those of a picture filling its page lie
/// half: a drag from just outside the page's corner trims the picture.
#[test]
fn a_crop_grip_past_the_page_edge_drags() {
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 40, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let before = h.state().session.get(id).unwrap().page_images(0)[0].visible_rect.unwrap();
let mut selection = pdfcraft_ui_egui::ImageSelection::at(0, 0);
selection.crop = Some(pdfcraft_ui_egui::CropMode::new(before));
h.state_mut().views[0].session.image_selection = Some(selection);
h.run_steps(3);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let outside = r.right_bottom() + egui::vec2(4.0, 4.0);
let to = r.right_bottom() - egui::vec2(r.width() / 4.0, r.height() / 4.0);
h.hover_at(outside);
h.run_steps(1);
h.drag_at(outside);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(outside + (to - outside) * (k as f32 / 4.0));
h.run_steps(1);
}
h.drop_at(to);
h.run_steps(2);
let keep = h.state().views[0].session.image_selection.as_ref().and_then(|s| s.crop.as_ref()).map(|c| c.keep);
assert!(keep.is_some_and(|k| k[2] < before[2] - 1.0 && k[1] > before[1] + 1.0), "trimmed from the corner: {keep:?} of {before:?}");
}
/// Delete removes the selected picture wherever the pointer is, on a paragraph too: the key is the
/// app's, not the pointer's.
#[test]
fn delete_removes_the_selected_picture_with_the_pointer_on_a_paragraph() {
let mut h = picture_pages(false);
let caption = page_at(&h, 0, 65.0, 134.0);
assert_eq!(cursor_over(&mut h, caption), egui::CursorIcon::Move, "on the caption's paragraph");
h.key_press(Key::Delete);
h.run_steps(3);
let id = h.state().views[0].id;
assert!(h.state().session.get(id).unwrap().page_images(0).is_empty(), "the picture is deleted");
assert!(h.state().views[0].session.image_selection.is_none());
}
/// Delete on a selected added picture deletes it alone: the added text placed after it stays. The
/// key's second reader, added content's own Delete, took the text that the picture's index then
/// named.
#[test]
fn delete_on_a_selected_added_picture_deletes_it_alone() {
let mut h = edit_page(pdfcraft_ui_egui::QuickTool::EditText);
let mut png = Vec::new();
image::RgbImage::from_pixel(30, 20, image::Rgb([20, 90, 200])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let ctx_h = h.ctx.clone();
h.state_mut().add_image(&ctx_h, "logo.png".into(), png);
h.run_steps(3);
let ctx_h = h.ctx.clone();
assert!(h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::AddText {
page: 0,
text: pdfcraft_engine::AddedText { rect: [20.0, 20.0, 120.0, 35.0], text: "Caption".into(), ..Default::default() },
}
));
h.run_steps(3);
let id = h.state().views[0].id;
let images = h.state().session.get(id).unwrap().page_images(0).len();
assert_eq!(images, 1, "the added picture is the page's picture");
h.state_mut().views[0].session.image_selection = Some(pdfcraft_ui_egui::ImageSelection::at(0, 0));
h.run_steps(3);
h.key_press(Key::Delete);
h.run_steps(4);
let doc = h.state().session.get(id).unwrap();
assert!(doc.page_images(0).is_empty(), "the picture is deleted");
let texts: Vec<&str> = doc
.added()
.iter()
.filter_map(|a| if let pdfcraft_engine::AddedContent::Text(t) = &a.content { Some(t.text.as_str()) } else { None })
.collect();
assert_eq!(texts, ["Caption"], "and the text placed after it stays");
}
/// A click that slips a few pixels between its press and its release, within egui's click distance.
fn slipping_click(h: &mut Harness<'static, PdfCraftApp>, press: egui::Pos2, release: egui::Pos2) {
h.hover_at(press);
h.run_steps(1);
h.drag_at(press);
h.run_steps(1);
h.drop_at(release);
h.run_steps(4);
}
/// [`harness`]'s page 1 in Edit text (`tool`), and the screen point at PDF (x, y) on it.
fn edit_page(tool: pdfcraft_ui_egui::QuickTool) -> Harness<'static, PdfCraftApp> {
let mut h = harness(2, |_| {});
h.state_mut().quick_tool = tool;
h.run_steps(3);
h
}
fn point_on(h: &Harness<'static, PdfCraftApp>, x: f64, y: f64) -> egui::Pos2 {
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
egui::pos2(r.left() + x as f32 * k, r.top() + (300.0 - y as f32) * k)
}
/// A click on a paragraph opens it where it was pressed, though the button came up just below its
/// box: an owner acts where its press was. It opened nothing.
#[test]
fn a_click_on_a_paragraph_released_just_below_it_opens_it() {
let mut h = edit_page(pdfcraft_ui_egui::QuickTool::EditText);
let id = h.state().views[0].id;
let block = h.state().session.get(id).unwrap().text_blocks(0)[0].clone();
// The box reaches 2 px below the text: pressed 1 px inside it, released 3 px below it.
let bottom = point_on(&h, (block.rect[0] + block.rect[2]) / 2.0, block.rect[1]) + egui::vec2(0.0, 2.0);
slipping_click(&mut h, bottom - egui::vec2(0.0, 1.0), bottom + egui::vec2(0.0, 3.0));
assert!(h.state().views[0].session.line_editor.is_some(), "the paragraph pressed is open for typing");
}
/// With the Select tool and the Edit panel open, a click on added text selects it where it was
/// pressed, though the button came up just beside it. It left nothing selected.
#[test]
fn a_click_on_added_text_released_just_beside_it_selects_it() {
let mut h = edit_page(pdfcraft_ui_egui::QuickTool::Select);
let ctx_h = h.ctx.clone();
assert!(h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::AddText {
page: 0,
text: pdfcraft_engine::AddedText { rect: [40.0, 250.0, 140.0, 265.0], text: "Added".into(), ..Default::default() },
}
));
assert!(h.execute("edit.text"));
h.run_steps(3);
h.state_mut().quick_tool = pdfcraft_ui_egui::QuickTool::Select;
h.run_steps(3);
// The item takes presses 2 px past its box: pressed 1 px inside that, released 3 px past it.
let edge = point_on(&h, 140.0, 258.0) + egui::vec2(2.0, 0.0);
slipping_click(&mut h, edge - egui::vec2(1.0, 0.0), edge + egui::vec2(3.0, 0.0));
assert_eq!(h.state().views[0].session.content.selected, Some((0, 0)), "the added text pressed is selected");
}
/// With Add text, a click pressed beside a paragraph and released inside its box starts a new box
/// where it was pressed. It finished the text being typed and placed nothing.
#[test]
fn an_add_text_click_pressed_beside_a_paragraph_starts_a_box_there() {
let mut h = edit_page(pdfcraft_ui_egui::QuickTool::AddText);
let id = h.state().views[0].id;
let block = h.state().session.get(id).unwrap().text_blocks(0)[0].clone();
// The box reaches 2 px past the text: pressed 2 px left of it, released 2 px inside it.
let left = point_on(&h, block.rect[0], (block.rect[1] + block.rect[3]) / 2.0) - egui::vec2(2.0, 0.0);
slipping_click(&mut h, left - egui::vec2(2.0, 0.0), left + egui::vec2(2.0, 0.0));
let draft = h.state().views[0].session.content.draft.as_ref().map(|d| d.rect);
let x = draft.map(|r| r[0]);
assert!(x.is_some_and(|x| x < block.rect[0]), "a new box at the press, left of the paragraph: {x:?} of {:?}", block.rect);
assert!(h.state().views[0].session.line_editor.is_none(), "the paragraph stays closed");
}
/// The rotate knob of a picture that fills its page lies past the page's edge, and a drag there
/// still turns the picture: the press is the page's whose knob it is.
#[test]
fn dragging_the_rotate_knob_past_the_page_edge_turns_the_picture() {
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 40, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
h.state_mut().views[0].session.image_selection = Some(pdfcraft_ui_egui::ImageSelection::at(0, 0));
h.run_steps(3);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
// The framework names and positions the knob for the current theme.
let knob = h.get_by_label("Rotate").rect().center();
assert!(!r.contains(knob));
assert_eq!(cursor_over(&mut h, knob), egui::CursorIcon::Grab);
let to = egui::pos2(r.right() + 60.0, r.center().y);
h.hover_at(knob);
h.run_steps(1);
h.drag_at(knob);
h.run_steps(1);
for k in 1..=4 {
h.hover_at(knob + (to - knob) * (k as f32 / 4.0));
h.run_steps(1);
}
h.drop_at(to);
h.run_steps(4);
let id = h.state().views[0].id;
assert_eq!(h.state().session.get(id).unwrap().can_undo(), Some("Rotate image"));
}
/// Organize pages has Acrobat's panel whenever the grid shows, however it was opened, and the
/// panel's Close ends Organize: the grid closes and All tools comes back.
#[test]
fn organize_panel_comes_with_the_grid_and_closes_it() {
let mut h = organize(3);
h.get_by_label("Insert");
h.get_by_label("Bates numbering");
h.get_by_label("Page properties");
h.get_by_label("Close panel").click();
h.run_steps(3);
assert!(!h.state().views[0].organize, "Close ends Organize");
assert_eq!(h.state().left, pdfcraft_ui_egui::LeftPanel::AllTools);
assert!(h.query_by_label("Page properties").is_none(), "the panel is gone");
}
/// Organize pages ▸ Page properties ▸ Tab Order sets the tab order of the selected pages only,
/// as one undo step, and opens on the order the pages have.
#[test]
fn page_properties_sets_the_tab_order_of_the_selected_pages() {
use pdfcraft_engine::TabOrder::{Annotations, Column, Row};
let tabs = |h: &Harness<'static, PdfCraftApp>| h.state().session.get(h.state().views[0].id).unwrap().tab_orders();
// The undo and redo labels: a new step replaces the redo step an undo left.
let undo = |h: &Harness<'static, PdfCraftApp>| {
let doc = h.state().session.get(h.state().views[0].id).unwrap();
(doc.can_undo().map(str::to_owned), doc.can_redo().map(str::to_owned))
};
let mut h = organize(3);
h.get_by_label("Page 1").click();
h.run_steps(2);
h.get_by_label("Page 3").click_modifiers(Modifiers::COMMAND);
h.run_steps(2);
h.get_by_label("Page properties").click();
h.run_steps(3);
assert!(!h.state().dialogs.is_empty(), "Page Properties is open");
assert!(!h.get_by_label("OK").accesskit_node().is_disabled(), "opens on the order the pages have");
h.get_by_label("Use Column Order").click();
h.run_steps(2);
h.get_by_label("OK").click();
h.run_steps(3);
assert!(h.state().dialogs.is_empty());
assert_eq!(tabs(&h), [Column, Annotations, Column]);
assert_eq!(undo(&h).0.as_deref(), Some("Set tab order"));
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(tabs(&h), [Annotations; 3], "one undo step");
// Pages with different orders open with no choice made, and OK is off until one is.
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(&ctx_h, pdfcraft_engine::Edit::SetTabOrder { pages: vec![0], order: Row });
h.run_steps(2);
h.state_mut().views[0].select_pages(&[0, 1]);
assert!(h.execute("page.properties"));
h.run_steps(3);
assert!(h.get_by_label("OK").accesskit_node().is_disabled(), "no choice is made for pages with different orders");
h.get_by_label("Use Row Order").click();
h.run_steps(2);
h.get_by_label("OK").click();
h.run_steps(3);
assert_eq!(tabs(&h), [Row, Row, Annotations]);
// OK on the order the pages already have adds no undo step, and Cancel changes nothing.
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
let before = (tabs(&h), undo(&h));
assert_eq!(before.1.1.as_deref(), Some("Set tab order"), "the undone step can be redone");
h.state_mut().views[0].select_pages(&[2]);
assert!(h.execute("page.properties"));
h.run_steps(3);
h.get_by_label("OK").click();
h.run_steps(3);
assert_eq!((tabs(&h), undo(&h)), before);
assert!(h.execute("page.properties"));
h.run_steps(3);
h.get_by_label("Use Document Structure").click();
h.run_steps(2);
h.get_by_label("Cancel").click();
h.run_steps(3);
assert!(h.state().dialogs.is_empty());
assert_eq!((tabs(&h), undo(&h)), before);
}
/// Page properties sets the tab order of the document it was opened on: when another document
/// is active by OK, neither document changes.
#[test]
fn page_properties_does_not_apply_to_another_active_document() {
use pdfcraft_engine::TabOrder::Annotations;
let mut h = organize(3);
let first = h.state().views[0].id;
h.state_mut().views[0].select_pages(&[0]);
assert!(h.execute("page.properties"));
h.run_steps(3);
h.get_by_label("Use Column Order").click();
h.run_steps(2);
let ctx_h = h.ctx.clone();
h.state_mut().open_bytes(&ctx_h, "other.pdf", None, fixture(3)).unwrap();
h.run_steps(4);
let other = h.state().views[1].id;
assert_eq!(h.state().active_ids().map(|(_, id)| id), Some(other));
h.get_by_label("OK").click();
h.run_steps(3);
assert!(h.state().dialogs.is_empty());
for id in [first, other] {
let doc = h.state().session.get(id).unwrap();
assert_eq!(doc.tab_orders(), [Annotations; 3]);
assert!(doc.can_undo().is_none());
}
}
/// Page properties sets the tab order of the pages it was opened on: when an edit changes the
/// document while the dialog is open, OK closes it without changing a page.
#[test]
fn page_properties_does_not_apply_after_the_document_changes() {
use pdfcraft_engine::TabOrder::Annotations;
let mut h = organize(3);
let id = h.state().views[0].id;
h.state_mut().views[0].select_pages(&[0]);
assert!(h.execute("page.properties"));
h.run_steps(3);
h.get_by_label("Use Column Order").click();
h.run_steps(2);
// Page 1 becomes page 3, so index 0 is another page now.
assert!(h.execute("page.reverse"));
h.run_steps(3);
h.get_by_label("OK").click();
h.run_steps(3);
assert!(h.state().dialogs.is_empty());
assert_eq!(
support::shown_notice(&mut h).as_deref(),
Some("The document changed while the dialog was open, so nothing was changed. Open Page properties again to set the tab order.")
);
let doc = h.state().session.get(id).unwrap();
assert_eq!(doc.tab_orders(), [Annotations; 3]);
assert_ne!(doc.can_undo(), Some("Set tab order"));
}
/// Leaving Organize (a page opened from the grid, or the grid's Close) goes back to the
/// workspace's panel: under the Edit tab, the Edit panel, not All tools.
#[test]
fn leaving_organize_goes_back_to_the_workspace_panel() {
use pdfcraft_ui_egui::{LeftPanel, Mode};
let mut h = harness(3, |_| {});
h.state_mut().mode = Mode::Edit;
h.state_mut().left = LeftPanel::Tool("edit");
h.run_steps(2);
h.state_mut().left = LeftPanel::Tool("organize");
h.state_mut().views[0].organize = true;
h.run_steps(3);
h.get_by_label("Insert");
// What opening a page from the grid does.
h.state_mut().views[0].organize = false;
h.run_steps(3);
assert_eq!(h.state().left, LeftPanel::Tool("edit"), "back to the Edit panel");
// The same from All tools goes back to All tools.
h.state_mut().mode = Mode::AllTools;
h.state_mut().left = LeftPanel::Tool("organize");
h.state_mut().views[0].organize = true;
h.run_steps(3);
h.state_mut().views[0].organize = false;
h.run_steps(3);
assert_eq!(h.state().left, LeftPanel::AllTools);
}
#[test]
fn image_copy_cut_paste_keyboard_and_context_menu() {
let mut png = Vec::new();
image::RgbImage::from_pixel(60, 40, image::Rgb([180, 50, 50])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
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| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = clipboard_app(&cc.egui_ctx, &sink);
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let initial_count = h.state().session.get(id).unwrap().page_images(0).len();
assert_eq!(initial_count, 1, "starts with 1 image");
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let c = r.center();
// 1. Select the image
h.hover_at(c);
h.run_steps(1);
h.drag_at(c);
h.run_steps(1);
h.drop_at(c);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_some(), "image selected");
// 2. Copy via keyboard shortcut (COMMAND+C / Event::Copy)
h.event(egui::Event::Copy);
h.run_steps(2);
assert!(copies.lock().unwrap().iter().flatten().any(|f| f.mime.starts_with("image/")), "the image went to the clipboard");
// 3. The integration delivers the pixels for the keyboard paste.
let image = egui::ColorImage::filled([60, 40], egui::Color32::from_rgb(180, 50, 50));
h.event(egui::Event::PasteImage(std::sync::Arc::new(image)));
h.run_steps(3);
let after_paste = h.state().session.get(id).unwrap().page_images(0).len();
assert_eq!(after_paste, 2, "now has 2 images");
assert_eq!(h.state().views[0].session.image_selection.as_ref().map(|s| s.index), Some(1), "new image is selected");
// 4. Cut via keyboard shortcut (COMMAND+X / Event::Cut)
h.event(egui::Event::Cut);
h.run_steps(3);
let after_cut = h.state().session.get(id).unwrap().page_images(0).len();
assert_eq!(after_cut, 1, "cut deleted the pasted image");
assert!(h.state().views[0].session.image_selection.is_none(), "selection cleared after cut");
// 5. Paste again via context menu on the page
h.hover_at(c);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
h.get_by_role_and_label(Role::MenuItem, "Paste").click();
h.step();
h.event(egui::Event::PasteImage(std::sync::Arc::new(egui::ColorImage::filled([60, 40], egui::Color32::from_rgb(180, 50, 50)))));
h.run_steps(3);
let after_paste2 = h.state().session.get(id).unwrap().page_images(0).len();
assert_eq!(after_paste2, 2, "pasted via context menu");
}
#[test]
fn added_and_pasted_image_uses_refactored_image_edits() {
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 80, image::Rgb([120, 180, 240])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = harness(1, |_| {});
h.run_steps(4);
// 1. Add image via add_image
let ctx_h = h.ctx.clone();
h.state_mut().add_image(&ctx_h, "test_add.png".into(), png);
h.run_steps(3);
// Verify refactored mode is active
assert_eq!(h.state().quick_tool, pdfcraft_ui_egui::QuickTool::EditText);
let id = h.state().views[0].id;
let images = h.state().session.get(id).unwrap().page_images(0);
assert_eq!(images.len(), 1, "image is added");
assert_eq!(
h.state().views[0].session.image_selection.as_ref().map(|s| (s.page, s.index)),
Some((0, 0)),
"added image has refactored image_selection"
);
// 2. Perform refactored crop on the added image
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let c = r.center();
h.hover_at(c);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
h.get_by_label("Crop").click();
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.crop.is_some()), "crop mode entered");
let smaller = egui::Rect::from_center_size(c, egui::vec2(40.0, 40.0));
set_crop_box(&mut h, smaller);
h.key_press(egui::Key::Enter);
h.run_steps(4);
assert!(!h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.crop.is_some()), "crop mode exited");
let images_after_crop = h.state().session.get(id).unwrap().page_images(0);
assert!(images_after_crop[0].visible_rect.is_some());
assert_ne!(images_after_crop[0].visible_rect, Some(images_after_crop[0].rect), "crop applied to added image");
// 3. Move the cropped added image
let orig_rect = images_after_crop[0].rect;
let orig_vis = images_after_crop[0].visible_rect.unwrap();
let dx = 20.0;
let dy = 20.0;
let new_rect = [orig_rect[0] + dx, orig_rect[1] + dy, orig_rect[2] + dx, orig_rect[3] + dy];
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(&ctx_h, pdfcraft_engine::Edit::EditPageImage { page: 0, index: 0, change: pdfcraft_engine::ImageEdit::Move(new_rect) });
h.run_steps(3);
let images_after_move = h.state().session.get(id).unwrap().page_images(0);
assert_eq!(images_after_move[0].rect, new_rect, "moved rect matches");
assert_eq!(
images_after_move[0].visible_rect,
Some([orig_vis[0] + dx, orig_vis[1] + dy, orig_vis[2] + dx, orig_vis[3] + dy]),
"crop preserved after move"
);
}
#[test]
fn stale_clipboard_plain_text_does_not_paste_image() {
let (mut h, _copies) = clipboard_harness("doc.pdf", fixture(1));
// Put an image into internal cache
h.state_mut().last_copied_image = Some((10, 10, vec![0; 400]));
h.state_mut().last_copied_file = Some(("cached.png".to_string(), vec![1, 2, 3]));
// The text arrives in the paste event, as it does from the OS.
// A paste event containing plain text clears internal image cache and does not paste image
h.event(egui::Event::Paste("just plain text".into()));
h.run_steps(2);
// can_paste_image should now be false because the clipboard has text, not an image!
assert!(!h.state_mut().can_paste_image(), "cannot paste image when the clipboard has text");
// paste_image should fail and return false without pasting
let ctx_h = h.ctx.clone();
assert!(!h.state_mut().paste_image(&ctx_h, 0, None), "must not paste stale cached image");
let doc = h.state().session.get(h.state().views[0].id).unwrap();
assert_eq!(doc.page_images(0).len(), 0, "no image pasted on the page");
}
/// Delivered image pixels paste once; accompanying key releases add no second image.
#[test]
fn delivered_image_with_key_releases_pastes_once() {
let (mut h, _copies) = clipboard_harness("form.pdf", include_bytes!("../data/form.pdf").to_vec());
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let initial_count = h.state().session.get(id).unwrap().page_images(0).len();
// The integration supplies the pixels and consumes native shortcut keys. Extra releases carry no paste authority.
h.event(egui::Event::PasteImage(std::sync::Arc::new(egui::ColorImage::filled([40, 20], egui::Color32::GREEN))));
h.event(egui::Event::Key { key: egui::Key::V, physical_key: None, pressed: false, repeat: false, modifiers: egui::Modifiers::COMMAND });
h.run_steps(3);
let count = h.state().session.get(id).unwrap().page_images(0).len();
assert_eq!(count, initial_count + 1, "exactly one image must be pasted for delivered pixels, got {count}");
h.event(egui::Event::Key { key: egui::Key::V, physical_key: None, pressed: false, repeat: false, modifiers: egui::Modifiers::NONE });
h.run_steps(2);
assert_eq!(h.state().session.get(id).unwrap().page_images(0).len(), initial_count + 1);
}
/// Win+V release without COMMAND must NOT paste an image.
#[test]
fn win_v_does_not_paste_on_open() {
let (mut h, _copies) = clipboard_harness("form.pdf", include_bytes!("../data/form.pdf").to_vec());
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let initial_count = h.state().session.get(id).unwrap().page_images(0).len();
// Complete an intentional paste immediately before opening clipboard history.
h.event(egui::Event::PasteImage(std::sync::Arc::new(egui::ColorImage::filled([40, 20], egui::Color32::GREEN))));
h.event(egui::Event::Key { key: egui::Key::V, physical_key: None, pressed: false, repeat: false, modifiers: egui::Modifiers::COMMAND });
h.run_steps(1);
assert_eq!(h.state().session.get(id).unwrap().page_images(0).len(), initial_count + 1);
// Simulate pressing Win+V (opening clipboard history flyout):
// The OS intercepts V press, so PdfCraft only sees V release without COMMAND (Modifiers::NONE).
h.event(egui::Event::Key { key: egui::Key::V, physical_key: None, pressed: false, repeat: false, modifiers: egui::Modifiers::NONE });
h.run_steps(2);
let count = h.state().session.get(id).unwrap().page_images(0).len();
assert_eq!(count, initial_count + 1, "opening Win+V after a completed paste must not paste again");
// Choosing an item delivers another image, possibly alongside synthetic shortcut events.
h.event(egui::Event::PasteImage(std::sync::Arc::new(egui::ColorImage::filled([40, 20], egui::Color32::GREEN))));
h.event(egui::Event::Key { key: egui::Key::V, physical_key: None, pressed: false, repeat: false, modifiers: egui::Modifiers::COMMAND });
h.run_steps(3);
let final_count = h.state().session.get(id).unwrap().page_images(0).len();
assert_eq!(final_count, initial_count + 2, "the two intentional deliveries must paste exactly two images");
}
/// Normal Ctrl+V press followed by release delivers exactly one image, not two.
#[test]
fn paste_press_and_release_delivers_single_image() {
let (mut h, _copies) = clipboard_harness("form.pdf", include_bytes!("../data/form.pdf").to_vec());
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let initial_count = h.state().session.get(id).unwrap().page_images(0).len();
// Press V with COMMAND; the platform answers with the pixels in the next frame.
h.event(egui::Event::Key { key: egui::Key::V, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::COMMAND });
h.run_steps(1);
h.event(egui::Event::PasteImage(std::sync::Arc::new(egui::ColorImage::filled([40, 20], egui::Color32::GREEN))));
// Release V with COMMAND
h.event(egui::Event::Key { key: egui::Key::V, physical_key: None, pressed: false, repeat: false, modifiers: egui::Modifiers::COMMAND });
h.run_steps(2);
let count = h.state().session.get(id).unwrap().page_images(0).len();
assert_eq!(count, initial_count + 1, "Ctrl+V press and release must deliver exactly one image, got {count}");
}
/// Issue P1: Paste via context menu at a point away from the centre centers the image at that point.
#[test]
fn paste_at_context_menu_point_centers_on_cursor() {
let (mut h, _copies) = clipboard_harness("form.pdf", include_bytes!("../data/form.pdf").to_vec());
// Keep the context-menu point clear of the window's lower edge.
h.set_option("zoom", "75").unwrap();
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let initial_count = h.state().session.get(id).unwrap().page_images(0).len();
let target_at = [120.0, 180.0];
let at = {
let app = h.state();
let info = &app.session.get(id).unwrap().info.pages[0];
let v = info.user_to_view(target_at[0] as f32, target_at[1] as f32);
app.views[0].page_xform(0).unwrap().norm_to_screen(v[0] / info.width, v[1] / info.height)
};
h.hover_at(at);
h.run_steps(1);
for pressed in [true, false] {
h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed, modifiers: Default::default() });
}
h.run_steps(2);
h.get_by_role_and_label(Role::MenuItem, "Paste").click();
h.step();
h.event(egui::Event::PasteImage(std::sync::Arc::new(egui::ColorImage::filled([60, 40], egui::Color32::from_rgb(50, 150, 250)))));
h.run_steps(3);
let images = h.state().session.get(id).unwrap().page_images(0);
assert_eq!(images.len(), initial_count + 1);
let r = images[initial_count].rect;
let center = ((r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0);
assert!((center.0 - target_at[0]).abs() < 1.0 && (center.1 - target_at[1]).abs() < 1.0, "expected center {target_at:?}, got {center:?}");
}
/// Issue P2: A newer clipboard override image beats stale in-app copy.
#[test]
fn newer_clipboard_override_beats_stale_in_app_copy() {
let (mut h, _copies) = clipboard_harness("form.pdf", include_bytes!("../data/form.pdf").to_vec());
let id = h.state().views[0].id;
let initial_count = h.state().session.get(id).unwrap().page_images(0).len();
// Stale in-app copy: 10x10 red image
h.state_mut().last_copied_image = Some((10, 10, [255u8, 0, 0, 255].repeat(100)));
// The platform delivers a newer 50x50 green image with the paste.
h.event(egui::Event::PasteImage(std::sync::Arc::new(egui::ColorImage::filled([50, 50], egui::Color32::GREEN))));
h.event(egui::Event::Key { key: egui::Key::V, physical_key: None, pressed: false, repeat: false, modifiers: egui::Modifiers::COMMAND });
h.run_steps(3);
let images = h.state().session.get(id).unwrap().page_images(0);
assert_eq!(images.len(), initial_count + 1);
let r = images[initial_count].rect;
let (w, h_dim) = (r[2] - r[0], r[3] - r[1]);
assert!((w - 50.0).abs() < 1.0 && (h_dim - 50.0).abs() < 1.0, "expected 50x50 override image, got {w}x{h_dim}");
}
#[test]
fn rotated_image_selection_and_crop() {
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 40, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let c = r.center();
// Select the image
h.hover_at(c);
h.run_steps(1);
h.drag_at(c);
h.run_steps(1);
h.drop_at(c);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_some(), "selected");
// Right-click and rotate clockwise
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
h.get_by_label("Rotate Clockwise").click();
h.run_steps(3);
let doc = h.state().session.get(id).unwrap();
let im_rotated = &doc.page_images(0)[0];
assert!(im_rotated.matrix[1].abs() > 1.0 || im_rotated.matrix[2].abs() > 1.0, "matrix is rotated: {:?}", im_rotated.matrix);
// Enter crop mode with user-space bounds [0.2..0.8] of rotated image rect
let full = im_rotated.rect;
let crop_keep = [
full[0] + (full[2] - full[0]) * 0.2,
full[1] + (full[3] - full[1]) * 0.2,
full[0] + (full[2] - full[0]) * 0.8,
full[1] + (full[3] - full[1]) * 0.8,
];
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop = Some(pdfcraft_ui_egui::CropMode::new(crop_keep));
h.run_steps(2);
// Apply crop via Enter key
h.key_press(egui::Key::Enter);
h.run_steps(4);
let doc = h.state().session.get(id).unwrap();
assert_eq!(doc.can_undo(), Some("Crop image"));
let im_cropped = &doc.page_images(0)[0];
assert!(im_cropped.matrix[1].abs() > 1.0 || im_cropped.matrix[2].abs() > 1.0, "matrix remains rotated: {:?}", im_cropped.matrix);
assert!(h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.crop.is_none()), "crop mode ended");
}
#[test]
fn test_image_context_menu_precedence() {
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 40, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let c = r.center();
// Select the image
h.hover_at(c);
h.run_steps(1);
h.drag_at(c);
h.run_steps(1);
h.drop_at(c);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_some(), "selected");
// Simulate a secondary click strictly inside an image's bounds
h.hover_at(c);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: c, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
// Assert that the image widget consumes the click and the canvas background menu does not trigger
h.get_by_label("Rotate Clockwise");
h.get_by_label("Flip horizontal");
assert!(!menu_row(&h, "Rotate view clockwise"), "canvas background menu must not trigger");
assert!(h.query_all_by_label("Add bookmark").next().is_none(), "canvas background menu must not trigger");
assert!(h.query_all_by_label("Take a snapshot").next().is_none(), "canvas background menu must not trigger");
}
#[test]
fn test_image_context_menu_in_crop_mode_and_when_moved() {
let mut png = Vec::new();
image::RgbImage::from_pixel(100, 100, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
// Shrink and center the picture on the page so page margins exist around it
let full = h.state().session.get(id).unwrap().page_images(0)[0].rect;
let (qw, qh) = ((full[2] - full[0]) / 4.0, (full[3] - full[1]) / 4.0);
let (mx, my) = ((full[0] + full[2]) / 2.0, (full[1] + full[3]) / 2.0);
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::EditPageImage { page: 0, index: 0, change: pdfcraft_engine::ImageEdit::Move([mx - qw, my - qh, mx + qw, my + qh]) },
);
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let c = r.center();
// Select the image
h.hover_at(c);
h.run_steps(1);
h.drag_at(c);
h.run_steps(1);
h.drop_at(c);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_some(), "selected");
// Enter crop mode:
let doc_id = h.state().views[0].id;
let original_rect = h.state().session.get(doc_id).unwrap().page_images(0)[0].rect;
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop = Some(pdfcraft_ui_egui::CropMode::new(original_rect));
h.run_steps(2);
// Set a smaller crop box that is moved towards top-left
let moved_crop = egui::Rect::from_center_size(c - egui::vec2(5.0, 5.0), egui::vec2(20.0, 20.0));
set_crop_box(&mut h, moved_crop);
h.run_steps(2);
// Simulate secondary click on the moved crop box
let click_pos = moved_crop.center();
h.hover_at(click_pos);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: click_pos, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: click_pos, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
// In crop mode and when moved, do not open right click window when clicked on the image (#5835)
assert!(h.query_all_by_label("Rotate Clockwise").next().is_none(), "image context menu must not trigger in crop mode");
assert!(h.query_all_by_label("Flip horizontal").next().is_none(), "image context menu must not trigger in crop mode");
assert!(!menu_row(&h, "Rotate view clockwise"), "canvas background menu must not trigger in crop mode");
assert!(h.query_all_by_label("Add bookmark").next().is_none(), "canvas background menu must not trigger in crop mode");
assert!(h.query_all_by_label("Take a snapshot").next().is_none(), "canvas background menu must not trigger in crop mode");
assert!(
h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.crop.is_some()),
"crop mode must remain active after right click on image"
);
// Action pills remain visible and interactive
assert!(h.query_by_label("Done").is_some());
assert!(h.query_by_label("Cancel").is_some());
// Right clicking outside the image on the page exits crop mode and opens the canvas background menu
let outside_pos = r.left_top() + egui::vec2(10.0, 10.0);
h.hover_at(outside_pos);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: outside_pos, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: outside_pos, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_none(), "clicking outside clears image selection");
assert!(menu_row(&h, "Rotate view clockwise"), "canvas background menu triggers outside image");
}
fn crop_secondary_click(h: &mut Harness<'static, PdfCraftApp>, p: egui::Pos2) {
assert!(h.state().views[0].viewport_rect().contains(p), "test point must be visible: {p:?}");
h.hover_at(p);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
}
/// Whether an open menu has a row whose label contains `label`: the page display bar's buttons and the
/// toolbar share some of the menus' labels, so a menu is told by its rows' role.
fn menu_row(h: &Harness<'static, PdfCraftApp>, label: &str) -> bool {
h.query_all_by(|n| n.role() == Role::MenuItem && n.label().is_some_and(|l| l.contains(label))).next().is_some()
}
fn assert_crop_context_preserved(h: &Harness<'static, PdfCraftApp>) {
let selection = h.state().views[0].session.image_selection.as_ref().expect("crop remains selected");
assert_eq!(selection.index, 0, "an overlapping image must not steal the crop");
assert!(selection.crop.is_some(), "crop mode remains active");
for label in ["Flip horizontal", "Rotate view clockwise", "Take a snapshot"] {
assert!(!menu_row(h, label), "unexpected menu: {label}");
}
assert!(h.query_by_label("Done").is_some());
assert!(h.query_by_label("Cancel").is_some());
}
#[test]
fn test_image_context_menu_crop_overlapping_image_and_handle() {
for rotation in [0, 90, 180, 270] {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1400.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "overlap.pdf", None, two_image_fixture()).unwrap();
app.views[0].view.rotation = rotation;
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
let rect = h.state().session.get(id).unwrap().page_images(0)[0].rect;
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(&ctx_h, pdfcraft_engine::Edit::EditPageImage { page: 0, index: 1, change: pdfcraft_engine::ImageEdit::Move(rect) });
let images = h.state().session.get(id).unwrap().page_images(0);
assert_eq!(images.len(), 2);
assert_eq!(images[0].rect, images[1].rect, "fixture images must overlap");
assert!(h.execute("edit.edit_text"));
h.run_steps(4);
let mut selection = pdfcraft_ui_egui::ImageSelection::at(0, 0);
selection.crop = Some(pdfcraft_ui_egui::CropMode::new(rect));
h.state_mut().views[0].session.image_selection = Some(selection);
h.run_steps(2);
let xf = h.state().views[0].page_xform(0).unwrap();
let doc = h.state().session.get(id).unwrap();
let image_rect = pdfcraft_ui_egui::canvas::image_screen_rect(&doc.page_images(0)[0], &xf, &doc.info, 0);
crop_secondary_click(&mut h, image_rect.center());
assert_crop_context_preserved(&h);
// This point is outside both images, but inside the left handle's grip.
crop_secondary_click(&mut h, image_rect.left_center() - egui::vec2(5.0, 0.0));
assert_crop_context_preserved(&h);
crop_secondary_click(&mut h, image_rect.left_center() - egui::vec2(20.0, 0.0));
assert!(h.state().views[0].session.image_selection.is_none());
assert!(menu_row(&h, "Rotate view clockwise"));
}
}
/// A right-click on the part of a picture its crop cuts away is the crop's, as one on the box kept:
/// crop mode stays and no menu opens (#5835).
#[test]
fn a_right_click_on_the_part_a_crop_cuts_away_keeps_crop_mode() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1400.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "cut.pdf", None, two_image_fixture()).unwrap();
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
let rect = h.state().session.get(id).unwrap().page_images(0)[0].rect;
assert!(h.execute("edit.edit_text"));
h.run_steps(4);
// Keep the picture's left half.
let mut selection = pdfcraft_ui_egui::ImageSelection::at(0, 0);
selection.crop = Some(pdfcraft_ui_egui::CropMode::new([rect[0], rect[1], (rect[0] + rect[2]) / 2.0, rect[3]]));
h.state_mut().views[0].session.image_selection = Some(selection);
h.run_steps(2);
let xf = h.state().views[0].page_xform(0).unwrap();
let doc = h.state().session.get(id).unwrap();
let b = pdfcraft_ui_egui::canvas::image_screen_rect(&doc.page_images(0)[0], &xf, &doc.info, 0);
assert!(b.width() > 60.0, "the picture is wide enough for a point clear of the grips: {b:?}");
crop_secondary_click(&mut h, b.lerp_inside(egui::vec2(0.8, 0.5)));
assert_crop_context_preserved(&h);
}
#[test]
fn test_rotated_image_crop_secondary_click_in_outer_bounding_box_clears_selection() {
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 80, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1400.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
let full = h.state().session.get(id).unwrap().page_images(0)[0].rect;
let (qw, qh) = ((full[2] - full[0]) / 4.0, (full[3] - full[1]) / 4.0);
let (mx, my) = ((full[0] + full[2]) / 2.0, (full[1] + full[3]) / 2.0);
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::EditPageImage { page: 0, index: 0, change: pdfcraft_engine::ImageEdit::Move([mx - qw, my - qh, mx + qw, my + qh]) },
);
h.run_steps(4);
// Rotate the image by 45°
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(&ctx_h, pdfcraft_engine::Edit::EditPageImage { page: 0, index: 0, change: pdfcraft_engine::ImageEdit::RotateBy(45.0) });
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let c = r.center();
// Select the image and put it into crop mode
h.hover_at(c);
h.run_steps(1);
h.drag_at(c);
h.run_steps(1);
h.drop_at(c);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_some(), "selected");
let doc_id = h.state().views[0].id;
let img = &h.state().session.get(doc_id).unwrap().page_images(0)[0];
let (cx, cy) = ((full[0] + full[2]) / 2.0, (full[1] + full[3]) / 2.0);
let shrunk_crop = [cx - 5.0, cy - 5.0, cx + 5.0, cy + 5.0];
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop = Some(pdfcraft_ui_egui::CropMode::new(shrunk_crop));
h.run_steps(2);
let xf = h.state().views[0].page_xform(0).unwrap();
let doc_info = &h.state().session.get(doc_id).unwrap().info;
let b = pdfcraft_ui_egui::canvas::image_screen_rect(img, &xf, doc_info, 0);
// The top-left corner of the axis-aligned bounding box `b` is outside the 45°-rotated diamond quad
let corner = b.left_top() + egui::vec2(2.0, 2.0);
assert!(b.contains(corner), "corner point is inside screen bounding box");
// Right-clicking at the corner must clear the crop selection and show the canvas context menu (#5900 review)
crop_secondary_click(&mut h, corner);
assert!(h.state().views[0].session.image_selection.is_none(), "clicking in outer bounding box of rotated image clears crop selection");
assert!(menu_row(&h, "Rotate view clockwise"));
}
/// A turned picture is its shape, not its bounds: a click in a corner of its bounds, off the
/// picture and its crop box, is a click away that applies the crop (#6422 review).
#[test]
fn a_click_beside_a_turned_picture_in_its_bounds_applies_its_crop() {
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 80, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1400.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
let full = h.state().session.get(id).unwrap().page_images(0)[0].rect;
let (qw, qh) = ((full[2] - full[0]) / 4.0, (full[3] - full[1]) / 4.0);
let (mx, my) = ((full[0] + full[2]) / 2.0, (full[1] + full[3]) / 2.0);
let edit = |change| pdfcraft_engine::Edit::EditPageImage { page: 0, index: 0, change };
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(&ctx_h, edit(pdfcraft_engine::ImageEdit::Move([mx - qw, my - qh, mx + qw, my + qh])));
h.run_steps(4);
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(&ctx_h, edit(pdfcraft_engine::ImageEdit::RotateBy(45.0)));
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let c = h.state().views[0].page_screen_rect(0).expect("on screen").center();
click_at(&mut h, c);
assert!(h.state().views[0].session.image_selection.is_some(), "selected");
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop =
Some(pdfcraft_ui_egui::CropMode::new([mx - 5.0, my - 5.0, mx + 5.0, my + 5.0]));
h.run_steps(2);
let img = h.state().session.get(id).unwrap().page_images(0)[0].clone();
let xf = h.state().views[0].page_xform(0).unwrap();
let b = pdfcraft_ui_egui::canvas::image_screen_rect(&img, &xf, &h.state().session.get(id).unwrap().info, 0);
// The bounds' top-left corner lies outside the 45° diamond.
click_at(&mut h, b.left_top() + egui::vec2(2.0, 2.0));
assert!(h.state().views[0].session.image_selection.is_none(), "a click off the turned picture lets it go");
assert_ne!(h.state().session.get(id).unwrap().page_images(0)[0].visible_rect, img.visible_rect, "the crop is applied");
}
/// A picture alone in the middle of a page, empty page around it (#6422).
fn picture_with_room_around() -> Vec<u8> {
use pdfcraft_cos::{Dict, Document, Object, Stream, write_incremental};
let mut doc = Document::open(std::sync::Arc::new(fixture(1))).unwrap();
let mut image = Dict::new();
image.set(b"Type".to_vec(), Object::name("XObject"));
image.set(b"Subtype".to_vec(), Object::name("Image"));
image.set(b"Width".to_vec(), Object::Int(1));
image.set(b"Height".to_vec(), Object::Int(1));
image.set(b"BitsPerComponent".to_vec(), Object::Int(8));
image.set(b"ColorSpace".to_vec(), Object::name("DeviceRGB"));
let image = doc.add(Object::Stream(Stream::flate(image, &[60, 110, 200])));
let page = pdfcraft_model::pages(&doc).swap_remove(0);
let contents = doc.add(Object::Stream(Stream::flate(Dict::new(), b"q 100 0 0 80 50 150 cm /Im0 Do Q")));
let mut resources = doc.resolve(page.dict.get(b"Resources").unwrap()).as_dict().unwrap().clone();
let mut xo = Dict::new();
xo.set(b"Im0".to_vec(), Object::Ref(image));
resources.set(b"XObject".to_vec(), Object::Dict(xo));
doc.update_dict(page.obj, |d| {
d.set(b"Resources".to_vec(), Object::Dict(resources));
d.set(b"Contents".to_vec(), Object::Ref(contents));
})
.unwrap();
write_incremental(&doc, &Default::default()).unwrap()
}
/// In crop mode a click on the empty page applies the crop and lets the picture go; a click on the
/// part being cropped away keeps crop mode (#6422).
#[test]
fn a_click_away_from_the_picture_applies_its_crop() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "room.pdf", None, picture_with_room_around()).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.state_mut().views[0].view.fit = pdfcraft_ui_egui::canvas::Fit::Page;
h.run_steps(3);
let r = h.state().views[0].page_screen_rect(0).unwrap();
let at = |x: f32, y: f32| egui::pos2(r.left() + x / 200.0 * r.width(), r.top() + (300.0 - y) / 300.0 * r.height());
let doc_id = h.state().views[0].id;
let full = h.state().session.get(doc_id).unwrap().page_images(0)[0].rect;
click_at(&mut h, at(100.0, 190.0));
assert!(h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.index == 0), "the picture is selected");
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop = Some(pdfcraft_ui_egui::CropMode::new(full));
h.run_steps(2);
let keep = [full[0], full[1], full[0] + 60.0, full[3]];
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop.as_mut().unwrap().keep = keep;
h.run_steps(2);
// On the dimmed part of the picture: still cropping, nothing applied.
click_at(&mut h, at(135.0, 190.0));
assert!(h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.crop.is_some()), "a click on the picture keeps crop mode");
assert_eq!(h.state().session.get(doc_id).unwrap().page_images(0)[0].visible_rect, Some(full), "nothing is applied yet");
// On a handle, where it reaches past the picture's frame: still cropping.
let handle = at(50.0, 190.0) - egui::vec2(6.0, 0.0);
click_at(&mut h, handle);
assert!(h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.crop.is_some()), "a click on a handle keeps crop mode");
assert_eq!(h.state().session.get(doc_id).unwrap().page_images(0)[0].visible_rect, Some(full), "nothing is applied yet");
// A click pressed on the picture and let go just past its frame: still cropping.
let edge = at(150.0, 190.0);
h.hover_at(edge - egui::vec2(2.0, 0.0));
h.run_steps(1);
h.drag_at(edge - egui::vec2(2.0, 0.0));
h.run_steps(1);
h.hover_at(edge + egui::vec2(2.0, 0.0));
h.run_steps(1);
h.drop_at(edge + egui::vec2(2.0, 0.0));
h.run_steps(4);
assert!(h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.crop.is_some()), "a press on the picture keeps crop mode");
assert_eq!(h.state().session.get(doc_id).unwrap().page_images(0)[0].visible_rect, Some(full), "nothing is applied yet");
// On the empty page: applied, and the picture is let go.
click_at(&mut h, at(100.0, 60.0));
assert!(h.state().views[0].session.image_selection.is_none(), "a click away lets the picture go");
let visible = h.state().session.get(doc_id).unwrap().page_images(0)[0].visible_rect.unwrap();
assert!(keep.iter().zip(visible).all(|(a, b)| (a - b).abs() < 0.5), "the crop is applied: {visible:?}");
}
#[test]
fn crop_action_pills_interactive_and_apply_cancel_reset() {
let mut png = Vec::new();
image::RgbImage::from_pixel(100, 100, image::Rgb([100, 150, 200]))
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
.unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "square.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let c = r.center();
// Select image
h.hover_at(c);
h.run_steps(1);
h.drag_at(c);
h.run_steps(1);
h.drop_at(c);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_some());
// Enter crop mode
let doc_id = h.state().views[0].id;
let original_rect = h.state().session.get(doc_id).unwrap().page_images(0)[0].rect;
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop = Some(pdfcraft_ui_egui::CropMode::new(original_rect));
h.run_steps(2);
// Set smaller crop rect (50x50 in center)
let smaller = egui::Rect::from_center_size(c, egui::vec2(50.0, 50.0));
set_crop_box(&mut h, smaller);
h.run_steps(2);
let cancel_pos = h.get_by_role_and_label(Role::Button, "Cancel").rect().center();
// Click Cancel pill: exits crop mode without applying changes
click_at(&mut h, cancel_pos);
h.run_steps(3);
assert!(h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.crop.is_none()), "Cancel exits crop mode");
assert_eq!(h.state().session.get(doc_id).unwrap().page_images(0)[0].visible_rect, Some(original_rect), "image is uncropped");
// Re-enter crop mode and set crop rect
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop = Some(pdfcraft_ui_egui::CropMode::new(original_rect));
set_crop_box(&mut h, smaller);
h.run_steps(2);
// Click "Remove Crop" pill
let reset_pos = h.get_by_role_and_label(Role::Button, "Remove Crop").rect().center();
click_at(&mut h, reset_pos);
h.run_steps(2);
let current_keep = h.state().views[0].session.image_selection.as_ref().unwrap().crop.as_ref().unwrap().keep;
assert_eq!(current_keep, original_rect, "Remove Crop restored crop to full bounds");
// Re-set smaller crop and click "Done" pill to apply
set_crop_box(&mut h, smaller);
h.run_steps(2);
let done_pos_after = h.get_by_role_and_label(Role::Button, "Done").rect().center();
click_at(&mut h, done_pos_after);
h.run_steps(4);
assert!(h.state().views[0].session.image_selection.as_ref().is_some_and(|s| s.crop.is_none()), "Done exits crop mode");
let after_images = h.state().session.get(doc_id).unwrap().page_images(0);
assert_ne!(after_images[0].visible_rect, Some(original_rect), "crop was applied");
}
#[test]
fn test_cropped_image_selection_and_bounding_box() {
let mut png = Vec::new();
image::RgbImage::from_pixel(100, 100, image::Rgb([100, 150, 200]))
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
.unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "square.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let doc_id = h.state().views[0].id;
let full_rect = h.state().session.get(doc_id).unwrap().page_images(0)[0].rect;
// Apply crop to the center quarter: [0.25, 0.25, 0.75, 0.75]
let crop_keep = [
full_rect[0] + (full_rect[2] - full_rect[0]) * 0.25,
full_rect[1] + (full_rect[3] - full_rect[1]) * 0.25,
full_rect[0] + (full_rect[2] - full_rect[0]) * 0.75,
full_rect[1] + (full_rect[3] - full_rect[1]) * 0.75,
];
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(&ctx_h, pdfcraft_engine::Edit::EditPageImage { page: 0, index: 0, change: pdfcraft_engine::ImageEdit::Crop(crop_keep) });
h.run_steps(4);
let img = &h.state().session.get(doc_id).unwrap().page_images(0)[0];
assert_eq!(img.rect, full_rect, "underlying CTM rect is non-destructively preserved");
let vis = img.visible_rect.expect("has visible_rect");
assert!((vis[0] - crop_keep[0]).abs() < 1e-3);
assert!((vis[1] - crop_keep[1]).abs() < 1e-3);
assert!((vis[2] - crop_keep[2]).abs() < 1e-3);
assert!((vis[3] - crop_keep[3]).abs() < 1e-3);
// Verify selectable bounds helper gives the cropped intersection
assert_eq!(pdfcraft_ui_egui::canvas::image_selectable_bounds(img), vis);
// Select tool (mouse tool)
h.state_mut().quick_tool = pdfcraft_ui_egui::QuickTool::Select;
h.run_steps(2);
let page_rect = h.state().views[0].page_screen_rect(0).expect("on screen");
// Center point is inside the cropped area:
let center = page_rect.center();
click_at(&mut h, center);
h.run_steps(2);
let picked = h.state().views[0].session.picked_image.expect("image is picked when clicking inside crop");
assert_eq!(picked.page, 0);
assert_eq!(picked.index, 0);
// The selection bounding box wraps the visible cropped area, NOT the full CTM rect!
assert_eq!(picked.rect, vis, "picked rect wraps the cropped bounds");
// Deselect
h.key_press(egui::Key::Escape);
h.run_steps(2);
assert!(h.state().views[0].session.picked_image.is_none());
// Corner point (e.g. top-left corner of the page) is outside the crop box but inside full CTM rect
let corner = page_rect.min + egui::vec2(10.0, 10.0);
click_at(&mut h, corner);
h.run_steps(2);
assert!(h.state().views[0].session.picked_image.is_none(), "clicking cropped-away region must not hit-test or select the image");
}
/// Print production ▸ Add printer marks: the dialog adds removable marks around
/// the trim box, widening the page to make room; Remove printer marks takes them off.
#[test]
fn printer_marks_are_added_from_the_dialog_and_removed() {
let mut h = harness(2, |_| {});
let id = h.state().views[0].id;
let before = h.state().session.get(id).unwrap().info.pages[0].width;
assert!(h.execute("prepress.marks"));
h.run_steps(2);
assert!(!h.state().dialogs.is_empty(), "Printer marks is open");
h.get_by_label("All marks");
h.get_by_label("Add").click();
h.run_steps(3);
let doc = h.state().session.get(id).unwrap();
assert_eq!(doc.can_undo(), Some("Add printer marks"));
assert!(doc.marks().contains(&pdfcraft_engine::MarkKind::PrinterMarks));
// No trim box: the whole page is the trim box, so the page grew 36 pt a side for the marks.
assert!((doc.info.pages[0].width - (before + 72.0)).abs() < 0.5, "{} → {}", before, doc.info.pages[0].width);
assert_eq!(support::shown_notice(&mut h).unwrap(), "Printer marks added");
assert!(h.execute("prepress.marks.remove"));
h.run_steps(3);
let doc = h.state().session.get(id).unwrap();
assert!(!doc.marks().contains(&pdfcraft_engine::MarkKind::PrinterMarks));
assert_eq!(doc.can_undo(), Some("Remove printer marks"));
}
/// Print production ▸ Convert colours: the dialog converts the page's colours
/// and says how many; with nothing to convert it says so and changes nothing.
#[test]
fn convert_colours_from_the_dialog() {
let mut doc = pdfcraft_cos::Document::open(std::sync::Arc::new(fixture(1))).unwrap();
let page = pdfcraft_model::pages(&doc).remove(0);
let cs = doc.add(pdfcraft_cos::Object::Stream(pdfcraft_cos::Stream::flate(
pdfcraft_cos::Dict::new(),
b"1 0 0 rg 10 10 50 50 re f 0 0 1 RG 0 0 m 100 100 l S",
)));
doc.update_dict(page.obj, |d| d.set(b"Contents".to_vec(), pdfcraft_cos::Object::Ref(cs))).unwrap();
let bytes = pdfcraft_cos::write_full(&doc, &Default::default()).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "colours.pdf", None, bytes.clone()).expect("opens");
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
assert!(h.execute("prepress.convert_colors"));
h.run_steps(2);
assert!(!h.state().dialogs.is_empty(), "Convert colours is open");
h.get_by_label("Grayscale");
// The mode bar has a "Convert" too: the dialog's button is the last one.
h.query_all_by_label("Convert").last().unwrap().click();
h.run_steps(3);
assert_eq!(support::shown_notice(&mut h).unwrap(), "Converted 2 colours and 0 pictures on 1 pages");
assert_eq!(h.state().session.get(id).unwrap().can_undo(), Some("Convert colours"));
assert!(h.execute("prepress.convert_colors"));
h.run_steps(2);
h.query_all_by_label("Convert").last().unwrap().click();
h.run_steps(3);
assert_eq!(support::shown_notice(&mut h).unwrap(), "Nothing on those pages needed converting");
}
/// Print production ▸ Fix hairlines: the dialog widens zero-width and too-thin
/// strokes and says how many; with nothing to fix it says so and changes nothing.
#[test]
fn fix_hairlines_from_the_dialog() {
// A page with a zero-width line and a 2 pt line.
let mut doc = pdfcraft_cos::Document::open(std::sync::Arc::new(fixture(1))).unwrap();
let page = pdfcraft_model::pages(&doc).remove(0);
let cs = doc
.add(pdfcraft_cos::Object::Stream(pdfcraft_cos::Stream::flate(pdfcraft_cos::Dict::new(), b"0 w 10 10 m 100 10 l S 2 w 10 20 m 100 20 l S")));
doc.update_dict(page.obj, |d| d.set(b"Contents".to_vec(), pdfcraft_cos::Object::Ref(cs))).unwrap();
let bytes = pdfcraft_cos::write_full(&doc, &Default::default()).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "lines.pdf", None, bytes.clone()).expect("opens");
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
assert!(h.execute("prepress.hairlines"));
h.run_steps(2);
assert!(!h.state().dialogs.is_empty(), "Fix hairlines is open");
h.get_by_label("Type 3 font glyphs");
h.get_by_label("Fix").click();
h.run_steps(3);
assert_eq!(support::shown_notice(&mut h).unwrap(), "Fixed 1 hairline on 1 page");
assert_eq!(h.state().session.get(id).unwrap().can_undo(), Some("Fix hairlines"));
// Nothing left: told, and no new undo step.
assert!(h.execute("prepress.hairlines"));
h.run_steps(2);
h.get_by_label("Fix").click();
h.run_steps(3);
assert_eq!(support::shown_notice(&mut h).unwrap(), "No hairlines found on those pages");
assert_eq!(h.state().session.get(id).unwrap().can_undo(), Some("Fix hairlines"));
}
/// Several strokes on one page: the notice says "hairlines" and "1 page".
#[test]
fn fix_hairlines_notice_uses_singular_and_plural() {
let mut doc = pdfcraft_cos::Document::open(std::sync::Arc::new(fixture(2))).unwrap();
for (n, page) in pdfcraft_model::pages(&doc).into_iter().enumerate() {
let lines = if n == 0 { "0 w 10 10 m 100 10 l S 0 w 10 20 m 100 20 l S" } else { "2 w 10 10 m 100 10 l S" };
let cs = doc.add(pdfcraft_cos::Object::Stream(pdfcraft_cos::Stream::flate(pdfcraft_cos::Dict::new(), lines.as_bytes())));
doc.update_dict(page.obj, |d| d.set(b"Contents".to_vec(), pdfcraft_cos::Object::Ref(cs))).unwrap();
}
let bytes = pdfcraft_cos::write_full(&doc, &Default::default()).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "lines.pdf", None, bytes.clone()).expect("opens");
app
}
});
h.run_steps(4);
// The dialog's default range is every page; only the first has hairlines.
assert!(h.execute("prepress.hairlines"));
h.run_steps(2);
h.get_by_label("Fix").click();
h.run_steps(3);
assert_eq!(support::shown_notice(&mut h).unwrap(), "Fixed 2 hairlines on 1 page");
}
/// The Read mode command (not the mode bar) applies a paragraph still open in the editor, as
/// putting the Edit text tool down does; left open, it would stay drawn in Read mode with no tool.
#[test]
fn read_mode_shortcut_applies_the_open_paragraph() {
use pdfcraft_ui_egui::{Mode, QuickTool};
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
// The line "Page 1" sits at (20, 150) on a 200 × 300 page, 24 pt.
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height());
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(3);
assert!(h.state().views[0].session.line_editor.is_some(), "the line opens for editing");
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = "Chapter One".into();
h.run_steps(1);
h.state_mut().run_command(&egui::Context::default(), "view.read_mode");
h.run_steps(3);
let s = h.state();
assert_eq!(s.mode, Mode::Read);
assert_eq!(s.quick_tool, QuickTool::Select);
assert!(s.views[0].session.line_editor.is_none(), "no editor stays open in Read mode");
assert_eq!(s.session.get(s.views[0].id).unwrap().text_lines(0)[0].text, "Chapter One", "the edit is applied");
}
#[test]
fn arrange_duplicate_image_clears_selection_when_ambiguous() {
use pdfcraft_cos::{Dict, Document, Object, SaveOptions, Stream, write_incremental};
let mut doc = Document::open(std::sync::Arc::new(fixture(1))).unwrap();
let page = pdfcraft_model::pages(&doc)[0].clone();
let mut form = Dict::new();
form.set(b"Subtype".to_vec(), Object::name("Form"));
form.set(b"BBox".to_vec(), Object::Array([0, 0, 200, 300].map(Object::Int).to_vec()));
let artwork = doc.add(Object::Stream(Stream::flate(form, b"0.2 0.5 0.9 rg 20 20 160 260 re f")));
// Paint two occurrences of the same figure at the exact same position
let contents = doc.add(Object::Stream(Stream::flate(Dict::new(), b"q 1 0 0 1 0 0 cm /Figure Do Q q 1 0 0 1 0 0 cm /Figure Do Q")));
doc.update_dict(page.obj, |d| {
d.set(b"Contents".to_vec(), Object::Ref(contents));
let mut xo = Dict::new();
xo.set(b"Figure".to_vec(), Object::Ref(artwork));
let mut resources = Dict::new();
resources.set(b"XObject".to_vec(), Object::Dict(xo));
d.set(b"Resources".to_vec(), Object::Dict(resources));
})
.unwrap();
let bytes = write_incremental(&doc, &SaveOptions::default()).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "dup.pdf", None, bytes.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let doc_id = h.state().views[0].id;
let images = h.state().session.get(doc_id).unwrap().page_images(0);
assert_eq!(images.len(), 2, "two duplicate image occurrences exist");
assert_eq!(images[0].rect, images[1].rect);
assert_eq!(images[0].name, images[1].name);
// Select the second occurrence (index 1)
h.state_mut().views[0].session.image_selection = Some(pdfcraft_ui_egui::ImageSelection::at(0, 1));
h.run_steps(2);
// Arrange send to back
let ctx_h = h.ctx.clone();
let applied = h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::EditPageImage {
page: 0,
index: 1,
change: pdfcraft_engine::ImageEdit::Arrange(pdfcraft_engine::ArrangeOrder::SendToBack),
},
);
assert!(applied);
h.run_steps(2);
// Because there are duplicate occurrences matching name, object, and rect,
// selection must be cleared rather than picking an arbitrary occurrence.
assert!(h.state().views[0].session.image_selection.is_none(), "selection is safely cleared when reordering ambiguous duplicate images");
}
#[test]
fn arrange_unique_image_preserves_selection() {
use pdfcraft_cos::{Dict, Document, Object, SaveOptions, Stream, write_incremental};
let mut doc = Document::open(std::sync::Arc::new(fixture(1))).unwrap();
let page = pdfcraft_model::pages(&doc)[0].clone();
let mut form = Dict::new();
form.set(b"Subtype".to_vec(), Object::name("Form"));
form.set(b"BBox".to_vec(), Object::Array([0, 0, 200, 300].map(Object::Int).to_vec()));
let artwork1 = doc.add(Object::Stream(Stream::flate(form.clone(), b"0.2 0.5 0.9 rg 20 20 160 260 re f")));
let artwork2 = doc.add(Object::Stream(Stream::flate(form, b"0.9 0.2 0.5 rg 40 40 100 100 re f")));
let contents = doc.add(Object::Stream(Stream::flate(Dict::new(), b"q 1 0 0 1 0 0 cm /FigA Do Q q 1 0 0 1 0 0 cm /FigB Do Q")));
doc.update_dict(page.obj, |d| {
d.set(b"Contents".to_vec(), Object::Ref(contents));
let mut xo = Dict::new();
xo.set(b"FigA".to_vec(), Object::Ref(artwork1));
xo.set(b"FigB".to_vec(), Object::Ref(artwork2));
let mut resources = Dict::new();
resources.set(b"XObject".to_vec(), Object::Dict(xo));
d.set(b"Resources".to_vec(), Object::Dict(resources));
})
.unwrap();
let bytes = write_incremental(&doc, &SaveOptions::default()).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "unique.pdf", None, bytes.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let doc_id = h.state().views[0].id;
let images = h.state().session.get(doc_id).unwrap().page_images(0);
assert_eq!(images.len(), 2);
assert_eq!(images[0].name, "FigA");
assert_eq!(images[1].name, "FigB");
// Select FigA at index 0 and bring it to front (should move after FigB, becoming index 1)
h.state_mut().views[0].session.image_selection = Some(pdfcraft_ui_egui::ImageSelection::at(0, 0));
h.run_steps(2);
let ctx_h = h.ctx.clone();
let applied = h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::EditPageImage {
page: 0,
index: 0,
change: pdfcraft_engine::ImageEdit::Arrange(pdfcraft_engine::ArrangeOrder::BringToFront),
},
);
assert!(applied);
h.run_steps(2);
let sel = h.state().views[0].session.image_selection.as_ref().expect("selection is preserved for unique image");
assert_eq!(sel.index, 1, "selection index tracks the unique image's new position");
}
#[test]
fn undo_arrange_re_resolves_image_selection_or_clears() {
use pdfcraft_cos::{Dict, Document, Object, SaveOptions, Stream, write_incremental};
let mut doc = Document::open(std::sync::Arc::new(fixture(1))).unwrap();
let page = pdfcraft_model::pages(&doc)[0].clone();
let mut form = Dict::new();
form.set(b"Subtype".to_vec(), Object::name("Form"));
form.set(b"BBox".to_vec(), Object::Array([0, 0, 200, 300].map(Object::Int).to_vec()));
let artwork1 = doc.add(Object::Stream(Stream::flate(form.clone(), b"0.2 0.5 0.9 rg 20 20 160 260 re f")));
let artwork2 = doc.add(Object::Stream(Stream::flate(form, b"0.9 0.2 0.5 rg 40 40 100 100 re f")));
let contents = doc.add(Object::Stream(Stream::flate(Dict::new(), b"q 1 0 0 1 0 0 cm /FigA Do Q q 1 0 0 1 0 0 cm /FigB Do Q")));
doc.update_dict(page.obj, |d| {
d.set(b"Contents".to_vec(), Object::Ref(contents));
let mut xo = Dict::new();
xo.set(b"FigA".to_vec(), Object::Ref(artwork1));
xo.set(b"FigB".to_vec(), Object::Ref(artwork2));
let mut resources = Dict::new();
resources.set(b"XObject".to_vec(), Object::Dict(xo));
d.set(b"Resources".to_vec(), Object::Dict(resources));
})
.unwrap();
let bytes = write_incremental(&doc, &SaveOptions::default()).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "undo_arrange.pdf", None, bytes.clone()).expect("opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let doc_id = h.state().views[0].id;
let images = h.state().session.get(doc_id).unwrap().page_images(0);
assert_eq!(images.len(), 2);
assert_eq!(images[0].name, "FigA");
assert_eq!(images[1].name, "FigB");
// Select FigA (back image, index 0)
h.state_mut().views[0].session.image_selection = Some(pdfcraft_ui_egui::ImageSelection::at(0, 0));
h.run_steps(2);
// Bring to front
let ctx_h = h.ctx.clone();
let applied = h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::EditPageImage {
page: 0,
index: 0,
change: pdfcraft_engine::ImageEdit::Arrange(pdfcraft_engine::ArrangeOrder::BringToFront),
},
);
assert!(applied);
h.run_steps(2);
// Selection now tracks FigA at index 1
let sel = h.state().views[0].session.image_selection.as_ref().expect("selected after arrange");
assert_eq!(sel.index, 1);
// Undo
assert!(h.execute("edit.undo"));
h.run_steps(2);
let doc_after = h.state().session.get(doc_id).unwrap();
let images_after = doc_after.page_images(0);
assert_eq!(images_after[0].name, "FigA");
assert_eq!(images_after[1].name, "FigB");
// After undoing Bring to front, selection must either be FigA (index 0) or None, never FigB (index 1)
let sel_after = h.state().views[0].session.image_selection.as_ref().expect("selection re-resolved to the same image");
assert_eq!(sel_after.index, 0, "selection should point to FigA at index 0, not FigB at index 1");
assert_eq!(images_after[sel_after.index].name, "FigA");
// Redo
assert!(h.execute("edit.redo"));
h.run_steps(2);
let doc_redo = h.state().session.get(doc_id).unwrap();
let images_redo = doc_redo.page_images(0);
assert_eq!(images_redo[0].name, "FigB");
assert_eq!(images_redo[1].name, "FigA");
let sel_redo = h.state().views[0].session.image_selection.as_ref().expect("selection re-resolved after redo");
assert_eq!(sel_redo.index, 1, "selection should point to FigA at index 1");
assert_eq!(images_redo[sel_redo.index].name, "FigA");
// Bringing to front when already at front is refused, but selection must be preserved
let ctx_h = h.ctx.clone();
let applied_again = h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::EditPageImage {
page: 0,
index: 1,
change: pdfcraft_engine::ImageEdit::Arrange(pdfcraft_engine::ArrangeOrder::BringToFront),
},
);
assert!(!applied_again);
h.run_steps(2);
let sel_refused = h.state().views[0].session.image_selection.as_ref().expect("selection preserved after refused arrange");
assert_eq!(sel_refused.index, 1);
}
/// #3061: a paragraph running past its page's bottom edge (an HTML converter relying on the
/// page clip) opens in an editor that draws on its page only. It covered the next page.
#[test]
fn a_paragraph_running_off_its_page_edits_without_covering_the_next_page() {
let _gpu = gpu();
let text = |first: &str, count: usize, leading: usize| {
let mut s = String::from("BT /F1 14 Tf\n");
for i in 0..count {
s.push_str(&format!("1 0 0 1 72 {} Tm ({first} line {}: the client may cancel this) Tj\n", 720 - (leading * i) as i64, i + 1));
}
s.push_str("ET");
s.into_bytes()
};
// Page 1's single paragraph goes down to y = -270; page 2 is ordinary.
let (one, two) = (text("Page one", 56, 18), text("PAGE TWO", 3, 40));
let stream = |d: &[u8]| [format!("<< /Length {} >>\nstream\n", d.len()).into_bytes(), d.to_vec(), b"\nendstream".to_vec()].concat();
let page = |contents: usize| {
format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents {contents} 0 R /Resources << /Font << /F1 7 0 R >> >> >>")
.into_bytes()
};
let objs: Vec<Vec<u8>> = vec![
b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(),
b"<< /Type /Pages /Kids [3 0 R 5 0 R] /Count 2 >>".to_vec(),
page(4),
stream(&one),
page(6),
stream(&two),
b"<< /Type /Font /Subtype /Type1 /BaseFont /Times-Roman >>".to_vec(),
];
let mut bytes = b"%PDF-1.7\n".to_vec();
let mut offsets = Vec::new();
for (i, o) in objs.iter().enumerate() {
offsets.push(bytes.len());
bytes.extend_from_slice(format!("{} 0 obj\n", i + 1).as_bytes());
bytes.extend_from_slice(o);
bytes.extend_from_slice(b"\nendobj\n");
}
let xref = bytes.len();
bytes.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
for o in offsets {
bytes.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
}
bytes.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes());
let mut h = Harness::builder().with_size(egui::vec2(1200.0, 1400.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "off-page.pdf", None, bytes).unwrap();
app
}
});
h.run_steps(4);
h.state_mut().views[0].view.fit = pdfcraft_ui_egui::canvas::Fit::Page;
h.run_steps(4);
let rects =
|h: &Harness<'static, PdfCraftApp>| (h.state().views[0].page_screen_rect(0).unwrap(), h.state().views[0].page_screen_rect(1).unwrap());
// The same pages without Edit text, so without the paragraph's outline: what the gap looks
// like there, pixel by pixel (review on #3070: it isn't one colour on every platform).
let (first, second) = rects(&h);
let plain = h.render().unwrap();
assert!(h.execute("edit.edit_text"));
h.run_steps(4);
assert_eq!(rects(&h), (first, second), "Edit text keeps the pages where they were");
let id = h.state().views[0].id;
let info = h.state().session.get(id).unwrap().info.pages[0].clone();
shown_on_its_page_only(&mut h, &plain, first, second, "the paragraph's outline");
let v = info.user_to_view(200.0, 378.0);
let p = first.min + egui::vec2(v[0], v[1]) * (first.width() / info.width);
h.hover_at(p);
h.run_steps(1);
h.drag_at(p);
h.run_steps(1);
h.drop_at(p);
crate::support::settle(&mut h);
assert!(
h.state().views[0].session.line_editor.as_ref().is_some_and(|e| e.page == 0 && e.text.contains("line 50")),
"the paragraph's off-page lines are in it"
);
shown_on_its_page_only(&mut h, &plain, first, second, "the open editor");
// Scrolled up past the tool bar, the editor stays inside the document area.
let viewport = h.state().views[0].viewport_rect();
let above = viewport.top() - 3.0;
let before = h.render().unwrap();
h.hover_at(egui::pos2(first.right() - 20.0, first.center().y));
h.event(egui::Event::MouseWheel {
unit: egui::MouseWheelUnit::Point,
delta: egui::vec2(0.0, -300.0),
phase: egui::TouchPhase::Move,
modifiers: egui::Modifiers::NONE,
source: egui::MouseWheelSource::Unknown,
});
h.run_steps(4);
let scrolled = h.state().views[0].page_screen_rect(0).unwrap();
assert!(scrolled.top() < viewport.top() - 100.0, "scrolled: {scrolled:?} in {viewport:?}");
assert!(h.state().views[0].session.line_editor.is_some(), "still editing");
let after = h.render().unwrap();
for x in (first.left() as u32 + 70)..(first.left() as u32 + 420) {
assert_eq!(after.get_pixel(x, above as u32), before.get_pixel(x, above as u32), "above the document area at x = {x}");
}
}
/// Between pages `first` and `second`, and on `second` above its own text, across where page
/// 1's paragraph is: what is there without it (the gap as `plain` shows it without Edit text,
/// white paper).
fn shown_on_its_page_only(h: &mut Harness<'static, PdfCraftApp>, plain: &image::RgbaImage, first: egui::Rect, second: egui::Rect, what: &str) {
let shot = h.render().unwrap();
let pixel = |x: f32, y: f32| shot.get_pixel(x as u32, y as u32).0;
let gap_y = (first.bottom() + second.top()) / 2.0;
for x in (first.left() as u32 + 70)..(first.left() as u32 + 420) {
let x = x as f32;
assert_eq!(pixel(x, gap_y), plain.get_pixel(x as u32, gap_y as u32).0, "{what}: between the pages at x = {x}");
for dy in [4.0, 20.0, 40.0] {
let [r, g, b, _] = pixel(x, second.top() + dy);
assert!(r > 240 && g > 240 && b > 240, "{what}: page 2 at {x}, +{dy}: {r},{g},{b}");
}
}
}
/// Where a mode bar tab is, by its label.
fn mode_tab(h: &Harness<'static, PdfCraftApp>, label: &str) -> egui::Rect {
h.get_all_by_role_and_label(Role::Tab, label).map(|n| n.rect()).find(|r| r.bottom() < 120.0).expect("a mode tab")
}
/// Hover each mode bar tab that page 1's text lies under after scrolling it up under the bar.
/// With All tools closed the text lies under Edit, Convert and E-Sign.
fn hover_mode_tabs(h: &mut Harness<'static, PdfCraftApp>, text: egui::Rect) {
h.state_mut().views[0].view.go_to = Some(craft::pages::ui::GoTo::Top { page: 0, fraction: 0.5 });
h.run_steps(4);
let offset = h.state().views[0].page_screen_rect(0).expect("on screen").top();
for label in ["Edit", "Convert", "E-Sign"] {
let at = mode_tab(h, label).center();
assert!(text.translate(egui::vec2(0.0, offset)).contains(at), "the text lies under {label}");
h.hover_at(at);
h.run_steps(2);
}
}
/// Page 1's text block on screen, relative to the page's top edge, in the window as it is now.
fn page_text(h: &Harness<'static, PdfCraftApp>) -> egui::Rect {
let id = h.state().views[0].id;
let block = h.state().session.get(id).unwrap().text_blocks(0)[0].clone();
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
let at = |x: f64, y: f64| egui::pos2(r.left() + x as f32 * k, (300.0 - y as f32) * k);
egui::Rect::from_two_pos(at(block.rect[0], block.rect[1]), at(block.rect[2], block.rect[3]))
}
/// Page text keeps its I-beam where the page is visible (#2755).
#[test]
fn mode_tabs_keep_the_hand_over_page_text_scrolled_under_them() {
let mut h = harness(2, |app| app.set_option(&egui::Context::default(), "left", "closed").unwrap());
let text = page_text(&h);
// Where the page shows, its text takes the I-beam, once the page's text has arrived.
h.state_mut().views[0].view.go_to = Some(craft::pages::ui::GoTo::Top { page: 0, fraction: 0.3 });
h.run_steps(4);
let top = h.state().views[0].page_screen_rect(0).expect("on screen").top();
let mut cursor = egui::CursorIcon::Default;
let text_cursor = crate::support::step_until(&mut h, |h| {
h.hover_at(text.center() + egui::vec2(0.0, top));
h.run_steps(2);
cursor = h.output().platform_output.cursor_icon;
cursor == egui::CursorIcon::Text
});
assert!(text_cursor, "the I-beam over page text, not {cursor:?}");
hover_mode_tabs(&mut h, text);
}
/// Edit text, then the All tools panel closed, as reported (#2755): a paragraph scrolled up
/// under the mode bar. The open paragraph's editor must not cover the tabs and take their clicks.
#[test]
fn mode_tabs_keep_the_hand_over_a_paragraph_scrolled_under_them() {
let mut h = harness(2, |_| {});
assert!(h.execute("edit.edit_text"));
h.set_option("left", "closed").unwrap();
h.run_steps(4);
let text = page_text(&h);
hover_mode_tabs(&mut h, text);
// Open it, where it shows.
h.state_mut().views[0].view.go_to = Some(craft::pages::ui::GoTo::Top { page: 0, fraction: 0.3 });
h.run_steps(4);
let top = h.state().views[0].page_screen_rect(0).expect("on screen").top();
click_at(&mut h, text.center() + egui::vec2(0.0, top));
assert!(h.state().views[0].session.line_editor.is_some());
hover_mode_tabs(&mut h, text);
// Nor does the editor's frame, with the paragraph's top edge just under a tab (the frame
// reaches 2 to 6 points outside it).
let convert = mode_tab(&h, "Convert").center();
h.state_mut().views[0].view.go_to = Some(craft::pages::ui::GoTo::Top { page: 0, fraction: 0.0 });
h.run_steps(4);
let page = h.state().views[0].page_screen_rect(0).expect("on screen");
h.state_mut().views[0].view.go_to =
Some(craft::pages::ui::GoTo::Top { page: 0, fraction: f64::from((page.top() - (convert.y + 4.0 - text.top())) / page.height()) });
h.run_steps(4);
let page = h.state().views[0].page_screen_rect(0).expect("on screen");
assert!((page.top() + text.top() - convert.y - 4.0).abs() < 1.0, "Convert lies on the paragraph's frame");
h.hover_at(convert);
h.run_steps(2);
h.state_mut().views[0].view.go_to = Some(craft::pages::ui::GoTo::Top { page: 0, fraction: 0.5 });
h.run_steps(4);
click_at(&mut h, convert);
assert_eq!(h.state().mode, pdfcraft_ui_egui::Mode::Convert, "the tab takes the click, not the paragraph's editor");
}
#[test]
fn copy_pasted_image_discards_coordinates_and_moves_without_offset() {
let mut png = Vec::new();
image::RgbImage::from_pixel(60, 60, image::Rgb([100, 150, 200])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let (mut h, _copies) = clipboard_harness("doc.pdf", fixture(1));
// 1. Add image to page 0
let ctx_h = h.ctx.clone();
h.state_mut().add_image(&ctx_h, "test_img.png".into(), png);
h.run_steps(3);
let id = h.state().views[0].id;
let images_before = h.state().session.get(id).unwrap().page_images(0);
assert_eq!(images_before.len(), 1);
// 2. Copy the image from page 0
assert!(h.state_mut().copy_page_image(0, 0));
assert!(h.state().last_copied_image.is_some(), "image pixel data is kept");
// 3. The platform delivers the copied pixels for Ctrl+V.
let (width, height, rgba) = h.state().last_copied_image.clone().unwrap();
let image = egui::ColorImage::from_rgba_unmultiplied([width as usize, height as usize], &rgba);
h.event(egui::Event::PasteImage(std::sync::Arc::new(image)));
h.run_steps(3);
let images_after_paste = h.state().session.get(id).unwrap().page_images(0);
assert_eq!(images_after_paste.len(), 2, "second image pasted");
// The pasted image should be centered on the page, not offset by (+20, -20) from the first image
let img1 = &images_after_paste[0];
let img2 = &images_after_paste[1];
assert_eq!(img1.rect, img2.rect, "pasted image placed using natural page centering");
// 4. Move the pasted image by an explicit Move change
let target = [50.0, 50.0, 110.0, 110.0];
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(&ctx_h, pdfcraft_engine::Edit::EditPageImage { page: 0, index: 1, change: pdfcraft_engine::ImageEdit::Move(target) });
h.run_steps(3);
let images_after_move = h.state().session.get(id).unwrap().page_images(0);
assert_eq!(images_after_move[1].rect, target, "moved image rect matches target exactly without offset");
}
#[test]
fn clicking_after_image_context_menu_action_resets_menu_state_and_updates_selection() {
let mut png = Vec::new();
image::RgbImage::from_pixel(80, 40, image::Rgb([200, 40, 40])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "picture.png", None, png.clone()).expect("opens");
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
// Move the first image to left side: [10.0, 10.0, 30.0, 30.0]
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::EditPageImage { page: 0, index: 0, change: pdfcraft_engine::ImageEdit::Move([10.0, 10.0, 30.0, 30.0]) },
);
// Add second image to right side: [50.0, 10.0, 70.0, 30.0]
let mut png2 = Vec::new();
image::RgbImage::from_pixel(20, 20, image::Rgb([40, 200, 40])).write_to(&mut std::io::Cursor::new(&mut png2), image::ImageFormat::Png).unwrap();
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::AddImage { page: 0, rect: Some([50.0, 10.0, 70.0, 30.0]), name: "img2.png".into(), bytes: std::sync::Arc::new(png2) },
);
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let info = h.state().session.get(id).unwrap().info.pages[0].clone();
let user_to_screen = move |u: [f64; 2]| {
let v = info.user_to_view(u[0] as f32, u[1] as f32);
let scale = r.width() / info.width;
egui::pos2(r.left() + v[0] * scale, r.top() + v[1] * scale)
};
let p0 = user_to_screen([20.0, 20.0]);
let p1 = user_to_screen([60.0, 20.0]);
let p_empty = user_to_screen([40.0, 35.0]);
// 1. Select image 0
h.hover_at(p0);
h.run_steps(1);
h.drag_at(p0);
h.run_steps(1);
h.drop_at(p0);
h.run_steps(2);
assert_eq!(h.state().views[0].session.image_selection.as_ref().map(|s| s.index), Some(0), "image 0 selected");
// 2. Select image 1 (switches selection)
h.hover_at(p1);
h.run_steps(1);
h.drag_at(p1);
h.run_steps(1);
h.drop_at(p1);
h.run_steps(2);
assert_eq!(h.state().views[0].session.image_selection.as_ref().map(|s| s.index), Some(1), "image 1 selected");
// 3. Click empty space (clears selection)
h.hover_at(p_empty);
h.run_steps(1);
h.drag_at(p_empty);
h.run_steps(1);
h.drop_at(p_empty);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_none(), "empty page clears selection");
// 4. Select image 0 again
h.hover_at(p0);
h.run_steps(1);
h.drag_at(p0);
h.run_steps(1);
h.drop_at(p0);
h.run_steps(2);
assert_eq!(h.state().views[0].session.image_selection.as_ref().map(|s| s.index), Some(0), "image 0 selected again");
// 5. Right-click image 0 to open context menu
h.hover_at(p0);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: p0, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: p0, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
// 6. Click "Flip horizontal"
h.get_by_label("Flip horizontal").click();
h.run_steps(3);
assert!(h.query_all_by_label("Flip horizontal").next().is_none(), "the image menu closes after its action");
// 7. Click image 1: should switch selection to image 1
h.hover_at(p1);
h.run_steps(1);
h.drag_at(p1);
h.run_steps(1);
h.drop_at(p1);
h.run_steps(2);
assert_eq!(
h.state().views[0].session.image_selection.as_ref().map(|s| s.index),
Some(1),
"clicking other image switches selection after context menu action"
);
// 8. Click empty space: should clear selection
h.hover_at(p_empty);
h.run_steps(1);
h.drag_at(p_empty);
h.run_steps(1);
h.drop_at(p_empty);
h.run_steps(2);
assert!(h.state().views[0].session.image_selection.is_none(), "clicking empty page clears selection after context menu action");
}
#[test]
fn format_panel_shows_selection_size_and_updates_after_font_applied() {
let mut objs: Vec<String> = vec![
"<< /Type /Catalog /Pages 2 0 R >>".into(),
"<< /Type /Pages /Kids [4 0 R] /Count 1 /MediaBox [0 0 200 300] >>".into(),
"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".into(),
"<< /Type /Page /Parent 2 0 R /Contents 5 0 R /Resources << /Font << /F1 3 0 R >> >> >>".into(),
];
let body = "BT /F1 24 Tf 20 200 Td (Heading) Tj /F1 12 Tf 0 -25 Td (Paragraph) Tj ET";
objs.push(format!("<< /Length {} >>\nstream\n{body}\nendstream", body.len()));
let mut pdf = Vec::from(b"%PDF-1.7\n");
let mut xref = Vec::new();
for (i, o) in objs.iter().enumerate() {
xref.push(pdf.len());
pdf.extend_from_slice(format!("{} 0 obj\n{o}\nendobj\n", i + 1).as_bytes());
}
let xref_pos = pdf.len();
pdf.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
for o in xref {
pdf.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
}
pdf.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref_pos}\n%%EOF\n", objs.len() + 1).as_bytes());
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "test.pdf", None, pdf).expect("fixture opens");
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 180.0) / 300.0 * r.height());
h.hover_at(at);
h.run_steps(1);
h.drag_at(at);
h.run_steps(1);
h.drop_at(at);
h.run_steps(3);
assert!(h.state().views[0].session.line_editor.is_some(), "editor opened");
let text = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
assert!(text.contains("Paragraph"), "text is {text}");
// Select the 12 pt paragraph text "Paragraph" via keyboard selection in the TextEdit
h.key_press_modifiers(Modifiers::COMMAND, Key::ArrowRight);
h.run_steps(1);
for _ in 0.."Paragraph".len() {
h.key_press_modifiers(Modifiers::SHIFT, Key::ArrowLeft);
h.run_steps(1);
}
use egui::accesskit::Role;
let size_node = h.get_by_role_and_label(Role::SpinButton, "Font size");
assert_eq!(size_node.value().as_deref(), Some("12"), "size text input value is 12");
let font_node = h.get_by_role_and_label(Role::ComboBox, "Font family");
assert_eq!(font_node.value().as_deref(), Some("Helvetica"), "initial font is Helvetica");
// Apply Times to the selection
font_node.click();
h.run_steps(2);
// The row and its first cell both carry the name: the row is the first.
h.query_all_by_label("Times").next().expect("the Times row").click();
h.run_steps(4);
// Font combo box for the whole block is now Mixed because Heading is Helvetica and Paragraph is Times
let font_node = h.get_by_role_and_label(Role::ComboBox, "Font family");
assert_eq!(font_node.value().as_deref(), Some("Mixed"), "whole block has mixed fonts");
// Apply Times to the whole block
font_node.click();
h.run_steps(2);
h.query_all_by_label("Times").next().expect("the Times row").click();
h.run_steps(4);
// Now all text is Times, so font combo box updates to Times
let font_node = h.get_by_role_and_label(Role::ComboBox, "Font family");
assert_eq!(font_node.value().as_deref(), Some("Times"), "font combo box updates to Times");
}
/// The paragraph opened for typing, as a person opens it: a click on its text.
fn open_paragraph(h: &mut Harness<'static, PdfCraftApp>) {
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let at = egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height());
click_at(h, at);
assert!(h.state().views[0].session.line_editor.is_some(), "the paragraph opens for typing");
}
/// Ctrl+Z after a formatting change, with the paragraph still open: the change is undone and
/// the editor closes. The box's own Undo used to take the key, putting back the text it had
/// before the change into a box laid out for the page after it, and the document kept the change.
#[test]
fn undo_with_the_paragraph_open_undoes_the_formatting() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
open_paragraph(&mut h);
h.get_by_label("Bold").click();
h.run_steps(4);
{
let s = h.state();
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!(doc.can_undo(), Some("Edit text"));
assert_eq!(doc.text_blocks(0)[0].base_font, "Helvetica-Bold");
assert!(s.views[0].session.line_editor.is_some(), "still editing");
}
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(4);
let s = h.state();
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!(doc.can_undo(), None, "the formatting is undone");
assert_eq!(doc.text_blocks(0)[0].base_font, "Helvetica");
assert!(s.views[0].session.line_editor.is_none(), "the editor closed with the step it showed");
}
/// Undo while typing takes back the typing in the box first; the document is left alone.
#[test]
fn undo_while_typing_takes_back_the_typing_first() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
open_paragraph(&mut h);
let original = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
h.key_press_modifiers(Modifiers::COMMAND, Key::End);
h.event(egui::Event::Text(" more".into()));
h.run_steps(2);
assert_ne!(h.state().views[0].session.line_editor.as_ref().unwrap().text, original);
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
let s = h.state();
let ed = s.views[0].session.line_editor.as_ref().expect("still typing");
assert_eq!(ed.text, original, "the typing is taken back");
assert_eq!(s.session.get(s.views[0].id).unwrap().can_undo(), None, "the document is untouched");
}
/// A paragraph of a short, a wide and a short row, opened in the paragraph editor.
fn three_row_paragraph() -> Harness<'static, PdfCraftApp> {
let bytes = paragraph_drag_fixture("BT /F1 10 Tf 12 TL 60 400 Td (One two) Tj T* (three four five six seven eight) Tj T* (nine ten) Tj ET", 0, 1);
let mut h = paragraph_drag_harness(bytes, 0);
let (at, _) = paragraph_drag_points(&h, "One two three four five six seven eight nine ten", [0.0, 0.0]);
click_at(&mut h, at);
assert!(h.state().views[0].session.line_editor.is_some(), "the paragraph opens for typing");
h
}
/// Format text's alignment flyout, by pointer.
fn align_by_pointer(h: &mut Harness<'static, PdfCraftApp>, choice: &str) {
let button = h.get_by_label("Alignment").rect().center();
click_at(h, button);
let item = h.get_by_label(choice).rect().center();
click_at(h, item);
}
fn row_rect(h: &Harness<'static, PdfCraftApp>, starts: &str) -> [f64; 4] {
let s = h.state();
s.session.get(s.views[0].id).unwrap().text_lines(0).into_iter().find(|l| l.text.starts_with(starts)).expect("the row").rect
}
/// Existing-row alignment stays disabled until intent survives reopening; cancellation keeps all rows.
#[test]
fn selected_existing_rows_keep_alignment_disabled_and_the_complete_paragraph() {
let mut h = three_row_paragraph();
let before = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
let first = row_rect(&h, "One");
let last = row_rect(&h, "nine");
h.key_press_modifiers(Modifiers::COMMAND, Key::End);
h.key_press_modifiers(Modifiers::SHIFT, Key::Home);
h.run_steps(2);
assert!(h.get_by_label("Alignment").accesskit_node().is_disabled());
let button = h.get_by_label("Alignment").rect().center();
click_at(&mut h, button);
assert_eq!(h.state().views[0].session.line_editor.as_ref().unwrap().text, before);
assert_eq!(row_rect(&h, "One"), first);
assert_eq!(row_rect(&h, "nine"), last);
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), None);
h.key_press(Key::Escape);
h.run_steps(3);
assert_eq!(row_rect(&h, "One"), first);
assert_eq!(row_rect(&h, "nine"), last);
}
/// Clearing the selection restores ordinary whole-paragraph alignment and its undo step.
#[test]
fn whole_paragraph_alignment_stays_available_without_a_row_selection() {
let mut h = three_row_paragraph();
h.key_press_modifiers(Modifiers::COMMAND, Key::End);
h.key_press_modifiers(Modifiers::SHIFT, Key::Home);
h.run_steps(2);
assert!(h.get_by_label("Alignment").accesskit_node().is_disabled());
h.key_press(Key::End);
h.run_steps(2);
assert!(!h.get_by_label("Alignment").accesskit_node().is_disabled());
align_by_pointer(&mut h, "Right align text");
assert!(h.state().views[0].session.line_editor.is_some());
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), None);
}
/// Redo after Undo in the box puts the typing back, in the box: it doesn't reach the document,
/// whose own Redo (a formatting change waiting here) must stay waiting.
#[test]
fn redo_after_undoing_typing_puts_the_typing_back() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
open_paragraph(&mut h);
h.get_by_label("Bold").click();
h.run_steps(4);
// Undo the bold (closing the editor), so the document has it to redo.
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(4);
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_redo(), Some("Edit text"));
open_paragraph(&mut h);
let original = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
h.key_press_modifiers(Modifiers::COMMAND, Key::End);
h.event(egui::Event::Text(" more".into()));
h.run_steps(2);
let typed = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(h.state().views[0].session.line_editor.as_ref().unwrap().text, original, "the typing is taken back");
h.key_press_modifiers(Modifiers::COMMAND | Modifiers::SHIFT, Key::Z);
h.run_steps(3);
let s = h.state();
let ed = s.views[0].session.line_editor.as_ref().expect("still typing");
assert_eq!(ed.text, typed, "Redo puts the typing back");
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!((doc.can_redo(), doc.text_blocks(0)[0].base_font.as_str()), (Some("Edit text"), "Helvetica"), "the document's Redo still waits");
}
/// Redo from the menu or toolbar with typing in the box the page doesn't have yet. Closing the
/// box first applied the typing, a new edit that dropped the document's waiting Redo, and the
/// Redo itself then failed: the step was gone. The typing now stays in the box and the Redo
/// waits; once Undo in the box takes the typing back, Redo from the menu puts it back there.
#[test]
fn redo_from_the_menu_with_typing_in_the_box_keeps_the_documents_redo() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
open_paragraph(&mut h);
h.get_by_label("Bold").click();
h.run_steps(4);
// Undo the bold (closing the editor), so the document has it to redo.
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(4);
open_paragraph(&mut h);
let original = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
h.key_press_modifiers(Modifiers::COMMAND, Key::End);
h.event(egui::Event::Text(" more".into()));
h.run_steps(2);
let typed = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
assert!(h.execute("edit.redo"));
h.run_steps(3);
{
let s = h.state();
let ed = s.views[0].session.line_editor.as_ref().expect("the box stays open");
assert_eq!(ed.text, typed, "the typing stays in the box, unapplied");
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!((doc.can_redo(), doc.text_blocks(0)[0].base_font.as_str()), (Some("Edit text"), "Helvetica"), "the document's Redo still waits");
}
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(h.state().views[0].session.line_editor.as_ref().unwrap().text, original, "Undo takes the typing back in the box");
assert!(h.execute("edit.redo"));
h.run_steps(3);
let s = h.state();
assert_eq!(s.views[0].session.line_editor.as_ref().expect("still open").text, typed, "Redo from the menu puts the typing back");
assert_eq!(s.session.get(s.views[0].id).unwrap().can_redo(), Some("Edit text"), "the document's Redo still waits");
}
/// Undo from the menu or toolbar with typing in the box the page doesn't have yet: the box takes
/// the typing back, as the key does, and stays open, and the document's waiting Redo stays. The
/// menu applied the typing first (a new edit, which dropped that Redo), closed the box and undid
/// the typing all at once.
#[test]
fn undo_from_the_menu_takes_back_typing_in_the_box() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
open_paragraph(&mut h);
h.get_by_label("Bold").click();
h.run_steps(4);
// Undo the bold (closing the editor), so the document has it to redo and nothing to undo.
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(4);
open_paragraph(&mut h);
let original = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
h.key_press_modifiers(Modifiers::COMMAND, Key::End);
h.event(egui::Event::Text(" more".into()));
h.run_steps(2);
h.key_press_modifiers(Modifiers::SHIFT, Key::Home);
h.run_steps(2);
assert!(h.state().views[0].session.line_editor.as_ref().unwrap().selection.is_some());
h.event(egui::Event::PointerMoved(egui::pos2(10.0, 10.0)));
h.ctx.memory_mut(|m| m.request_focus(egui::Id::unique("test-menu")));
assert!(h.execute("edit.undo"), "Undo is enabled for the box's typing");
assert_eq!(
h.state().views[0].session.line_editor.as_ref().unwrap().selection,
None,
"menu history clears the old byte range before the next frame"
);
h.run_steps(3);
let s = h.state();
let ed = s.views[0].session.line_editor.as_ref().expect("the box stays open");
assert_eq!(ed.text, original, "the typing is taken back in the box");
let doc = s.session.get(s.views[0].id).unwrap();
assert_eq!((doc.can_redo(), doc.can_undo()), (Some("Edit text"), None), "the document is untouched");
}
/// Redo from the menu after Undo in the box took the typing back, with nothing for the document
/// to redo: the menu item follows the box's own Redo too, and puts the typing back. It was
/// disabled, as it followed only the document's Redo, and only the key could do it.
#[test]
fn redo_from_the_menu_is_enabled_for_the_boxs_own_typing() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
open_paragraph(&mut h);
let original = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
h.key_press_modifiers(Modifiers::COMMAND, Key::End);
h.event(egui::Event::Text(" more".into()));
h.run_steps(2);
let typed = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(h.state().views[0].session.line_editor.as_ref().unwrap().text, original, "the typing is taken back");
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_redo(), None, "the document has nothing to redo");
// A range retained while the unfocused editor still showed its previous text.
h.state_mut().views[0].session.line_editor.as_mut().unwrap().selection = Some(craft::text::ui::editor::TextSelection::new(0, 1));
h.event(egui::Event::PointerMoved(egui::pos2(10.0, 10.0)));
h.ctx.memory_mut(|m| m.request_focus(egui::Id::unique("test-menu")));
assert!(h.execute("edit.redo"), "Redo is enabled for the box's own step");
assert_eq!(
h.state().views[0].session.line_editor.as_ref().unwrap().selection,
None,
"menu history clears the old byte range before the next frame"
);
h.run_steps(3);
assert_eq!(h.state().views[0].session.line_editor.as_ref().expect("still open").text, typed, "Redo puts the typing back");
}
/// Typing in tab A's paragraph, a paragraph opened in tab B, then back to A: Edit ▸ Undo takes
/// the typing back in A's box. The tabs share one text box, and its history was cleared each time
/// another tab's box took it, so Undo found no step and closed A's paragraph instead
/// (skylartaylor's and FredApps's review of 08ec1db7).
#[test]
fn undo_after_a_round_trip_through_another_tab_takes_back_the_typing() {
let mut h = harness(1, |app| {
app.open_bytes(&egui::Context::default(), "copy.pdf", None, fixture(1)).expect("the copy opens");
});
let open_in = |h: &mut Harness<'static, PdfCraftApp>, tab: usize| {
h.state_mut().active = Some(tab);
h.run_steps(2);
// Each tab keeps its own zoom; keep its paragraph clear of the lower edge.
h.set_option("zoom", "75").unwrap();
h.run_steps(2);
if h.state().views[tab].session.line_editor.is_none() {
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
}
let r = h.state().views[tab].page_screen_rect(0).expect("on screen");
click_at(h, egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height()));
assert!(h.state().views[tab].session.line_editor.is_some(), "the paragraph opens for typing");
};
open_in(&mut h, 0);
h.get_by_label("Bold").click();
h.run_steps(4);
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(4);
open_in(&mut h, 0);
let original = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
h.key_press_modifiers(Modifiers::COMMAND, Key::End);
h.event(egui::Event::Text(" more".into()));
h.run_steps(2);
assert!(h.state().views[0].session.line_editor.as_ref().unwrap().text.ends_with(" more"));
open_in(&mut h, 1);
h.state_mut().active = Some(0);
h.run_steps(3);
assert!(h.state().views[0].session.line_editor.is_some(), "A's paragraph is still open after the round trip");
assert!(h.execute("edit.undo"), "Undo is enabled");
h.run_steps(3);
let s = h.state();
let ed = s.views[0].session.line_editor.as_ref().expect("the box stays open: Undo takes back the typing");
assert_eq!(ed.text, original, "the typing is taken back in the box");
assert_eq!(s.session.get(s.views[0].id).unwrap().can_redo(), Some("Edit text"), "the document's Redo still waits");
}
/// Typing taken back with Ctrl+Z, then Bold applied with the paragraph still open: Ctrl+Shift+Z
/// must not bring the typing back on top of the bold, as the box's history is cleared once the
/// page has its text. The format left the text as it was, so the box kept its history and its
/// Redo put "Page 1 more" back (FredApps's review of abc378c0).
#[test]
fn redo_after_formatting_never_brings_back_typing_undone_before_it() {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
open_paragraph(&mut h);
let original = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
h.key_press_modifiers(Modifiers::COMMAND, Key::End);
h.event(egui::Event::Text(" more".into()));
h.run_steps(2);
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(h.state().views[0].session.line_editor.as_ref().unwrap().text, original, "the typing is taken back");
h.get_by_label("Bold").click();
h.run_steps(4);
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().can_undo(), Some("Edit text"), "the bold is applied");
h.key_press_modifiers(Modifiers::COMMAND | Modifiers::SHIFT, Key::Z);
h.run_steps(3);
let text = h.state().views[0].session.line_editor.as_ref().map(|ed| ed.text.clone());
assert_eq!(text.as_deref(), Some(original.as_str()), "the typing undone before the bold stays undone");
// Nor through what the menu's Redo runs.
h.state_mut().redo(&egui::Context::default());
h.run_steps(3);
let s = h.state();
let text = s.views[0].session.line_editor.as_ref().map(|ed| ed.text.clone());
assert!(!text.as_deref().unwrap_or_default().contains("more"), "the menu's Redo brings it back: {text:?}");
assert_eq!(s.session.get(s.views[0].id).unwrap().text_blocks(0)[0].base_font, "Helvetica-Bold", "the bold stays");
}
/// Edit ▸ Undo chosen in the same frame as the switch back to tab A, before A's box is drawn
/// again: the shared text box still holds tab B's history then. Stepping through it put B's
/// typing into A's box; the box now claims its own history back first (CodeRabbit on abc378c0).
#[test]
fn undo_in_the_frame_of_the_switch_back_takes_back_the_typing() {
let mut h = harness(1, |app| {
app.open_bytes(&egui::Context::default(), "copy.pdf", None, fixture(1)).expect("the copy opens");
});
let open_in = |h: &mut Harness<'static, PdfCraftApp>, tab: usize| {
h.state_mut().active = Some(tab);
h.run_steps(2);
if h.state().views[tab].session.line_editor.is_none() {
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
}
let r = h.state().views[tab].page_screen_rect(0).expect("on screen");
click_at(h, egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height()));
assert!(h.state().views[tab].session.line_editor.is_some(), "the paragraph opens for typing");
};
open_in(&mut h, 0);
let original = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
h.key_press_modifiers(Modifiers::COMMAND, Key::End);
h.event(egui::Event::Text(" more".into()));
h.run_steps(2);
// In B, a paragraph whose text differs from A's ("Page 1 B", applied with Bold), so B's
// history can't pass for A's.
open_in(&mut h, 1);
h.key_press_modifiers(Modifiers::COMMAND, Key::End);
h.event(egui::Event::Text(" B".into()));
h.run_steps(2);
h.get_by_label("Bold").click();
h.run_steps(4);
assert_eq!(h.state().views[1].session.line_editor.as_ref().map(|ed| ed.text.as_str()), Some("Page 1 B"), "B's paragraph is open on its own text");
// Back to A and Undo at once: no frame draws A's box in between.
h.state_mut().active = Some(0);
assert!(h.execute("edit.undo"), "Undo is enabled");
h.run_steps(3);
let ed = h.state().views[0].session.line_editor.clone().expect("the box stays open: Undo takes back the typing");
assert_eq!(ed.text, original, "the typing is taken back in A's box");
}
/// Redo from the menu chosen in the same frame as a switch to tab B, whose paragraph is open on
/// the same text as A's: the shared text box still holds A's history then, with A's undone
/// typing to redo, so B's box claims its own history back before stepping.
#[test]
fn redo_in_the_frame_of_a_tab_switch_never_brings_another_tabs_typing() {
let mut h = harness(1, |app| {
app.open_bytes(&egui::Context::default(), "copy.pdf", None, fixture(1)).expect("the copy opens");
});
let open_in = |h: &mut Harness<'static, PdfCraftApp>, tab: usize| {
h.state_mut().active = Some(tab);
h.run_steps(2);
// Each tab keeps its own zoom; keep its paragraph clear of the lower edge.
h.set_option("zoom", "75").unwrap();
h.run_steps(2);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[tab].page_screen_rect(0).expect("on screen");
click_at(h, egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height()));
assert!(h.state().views[tab].session.line_editor.is_some(), "the paragraph opens for typing");
};
open_in(&mut h, 1);
open_in(&mut h, 0);
let original = h.state().views[0].session.line_editor.as_ref().unwrap().text.clone();
h.key_press_modifiers(Modifiers::COMMAND, Key::End);
h.event(egui::Event::Text(" secret".into()));
h.run_steps(2);
assert!(h.execute("edit.undo"));
h.run_steps(3);
assert_eq!(h.state().views[0].session.line_editor.as_ref().unwrap().text, original, "taken back in A");
// To B and Redo at once: no frame draws B's box in between.
h.state_mut().active = Some(1);
h.state_mut().redo(&egui::Context::default());
h.run_steps(3);
let s = h.state();
let b = s.views[1].session.line_editor.as_ref().map(|ed| ed.text.clone());
assert!(!b.as_deref().unwrap_or_default().contains("secret"), "A's typing came back in B: {b:?}");
assert!(!s.session.get(s.views[1].id).unwrap().text_blocks(0)[0].text.contains("secret"), "A's typing reached B's page");
}
/// Two copies of a document, the same paragraph open in each. The paragraph boxes share one text
/// box, and so one Undo history: a box opened on the same text as another tab's didn't clear it.
/// Typing in one tab, taken back with Undo, then came back with Redo (the key or the menu) in the
/// other tab.
#[test]
fn typing_in_one_tab_never_reaches_another_tabs_paragraph() {
let mut h = harness(1, |app| {
app.open_bytes(&egui::Context::default(), "copy.pdf", None, fixture(1)).expect("the copy opens");
});
let open_in = |h: &mut Harness<'static, PdfCraftApp>, tab: usize| {
h.state_mut().active = Some(tab);
h.run_steps(2);
// Each tab keeps its own zoom; keep its paragraph clear of the lower edge.
h.set_option("zoom", "75").unwrap();
h.run_steps(2);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[tab].page_screen_rect(0).expect("on screen");
click_at(h, egui::pos2(r.left() + 40.0 / 200.0 * r.width(), r.top() + (300.0 - 158.0) / 300.0 * r.height()));
assert!(h.state().views[tab].session.line_editor.is_some(), "the paragraph opens for typing");
};
open_in(&mut h, 1);
let original = h.state().views[1].session.line_editor.as_ref().unwrap().text.clone();
open_in(&mut h, 0);
assert_eq!(h.state().views[0].session.line_editor.as_ref().unwrap().text, original, "the same paragraph");
h.key_press_modifiers(Modifiers::COMMAND, Key::End);
h.event(egui::Event::Text(" secret".into()));
h.run_steps(2);
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(h.state().views[0].session.line_editor.as_ref().unwrap().text, original, "taken back");
// The copy, its paragraph still open: Redo there has nothing of its own to put back.
h.state_mut().active = Some(1);
h.run_steps(3);
assert!(h.state().views[1].session.line_editor.is_some(), "the copy's paragraph is still open");
h.key_press_modifiers(Modifiers::COMMAND | Modifiers::SHIFT, Key::Z);
h.run_steps(3);
let text = h.state().views[1].session.line_editor.as_ref().expect("the copy's paragraph stays open through Redo").text.clone();
assert!(!text.contains("secret"), "the other tab's typing came in: {text:?}");
// Nor through what the menu's Redo runs, which reads the box's history itself (the menu item
// is off here, as the copy's document has nothing to redo). With no Redo of its own, the box
// passes the step to the document and closes, so the page's text is checked too.
h.state_mut().redo(&egui::Context::default());
h.run_steps(3);
let s = h.state();
let text = s.views[1].session.line_editor.as_ref().map(|ed| ed.text.clone());
assert!(!text.as_deref().unwrap_or_default().contains("secret"), "the other tab's typing came in from the menu: {text:?}");
assert_eq!(s.session.get(s.views[1].id).unwrap().text_blocks(0)[0].text, s.session.get(s.views[0].id).unwrap().text_blocks(0)[0].text);
}
/// A paragraph selected by its frame goes with one Delete (or Backspace), as in Acrobat, instead
/// of being emptied a typed character at a time, and undo brings it back.
#[test]
fn a_paragraph_selected_by_its_frame_deletes_with_one_key() {
for key in [Key::Delete, Key::Backspace] {
let mut h = harness(2, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let blocks = |h: &Harness<'static, PdfCraftApp>| h.state().session.get(id).unwrap().text_blocks(0);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
let screen = |x: f64, y: f64| egui::pos2(r.left() + x as f32 * k, r.top() + (300.0 - y as f32) * k);
let b = blocks(&h)[0].clone();
click_at(&mut h, screen((b.rect[0] + b.rect[2]) / 2.0, (b.rect[1] + b.rect[3]) / 2.0));
assert!(h.state().views[0].session.line_editor.is_some(), "a click on the text opens it for typing");
click_at(&mut h, screen((b.rect[0] + b.rect[2]) / 2.0, b.rect[3]) - egui::vec2(0.0, 4.0));
assert!(h.state().views[0].session.line_editor.is_none(), "the frame selects the box");
press(&mut h, Modifiers::NONE, key);
assert!(blocks(&h).is_empty(), "{key:?} deletes the whole paragraph: {:?}", blocks(&h));
assert!(h.state().views[0].session.line_editor.is_none(), "no empty editor is left behind");
assert!(h.query_all_by_label_contains("object selected").next().is_none(), "the deleted paragraph is no longer selected");
assert_eq!(h.state().session.get(id).unwrap().text_blocks(1)[0].text, "Page 2", "the next page keeps its text");
assert_eq!(h.state().session.get(id).unwrap().info.pages.len(), 2, "Delete removed the paragraph, not a page");
assert!(h.execute("edit.undo"));
h.run_steps(2);
assert_eq!(blocks(&h).iter().map(|b| b.text.as_str()).collect::<Vec<_>>(), ["Page 1"], "undo restores it");
}
}
fn two_image_fixture() -> Vec<u8> {
let content = "q 20 0 0 20 100 100 cm /Im0 Do Q q 20 0 0 20 20 20 cm /Im0 Do Q";
let objs: Vec<String> = vec![
"<< /Type /Catalog /Pages 2 0 R >>".into(),
"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".into(),
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Contents 4 0 R /Resources << /XObject << /Im0 5 0 R >> >> >>".into(),
format!("<< /Length {} >>\nstream\n{content}\nendstream", content.len()),
"<< /Type /XObject /Subtype /Image /Width 2 /Height 2 /ColorSpace /DeviceGray /BitsPerComponent 8 /Length 4 >>\nstream\n\0\0\0\0\nendstream"
.into(),
];
let mut out = b"%PDF-1.7\n".to_vec();
let mut offsets = Vec::new();
for (i, o) in objs.iter().enumerate() {
offsets.push(out.len());
out.extend_from_slice(format!("{} 0 obj\n{o}\nendobj\n", i + 1).as_bytes());
}
let xref = out.len();
out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
for o in offsets {
out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
}
out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes());
out
}
#[test]
fn duplicate_xobject_at_different_rects_arrange_and_delete_keeps_other_instance_through_menu() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1400.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "dup.pdf", None, two_image_fixture()).expect("opens fixture");
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let xf = h.state().views[0].page_xform(0).expect("page 0 xform");
let imgs = h.state().session.get(id).unwrap().page_images(0);
// B is at index 0 [100, 100, 120, 120], in the back
let b0 = pdfcraft_ui_egui::canvas::image_screen_rect(&imgs[0], &xf, &h.state().session.get(id).unwrap().info, 0);
let p_b = b0.center();
// 1. Select B (image 0)
h.hover_at(p_b);
h.run_steps(1);
h.drag_at(p_b);
h.run_steps(1);
h.drop_at(p_b);
h.run_steps(2);
assert_eq!(h.state().views[0].session.image_selection.as_ref().map(|s| s.index), Some(0), "image 0 (B) selected");
// 2. Right-click B (image 0) to open context menu
h.hover_at(p_b);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: p_b, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: p_b, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
// 3. Open "Arrange image" submenu and click "Bring to front"
h.get_by_label("Arrange image").click();
h.run_steps(2);
h.get_by_label("Bring to front").click();
h.run_steps(3);
// 4. Selection must be preserved after arrange: B is now at index 1 (in front)
let sel = h.state().views[0].session.image_selection.as_ref().map(|s| s.index);
assert_eq!(sel, Some(1), "selection must track B to index 1 after arrange");
// 5. Right-click on the selected image (B at index 1) again to open context menu
let sel_idx = sel.unwrap();
let xf_again = h.state().views[0].page_xform(0).expect("page 0 xform");
let cur_imgs = h.state().session.get(id).unwrap().page_images(0);
let b_rect = pdfcraft_ui_egui::canvas::image_screen_rect(&cur_imgs[sel_idx], &xf_again, &h.state().session.get(id).unwrap().info, 0);
let p_b2 = b_rect.center();
h.hover_at(p_b2);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: p_b2, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: p_b2, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
// 6. Click "Delete" in context menu
h.get_by_label("Delete").click();
h.run_steps(3);
// 7. Save, reopen and verify: A remains at [20, 20, 40, 40], B is deleted
let saved = h.state().session.save_bytes(id).unwrap();
let mut reopened = pdfcraft_engine::Session::new();
let rid = reopened.open("again.pdf", None, saved, None).unwrap();
let imgs = reopened.get(rid).unwrap().page_images(0);
assert_eq!(imgs.len(), 1, "exactly one image must remain after deleting B");
assert!((imgs[0].rect[0] - 20.0).abs() < 1e-2, "remaining image must be A at [20, 20, 40, 40], was {:?}", imgs[0].rect);
}
/// Esc in an open paragraph leaves its text and selects its box, as in Acrobat, so Esc and
/// Delete remove it without aiming for the thin frame; the unapplied draft is discarded.
#[test]
fn escape_in_an_open_paragraph_selects_its_box_for_delete() {
for draft in [None, Some("Page one")] {
let mut h = harness(1, |_| {});
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let id = h.state().views[0].id;
let blocks = |h: &Harness<'static, PdfCraftApp>| h.state().session.get(id).unwrap().text_blocks(0);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
let k = r.width() / 200.0;
let b = blocks(&h)[0].clone();
click_at(
&mut h,
egui::pos2(r.left() + ((b.rect[0] + b.rect[2]) / 2.0) as f32 * k, r.top() + (300.0 - (b.rect[1] + b.rect[3]) / 2.0) as f32 * k),
);
assert!(h.state().views[0].session.line_editor.is_some(), "a click inside the paragraph opens it for typing");
if let Some(text) = draft {
h.state_mut().views[0].session.line_editor.as_mut().unwrap().text = text.into();
h.run_steps(1);
}
press(&mut h, Modifiers::NONE, Key::Escape);
assert!(h.state().views[0].session.line_editor.is_none(), "Esc leaves the text");
h.get_by_label("1 object selected");
assert_eq!(blocks(&h)[0].text, "Page 1", "the draft {draft:?} is discarded");
press(&mut h, Modifiers::NONE, Key::Delete);
assert!(blocks(&h).is_empty(), "Esc then Delete removes the paragraph: {:?}", blocks(&h));
// One undo step brings it back.
assert!(h.execute("edit.undo"));
h.run_steps(2);
assert_eq!(blocks(&h)[0].text, "Page 1");
}
}
/// #3005: a table drawn inside one Form XObject is a single piece of artwork in Edit text &
/// images. Its right-click menu offers Ungroup, which draws the form in place so the table's
/// text can be edited; one Undo puts the form back.
#[test]
fn ungroup_in_the_artwork_menu_makes_a_forms_text_editable() {
let table = b"BT /F1 14 Tf 1 0 0 1 40 200 Tm (Revenue 153,300.00) Tj 1 0 0 1 40 170 Tm (Costs 382,110.00) Tj ET";
let objs: Vec<Vec<u8>> = vec![
b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(),
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(),
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 400 300] /Contents 4 0 R /Resources << /XObject << /Fm1 5 0 R >> >> >>".to_vec(),
pdf_stream("<<", b"q /Fm1 Do Q"),
pdf_stream(
"<< /Type /XObject /Subtype /Form /BBox [0 0 400 300] /Group << /S /Transparency >> /Resources << /Font << /F1 6 0 R >> >>",
table,
),
b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>".to_vec(),
];
let pdf = pdf_of_objects(&objs);
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "table.pdf", None, pdf.clone()).expect("opens");
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
let blocks = |h: &Harness<'static, PdfCraftApp>| h.state().session.get(id).unwrap().text_blocks(0).len();
assert_eq!(blocks(&h), 0, "the table's text is inside the form");
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
// An empty spot of the form: the artwork, not a paragraph.
let at = r.left_top() + egui::vec2(r.width() * 0.8, r.height() * 0.85);
right_click_at(&mut h, at);
h.run_steps(1);
let selection = h.state().views[0].session.image_selection.as_ref().map(|s| (s.page, s.index));
assert_eq!(selection, Some((0, 0)), "the right-click selects the form");
assert!(h.query_by_label("Ungroup").is_some(), "the artwork menu offers Ungroup");
// The page's own menu opens over the artwork menu (#2637, fixed by #2155), so the item is
// applied the way it queues its edit rather than clicked through the overlap.
h.state_mut().views[0].session.pending_edit =
Some(pdfcraft_engine::Edit::EditPageImage { page: 0, index: 0, change: pdfcraft_engine::ImageEdit::Ungroup });
h.run_steps(4);
assert!(blocks(&h) >= 1, "the table's rows are page text now");
let images = h.state().session.get(id).unwrap().page_images(0);
assert!(images.is_empty(), "no artwork left: {images:?}");
assert!(h.execute("edit.undo"));
h.run_steps(3);
assert_eq!(blocks(&h), 0, "undo puts the form back");
}
/// A stream object `dict` (its opening `<<` and entries, without `/Length` or `>>`) holding `data`.
fn pdf_stream(dict: &str, data: &[u8]) -> Vec<u8> {
[format!("{dict} /Length {} >>\nstream\n", data.len()).as_bytes(), data, b"\nendstream"].concat()
}
/// A PDF of `objs`, numbered from 1, with the first as its catalog.
fn pdf_of_objects(objs: &[Vec<u8>]) -> Vec<u8> {
let mut pdf = b"%PDF-1.7\n".to_vec();
let mut offsets = Vec::new();
for (i, o) in objs.iter().enumerate() {
offsets.push(pdf.len());
pdf.extend_from_slice(format!("{} 0 obj\n", i + 1).as_bytes());
pdf.extend_from_slice(o);
pdf.extend_from_slice(b"\nendobj\n");
}
let xref = pdf.len();
pdf.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
for o in offsets {
pdf.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
}
pdf.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes());
pdf
}
/// Grouped text is not prepared while the pages are only looked at: entering Edit text and
/// scrolling change nothing. One click on the group's text prepares that page, as one undoable
/// step, and opens the paragraph clicked for typing. A click on a grouped logo selects it, as a
/// click on any picture does, and changes nothing; a drag from the text group prepares nothing.
#[test]
fn one_click_on_grouped_text_opens_it_for_editing() {
let objs: Vec<Vec<u8>> = vec![
b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(),
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(),
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 400 300] /Contents 4 0 R /Resources << /XObject << /Body 5 0 R /Logo 7 0 R >> >> >>".to_vec(),
pdf_stream("<<", b"q /Body Do Q q 1 0 0 1 300 20 cm /Logo Do Q"),
pdf_stream(
"<< /Type /XObject /Subtype /Form /BBox [0 0 280 300] /Resources << /Font << /F1 6 0 R >> >>",
b"BT /F1 24 Tf 1 0 0 1 40 200 Tm (Grouped words) Tj ET",
),
b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>".to_vec(),
pdf_stream("<< /Type /XObject /Subtype /Form /BBox [0 0 60 60] /Resources << >>", b"0 0 1 rg 0 0 60 60 re f"),
];
let pdf = pdf_of_objects(&objs);
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "grouped.pdf", None, pdf.clone()).expect("opens");
app
}
});
h.run_steps(4);
let id = h.state().views[0].id;
let doc = |h: &Harness<'static, PdfCraftApp>| {
let d = h.state().session.get(id).unwrap();
(d.text_blocks(0).len(), d.page_images(0).len(), d.can_undo().map(str::to_string))
};
assert!(h.execute("edit.edit_text"));
h.run_steps(6);
assert_eq!(doc(&h), (0, 2, None), "nothing is prepared while the page is only shown");
let r = h.state().views[0].page_screen_rect(0).expect("on screen");
// User space (x, y) on the page, which is 400 by 300 points and upright.
let at = |x: f32, y: f32| r.left_top() + egui::vec2(r.width() * x / 400.0, r.height() * (300.0 - y) / 300.0);
click_at(&mut h, at(330.0, 50.0));
let logo = h.state().session.get(id).unwrap().page_images(0).iter().position(|i| i.name == "Logo");
assert_eq!(h.state().views[0].session.image_selection.as_ref().map(|s| (s.page, s.index)), logo.map(|i| (0, i)), "the logo is selected");
assert_eq!(doc(&h), (0, 2, None), "a click on artwork without text changes nothing");
assert!(h.state().views[0].session.line_editor.is_none());
drag(&mut h, at(60.0, 208.0), at(120.0, 150.0));
assert_eq!(doc(&h).0, 0, "a drag on the text group prepares nothing");
click_at(&mut h, at(60.0, 208.0));
h.run_steps(2);
assert_eq!(doc(&h), (1, 1, Some("Prepare grouped text".into())), "the page's grouped text is prepared, in one step");
let editing = h.state().views[0].session.line_editor.as_ref().map(|e| e.text.clone());
assert_eq!(editing.as_deref(), Some("Grouped words"), "the paragraph clicked opens for typing");
assert!(h.execute("edit.undo"));
h.run_steps(3);
let (blocks, images, _) = doc(&h);
assert_eq!((blocks, images), (0, 2), "undo puts the group back");
}
#[test]
fn outward_image_grip_drags_grow_on_rotated_pages() {
for rotation in [0, 90, 180, 270] {
for image_turn in [0.0, 25.0] {
for grip in 0..8 {
let mut png = Vec::new();
image::RgbImage::from_pixel(100, 80, image::Rgb([100, 150, 200]))
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
.unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "grips.png", None, png.clone()).unwrap();
app
}
});
h.run_steps(4);
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::EditPageImage { page: 0, index: 0, change: pdfcraft_engine::ImageEdit::Move([20.0, 20.0, 80.0, 60.0]) },
);
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(&ctx_h, pdfcraft_engine::Edit::RotatePages { pages: vec![0], degrees: rotation });
if image_turn != 0.0 {
let ctx_h = h.ctx.clone();
h.state_mut().apply_edit(
&ctx_h,
pdfcraft_engine::Edit::EditPageImage { page: 0, index: 0, change: pdfcraft_engine::ImageEdit::RotateBy(image_turn) },
);
}
h.state_mut().views[0].view.fit = pdfcraft_ui_egui::canvas::Fit::Page;
assert!(h.execute("edit.edit_text"));
h.state_mut().views[0].session.image_selection = Some(pdfcraft_ui_egui::ImageSelection::at(0, 0));
h.run_steps(3);
let before = h.state().session.get(h.state().views[0].id).unwrap().page_images(0)[0].clone();
let corners = picture_corners(&h, 0, 0);
let centre = corners[0].lerp(corners[2], 0.5);
let grips = grip_points(corners);
let opposite_grip = (grip % 4 + 2) % 4 + 4 * (grip / 4);
let (start, opposite) = (grips[grip], grips[opposite_grip]);
let finish = start + (start - centre) * 0.15;
let viewport = h.state().views[0].viewport_rect();
assert!(viewport.contains(start) && viewport.contains(finish), "test grips must be visible");
h.hover_at(start);
h.run_steps(1);
h.drag_at(start);
h.run_steps(1);
let started = h.state().views[0].session.image_selection.clone();
h.hover_at(finish);
h.run_steps(2);
let moved = h.state().views[0].session.image_selection.clone();
h.drop_at(finish);
h.run_steps(3);
let after = h.state().session.get(h.state().views[0].id).unwrap().page_images(0)[0].clone();
let after_grips = grip_points(picture_corners(&h, 0, 0));
assert!(
after_grips[opposite_grip].distance(opposite) < 1.0,
"fixed grip moved: rotation={rotation} image_turn={image_turn} grip={grip}"
);
assert!(
after_grips[grip].distance(finish) < 1.0,
"dragged grip missed pointer: rotation={rotation} image_turn={image_turn} grip={grip}"
);
let area = |m: [f64; 6]| (m[0] * m[3] - m[1] * m[2]).abs();
assert!(
area(after.matrix) > area(before.matrix) * 1.05,
"outward grip shrank or ignored: rotation={rotation} image_turn={image_turn} grip={grip}: {:?} => {:?}; started={started:?} moved={moved:?}",
before.matrix,
after.matrix
);
}
}
}
}
#[test]
fn author_image_clip_is_not_offered_as_a_removable_editor_crop() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "author-clip.pdf", None, clipped_artwork_with_labels()).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.state_mut().views[0].session.image_selection = Some(pdfcraft_ui_egui::ImageSelection::at(0, 0));
h.run_steps(3);
let view = &h.state().views[0];
let doc = h.state().session.get(view.id).unwrap();
let image = doc.page_images(0)[0].clone();
let visible = image.visible_rect.unwrap();
let xf = view.page_xform(0).unwrap();
let bounds = pdfcraft_ui_egui::canvas::image_screen_rect(&image, &xf, &doc.info, 0);
let p = bounds.center();
h.hover_at(p);
h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
assert!(h.query_all_by_label("Remove crop").next().is_none(), "author clips are not editor crops");
h.get_by_label("Crop").click();
h.run_steps(2);
assert!(h.query_all_by_label("Remove Crop").next().is_none(), "the initial author clip has no removable draft crop");
let mut draft = visible;
draft[0] += (draft[2] - draft[0]) * 0.2;
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop.as_mut().unwrap().keep = draft;
h.run_steps(3);
h.get_by_label("Remove Crop").click();
h.run_steps(1);
assert_eq!(
h.state().views[0].session.image_selection.as_ref().unwrap().crop.as_ref().unwrap().keep,
visible,
"draft reset preserves the author's clip"
);
h.get_by_label("Done").click();
h.run_steps(2);
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().page_images(0)[0].visible_rect, Some(visible));
}
fn affine_review_image_fixture(matrix: [f64; 6], clip: Option<[f64; 4]>, rotation: i64) -> Vec<u8> {
use pdfcraft_cos::{Dict, Document, Object, Stream};
let mut doc = Document::open(std::sync::Arc::new(fixture(1))).unwrap();
let page = pdfcraft_model::pages(&doc).remove(0);
let mut image = Dict::new();
image.set(b"Type".to_vec(), Object::name("XObject"));
image.set(b"Subtype".to_vec(), Object::name("Image"));
image.set(b"Width".to_vec(), Object::Int(1));
image.set(b"Height".to_vec(), Object::Int(1));
image.set(b"BitsPerComponent".to_vec(), Object::Int(8));
image.set(b"ColorSpace".to_vec(), Object::name("DeviceRGB"));
let image = doc.add(Object::Stream(Stream::flate(image, &[220, 80, 50])));
let mut xobjects = Dict::new();
xobjects.set(b"ImReview".to_vec(), Object::Ref(image));
let mut resources = Dict::new();
resources.set(b"XObject".to_vec(), Object::Dict(xobjects));
let mut content = String::from("q ");
if let Some([x0, y0, x1, y1]) = clip {
content.push_str(&format!("{x0} {y0} {} {} re W n ", x1 - x0, y1 - y0));
}
let [a, b, c, d, e, f] = matrix;
content.push_str(&format!("{a} {b} {c} {d} {e} {f} cm /ImReview Do Q"));
let stream = doc.add(Object::Stream(Stream::flate(Dict::new(), content.as_bytes())));
doc.update_dict(page.obj, |dict| {
dict.set(b"Contents".to_vec(), Object::Ref(stream));
dict.set(b"Resources".to_vec(), Object::Dict(resources));
dict.set(b"MediaBox".to_vec(), Object::Array(vec![Object::Int(0), Object::Int(0), Object::Int(360), Object::Int(360)]));
dict.set(b"Rotate".to_vec(), Object::Int(rotation));
})
.unwrap();
pdfcraft_cos::write_incremental(&doc, &Default::default()).unwrap()
}
fn affine_review_harness(bytes: Vec<u8>) -> Harness<'static, PdfCraftApp> {
let mut h = Harness::builder().with_size(egui::vec2(1200.0, 1000.0)).build_eframe(move |cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "affine-review.pdf", None, bytes.clone()).unwrap();
app
}
});
h.run_steps(4);
h.state_mut().views[0].view.fit = pdfcraft_ui_egui::canvas::Fit::Page;
assert!(h.execute("edit.edit_text"));
h.state_mut().views[0].session.image_selection = Some(pdfcraft_ui_egui::ImageSelection::at(0, 0));
h.run_steps(3);
h
}
#[test]
fn affine_review_resize_out_and_back_preserves_saved_placement() {
for (matrix, clip, rotation) in [
([100.0, 0.0, 0.0, 100.0, 100.0, 100.0], None, 0),
([100.0, 0.0, 50.0, 100.0, 100.0, 100.0], None, 0),
([86.60254, 50.0, -50.0, 86.60254, 150.0, 100.0], None, 0),
([100.0, 0.0, 0.0, 100.0, 100.0, 100.0], Some([100.0, 100.0, 150.0, 200.0]), 90),
] {
for offset in [egui::Vec2::ZERO, egui::vec2(3.0, 4.0)] {
let mut h = affine_review_harness(affine_review_image_fixture(matrix, clip, rotation));
let view = &h.state().views[0];
let id = view.id;
let doc = h.state().session.get(id).unwrap();
let original = doc.page_images(0)[0].matrix;
let before = h.state().session.save_bytes(id).unwrap();
let start = picture_corners(&h, 0, 0)[2] + offset;
h.hover_at(start);
h.run_steps(1);
h.drag_at(start);
h.run_steps(1);
h.hover_at(start + egui::vec2(30.0, -25.0));
h.run_steps(2);
h.hover_at(start);
h.run_steps(2);
h.drop_at(start);
h.run_steps(3);
let saved = h.state().session.save_bytes(id).unwrap();
assert!(saved == before, "out-and-back must not create an edit: matrix={matrix:?} clip={clip:?} rotation={rotation} offset={offset:?}");
assert_eq!(h.state().session.get(id).unwrap().can_undo(), None);
let mut reopened = pdfcraft_engine::Session::new();
let reopened_id = reopened.open("saved.pdf", None, saved, None).unwrap();
assert_eq!(reopened.get(reopened_id).unwrap().page_images(0)[0].matrix, original);
}
}
}
#[test]
fn affine_review_cropped_rotation_preview_matches_saved_corners() {
let matrix = [70.710678, 70.710678, -70.710678, 70.710678, 170.710678, 100.0];
for (clip, rotation) in [(None, 0), (Some([100.0, 170.0, 241.421356, 241.421356]), 0), (Some([100.0, 170.0, 241.421356, 241.421356]), 90)] {
let mut h = affine_review_harness(affine_review_image_fixture(matrix, clip, rotation));
let view = &h.state().views[0];
let id = view.id;
let doc = h.state().session.get(id).unwrap();
let image = doc.page_images(0)[0].clone();
let xf = view.page_xform(0).unwrap();
let visible = image.visible_rect.unwrap_or(image.rect);
let p = doc.info.pages[0].user_to_view(((visible[0] + visible[2]) * 0.5) as f32, ((visible[1] + visible[3]) * 0.5) as f32);
let centre = xf.norm_to_screen(p[0] / xf.pw, p[1] / xf.ph);
let start = knob_point(&h);
let original = picture_corners(&h, 0, 0);
let v = start - centre;
let finish = centre + egui::vec2(-v.y, v.x);
h.hover_at(start);
h.run_steps(1);
h.drag_at(start);
h.run_steps(1);
h.hover_at(finish);
h.run_steps(2);
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grabbing, "the picture is turning");
if clip.is_some()
&& rotation == 0
&& let Ok(directory) = std::env::var("PDFCRAFT_AFFINE_SHOTS")
{
h.render().unwrap().save(std::path::Path::new(&directory).join("affine-rotation-preview.png")).unwrap();
}
h.drop_at(finish);
h.run_steps(3);
if clip.is_some()
&& rotation == 0
&& let Ok(directory) = std::env::var("PDFCRAFT_AFFINE_SHOTS")
{
h.render().unwrap().save(std::path::Path::new(&directory).join("affine-rotation-committed.png")).unwrap();
}
let saved = h.state().session.save_bytes(id).unwrap();
let mut reopened = pdfcraft_engine::Session::new();
let reopened_id = reopened.open("saved.pdf", None, saved, None).unwrap();
let doc = reopened.get(reopened_id).unwrap();
let after = doc.page_images(0)[0].clone();
let committed = picture_corners(&h, 0, 0);
let turned = original.map(|c| centre + egui::vec2(-(c - centre).y, (c - centre).x));
let (cx, cy) = ((visible[0] + visible[2]) * 0.5, (visible[1] + visible[3]) * 0.5);
let expected = pdfcraft_content::Matrix(image.matrix).then(&pdfcraft_content::Matrix([0.0, -1.0, 1.0, 0.0, cx - cy, cy + cx])).0;
assert!(
after.matrix.into_iter().zip(expected).all(|(a, b)| (a - b).abs() < 1e-3),
"knob must commit the requested 90-degree turn around visible bounds"
);
assert!(
turned.iter().all(|t| committed.iter().any(|c| c.distance(*t) < 1.0)),
"the saved corners are the original corners turned a quarter about the centre: clip={clip:?} rotation={rotation}: {turned:?} != {committed:?}"
);
}
}
/// The Escape that closes a dialog opened from a menu while a paragraph is being typed is the
/// dialog's: the typing stays, in the open paragraph or applied to the page. It was thrown away.
#[test]
fn escape_closing_a_dialog_over_a_paragraph_being_typed_keeps_the_typing() {
let mut h = open_source_font_fixture();
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.run_steps(2);
let page = h.state().views[0].page_screen_rect(0).expect("on screen");
let serif = egui::pos2(page.left() + 25.0 / 300.0 * page.width(), page.top() + (300.0 - 228.0) / 300.0 * page.height());
h.hover_at(serif);
h.run_steps(1);
h.drag_at(serif);
h.run_steps(1);
h.drop_at(serif);
h.run_steps(3);
assert!(h.state().views[0].session.line_editor.is_some(), "the paragraph is open");
h.event(egui::Event::Text("typed ".into()));
h.run_steps(2);
crate::support::open_shortcuts_from_the_help_menu(&mut h);
h.key_press(egui::Key::Escape);
h.run_steps(3);
assert!(h.state().dialogs.is_empty(), "Escape closes the dialog");
let s = h.state();
let open = s.views[0].session.line_editor.as_ref().is_some_and(|e| e.text.contains("typed"));
let applied = s.session.get(s.views[0].id).unwrap().text_blocks(0).iter().any(|b| b.text.contains("typed"));
assert!(open || applied, "the typing stays");
}
/// Edit ▸ Select all from the in-window menu bar selects every page in Organize. The bar reads
/// as open on the pass after its click, and its ⌘A waits for the bar to let go of the keys, which
/// the chords leave to it: nothing was selected.
#[test]
fn edit_select_all_from_the_menu_bar_selects_every_page() {
let mut h = organize(3);
h.run_steps(2);
support::open_menu(&mut h, "Edit");
support::menu_row(&h, "Select all").click();
h.run_steps(4);
assert_eq!(h.state().views[0].target_pages(), [0, 1, 2], "Edit ▸ Select all selected every page");
}
/// Edit ▸ Cut from the in-window menu bar cuts the selected page in Organize: its Cut waits for
/// the bar, which reads as open on the pass after its click, to let go of the keys.
#[test]
fn edit_cut_from_the_menu_bar_cuts_the_page() {
let mut h = organize(3);
h.run_steps(2);
h.get_by_label("Page 2").click();
h.run_steps(2);
support::open_menu(&mut h, "Edit");
support::menu_row(&h, "Cut").click();
h.run_steps(4);
assert!(h.state().page_clipboard.is_some(), "Edit ▸ Cut put the page on the clipboard");
assert_eq!(h.state().session.get(h.state().views[0].id).unwrap().info.pages.len(), 2, "Edit ▸ Cut took the page out");
}
/// A native menu's Edit ▸ Paste with a page's context menu open closes the menu, which the click
/// never reached, and pastes the page; each Edit ▸ Paste pastes it once.
#[test]
fn a_late_paste_for_a_native_edit_paste_with_a_context_menu_open_pastes_the_page() {
let mut h = organize(3);
h.run_steps(2);
h.get_by_label("Page 2").click();
h.run_steps(2);
h.event(egui::Event::Copy);
h.run_steps(3);
assert!(h.state().page_clipboard.is_some(), "the page is copied");
let pages = |h: &Harness<'static, PdfCraftApp>| h.state().session.get(h.state().views[0].id).unwrap().info.pages.len();
h.get_by_label("Page 2").click_button(egui::PointerButton::Secondary);
h.run_steps(2);
assert!(h.query_by_role(Role::Menu).is_some(), "the page's context menu is open");
support::fire_menu(&mut h, "edit.paste");
h.run_steps(3);
assert!(h.query_by_role(Role::Menu).is_none(), "Edit ▸ Paste closed the context menu");
assert_eq!(pages(&h), 4, "Edit ▸ Paste pasted the page");
support::fire_menu(&mut h, "edit.paste");
h.run_steps(3);
assert_eq!(pages(&h), 5, "a second Edit ▸ Paste pasted it again");
}
/// A paste (⌘V's answer from the platform) behind a page's context menu stays there: it pastes no
/// page.
#[test]
fn a_paste_behind_a_context_menu_opened_after_edit_paste_pastes_nothing() {
let mut h = organize(3);
h.run_steps(2);
h.get_by_label("Page 2").click();
h.run_steps(2);
h.event(egui::Event::Copy);
h.run_steps(3);
assert!(h.state().page_clipboard.is_some(), "the page is copied");
let pages = |h: &Harness<'static, PdfCraftApp>| h.state().session.get(h.state().views[0].id).unwrap().info.pages.len();
support::fire_menu(&mut h, "edit.paste");
h.run_steps(2);
assert_eq!(pages(&h), 4, "Edit ▸ Paste pasted the page");
h.get_by_label("Page 2").click_button(egui::PointerButton::Secondary);
h.run_steps(2);
assert!(h.query_by_role(Role::Menu).is_some(), "the page's context menu is open");
h.event(egui::Event::Paste("clipboard text".into()));
h.run_steps(3);
assert_eq!(pages(&h), 4, "the paste behind the context menu pasted no page");
}
/// Edit ▸ Paste from the in-window menu bar pastes the copied page in Organize, where the clipboard
/// commands are the picked pages' (`page.paste`), once and whatever the system clipboard holds.
#[test]
fn edit_paste_from_the_menu_bar_pastes_the_copied_page() {
let mut h = organize(3);
h.run_steps(2);
h.get_by_label("Page 2").click();
h.run_steps(2);
h.event(egui::Event::Copy);
h.run_steps(3);
assert!(h.state().page_clipboard.is_some(), "the page is copied");
let pages = |h: &Harness<'static, PdfCraftApp>| h.state().session.get(h.state().views[0].id).unwrap().info.pages.len();
assert_eq!(pages(&h), 3);
support::open_menu(&mut h, "Edit");
support::menu_row(&h, "Paste").click();
h.run_steps(4);
assert_eq!(pages(&h), 4, "Edit ▸ Paste pasted the page");
}
#[test]
fn a_modified_click_away_from_a_cropped_picture_applies_its_crop() {
// Shift-, Ctrl- or Command-clicking away is still a click away: it applies the crop and lets
// the picture go, rather than taking the picture into a group and dropping its crop (#6422
// review).
for mods in [egui::Modifiers::SHIFT, egui::Modifiers::COMMAND] {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1100.0)).build_eframe(|cc| {
crate::support::install_runtime(&cc.egui_ctx);
{
let mut app = crate::control::new_app();
app.set_option(&egui::Context::default(), "language", "en").unwrap();
app.open_bytes(&egui::Context::default(), "room.pdf", None, picture_with_room_around()).unwrap();
app
}
});
h.run_steps(4);
assert!(h.execute("edit.edit_text"));
h.state_mut().views[0].view.fit = pdfcraft_ui_egui::canvas::Fit::Page;
h.run_steps(3);
let r = h.state().views[0].page_screen_rect(0).unwrap();
let at = |x: f32, y: f32| egui::pos2(r.left() + x / 200.0 * r.width(), r.top() + (300.0 - y) / 300.0 * r.height());
let doc_id = h.state().views[0].id;
let full = h.state().session.get(doc_id).unwrap().page_images(0)[0].rect;
click_at(&mut h, at(100.0, 190.0));
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop = Some(pdfcraft_ui_egui::CropMode::new(full));
h.run_steps(2);
let keep = [full[0], full[1], full[0] + 60.0, full[3]];
h.state_mut().views[0].session.image_selection.as_mut().unwrap().crop.as_mut().unwrap().keep = keep;
h.run_steps(2);
let p = at(100.0, 60.0);
h.hover_at(p);
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: true, modifiers: mods });
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: false, modifiers: mods });
h.run_steps(4);
assert!(h.state().views[0].session.image_selection.is_none(), "{mods:?}: the picture is let go");
let visible = h.state().session.get(doc_id).unwrap().page_images(0)[0].visible_rect.unwrap();
assert!(keep.iter().zip(visible).all(|(a, b)| (a - b).abs() < 0.5), "{mods:?}: the crop is applied: {visible:?}");
}
}