mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 05:22:58 -05:00
Merge pull request #10860 from dotCooCoo/fix/placed-dimension-composite-font
Place: a placed dimension keeps the composite font its label was renamed to
This commit is contained in:
4 files changed
+198
-17
No files matched your search
@@ -99,12 +99,19 @@ fn family_names(d: &Document) -> Result<BTreeSet<String>> {
|
||||
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);
|
||||
@@ -112,6 +119,9 @@ fn family_names(d: &Document) -> Result<BTreeSet<String>> {
|
||||
}
|
||||
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);
|
||||
}
|
||||
@@ -145,9 +155,15 @@ 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()) {
|
||||
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);
|
||||
}
|
||||
@@ -170,12 +186,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<String>, cmd: &str) -> Result<String> {
|
||||
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())
|
||||
@@ -189,7 +215,13 @@ fn composite_alias(base: &str, reserved: &BTreeSet<String>, 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<CompositeFont>, source_families: BTreeSet<String>, target: &Document, cmd: &str) -> Result<CompositeImport> {
|
||||
pub(super) fn plan_import(
|
||||
source: Vec<CompositeFont>,
|
||||
source_families: BTreeSet<String>,
|
||||
dimension_families: BTreeSet<String>,
|
||||
target: &Document,
|
||||
cmd: &str,
|
||||
) -> Result<CompositeImport> {
|
||||
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")));
|
||||
}
|
||||
@@ -230,12 +262,15 @@ pub(super) fn plan_import(source: Vec<CompositeFont>, 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 mut reused = CompositeFont::named(&target.composite_fonts, &font.name).filter(equivalent);
|
||||
let dimension = dimension_families.contains(&font.name.to_lowercase());
|
||||
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(),
|
||||
|
||||
@@ -545,16 +545,22 @@ 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], composites: bool) -> (Used, Vec<Brush>, BTreeSet<String>) {
|
||||
fn collect_resources(src: &Document, nodes: &mut [Node], composites: bool) -> (Used, Vec<Brush>, BTreeSet<String>, BTreeSet<String>) {
|
||||
let mut used = Used::default();
|
||||
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);
|
||||
@@ -585,18 +591,18 @@ fn collect_resources(src: &Document, nodes: &mut [Node], composites: bool) -> (U
|
||||
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 composites = !src.composite_fonts.is_empty() || !dst.composite_fonts.is_empty();
|
||||
let (used, mut src_brushes, families) = collect_resources(src, nodes, composites);
|
||||
let (used, mut src_brushes, families, dimension_families) = collect_resources(src, nodes, composites);
|
||||
let fonts = src.composite_fonts.iter().filter(|font| families.contains(&font.name.to_lowercase())).cloned().collect::<Vec<_>>();
|
||||
// A family the source does not define as a composite font, which the destination defines as
|
||||
// one, would draw in the destination's composite font after placement.
|
||||
@@ -604,7 +610,7 @@ pub(crate) fn adopt(dst: &mut Document, src: &Document, nodes: &mut [Node], cmd:
|
||||
.iter()
|
||||
.any(|family| CompositeFont::named(&src.composite_fonts, family).is_none() && CompositeFont::named(&dst.composite_fonts, family).is_some());
|
||||
// All representability/validation errors precede incoming or destination mutation.
|
||||
let plan = if fonts.is_empty() && !undefined_clash { None } else { Some(plan_import(fonts, families, dst, cmd)?) };
|
||||
let plan = if fonts.is_empty() && !undefined_clash { None } else { Some(plan_import(fonts, families, dimension_families, dst, cmd)?) };
|
||||
let staged;
|
||||
let mut aliased = false;
|
||||
let src = if let Some(plan) = &plan {
|
||||
|
||||
@@ -140,6 +140,144 @@ 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_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();
|
||||
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();
|
||||
|
||||
@@ -11827,6 +11827,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, <http://www.telegraphics.net/doc/DCS.txt>) 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)).
|
||||
|
||||
Reference in new issue
Block a user