mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 22:33:08 +00:00
.ai import: guides past an object's art keep the drawn look under Transform and Free Distort
A compound path or group with a Transform that scales, rotates or reflects was read live whatever guides it held, and so was a group with a Free Distort that moves a corner. The live effect is measured from bounds that take in the guides, so where a guide reaches past the rest of the object's art, the live effect is placed from a larger box than the art. Such an object now opens as its drawn look. Guides are looked for in members at any depth, a guide in a group inside the object included, and a hidden member counts as art. These keep opening live: a Transform that only moves, a Transform with Transform Objects off, a Free Distort on a compound path, and objects whose guides lie inside the rest of their art. The `.ai` writer keeps a graphic-style link only where the reader reads the style live, so such an object is now written without the link, with the existing style-link warning.
This commit is contained in:
@@ -7682,6 +7682,74 @@ fn a_link_to_a_twist_style_is_kept_where_the_reader_reads_it() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A link to a graphic style with Transform or Free Distort is kept where the reader reads the
|
||||
/// effect. On a compound path of a square and a guide: Transform with the guide inside the square,
|
||||
/// moving only, or with Transform Objects off (Transform Patterns on or off), and Free Distort. On a
|
||||
/// group holding such a compound path: Transform with the guide inside. Transform that scales where
|
||||
/// the guide reaches past the square is written without the link and warns.
|
||||
#[test]
|
||||
fn a_link_to_a_transform_or_free_distort_style_with_a_guide_is_kept_where_the_reader_reads_it() {
|
||||
// A compound path of a filled square and a guide from `a` to `b`, with appearance `ap`.
|
||||
fn guided(doc: &mut Document, ap: Appearance, a: (f64, f64), b: (f64, f64)) -> Node {
|
||||
let square = Node::path(doc.alloc_id(), shapes::rectangle(Rect::new(10.0, 10.0, 50.0, 40.0)), Appearance::default());
|
||||
let path = shapes::rectangle(Rect::new(a.0, a.1, b.0, b.1));
|
||||
let guide = Node::new(doc.alloc_id(), NodeKind::Path { path, rule: FillRule::NonZero, live: None, clipping: false, guide: true });
|
||||
let mut n = Node::new(doc.alloc_id(), NodeKind::Compound { children: vec![Arc::new(square), Arc::new(guide)], rule: FillRule::NonZero });
|
||||
n.appearance = ap.into();
|
||||
n
|
||||
}
|
||||
// A document with that compound path (in a group when `grouped`) with effect `e`, linked to a
|
||||
// graphic style of that appearance.
|
||||
let make = |e: vectorcraft_doc::Effect, grouped: bool, a: (f64, f64), b: (f64, f64)| {
|
||||
let mut doc = Document::new(200.0, 100.0);
|
||||
let filled = Appearance::basic(black_fill(), Paint::None, 0.0);
|
||||
let ap = Appearance { effects: vec![e], ..if grouped { Appearance::default() } else { filled.clone() } };
|
||||
let mut g = vectorcraft_doc::GraphicStyle::new("Moved", ap.clone());
|
||||
g.id = 9;
|
||||
doc.graphic_styles.push(g);
|
||||
let mut n = if grouped {
|
||||
let member = guided(&mut doc, filled, a, b);
|
||||
let mut n = Node::group(doc.alloc_id(), vec![Arc::new(member)]);
|
||||
n.appearance = ap.into();
|
||||
n
|
||||
} else {
|
||||
guided(&mut doc, ap, a, b)
|
||||
};
|
||||
n.extras_mut().graphic_style = Some(9);
|
||||
put(&mut doc, n);
|
||||
doc
|
||||
};
|
||||
let scale = || fx("distort.transform", serde_json::json!({"copies": 0, "scaleH": 50.0, "scaleV": 50.0}));
|
||||
let moved = || fx("distort.transform", serde_json::json!({"copies": 0, "moveH": 5.0}));
|
||||
let in_place = || fx("distort.transform", serde_json::json!({"copies": 0, "scaleH": 50.0, "objects": false, "patterns": false}));
|
||||
let patterns_only = || fx("distort.transform", serde_json::json!({"copies": 0, "scaleH": 50.0, "objects": false, "patterns": true}));
|
||||
let slant = || fx("distort.freeDistort", serde_json::json!({"corners": [[0.2, 0.0], [1.2, 0.0], [1.0, 1.0], [0.0, 1.0]]}));
|
||||
let (inside, past) = (((20.0, 20.0), (30.0, 30.0)), ((20.0, 20.0), (90.0, 30.0)));
|
||||
for (what, doc, id, kept) in [
|
||||
("a compound path scaled, its guide inside", make(scale(), false, inside.0, inside.1), "distort.transform", true),
|
||||
("a compound path moved, its guide past", make(moved(), false, past.0, past.1), "distort.transform", true),
|
||||
("a compound path scaled with Transform Objects off, its guide past", make(in_place(), false, past.0, past.1), "distort.transform", true),
|
||||
(
|
||||
"a compound path scaled with Transform Objects off and Transform Patterns on, its guide past",
|
||||
make(patterns_only(), false, past.0, past.1),
|
||||
"distort.transform",
|
||||
true,
|
||||
),
|
||||
("a compound path distorted, its guide past", make(slant(), false, past.0, past.1), "distort.freeDistort", true),
|
||||
("a group scaled, its guide inside", make(scale(), true, inside.0, inside.1), "distort.transform", true),
|
||||
("a compound path scaled, its guide past", make(scale(), false, past.0, past.1), "distort.transform", false),
|
||||
("a group scaled, its guide past", make(scale(), true, past.0, past.1), "distort.transform", false),
|
||||
] {
|
||||
let out = export_ai_editing(&doc, &opts()).unwrap();
|
||||
assert_eq!(out.warnings.iter().any(|w| w == writer::STYLE_LINKS), !kept, "{what}: {:?}", out.warnings);
|
||||
let back = read_back(&doc);
|
||||
let n = &back.layers[0].children().unwrap()[0];
|
||||
let style = n.extras().graphic_style.and_then(|id| back.graphic_style_by_id(id)).map(|g| g.name.as_str());
|
||||
assert_eq!(style, kept.then_some("Moved"), "{what}: {n:?}");
|
||||
assert_eq!(n.appearance.effects.iter().any(|e| e.id == id), kept, "{what}: {n:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Twist and Transform on an object that isn't linked to a graphic style are not written as a style
|
||||
/// of its own: the object reads back without live effects.
|
||||
#[test]
|
||||
|
||||
@@ -1076,13 +1076,47 @@ fn no_guides(n: &Node, depth: usize) -> bool {
|
||||
depth <= MAX_DEPTH && !n.is_guide() && n.children().into_iter().flatten().all(|c| no_guides(c, depth + 1))
|
||||
}
|
||||
|
||||
/// Is `n` a compound path with a guide member whose bounds reach past the bounds of its members that
|
||||
/// aren't guides, or with guide members only? Any other object counts as no.
|
||||
/// Is `n` a compound path or group with a guide in it whose bounds reach past the bounds of the rest
|
||||
/// of its art, or with guides only? Members are looked into to [`MAX_DEPTH`]; deeper counts as yes.
|
||||
/// Any other object counts as no.
|
||||
fn guides_reach_past(n: &Node) -> bool {
|
||||
let NodeKind::Compound { children, .. } = &n.kind else { return false };
|
||||
let bounds =
|
||||
|guides: bool| children.iter().filter(|c| c.is_guide() == guides).fold(None, |acc, c| vectorcraft_geom::union_opt(acc, c.geometric_bounds()));
|
||||
bounds(true).is_some_and(|g| bounds(false).is_none_or(|o| o.union(g) != o))
|
||||
/// Adds the bounds of the guides in `n`'s members to `guides` and of the rest to `art`; false when
|
||||
/// it is deeper than [`MAX_DEPTH`].
|
||||
fn split(n: &Node, depth: usize, guides: &mut Option<Rect>, art: &mut Option<Rect>) -> bool {
|
||||
depth <= MAX_DEPTH
|
||||
&& n.children().into_iter().flatten().all(|c| match c.children() {
|
||||
_ if c.is_guide() => {
|
||||
*guides = vectorcraft_geom::union_opt(*guides, c.geometric_bounds());
|
||||
true
|
||||
}
|
||||
Some(_) => split(c, depth + 1, guides, art),
|
||||
None => {
|
||||
*art = vectorcraft_geom::union_opt(*art, c.geometric_bounds());
|
||||
true
|
||||
}
|
||||
})
|
||||
}
|
||||
if !matches!(n.kind, NodeKind::Compound { .. } | NodeKind::Group { .. }) {
|
||||
return false;
|
||||
}
|
||||
let (mut guides, mut art) = (None, None);
|
||||
!split(n, 0, &mut guides, &mut art) || guides.is_some_and(|g| art.is_none_or(|o| o.union(g) != o))
|
||||
}
|
||||
|
||||
/// Do Transform params `p` scale, turn or reflect, so that where the object's bounds lie changes
|
||||
/// what they draw? Moving alone doesn't.
|
||||
fn transform_measures(p: &serde_json::Value) -> bool {
|
||||
p["scaleH"].as_f64() != Some(100.0)
|
||||
|| p["scaleV"].as_f64() != Some(100.0)
|
||||
|| p["rotate"].as_f64() != Some(0.0)
|
||||
|| p["reflectX"].as_bool() != Some(false)
|
||||
|| p["reflectY"].as_bool() != Some(false)
|
||||
}
|
||||
|
||||
/// Do Free Distort params `p` move a corner of the box, so that where the object's bounds lie
|
||||
/// changes what they draw?
|
||||
fn free_distort_measures(p: &serde_json::Value) -> bool {
|
||||
p["corners"] != serde_json::json!([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]])
|
||||
}
|
||||
|
||||
/// The largest Twist angle, in degrees either way, read as a live effect on an object that isn't a
|
||||
@@ -1101,7 +1135,11 @@ const MAX_STRAIGHT_TWIST: f64 = 180.0;
|
||||
/// compound path with straight segments (only at 0° when its guide members reach past its other
|
||||
/// members, see [`guides_reach_past`]), or up to [`MAX_TWIST`] degrees either way on another object
|
||||
/// with no guide in it. Transform that scales, turns or reflects is read only when nothing is stroked
|
||||
/// (its reference point lies on the bounds without strokes).
|
||||
/// (its reference point lies on the bounds without strokes) and, unless Transform Objects is off,
|
||||
/// no guide reaches past the rest of the object's art ([`guides_reach_past`]). Free Distort that
|
||||
/// moves a corner of a group is read only when no guide in the group reaches past the rest of its
|
||||
/// art. The live effect is measured from bounds that take in the guides, so such an object keeps
|
||||
/// its drawn look.
|
||||
fn moves_with(object: &Node, st: &crate::import::ai::style::ArtStyle) -> bool {
|
||||
use crate::import::ai::style::StyleItem;
|
||||
let stroked = st.items.iter().any(|i| matches!(i, StyleItem::Stroke { paint, .. } if !paint.is_none()))
|
||||
@@ -1121,12 +1159,14 @@ fn moves_with(object: &Node, st: &crate::import::ai::style::ArtStyle) -> bool {
|
||||
}
|
||||
"distort.transform" => {
|
||||
let p = &e.params;
|
||||
!stroked
|
||||
|| (p["scaleH"].as_f64() == Some(100.0)
|
||||
&& p["scaleV"].as_f64() == Some(100.0)
|
||||
&& p["rotate"].as_f64() == Some(0.0)
|
||||
&& p["reflectX"].as_bool() == Some(false)
|
||||
&& p["reflectY"].as_bool() == Some(false))
|
||||
// With Transform Objects off, nothing is placed from the bounds: a style read here and
|
||||
// the object's members paint with no pattern for Transform Patterns to move (`simple`,
|
||||
// `moves_alike`).
|
||||
let placed = p["objects"].as_bool() != Some(false);
|
||||
!transform_measures(p) || (!stroked && (!placed || !guides_reach_past(object)))
|
||||
}
|
||||
"distort.freeDistort" => {
|
||||
!matches!(object.kind, NodeKind::Group { .. }) || !free_distort_measures(&e.params) || !guides_reach_past(object)
|
||||
}
|
||||
_ => true,
|
||||
})
|
||||
|
||||
@@ -7367,6 +7367,92 @@ fn twist_is_read_on_straight_paths_and_compound_paths_up_to_half_a_turn() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transform_and_free_distort_keep_the_drawn_look_where_a_guide_reaches_past_the_rest_of_the_art() {
|
||||
// GUARDS: a compound path or group whose guides reach past the rest of its art was read with a
|
||||
// live Transform that scales, turns or reflects (or, on a group, a live Free Distort) measured from
|
||||
// bounds that take in those guides. Such an object keeps its drawn look.
|
||||
let hide = |s: String| s.replace('\n', "\n%_");
|
||||
// A compound path of a square and `rest`, under its drawn look.
|
||||
let compound = |rest: &str| {
|
||||
format!(
|
||||
"0 Ae\nu\n{RED}{}f\nU\n%_Bb\n%_{}0 BB\n1 (Bent) XW\n",
|
||||
rect_path(16.0, 16.0, 64.0, 54.0),
|
||||
hide(format!("*u\n{}f\n{rest}*U\n", rect_path(20.0, 20.0, 60.0, 50.0)))
|
||||
)
|
||||
};
|
||||
// A group of a red square and `rest`, under its drawn look.
|
||||
let group = |rest: &str| {
|
||||
format!(
|
||||
"0 Ae\nu\n{RED}{}f\nU\n%_0 Ae\n%_u\n%_{}U\n1 (Bent) XW\n",
|
||||
rect_path(16.0, 16.0, 64.0, 54.0),
|
||||
hide(format!("{RED}{}f\n{rest}", rect_path(20.0, 20.0, 60.0, 50.0)))
|
||||
)
|
||||
};
|
||||
let guide = |from: (f64, f64), to: (f64, f64)| format!("{} {} m\n{} {} L\n(N) *\n", from.0, from.1, to.0, to.1);
|
||||
let inside = guide((25.0, 25.0), (35.0, 35.0));
|
||||
let past = guide((30.0, 30.0), (70.0, 40.0));
|
||||
let nested = format!("0 Ae\nu\n{past}U\n");
|
||||
let transform = |keys: &str| format!("{keys}0 /Bool (transformPatterns) ,\n");
|
||||
let scale = transform("0.5 /Real (scaleH_Factor) ,\n0.5 /Real (scaleV_Factor) ,\n0 /Bool (scaleLines) ,\n");
|
||||
let turn = transform("0.5 /Real (rotate_Radians) ,\n");
|
||||
let reflect = transform("1 /Bool (reflectX) ,\n");
|
||||
let moved = transform("5 /Real (moveH_Pts) ,\n");
|
||||
let scale_in_place = transform("0.5 /Real (scaleH_Factor) ,\n0 /Bool (scaleLines) ,\n0 /Bool (transformObjects) ,\n");
|
||||
// Transform Objects off and Transform Patterns on: only a pattern moves, and a solid fill has none.
|
||||
let scale_patterns = "0.5 /Real (scaleH_Factor) ,\n0 /Bool (scaleLines) ,\n0 /Bool (transformObjects) ,\n1 /Bool (transformPatterns) ,\n";
|
||||
let slanted = free_distort_keys([[25.0, 50.0], [65.0, 50.0], [20.0, 20.0], [60.0, 20.0]]);
|
||||
let read = |name: &str, keys: &str, paint, art_: &str| read_ok(&format!("{}{}", distort_styles(name, keys, paint), layer(art_)));
|
||||
let tr = "Adobe Transform";
|
||||
let fd = "Adobe Free Distort";
|
||||
for (what, r, id) in [
|
||||
("a compound path scaled, its guide inside", read(tr, &scale, BLUE_FILL, &compound(&inside)), "distort.transform"),
|
||||
("a compound path moved, its guide past", read(tr, &moved, BLUE_FILL, &compound(&past)), "distort.transform"),
|
||||
("a compound path distorted, its guide past", read(fd, &slanted, BLUE_FILL, &compound(&past)), "distort.freeDistort"),
|
||||
("a group scaled, its guide inside", read(tr, &scale, None, &group(&inside)), "distort.transform"),
|
||||
("a group moved, its guide past", read(tr, &moved, None, &group(&past)), "distort.transform"),
|
||||
("a group scaled with Transform Objects off, its guide past", read(tr, &scale_in_place, None, &group(&past)), "distort.transform"),
|
||||
(
|
||||
"a compound path scaled with Transform Objects off and Transform Patterns on, its guide past",
|
||||
read(tr, scale_patterns, BLUE_FILL, &compound(&past)),
|
||||
"distort.transform",
|
||||
),
|
||||
("a group distorted, its guide inside", read(fd, &slanted, None, &group(&inside)), "distort.freeDistort"),
|
||||
("a group distorted with no corner moved, its guide past", read(fd, "", None, &group(&past)), "distort.freeDistort"),
|
||||
// A hidden member is measured too, and this one takes in the guide.
|
||||
(
|
||||
"a group scaled, its guide past the square and inside a hidden member",
|
||||
read(tr, &scale, None, &group(&format!("1 Xw\n{RED}{}f\n0 Xw\n{past}", rect_path(10.0, 10.0, 80.0, 60.0)))),
|
||||
"distort.transform",
|
||||
),
|
||||
] {
|
||||
assert!(!r.warnings.iter().any(|w| w.contains("drawn look")), "{what}: {:?}", r.warnings);
|
||||
let a = art(&r.doc);
|
||||
assert_eq!(a.len(), 1, "{what}: {a:#?}");
|
||||
assert!(a[0].children().map_or(0, |c| c.len()) >= 2, "{what}: the object, not its look: {a:#?}");
|
||||
let [e] = a[0].appearance.effects.as_slice() else { panic!("{what}: {:?}", a[0].appearance) };
|
||||
assert_eq!(e.id, id, "{what}");
|
||||
}
|
||||
for (what, r) in [
|
||||
("a compound path scaled, its guide past", read(tr, &scale, BLUE_FILL, &compound(&past))),
|
||||
("a compound path turned, its guide past", read(tr, &turn, BLUE_FILL, &compound(&past))),
|
||||
("a compound path reflected, its guide past", read(tr, &reflect, BLUE_FILL, &compound(&past))),
|
||||
("a group scaled, its guide past", read(tr, &scale, None, &group(&past))),
|
||||
("a group turned, its guide past", read(tr, &turn, None, &group(&past))),
|
||||
("a group distorted, its guide past", read(fd, &slanted, None, &group(&past))),
|
||||
("a group scaled, its guide past in a group in it", read(tr, &scale, None, &group(&nested))),
|
||||
(
|
||||
"a group scaled, its guide past in a compound path in it",
|
||||
read(tr, &scale, None, &group(&format!("*u\n{RED}{}f\n{past}*U\n", rect_path(25.0, 25.0, 35.0, 35.0)))),
|
||||
),
|
||||
] {
|
||||
let a = art(&r.doc);
|
||||
assert_eq!(a.len(), 1, "{what}: {a:#?}");
|
||||
assert!(a[0].appearance.effects.is_empty(), "{what}: the drawn look: {a:#?}");
|
||||
assert!(r.warnings.iter().any(|w| w.contains("drawn look")), "{what}: {:?}", r.warnings);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn twist_on_a_straight_compound_path_keeps_the_drawn_look_where_a_guide_reaches_past_its_other_members() {
|
||||
// GUARDS: a straight compound path whose guide members reach past its other members was read
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1670,7 +1670,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
In a `.ai` opened from its editing data, a path or type object with Offset Path, Outline Object or Outline Stroke on the whole object opens as itself with those effects live, in their order and shown or hidden; on a fill or stroke of type they stay on that fill or stroke. Offset Path keeps its Offset, Joins and Miter Limit, and Outline Stroke takes its width from the stroke. An Offset Path with a setting missing, out of range or not read keeps the drawn art, as does a path with a Path effect on one fill or stroke (`crates/eps/src/import/ai/style/tests.rs`, `crates/eps/src/import/ai/tests.rs`, `crates/eps/src/import/tests_native.rs`).
|
||||
In a `.ai` or EPS opened from its editing data, a Scatter brush whose last 15 numbers start with 1 comes into the Brushes panel in Tints, and each brush art section is read without its frame, an unnamed, unpainted rectangle with no settings of its own at the art's bounds (within 1/1000 pt). A wider frame stays in the art, and an art group with a state or look of its own (hidden, locked, isolated, knockout, an opacity, a blend mode, a name, a mask, a note or an appearance) stays a group (`crates/eps/src/import/ai/tests.rs`).
|
||||
In a `.ai` opened from its editing data, type with Warp on the object or on one of its fills or strokes opens as type with the effect live when the live warp draws as the art saved with it. When it draws differently, or when the file's limit of checks has no check left for it, it opens as that art, and one note says how many did. Type stays live without a comparison when the saved art paints nothing or draws type of its own, in frames of threaded type, when a text variable binds it, and when its text object doesn't come in as one text object (`crates/eps/src/import/ai/envelope.rs`, `crates/eps/src/import/tests_native.rs`).
|
||||
In a `.ai` opened from its editing data, an object with a Tweak that moves nothing or Pucker & Bloat at 0 opens as itself with the effect live. A path, compound path or group whose only effect is Transform without copies, or a Free Distort whose corners make a parallelogram, opens as itself with it live, and so does an object with nothing stroked whose only effect is Twist: a path or compound path with straight segments at up to 180° either way, unless it is a compound path with a guide member that reaches past its other members and the angle is not 0°, and another path, compound path or group with no guide in it at up to 90° either way (limits VectorCraft sets); one twisted further keeps the drawn art. A gradient or pattern, an image, type, a clipping group, an opacity mask or effects inside the object, or a stroke with a Transform that scales, rotates or reflects, keep the drawn art (`crates/eps/src/import/ai/style/tests.rs`, `crates/eps/src/import/ai/tests.rs`, `crates/eps/src/import/ai/read/ops.rs`).
|
||||
In a `.ai` opened from its editing data, an object with a Tweak that moves nothing or Pucker & Bloat at 0 opens as itself with the effect live. A path, compound path or group whose only effect is Transform without copies, or a Free Distort whose corners make a parallelogram, opens as itself with it live (not when a guide in it reaches past the rest of its art, hidden members included, and the Transform scales, rotates or reflects the object with Transform Objects on, or the object is a group and the Free Distort moves a corner), and so does an object with nothing stroked whose only effect is Twist: a path or compound path with straight segments at up to 180° either way, unless it is a compound path with a guide member that reaches past its other members and the angle is not 0°, and another path, compound path or group with no guide in it at up to 90° either way (limits VectorCraft sets); one twisted further keeps the drawn art. A gradient or pattern, an image, type, a clipping group, an opacity mask or effects inside the object, or a stroke with a Transform that scales, rotates or reflects, keep the drawn art (`crates/eps/src/import/ai/style/tests.rs`, `crates/eps/src/import/ai/tests.rs`, `crates/eps/src/import/ai/read/ops.rs`).
|
||||
A PDF page whose `UserUnit` (PDF 1.6) is above 1 opens at its size in points, its numbers times the `UserUnit` (one above 75,000 counts as 75,000), and `document.pdfInfo` gives its size and boxes in points too. A `.ai` whose editing data has its artboards in the units of such a page opens from that data at the page's size, with its layers, and when the layers can't be read its pages still take the artboard layout of the data, scaled the same way (part of #1482).
|
||||
`document.open`, `file.place` and `document.pdfInfo` reject a `cropTo`, `crop`, `password`, `colorMode` or `textAs` that is not a string, such as `"textAs": true`, and `document.pdfInfo` rejects a `thumbnailSize` that is not a number; the error names the param, and nothing opens or is placed. A param left out or given as null keeps its default (`crates/engine/src/cmd/fileio/tests_pdfimport.rs`).
|
||||
When the PDF content of a `.ai` has a page for each artboard, the layers are compared with every page on its artboard (until the pages compared take 8,388,608 pixels at the comparison's scale, about 67 letter-size pages, or number 128): art that only the layers or only a later page draws is handled as art that differs on the first page is, and a text object that a later page doesn't draw is left out unless its layer doesn't print (`crates/eps/src/import/tests_native.rs`).
|
||||
|
||||
Reference in New Issue
Block a user