Type: new type takes the font and size of a redefined [Normal Character Style]

New type started in [Normal Character Style]'s fill only and always used the
default font and size. It now takes the style's font, style, size and other
character attributes too, so redefining that style in a New Document profile
sets the default font of the profile's documents. A font, style, size or
color given to text.create still wins.

Part of #8350
This commit is contained in:
dotCooCoo
2026-11-17 14:29:18 -08:00
parent ee60c51fd9
commit 4c78b10c31
5 changed files with 107 additions and 24 deletions
+41 -16
View File
@@ -126,7 +126,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Create Text",
[],
None,
"{x, y, text, rotation?: deg clockwise about x, y (0: level), vertical?: bool = false, size?: pt 0.1..1296 (outside it: an error, and no type is made), font?: family, style? (default Regular, or the family's closest style when it has no Regular), color? (#rrggbbaa and #rgba are refused), keepModel?: bool (as paint.setFill), area?: {width, height, fit?: none|autoHeight|shrinkText|block, fitMinPercent?, wordGap?, lineGap? (as text.areaOptions)}, placeholder?: bool (fill it with placeholder text instead, as the Type tools do with prefs placeholderText), justify?: \"auto\" (default: the start of each paragraph's direction)|\"left\"|\"center\"|\"right\"|\"justifyAll\" (point type: aligned on x, so \"center\" centres each line on the point and \"right\" ends it there; text.setStyle changes it later), leadingModel?: \"romanBaseline\"|\"emBoxTop\", charAlign?: \"romanBaseline\"|\"emBoxTop\"|\"emBoxCenter\"|\"emBoxBottom\"|\"icfTop\"|\"icfBottom\" (default: emBoxTop and emBoxCenter while the interface is in Japanese, else romanBaseline)} → {id, bounds: {x, y, width, height} (the new type's box as document.inspect reports it, after justify), overflow?: [id] (area type whose text doesn't fit its frame: the lines past it aren't drawn; left out when it fits), warning?: text (the font or style given isn't available: the type is drawn in the fallback font or the family's closest style)}; new area type gets Auto Size (fit autoHeight) when the autoSizeAreaType preference is on",
"{x, y, text, rotation?: deg clockwise about x, y (0: level), vertical?: bool = false, size?: pt 0.1..1296 (outside it: an error, and no type is made), font?: family, style? (default Regular, or the family's closest style when it has no Regular), color? (#rrggbbaa and #rgba are refused; size, font, style and color left out come from [Normal Character Style], the defaults unless the document redefined it), keepModel?: bool (as paint.setFill), area?: {width, height, fit?: none|autoHeight|shrinkText|block, fitMinPercent?, wordGap?, lineGap? (as text.areaOptions)}, placeholder?: bool (fill it with placeholder text instead, as the Type tools do with prefs placeholderText), justify?: \"auto\" (default: the start of each paragraph's direction)|\"left\"|\"center\"|\"right\"|\"justifyAll\" (point type: aligned on x, so \"center\" centres each line on the point and \"right\" ends it there; text.setStyle changes it later), leadingModel?: \"romanBaseline\"|\"emBoxTop\", charAlign?: \"romanBaseline\"|\"emBoxTop\"|\"emBoxCenter\"|\"emBoxBottom\"|\"icfTop\"|\"icfBottom\" (default: emBoxTop and emBoxCenter while the interface is in Japanese, else romanBaseline)} → {id, bounds: {x, y, width, height} (the new type's box as document.inspect reports it, after justify), overflow?: [id] (area type whose text doesn't fit its frame: the lines past it aren't drawn; left out when it fits), warning?: text (the font or style given isn't available: the type is drawn in the fallback font or the family's closest style)}; new area type gets Auto Size (fit autoHeight) when the autoSizeAreaType preference is on",
has_doc,
text_create
),
@@ -942,7 +942,13 @@ fn text_create(s: &mut Session, p: &Value) -> Result<Value> {
let id = made.get("id").and_then(Value::as_u64).map(NodeId);
let made = super::typecmd::with_bounds(s, id, made);
let mut made = super::typecmd::with_overflow(s, id.as_slice(), made);
if p.get("font").is_some() || p.get("style").is_some() {
// A face from a redefined [Normal Character Style] is checked too, as one given here is.
let normal_face = s
.doc()
.ok()
.and_then(|d| super::textstyles::style_attrs(&d.doc, false, super::textstyles::NORMAL_CHAR))
.is_some_and(|m| m.contains_key("font_family") || m.contains_key("font_style"));
if p.get("font").is_some() || p.get("style").is_some() || normal_face {
let runs = match id.and_then(|i| s.doc().ok()?.doc.node(i)).map(|n| &n.kind) {
Some(NodeKind::Text(t)) => t.runs.iter().flat_map(super::fonts::run_fonts).collect(),
_ => Default::default(),
@@ -970,8 +976,9 @@ pub(crate) fn new_type_para() -> vectorcraft_doc::ParaStyle {
/// The paragraph alignment, leading model and character alignment new type starts with (`t`'s
/// paragraph and runs): `justify` from `p` (else as [`new_type_para`] left it), `leadingModel`
/// and `charAlign` from `p`, else em box top to top and em box centre while the
/// interface is in Japanese (#432), else the Roman baseline for both. Noted in the journal, so a
/// and `charAlign` from `p` (`charAlign` else from a redefined `[Normal Character Style]` that
/// sets it), else em box top to top and em box centre while the interface is in Japanese (#432),
/// else the Roman baseline for both. Noted in the journal, so a
/// replay sets the same whatever the interface.
pub(crate) fn new_type_alignment(s: &mut Session, p: &Value, cmd: &str, t: &mut TextObject) -> Result<()> {
use vectorcraft_doc::{CharAlign, LeadingModel};
@@ -991,7 +998,14 @@ pub(crate) fn new_type_alignment(s: &mut Session, p: &Value, cmd: &str, t: &mut
l
}
};
let align = match super::textedit::char_align_param(p, cmd)? {
// A redefined [Normal Character Style] that sets Character Alignment gives it to new type.
let normal_align = s
.doc()
.ok()
.and_then(|d| super::textstyles::style_attrs(&d.doc, false, super::textstyles::NORMAL_CHAR))
.filter(|m| m.contains_key("charAlign"))
.and(t.runs.first().map(|r| r.style.char_align));
let align = match super::textedit::char_align_param(p, cmd)?.or(normal_align) {
Some(a) => a,
None => {
let a = if japanese { CharAlign::EmBoxCenter } else { CharAlign::RomanBaseline };
@@ -1019,17 +1033,25 @@ 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 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.
/// `color` in a CMYK document as [`super::paint::new_color`] stores it), else
/// `[Normal Character Style]`'s (the default font, size and black fill unless redefined;
/// never the current fill, #7975), in the style's language, else the document's. 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();
let mut style = s.doc().map_or_else(|_| CharStyle::default(), |d| super::textstyles::normal_char_style(&d.doc));
if let Some(sz) = p.get("size").and_then(Value::as_f64) {
style.size = super::typecmd::given_type_size(sz, cmd)?;
}
if str_param(p, "font").is_some() || str_param(p, "style").is_some() {
// A face given to the command takes its own version and axis settings, as a font change
// does, not those [Normal Character Style] gave another face.
style.font_version = None;
style.font_axes.clear();
}
if let Some(f) = str_param(p, "font") {
style.font_family = f.to_string();
// A family alone starts in Regular, whatever style [Normal Character Style] has.
style.font_style = CharStyle::default().font_style;
}
if let Some(f) = str_param(p, "style") {
style.font_style = f.to_string();
@@ -1039,12 +1061,15 @@ 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 => s.doc().map_or_else(|_| CharStyle::default().fill, |d| super::textstyles::normal_char_fill(&d.doc)),
None => style.fill,
};
// In the language Document Setup chose, else Preferences › Hyphenation › Default Language; none
// for the default. Type already in a document keeps its own.
let chosen = |name: &str| vectorcraft_doc::setup::char_language(name).ok().flatten();
let document = s.doc().ok().and_then(|d| chosen(&d.doc.setup.language));
style.language = document.or_else(|| chosen(&s.prefs.hyphenation_language)).map(str::to_string);
// In the language a redefined [Normal Character Style] gives, else the one Document Setup chose,
// else Preferences › Hyphenation › Default Language; none for the default. Type already in a
// document keeps its own.
if style.language.is_none() {
let chosen = |name: &str| vectorcraft_doc::setup::char_language(name).ok().flatten();
let document = s.doc().ok().and_then(|d| chosen(&d.doc.setup.language));
style.language = document.or_else(|| chosen(&s.prefs.hyphenation_language)).map(str::to_string);
}
Ok(style)
}
+9 -7
View File
@@ -516,14 +516,16 @@ 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-styles.html):
/// every character is in it by default; [FAQs on fonts](https://helpx.adobe.com/illustrator/using/fonts-faq.html):
/// new type's defaults are changed by editing it), 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 {
/// `[Normal Character Style]` in `d`: the defaults, with whatever a redefinition gave it (font,
/// size, fill…). New type starts in that style ([Character and paragraph styles](https://helpx.adobe.com/illustrator/using/character-paragraph-styles.html):
/// every character is in it by default; [FAQs on fonts](https://helpx.adobe.com/illustrator/using/fonts-faq.html)
/// and [Change the default font](https://helpx.adobe.com/illustrator/desktop/design-with-text/fonts-and-scripts/change-the-default-font-in-illustrator.html):
/// new type's defaults, its font included, are changed by editing it), so it takes this style's
/// font and fill, not the current fill left from the last shape (#7975). Attributes that don't
/// pass its checks leave the defaults.
pub(crate) fn normal_char_style(d: &Document) -> CharStyle {
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)
with_attrs(&CharStyle::default(), &normal, "text.create").unwrap_or_default()
}
/// `attrs` in the form `charStyle.new` (`para`: `paraStyle.new`) stores them in `d`, or `None`
+50
View File
@@ -682,6 +682,56 @@ fn the_profile_follows_the_preset_access_and_reset() {
let _ = std::fs::remove_dir_all(&dir);
}
/// The first run's style of text object `id`.
fn first_run(s: &Session, id: &Value) -> vectorcraft_doc::CharStyle {
let n = s.doc().unwrap().doc.node(vectorcraft_doc::NodeId(id.as_u64().unwrap())).cloned().unwrap();
let vectorcraft_doc::NodeKind::Text(t) = &n.kind else { panic!("not type: {n:?}") };
t.runs[0].style.clone()
}
#[test]
fn a_profiles_normal_character_style_is_the_default_font() {
let (mut s, dir) = with_profiles("default-font");
// The Print profile's [Normal Character Style] becomes Source Serif 4 Regular at 9 pt.
s.execute("file.newProfiles.edit", &json!({"name": "Print"})).unwrap();
let attrs = json!({"font_family": "Source Serif 4", "font_style": "Regular", "size": 9, "charAlign": "emBoxBottom"});
s.execute("charStyle.setAttrs", &json!({"name": "[Normal Character Style]", "attrs": attrs})).unwrap();
s.execute("document.save", &json!({})).unwrap();
// New type in a document from the profile takes the font, size and Character Alignment.
s.execute("file.new", &json!({"preset": "A4"})).unwrap();
let made = s.execute("text.create", &json!({"x": 20, "y": 40, "text": "Serif"})).unwrap();
assert!(made.get("warning").is_none(), "{made}");
let st = first_run(&s, &made["id"]);
assert_eq!((st.font_family.as_str(), st.font_style.as_str(), st.size), ("Source Serif 4", "Regular", 9.0));
assert_eq!(st.char_align, vectorcraft_doc::CharAlign::EmBoxBottom);
// A font, size or Character Alignment given to the command still wins.
let made = s
.execute("text.create", &json!({"x": 20, "y": 80, "text": "Sans", "font": "Source Sans 3", "size": 14, "charAlign": "romanBaseline"}))
.unwrap();
let st = first_run(&s, &made["id"]);
assert_eq!((st.font_family.as_str(), st.size, st.char_align), ("Source Sans 3", 14.0, vectorcraft_doc::CharAlign::RomanBaseline));
// A family given alone starts in Regular, not the style's Bold, and without its axis settings.
let attrs = json!({"font_style": "Bold", "fontAxes": {"wght": 700}});
s.execute("charStyle.setAttrs", &json!({"name": "[Normal Character Style]", "attrs": attrs})).unwrap();
let made = s.execute("text.create", &json!({"x": 20, "y": 120, "text": "Inter", "font": "Inter"})).unwrap();
let st = first_run(&s, &made["id"]);
assert_eq!((st.font_family.as_str(), st.font_style.as_str()), ("Inter", "Regular"));
assert!(st.font_axes.is_empty(), "{:?}", st.font_axes);
// A font of the style that isn't available is reported, as one given to the command is.
s.execute("charStyle.setAttrs", &json!({"name": "[Normal Character Style]", "attrs": {"font_family": "No Such Family 7498"}})).unwrap();
let made = s.execute("text.create", &json!({"x": 20, "y": 160, "text": "Missing"})).unwrap();
assert!(made["warning"].as_str().is_some_and(|w| w.contains("No Such Family 7498")), "{made}");
// A document from a profile left as shipped starts type in the default font and size.
s.execute("file.new", &json!({"profile": "Web"})).unwrap();
let made = s.execute("text.create", &json!({"x": 20, "y": 40, "text": "Default"})).unwrap();
let st = first_run(&s, &made["id"]);
let default = vectorcraft_doc::CharStyle::default();
assert_eq!((st.font_family, st.size), (default.font_family, default.size));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn profile_errors_name_the_file_by_its_name_outside_the_read_roots() {
let (mut s, dir) = with_profiles("errors");
+6
View File
@@ -7516,6 +7516,12 @@ A built-in preset names its category's profile in `file.newPresets` (`profile`).
profile of the preset they were made from. `file.new {profile: "Print"}` picks a profile for any settings, and
`profile: null` starts with the defaults. A document made without a preset starts with the defaults.
A profile also sets the default font of its documents: new type starts in [Normal Character Style], so when the profile
redefines that style (`charStyle.setAttrs {name: "[Normal Character Style]", attrs: {font_family, font_style, size}}` in
the document `file.newProfiles.edit` opens, then save), type made in the profile's documents takes its font, style, size
and fill. A `font`, `style`, `size` or `color` given to `text.create` still wins
([Change the default font](https://helpx.adobe.com/illustrator/desktop/design-with-text/fonts-and-scripts/change-the-default-font-in-illustrator.html)).
The profiles are `.vectorcraft` files named after their category in the `New Document Profiles` folder next to the
preferences (VectorCraft's own place and form). A category without a file has the shipped profile, which holds the
defaults. Runs without preferences (`VECTORCRAFT_NO_PREFS`) and the web build have no folder, so every document starts
File diff suppressed because one or more lines are too long