diff --git a/crates/eps/src/legacy_ai.rs b/crates/eps/src/legacy_ai.rs index d304ed27df..ea569c94a5 100644 --- a/crates/eps/src/legacy_ai.rs +++ b/crates/eps/src/legacy_ai.rs @@ -122,6 +122,7 @@ const DIAMOND: &str = "diamond gradients are written as four linear gradients cl const SPREAD: &str = "gradients that repeat or reflect are written with their stops repeated across the shape (legacy .ai gradients only pad)"; const SPREAD_CAPPED: &str = "gradients that repeat or reflect are written with at most 256 stops; they pad past those"; const HUE_STOPS: &str = "gradients mixed around the hue wheel are written in process colors, with the colors between their stops as stops of their own (at most 256; legacy .ai gradients mix their stops' colors directly)"; +const PERCEPTUAL_STOPS: &str = "Perceptual gradients are written in process colors, with the colors between their stops as stops of their own (at most 256; legacy .ai gradients mix their stops' colors directly)"; /// Wedges a conic gradient is written as: one a degree. const CONIC_WEDGES: usize = 360; const STROKES: &str = diff --git a/crates/eps/src/legacy_ai/tests.rs b/crates/eps/src/legacy_ai/tests.rs index f2ee896dc5..9512f3ddbf 100644 --- a/crates/eps/src/legacy_ai/tests.rs +++ b/crates/eps/src/legacy_ai/tests.rs @@ -272,6 +272,81 @@ fn hue_wheel_gradients_are_written_with_the_stops_on_their_curve() { } } +/// The written definition of the gradient named `name` in `text`, and how many stops it holds. +fn definition<'a>(text: &'a str, name: &str) -> (&'a str, usize) { + let def = text.split(&format!("%AI5_BeginGradient: ({name})\n")).nth(1).and_then(|d| d.split("%AI5_EndGradient").next()).unwrap(); + (def, def.matches(" %_Bs\n").count()) +} + +/// The format's gradients mix their stops' colors directly: a Perceptual gradient is written in +/// process colors with the colors on its curve as stops, no more than the reader reads, in a +/// definition of its own rather than its swatch's, with a warning, and opens with the colors it +/// had. A Classic gradient keeps its own stops and gives no warning. +#[test] +fn perceptual_gradients_are_written_with_the_stops_on_their_curve() { + use vectorcraft_color::{GradientMethod, Swatch}; + let mut doc = Document::new(200.0, 100.0); + for (name, color) in [("Night", Color::cmyk(1.0, 1.0, 0.0, 0.0)), ("Sun", Color::cmyk(0.0, 0.0, 1.0, 0.0))] { + doc.swatches.push(Swatch { name: name.into(), paint: Paint::solid(color), global: true, spot: true }); + } + let stop = |offset: f32, color: Color, swatch: &str| GradientStop { swatch: Some(swatch.into()), ..GradientStop::new(offset, color) }; + let stops = vec![stop(0.0, Color::cmyk(1.0, 1.0, 0.0, 0.0), "Night"), stop(1.0, Color::cmyk(0.0, 0.0, 1.0, 0.0), "Sun")]; + let mut g = GradientPaint::new(Gradient { method: GradientMethod::Perceptual, ..Gradient::new(GradientKind::Linear, stops) }); + g.swatch = Some("Dusk".into()); + let want = g.gradient.clone(); + // The same stops mixed directly stray from the Perceptual curve, so a file holding only the + // two stops would not open with the colors it had. + let classic = Gradient { method: GradientMethod::Classic, ..want.clone() }; + assert!( + (0..=100).any(|k| { + let t = k as f32 / 100.0; + want.sample(t).0.to_rgb().iter().zip(classic.sample(t).0.to_rgb()).any(|(a, b)| (a - b).abs() > 0.05) + }), + "the methods mix alike" + ); + let n = shape(&mut doc, shapes::rectangle(Rect::new(20.0, 10.0, 180.0, 90.0)), Paint::Gradient(Box::new(g)), Paint::None); + let l = doc.layers[0].id; + doc.insert(Some(l), 0, n).unwrap(); + let out = write(&doc); + assert!(out.warnings.iter().any(|w| w == PERCEPTUAL_STOPS), "{:?}", out.warnings); + assert!(!out.warnings.iter().any(|w| w == HUE_STOPS), "{:?}", out.warnings); + let text = String::from_utf8_lossy(&out.bytes).into_owned(); + assert!(!text.contains("(Dusk)"), "the curve is not the swatch's gradient"); + assert!(!text.contains("(Night)") && !text.contains("(Sun)"), "the stops are process colors: {text}"); + let (_, written) = definition(&text, "Gradient"); + assert!((3..=crate::import::MAX_GRADIENT_STOPS).contains(&written), "{written} stops"); + let read = crate::import(&out.bytes).unwrap().document; + let mut back = None; + read.walk(|n| { + if let (None, Paint::Gradient(g)) = (&back, n.appearance.fill_paint()) { + back = Some(g.gradient.clone()); + } + }); + let back = back.unwrap(); + assert!(back.method.is_classic()); + assert_eq!(back.stops.len(), written, "every stop written is read"); + assert!(back.stops.iter().all(|s| s.swatch.is_none()), "{:?}", back.stops); + for k in 0..=100 { + let t = k as f32 / 100.0; + let (a, b) = (want.sample(t).0.to_rgb(), back.sample(t).0.to_rgb()); + assert!(a.iter().zip(b).all(|(x, y)| (x - y).abs() < 0.03), "at {t}: {a:?} vs {b:?}"); + } + + // Classic: its own two stops, its spot colors and its swatch's name, without the warning. + let mut doc = Document::new(200.0, 100.0); + doc.swatches.push(Swatch { name: "Night".into(), paint: Paint::solid(Color::cmyk(1.0, 1.0, 0.0, 0.0)), global: true, spot: true }); + let mut g = GradientPaint::new(classic); + g.swatch = Some("Dusk".into()); + let n = shape(&mut doc, shapes::rectangle(Rect::new(20.0, 10.0, 180.0, 90.0)), Paint::Gradient(Box::new(g)), Paint::None); + let l = doc.layers[0].id; + doc.insert(Some(l), 0, n).unwrap(); + let out = write(&doc); + assert!(!out.warnings.iter().any(|w| w == PERCEPTUAL_STOPS), "{:?}", out.warnings); + let text = String::from_utf8_lossy(&out.bytes).into_owned(); + assert_eq!(definition(&text, "Dusk").1, 2); + assert!(text.contains("%%DocumentCustomColors: (Night)"), "{text}"); +} + /// A radial gradient repeated out from a center far from the shape is written with its ramp points /// (percent) to five decimals, so its runs keep their places hundreds of runs out. #[test] diff --git a/crates/eps/src/legacy_ai/writer.rs b/crates/eps/src/legacy_ai/writer.rs index e6653bfcd3..1a74c6d831 100644 --- a/crates/eps/src/legacy_ai/writer.rs +++ b/crates/eps/src/legacy_ai/writer.rs @@ -9,7 +9,7 @@ use super::{ ROTATED_TYPE, Rect, STROKES, SUBLAYERS, Shape, StrokeLayer, SubPath, TITLE_AND_COLOR_NAMES, TRANSPARENCY, TYPE, TYPE_ATTRIBUTES, TextKind, TextObject, UNDECODED, Vec2, Write, ai_ops, latin, ps, stroke, }; -use super::{CONIC, CONIC_WEDGES, DIAMOND, GRAPHS, HUE_STOPS, SPREAD, SPREAD_CAPPED}; +use super::{CONIC, CONIC_WEDGES, DIAMOND, GRAPHS, HUE_STOPS, PERCEPTUAL_STOPS, SPREAD, SPREAD_CAPPED}; #[cfg(test)] thread_local! { @@ -755,11 +755,11 @@ impl<'a> Writer<'a> { /// (its distance a fraction of the radius, at most 1, §3.5.5; `0 0 0 0 Bh` at the center, /// §3.5.6), `1 (name) x y angle length 1 0 0 1 0 0 Bg`. fn gradient(&mut self, g: &GradientPaint, bounds: Rect) -> String { - // A gradient mixed around the hue wheel (the format's gradients mix their stops' colors - // directly): the stops on its curve, no more than the reader reads, in a definition of - // its own. - if g.gradient.method.is_hue() { - self.warn(HUE_STOPS); + // A gradient mixed by the Perceptual method or around the hue wheel (the format's + // gradients mix their stops' colors directly): the stops on its curve, in process colors, + // no more than the reader reads, in a definition of its own. + if !g.gradient.method.is_classic() { + self.warn(if g.gradient.method.is_hue() { HUE_STOPS } else { PERCEPTUAL_STOPS }); let curve = g.gradient.as_classic_curve(); let gradient = if curve.is_spread() { curve } else { curve.fitted(crate::import::MAX_GRADIENT_STOPS).into_owned() }; let p = GradientPaint { gradient, swatch: None, ..g.clone() }; diff --git a/docs/mcp.md b/docs/mcp.md index 092faabc4c..4ac57d5003 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -1660,7 +1660,8 @@ its noise). PDF exports write it the same way, except for a linear, radial, coni stops whose other stops are all paper white or written in CMYK or gray, that is not a dithered fill and whose curve does not have too many stops (a diamond as four such shadings). An export that converts colors to a CMYK destination profile writes RGB and Lab stops in CMYK, and so does a CMYK document unless the export converts colors to an RGB destination profile. PDF writes tints of one spot ink as a Separation shading in that ink, and spot inks mixed with other spot, CMYK -or gray inks as a DeviceN shading. +or gray inks as a DeviceN shading. A legacy `.ai` file gets the colors between a Perceptual gradient's stops as stops +of their own, in process colors (at most 256, with a warning). `hue-clockwise` and `hue-counterclockwise` (VectorCraft's own methods) mix a linear, radial, conic or diamond gradient's colors around the hue wheel: hue, saturation and brightness each change evenly from one stop to the next. With `hue-clockwise` the hue angle falls (red, magenta, blue, cyan, green, yellow, red), which is clockwise on the diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index d661735982..3ad9d8efcd 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2419,6 +2419,8 @@ PDF export writes a Perceptual linear or radial gradient whose stops are tints o In these Separation and DeviceN shadings, a white stop linked to a swatch that is not a spot color (a global White swatch, for example) is paper white, 0% of the ink, as an unlinked white stop is, so a gradient of one spot ink and such a stop stays a Separation shading; a stop linked to a white spot color stays a stop of that ink (VectorCraft's own rule). Test: `engine::tests::linked_stops::white_stops_linked_to_process_swatches_are_paper_in_spot_gradients`. +Legacy `.ai` export writes a Perceptual gradient as it writes a gradient mixed around the hue wheel: the colors on its curve become stops of their own, in process colors, at most 256, in a gradient definition of their own rather than the gradient swatch's, with a warning. In a legacy `.ai` gradient definition, each color stop holds only its color, midpoint and position (`colorSpec colorStyle midPoint rampPoint %_Bs`, [File Format Specification §3.5.2](https://web.archive.org/web/20050126105313/https://partners.adobe.com/public/developer/en/illustrator/sdk/AI7FileFormat.pdf#page=41)). VectorCraft's legacy `.ai` reader opens such a gradient as a Classic gradient and mixes the stop colors directly. Spot color stops lose their inks, as they do in EPS (VectorCraft's own rule). Test: `eps::legacy_ai::tests::perceptual_gradients_are_written_with_the_stops_on_their_curve`. + ## Artwork Cleanup add-on (2026-10-30) A VectorCraft addition, not a feature of the reference app, so no area score moves. Object › Plug-ins › Artwork Cleanup and its