From 7169b0d6ad760b3fe5d6f07b964f0970794dfef6 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 8 Nov 2026 21:41:20 +0700 Subject: [PATCH] Ungroup refuses a form the page's default colour space would remap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A form with resources of its own and no /DefaultRGB (likewise Gray, CMYK) draws its device colours as they are. Inline, the page's resources apply, and a /DefaultRGB there would remap them (ISO 32000-2 §8.6.5.6). Only the form having an entry the page lacks was refused; now either side having one the other doesn't share is. --- crates/edit/src/tests.rs | 7 ++++++- crates/edit/src/ungroup.rs | 40 +++++++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs index 1abf5d4f92..934153a321 100644 --- a/crates/edit/src/tests.rs +++ b/crates/edit/src/tests.rs @@ -5144,7 +5144,9 @@ fn spaces(entries: &[(&str, Object)]) -> Dict { /// A form's `/DefaultRGB` (likewise `/DefaultGray`, `/DefaultCMYK`) remaps the device colours it /// paints (ISO 32000-2 §8.6.5.6), looked up in the current resource dictionary (§7.8.3). Inline, /// that is the page's: renamed into it, the form's entry would no longer remap anything and its -/// red would draw in another colour, so Ungroup refuses unless the page has the same entry. +/// red would draw in another colour, so Ungroup refuses unless the page has the same entry. A +/// page's entry that the form's own resources lack would remap the form's colours inline, so that +/// is refused too. #[test] fn ungroup_refuses_a_form_with_its_own_default_colour_space() { let refused = |r: Result<(), EditError>, key: &str| match r { @@ -5159,6 +5161,9 @@ fn ungroup_refuses_a_form_with_its_own_default_colour_space() { // Only an entry of the same name counts: the page's DefaultRGB doesn't stand for a DefaultCMYK. let r = ungroup_with_spaces(|_| (spaces(&[("DefaultCMYK", dark_cal_rgb())]), Some(spaces(&[("DefaultRGB", dark_cal_rgb())])))).0; refused(r, "/DefaultCMYK"); + // The other way round: the page's DefaultRGB would remap the red of a form whose own + // resources have none, which draws it as DeviceRGB red. + refused(ungroup_with_spaces(|_| (spaces(&[]), Some(spaces(&[("DefaultRGB", dark_cal_rgb())])))).0, "/DefaultRGB"); } /// When the page's resources have the same default colour space (the same object, or an equal diff --git a/crates/edit/src/ungroup.rs b/crates/edit/src/ungroup.rs index d2c9842717..73c255f747 100644 --- a/crates/edit/src/ungroup.rs +++ b/crates/edit/src/ungroup.rs @@ -13,11 +13,12 @@ //! a blend mode other than Normal inside, or a group drawn translucent, blended or under a soft //! mask, or a group with a blending colour space (`/CS`) other than the page's: its contents //! composite in that space and are then converted (ISO 32000-2 §11.6.6), inline they would -//! composite in the page's. Those are refused rather than changed, as is a form whose -//! `/DefaultGray`, `/DefaultRGB` or `/DefaultCMYK` the page doesn't share (it remaps the form's -//! device colours, §8.6.5.6, and inline the page's resources apply), tagged form content (its -//! structure link would be lost) and content whose marked content or text objects don't balance -//! (inlined, they would change the scope of the page's own). A form in a layer (`/OC`) keeps its membership as `/OC … BDC … EMC`. +//! composite in the page's. Those are refused rather than changed, as is a form with resources +//! of its own whose `/DefaultGray`, `/DefaultRGB` or `/DefaultCMYK` differs from the page's, one +//! having it and the other not included (it remaps the device colours painted with it, +//! §8.6.5.6, and inline the page's resources apply), tagged form content (its structure link +//! would be lost) and content whose marked content or text objects don't balance (inlined, they +//! would change the scope of the page's own). A form in a layer (`/OC`) keeps its membership as `/OC … BDC … EMC`. //! //! A pattern's matrix maps pattern space to the default space of the content stream that uses it //! (ISO 32000-2 §8.7.2): inside the form that is the form's space at the `Do`, on the page it is @@ -182,10 +183,14 @@ pub(crate) fn ungroup_ops( // A form's default colour space remaps its device colours (ISO 32000-2 §8.6.5.6); inline, // the page's resources are the current ones (§7.8.3). if own.is_some() - && let Some(key) = unshared_default_space(doc, &form_res, resources) + && let Some((key, form_has)) = unshared_default_space(doc, &form_res, resources) { let key = String::from_utf8_lossy(key); - return Err(refuse(&format!("it remaps its device colours with a /{key} the page doesn't share"))); + return Err(refuse(&if form_has { + format!("it remaps its device colours with a /{key} the page doesn't share") + } else { + format!("the page's /{key} would remap its device colours, which it draws as they are") + })); } // Merge the form's resources into a copy of the page's, renaming what clashes. @@ -401,14 +406,23 @@ pub(crate) fn merges_without_renaming(doc: &Document, resources: &Dict, name: &[ }) } -/// The first of `/DefaultGray`, `/DefaultRGB` and `/DefaultCMYK` in the form's `/ColorSpace` -/// resources that the page's don't have with an equal value (the same object, or the same value -/// resolved). A default colour space remaps the device colours painted with the resource -/// dictionary it is in (ISO 32000-2 §8.6.5.6, §7.8.3), so inline only the page's would apply. -fn unshared_default_space(doc: &Document, form: &Dict, page: &Dict) -> Option<&'static [u8]> { +/// The first of `/DefaultGray`, `/DefaultRGB` and `/DefaultCMYK` that the form's and the page's +/// `/ColorSpace` resources don't share with an equal value (the same object, or the same value +/// resolved), and whether the form has it (else only the page does). A default colour space +/// remaps the device colours painted with the resource dictionary it is in (ISO 32000-2 +/// §8.6.5.6, §7.8.3), so inline only the page's would apply: the form's would no longer remap +/// its colours, and the page's would start to. +fn unshared_default_space(doc: &Document, form: &Dict, page: &Dict) -> Option<(&'static [u8], bool)> { let spaces = |res: &Dict| res.get(b"ColorSpace").map(|x| doc.resolve(x)).and_then(|x| x.as_dict().cloned()).unwrap_or_default(); let (theirs, mine) = (spaces(form), spaces(page)); - DEFAULT_SPACES.into_iter().find(|key| theirs.get(key).is_some_and(|v| !mine.get(key).is_some_and(|p| *doc.resolve(p) == *doc.resolve(v)))) + let differs = |key: &[u8]| match (theirs.get(key), mine.get(key)) { + (None, None) => false, + (Some(v), Some(p)) => *doc.resolve(p) != *doc.resolve(v), + _ => true, + }; + // The form's own entries are named first. + let form_has = DEFAULT_SPACES.into_iter().find(|key| theirs.get(key).is_some() && differs(key)).map(|key| (key, true)); + form_has.or_else(|| DEFAULT_SPACES.into_iter().find(|key| differs(key)).map(|key| (key, false))) } /// A copy of pattern `entry` whose `/Matrix` maps its space to the page's default space instead