Type: new type starts in [Normal Character Style]'s fill, not the current fill (#7975)

This commit is contained in:
datatimp
2026-11-15 18:23:16 -05:00
parent 613f2039e0
commit 2ff3c6e506
6 changed files with 67 additions and 10 deletions
+3 -3
View File
@@ -863,7 +863,8 @@ pub(crate) fn new_area_fit(s: &Session, p: &Value, c: &str, t: &mut TextObject)
/// The character style new type gets: `size`, `font`, `style` and `color` from `p` (an RGB
/// `color` in a CMYK document as [`super::paint::new_color`] stores it), else the defaults,
/// filled with the current fill unless that is None or white (black then), in the document's
/// filled as `[Normal Character Style]` is (black unless redefined; never the current fill,
/// #7975), in the document's
/// language. A `size` outside [`super::typecmd::TYPE_SIZES`] is an error.
pub(crate) fn new_type_style(s: &Session, p: &Value, cmd: &str) -> Result<CharStyle> {
let mut style = CharStyle::default();
@@ -881,8 +882,7 @@ pub(crate) fn new_type_style(s: &Session, p: &Value, cmd: &str) -> Result<CharSt
}
style.fill = match p.get("color").and_then(color_value) {
Some(c) => vectorcraft_color::Paint::solid(super::paint::new_color(s, p, c)),
None if !s.paint.fill.is_none() && s.paint.fill != vectorcraft_color::Paint::solid(vectorcraft_color::Color::WHITE) => s.paint.fill.clone(),
None => vectorcraft_color::Paint::solid(vectorcraft_color::Color::BLACK),
None => s.doc().map_or_else(|_| CharStyle::default().fill, |d| super::textstyles::normal_char_fill(&d.doc)),
};
// In the language Document Setup chose, else Preferences › Hyphenation › Default Language; none
// for the default. Type already in a document keeps its own.
+4 -6
View File
@@ -857,14 +857,12 @@ mod tests {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 200, "height": 100})).unwrap();
let r = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap()["id"].clone();
s.execute(
"paint.setFill",
&json!({"gradient": {"kind": "linear", "stops": [{"offset": 0, "color": "#000"}, {"offset": 1, "color": "#fff"}]}}),
)
.unwrap();
let gradient = json!({"kind": "linear", "stops": [{"offset": 0, "color": "#000"}, {"offset": 1, "color": "#fff"}]});
s.execute("paint.setFill", &json!({"gradient": gradient})).unwrap();
let t = s.execute("text.create", &json!({"x": 10, "y": 40, "text": "Hi"})).unwrap()["id"].clone();
// New type starts black (#7975); filled with the gradient, its characters make it a gradient object too.
s.execute("paint.setFill", &json!({"gradient": gradient, "ids": [t]})).unwrap();
let i = s.execute("document.info", &json!({})).unwrap();
// The type's characters take the current gradient fill, so it is a gradient object too.
assert_eq!(
(i["objects"]["paths"].as_u64(), i["objects"]["textObjects"].as_u64(), i["objects"]["gradients"].as_u64()),
(Some(1), Some(1), Some(2))
+9
View File
@@ -515,6 +515,15 @@ pub(crate) fn style_attrs(d: &Document, para: bool, name: &str) -> Option<Map<St
if para { Kind::Para } else { Kind::Char }.attrs(d, name)
}
/// The fill of `[Normal Character Style]` in `d`: black, or the fill a redefinition gave it. New
/// type starts in that style ([Character and paragraph styles](https://helpx.adobe.com/illustrator/using/character-paragraph-styles1.html):
/// every character is in it by default), so it takes this fill, not the current fill left from
/// the last shape (#7975).
pub(crate) fn normal_char_fill(d: &Document) -> vectorcraft_color::Paint {
let normal = Kind::Char.attrs(d, NORMAL_CHAR).unwrap_or_default();
with_attrs(&CharStyle::default(), &normal, "text.create").map_or_else(|_| CharStyle::default().fill, |st| st.fill)
}
/// `attrs` in the form `charStyle.new` (`para`: `paraStyle.new`) stores them in `d`, or `None`
/// when they don't pass its checks.
pub(crate) fn stored_style_attrs(d: &Document, para: bool, attrs: &Map<String, Value>) -> Option<Map<String, Value>> {
+18
View File
@@ -512,3 +512,21 @@ fn keep_plain_text_preference_drops_pasted_formatting() {
assert_eq!(paste_copied_range(false), 40.0, "Keep Formatting");
assert_eq!(paste_copied_range(true), 10.0, "Keep Plain Text");
}
/// #7975: pasted plain text is new type, filled as [Normal Character Style] is, not with the
/// current fill.
#[test]
fn pasted_plain_text_takes_the_normal_character_style_fill() {
use vectorcraft_color::{Color, Paint};
let mut s = session();
run(&mut s, "paint.setFill", json!({"color": "#ff0000"}));
run(&mut s, "clipboard.importText", json!({"text": "Plain"}));
run(&mut s, "edit.paste", json!({"center": [200, 150]}));
assert_eq!(text_of(&pasted(&s)[0]).runs[0].style.fill, Paint::solid(Color::BLACK));
let blue = Paint::solid(Color::rgb(0.0, 0.0, 1.0));
let attrs = crate::cmd::textstyles::char_attrs(&vectorcraft_doc::CharStyle { fill: blue.clone(), ..Default::default() });
run(&mut s, "charStyle.setAttrs", json!({"name": "[Normal Character Style]", "attrs": {"fill": attrs["fill"]}}));
run(&mut s, "clipboard.importText", json!({"text": "Plain"}));
run(&mut s, "edit.paste", json!({"center": [200, 150]}));
assert_eq!(text_of(&pasted(&s)[0]).runs[0].style.fill, blue);
}
+32
View File
@@ -4027,3 +4027,35 @@ fn apply_fill_to_stroke_with_characters_selected_changes_only_them() {
let labels = undo_labels(&s);
assert_eq!((labels.len(), labels.last().map(String::as_str)), (steps + 1, Some("Apply Fill to Stroke")));
}
/// Redefines `[Normal Character Style]` with `fill` (`charStyle.setAttrs`).
fn set_normal_fill(s: &mut Session, fill: vectorcraft_color::Paint) {
let attrs = crate::cmd::textstyles::char_attrs(&CharStyle { fill, ..CharStyle::default() });
s.execute("charStyle.setAttrs", &json!({"name": "[Normal Character Style]", "attrs": {"fill": attrs["fill"]}})).unwrap();
}
/// #7975: type the Type tool starts is filled as [Normal Character Style] is, black unless it was
/// redefined, not with the current fill a shape left behind.
#[test]
fn new_type_takes_the_normal_character_style_fill_not_the_current_fill() {
use vectorcraft_color::{Color, Paint};
let mut s = session();
let v = ViewInfo::default();
s.execute("shape.rectangle", &json!({"x": 20, "y": 20, "width": 60, "height": 60})).unwrap();
s.execute("paint.setFill", &json!({"color": "#ff0000"})).unwrap();
s.execute("select.set", &json!({"ids": []})).unwrap();
assert_eq!(s.paint.fill, Paint::solid(Color::rgb(1.0, 0.0, 0.0)), "the current fill is red");
s.select_tool("type", v).unwrap();
click(&mut s, 200.0, 200.0);
s.tool_text("Black", v).unwrap();
let id = s.doc().unwrap().selection.objects[0];
assert_eq!(obj(&s, id).runs[0].style.fill, Paint::solid(Color::BLACK));
// A redefined [Normal Character Style] gives new type its fill.
let blue = Paint::solid(Color::rgb(0.0, 0.0, 1.0));
set_normal_fill(&mut s, blue.clone());
let made = s.execute("text.create", &json!({"x": 100, "y": 300, "text": "Blue"})).unwrap();
assert_eq!(obj(&s, NodeId(made["id"].as_u64().unwrap())).runs[0].style.fill, blue);
// A color given to the command still wins.
let made = s.execute("text.create", &json!({"x": 100, "y": 400, "text": "Green", "color": "#00ff00"})).unwrap();
assert_eq!(obj(&s, NodeId(made["id"].as_u64().unwrap())).runs[0].style.fill, Paint::solid(Color::rgb(0.0, 1.0, 0.0)));
}
File diff suppressed because one or more lines are too long