From b6a5cd2a3611033b30eea078751afc7930938682 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 13:10:47 -0800 Subject: [PATCH 1/3] Place: a placed dimension keeps the composite font its label was renamed to When placed editable art uses a composite font whose name the document already gives a different font, the placed type is renamed to a free name such as "Name 2". A dimension's label was renamed, but the dimension's own options kept the old name, so the next redraw of the label (moving a point, a refresh) switched it to the document's other font. - The adoption walker renames a dimension's font family with its label, keeping a spelling that already names the same font. - Planning reserves the families destination dimensions name. - A generated alias stays within the 256 bytes a dimension's family keeps, and a font that an incoming dimension uses reuses only an equivalent font whose name fits. --- crates/engine/src/cmd/compositefonts.rs | 43 +++++++++++++++-- crates/engine/src/cmd/place/adopt.rs | 22 +++++---- .../engine/tests/native_composite_adoption.rs | 46 +++++++++++++++++++ 3 files changed, 99 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/cmd/compositefonts.rs b/crates/engine/src/cmd/compositefonts.rs index bff15c6540..b9da8cadf7 100644 --- a/crates/engine/src/cmd/compositefonts.rs +++ b/crates/engine/src/cmd/compositefonts.rs @@ -99,12 +99,19 @@ fn family_names(d: &Document) -> Result> { note(family); } } - let mut graphs = Vec::new(); + let (mut graphs, mut dimensions) = (Vec::new(), Vec::new()); view.visit_stored(|node, _| { + // A dimension's options name the family its label is redrawn in. + if let Some(dimension) = &node.extras().dimension { + dimensions.push(dimension.options.font_family.clone()); + } if node.extras().graph.is_some() { graphs.push(node.id); } }); + for mut family in dimensions { + note(&mut family); + } for id in graphs { if let Some(node) = view.stored_node_mut(id) { graph_families(node, &mut note); @@ -141,6 +148,16 @@ pub(super) fn node_families(node: &mut Node, visit: &mut impl FnMut(&mut String) visit(&mut run.style.font_family); } } + // A dimension redraws its label from its own options, so they follow the label's family. + // A spelling that already names the same font, or one too long for the options, is kept. + if let Some(dimension) = node.extras_if_any().and_then(|extras| extras.dimension.as_mut()) { + let family = &mut dimension.options.font_family; + let mut renamed = family.clone(); + visit(&mut renamed); + if !CompositeFont::same_name(&renamed, family) && renamed.len() <= ALIAS_BYTES_MAX { + *family = renamed; + } + } graph_families(node, visit); } @@ -162,12 +179,22 @@ impl CompositeImport { } } +/// The longest family name in bytes that a dimension's options keep (`DimensionOptions::validate`). +const ALIAS_BYTES_MAX: usize = 256; + fn composite_alias(base: &str, reserved: &BTreeSet, cmd: &str) -> Result { let db = FontDb::global(); let mut number = 2_u64; loop { let suffix = format!(" {number}"); - let stem: String = base.chars().take(COMPOSITE_NAME_MAX.saturating_sub(suffix.chars().count())).collect(); + let bytes = ALIAS_BYTES_MAX.saturating_sub(suffix.len()); + let mut stem = String::new(); + for c in base.chars().take(COMPOSITE_NAME_MAX.saturating_sub(suffix.chars().count())) { + if stem.len() + c.len_utf8() > bytes { + break; + } + stem.push(c); + } let name = format!("{}{suffix}", stem.trim_end()); if CompositeFont::clean_name(&name).as_deref() == Some(name.as_str()) && !reserved.contains(&name.to_lowercase()) @@ -181,7 +208,13 @@ fn composite_alias(base: &str, reserved: &BTreeSet, cmd: &str) -> Result /// Plan used incoming definitions before changing either document or the font database. /// Keep existing meanings even for an uninstalled family that destination art still names. -pub(super) fn plan_import(source: Vec, source_families: BTreeSet, target: &Document, cmd: &str) -> Result { +pub(super) fn plan_import( + source: Vec, + source_families: BTreeSet, + dimension_families: BTreeSet, + target: &Document, + cmd: &str, +) -> Result { if source.len() > COMPOSITE_FONTS_MAX || target.composite_fonts.len() > COMPOSITE_FONTS_MAX { return Err(bad(cmd, format!("a document keeps at most {COMPOSITE_FONTS_MAX} composite fonts"))); } @@ -208,7 +241,9 @@ pub(super) fn plan_import(source: Vec, source_families: BTreeSet< let mut names = BTreeMap::new(); let mut combined = target.composite_fonts.to_vec(); for font in source { - let equivalent = |other: &&CompositeFont| other.parts == font.parts && other.custom == font.custom; + let dimension = dimension_families.contains(&font.name.to_lowercase()); + let equivalent = + |other: &&CompositeFont| other.parts == font.parts && other.custom == font.custom && (!dimension || other.name.len() <= ALIAS_BYTES_MAX); let mut reused = CompositeFont::named(&target.composite_fonts, &font.name).filter(equivalent); let needs_alias = reused.is_none() && (target_taken.contains(&font.name.to_lowercase()) || db.has_installed_family(&font.name)); if needs_alias { diff --git a/crates/engine/src/cmd/place/adopt.rs b/crates/engine/src/cmd/place/adopt.rs index 483503f190..b340052962 100644 --- a/crates/engine/src/cmd/place/adopt.rs +++ b/crates/engine/src/cmd/place/adopt.rs @@ -545,17 +545,23 @@ fn remap_composite_families(src: &Document, nodes: &mut [Node], used: &Used, bru /// Discover the incoming resource frontier and its composite-font users before planning /// names or changing either document. Resource order and the no-composite fast path stay shared. -fn collect_resources(src: &Document, nodes: &mut [Node]) -> (Used, Vec, BTreeSet) { +fn collect_resources(src: &Document, nodes: &mut [Node]) -> (Used, Vec, BTreeSet, BTreeSet) { let mut used = Used::default(); let composites = !src.composite_fonts.is_empty(); let mut families = BTreeSet::new(); - let mut note_family = |name: &mut String| { - families.insert(name.to_lowercase()); + let mut dimension_families = BTreeSet::new(); + let mut note_families = |node: &mut Node| { + if let Some(dimension) = &node.extras().dimension { + dimension_families.insert(dimension.options.font_family.to_lowercase()); + } + node_families(node, &mut |name| { + families.insert(name.to_lowercase()); + }); }; for n in nodes.iter_mut() { refs(n, &mut |r| used.note(r)); if composites { - each_mut(n, &mut |node| node_families(node, &mut note_family)); + each_mut(n, &mut note_families); } } let src_brushes = vectorcraft_brush::library(src); @@ -586,20 +592,20 @@ fn collect_resources(src: &Document, nodes: &mut [Node]) -> (Used, Vec, B for mut a in art { refs(&mut a, &mut |r| used.note(r)); if composites { - each_mut(&mut a, &mut |node| node_families(node, &mut note_family)); + each_mut(&mut a, &mut note_families); } } } - (used, src_brushes, families) + (used, src_brushes, families, dimension_families) } /// Bring the resources `nodes` (art from `src`) use into `dst`, renaming on conflict, and point /// `nodes` at the names they got. pub(crate) fn adopt(dst: &mut Document, src: &Document, nodes: &mut [Node], cmd: &str) -> Result<()> { - let (used, mut src_brushes, families) = collect_resources(src, nodes); + let (used, mut src_brushes, families, dimension_families) = collect_resources(src, nodes); let fonts = src.composite_fonts.iter().filter(|font| families.contains(&font.name.to_lowercase())).cloned().collect::>(); // All representability/validation errors precede incoming or destination mutation. - let plan = if fonts.is_empty() { None } else { Some(plan_import(fonts, families, dst, cmd)?) }; + let plan = if fonts.is_empty() { None } else { Some(plan_import(fonts, families, dimension_families, dst, cmd)?) }; let staged; let mut aliased = false; let src = if let Some(plan) = &plan { diff --git a/crates/engine/tests/native_composite_adoption.rs b/crates/engine/tests/native_composite_adoption.rs index 55f1f4976f..803f20c416 100644 --- a/crates/engine/tests/native_composite_adoption.rs +++ b/crates/engine/tests/native_composite_adoption.rs @@ -140,6 +140,52 @@ fn native_composite_meanings_survive_editable_and_embedded_placement() { } } +#[test] +fn native_dimension_label_font_follows_a_renamed_composite_font() { + let _context = FontContext::hold(); + let dir = TempDir::new("native-composite-dimension"); + let path = dir.join("dimension.vectorcraft"); + // The second name is 255 bytes, so an alias that only counted characters would pass the 256 + // bytes a dimension's family keeps. + for name in ["NativeAdoptionDimension".to_string(), "\u{3042}".repeat(85)] { + assert_dimension_alias(&path, &name); + } +} + +fn assert_dimension_alias(path: &Path, name: &str) { + let incoming = CompositeFont::new(name, "Source Sans 3", "Regular"); + let mut source = destination(std::slice::from_ref(&incoming)); + let options = json!({"fontFamily": incoming.name, "fontStyle": "Regular"}); + source.execute("dimension.create", &json!({"a": [4, 20], "b": [60, 20], "options": options})).unwrap(); + let source_doc = document(&source).clone(); + source.execute("type.compositeFonts.set", &json!({"fonts": []})).unwrap(); + write_native(path, &source_doc); + for linked in [false, true] { + let mut existing = incoming.clone(); + existing.parts.roman.size = 125.0; + let mut session = destination(&[existing]); + let id = place(&mut session, path, linked); + let root = document(&session).node(id).unwrap(); + let (mut stored, mut drawn) = (Vec::new(), Vec::new()); + root.walk(&mut |node| { + if let Some(dimension) = &node.extras().dimension { + stored.push(dimension.options.font_family.clone()); + } + if let NodeKind::Text(text) = &node.kind { + drawn.extend(text.runs.iter().map(|run| run.style.font_family.clone())); + } + }); + assert_eq!(stored.len(), 1, "REGRESSION: the placed art keeps its one editable dimension"); + assert!(stored[0].len() <= 256, "REGRESSION: the alias fits a dimension's family ({} bytes)", stored[0].len()); + assert!(!drawn.is_empty(), "CONTROL: the dimension draws a label"); + assert!(!CompositeFont::same_name(&drawn[0], &incoming.name), "CONTROL: the conflicting name is aliased"); + assert!( + drawn.iter().all(|family| family == &stored[0]), + "REGRESSION: a redrawn dimension label must keep the aliased composite font ({stored:?} vs {drawn:?})" + ); + } +} + #[test] fn native_composite_alias_is_reused_without_changing_destination_art() { let _context = FontContext::hold(); From 88bd2243876e7d676e8f1b0ac80bce7ba25f0662 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 14:03:09 -0800 Subject: [PATCH 2/3] Place: a composite alias skips families named by dimensions in brush art Planning an alias read dimension option families only from document art. A dimension inside an art brush's art was reached for graph label families but not for its own options, so an incoming composite font could take the family that dimension names, and the dimension's label would switch to the incoming font on redraw. The brush walk now notes dimension options too. --- crates/engine/src/cmd/compositefonts.rs | 3 + .../engine/tests/native_composite_adoption.rs | 59 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/crates/engine/src/cmd/compositefonts.rs b/crates/engine/src/cmd/compositefonts.rs index b9da8cadf7..7e82b25903 100644 --- a/crates/engine/src/cmd/compositefonts.rs +++ b/crates/engine/src/cmd/compositefonts.rs @@ -119,6 +119,9 @@ fn family_names(d: &Document) -> Result> { } fn brush_graphs(node: &mut Node, note: &mut impl FnMut(&mut String)) { graph_families(node, note); + if let Some(dimension) = node.extras_if_any().and_then(|extras| extras.dimension.as_mut()) { + note(&mut dimension.options.font_family); + } if let Some(mask) = node.extras_if_any().and_then(|extras| extras.mask.as_mut()) { brush_graphs(Arc::make_mut(&mut mask.art), note); } diff --git a/crates/engine/tests/native_composite_adoption.rs b/crates/engine/tests/native_composite_adoption.rs index 803f20c416..0a8e63b3db 100644 --- a/crates/engine/tests/native_composite_adoption.rs +++ b/crates/engine/tests/native_composite_adoption.rs @@ -186,6 +186,65 @@ fn assert_dimension_alias(path: &Path, name: &str) { } } +#[test] +fn native_composite_alias_skips_a_family_named_by_a_dimension_in_brush_art() { + let _context = FontContext::hold(); + let dir = TempDir::new("native-composite-brush-dimension"); + let path = dir.join("lettering.vectorcraft"); + let incoming = CompositeFont::new("NativeAdoptionBrushDimension", "Source Sans 3", "Regular"); + write_native(&path, &font_document(&incoming)); + let taken = format!("{} 2", incoming.name); + // The destination's only use of the first free alias is a dimension inside an art brush. + let mut builder = destination(&[]); + let options = json!({"fontFamily": taken, "fontStyle": "Regular"}); + let made = builder.execute("dimension.create", &json!({"a": [4, 20], "b": [60, 20], "options": options})).unwrap(); + let dimension = NodeId(made["id"].as_u64().unwrap()); + builder.execute("brush.new", &json!({"type": "art", "name": "NativeAdoptionDimensionBrush", "ids": [dimension.0]})).unwrap(); + let mut doc = document(&builder).clone(); + if doc.node(dimension).is_some() { + doc.remove(dimension).unwrap(); + } + // Leave the dimension's options as the only place the family is named, as in a label drawn + // before its options changed, so the reservation cannot come from the label's own runs. + fn relabel(node: &mut Node) { + if let NodeKind::Text(text) = &mut node.kind { + for run in &mut text.runs { + run.style.font_family = "Source Sans 3".into(); + } + } + for child in node.children_mut().into_iter().flatten() { + relabel(std::sync::Arc::make_mut(child)); + } + } + let mut brushes = vectorcraft_brush::library(&doc); + for art in brushes.iter_mut().flat_map(|brush| brush.art_mut()) { + relabel(art); + } + vectorcraft_brush::store(&mut doc, &brushes); + let mut in_art = Vec::new(); + doc.visit_stored(|node, _| in_art.extend(node.extras().dimension.as_ref().map(|d| d.options.font_family.clone()))); + assert!(in_art.is_empty(), "CONTROL: no dimension is left in the document's art"); + let mut in_brush = Vec::new(); + for brush in vectorcraft_brush::library(&doc) { + if let vectorcraft_brush::BrushKind::Art(art) = &brush.kind { + art.art.walk(&mut |node| in_brush.extend(node.extras().dimension.as_ref().map(|d| d.options.font_family.clone()))); + } + } + assert_eq!(in_brush, vec![taken.clone()], "CONTROL: the brush art holds the dimension"); + let mut existing = incoming.clone(); + existing.parts.roman.size = 125.0; + let mut session = Session::new(); + session.add_document(doc, None); + session.execute("type.compositeFonts.set", &json!({"fonts": [existing]})).unwrap(); + let id = place(&mut session, &path, false); + let family = placed_family(&session, id); + assert!(!CompositeFont::same_name(&family, &incoming.name), "CONTROL: the conflicting name is aliased"); + assert!( + !CompositeFont::same_name(&family, &taken), + "REGRESSION: the alias must not take a family a brush-art dimension already names ({family})" + ); +} + #[test] fn native_composite_alias_is_reused_without_changing_destination_art() { let _context = FontContext::hold(); From b8e11fb2c75a91fed6534d583e0d51210dbe2c1b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 16:00:35 -0800 Subject: [PATCH 3/3] Place: a placed dimension reuses an equal composite font of the same name When the document defined a composite font with the same name and the same definition as one a placed dimension used, but spelled in more than the 256 bytes a dimension's family keeps (a case-insensitive match such as Kelvin signs for k), placement copied the incoming font under a new name instead of reusing the document's. The byte limit now applies only to an alias under another name; a same-name reuse keeps the dimension's own spelling. Merging main had also left a second, unguarded rename of a dimension's family next to the guarded one, so a renamed family that was too long or only differed in case was still written. The unguarded rename is removed. docs/mcp.md describes how file.place renames a placed dimension's family. --- crates/engine/src/cmd/compositefonts.rs | 13 +++----- .../engine/tests/native_composite_adoption.rs | 33 +++++++++++++++++++ docs/mcp.md | 2 ++ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/cmd/compositefonts.rs b/crates/engine/src/cmd/compositefonts.rs index 35e47de772..3b15730f9a 100644 --- a/crates/engine/src/cmd/compositefonts.rs +++ b/crates/engine/src/cmd/compositefonts.rs @@ -155,10 +155,6 @@ pub(super) fn node_families(node: &mut Node, visit: &mut impl FnMut(&mut String) visit(&mut font.family); } } - // A live dimension makes its label again from its options. - if let Some(dimension) = node.extras_if_any().and_then(|extras| extras.dimension.as_deref_mut()) { - visit(&mut dimension.options.font_family); - } // A dimension redraws its label from its own options, so they follow the label's family. // A spelling that already names the same font, or one too long for the options, is kept. if let Some(dimension) = node.extras_if_any().and_then(|extras| extras.dimension.as_mut()) { @@ -267,13 +263,14 @@ pub(super) fn plan_import( let mut combined = target.composite_fonts.to_vec(); for font in source { let dimension = dimension_families.contains(&font.name.to_lowercase()); - let equivalent = - |other: &&CompositeFont| other.parts == font.parts && other.custom == font.custom && (!dimension || other.name.len() <= ALIAS_BYTES_MAX); - let mut reused = CompositeFont::named(&target.composite_fonts, &font.name).filter(equivalent); + let equal = |other: &&CompositeFont| other.parts == font.parts && other.custom == font.custom; + // A dimension keeps its own spelling of a reused name of the same font, so only an alias + // under another name has to fit the bytes its options keep. + let mut reused = CompositeFont::named(&target.composite_fonts, &font.name).filter(equal); let needs_alias = reused.is_none() && (target_taken.contains(&font.name.to_lowercase()) || db.has_installed_family(&font.name)); if needs_alias { // A repeated import reuses its already-installed equal alias. - reused = target.composite_fonts.iter().find(equivalent); + reused = target.composite_fonts.iter().find(|other| equal(other) && (!dimension || other.name.len() <= ALIAS_BYTES_MAX)); } let name = match reused { Some(other) => other.name.clone(), diff --git a/crates/engine/tests/native_composite_adoption.rs b/crates/engine/tests/native_composite_adoption.rs index 38f32cd1eb..d6eee8153c 100644 --- a/crates/engine/tests/native_composite_adoption.rs +++ b/crates/engine/tests/native_composite_adoption.rs @@ -186,6 +186,39 @@ fn assert_dimension_alias(path: &Path, name: &str) { } } +#[test] +fn native_dimension_reuses_an_equal_same_named_composite_font_with_a_longer_spelling() { + let _context = FontContext::hold(); + let dir = TempDir::new("native-composite-dimension-same-name"); + let path = dir.join("dimension.vectorcraft"); + // 86 ASCII letters are 86 bytes; 86 Kelvin signs are the same name in any case and 258 bytes. + let incoming = CompositeFont::new("k".repeat(86), "Source Sans 3", "Regular"); + let mut source = destination(std::slice::from_ref(&incoming)); + let options = json!({"fontFamily": incoming.name, "fontStyle": "Regular"}); + source.execute("dimension.create", &json!({"a": [4, 20], "b": [60, 20], "options": options})).unwrap(); + let source_doc = document(&source).clone(); + source.execute("type.compositeFonts.set", &json!({"fonts": []})).unwrap(); + write_native(&path, &source_doc); + let mut existing = incoming.clone(); + existing.name = "\u{212A}".repeat(86); + assert!(existing.name.len() > 256, "CONTROL: the destination's spelling is longer than a dimension's family keeps"); + assert!(CompositeFont::same_name(&existing.name, &incoming.name), "CONTROL: the two spellings are one name"); + for linked in [false, true] { + let mut session = destination(std::slice::from_ref(&existing)); + let id = place(&mut session, &path, linked); + assert_eq!( + document(&session).composite_fonts.len(), + 1, + "REGRESSION: an equal composite font of the same name is reused, not copied under a new name" + ); + let mut stored = Vec::new(); + document(&session).node(id).unwrap().walk(&mut |node| { + stored.extend(node.extras().dimension.as_ref().map(|d| d.options.font_family.clone())); + }); + assert_eq!(stored, vec![incoming.name.clone()], "REGRESSION: the dimension keeps its own spelling of the reused name"); + } +} + #[test] fn native_composite_alias_skips_a_family_named_by_a_dimension_in_brush_art() { let _context = FontContext::hold(); diff --git a/docs/mcp.md b/docs/mcp.md index 76d0797f83..3a9f3c75db 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -11790,6 +11790,8 @@ with `link: false` or from `dataBase64` it is one group. `at` centres it, `rect` swaps the selected object (keeping its place and transform), `template` puts it on a new template layer. When placed editable art names a font family it does not define as a composite font, and the document defines a composite font of that name, the placed type gets a free name such as "Name 2" so it keeps the font it was laid out in. +A placed live dimension's label options take the same new name, so a redrawn label keeps that font; a document +composite font with the same name and the same definition is reused, and the dimension keeps its own spelling of it. A DCS file (Desktop Color Separation, version 1.0 or 2.0: an EPS file whose header names one EPS program per printing plate, ) is placed only from its `path` with link, as one linked image ([Import DCS files](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-other-file-types/import-dcs-files.html)).