From 996d34f432a353bb4fe2531ca7be55c6ffbd9979 Mon Sep 17 00:00:00 2001 From: Valentin Klein Date: Wed, 18 Nov 2026 10:38:39 +0100 Subject: [PATCH] A hidden layer stays hidden after a full Save As A layer I hid stays hidden in the open document after a full Save As. Fixes #7411
Details for review **Cause:** the issue's analysis is right. A full save rewrites the file and renumbers its objects, then `mark_saved` refreshes the open document. The refresh looked old layer choices up by the old object number, and the renderer's layer overrides (`config.layers`) still named the old numbers, so the hidden layer came back as shown. An incremental save keeps the numbers, which is why it was unaffected. **Fix:** `mark_saved` now knows when the saved bytes are a rewrite (it already computed that to reset the script runtime). In that case the refresh carries the visibility over by layer name (the nth layer with a name maps to the nth layer with that name, the writer keeps the `/OCGs` order) and rebuilds the renderer's overrides with the new numbers. Name matching also avoids an old number being reused by a different layer. The undo history survives the save, so undo and redo bring back a graph with the old numbers; when the restored layer numbers differ from the open document's, they use the same carry-over by name. Other refreshes are unchanged. **Test** (`crates/engine/src/tests.rs`): `a_full_save_as_keeps_the_layer_choices_of_the_open_document` hides one of two layers whose objects get renumbered by the rewrite, saves in full, and checks both the layer list and the rendered pixels. Without the fix it fails with the hidden layer shown again (I checked this). `undo_and_redo_after_a_full_save_as_keep_the_hidden_layer_hidden` hides a layer after an edit, saves in full, then undoes and redoes, checking the layer list and the pixels each time (it fails without the undo/redo carry-over). Checked: `cargo test -p pdfcraft-engine` (779 unit tests pass in the lib, the other test binaries pass; 7 OCR tests fail here because of the OCR setup on my machine, unrelated to layers), clippy `-D warnings --all-targets`, rustfmt, `xtask layers`. I did not run the native app.
--- crates/engine/src/session/editing.rs | 70 ++++++++++++++++++--- crates/engine/src/session/save.rs | 5 +- crates/engine/src/tests.rs | 92 ++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/session/editing.rs b/crates/engine/src/session/editing.rs index 872d79eb2a..31d47d2927 100644 --- a/crates/engine/src/session/editing.rs +++ b/crates/engine/src/session/editing.rs @@ -396,7 +396,7 @@ impl Session { let current_keys = std::mem::replace(&mut editor.keys, keys); editor.redo.push((label.clone(), current, current_keys, scope)); Self::adopt_keys(doc); - let refreshed = guard(|| Self::refresh_scoped(doc, scope)).unwrap_or_else(|m| Err(EditError::Reopen(m))); + let refreshed = guard(|| Self::refresh_restored(doc, scope)).unwrap_or_else(|m| Err(EditError::Reopen(m))); if let Err(e) = refreshed { if let Some(ed) = doc.editor.as_mut() && let Some((label, current, current_keys, scope)) = ed.redo.pop() @@ -433,7 +433,7 @@ impl Session { let current_keys = std::mem::replace(&mut editor.keys, keys); editor.undo.push((label.clone(), current, current_keys, scope)); Self::adopt_keys(doc); - let refreshed = guard(|| Self::refresh_scoped(doc, scope)).unwrap_or_else(|m| Err(EditError::Reopen(m))); + let refreshed = guard(|| Self::refresh_restored(doc, scope)).unwrap_or_else(|m| Err(EditError::Reopen(m))); if let Err(e) = refreshed { if let Some(ed) = doc.editor.as_mut() && let Some((label, current, current_keys, scope)) = ed.undo.pop() @@ -476,6 +476,16 @@ impl Session { } } + /// [`Self::refresh_scoped`] for a graph undo or redo just brought back. The history survives a + /// full save, which renumbers the objects, so the graph may name the layers by other numbers + /// than the open document's: the user's layer choices then follow their layers by name, as + /// after the save itself, and the layer list is read again whatever `scope` was. + fn refresh_restored(doc: &mut Document, scope: Scope) -> Result<(), EditError> { + let renumbered = + !doc.info.layers.is_empty() && doc.editor.as_ref().is_some_and(|e| layer_ids(&e.cos).iter().ne(doc.info.layers.iter().map(|l| &l.id))); + if renumbered { Self::refresh_renumbered(doc) } else { Self::refresh_scoped(doc, scope) } + } + /// Rebuild the working bytes and the view data that `scope` may have changed. Metadata edits /// keep the renderer; other scopes also replace it. /// Comment and form edits skip the full re-inspection (seconds on very large files): the @@ -672,15 +682,21 @@ impl Session { /// Rebuild working bytes, inspection and renderer from the current edit state. pub(crate) fn refresh(doc: &mut Document) -> Result<(), EditError> { - Self::refresh_with_indexes(doc, true, true) + Self::refresh_with_indexes(doc, true, true, false) + } + + /// [`Self::refresh`] after a full rewrite renumbered the objects: the user's layer choices + /// follow their layers to the new numbers instead of being looked up by the old ones. + pub(super) fn refresh_renumbered(doc: &mut Document) -> Result<(), EditError> { + Self::refresh_with_indexes(doc, true, true, true) } /// Reinspect a link edit while preserving the page-content marker scan cache. fn refresh_links(doc: &mut Document) -> Result<(), EditError> { - Self::refresh_with_indexes(doc, false, true) + Self::refresh_with_indexes(doc, false, true, false) } - fn refresh_with_indexes(doc: &mut Document, invalidate_markers: bool, invalidate_links: bool) -> Result<(), EditError> { + fn refresh_with_indexes(doc: &mut Document, invalidate_markers: bool, invalidate_links: bool, renumbered: bool) -> Result<(), EditError> { #[cfg(test)] crate::record_full_refresh(); // A full refresh may follow structural or annotation changes: cached appearances @@ -699,9 +715,17 @@ impl Session { let info = inspect(bytes.clone(), doc.password.as_deref()).map_err(|e| EditError::Reopen(e.to_string()))?; // Keep the user's layer choices where the layers still exist. let mut info = info; - for l in &mut info.layers { - if let Some(old) = doc.info.layers.iter().find(|o| o.id == l.id) { - l.visible = old.visible; + if renumbered { + carry_renumbered_layer_choices(&doc.info.layers, &mut info.layers); + // The renderer's overrides name the layers by their numbers too. + if !doc.config.layers.is_empty() || info.layers.iter().any(|l| l.visible != l.initial) { + doc.config.layers = Arc::new(info.layers.iter().map(|l| (l.id.0 as i32, l.id.1 as i32, l.visible)).collect()); + } + } else { + for l in &mut info.layers { + if let Some(old) = doc.info.layers.iter().find(|o| o.id == l.id) { + l.visible = old.visible; + } } } note_warnings(&mut info.warnings, &doc.xfa_warnings); @@ -733,6 +757,36 @@ impl Session { } } +/// The optional content groups `cos` lists as layers, in order (what the inspection reads: a group +/// listed twice is one layer, and one that is not a dictionary is none). +fn layer_ids(cos: &pdfcraft_cos::Document) -> Vec<(u32, u16)> { + let props = + cos.trailer().get(b"Root").map(|r| cos.resolve(r)).and_then(|r| r.as_dict().and_then(|r| r.get(b"OCProperties").map(|p| cos.resolve(p)))); + let groups = props.as_deref().and_then(|p| p.as_dict()).and_then(|p| p.get(b"OCGs").map(|g| cos.resolve(g))); + let mut ids: Vec<(u32, u16)> = Vec::new(); + for group in groups.as_deref().and_then(|g| g.as_array()).into_iter().flatten() { + let Some(id) = group.as_ref().map(|id| (id.num, id.generation)) else { continue }; + if cos.resolve(group).as_dict().is_some() && !ids.contains(&id) { + ids.push(id); + } + } + ids +} + +/// Give `new`, the layers of a file whose objects were renumbered, the visibility the user chose on +/// `old`. The numbers say nothing across a rewrite (one may now belong to another layer), so each +/// layer is matched by name: the nth layer called so in `new` is the nth one called so in `old` +/// (the writer keeps the `/OCGs` order). A layer with no counterpart keeps the file's own default. +fn carry_renumbered_layer_choices(old: &[pdfcraft_render::Layer], new: &mut [pdfcraft_render::Layer]) { + let mut seen: std::collections::HashMap<&str, usize> = std::collections::HashMap::new(); + for layer in new { + let nth = seen.entry(layer.name.as_str()).or_insert(0); + let counterpart = old.iter().filter(|o| o.name == layer.name).nth(*nth); + *nth += 1; + layer.visible = counterpart.map_or(layer.initial, |o| o.visible); + } +} + /// Hand on what an edit's XFA scripts showed or asked for (`out`), and when one of them ran away, /// turn the form's scripts off and say so. fn take_xfa_output(doc: &mut Document, out: js::JsOutput, ran_away: bool) { diff --git a/crates/engine/src/session/save.rs b/crates/engine/src/session/save.rs index ec0033d318..d23e159e20 100644 --- a/crates/engine/src/session/save.rs +++ b/crates/engine/src/session/save.rs @@ -65,6 +65,7 @@ impl Session { /// so the next save appends only newer edits. Undo history is kept. pub fn mark_saved(&mut self, id: DocId, bytes: Arc>, path: Option) -> Result<(), EditError> { let doc = self.doc_mut(id)?; + let mut renumbered = false; if let Some(editor) = doc.editor.as_mut() { // An encrypted file needs the password it is protected with now (the owner // password when known, so saving never downgrades this session's rights). @@ -77,6 +78,7 @@ impl Session { let reopened = pdfcraft_cos::Document::open_with_password(bytes.clone(), keys.reopen.as_deref()).map_err(|e| EditError::Reopen(e.to_string()))?; if !preserves_objects { + renumbered = true; crate::js::replace_runtime(&mut doc.js_runtime); } editor.cos = reopened; @@ -95,7 +97,8 @@ impl Session { } doc.dirty = false; doc.generation += 1; - Self::refresh(doc).inspect_err(|_| Self::show_released(doc)) + let refreshed = if renumbered { Self::refresh_renumbered(doc) } else { Self::refresh(doc) }; + refreshed.inspect_err(|_| Self::show_released(doc)) } /// Enter the permissions (owner) password of a document opened with its user password, diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 3e55f09fc5..316088ac7a 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1201,6 +1201,98 @@ fn reset_layers_returns_to_the_files_visibility() { assert!(!s.reset_layers(DocId(999))); } +/// #7411: a full Save As renumbers the objects (the layer groups move from 9 and 10 to the lowest +/// free numbers); the open document keeps the layer the user hid hidden, and the other shown. +#[test] +fn a_full_save_as_keeps_the_layer_choices_of_the_open_document() { + let content = b"/OC /R BDC 1 0 0 rg 10 10 40 80 re f EMC /OC /B BDC 0 0 1 rg 110 10 40 80 re f EMC"; + let mut objs: Vec> = vec![ + b"<< /Type /Catalog /Pages 2 0 R /OCProperties << /OCGs [9 0 R 10 0 R] /D << /BaseState /ON /Order [9 0 R 10 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 200 100] /Contents 4 0 R /Resources << /Properties << /R 9 0 R /B 10 0 R >> >> >>".to_vec(), + [format!("<< /Length {} >>\nstream\n", content.len()).as_bytes(), content, b"\nendstream"].concat(), + ]; + // Objects 5 to 8 are not used, so a rewrite closes the gap. + objs.extend((5..=8).map(|_| b"null".to_vec())); + objs.push(b"<< /Type /OCG /Name (Red rectangle) >>".to_vec()); + objs.push(b"<< /Type /OCG /Name (Blue rectangle) >>".to_vec()); + let mut s = Session::new(); + let id = s.open("layers.pdf", None, Arc::new(pdf_of_objects(&objs)), None).unwrap(); + let colours = |s: &Session| { + let page = render_page(s, id); + let px = page.rgba.as_chunks::<4>().0; + let red = px.iter().filter(|p| p[0] > 200 && p[1] < 60 && p[2] < 60).count(); + let blue = px.iter().filter(|p| p[2] > 200 && p[0] < 60 && p[1] < 60).count(); + (red, blue) + }; + let hidden = |s: &Session| s.get(id).unwrap().info.layers.iter().map(|l| (l.name.clone(), l.visible)).collect::>(); + let (red, blue) = colours(&s); + assert!(red > 100 && blue > 100, "both layers show as the file opens: {red} red, {blue} blue"); + assert!(s.set_layer_visible(id, 0, false)); + assert_eq!(colours(&s).0, 0, "the red rectangle is hidden"); + let before = s.get(id).unwrap().info.layers.iter().map(|l| l.id).collect::>(); + + let saved = s.save_full_bytes(id).unwrap(); + s.mark_saved(id, saved, Some("/tmp/out/layers.pdf".into())).unwrap(); + + let after = s.get(id).unwrap().info.layers.iter().map(|l| l.id).collect::>(); + assert_ne!(before, after, "the rewrite renumbered the layer groups"); + assert_eq!(hidden(&s), [("Red rectangle".to_string(), false), ("Blue rectangle".to_string(), true)]); + let (red, blue) = colours(&s); + assert_eq!(red, 0, "the page is still drawn without the red rectangle"); + assert!(blue > 100, "and with the blue one: {blue}"); +} + +/// #7411: undo history survives the full Save As, whose renumbering the restored graph does not +/// share. Undo and redo carry the user's layer choices over by name, so the layer hidden before the +/// save stays hidden in the layer list and in the pixels, and the renderer's overrides name the +/// numbers the restored graph has. +#[test] +fn undo_and_redo_after_a_full_save_as_keep_the_hidden_layer_hidden() { + let content = b"/OC /R BDC 1 0 0 rg 10 10 40 80 re f EMC /OC /B BDC 0 0 1 rg 110 10 40 80 re f EMC"; + let mut objs: Vec> = vec![ + b"<< /Type /Catalog /Pages 2 0 R /OCProperties << /OCGs [9 0 R 10 0 R] /D << /BaseState /ON /Order [9 0 R 10 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 200 100] /Contents 4 0 R /Resources << /Properties << /R 9 0 R /B 10 0 R >> >> >>".to_vec(), + [format!("<< /Length {} >>\nstream\n", content.len()).as_bytes(), content, b"\nendstream"].concat(), + ]; + objs.extend((5..=8).map(|_| b"null".to_vec())); + objs.push(b"<< /Type /OCG /Name (Red rectangle) >>".to_vec()); + objs.push(b"<< /Type /OCG /Name (Blue rectangle) >>".to_vec()); + let mut s = Session::new(); + let id = s.open("layers.pdf", None, Arc::new(pdf_of_objects(&objs)), None).unwrap(); + let colours = |s: &Session| { + let page = render_page(s, id); + let px = page.rgba.as_chunks::<4>().0; + let red = px.iter().filter(|p| p[0] > 200 && p[1] < 60 && p[2] < 60).count(); + let blue = px.iter().filter(|p| p[2] > 200 && p[0] < 60 && p[1] < 60).count(); + (red, blue) + }; + let layers = |s: &Session| s.get(id).unwrap().info.layers.iter().map(|l| (l.name.clone(), l.visible)).collect::>(); + let ids = |s: &Session| s.get(id).unwrap().info.layers.iter().map(|l| l.id).collect::>(); + s.apply(id, Edit::RotatePages { pages: vec![0], degrees: 180 }).unwrap(); + assert!(s.set_layer_visible(id, 0, false)); + let saved = s.save_full_bytes(id).unwrap(); + s.mark_saved(id, saved, Some("/tmp/out/layers.pdf".into())).unwrap(); + let saved_ids = ids(&s); + let expect_red_hidden = |s: &Session, step: &str| { + assert_eq!(layers(s), [("Red rectangle".to_string(), false), ("Blue rectangle".to_string(), true)], "{step}: the layer list"); + let (red, blue) = colours(s); + assert_eq!((red, blue > 100), (0, true), "{step}: the pixels show the blue rectangle only"); + }; + expect_red_hidden(&s, "after the save"); + + s.undo(id).unwrap(); + assert_ne!(ids(&s), saved_ids, "undo brings back the numbers from before the save"); + expect_red_hidden(&s, "after undo"); + s.redo(id).unwrap(); + assert_eq!(ids(&s), saved_ids, "redo brings back the numbers the save wrote"); + expect_red_hidden(&s, "after redo"); + // Choices made after the trip still apply to the numbers now in use. + assert!(s.set_layer_visible(id, 0, true)); + assert!(colours(&s).0 > 100, "the red rectangle shows again when asked for"); +} + #[test] fn edits_to_unknown_documents_are_rejected() { let (mut s, _) = session_with(1);