diff --git a/crates/engine/src/concept_vector/offline.rs b/crates/engine/src/concept_vector/offline.rs index 57c569e13d..9189d5a58a 100644 --- a/crates/engine/src/concept_vector/offline.rs +++ b/crates/engine/src/concept_vector/offline.rs @@ -221,12 +221,30 @@ impl Decoded { } /// The variation of `style` that the trace `res` of this image, made with `params`, makes, and - /// the seam fills of an Abutting trace with fills ([`tr::seam_fills`]) kept inside the image - /// ([`within_image`]), for [`with_seams`] to lay under its paths. + /// the seam fills of an Abutting trace with fills ([`tr::seam_fills`]) trimmed inside the + /// image ([`within_image`]) and outlined in raster pixels before mapping them back, for + /// [`with_seams`] to lay under its paths. fn variation(&self, style: &str, params: &tr::TraceParams, res: tr::TraceResult) -> Made { let size = (self.raster.width, self.raster.height); let seams = tr::seam_fills(&res.paths, params, size).map_or_else(Vec::new, |s| { - s.strokes.into_iter().map(|tp| within_image(tp, size)).filter(|tp| !tp.path.subpaths.is_empty()).map(|tp| self.vector_path(tp)).collect() + s.strokes + .into_iter() + .map(|tp| { + let mut tp = within_image(tp, size); + if let Some(width) = tp.stroke.take() { + tp.path = vectorcraft_pathops::outline_stroke( + &tp.path, + width, + vectorcraft_pathops::Cap::Butt, + vectorcraft_pathops::Join::Round, + 4.0, + ); + } + tp + }) + .filter(|tp| !tp.path.subpaths.is_empty()) + .map(|tp| self.vector_path(tp)) + .collect() }); let paths = res.paths.into_iter().map(|tp| self.vector_path(tp)).collect(); Made { variation: Variation { style: style.to_string(), paths, warnings: self.warnings.clone() }, seams } @@ -242,7 +260,7 @@ impl Decoded { } } -/// A variation made, and the seam fills to lay under its paths. +/// A variation made, and the outlined seam fills to lay under its paths. pub(super) struct Made { pub(super) variation: Variation, pub(super) seams: Vec, @@ -275,7 +293,7 @@ pub(super) fn with_seams_within(made: Vec, max_anchors: usize, max_paths: } /// Seam stroke `tp` of a `size` raster trimmed to half its width inside the raster's border, -/// so that its stroke, round caps and round joins stay inside the image. The pieces left form +/// so that its outline with butt caps and round joins stays inside the image. The pieces left form /// open runs; a closed subpath left whole stays closed. fn within_image(mut tp: tr::TracedPath, size: (u32, u32)) -> tr::TracedPath { let half = tp.stroke.unwrap_or_default() / 2.0; diff --git a/crates/engine/src/concept_vector/tests.rs b/crates/engine/src/concept_vector/tests.rs index 8fb74cab4d..93d216f4fe 100644 --- a/crates/engine/src/concept_vector/tests.rs +++ b/crates/engine/src/concept_vector/tests.rs @@ -441,7 +441,34 @@ fn a_stopped_script_stops_generate_and_keeps_no_job() { assert_eq!(generate(&mut s, json!({"provider": ID, "styles": ["flatColor"]})).unwrap()["state"], "done"); } -/// Flat Color's abutting fills get seam fills under them: strokes in the colors of the fills, +/// The seam fills before the unchanged traced fills, mapped back to the image's pixels. +fn seam_fills_before_trace<'a>(raster: &Raster, variation: &'a Variation, back: Affine) -> &'a [VectorPath] { + let params = offline::style_params(&variation.style, 50).unwrap(); + let traced = vectorcraft_trace::trace(raster, ¶ms); + let fills: Vec<_> = traced + .paths + .into_iter() + .map(|p| VectorPath { path: p.path.transformed(back), fill: Some(p.color), stroke: None, square_caps: false }) + .collect(); + assert!(!fills.is_empty(), "the reference trace has fills"); + let seams = variation.paths.len().checked_sub(fills.len()).expect("the traced fills are kept"); + assert_eq!(&variation.paths[seams..], fills, "the traced fills follow the seam fills unchanged"); + &variation.paths[..seams] +} + +/// Every filled path stays inside the image's pixel box. +fn fills_stay_inside_image(variation: &Variation, width: u32, height: u32) { + for p in &variation.paths { + assert!(p.fill.is_some() && p.stroke.is_none(), "seams and traced paths are filled"); + let b = p.path.bounds().expect("a filled path has bounds"); + assert!( + b.x0 >= -1e-6 && b.y0 >= -1e-6 && b.x1 <= f64::from(width) + 1e-6 && b.y1 <= f64::from(height) + 1e-6, + "{b:?} reaches past the border" + ); + } +} + +/// Flat Color's abutting fills get seam fills under them: areas in the colors of the fills, /// before every fill. Detailed Color stacks its fills and gets none. #[test] fn flat_color_lays_seam_fills_under_its_abutting_fills() { @@ -458,17 +485,15 @@ fn flat_color_lays_seam_fills_under_its_abutting_fills() { let made = |style: &str| offline::TraceStyles.generate(&req(style), &|_, _| true).unwrap().remove(0); let flat = made("flatColor"); check_result(&req("flatColor"), std::slice::from_ref(&flat)).unwrap(); - let seams = flat.paths.iter().take_while(|p| p.fill.is_none()).count(); - assert!(seams > 0, "no seam fills"); - let fills: Vec<_> = flat.paths.iter().skip(seams).map(|p| p.fill).collect(); - assert!(!fills.is_empty() && fills.iter().all(Option::is_some), "the fills follow the seam fills"); - let fill_colors: std::collections::BTreeSet<_> = fills.into_iter().flatten().collect(); - for p in flat.paths.iter().take(seams) { - let (color, width) = p.stroke.expect("a seam fill is a stroke"); - assert!(fill_colors.contains(&color) && width > 0.0, "{color:?} {width}"); + let seams = seam_fills_before_trace(&raster, &flat, Affine::IDENTITY); + assert!(!seams.is_empty(), "no seam fills"); + let fills = &flat.paths[seams.len()..]; + let fill_colors: std::collections::BTreeSet<_> = fills.iter().filter_map(|p| p.fill).collect(); + for p in seams { + assert!(p.stroke.is_none() && p.fill.is_some_and(|color| fill_colors.contains(&color)), "{p:?}"); } let detailed = made("detailedColor"); - assert!(!detailed.paths.is_empty() && detailed.paths.iter().all(|p| p.stroke.is_none()), "stacked fills get no seam fills"); + assert!(seam_fills_before_trace(&raster, &detailed, Affine::IDENTITY).is_empty(), "stacked fills get no seam fills"); } /// Seam fills are laid only in what the traced paths of every style leave of the limits, in the @@ -504,7 +529,7 @@ fn seam_fills_fit_in_what_every_styles_paths_leave() { } /// Seam fills stay inside the image: where two colors meet at its border, the part of the seam -/// fill whose stroke would reach past the border is left out, and the rest is kept. +/// fill whose outline would reach past the border is left out, and the rest is kept. #[test] fn seam_fills_stay_inside_the_image() { // A red block against the top border, on white. @@ -519,15 +544,11 @@ fn seam_fills_stay_inside_the_image() { output: Output::Vector, }; let flat = offline::TraceStyles.generate(&req, &|_, _| true).unwrap().remove(0); - let seams: Vec<_> = flat.paths.iter().filter(|p| p.fill.is_none()).collect(); + let seams = seam_fills_before_trace(&raster, &flat, Affine::IDENTITY); assert!(!seams.is_empty(), "the block's lower edge keeps its seam fill"); + fills_stay_inside_image(&flat, raster.width, raster.height); for p in seams { - let half = p.stroke.map_or(0.0, |(_, w)| w / 2.0); - for (_, _, a) in p.path.anchors() { - for q in [a.p, a.h_in, a.h_out] { - assert!(q.x >= half && q.y >= half && q.x <= 80.0 - half && q.y <= 60.0 - half, "{q:?} reaches past the border"); - } - } + assert!(p.path.bounds().unwrap().y0 > 0.0, "the seam is trimmed inside the top border"); } } @@ -545,18 +566,64 @@ fn seam_fills_keep_the_interior_of_a_split_image_edge() { output: Output::Vector, }; let flat = offline::TraceStyles.generate(&req, &|_, _| true).unwrap().remove(0); - let seams: Vec<_> = flat.paths.iter().filter(|p| p.fill.is_none()).collect(); + let seams = seam_fills_before_trace(&raster, &flat, Affine::IDENTITY); assert!(!seams.is_empty(), "the shared edge keeps its seam fill"); + fills_stay_inside_image(&flat, raster.width, raster.height); + assert!( + seams.iter().any(|p| { + let b = p.path.bounds().unwrap(); + b.x0 < 40.0 && b.x1 > 40.0 && b.y0 < 30.0 && b.y1 > 30.0 + }), + "the seam fill covers the interior of the split" + ); for p in seams { - let (_, width) = p.stroke.expect("a seam fill is a stroke"); - let half = width / 2.0; - for (_, _, a) in p.path.anchors() { - assert!( - a.p.x >= half && a.p.y >= half && a.p.x <= f64::from(raster.width) - half && a.p.y <= f64::from(raster.height) - half, - "{:?} reaches past the border", - a.p - ); - } + let b = p.path.bounds().unwrap(); + assert!(b.y0 > 0.0 && b.y1 < f64::from(raster.height), "{b:?} is trimmed inside both borders"); + } +} + +/// A raster fitted to a small pixel budget keeps seam fills inside the original image after +/// mapping back with different horizontal and vertical scales. +#[test] +fn seam_fills_stay_inside_a_nonuniformly_scaled_down_image() { + let original = Raster::from_fn(800, 60, |_, y| if y < 30 { [220, 30, 30, 255] } else { [30, 60, 220, 255] }); + let (width, height) = (original.width, original.height); + let raster = offline::fit_within(original, 8000); + assert!(raster.width < width && raster.height < height, "the raster was scaled down"); + assert_ne!(width * raster.height, height * raster.width, "rounding gives different scales"); + // Supply the fitted raster and its original dimensions, as decoding a large image does. + let req = Request { + image: Arc::new(raster.encode_png()), + width, + height, + prompt: None, + styles: vec!["flatColor".into()], + detail: 50, + output: Output::Vector, + }; + let flat = offline::TraceStyles.generate(&req, &|_, _| true).unwrap().remove(0); + let back = Affine::scale_non_uniform(f64::from(width) / f64::from(raster.width), f64::from(height) / f64::from(raster.height)); + let seams = seam_fills_before_trace(&raster, &flat, back); + assert!(!seams.is_empty(), "the shared edge keeps its seam fill after scaling back"); + fills_stay_inside_image(&flat, width, height); + let native_req = Request { width: raster.width, height: raster.height, ..req.clone() }; + let native = offline::TraceStyles.generate(&native_req, &|_, _| true).unwrap().remove(0); + let native_seams = seam_fills_before_trace(&raster, &native, Affine::IDENTITY); + assert_eq!(seams.len(), native_seams.len(), "scaling keeps every seam fill"); + for (scaled, native) in seams.iter().zip(native_seams) { + assert_eq!(scaled.path, native.path.transformed(back), "the seam's area is mapped with the image"); + assert_eq!(scaled.fill, native.fill, "scaling keeps the seam's color"); + } + assert!( + seams.iter().any(|p| { + let b = p.path.bounds().unwrap(); + b.x0 < 400.0 && b.x1 > 400.0 && b.y0 < 30.0 && b.y1 > 30.0 + }), + "the seam fill covers the interior of the split after scaling back" + ); + for p in seams { + let b = p.path.bounds().unwrap(); + assert!(b.x0 > 0.0 && b.x1 < f64::from(width), "{b:?} is trimmed inside both borders after scaling back"); } }