From e9c53ea71883b260794a4bcebc05c6a471b9a4d7 Mon Sep 17 00:00:00 2001 From: Hazel Granados Date: Tue, 20 Oct 2026 00:09:41 -0500 Subject: [PATCH] M7: keep a behind watermark under one on top on an empty page (#3591) On a page with no content of its own, an on-top watermark is the first stream in /Contents, so it was counted with the behind-content marks and a watermark added behind later was drawn over it. Nothing records whether a watermark is behind, so a new behind mark now goes right after the last background, which is always below any on-top watermark. A newer behind watermark goes under older ones again, as before #3591. --- ROADMAP.md | 2 +- crates/edit/src/lib.rs | 19 +++++++++---------- crates/edit/src/tests.rs | 16 ++++++++++++++-- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index ee1dc5823c..6b6607df88 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -172,7 +172,7 @@ Ranked; detail and the full milestone table in [docs/roadmap.md](docs/roadmap.md ## Progress log -- **2026-10-19 (M7, #3591):** A background added to a page that already has one is painted over the old one instead of going under it and staying hidden. Behind the content, backgrounds come first and then behind-content watermarks, each in the order added, so a watermark added after a background no longer disappears. Synthetic regressions in `crates/edit` check the order. Estimates unchanged. +- **2026-10-19 (M7, #3591):** A background added to a page that already has one is painted over the old one instead of going under it and staying hidden. Behind the content, backgrounds come first, in the order added, and then behind-content watermarks, so a watermark added after a background no longer disappears. Synthetic regressions in `crates/edit` check the order. Estimates unchanged. - **2026-10-19 (M14, #3373):** The main menu and its long submenus scroll in short windows, keeping their commands reachable. Estimates unchanged. diff --git a/crates/edit/src/lib.rs b/crates/edit/src/lib.rs index 0bd0ab5691..850838cc24 100644 --- a/crates/edit/src/lib.rs +++ b/crates/edit/src/lib.rs @@ -2,8 +2,9 @@ //! Watermark, Background and Bates numbering (execution plan M7.6). //! //! **How marks are stored.** Each mark is one content stream appended to (or, for content behind -//! the page, prepended to) the page's `/Contents`, drawn in display space (after `/Rotate`) and -//! wrapped in a marked-content artifact (§14.8.2.2) so readers and screen readers skip it: +//! the page, inserted after the backgrounds at the start of) the page's `/Contents`, drawn in +//! display space (after `/Rotate`) and wrapped in a marked-content artifact (§14.8.2.2) so +//! readers and screen readers skip it: //! `/Artifact <> BDC … EMC`. The //! `/PCMark` entry (also in the stream dictionary) names the kind of mark, which is how Update and //! Remove find them again without decompressing page content. When @@ -518,7 +519,7 @@ fn place(doc: &mut Document, page: &pdfcraft_model::Page, kind: MarkKind, conten /// sequences are closed at the end too, so hidden optional content cannot hide the new mark. fn place_tagged(doc: &mut Document, page: &pdfcraft_model::Page, tag: &str, content: Vec, behind: bool) -> Result<(), EditError> { let mut list = contents(doc, page)?; - // The behind-marks at the start: backgrounds, then watermarks, each in the order added. + // The behind-marks at the start: backgrounds in the order added, then watermarks. let behind_kinds = [MarkKind::Background.tag(), MarkKind::Watermark.tag()]; let behind_marks = list.iter().take_while(|o| stream_tag(doc, o).is_some_and(|t| behind_kinds.contains(&t.as_str()))).count(); let wrap = if behind { @@ -534,13 +535,11 @@ fn place_tagged(doc: &mut Document, page: &pdfcraft_model::Page, tag: &str, cont }; let mark = Object::Ref(doc.add(Object::Stream(Stream::flate(tagged(tag), &content)))); if behind { - // A new background goes after the backgrounds already there and below any watermark; - // a new watermark after all of them. Each is painted over the older ones (#3591). - let at = if tag == MarkKind::Background.tag() { - list.iter().take(behind_marks).rposition(|o| stream_tag(doc, o).as_deref() == Some(tag)).map_or(0, |i| i + 1) - } else { - behind_marks - }; + // Right after the backgrounds already there, so a new background is painted over the + // older ones (#3591) and a watermark over all of them. Not after the watermarks: on a + // page with no content, one on top is at the start too and can't be told apart, so a + // newer watermark behind goes under the older ones. + let at = list.iter().take(behind_marks).rposition(|o| stream_tag(doc, o).as_deref() == Some(MarkKind::Background.tag())).map_or(0, |i| i + 1); list.insert(at, mark); } else { if let Some((split, unclosed, marked)) = wrap { diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs index bd506d3b97..a95d7bfe0a 100644 --- a/crates/edit/src/tests.rs +++ b/crates/edit/src/tests.rs @@ -183,8 +183,8 @@ fn a_second_background_is_drawn_over_the_first() { #[test] fn backgrounds_stay_below_watermarks_behind_the_content() { - // Behind the content, backgrounds come first and then watermarks, each in the order added, - // so a watermark added after a background shows and a later background doesn't cover it. + // Behind the content, backgrounds come first, in the order added, and then watermarks, so a + // watermark added after a background shows and a later background doesn't cover it. let mut doc = fixture(); add_background(&mut doc, &[0], &Background { color: [0.0, 0.0, 1.0], opacity: 1.0, ..Background::default() }, false).unwrap(); add_watermark(&mut doc, &[0], &Watermark { text: "DRAFT".into(), behind: true, ..Watermark::default() }, false).unwrap(); @@ -195,6 +195,18 @@ fn backgrounds_stay_below_watermarks_behind_the_content() { assert!(s[2].contains("/PCMark /Watermark") && s[3].contains("(Hi) Tj"), "then the watermark, then the content: {s:?}"); } +#[test] +fn a_watermark_behind_goes_under_one_on_top_of_a_page_without_content() { + // With no content of its own, the page's watermark on top is its first stream, which looks + // like a behind-mark; a watermark added behind later must still be painted under it. + let mut doc = fixture(); + add_watermark(&mut doc, &[2], &Watermark { text: "TOP".into(), ..Watermark::default() }, false).unwrap(); + add_watermark(&mut doc, &[2], &Watermark { text: "BEHIND".into(), behind: true, ..Watermark::default() }, false).unwrap(); + let s = streams(&doc, 2); + assert_eq!(s.len(), 2, "{s:?}"); + assert!(s[0].contains("(BEHIND) Tj") && s[1].contains("(TOP) Tj"), "the watermark behind is painted first: {s:?}"); +} + #[test] fn watermark_offset_moves_the_centre_right_and_up() { let mut doc = fixture();