diff --git a/crates/io/src/vector_map.rs b/crates/io/src/vector_map.rs index 63b2490067..b51593fd20 100644 --- a/crates/io/src/vector_map.rs +++ b/crates/io/src/vector_map.rs @@ -866,4 +866,35 @@ mod align_edges_tests { assert_eq!(raw[0].1, data, "unchanged true values stay byte-identical"); } } + /// Layer-wide snapping no longer needs the old canvas quantum to sharpen a rectangle. + #[test] + fn pixel_alignment_narrowed_psd_canvas_keeps_crisp_edges_after_recolor() { + let shape = ShapeLayer { + path: photocraft_vector::shapes::rect(10.0, 10.0, 60.0, 60.0), + use_aligned_rendering: true, + stroke: Some(ShapeStroke { width: 1.0, ..Default::default() }), + ..Default::default() + }; + let mut raw = Vec::new(); + shape_blocks(&shape, &mut raw, 300_000, 300_000, 72.0); + let mut imported = ShapeLayer::default(); + shape_from_blocks(&mut imported, &|key| raw.iter().find(|(k, _)| k == key).map(|(_, bytes)| bytes.clone()), 300_000, 300_000, 72.0); + assert_ne!(imported.path, shape.path, "fixture must exercise PSD path quantization"); + shape_blocks(&imported, &mut raw, 1000, 1000, 72.0); + let mut reopened = ShapeLayer::default(); + shape_from_blocks(&mut reopened, &|key| raw.iter().find(|(k, _)| k == key).map(|(_, bytes)| bytes.clone()), 1000, 1000, 72.0); + assert_eq!(reopened.psd_coordinate_frame, Some([1000, 1000])); + assert!(reopened.use_aligned_rendering); + reopened.stroke.as_mut().unwrap().paint = Fill::Solid(photocraft_doc::Color::rgb(0.4, 0.2, 0.7)); + let path = reopened.path.clone(); + let rect = photocraft_geom::Rect::new(0, 0, 80, 80); + let pixels = photocraft_vector::CompiledShape::new(&reopened, photocraft_vector::DEFAULT_TOLERANCE, rect).render_rgba(rect); + for y in 0..80 { + for x in 0..80 { + let edge = (10..70).contains(&x) && (y == 10 || y == 69) || (10..70).contains(&y) && (x == 10 || x == 69); + assert_eq!(pixels[y * 80 + x][3], f32::from(edge), "({x},{y})"); + } + } + assert_eq!(reopened.path, path); + } } diff --git a/crates/ui-egui/src/tools/shape_stroke_ui.rs b/crates/ui-egui/src/tools/shape_stroke_ui.rs index 0ed042cbac..71549c5518 100644 --- a/crates/ui-egui/src/tools/shape_stroke_ui.rs +++ b/crates/ui-egui/src/tools/shape_stroke_ui.rs @@ -220,7 +220,18 @@ fn new_document_preview( ((f64::from(canvas.y1) - f64::from(bounds.y0)) * scale).ceil() as i32, ); let fallback = photocraft_vector::CompiledShape::with_frames(&proxy, photocraft_vector::DEFAULT_TOLERANCE, output, frame).render_rgba(output); - let bytes: Vec = fallback.iter().flat_map(|p| p.map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8)).collect(); + let bytes: Vec = fallback + .iter() + .flat_map(|p| { + let mut rgba = p.map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8); + // A 1px document stroke can be below half an alpha byte in this + // coarse proxy. Keep positive coverage until native pixels replace it. + if p[3] > 0.0 && rgba[3] == 0 { + rgba[3] = 1; + } + rgba + }) + .collect(); work.set_fallback(egui::ColorImage::from_rgba_unmultiplied(size, &bytes)); } let image = work.image(); diff --git a/crates/ui-egui/src/tools/shape_stroke_ui/tests.rs b/crates/ui-egui/src/tools/shape_stroke_ui/tests.rs index d916ddb043..25ad02199d 100644 --- a/crates/ui-egui/src/tools/shape_stroke_ui/tests.rs +++ b/crates/ui-egui/src/tools/shape_stroke_ui/tests.rs @@ -1056,3 +1056,32 @@ fn pixel_align_round2_changing_preview_has_complete_fill() { output.textures_delta.clear(); } } + +/// The coarse proxy of a changing 300k outline has less than half an alpha byte +/// of coverage. Unvisited edges must survive the uploaded texture, not just f32 rasterization. +#[test] +fn pixel_align_fallback_preserves_thin_strokes_in_uploaded_texture() { + let ctx = egui::Context::default(); + for width in [300_000.0, 299_999.0] { + let shape = photocraft_doc::ShapeLayer { + path: photocraft_vector::shapes::rect(0.0, 0.0, width, 4096.0), + stroke: Some(photocraft_doc::ShapeStroke { width: 1.0, ..Default::default() }), + use_aligned_rendering: true, + ..Default::default() + }; + let mut texture = None; + let mut output = ctx.run_ui(Default::default(), |ui| { + let painter = ui.ctx().layer_painter(egui::LayerId::background()); + let canvas = photocraft_geom::Rect::new(0, 0, 300_000, 4096); + let cache = new_document_preview(&painter, &shape, canvas, canvas, egui::TextureOptions::LINEAR).unwrap(); + assert!(cache.reduction.is_some(), "fixture must leave native tiles unvisited"); + texture = Some(cache.texture.clone()); + }); + let delta = output.textures_delta.set.get(&texture.as_ref().unwrap().id()).unwrap(); + let egui::ImageData::Color(image) = &delta[0].image; + let row = image.size[1] / 2; + assert!(image.pixels[row * image.size[0] + image.size[0] - 1].a() > 0, "unvisited right edge, width={width}"); + assert_eq!(image.pixels[row * image.size[0] + image.size[0] / 2].a(), 0, "transparent interior must remain transparent"); + output.textures_delta.clear(); + } +}