Skip to content

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

M4: Document Properties reads a named opening destination's page, so choosing page 1 replaces it (ISO 32000-2 12.3.2.4) (#4574)

mtlcl410e3910536580a
diff --git a/crates/automation/tests/it/automation.rs b/crates/automation/tests/it/automation.rs
index 41b38ede6..9ea6f7f17 100644
--- a/crates/automation/tests/it/automation.rs
+++ b/crates/automation/tests/it/automation.rs
@@ -252,6 +252,19 @@ fn doc_info_reports_the_open_destination() {
"{open}"
);
assert_eq!(open["destination"]["zoom"].as_f64(), Some(2.0));
+ // The initial view reads the named destination's page, and choosing page 1 replaces it (ISO
+ // 32000-2 §12.3.2.4): one undo step, kept by the save.
+ let page = |a: &mut Automation, doc: u64| ok(a, "doc_info", json!({ "doc": doc }))["open_destination"]["page"].clone();
+ assert_eq!(ok(&mut a, "doc_initial_view", json!({ "doc": doc }))["initial_view"]["page"], 2);
+ assert_eq!(ok(&mut a, "doc_initial_view", json!({ "doc": doc, "page": 1 }))["initial_view"]["page"], 1);
+ assert_eq!(page(&mut a, doc), 1);
+ ok(&mut a, "edit_undo", json!({ "doc": doc }));
+ assert_eq!(page(&mut a, doc), 2);
+ ok(&mut a, "edit_redo", json!({ "doc": doc }));
+ assert_eq!(page(&mut a, doc), 1);
+ ok(&mut a, "doc_save", json!({ "doc": doc, "path": "named-page1.pdf" }));
+ let saved = ok(&mut a, "doc_open", json!({ "path": "named-page1.pdf" }))["doc"].as_u64().unwrap();
+ assert_eq!(page(&mut a, saved), 1);
let doc = ok(&mut a, "doc_open", json!({ "path": "script.pdf" }))["doc"].as_u64().unwrap();
assert!(ok(&mut a, "doc_info", json!({ "doc": doc }))["open_destination"].is_null());
}
diff --git a/crates/organize/src/outline.rs b/crates/organize/src/outline.rs
index cc1bbb088..2bb4122d2 100644
--- a/crates/organize/src/outline.rs
+++ b/crates/organize/src/outline.rs
@@ -265,6 +265,15 @@ fn explicit_dest(doc: &Document, targets: &mut Targets, dest: &Object, depth: u8
}
}
+/// The explicit destination array `dest` stands for: itself, a dictionary whose `/D` is one, or a
+/// named destination (a name or string, ISO 32000-2 §12.3.2.4) looked up in the `/Names /Dests`
+/// tree, then the catalog `/Dests`. `None` when a name names nothing.
+pub(crate) fn resolve_dest(doc: &Document, dest: &Object) -> Option<Vec<Object>> {
+ // Names only: the page map is not needed to look one up.
+ let mut targets = Targets { pages: HashMap::new(), names: None };
+ explicit_dest(doc, &mut targets, dest, 0)
+}
+
/// The page index an explicit destination array names.
fn page_of_explicit(targets: &Targets, dest: &[Object]) -> Option<usize> {
match dest.first()? {
diff --git a/crates/organize/src/tests.rs b/crates/organize/src/tests.rs
index 1ba8861cb..1fd60d744 100644
--- a/crates/organize/src/tests.rs
+++ b/crates/organize/src/tests.rs
@@ -2460,10 +2460,89 @@ fn an_unchanged_opening_destination_keeps_its_position_and_name() {
assert_eq!(crate::initial_view(&doc), crate::InitialView { hide_toolbar: true, ..v.clone() });
// A changed magnification writes a destination of its own.
let mut doc = doc;
- crate::set_initial_view(&mut doc, &crate::InitialView { magnification: Magnification::FitPage, ..v }).unwrap();
+ crate::set_initial_view(&mut doc, &crate::InitialView { magnification: Magnification::FitWidth, ..v }).unwrap();
let catalog = doc.get(root).as_dict().cloned().unwrap();
assert_ne!(catalog.get(b"OpenAction"), Some(&action));
- assert_eq!(crate::initial_view(&doc).magnification, Magnification::FitPage);
+ assert_eq!(crate::initial_view(&doc).magnification, Magnification::FitWidth);
+ }
+}
+
+/// A document whose `/OpenAction` is `action`, with the named destination `there` (page 3 at
+/// 200%) in the catalog `/Dests` and `here` (page 1, fit) in a `/Names /Dests` tree.
+fn doc_opening_at(action: Object) -> Document {
+ let page1 = Object::Ref(pdfcraft_cos::ObjRef::new(4, 0));
+ let page3 = Object::Ref(pdfcraft_cos::ObjRef::new(6, 0));
+ let mut dests = pdfcraft_cos::Dict::new();
+ dests.set(b"there".to_vec(), Object::Array(vec![page3, Object::name("XYZ"), Object::Null, Object::Null, Object::Int(2)]));
+ let mut tree = pdfcraft_cos::Dict::new();
+ tree.set(b"Names".to_vec(), Object::Array(vec![Object::String(PdfString::text("here")), Object::Array(vec![page1, Object::name("Fit")])]));
+ let mut names = pdfcraft_cos::Dict::new();
+ names.set(b"Dests".to_vec(), Object::Dict(tree));
+ let mut doc = open(fixture());
+ let root = doc.root().unwrap();
+ doc.update_dict(root, |c| {
+ c.set(b"OpenAction".to_vec(), action);
+ c.set(b"Dests".to_vec(), Object::Dict(dests));
+ c.set(b"Names".to_vec(), Object::Dict(names));
+ })
+ .unwrap();
+ doc
+}
+
+fn open_action(doc: &Document) -> Option<Object> {
+ doc.get(doc.root().unwrap()).as_dict().and_then(|c| c.get(b"OpenAction").cloned())
+}
+
+/// ISO 32000-2 §12.3.2.4: a named destination stands for the explicit one it names. Choosing
+/// page 1 in Document Properties on a document that opens at a named destination elsewhere
+/// writes a destination to page 1; it used to read the name as page 1 and change nothing.
+#[test]
+fn choosing_page_one_replaces_a_named_opening_destination() {
+ use crate::view::Magnification;
+ let page1 = Object::Ref(pdfcraft_cos::ObjRef::new(4, 0));
+ let mut goto = pdfcraft_cos::Dict::new();
+ goto.set(b"S".to_vec(), Object::name("GoTo"));
+ goto.set(b"D".to_vec(), Object::name("there"));
+ for action in [Object::Dict(goto), Object::name("there"), Object::String(PdfString::text("there"))] {
+ let mut doc = doc_opening_at(action.clone());
+ // The dialog shows where the document really opens.
+ let v = crate::initial_view(&doc);
+ assert_eq!((v.page, v.magnification), (2, Magnification::Percent(200.0)), "{action:?}");
+ // Page 1 at the same magnification: an explicit destination to page 1.
+ crate::set_initial_view(&mut doc, &crate::InitialView { page: 0, ..v.clone() }).unwrap();
+ let written = Object::Array(vec![page1.clone(), Object::name("XYZ"), Object::Null, Object::Null, Object::Real(2.0)]);
+ assert_eq!(open_action(&doc), Some(written), "{action:?}");
+ let reopened = save_and_reopen(&doc);
+ assert_eq!(crate::initial_view(&reopened), crate::InitialView { page: 0, ..v.clone() }, "{action:?}");
+ // Page 1 at the default magnification: no opening action, so the first page opens.
+ let mut doc = doc_opening_at(action.clone());
+ crate::set_initial_view(&mut doc, &crate::InitialView { page: 0, magnification: Magnification::Default, ..v }).unwrap();
+ assert_eq!(open_action(&doc), None, "{action:?}");
+ assert_eq!(crate::initial_view(&save_and_reopen(&doc)).page, 0, "{action:?}");
+ }
+}
+
+/// A named opening destination that already goes to the chosen page and magnification is kept,
+/// and so is one that names nothing (it reads as page 1) until another page is chosen.
+#[test]
+fn a_named_opening_destination_is_kept_while_it_still_holds() {
+ use crate::view::Magnification;
+ let page2 = Object::Ref(pdfcraft_cos::ObjRef::new(5, 0));
+ for (action, magnification) in
+ [(Object::String(PdfString::text("here")), Magnification::FitPage), (Object::name("nowhere"), Magnification::Default)]
+ {
+ let mut doc = doc_opening_at(action.clone());
+ let v = crate::initial_view(&doc);
+ assert_eq!((v.page, v.magnification), (0, magnification), "{action:?}");
+ crate::set_initial_view(&mut doc, &crate::InitialView { hide_toolbar: true, ..v.clone() }).unwrap();
+ assert_eq!(open_action(&doc), Some(action.clone()), "{action:?}");
+ crate::set_initial_view(&mut doc, &crate::InitialView { page: 1, ..v.clone() }).unwrap();
+ let kind = if magnification == Magnification::FitPage { "Fit" } else { "XYZ" };
+ assert_eq!(
+ open_action(&doc).as_ref().and_then(Object::as_array).map(|a| (a.first().cloned(), a.get(1).cloned())),
+ Some((Some(page2.clone()), Some(Object::name(kind)))),
+ "{action:?}"
+ );
}
}
diff --git a/crates/organize/src/view.rs b/crates/organize/src/view.rs
index b51df1aa5..899293b98 100644
--- a/crates/organize/src/view.rs
+++ b/crates/organize/src/view.rs
@@ -118,10 +118,12 @@ pub fn initial_view(doc: &Document) -> InitialView {
};
}
}
- // The opening destination: an explicit array, or a GoTo action's.
+ // The opening destination: an explicit array, a GoTo action's `/D`, or a named destination (a
+ // name or string, §12.3.2.4) as the explicit one it names, so that the page and magnification
+ // read here are where the document opens. A name that names nothing reads as page 1.
let dest = c.get(b"OpenAction").map(|o| doc.resolve(o)).and_then(|o| match &*o {
- Object::Array(a) => Some(a.clone()),
- Object::Dict(d) if d.name(b"S") == Some(b"GoTo") => d.get(b"D").map(|x| doc.resolve(x)).and_then(|x| x.as_array().cloned()),
+ Object::Array(_) | Object::Name(_) | Object::String(_) => crate::outline::resolve_dest(doc, &o),
+ Object::Dict(d) if d.name(b"S") == Some(b"GoTo") => d.get(b"D").and_then(|x| crate::outline::resolve_dest(doc, x)),
_ => None,
});
if let Some(d) = dest {
@@ -195,9 +197,12 @@ pub fn set_initial_view(doc: &mut Document, v: &InitialView) -> Result<(), Organ
} else {
vp.remove(b"Direction");
}
+ // A destination, explicit or named (§12.3.2.4), or a GoTo action to one.
let open_action_is_dest = match c.get(b"OpenAction").map(|o| doc.resolve(o)) {
None => true,
- Some(o) => matches!(&*o, Object::Array(_)) || o.as_dict().is_some_and(|d| d.name(b"S") == Some(b"GoTo")),
+ Some(o) => {
+ matches!(&*o, Object::Array(_) | Object::Name(_) | Object::String(_)) || o.as_dict().is_some_and(|d| d.name(b"S") == Some(b"GoTo"))
+ }
};
let dest = match v.magnification {
Magnification::Default if v.page == 0 => None,
diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml
index c9737046e..08e63bc71 100644
--- a/parity/acrobat-features.toml
+++ b/parity/acrobat-features.toml
@@ -3507,8 +3507,8 @@ milestone = "M4"
status = "shipped"
commands = ["file.properties"]
tools = ["doc_initial_view"]
-evidence = ["crates/organize/src/view.rs::initial_view_resolves_indirect_boolean_preferences", "crates/ui-egui/tests/it/ui.rs::a_document_asking_for_full_screen_is_asked_about_first", "crates/organize/src/tests.rs::initial_view_round_trips_and_keeps_scripts", "crates/ui-egui/tests/it/viewer.rs::initial_view_is_edited_and_honoured_on_open", "crates/automation/tests/it/automation.rs::initial_view_through_tools", "crates/render/src/inspect.rs::a_go_to_action_opens_at_its_destination_explicit_or_named", "crates/ui-egui/tests/it/open_action.rs::an_xyz_open_action_puts_its_point_at_the_window_top_left", "crates/ui-egui/tests/it/open_action.rs::a_fith_open_action_puts_its_top_at_the_window_top", "crates/ui-egui/tests/it/open_action.rs::a_named_open_destination_opens_its_page", "crates/ui-egui/tests/it/open_action.rs::a_full_screen_document_shows_its_non_full_screen_panel_outside_full_screen", "crates/organize/src/tests.rs::an_unchanged_opening_destination_keeps_its_position_and_name", "crates/automation/tests/it/automation.rs::doc_info_reports_the_open_destination"]
-notes = "navigation panel, layout, magnification, page, window and interface options; honoured on open; script open actions kept. Open in Full Screen mode (/PageMode /FullScreen) asks first (Full Screen / Not now), as Acrobat does, instead of taking the screen. Window and interface options also read indirect boolean ViewerPreferences values. Missing: Acrobat's Full Screen preference to turn that alert off and its \"remember my choice for this document\" (misc.prefs-full-screen). On open the /OpenAction destination places the view as ISO 32000-2 Table 149 says (the /XYZ point at the window's top-left, /FitH and /FitV top or left at the edge), and a named destination (name or string, /Names /Dests tree or catalog /Dests, §12.3.2.4) resolves; Always use my default zoom keeps only the page. A full-screen document shows its /NonFullScreenPageMode panel outside full screen (Table 147). doc_info reports the opening destination. Document Properties keeps an opening action whose page and magnification are left alone, but still edits only those two, and shows page 1 for a named destination"
+evidence = ["crates/organize/src/view.rs::initial_view_resolves_indirect_boolean_preferences", "crates/ui-egui/tests/it/ui.rs::a_document_asking_for_full_screen_is_asked_about_first", "crates/organize/src/tests.rs::initial_view_round_trips_and_keeps_scripts", "crates/ui-egui/tests/it/viewer.rs::initial_view_is_edited_and_honoured_on_open", "crates/automation/tests/it/automation.rs::initial_view_through_tools", "crates/render/src/inspect.rs::a_go_to_action_opens_at_its_destination_explicit_or_named", "crates/ui-egui/tests/it/open_action.rs::an_xyz_open_action_puts_its_point_at_the_window_top_left", "crates/ui-egui/tests/it/open_action.rs::a_fith_open_action_puts_its_top_at_the_window_top", "crates/ui-egui/tests/it/open_action.rs::a_named_open_destination_opens_its_page", "crates/ui-egui/tests/it/open_action.rs::a_full_screen_document_shows_its_non_full_screen_panel_outside_full_screen", "crates/organize/src/tests.rs::an_unchanged_opening_destination_keeps_its_position_and_name", "crates/automation/tests/it/automation.rs::doc_info_reports_the_open_destination", "crates/organize/src/tests.rs::choosing_page_one_replaces_a_named_opening_destination", "crates/organize/src/tests.rs::a_named_opening_destination_is_kept_while_it_still_holds"]
+notes = "navigation panel, layout, magnification, page, window and interface options; honoured on open; script open actions kept. Open in Full Screen mode (/PageMode /FullScreen) asks first (Full Screen / Not now), as Acrobat does, instead of taking the screen. Window and interface options also read indirect boolean ViewerPreferences values. Missing: Acrobat's Full Screen preference to turn that alert off and its \"remember my choice for this document\" (misc.prefs-full-screen). On open the /OpenAction destination places the view as ISO 32000-2 Table 149 says (the /XYZ point at the window's top-left, /FitH and /FitV top or left at the edge), and a named destination (name or string, /Names /Dests tree or catalog /Dests, §12.3.2.4) resolves; Always use my default zoom keeps only the page. A full-screen document shows its /NonFullScreenPageMode panel outside full screen (Table 147). doc_info reports the opening destination. Document Properties keeps an opening action whose page and magnification are left alone, but still edits only those two; it shows a named destination's page and magnification, and choosing another page or magnification replaces the name with an explicit destination, except that page 1 at the default magnification removes /OpenAction so the first page opens. A name that resolves nowhere reads as page 1 and is kept until another page is chosen"
[[feature]]
id = "organize.custom-properties"