From e085c479060bb925f065c68eacb083bfb2252544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Mon, 30 Nov 2026 11:04:19 -0500 Subject: [PATCH 1/4] feat(layout): export a layout as PNG or SVG and at a chosen dpi from the browser Closes #3461 --- crates/ui-egui/src/lib.rs | 100 ++++++++++++++++++++++++++++++++--- crates/ui-egui/src/ribbon.rs | 26 ++++++++- docs/user-guide/layouts.md | 5 +- 3 files changed, 121 insertions(+), 10 deletions(-) diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 2bfe9858b5..77f37fa7c7 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1121,6 +1121,8 @@ pub struct UiState { pub(crate) history_jump: Option, /// Whether a layout's PDF georeferences its map frames (Layout › Export › Georeference PDF). pub(crate) pdf_georeference: bool, + /// The resolution picked in Export Layout's menu, where files are downloaded (web, phones). + pub(crate) layout_export_dpi: u32, /// The native title bar the header is drawn into; set by the desktop app on macOS. pub titlebar: Option, /// Saved pane arrangements: (name, left pane, right pane in view, ribbon tab, right panes open). @@ -1454,6 +1456,7 @@ impl Default for UiState { quick_access: Vec::new(), history_jump: None, pdf_georeference: true, + layout_export_dpi: 150, titlebar: None, pane_sets: Vec::new(), layer_filter: String::new(), @@ -5386,11 +5389,20 @@ impl RustGisApp { let li = self.session.active_layout.ok_or("open a layout first")?; let name = self.session.project.layouts.get(li).map(|l| l.name.clone()).unwrap_or_default(); let print = id == "ui.printLayout"; - let pdf_only = print || p.get("format").and_then(Value::as_str) == Some("pdf"); + // Print is always a PDF at 300 dpi; Export takes a format and a resolution. + let format = match p.get("format").filter(|v| !v.is_null()) { + _ if print => Some("pdf"), + None => None, + Some(v) => Some( + ["pdf", "png", "svg"].into_iter().find(|f| v == f).ok_or_else(|| tl!("format is pdf, png or svg").to_string())?, + ), + }; + let dpi = p.get("dpi").and_then(Value::as_f64).filter(|_| !print); if self.services.download.is_some() { - let dpi = if print { 300 } else { 150 }; + let dpi = dpi.unwrap_or(if print { 300.0 } else { 150.0 }); let params = json!({"layout": li, "dpi": dpi, "georeference": self.ui.pdf_georeference}); - return self.run_offering("layout.export", &format!("{name}.pdf"), params, None); + let file = format!("{name}.{}", format.unwrap_or("pdf")); + return self.run_offering("layout.export", &file, params, None); } else if print { // Printing goes through the system's PDF viewer, which owns the printers. let bytes = self.layout_bytes(li, true, 300.0)?; @@ -5401,11 +5413,16 @@ impl RustGisApp { open_with_system(&path)?; self.session.notify(i18n::fmt("Opened {name} for printing", &[("name", &path.display().to_string())]), false); } else { - let (exts, kinds): (&[&str], _) = - if pdf_only { (&["pdf"], tl!("PDF")) } else { (&["pdf", "png", "svg"], tl!("PDF, PNG or SVG")) }; - if let Some(path) = dialogs::pick_save(&format!("{name}.pdf"), exts, kinds) { + let (exts, kinds): (&[&str], _) = match format { + Some("png") => (&["png"], tl!("PNG")), + Some("svg") => (&["svg"], tl!("SVG")), + Some(_) => (&["pdf"], tl!("PDF")), + None => (&["pdf", "png", "svg"], tl!("PDF, PNG or SVG")), + }; + if let Some(path) = dialogs::pick_save(&format!("{name}.{}", exts[0]), exts, kinds) { // Drawn on a thread of its own, with progress and Cancel in the status bar. - let params = json!({"layout": li, "path": path, "dpi": 200, "georeference": self.ui.pdf_georeference}); + let dpi = dpi.unwrap_or(200.0); + let params = json!({"layout": li, "path": path, "dpi": dpi, "georeference": self.ui.pdf_georeference}); #[cfg(not(target_arch = "wasm32"))] return exportjob::start(self, "layout.export", params); #[cfg(target_arch = "wasm32")] @@ -10430,6 +10447,7 @@ mod download_service_tests { ("ui.exportRaster", json!({}), "Dem.tif".into()), ("ui.exportMap", json!({}), format!("{map}.png")), ("ui.exportLayout", json!({}), "Page.pdf".into()), + ("ui.exportLayout", json!({"format": "png"}), "Page.png".into()), ("ui.printLayout", json!({}), "Page.pdf".into()), ("ui.saveLayout", json!({}), "Page.rgislayout".into()), ("ui.exportReport", json!({}), "List.pdf".into()), @@ -10458,6 +10476,74 @@ mod download_service_tests { assert!(path.exists()); } + /// The browser's Export Layout downloads the format and resolution asked for; without them, a + /// PDF at 150 dpi (#3461). + #[test] + fn export_layout_downloads_the_format_and_dpi_asked_for() { + let taken: std::sync::Arc)>>> = Default::default(); + let downloads = taken.clone(); + let mut app = RustGisApp::with_sample().with_services(Services { + download: Some(Box::new(move |name, bytes| { + downloads.lock().unwrap().push((name.to_string(), std::sync::Arc::unwrap_or_clone(bytes))); + Ok(()) + })), + ..Default::default() + }); + let e = app.run("ui.exportLayout", json!({"format": "png"})).unwrap_err(); + assert!(e.contains("open a layout first"), "{e}"); + app.run("layout.new", json!({"name": "Page", "template": "blank"})).unwrap(); + let width = app.session.project.layouts[0].width; + let mut export = |params: serde_json::Value| { + app.run("ui.exportLayout", params).unwrap(); + let mut files = std::mem::take(&mut *taken.lock().unwrap()); + assert_eq!(files.len(), 1); + files.pop().unwrap() + }; + for (params, dpi) in + [(json!({"format": "png"}), 150.0), (json!({"format": "png", "dpi": 96}), 96.0), (json!({"format": "png", "dpi": 300}), 300.0)] + { + let (name, bytes) = export(params); + assert_eq!(name, "Page.png"); + let got = image::load_from_memory(&bytes).unwrap().width() as f64; + assert!((got - width / 25.4 * dpi).abs() <= 1.0, "{got} px at {dpi} dpi"); + } + let (name, bytes) = export(json!({"format": "svg"})); + assert_eq!(name, "Page.svg"); + let text = String::from_utf8(bytes).unwrap(); + assert!(text.contains(" Date: Mon, 30 Nov 2026 11:05:24 -0500 Subject: [PATCH 2/4] fix(layout): read Export Layout's dpi given as text --- crates/ui-egui/src/lib.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 77f37fa7c7..0dbb745512 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -5397,9 +5397,10 @@ impl RustGisApp { ["pdf", "png", "svg"].into_iter().find(|f| v == f).ok_or_else(|| tl!("format is pdf, png or svg").to_string())?, ), }; - let dpi = p.get("dpi").and_then(Value::as_f64).filter(|_| !print); + // Passed on as given: `layout.export` reads it, a number in text too. + let dpi = p.get("dpi").filter(|v| !v.is_null() && !print).cloned(); if self.services.download.is_some() { - let dpi = dpi.unwrap_or(if print { 300.0 } else { 150.0 }); + let dpi = dpi.unwrap_or_else(|| json!(if print { 300 } else { 150 })); let params = json!({"layout": li, "dpi": dpi, "georeference": self.ui.pdf_georeference}); let file = format!("{name}.{}", format.unwrap_or("pdf")); return self.run_offering("layout.export", &file, params, None); @@ -5421,7 +5422,7 @@ impl RustGisApp { }; if let Some(path) = dialogs::pick_save(&format!("{name}.{}", exts[0]), exts, kinds) { // Drawn on a thread of its own, with progress and Cancel in the status bar. - let dpi = dpi.unwrap_or(200.0); + let dpi = dpi.unwrap_or_else(|| json!(200)); let params = json!({"layout": li, "path": path, "dpi": dpi, "georeference": self.ui.pdf_georeference}); #[cfg(not(target_arch = "wasm32"))] return exportjob::start(self, "layout.export", params); @@ -10499,9 +10500,12 @@ mod download_service_tests { assert_eq!(files.len(), 1); files.pop().unwrap() }; - for (params, dpi) in - [(json!({"format": "png"}), 150.0), (json!({"format": "png", "dpi": 96}), 96.0), (json!({"format": "png", "dpi": 300}), 300.0)] - { + for (params, dpi) in [ + (json!({"format": "png"}), 150.0), + (json!({"format": "png", "dpi": 96}), 96.0), + (json!({"format": "png", "dpi": 300}), 300.0), + (json!({"format": "png", "dpi": "96"}), 96.0), + ] { let (name, bytes) = export(params); assert_eq!(name, "Page.png"); let got = image::load_from_memory(&bytes).unwrap().width() as f64; From 0dd8304b9d07bbd61306f3788bcaa32ef8723a6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Mon, 30 Nov 2026 11:17:40 -0500 Subject: [PATCH 3/4] fix(layout): translate Export Layout's resolution labels --- crates/ui-egui/src/ribbon.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index d80c013fcb..f078240103 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -3748,7 +3748,8 @@ fn export_layout_button(ui: &mut Ui, app: &mut RustGisApp, label: &str, desc: &s ui.label(tl!("Resolution")); ui.horizontal(|ui| { for dpi in [96, 150, 300] { - ui.radio_value(&mut app.ui.layout_export_dpi, dpi, format!("{dpi} dpi")); + let text = crate::i18n::fmt("{dpi} dpi", &[("dpi", &dpi.to_string())]); + ui.radio_value(&mut app.ui.layout_export_dpi, dpi, text); } }); ui.separator(); From 30ab502c5f75bf7c4f71cbc31715a5ff588f889e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Mon, 30 Nov 2026 11:28:21 -0500 Subject: [PATCH 4/4] fix(layout): name SVG in both Export button descriptions --- crates/ui-egui/src/ribbon.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index f078240103..ebff762bf3 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -3336,7 +3336,7 @@ fn share_groups(app: &mut RustGisApp, ui: &mut Ui) { }); }); group(ui, "Layout", |ui| { - export_layout_button(ui, app, "Export\nLayout", "Export the open layout as PDF or PNG."); + export_layout_button(ui, app, "Export\nLayout", "Export the open layout as PDF, PNG or SVG."); big( ui, app, @@ -3708,7 +3708,7 @@ fn layout_groups(app: &mut RustGisApp, ui: &mut Ui) { {} }); group(ui, "Export", |ui| { - export_layout_button(ui, app, "Export", "Export the layout as PDF or PNG."); + export_layout_button(ui, app, "Export", "Export the layout as PDF, PNG or SVG."); big(ui, app, "print", "Print", "Print the layout.", "ui.printLayout", Value::Null, false, false); col(ui, |ui| { small( @@ -3744,7 +3744,7 @@ fn export_layout_button(ui: &mut Ui, app: &mut RustGisApp, label: &str, desc: &s big(ui, app, "export", label, desc, "ui.exportLayout", Value::Null, false, false); return; } - big_form_menu(ui, app, "export", label, "Export the layout as PDF, PNG or SVG.", |ui, app| { + big_form_menu(ui, app, "export", label, desc, |ui, app| { ui.label(tl!("Resolution")); ui.horizontal(|ui| { for dpi in [96, 150, 300] {