From aa0e47006a1abb6513a6603c012e6cd27ad3de2d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 13 Nov 2026 18:07:53 -0800 Subject: [PATCH] Concept to Vector: traced lines keep their square ends Image Trace marks a centerline whose ink ends straight as square-capped, but the built-in styles dropped the flag, so Insert and the panel's previews gave every line round ends. A variation's path now carries `square_caps`, Insert sets the stroke's cap from it, and previews paint square ends for it. --- crates/engine/src/cmd/conceptvector.rs | 3 +- crates/engine/src/concept_vector.rs | 3 ++ crates/engine/src/concept_vector/offline.rs | 4 +-- crates/engine/src/concept_vector/tests.rs | 28 +++++++++++++++---- crates/ui-egui/src/panels/concept_vector.rs | 16 +++++++---- .../src/panels/concept_vector/tests.rs | 22 +++++++++++++-- 6 files changed, 60 insertions(+), 16 deletions(-) diff --git a/crates/engine/src/cmd/conceptvector.rs b/crates/engine/src/cmd/conceptvector.rs index 85daa223ce..2391284881 100644 --- a/crates/engine/src/cmd/conceptvector.rs +++ b/crates/engine/src/cmd/conceptvector.rs @@ -396,7 +396,8 @@ fn appearance_of(p: &cv::VectorPath, unit: f64) -> Appearance { items.push(AppearanceItem::Fill(FillLayer::new(solid(c)))); } if let Some((c, w)) = p.stroke { - items.push(AppearanceItem::Stroke(StrokeLayer { cap: LineCap::Round, join: LineJoin::Round, ..StrokeLayer::new(solid(c), w * unit) })); + let cap = if p.square_caps { LineCap::Square } else { LineCap::Round }; + items.push(AppearanceItem::Stroke(StrokeLayer { cap, join: LineJoin::Round, ..StrokeLayer::new(solid(c), w * unit) })); } Appearance { items, ..Default::default() } } diff --git a/crates/engine/src/concept_vector.rs b/crates/engine/src/concept_vector.rs index 53884eff0f..fff1fe80d7 100644 --- a/crates/engine/src/concept_vector.rs +++ b/crates/engine/src/concept_vector.rs @@ -92,6 +92,9 @@ pub struct VectorPath { pub fill: Option<[u8; 3]>, /// The stroke color (sRGB) and width in pixels, if it is stroked. pub stroke: Option<([u8; 3], f64)>, + /// Stroked: the stroke's ends are square (cut straight half the width past the path's end); + /// otherwise round. + pub square_caps: bool, } /// The variation a provider made for one style. diff --git a/crates/engine/src/concept_vector/offline.rs b/crates/engine/src/concept_vector/offline.rs index de9221235b..b0fb659ad9 100644 --- a/crates/engine/src/concept_vector/offline.rs +++ b/crates/engine/src/concept_vector/offline.rs @@ -225,8 +225,8 @@ impl Decoded { .map(|tp| { let path = tp.path.transformed(self.back); match tp.stroke { - Some(w) => VectorPath { path, fill: None, stroke: Some((tp.color, w * self.unit)) }, - None => VectorPath { path, fill: Some(tp.color), stroke: None }, + Some(w) => VectorPath { path, fill: None, stroke: Some((tp.color, w * self.unit)), square_caps: tp.square_caps }, + None => VectorPath { path, fill: Some(tp.color), stroke: None, square_caps: false }, } }) .collect(); diff --git a/crates/engine/src/concept_vector/tests.rs b/crates/engine/src/concept_vector/tests.rs index 8a4d67a711..41dda23f0b 100644 --- a/crates/engine/src/concept_vector/tests.rs +++ b/crates/engine/src/concept_vector/tests.rs @@ -104,7 +104,7 @@ fn squares(r: &Request, _: Steps) -> Result, ProviderError> { .iter() .map(|st| Variation { style: st.clone(), - paths: vec![VectorPath { path: sq.clone(), fill: Some([9, 9, 9]), stroke: None }], + paths: vec![VectorPath { path: sq.clone(), fill: Some([9, 9, 9]), stroke: None, square_caps: false }], warnings: vec![], }) .collect()) @@ -204,7 +204,7 @@ fn results_are_checked_whatever_provider_made_them() { let sq = |x: f64| PathData::single(SubPath::polyline(&[Point::new(x, 0.0), Point::new(x + 10.0, 0.0), Point::new(x + 10.0, 10.0)], true)); let v = |style: &str, path: PathData, fill: Option<[u8; 3]>, stroke: Option<([u8; 3], f64)>| Variation { style: style.into(), - paths: vec![VectorPath { path, fill, stroke }], + paths: vec![VectorPath { path, fill, stroke, square_caps: false }], warnings: vec![], }; let ok = vec![v("a", sq(0.0), Some([1, 2, 3]), None), v("b", sq(0.0), None, Some(([0, 0, 0], 2.0)))]; @@ -429,7 +429,7 @@ fn styles_made_one_at_a_time_match_one_call_and_are_checked_as_they_come() { .iter() .map(|st| Variation { style: st.clone(), - paths: vec![VectorPath { path: path.clone(), fill: Some([0; 3]), stroke: None }], + paths: vec![VectorPath { path: path.clone(), fill: Some([0; 3]), stroke: None, square_caps: false }], warnings: vec![], }) .collect()) @@ -540,6 +540,24 @@ fn detail_moves_each_style_within_its_range() { assert!(anchors(100) > anchors(0), "{} vs {}", anchors(100), anchors(0)); } +/// Insert gives a stroked path the ends its variation names: square or round. +#[test] +fn insert_keeps_the_stroke_caps_of_each_path() { + fn lines(r: &Request, _: Steps) -> Result, ProviderError> { + let line = |y: f64| PathData::single(SubPath::polyline(&[Point::new(10.0, y), Point::new(60.0, y)], false)); + let path = |y: f64, square_caps: bool| VectorPath { path: line(y), fill: None, stroke: Some(([0, 0, 0], 4.0)), square_caps }; + Ok(r.styles.iter().map(|st| Variation { style: st.clone(), paths: vec![path(20.0, true), path(40.0, false)], warnings: vec![] }).collect()) + } + let (mut s, img) = doc_with(&art()); + with_fake(&mut s, fake(lines)); + let job = generate(&mut s, json!({"provider": "test.fake", "styles": ["s0"], "id": img.0})).unwrap()["job"].as_u64().unwrap(); + let r = s.execute("conceptVector.insert", &json!({"job": job, "variations": ["s0"]})).unwrap(); + let d = &s.doc().unwrap().doc; + let group = d.node(NodeId(r["ids"][0].as_u64().unwrap())).unwrap(); + let caps: Vec<_> = group.children().unwrap().iter().map(|n| n.appearance.stroke().unwrap().cap).collect(); + assert_eq!(caps, [vectorcraft_doc::appearance::LineCap::Square, vectorcraft_doc::appearance::LineCap::Round]); +} + /// An image scaled down to fit keeps within the pixel limit whatever its shape, a long thin one /// included, and one that fits stays as it is. #[test] @@ -657,7 +675,7 @@ fn insert_moves_the_row_past_art_that_a_variation_reaches_beyond_the_image_box() .iter() .map(|st| Variation { style: st.clone(), - paths: vec![VectorPath { path: bar.clone(), fill: Some([9, 9, 9]), stroke: None }], + paths: vec![VectorPath { path: bar.clone(), fill: Some([9, 9, 9]), stroke: None, square_caps: false }], warnings: vec![], }) .collect()) @@ -690,7 +708,7 @@ fn insert_keeps_the_gap_when_a_variation_reaches_left_of_the_image_box() { .iter() .map(|st| Variation { style: st.clone(), - paths: vec![VectorPath { path: bar.clone(), fill: Some([9, 9, 9]), stroke: None }], + paths: vec![VectorPath { path: bar.clone(), fill: Some([9, 9, 9]), stroke: None, square_caps: false }], warnings: vec![], }) .collect()) diff --git a/crates/ui-egui/src/panels/concept_vector.rs b/crates/ui-egui/src/panels/concept_vector.rs index b10f927ade..ae407f4737 100644 --- a/crates/ui-egui/src/panels/concept_vector.rs +++ b/crates/ui-egui/src/panels/concept_vector.rs @@ -361,15 +361,18 @@ fn fill_rings(img: &mut egui::ColorImage, rings: &[Vec], color: Color32) { /// Paints `rings` (polylines in 0..1 of the image's box) stroked `width` pixels wide in `color` /// over `img`: each segment as a rectangle, with a disc at each point for round joins and caps -/// once the stroke is wide enough for them to show. -fn stroke_rings(img: &mut egui::ColorImage, rings: &[Vec], width: f32, color: Color32) { +/// once the stroke is wide enough for them to show. With `square_caps`, an open line has no disc +/// at its ends, and its end segments reach half the width past them. +fn stroke_rings(img: &mut egui::ColorImage, rings: &[Vec], width: f32, color: Color32, square_caps: bool) { let (sx, sy) = (img.size[0] as f32, img.size[1] as f32); let r = width / 2.0; let unit = |p: Pos2| Pos2::new(p.x / sx, p.y / sy); for ring in rings { let pts: Vec = ring.iter().map(|q| Pos2::new(q.x * sx, q.y * sy)).collect(); + let last = pts.len().saturating_sub(1); + let square = square_caps && pts.len() > 1 && pts.first() != pts.last(); for (k, a) in pts.iter().enumerate() { - if r > 0.75 { + if r > 0.75 && !(square && (k == 0 || k == last)) { let disc: Vec = (0..8) .map(|i| { let t = i as f32 * std::f32::consts::TAU / 8.0; @@ -383,7 +386,10 @@ fn stroke_rings(img: &mut egui::ColorImage, rings: &[Vec], width: f32, col let len = d.length(); if len > 0.0 { let n = vec2(-d.y, d.x) * (r / len); - fill_rings(img, &[vec![unit(*a + n), unit(*b + n), unit(*b - n), unit(*a - n)]], color); + let along = d * (r / len); + let a = if square && k == 0 { *a - along } else { *a }; + let b = if square && k + 1 == last { *b + along } else { *b }; + fill_rings(img, &[vec![unit(a + n), unit(b + n), unit(b - n), unit(a - n)]], color); } } } @@ -455,7 +461,7 @@ fn preview(v: &Variation, label: String, size: (u32, u32)) -> Preview { if let Some((c, width)) = p.stroke { // To scale with the image, and at least MIN_STROKE points. let px = ((width / w) as f32 * image.size[0] as f32).max(MIN_STROKE * PREVIEW_SCALE); - stroke_rings(&mut image, &rings, px, rgb(c)); + stroke_rings(&mut image, &rings, px, rgb(c), p.square_caps); } } Preview { style: v.style.clone(), label, aspect, image, texture: None, warnings: v.warnings.clone() } diff --git a/crates/ui-egui/src/panels/concept_vector/tests.rs b/crates/ui-egui/src/panels/concept_vector/tests.rs index 2d169b2197..96b15cae13 100644 --- a/crates/ui-egui/src/panels/concept_vector/tests.rs +++ b/crates/ui-egui/src/panels/concept_vector/tests.rs @@ -206,7 +206,7 @@ fn generate_runs_off_the_ui_thread_and_cancel_drops_it() { fn one_path(points: &[Point], fill: Option<[u8; 3]>, stroke: Option<([u8; 3], f64)>) -> Variation { Variation { style: "s".into(), - paths: vec![VectorPath { path: PathData::single(SubPath::polyline(points, true)), fill, stroke }], + paths: vec![VectorPath { path: PathData::single(SubPath::polyline(points, true)), fill, stroke, square_caps: false }], warnings: vec![], } } @@ -247,6 +247,22 @@ fn preview_strokes_keep_their_widths() { assert!(stroke_cover(0.01) >= 1, "thin lines still show"); } +/// A line with square caps is painted to the corners of its ends, and one with round caps leaves +/// them white. +#[test] +fn preview_strokes_keep_their_caps() { + let line = PathData::single(SubPath::polyline(&[Point::new(25.0, 50.0), Point::new(75.0, 50.0)], false)); + let corner = |square_caps: bool| { + let paths = vec![VectorPath { path: line.clone(), fill: None, stroke: Some(([0, 0, 0], 20.0)), square_caps }]; + let p = preview(&Variation { style: "s".into(), paths, warnings: vec![] }, "s".into(), (100, 100)); + let [w, h] = p.image.size; + // Past the line's right end, near the corner of a square cap: 8 and 8 pixels of the image. + p.image.pixels[(h * 58 / 100) * w + w * 83 / 100] + }; + assert_eq!(corner(true), Color32::BLACK, "square caps"); + assert_eq!(corner(false), Color32::WHITE, "round caps"); +} + /// Paths are painted in their order: a fill above a stroke covers it, and a stroke above a fill /// shows. #[test] @@ -254,8 +270,8 @@ fn previews_paint_paths_in_their_order() { let square = [Point::new(25.0, 25.0), Point::new(75.0, 25.0), Point::new(75.0, 75.0), Point::new(25.0, 75.0)]; let cover = [Point::new(0.0, 0.0), Point::new(100.0, 0.0), Point::new(100.0, 100.0), Point::new(0.0, 100.0)]; let path = |pts: &[Point]| PathData::single(SubPath::polyline(pts, true)); - let stroke = VectorPath { path: path(&square), fill: None, stroke: Some(([0, 0, 0], 4.0)) }; - let fill = VectorPath { path: path(&cover), fill: Some([200, 20, 20]), stroke: None }; + let stroke = VectorPath { path: path(&square), fill: None, stroke: Some(([0, 0, 0], 4.0)), square_caps: false }; + let fill = VectorPath { path: path(&cover), fill: Some([200, 20, 20]), stroke: None, square_caps: false }; let on_edge = |paths: Vec| { let p = preview(&Variation { style: "s".into(), paths, warnings: vec![] }, "s".into(), (100, 100)); let [w, h] = p.image.size;