diff --git a/crates/color/src/cms.rs b/crates/color/src/cms.rs index b3f312879b..0a873cd5df 100644 --- a/crates/color/src/cms.rs +++ b/crates/color/src/cms.rs @@ -446,6 +446,15 @@ impl Cms { CmykSpace::Generic(g) => g.to_srgb(cmyk, absolute, bpc), CmykSpace::Device => naive_cmyk_to_rgb(cmyk), CmykSpace::Icc(p) => { + // With black-point compensation the profile's black maps onto sRGB black, as + // `cmyk_to_srgb` shows the working space. + if bpc + && !absolute + && let Some(srgb) = builtin_rgb_cache(SRGB) + && let Some(v) = p.to_rgb_of(&srgb, &cmyk, Intent::RelativeColorimetric, true) + { + return v; + } let i = if absolute { Intent::AbsoluteColorimetric } else { Intent::RelativeColorimetric }; p.to_srgb(&cmyk, i).unwrap_or_else(|| naive_cmyk_to_rgb(cmyk)) } diff --git a/crates/color/src/cms/tests_icc.rs b/crates/color/src/cms/tests_icc.rs index 2c1d96638b..3506bcb856 100644 --- a/crates/color/src/cms/tests_icc.rs +++ b/crates/color/src/cms/tests_icc.rs @@ -187,6 +187,25 @@ fn black_point_compensation_maps_the_darkest_ink_to_rgb_black() { assert_eq!(abs(true), abs(false)); } +/// A loaded ICC CMYK profile as Proof Setup's device shows its darkest ink with the settings' +/// black-point compensation, as the working CMYK proof of the same profile does, and Simulate +/// Black Ink shows it as the lighter gray the press prints. +#[test] +fn simulate_black_ink_shows_a_loaded_cmyk_profiles_black_as_gray() { + let c = icc_cmyk(true); + let k100 = [0.0, 0.0, 0.0, 1.0]; + let device = ProofSetup { target: ProofTarget::Cmyk(c.settings().cmyk.clone()), ..Default::default() }; + let plain = c.proof_cmyk_to_srgb(k100, &device); + let working = c.proof_cmyk_to_srgb(k100, &ProofSetup { target: ProofTarget::WorkingCmyk, ..Default::default() }); + assert!(plain.iter().zip(working).all(|(a, b)| (a - b).abs() < 0.01), "device {plain:?}, working {working:?}"); + let black = c.proof_cmyk_to_srgb(k100, &ProofSetup { simulate_black: true, ..device.clone() }); + assert!(plain.iter().zip(black).all(|(a, b)| b > a + 0.03), "black ink {black:?}, without {plain:?}"); + // Without black-point compensation the device shows the darkest ink as the press prints it. + let off = icc_cmyk(false); + let plain_off = off.proof_cmyk_to_srgb(k100, &ProofSetup { target: ProofTarget::Cmyk(off.settings().cmyk.clone()), ..Default::default() }); + assert!(plain_off.iter().zip(black).all(|(a, b)| (a - b).abs() < 0.02), "without compensation {plain_off:?}, black ink {black:?}"); +} + /// #1584: every built-in profile (RGB, CMYK and gray) has each tag on a 4-byte boundary and a size /// that is a multiple of 4 (ICC.1 §7.1.2), and still reads back as the same space. #[test] diff --git a/crates/engine/tests/color_settings.rs b/crates/engine/tests/color_settings.rs index 17444e4c50..289b24436e 100644 --- a/crates/engine/tests/color_settings.rs +++ b/crates/engine/tests/color_settings.rs @@ -20,6 +20,22 @@ fn settings() -> MutexGuard<'static, ()> { SETTINGS.lock().unwrap_or_else(|e| e.into_inner()) } +/// The working Color Settings as a test found them, put back when it ends (a failing one too) +/// before the next test takes its turn. +struct Restored(cms::ColorSettings, #[allow(dead_code)] MutexGuard<'static, ()>); + +impl Drop for Restored { + fn drop(&mut self) { + let _ = cms::set_active(&self.0); + } +} + +/// [`settings`], with the working Color Settings put back afterwards. +fn restored_settings() -> Restored { + let g = settings(); + Restored(cms::working_settings(), g) +} + #[test] fn color_settings_lists_profiles_and_validates() { let _g = settings(); @@ -929,8 +945,7 @@ fn saved_color_settings_that_cannot_be_used_leave_the_defaults() { /// proofs the same way. Without it the proof starts from the drawn pixels, which are the same. #[test] fn preserve_cmyk_numbers_proofs_each_color_from_its_inks() { - let _g = settings(); - let old = cms::active_settings(); + let _g = restored_settings(); let mut s = session(); s.execute("edit.colorSettings", &json!({"cmyk": cms::DEVICE_CMYK})).unwrap(); let k50 = json!({"c": 0, "m": 0, "y": 0, "k": 50}); @@ -968,7 +983,6 @@ fn preserve_cmyk_numbers_proofs_each_color_from_its_inks() { let tint = cms::active().proof_srgb([1.0; 3], &paper).map(|v| (v * 255.0).round() as i32); let opts = RenderOptions { proof: Some(paper), background: Some([255, 255, 255, 255]), ..Default::default() }; let bare = pixel(&Renderer::new().render(&doc, 200, 200, Affine::IDENTITY, &opts).pixels, 50, 115); - cms::set_active(&old).unwrap(); assert_eq!(a, b, "the two builds show as one gray in Device CMYK"); assert_eq!(pa, pb, "proofed from the pixels, they stay one gray"); assert!(ca != cb, "proofed from their inks, they print differently: {ca:?} vs {cb:?}"); @@ -982,8 +996,7 @@ fn preserve_cmyk_numbers_proofs_each_color_from_its_inks() { /// mask covers the same in a pattern's tile as outside one. #[test] fn preserve_cmyk_numbers_leaves_mask_images_in_pattern_tiles_as_they_are() { - let _g = settings(); - let old = cms::active_settings(); + let _g = restored_settings(); let mut s = session(); // A K100 square masked by a blue image, the same at (0, 0) and as a pattern's tile. let masked = |s: &mut Session, x: f64| { @@ -1003,6 +1016,9 @@ fn preserve_cmyk_numbers_leaves_mask_images_in_pattern_tiles_as_they_are() { exec(&mut s, "object.pattern.make", json!({"ids": [tile.0], "name": "Masked Tile", "edit": false})); let band = rect(&mut s, 0.0, 100.0, 200.0, 40.0); exec(&mut s, "paint.setFill", json!({"ids": [band.0], "swatch": "Masked Tile"})); + // An unmasked K100 square, for the color the masked one covers the page with. + let solid = rect(&mut s, 100.0, 0.0, 20.0, 20.0); + exec(&mut s, "paint.setFill", json!({"ids": [solid.0], "color": {"c": 0, "m": 0, "y": 0, "k": 100}})); let proof = vectorcraft_render::proof::ProofSetup { target: vectorcraft_render::proof::ProofTarget::Cmyk(cms::GENERIC_CMYK.into()), preserve_numbers: true, @@ -1010,18 +1026,25 @@ fn preserve_cmyk_numbers_leaves_mask_images_in_pattern_tiles_as_they_are() { }; let doc = s.doc().unwrap().doc.clone(); let pixel = |px: &[u8], x: usize, y: usize| [px[(y * 200 + x) * 4], px[(y * 200 + x) * 4 + 1], px[(y * 200 + x) * 4 + 2]]; + // The masked square, the middle of a tile (the tiles start at the art's left and top edges, + // every 20 points), the unmasked square and the bare page. let shown = |proof: Option| { let opts = RenderOptions { proof, background: Some([255, 255, 255, 255]), ..Default::default() }; let px = Renderer::new().render(&doc, 200, 200, Affine::IDENTITY, &opts).pixels; - // The middle of a tile: the tiles start at the art's left and top edges, every 20 points. - (pixel(&px, 10, 10), pixel(&px, 50, 130)) + (pixel(&px, 10, 10), pixel(&px, 50, 130), pixel(&px, 110, 10), pixel(&px, 190, 60)) }; - let (plain, plain_tile) = shown(None); - let (proofed, proofed_tile) = shown(Some(proof)); - cms::set_active(&old).unwrap(); + let (plain, plain_tile, plain_k, plain_page) = shown(None); + let (proofed, proofed_tile, proofed_k, proofed_page) = shown(Some(proof)); let near = |a: [u8; 3], b: [u8; 3]| a.iter().zip(b).all(|(a, b)| (i32::from(*a) - i32::from(b)).abs() <= 2); assert!(plain != [255; 3] && near(plain, plain_tile), "the tile shows the masked square: {plain:?} vs {plain_tile:?}"); assert!(near(proofed, proofed_tile), "the tile's mask covers as the square's does: {proofed:?} vs {proofed_tile:?}"); + // The proof changes the ink's color but not how much of the page the mask lets it cover. + let channel = |v: [u8; 3], i: usize| f32::from(v[i]); + let cover = (0..3).map(|i| (channel(plain_page, i) - channel(plain, i)) / (channel(plain_page, i) - channel(plain_k, i))).sum::() / 3.0; + assert!(cover > 0.02 && cover < 0.98, "the mask covers part of the square: {cover}"); + let want: [u8; 3] = + std::array::from_fn(|i| (channel(proofed_page, i) + cover * (channel(proofed_k, i) - channel(proofed_page, i))).round() as u8); + assert!(near(proofed, want), "the proofed square keeps the mask's coverage: {proofed:?} vs {want:?}"); } /// Edit › Color Settings' color management policies decide what opening a document tagged with diff --git a/crates/ui-egui/src/dialogs/proof_setup.rs b/crates/ui-egui/src/dialogs/proof_setup.rs index 7e534de2f4..69c0655eef 100644 --- a/crates/ui-egui/src/dialogs/proof_setup.rs +++ b/crates/ui-egui/src/dialogs/proof_setup.rs @@ -34,8 +34,12 @@ const LABEL_W: f32 = 140.0; pub(super) const SPEC: DialogSpec = DialogSpec { heading: |_| tl!("Proof Setup").into(), body, confirm, min_width: 420.0, ..DialogSpec::FORM }; -/// Open the dialog on the current proof setup. +/// Open the dialog on the current proof setup. While it is open, it stays as it is, so Cancel +/// still puts back the view it found first. pub fn open(app: &mut VectorcraftApp) { + if app.ui.dialog.as_ref().is_some_and(|d| d.kind == KIND) { + return; + } let v = proof::view(); let s = &v.setup; let device = match &s.target { @@ -147,12 +151,30 @@ fn preview(app: &mut VectorcraftApp, d: &mut Dialog) { } } -/// Put back the proof view the dialog found. +/// Put back the proof view the dialog found and redraw the documents. The view is set directly, +/// not through `view.proofSetup`, which refuses a device profile removed since it was set. fn restore(app: &mut VectorcraftApp, d: &Dialog) { - if let Some(before) = d.fields.get(BEFORE).cloned() - && let Err(e) = app.run(CMD, before) - { - app.ui.status = e; + let Some(before) = d.fields.get(BEFORE) else { return }; + let text = |k: &str| before.get(k).and_then(Value::as_str).unwrap_or_default(); + let on = |k: &str| before.get(k).and_then(Value::as_bool).unwrap_or_default(); + let (Some(target), Some(intent)) = (ProofTarget::parse(text("target")), Intent::parse(text("intent"))) else { return }; + let mut v = proof::view(); + let was = v.clone(); + v.setup.target = target; + v.setup.intent = intent; + v.setup.simulate_paper = on("simulatePaper"); + v.setup.preserve_numbers = on("preserveNumbers"); + v.setup.simulate_black = on("simulateBlack"); + v.proof_colors = on("proof"); + if v == was { + return; + } + proof::set_view(v); + let uids: Vec = app.session.documents().iter().map(|d| d.uid).collect(); + for uid in uids { + if let Some(d) = app.session.document_mut(uid) { + d.revision += 1; + } } } @@ -268,6 +290,52 @@ mod tests { assert_eq!(proof::view(), before, "Cancel puts the view back"); } + /// Cancel and Preview off put back a proof view whose device profile was removed after it was + /// set, and redraw the documents. + #[test] + fn cancel_puts_back_a_view_whose_profile_was_removed() { + let _v = DefaultView::new(); + let (mut app, ctx) = app(); + let name = "Proof Setup Removed RGB"; + cms::register_icc(&cms::icc_bytes(cms::DISPLAY_P3).unwrap(), Some(name.into())).unwrap(); + app.run("view.proofSetup", json!({"target": format!("rgb:{name}"), "proof": true})).unwrap(); + app.run("color.removeProfile", json!({"name": name})).unwrap(); + let before = proof::view(); + assert_eq!(before.setup.target, ProofTarget::Rgb(name.into()), "removing the profile leaves it the device"); + app.run("ui.proofSetupDialog", json!({})).unwrap(); + app.ui.dialog.as_mut().unwrap().fields.insert("device".into(), json!(WORKING)); + frame(&mut app, &ctx); + assert_eq!(proof::view().setup.target, ProofTarget::WorkingCmyk, "the preview shows the working CMYK"); + app.ui.dialog.as_mut().unwrap().fields.insert("preview".into(), json!(false)); + frame(&mut app, &ctx); + assert_eq!(proof::view(), before, "Preview off puts the view back"); + app.ui.dialog.as_mut().unwrap().fields.insert("preview".into(), json!(true)); + frame(&mut app, &ctx); + let revision = app.session.doc().unwrap().revision; + super::super::cancel(&mut app); + assert!(app.ui.dialog.is_none()); + assert_eq!(proof::view(), before, "Cancel puts the view back"); + assert!(app.session.doc().unwrap().revision > revision, "the document redraws"); + } + + /// Run again while the dialog is open, Proof Setup keeps the dialog as it is, so Cancel still + /// puts back the view it found the first time. + #[test] + fn running_proof_setup_again_keeps_the_open_dialog() { + let _v = DefaultView::new(); + let (mut app, ctx) = app(); + let before = proof::view(); + app.run("ui.proofSetupDialog", json!({})).unwrap(); + let rgb = format!("rgb:{}", cms::DISPLAY_P3); + app.ui.dialog.as_mut().unwrap().fields.insert("device".into(), json!(rgb)); + frame(&mut app, &ctx); + assert_eq!(proof::view().setup.target, ProofTarget::Rgb(cms::DISPLAY_P3.into())); + app.run("ui.proofSetupDialog", json!({})).unwrap(); + assert_eq!(app.ui.dialog.as_ref().unwrap().str("device"), rgb, "the open dialog stays as it is"); + super::super::cancel(&mut app); + assert_eq!(proof::view(), before, "Cancel puts back the view the first run found"); + } + /// A device the dialog can't use (a profile that is not loaded) is refused, and the dialog /// stays open with the proof view as it was. #[test]