PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge PR #8353: Scale shape strokes when resizing for Export As
aionggff2ba3727833
diff --cc crates/ui-egui/src/export_dialog.rsindex d72c0ff30,907c2cb0b..190168781--- a/crates/ui-egui/src/export_dialog.rs+++ b/crates/ui-egui/src/export_dialog.rs@@@ -1393,86 -1229,36 +1393,116 @@@ mod tests}}+ /// #6595: Export As shows the size the file will have, as Photoshop does. The proxy estimate+ /// read 36–81 % of it for a detailed image; up to `EXACT_ESTIMATE_PX` the full export is+ /// encoded off the UI thread instead.+ #[test]+ fn the_estimate_is_the_exported_files_size() {+ let mut doc = Document::with_background(+ "x",+ photocraft_doc::Size::new(900, 300),+ photocraft_doc::ColorMode::Rgb,+ photocraft_doc::SampleType::U8,+ photocraft_doc::Color::WHITE,+ );+ let id = doc.layers[0].id;+ let surf = doc.layer_mut(id).unwrap().surface_mut().unwrap();+ let mut seed = 12345u32;+ for y in (0..300).step_by(2) {+ for x in (0..900).step_by(2) {+ seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);+ let v = (seed >> 24) as f32 / 255.0;+ surf.fill_rect(photocraft_geom::Rect::new(x, y, x + 2, y + 2), &[v, x as f32 / 900.0, y as f32 / 300.0, 1.0]);+ }+ }+ let doc = Arc::new(doc);+ let f = Map::from_iter([("format".into(), json!("jpg")), ("scale".into(), json!(100)), ("quality".into(), json!(32))]);+ let actual = photocraft_io::export(&export_document(&doc, &f, None).unwrap(), "x.jpg", &settings(&f).io_options()).unwrap().bytes.len() as u64;+ let proxy = preview_proxy(&doc, &f).unwrap();+ let old = estimate_native(&doc, &proxy, &f).unwrap();+ assert!((old as f64 / actual as f64 - 1.0).abs() > 0.1, "the proxy estimate was off: {old} vs {actual}");++ let ctx = egui::Context::default();+ let key = egui::Id::new("exact-estimate-test");+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20);+ let size = loop {+ let mut size = None;+ let mut output = ctx.run_ui(Default::default(), |ctx| {+ egui::CentralPanel::default().show(ctx, |ui| size = poll_or_start_exact_estimate(ui, key, "jpg32", doc.clone(), &f));+ });+ output.textures_delta.clear();+ if let Some(size) = size {+ break size;+ }+ assert!(std::time::Instant::now() < deadline, "the exact estimate did not arrive");+ std::thread::sleep(std::time::Duration::from_millis(10));+ };+ assert_eq!(size, Some(actual));+ // Over the limit there is no exact size; the proxy estimate stays.+ let huge = Map::from_iter([+ ("format".into(), json!("jpg")),+ ("sizing".into(), json!("pixels")),+ ("width".into(), json!(10_000)),+ ("height".into(), json!(10_000)),+ ]);+ assert_eq!(exact_estimate(&doc, &huge), None);+ // In scale mode the size comes from the scale, as the export's does, not from stale pixels.+ let big = Arc::new(Document::with_background(+ "big",+ photocraft_doc::Size::new(1000, 1000),+ photocraft_doc::ColorMode::Rgb,+ photocraft_doc::SampleType::U8,+ photocraft_doc::Color::WHITE,+ ));+ let stale = Map::from_iter([+ ("format".into(), json!("png")),+ ("sizing".into(), json!("scale")),+ ("scale".into(), json!(1000)),+ ("width".into(), json!(100)),+ ("height".into(), json!(100)),+ ]);+ assert_eq!(exact_estimate(&big, &stale), None, "10000 × 10000 at 1000 %, over the limit");+ let small = Map::from_iter([+ ("format".into(), json!("png")),+ ("sizing".into(), json!("scale")),+ ("scale".into(), json!(10)),+ ("width".into(), json!(9000)),+ ("height".into(), json!(9000)),+ ]);+ let at_10 = photocraft_io::export(&export_document(&big, &small, None).unwrap(), "x.png", &settings(&small).io_options()).unwrap().bytes.len() as u64;+ assert_eq!(exact_estimate(&big, &small), Some(at_10), "100 × 100 at 10 %, within it");+ }++ /// #8341: Export As and its reduced preview carry the stroke width with the path.+ #[test]+ fn export_as_downscales_stroke_only_shapes_and_preview() {+ for depth in [8, 16, 32] {+ let mut session = photocraft_engine::Session::new();+ session.execute("file.new", json!({"width": 1024, "height": 1024, "depth": depth, "background": "transparent"})).unwrap();+ let id = session.execute("shape.create", json!({"rect": [256, 256, 512, 512], "fill": null, "stroke": {"width": 32}})).unwrap()["layer"]+ .as_u64()+ .unwrap();+ let doc = session.active().unwrap().doc.clone();+ let fields = Map::from_iter([("format".into(), json!("png")), ("scale".into(), json!(25))]);+ for limit in [None, Some(128)] {+ let out = export_document(&doc, &fields, limit).unwrap();+ let side = if limit.is_some() { 128 } else { 256 };+ assert_eq!((out.size.width, out.size.height), (side, side));+ let shape = out.layer(photocraft_doc::LayerId(id)).unwrap();+ let photocraft_doc::LayerContent::Shape(shape) = &shape.content else {+ panic!("shape lost");+ };+ assert_eq!(shape.stroke.as_ref().unwrap().width, side as f32 / 32.0);+ let pixels = photocraft_compose::flatten(&out);+ let row = (side / 2 * side) as usize;+ assert!(pixels.px[row + (side / 4) as usize][3] > 0.99);+ assert!(pixels.px[row + (side / 4 - side / 32) as usize][3] < 0.01);+ assert!(pixels.px[row + (side / 2) as usize][3] < 0.01);+ }+ assert_eq!(session.active().unwrap().doc, doc, "export leaves the source unchanged");+ }+ }+#[test]fn export_document_scales_and_flattens() {let doc = Document::with_background(