diff --git a/crates/eps/src/import/ai.rs b/crates/eps/src/import/ai.rs index 92847a96ef..09f553ad2f 100644 --- a/crates/eps/src/import/ai.rs +++ b/crates/eps/src/import/ai.rs @@ -37,10 +37,10 @@ use crate::ps; pub(crate) use blend::{MAX_BLEND_OBJECTS, MAX_KEYS as MAX_BLEND_KEYS, PLUGIN as BLEND_PLUGIN, blend_reads}; pub(crate) use brush::{BrushesRead, MANAGER_NAME as BRUSH_MANAGER, brushes_kept, plugin_name as brush_plugin_name, read_brush_object}; pub(crate) use envelope::DRAWN as ENVELOPE_DRAWN; -pub(crate) use envelope::MAX_CHECKED; pub(crate) use envelope::TYPE_DRAWN as ENVELOPE_TYPE_DRAWN; pub(crate) use envelope::{GridPoint as EnvelopeGridPoint, PLUGIN as DEFORM_PLUGIN, outline as envelope_outline, upright as envelope_upright}; pub(crate) use envelope::{MAX_ANY as ENVELOPE_MAX_ANY, MAX_SOLID as ENVELOPE_MAX_SOLID}; +pub(crate) use envelope::{MAX_CHECKED, MAX_DASH_CHECKS}; pub(crate) use envelope::{WARPED_TYPE_DRAWN, WarpedType}; #[cfg(test)] pub(crate) use frames::TEXT_FRAME_SCANS; diff --git a/crates/eps/src/import/ai/envelope.rs b/crates/eps/src/import/ai/envelope.rs index 501b9f4b00..b70329ae11 100644 --- a/crates/eps/src/import/ai/envelope.rs +++ b/crates/eps/src/import/ai/envelope.rs @@ -59,6 +59,9 @@ pub(crate) fn drawn_note(typed: bool) -> &'static str { pub(super) const MAX_PENDING: usize = 64; /// The most envelopes and warped text objects one import draws to check; more keep their drawn art. pub(crate) const MAX_CHECKED: usize = 256; +/// The most strokes with dashes aligned to corners one import draws to check against their drawn +/// looks (apart from [`MAX_CHECKED`]); more keep their drawn looks. +pub(crate) const MAX_DASH_CHECKS: usize = 256; /// The most rows or columns of a mesh read (Make with Mesh's own limit). const MAX_GRID: u32 = 50; /// How far, in points over width and height together, a saved size may lie from the contents' diff --git a/crates/eps/src/import/ai/read.rs b/crates/eps/src/import/ai/read.rs index b045dd128f..6c2071c0d0 100644 --- a/crates/eps/src/import/ai/read.rs +++ b/crates/eps/src/import/ai/read.rs @@ -593,6 +593,9 @@ struct Reader<'a> { envelope_checks: usize, /// How far the live envelopes read so far draw from their drawn art ([`super::envelope::Spent`]). envelope_spent: super::envelope::Spent, + /// How many more strokes with dashes aligned to corners this import may draw to check + /// ([`super::MAX_DASH_CHECKS`]). + dash_checks: usize, /// The text objects read with a Warp ([`super::envelope::WarpedType`]). warped_type: Vec, /// The envelopes of type read with their drawn art, settled once the type is laid out @@ -822,6 +825,7 @@ impl<'a> Reader<'a> { envelopes: Vec::new(), envelope_checks: super::envelope::MAX_CHECKED, envelope_spent: super::envelope::Spent::default(), + dash_checks: super::MAX_DASH_CHECKS, warped_type: Vec::new(), typed_envelopes: Vec::new(), envelopes_drawn: Vec::new(), diff --git a/crates/eps/src/import/ai/read/directives.rs b/crates/eps/src/import/ai/read/directives.rs index 8ca3781717..67b3e799e2 100644 --- a/crates/eps/src/import/ai/read/directives.rs +++ b/crates/eps/src/import/ai/read/directives.rs @@ -837,11 +837,13 @@ impl<'a> Reader<'a> { } /// Lend `sub`, a reader of a definition, what this import may still spend on laying out blends - /// and checking envelopes ([`Reader::take_back_budgets`] takes back what is left). + /// and checking envelopes and aligned dashes ([`Reader::take_back_budgets`] takes back what is + /// left). fn lend_budgets(&mut self, sub: &mut Reader<'_>) { sub.blend_budget = self.blend_budget; sub.envelope_checks = self.envelope_checks; sub.envelope_spent = std::mem::take(&mut self.envelope_spent); + sub.dash_checks = self.dash_checks; } /// Take back from `sub` what [`Self::lend_budgets`] lent it, less what it spent. @@ -849,6 +851,7 @@ impl<'a> Reader<'a> { self.blend_budget = sub.blend_budget; self.envelope_checks = sub.envelope_checks; self.envelope_spent = std::mem::take(&mut sub.envelope_spent); + self.dash_checks = sub.dash_checks; } /// In a pattern's definition, the operands of its first operator start with its head, diff --git a/crates/eps/src/import/ai/read/ops.rs b/crates/eps/src/import/ai/read/ops.rs index 721a6988ba..60bf922c63 100644 --- a/crates/eps/src/import/ai/read/ops.rs +++ b/crates/eps/src/import/ai/read/ops.rs @@ -835,15 +835,30 @@ impl<'a> Reader<'a> { }) }); // An image with a Warp is the object only where its live warp draws as its drawn look, and - // while the import has a check left for it; otherwise its drawn look stays. + // while the import has a check left for it; otherwise its drawn look stays. So is a stroke + // with dashes aligned to corners and path ends, whose fitting is VectorCraft's own, within + // checks of its own ([`crate::import::ai::MAX_DASH_CHECKS`]) and without weighing the page: + // the envelopes' checks and share of the page check stay theirs alone. Type with such + // dashes keeps its drawn look, as before. let appearance = match appearance { + Some(ap) if aligns_dashes(&ap) && slot_of(object.name.as_deref()).is_some() => None, Some(ap) if matches!(object.kind, NodeKind::Image(_)) && ap.effects.iter().any(|e| e.id.starts_with("warp.")) => { let alike = self.envelope_checks > 0 && { self.envelope_checks -= 1; let layer = self.doc.alloc_id(); let shared = crate::import::ai::envelope::Shared { patterns: &self.doc.patterns, images: &self.doc.images }; let shown = !hidden && object.visible && look.visible; - image_warp_alike(object, &ap, look, &self.hidden_ids, hidden, layer, &shared, page.filter(|_| shown), &mut self.envelope_spent) + look_alike(object, &ap, look, &self.hidden_ids, hidden, layer, &shared, page.filter(|_| shown), &mut self.envelope_spent) + }; + alike.then_some(ap) + } + Some(ap) if aligns_dashes(&ap) => { + let alike = self.dash_checks > 0 && { + self.dash_checks -= 1; + let layer = self.doc.alloc_id(); + let shared = crate::import::ai::envelope::Shared { patterns: &self.doc.patterns, images: &self.doc.images }; + let mut spent = crate::import::ai::envelope::Spent::default(); + look_alike(object, &ap, look, &self.hidden_ids, hidden, layer, &shared, None, &mut spent) }; alike.then_some(ap) } @@ -1187,18 +1202,19 @@ fn moves_with(object: &Node, st: &crate::import::ai::style::ArtStyle) -> bool { /// Is `object` an image without an opacity mask, and `st` one Warp on it with nothing else that /// paints ([`crate::import::ai::style::ArtStyle::image_warp`])? Such an image is read with its Warp -/// live where that draws as its drawn look ([`image_warp_alike`]). +/// live where that draws as its drawn look ([`look_alike`]). fn image_warps(object: &Node, st: &crate::import::ai::style::ArtStyle) -> bool { matches!(object.kind, NodeKind::Image(_)) && object.extras().mask.is_none() && st.image_warp } -/// Does image `object` with `appearance` (its style's Warp) draw as `look`, its drawn look, compared -/// over all that the warp draws ([`crate::import::ai::envelope::warped_alike`]) on `layer`, a layer -/// id no other node has? Both are drawn shown, opaque and in the Normal mode; a hidden look is drawn -/// with every object in it shown, as the hidden object's look holds them. Unless the look is on -/// `%_` lines itself (`hidden`), the copies in it that are on `%_` lines (`hidden_ids`) are left out. +/// Does `object` with `appearance` (an image's Warp, or a stroke with dashes aligned to corners) +/// draw as `look`, its drawn look, compared over all that it draws, effects included +/// ([`crate::import::ai::envelope::warped_alike`]), on `layer`, a layer id no other node has? Both +/// are drawn shown, opaque and in the Normal mode; a hidden look is drawn with every object in it +/// shown, as the hidden object's look holds them. Unless the look is on `%_` lines itself +/// (`hidden`), the copies in it that are on `%_` lines (`hidden_ids`) are left out. #[allow(clippy::too_many_arguments)] -fn image_warp_alike( +fn look_alike( object: &Node, appearance: &Appearance, look: &Node, @@ -1224,6 +1240,11 @@ fn image_warp_alike( crate::import::ai::envelope::warped_alike(&live, &plain, layer, shared, page, spent) } +/// Does `appearance` stroke with dashes aligned to corners and path ends? +fn aligns_dashes(appearance: &Appearance) -> bool { + appearance.items.iter().any(|i| matches!(i, AppearanceItem::Stroke(s) if s.dash.as_ref().is_some_and(|d| d.align_corners && d.is_dashed()))) +} + /// Show every object in container `n`, at any depth. fn show_members(n: &mut Node) { for c in n.children_mut().into_iter().flatten() { @@ -1245,7 +1266,7 @@ fn styled(object: &Node, look: &Node, st: &crate::import::ai::style::ArtStyle, p let slot = slot_of(object.name.as_deref()).is_some(); // Several fills and strokes, on a path or a compound path. let stacked = !slot && !st.simple && st.stacked && matches!(object.kind, NodeKind::Path { .. } | NodeKind::Compound { .. }); - // One Warp on an image, checked against its drawn look by the caller ([`image_warp_alike`]). + // One Warp on an image, checked against its drawn look by the caller ([`look_alike`]). let image_warp = !st.simple && image_warps(object, st); // A symbol instance draws its symbol's art: it keeps its drawn look. if !st.plain diff --git a/crates/eps/src/import/ai/style.rs b/crates/eps/src/import/ai/style.rs index e0068a52be..aea1ec9906 100644 --- a/crates/eps/src/import/ai/style.rs +++ b/crates/eps/src/import/ai/style.rs @@ -1461,11 +1461,10 @@ fn stroke_item(f: &Filter, align: StrokeAlign) -> Result, ()> if !f.paints.0 { return Ok(None); } - // Dashes aligned to corners and path ends draw as the look's pieces: a solid end at each end - // and the pattern, fitted, between them. - if f.params.get("BSDashAdj").is_some_and(|v| *v != 0.0) { - return Err(()); - } + // Dashes aligned to corners and path ends (the switch in the stroke's dictionary): the look + // draws them fitted between solid ends, which the reader checks the stroke against before it + // takes it. + let aligned = f.params.get("BSDashAdj").is_some_and(|v| *v != 0.0); match &f.stroke { Some(p) if !f.gradient && f.pattern.is_none() => Ok(Some(StyleItem::Stroke { paint: p.clone(), @@ -1473,7 +1472,7 @@ fn stroke_item(f: &Filter, align: StrokeAlign) -> Result, ()> cap: f.cap, join: f.join, miter: f.miter, - dash: f.dash.clone(), + dash: f.dash.clone().map(|d| Dash { align_corners: aligned, ..d }), profile: width_profile(&f.arrays), align, brush: None, diff --git a/crates/eps/src/import/ai/style/tests.rs b/crates/eps/src/import/ai/style/tests.rs index c4b006fe2a..b5cd7db1cb 100644 --- a/crates/eps/src/import/ai/style/tests.rs +++ b/crates/eps/src/import/ai/style/tests.rs @@ -61,13 +61,17 @@ fn a_uniform_or_unreadable_width_profile_stays_a_live_stroke() { } #[test] -fn dashes_aligned_to_corners_keep_the_drawn_look() { - // GUARDS: the look holds the fitted pattern and the solid ends the style's dash array doesn't. +fn dashes_aligned_to_corners_read_as_aligned_dashes() { + // The look holds the fitted pattern, which the style's dash array doesn't: the reader takes the + // stroke only where VectorCraft's own fitting draws as that look. let data = b"/KnownStyle :\n(Stitches) /Name ,\n/ActiveStyle :\n/CompoundFilter :\n(Chain Style Filter) 0 0 /Filter ,\n1 /Visible ,\n\ /BasicFilter :\n(Stroke Style Filter) 0 0 /Filter ,\n1 /Visible ,\n2 /FillOrStroke ,\n/Dictionary : /NotRecorded ,\n\ /Dictionary : /NotRecorded ,\n1 /Bool (BSDashAdj) ,\n; (kAIBeautifulStrokesParamsDictKey) ,\n\ /StrokeStyle : 0 R\n0 0 0 0 0 0 1 XA\n1 J 0 j 2 w 10 M [5 10 ]0 d\n0 XR\n1 0 Xd\n/Def ;\n (StrokeStyle) ,\n; /Dict ;\n /Part ,\n;\n"; - assert!(!art_styles(data)["Stitches"].plain); + let style = &art_styles(data)["Stitches"]; + assert!(style.plain, "{style:?}"); + let [StyleItem::Stroke { dash: Some(dash), .. }] = style.items.as_slice() else { panic!("{:?}", style.items) }; + assert_eq!((dash.pattern.as_slice(), dash.align_corners), ([5.0, 10.0].as_slice(), true)); } /// The style `Shade`: a Drop Shadow whose dictionary holds `keys`. diff --git a/crates/eps/src/import/ai/tests.rs b/crates/eps/src/import/ai/tests.rs index 68ba060352..adef4a4dc5 100644 --- a/crates/eps/src/import/ai/tests.rs +++ b/crates/eps/src/import/ai/tests.rs @@ -6216,21 +6216,13 @@ fn a_stroke_offset_filter_keeps_its_strokes_style() { } /// A Stroke Offset stroke this doesn't read as a stroke keeps the object's drawn look: a switch -/// written as another kind, dashes aligned to corners and path ends, a gradient. +/// written as another kind, a gradient. #[test] fn a_stroke_offset_filter_with_a_stroke_this_does_not_read_keeps_the_drawn_look() { let outside = "0 /Bool (StrokeOffsetInside) ,\n"; let gradient = "0 R\nBb\n0 0 0 0 Bh\n2 (Red ramp) 0 0 90 1 1 0 0 1 0 0 1 Bg\n0 BB\n0 J 0 j 2 w 10 M []0 d\n0 XR\n1 0 Xd"; let cases = [ ("a switch written as a number", offset_stroke_style_with("Edge", "1 /Int (StrokeOffsetInside) ,\n", OFFSET_STROKE)), - ( - "dashes aligned to corners", - offset_stroke_style_with( - "Edge", - &format!("{outside}/Dictionary : /NotRecorded ,\n1 /Bool (BSDashAdj) ,\n; (kAIBeautifulStrokesParamsDictKey) ,\n"), - &OFFSET_STROKE.replace("[]0 d", "[5 10 ]0 d"), - ), - ), ("a gradient", offset_stroke_style_with("Edge", outside, gradient)), ]; for (why, style) in cases { @@ -6239,6 +6231,26 @@ fn a_stroke_offset_filter_with_a_stroke_this_does_not_read_keeps_the_drawn_look( } } +/// A Stroke Offset stroke with dashes aligned to corners and path ends reads as such a stroke, +/// outside the path; the object takes it only where it draws as its drawn look. Here the look (a +/// filled square) draws otherwise, so the look check keeps the drawn look. +#[test] +fn a_stroke_offset_with_dashes_aligned_to_corners_unlike_its_look_keeps_the_look() { + use crate::import::ai::style::StyleItem; + use vectorcraft_doc::StrokeAlign; + let keys = "0 /Bool (StrokeOffsetInside) ,\n/Dictionary : /NotRecorded ,\n1 /Bool (BSDashAdj) ,\n; (kAIBeautifulStrokesParamsDictKey) ,\n"; + let style = offset_stroke_style_with("Edge", keys, &OFFSET_STROKE.replace("[]0 d", "[5 10 ]0 d")); + let styles = crate::import::ai::style::art_styles(style.as_bytes()); + let Some(StyleItem::Stroke { dash: Some(dash), align, .. }) = styles["Edge"].items.iter().find(|i| matches!(i, StyleItem::Stroke { .. })) else { + panic!("{:?}", styles["Edge"]) + }; + assert_eq!((dash.pattern.as_slice(), dash.align_corners, *align), ([5.0, 10.0].as_slice(), true, StrokeAlign::Outside)); + let r = read_ok(&format!("%AI9_BeginArtStyles\n{style}%AI9_EndArtStyles\n{}", layer(&styled_square("Edge")))); + assert!(r.warnings.iter().any(|w| w.contains("drawn look")), "{:?}", r.warnings); + let a = art(&r.doc); + assert!(matches!(a.as_slice(), [n] if matches!(n.kind, NodeKind::Group { .. })), "the drawn look: {a:#?}"); +} + /// A listed `[Default]` style whose stroke is aligned outside comes in as the default graphic /// style with that stroke. #[test] diff --git a/crates/eps/src/import/tests_native.rs b/crates/eps/src/import/tests_native.rs index dbac845dad..c88a071d42 100644 --- a/crates/eps/src/import/tests_native.rs +++ b/crates/eps/src/import/tests_native.rs @@ -7198,6 +7198,122 @@ fn warped_type_that_draws_apart_from_its_look_comes_in_as_the_look() { } } +/// An art style section with style `Dashed`: a black stroke 2 units wide with butt caps, miter joins +/// and dashes of 6 and gaps of 4, aligned to corners and path ends (`BSDashAdj`). +fn aligned_dash_style() -> String { + "%AI9_BeginArtStyles\n/KnownStyle :\n(Dashed) /Name ,\n/ActiveStyle :\n/CompoundFilter :\n(Chain Style Filter) 0 0 /Filter ,\n1 /Visible ,\n\ + /BasicFilter :\n(Stroke Style Filter) 0 0 /Filter ,\n1 /Visible ,\n2 /FillOrStroke ,\n/Dictionary : /NotRecorded ,\n\ + /Dictionary : /NotRecorded ,\n1 /Bool (BSDashAdj) ,\n; (kAIBeautifulStrokesParamsDictKey) ,\n\ + /StrokeStyle : 0 R\n0 0 0 1 0 0 0 XA\n0 J 0 j 2 w 10 M [6 4 ]0 d\n0 XR\n1 0 Xd\n/Def ;\n (StrokeStyle) ,\n; /Dict ;\n /Part ,\n;\n%AI9_EndArtStyles\n" + .to_string() +} + +/// The open path of [`aligned_dash_file`], a corner at (80, 20), in the document's coordinates. +fn aligned_dash_path() -> kurbo::BezPath { + let mut bp = kurbo::BezPath::new(); + bp.move_to((20.0, 20.0)); + bp.line_to((80.0, 20.0)); + bp.line_to((80.0, 70.0)); + bp +} + +/// What [`aligned_dash_style`] draws along [`aligned_dash_path`], with its dashes aligned to corners +/// and path ends by VectorCraft or (`aligned` false) not, as look operators in black. +fn aligned_dash_look(aligned: bool) -> String { + use vectorcraft_color::{Color, Paint}; + use vectorcraft_doc::{Dash, LineCap, LineJoin, StrokeLayer}; + let mut stroke = StrokeLayer::new(Paint::solid(Color::BLACK), 2.0); + (stroke.cap, stroke.join, stroke.miter_limit) = (LineCap::Butt, LineJoin::Miter, 10.0); + stroke.dash = Some(Dash { pattern: vec![6.0, 4.0], offset: 0.0, align_corners: aligned }); + let outline = vectorcraft_effects::stroke::line_outline(&aligned_dash_path(), &stroke, 2.0, 0.01); + assert!(!outline.elements().is_empty()); + drawn_as_operators(&outline).replace("0 1 1 0 1 0 0 Xa", "0 0 0 1 0 0 0 Xa") +} + +/// The editing copy of [`aligned_dash_path`] under [`aligned_dash_style`], with the look `drawing`. +fn aligned_dash_file(drawing: &str) -> String { + aligned_dash_files(drawing, 1) +} + +/// [`aligned_dash_file`] with `n` such paths. +fn aligned_dash_files(drawing: &str, n: usize) -> String { + let own = "%_0 0 0 1 0 0 0 XA\n%_2 w\n%_20 80 m\n%_80 80 L\n%_80 30 L\n%_S\n"; + let one = format!("u\n{drawing}\nU\n9 () XW\n{own}1 (Dashed) XW\n"); + native(&(aligned_dash_style() + &layer("Art", true, &one.repeat(n)))) +} + +/// The paths of `d` that stroke with dashes, each with whether its dashes are aligned to corners. +fn dashed_paths(d: &Document) -> Vec { + let mut out = vec![]; + d.walk(|n| { + if matches!(n.kind, NodeKind::Path { .. }) { + for i in &n.appearance.items { + if let vectorcraft_doc::AppearanceItem::Stroke(s) = i + && let Some(dash) = &s.dash + { + out.push(dash.align_corners); + } + } + } + }); + out +} + +/// A stroke whose dashes are aligned to corners and path ends comes in as the path with that stroke, +/// editable, where VectorCraft's fitting draws as its drawn look. +#[test] +fn dashes_aligned_to_corners_that_draw_as_the_look_come_in_live() { + let (d, notes) = crate::ai_alone(&private(&aligned_dash_file(&aligned_dash_look(true)))).unwrap(); + assert_eq!(dashed_paths(&d), [true], "{notes:?}"); + assert!(!notes.iter().any(|w| w.contains("drawn look")), "{notes:?}"); +} + +/// Where the look draws the dashes elsewhere (here, not fitted), the path keeps its drawn look. +#[test] +fn dashes_aligned_to_corners_that_draw_apart_from_the_look_keep_it() { + let (d, notes) = crate::ai_alone(&private(&aligned_dash_file(&aligned_dash_look(false)))).unwrap(); + assert!(dashed_paths(&d).is_empty(), "{notes:?}"); + let looks = d.layers[0].children().unwrap(); + assert!(matches!(looks.as_slice(), [l] if matches!(l.kind, NodeKind::Group { .. }) && l.visual_bounds().is_some()), "{looks:#?}"); +} + +/// The strokes with aligned dashes one import checks against their looks are limited +/// ([`crate::import::ai::MAX_DASH_CHECKS`]), apart from the envelopes' checks: past the +/// limit, a path that would draw as its look keeps the look. +#[test] +fn dashes_aligned_to_corners_past_their_checks_keep_the_look() { + let n = crate::import::ai::MAX_DASH_CHECKS; + let (d, notes) = crate::ai_alone(&private(&aligned_dash_files(&aligned_dash_look(true), n + 1))).unwrap(); + assert_eq!(dashed_paths(&d).len(), n, "{notes:?}"); + let looks = d.layers[0].children().unwrap().iter().filter(|c| matches!(c.kind, NodeKind::Group { .. })).count(); + assert_eq!(looks, 1); +} + +/// Type under a style with dashes aligned to corners keeps its drawn look, as before, and takes no +/// such stroke: the look check isn't made for type. +#[test] +fn type_with_dashes_aligned_to_corners_keeps_its_drawn_look() { + let doc = text_document(&[("TILT\r", 0, (8200.0, 8180.0), (0.0, 0.0), 0.0)]); + let own: String = text_object(0).lines().map(|l| format!("%_{l}\n")).collect(); + let look = format!("u\n{}\nU\n9 () XW\n", aligned_dash_look(true)); + let file = native_with_text(&(aligned_dash_style() + &layer("Art", true, &format!("{look}{own}1 (Dashed) XW\n"))), &doc); + let (d, notes) = crate::ai_alone(&private(&file)).unwrap(); + // The look stays, and the type takes no stroke with aligned dashes. + let kids = d.layers[0].children().unwrap(); + assert!(kids.iter().any(|c| matches!(c.kind, NodeKind::Group { .. }) && c.visual_bounds().is_some()), "{notes:?}"); + let mut aligned = false; + d.walk(|n| { + if matches!(n.kind, NodeKind::Text(_)) { + aligned |= n + .appearance + .items + .iter() + .any(|i| matches!(i, vectorcraft_doc::AppearanceItem::Stroke(s) if s.dash.as_ref().is_some_and(|d| d.align_corners))); + } + }); + assert!(!aligned, "{notes:?}"); +} + /// Warped type that a text variable binds stays type with the effect live and keeps its binding, /// though its live warp draws apart from its look. #[test]