PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Merge branch 'main' into main
Combilere85c654ead41
diff --cc crates/edit/src/tests.rsindex 4e0c056ba,0549906db..239c0d92d--- a/crates/edit/src/tests.rs+++ b/crates/edit/src/tests.rs@@@ -7173,157 -6403,26 +7222,175 @@@ fn reading_keeps_dotted_leader_entries_"Ordinary body text. Still the same paragraph.",]);++ #[cfg(test)]+ mod proptests {+ use super::*;+ use proptest::prelude::*;++ proptest! {++ #[test]+ fn prop_is_entire_page_wrapped_arbitrary_bytes(bytes in proptest::collection::vec(any::<u8>(), 0..2048)) {+ let pieces = pdfcraft_content::Pieces::join(&[&bytes]);+ let ops = pieces.parse().ops;+ let _ = images::is_entire_page_wrapped(&ops);+ }++ #[test]+ fn prop_content_units_arbitrary_bytes(+ bytes in proptest::collection::vec(any::<u8>(), 0..2048),+ start in 0usize..50,+ end in 0usize..50,+ ) {+ let pieces = pdfcraft_content::Pieces::join(&[&bytes]);+ let ops = pieces.parse().ops;+ let scan_start = start.min(ops.len());+ let scan_end = end.min(ops.len()).max(scan_start);+ let units = images::content_units(&ops, scan_start, scan_end);+ for u in units {+ prop_assert!(u.start <= u.end);+ prop_assert!(u.end <= ops.len());+ }+ }++ #[test]+ fn prop_arrange_image_arbitrary_malformed(+ index in 0usize..10,+ order_idx in 0u8..4,+ bytes in proptest::collection::vec(any::<u8>(), 0..1024),+ ) {+ let order = match order_idx {+ 0 => images::ArrangeOrder::BringToFront,+ 1 => images::ArrangeOrder::SendToBack,+ 2 => images::ArrangeOrder::BringForward,+ _ => images::ArrangeOrder::SendBackward,+ };+ let mut doc = image_page_with(&String::from_utf8_lossy(&bytes));+ let _ = images::arrange_image(&mut doc, 0, index, order);+ }++ #[test]+ fn prop_crop_image_malformed_inputs(+ x1 in -500.0f64..500.0,+ y1 in -500.0f64..500.0,+ x2 in -500.0f64..500.0,+ y2 in -500.0f64..500.0,+ bytes in proptest::collection::vec(any::<u8>(), 0..1024),+ ) {+ let mut doc = image_page_with(&String::from_utf8_lossy(&bytes));+ let _ = images::crop_image(&mut doc, 0, 0, [x1, y1, x2, y2]);+ let _ = images::remove_image_crop(&mut doc, 0, 0);+ }+ }+ }+}++/// One 600×800 page: Im0 (4×2 gray image), Fm0 (200×100 Form), ExtGState GSone (ca 1).+fn regression_page(content: &str) -> Document {+ let form = "0 g 0 0 200 100 re f";+ 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 600 800] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> /XObject << /Im0 6 0 R /Fm0 7 0 R >> /ExtGState << /GSone << /Type /ExtGState /ca 1 >> >> >> >>".into(),+ format!("<< /Length {} >>\nstream\n{content}\nendstream", content.len()),+ "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".into(),+ "<< /Type /XObject /Subtype /Image /Width 4 /Height 2 /ColorSpace /DeviceGray /BitsPerComponent 8 /Length 8 >>\nstream\n\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\nendstream".into(),+ format!("<< /Type /XObject /Subtype /Form /BBox [0 0 200 100] /Length {} >>\nstream\n{form}\nendstream", form.len()),+ ];+ let mut out = b"%PDF-1.7\n".to_vec();+ let mut offs = Vec::new();+ for (i, o) in objs.iter().enumerate() {+ offs.push(out.len());+ out.extend_from_slice(format!("{} 0 obj\n{o}\nendobj\n", i + 1).as_bytes());+ }+ let x = out.len();+ out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());+ for o in offs {+ 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{x}\n%%EOF\n", objs.len() + 1).as_bytes());+ Document::open(Arc::new(out)).unwrap()+}++fn regression_rects(doc: &Document) -> Vec<[i64; 4]> {+ let mut v: Vec<[i64; 4]> = images::page_images(doc, 0).unwrap().iter().map(|i| i.rect.map(|x| x.round() as i64)).collect();+ v.sort();+ v+}++/// A `cm` inside a BDC…EMC unit applies to later content; arranging the unit must not shift anything.+#[test]+fn arrange_keeps_cm_leaking_out_of_marked_content() {+ let mut doc = regression_page("/Figure <</MCID 0>> BDC 1 0 0 1 50 50 cm q 100 0 0 100 0 0 cm /Im0 Do Q EMC q 10 0 0 10 200 200 cm /Im0 Do Q");+ let before = regression_rects(&doc);+ images::arrange_image(&mut doc, 0, 0, images::ArrangeOrder::BringToFront).unwrap();+ assert_eq!(before, regression_rects(&reopen(&doc)), "other content moved");+}++/// Cropping a Form XObject to 100×100 keeps a 100×100 visible area (or is refused) — never 1×1.+#[test]+fn crop_form_xobject_keeps_requested_area() {+ let mut doc = regression_page("q 1 0 0 1 100 100 cm /Fm0 Do Q");+ if images::crop_image(&mut doc, 0, 0, [100.0, 100.0, 200.0, 200.0]).is_err() {+ return; // refusing to crop vector art is also acceptable+ }+ let after = images::page_images(&reopen(&doc), 0).unwrap()[0].clone();+ let v = after.visible_rect.unwrap_or(after.rect);+ assert!((v[2] - v[0] - 100.0).abs() < 1.0 && (v[3] - v[1] - 100.0).abs() < 1.0, "expected 100x100 visible, got {v:?}");+}++/// A gs naming a missing ExtGState must not be read as 50% opacity and block the reorder.+#[test]+fn arrange_tolerates_missing_extgstate() {+ let mut doc = regression_page("q /GSmissing gs 100 0 0 100 50 50 cm /Im0 Do Q /GSone gs q 10 0 0 10 200 200 cm /Im0 Do Q");+ let r = images::arrange_image(&mut doc, 0, 0, images::ArrangeOrder::BringToFront);+ assert!(r.is_ok(), "{r:?}");+}++/// Bring to front on the image already in front must not report a change it didn't make.+#[test]+fn noop_arrange_is_not_ok_without_change() {+ let mut doc = regression_page("q 10 0 0 10 200 200 cm /Im0 Do Q q 100 0 0 100 50 50 cm /Im0 Do Q");+ let before = write_incremental(&doc, &SaveOptions::default()).unwrap();+ let r = images::arrange_image(&mut doc, 0, 1, images::ArrangeOrder::BringToFront);+ let after = write_incremental(&doc, &SaveOptions::default()).unwrap();+ assert!(r.is_err() || before != after, "Ok with no change");+}++/// Cropping a turned image keeps the requested upright box in user space, rather than expanding+/// to the bounding box of the turned image coordinates.+#[test]+fn crop_turned_image_preserves_requested_area() {+ let mut doc = regression_page("q 70.71 70.71 -70.71 70.71 300 100 cm /Im0 Do Q");+ let req = [270.0, 150.0, 330.0, 200.0];+ images::crop_image(&mut doc, 0, 0, req).expect("crop succeeds");+ let after = &images::page_images(&reopen(&doc), 0).unwrap()[0];+ let v = after.visible_rect.expect("visible rect exists");+ assert!((v[0] - req[0]).abs() < 1.0, "x0: expected {}, got {}", req[0], v[0]);+ assert!((v[1] - req[1]).abs() < 1.0, "y0: expected {}, got {}", req[1], v[1]);+ assert!((v[2] - req[2]).abs() < 1.0, "x1: expected {}, got {}", req[2], v[2]);+ assert!((v[3] - req[3]).abs() < 1.0, "y1: expected {}, got {}", req[3], v[3]);}+ /// Text set larger than the widest page PDF allows (14 400 pt) panicked when its paragraph was given+ /// a width: the width is clamped between the font size and 14 400 pt, and `f64::clamp` panics when+ /// its minimum is the larger. The width is now capped at 14 400 pt whatever the font size: from the+ /// file (`Tf`, or a small `Tf` scaled up by `Tm`) or asked for (the automation tools pass it through).+ #[test]+ fn width_on_a_paragraph_set_larger_than_a_page_is_capped_not_a_panic() {+ let wide = text::BlockStyle { width: Some(200.0), ..Default::default() };+ let huge = text::BlockStyle { size: Some(20_000.0), width: Some(200.0), ..Default::default() };+ for (content, style) in [+ ("BT /F2 20000 Tf 72 700 Td (ab) Tj ET", &wide),+ ("BT /F2 1 Tf 20000 0 0 20000 72 700 Tm (ab) Tj ET", &wide),+ ("BT /F2 12 Tf 72 700 Td (ab) Tj ET", &huge),+ ] {+ let mut doc = text_page(content);+ text::rewrite_block(&mut doc, 0, 0, None, style).unwrap();+ }+ }+#[test]fn added_text_is_wrapped_once_per_write() {let mut doc = fixture();diff --cc crates/ui-egui/src/editing.rsindex 49ef05958,2fdc2e9d0..876278d6f--- a/crates/ui-egui/src/editing.rs+++ b/crates/ui-egui/src/editing.rs@@@ -1074,11 -1021,9 +1076,12 @@@ fn is_form_edit(edit: &Edit) -> bool////// Outlook opens an attachment from its own cache: `INetCache\Content.Outlook\<random>\` for/// classic Outlook (`Temporary Internet Files\Content.Outlook\` on older Windows), and the- /// `Microsoft.OutlookForWindows` package folder for the new Outlook. Saving there either fails,+ /// `Packages\Microsoft.OutlookForWindows_<id>` package folder for the new Outlook. The patterns+ /// follow those layouts, so an ordinary folder that merely shares a name doesn't match. Saving there either fails,/// because Outlook can still hold the file, or appears to work while the edits stay out of the+/// email and are deleted with the cache. Matched on either separator and in any case, so it can+/// be tested on every platform.+#[allow(dead_code)]/// email and are deleted with the cache. Apple Mail opens one from `Mail Downloads/<UUID>/` in/// its sandbox container, where other apps can't write, so saving there fails (#2751). Matched on/// either separator and in any case, so it can be tested on every platform.diff --cc crates/ui-egui/src/panels.rsindex 08cc614e1,7d16bc340..a50e2710e--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@@ -68,13 -68,10 +68,14 @@@ fn all_tools(app: &mut PdfCraftApp, uiapp.left_open = false;}egui::ScrollArea::vertical().auto_shrink([false, false]).show(ui, |ui| {+ ui.add_space(2.0);let shown = if app.all_tools_expanded { TOOL_GROUPS.len() } else { COLLAPSED_TOOLS.min(TOOL_GROUPS.len()) };for g in &TOOL_GROUPS[..shown] {- if tool_row(ui, t, g).clicked() {+ let pinned = app.is_tool_pinned(g.id);+ let (resp, pin_clicked) = tool_row(ui, t, g, pinned);+ if pin_clicked {+ app.toggle_pin_tool(g.id);+ } else if resp.clicked() {app.select_tool(g.id);}}