From 626994effd3a808d39512b745746f32294cfef68 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 23:25:17 -0800 Subject: [PATCH] Scripts: a text color assigned in a CMYK document is compared as the document stores it Assigning fillColor or strokeColor on a text range compared the color as given with the paint the characters have. In a CMYK document an RGB color is stored as CMYK, so assigning the RGB color the text already stores ran text.setRangeStyle again and recorded an undo step that changed nothing. The comparison now converts the color through the text's own document (its color mode and color settings), so the same assignment records no step, and text in a background RGB document keeps an RGB color assigned to it. The text paint setters came with #9304. --- crates/engine/src/cmd/newart.rs | 7 +++++ crates/engine/src/script/dom/text.rs | 17 ++++++++--- crates/engine/src/script/dom/text/styles.rs | 2 +- crates/engine/src/tests/script_text.rs | 33 +++++++++++++++++++++ 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/newart.rs b/crates/engine/src/cmd/newart.rs index f420054d11..6296b4e302 100644 --- a/crates/engine/src/cmd/newart.rs +++ b/crates/engine/src/cmd/newart.rs @@ -109,6 +109,13 @@ pub(crate) fn stored_color(s: &Session, c: vectorcraft_color::Color) -> vectorcr super::paint::new_color(s, &json!({}), c) } +/// `c` as a paint command without `keepModel` stores it in `d`: an RGB color in a CMYK document +/// becomes CMYK through the document's own color settings ([`super::colormgmt::doc_cms`]). +pub(crate) fn stored_color_in(d: &vectorcraft_doc::Document, c: vectorcraft_color::Color) -> vectorcraft_color::Color { + let model = (d.color_mode != vectorcraft_doc::ColorMode::Rgb).then(|| d.color_mode.model()); + vectorcraft_color::cms::with_active(super::colormgmt::doc_cms(d), || super::paint::stored_color(model, c)) +} + /// `appearance.setNewArt`: new art's fill, stroke and stroke options, every value checked before /// anything changes. fn set_new_art(s: &mut Session, p: &Value) -> Result { diff --git a/crates/engine/src/script/dom/text.rs b/crates/engine/src/script/dom/text.rs index ebfe900ee4..4f4e477979 100644 --- a/crates/engine/src/script/dom/text.rs +++ b/crates/engine/src/script/dom/text.rs @@ -970,8 +970,16 @@ fn stroke_scale(t: &TextObject) -> f64 { vectorcraft_doc::Scaling::factor(t.xf).filter(|k| k.is_finite() && *k > 1e-12).unwrap_or(1.0) } -/// The `text.setRangeStyle` parameter for `attr` set to `v`, and the value it reads as after. -fn attr_param(h: &Host, attr: Attr, name: &str, v: &Data, first: (&TextObject, &CharStyle, usize)) -> std::result::Result<(Value, Data), HostError> { +/// The `text.setRangeStyle` parameter for `attr` set to `v`, and the value it reads as after in +/// the document `doc` (a color as given when there is none). +fn attr_param( + h: &Host, + attr: Attr, + name: &str, + v: &Data, + first: (&TextObject, &CharStyle, usize), + doc: Option<&vectorcraft_doc::Document>, +) -> std::result::Result<(Value, Data), HostError> { let finite = || number(v, name); Ok(match attr { Attr::Size => { @@ -1040,8 +1048,9 @@ fn attr_param(h: &Host, attr: Attr, name: &str, v: &Data, first: (&TextObject, & } Attr::FillColor | Attr::StrokeColor => { let color = colors::assigned(v, name)?; - let paint = color.map_or(Paint::None, Paint::solid); let given = color.map_or_else(|| json!("none"), |c| colors::param(&c)); + // The paint reads back as the text's document stores it: an RGB color in a CMYK document as CMYK. + let paint = color.map_or(Paint::None, |c| Paint::solid(doc.map_or(c, |d| crate::cmd::newart::stored_color_in(d, c)))); let key = if matches!(attr, Attr::FillColor) { "fill" } else { "stroke" }; (json!({ key: given }), colors::of_paint(&paint)) } @@ -1082,7 +1091,7 @@ fn write_attr(h: &Host, a: &[Data], attr: Attr, name: &str) -> Answer { let t = text_of(st, r.item.id).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?; let styles = styles_in(t, ba, bb, false)?; let first = styles.first().map(|(s, p)| (s.clone(), *p)).unwrap_or_else(|| (CharStyle::default(), 0)); - let (param, wants) = attr_param(h, attr, name, arg(a, 1), (t, &first.0, first.1))?; + let (param, wants) = attr_param(h, attr, name, arg(a, 1), (t, &first.0, first.1), Some(&st.doc))?; // A leading set on characters with auto leading fixes it even where auto gives that value. let has = |st: &CharStyle, p: usize| (!matches!(attr, Attr::Leading) || st.leading.is_some()) && same(&attr_value(h, attr, t, st, p), &wants); // An empty range changes nothing, also one inside a character that `ba..bb` widens to it. diff --git a/crates/engine/src/script/dom/text/styles.rs b/crates/engine/src/script/dom/text/styles.rs index 45d1a428f3..af3e9118ea 100644 --- a/crates/engine/src/script/dom/text/styles.rs +++ b/crates/engine/src/script/dom/text/styles.rs @@ -471,7 +471,7 @@ fn write_char(h: &Host, a: &[Data], attr: Attr, name: &str) -> Answer { let leading = old.get("leading").and_then(Value::as_f64); let sample = CharStyle { size, leading, ..base }; let t = TextObject::point(Point::default(), "", sample.clone()); - let (param, _) = attr_param(h, attr, name, arg(a, 1), (&t, &sample, 0))?; + let (param, _) = attr_param(h, attr, name, arg(a, 1), (&t, &sample, 0), None)?; let mut attrs = old.clone(); for (k, v) in param.as_object().into_iter().flatten() { let Some((_, key)) = CHAR_KEYS.iter().find(|(p, _)| *p == k.as_str()) else { continue }; diff --git a/crates/engine/src/tests/script_text.rs b/crates/engine/src/tests/script_text.rs index 480779c60c..829bc5770e 100644 --- a/crates/engine/src/tests/script_text.rs +++ b/crates/engine/src/tests/script_text.rs @@ -302,6 +302,39 @@ fn character_attributes_paint_only_their_own_characters() { assert!(paints(&s, id).iter().all(|r| r.2 == Paint::None), "a NoColor takes the stroke away"); } +#[test] +fn an_rgb_paint_a_cmyk_document_already_stores_records_no_undo_step() { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 400, "height": 300, "colorMode": "cmyk"})).unwrap(); + s.execute("text.create", &json!({"x": 20, "y": 100, "text": "abc", "size": 12})).unwrap(); + let out = log( + &mut s, + "var r = app.activeDocument.textFrames[0].textRange, red = new RGBColor(); red.red = 255; r.fillColor = red; vc.log(r.fillColor.typename);", + ); + assert_eq!(out[0], json!("CMYKColor"), "the document stores the color as CMYK"); + let steps = undo_steps(&s); + log(&mut s, "var r = app.activeDocument.textFrames[0].textRange, red = new RGBColor(); red.red = 255; r.fillColor = red;"); + assert_eq!(undo_steps(&s), steps, "assigning the color the text already stores records no undo step"); +} + +#[test] +fn an_rgb_paint_on_text_in_a_background_rgb_document_is_kept_as_rgb() { + let (mut s, id) = doc_with("abc"); + s.execute("file.new", &json!({"width": 400, "height": 300, "colorMode": "cmyk"})).unwrap(); + s.execute("text.create", &json!({"x": 20, "y": 100, "text": "abc", "size": 12})).unwrap(); + let out = log( + &mut s, + "var front = app.activeDocument, back = app.documents[0] === front ? app.documents[1] : app.documents[0];\ + var red = new RGBColor(); red.red = 255;\ + var f = front.textFrames[0].textRange, b = back.textFrames[0].textRange;\ + f.fillColor = red; b.fillColor = f.fillColor; b.fillColor = red; vc.log(b.fillColor.typename);", + ); + assert_eq!(out[0], json!("RGBColor"), "the RGB document keeps the RGB color it is given: {out:?}"); + let rgb = s.documents().iter().find(|d| d.doc.color_mode == vectorcraft_doc::ColorMode::Rgb).expect("the RGB document"); + let NodeKind::Text(t) = &rgb.doc.node(id).expect("the RGB document's text").kind else { panic!("not text") }; + assert!(t.runs.iter().all(|r| r.style.fill == Paint::solid(Color::rgb(1.0, 0.0, 0.0))), "{:?}", t.runs); +} + #[test] fn stroke_weight_reads_back_in_document_points_on_scaled_type() { let (mut s, id) = doc_with("abc");