Skip to content
11 changes: 6 additions & 5 deletions crates/data/src/progress.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Cancelling a long read or write and following its progress (#1291 step 3). Whoever runs it
//! (the app's export on a thread of its own) installs a [`Probe`] on that thread; the writers'
//! loops say how far they have come with [`begin`] and [`step`], and `step` stops them once the
//! probe was cancelled. With none installed, as for a script or the web app, they only run.
//! loops (and a layout export's pages) say how far they have come with [`begin`] and [`step`],
//! and `step` stops them once the probe was cancelled; [`check`] only stops (the map's wait for
//! its tiles). With none installed, as for a script or the web app, they only run.
//!
//! The analysis crate's run control is a probe, so the same Cancel and progress reach a tool and
//! an export.
Expand Down Expand Up @@ -43,7 +44,7 @@ impl Drop for Installed {
}

/// Starts counting a step of `total` items for the progress.
pub(crate) fn begin(total: usize) {
pub fn begin(total: usize) {
CURRENT.with(|c| {
if let Some(p) = c.borrow().as_ref() {
p.begin(total as u64);
Expand All @@ -53,7 +54,7 @@ pub(crate) fn begin(total: usize) {

/// Counts `n` more items done, and stops the write once its run was asked to: call it for each
/// feature, row or tile of a long loop.
pub(crate) fn step(n: usize) -> Result<()> {
pub fn step(n: usize) -> Result<()> {
CURRENT.with(|c| match c.borrow().as_ref() {
Some(p) if p.cancelled() => Err(DataError::Cancelled),
Some(p) => {
Expand All @@ -66,7 +67,7 @@ pub(crate) fn step(n: usize) -> Result<()> {

/// Stops the write once its run was asked to, counting nothing: for a check after a loop that
/// can't stop by itself.
pub(crate) fn check() -> Result<()> {
pub fn check() -> Result<()> {
CURRENT.with(|c| match c.borrow().as_ref() {
Some(p) if p.cancelled() => Err(DataError::Cancelled),
_ => Ok(()),
Expand Down
139 changes: 120 additions & 19 deletions crates/engine/src/cmd/exports.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
//! Exports that write a file from what the session shares (a layer's features, a raster's
//! pixels). A script or the command line writes it where the command is called; the app writes it
//! on a thread of its own with progress and Cancel (#1291 step 3): [`export_start`] reads the
//! session, [`ExportRun::run`] writes the file, and [`export_land`] says what was written, as the
//! command would have. It lands whatever layer is active or project is open by then: the file is
//! written either way, and an export changes nothing in the project.
//! pixels, a copy of the map or project to draw). A script or the command line writes it where
//! the command is called; the app writes it on a thread of its own with progress and Cancel
//! (#1291 step 3): [`export_start`] reads the session, [`ExportRun::run`] writes the file, and
//! [`export_land`] says what was written, as the command would have. It lands whatever layer is
//! active or project is open by then: the file is written either way, and an export changes
//! nothing in the project.

use serde_json::Value;

use crate::{CmdResult, Session};

/// What an export wrote: the command's answer, and what it says of the file (the last says it
/// was exported).
/// was exported, unless `then` says it).
pub struct Wrote {
pub(crate) answer: Value,
pub(crate) notes: Vec<String>,
Expand All @@ -19,8 +20,9 @@ pub struct Wrote {
pub(crate) then: Option<Then>,
}

/// What an export does to the session once its file is written, and adds to its answer.
pub(crate) type Then = Box<dyn FnOnce(&mut Session, &mut Value) + Send>;
/// What an export does to the session once its file is written, and adds to its answer; or the
/// failure it says once what came before it was said (a layout's warnings, then its error).
pub(crate) type Then = Box<dyn FnOnce(&mut Session, &mut Value) -> Result<(), String> + Send>;

/// The writing of an export, its inputs read: it needs no session.
pub(crate) type Work = Box<dyn FnOnce() -> Result<Wrote, String> + Send>;
Expand All @@ -33,6 +35,8 @@ type Start = fn(&Session, &Value) -> Result<Work, String>;
const IN_BACKGROUND: &[(&str, &str, Start)] = &[
("share.exportLayer", "Export Features", super::share::layer_work),
("share.exportRaster", "Export Raster", super::raster::export_work),
("share.exportMap", "Export Map", super::share::map_work),
("layout.export", "Export Layout", super::layout::export_work),
];

/// `command` and what it is called, when the app writes its file on another thread (see
Expand All @@ -47,7 +51,7 @@ pub(crate) fn write_here(s: &mut Session, p: &Value, command: &str) -> CmdResult
let work = start(s, p)?;
// Where a person at a terminal can stop it with Ctrl-C, as on the app's thread.
let wrote = if s.stoppable { written_under(name, &rustgis_analysis::control::Control::stoppable(), work)? } else { work()? };
Ok(said(s, wrote))
said(s, wrote)
}

/// `work` under `control`, as [`rustgis_analysis::run_under`] runs it, except that a file once
Expand All @@ -61,16 +65,77 @@ fn written_under(name: &str, control: &std::sync::Arc<rustgis_analysis::control:
}
}

/// Says what an export wrote, and gives its answer.
fn said(s: &mut Session, wrote: Wrote) -> Value {
/// Says what an export wrote, and gives its answer (or the failure its `then` says).
fn said(s: &mut Session, wrote: Wrote) -> CmdResult {
Comment thread
pick2510 marked this conversation as resolved.
Resolved
let Wrote { mut answer, notes, then } = wrote;
for note in notes {
s.notify(note, false);
}
if let Some(then) = then {
then(s, &mut answer);
then(s, &mut answer)?;
}
Ok(answer)
}

/// Lands an export drawn on a copy of the map or project (Export Map, Export Layout): what
/// drawing it said, with each notice's severity; the streamed layers whose source failed on the
/// copy, marked on their live layers as drawing them there would have (or, where that layer is
/// gone or opened afresh, said in one notice to be missing from the file); then that it was
/// written to `path`, or why not. A cancelled one says only that it was cancelled.
pub(crate) fn drawn(
maps: &[rustgis_map::Map],
notices: Vec<(rustgis_data::diagnostics::Severity, String)>,
written: Result<Value, String>,
path: String,
) -> Result<Wrote, String> {
if let (Err(e), Err(_)) = (&written, rustgis_data::progress::check()) {
return Err(e.clone());
}
answer
// Each by its live layer, with the source it failed on: one the user removed, or opened
// afresh meanwhile (`SourceLayer::same_opening`), is left as it is.
let failed: Vec<_> = maps
.iter()
.flat_map(|m| &m.layers)
.filter_map(|l| {
let src = l.source.as_ref()?;
Some((l.instance_id(), l.name.clone(), src.error.clone()?, src.clone()))
})
.collect();
let answer = written.as_ref().map_or(Value::Null, Value::clone);
let then: Then = Box::new(move |s: &mut Session, _: &mut Value| {
use rustgis_data::diagnostics::Severity;
for (severity, text) in notices {
match severity {
Severity::Error => s.notify(text, true),
Severity::Warning => s.warn(text),
Severity::Info => s.notify(text, false),
}
}
let mut missing = Vec::new();
for (instance, name, error, copied) in failed {
let live = s.project.maps.iter_mut().flat_map(|m| m.layers.iter_mut()).find(|l| l.instance_id() == instance);
match live.and_then(|l| l.source.as_mut()) {
// The same open: its failure shows on the layer (it may already).
Some(src) if src.same_opening(&copied) => {
src.error.get_or_insert(error);
}
_ => missing.push((name, error)),
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
// One notice for them all, with a count, naming the first.
if let Some((name, error)) = missing.first() {
let n = missing.len();
s.warn(if n == 1 {
format!("{path}: {name} isn't in the file, as its source couldn't be read: {error}")
} else {
format!("{path}: {n} layers aren't in the file, as their sources couldn't be read; the first, {name}: {error}")
});
}
written?;
s.notify(format!("Exported {path}"), false);
Ok(())
});
Ok(Wrote { answer, notes: Vec::new(), then: Some(then) })
}

#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -81,16 +146,33 @@ fn said(s: &mut Session, wrote: Wrote) -> Value {
pub struct ExportRun {
/// What the export is called, for its progress and a cancelled run's error.
pub name: &'static str,
started: Started,
work: Work,
}

#[cfg(not(target_arch = "wasm32"))]
/// The command an export started on another thread runs, with what and when: [`export_land`]
/// says what it wrote as that command, and puts it in the session's log as `Session::run` does.
#[derive(Clone)]
pub struct Started {
pub(crate) command: &'static str,
pub(crate) params: Value,
pub(crate) at: u64,
pub(crate) start: web_time::Instant,
}

#[cfg(not(target_arch = "wasm32"))]
impl ExportRun {
/// What [`export_land`] needs of it once it has run.
pub fn started(&self) -> Started {
self.started.clone()
}

/// Writes the file, blocking until it's written; `control` stops it and counts its progress
/// (see [`rustgis_analysis::control`]). A cancelled export leaves no file, or the one there
/// before.
pub fn run(self, control: &std::sync::Arc<rustgis_analysis::control::Control>) -> Result<Wrote, String> {
let ExportRun { name, work } = self;
let ExportRun { name, work, .. } = self;
// A bug in a writer must not take the app down.
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| written_under(name, control, work)))
.unwrap_or_else(|_| Err(format!("{name} failed unexpectedly")))
Expand All @@ -101,15 +183,34 @@ impl ExportRun {
/// Starts `command` (one of [`IN_BACKGROUND`]'s) with `p`, to write its file on another thread:
/// its parameters checked and its inputs taken now.
pub fn export_start(s: &mut Session, command: &str, p: &Value) -> Result<ExportRun, String> {
let &(_, name, start) =
let &(command, name, start) =
IN_BACKGROUND.iter().find(|(c, ..)| *c == command).ok_or_else(|| format!("{command} doesn't run in the background"))?;
Ok(ExportRun { name, work: start(s, p)? })
let started = Started { command, params: p.clone(), at: crate::project_log::now_ms(), start: web_time::Instant::now() };
Ok(ExportRun { name, started, work: start(s, p)? })
}

#[cfg(not(target_arch = "wasm32"))]
/// Says what [`ExportRun::run`] wrote, as its command would have, and gives its answer.
pub fn export_land(s: &mut Session, wrote: Wrote) -> Value {
said(s, wrote)
/// Says what [`ExportRun::run`] wrote (or why it failed), as its command would have, and gives
/// its answer: the troubleshooting history files what it says (its failure too) under that
/// command, and the session's log has its entry, as when it runs here.
pub fn export_land(s: &mut Session, started: Started, done: Result<Wrote, String>) -> CmdResult {
let Started { command, params, at, start } = started;
let before = s.diagnostic_source.replace(command);
let mark = s.notice_mark();
let landed = done.and_then(|wrote| said(s, wrote));
// Its failure said once, under the command, as `Session::run` says one.
if let Err(e) = &landed {
let text = Session::redacted(e);
if !s.notices_since(mark).iter().any(|n| n.error && n.text == text.as_str()) {
s.notify_redacted(text, true);
}
}
s.diagnostic_source = before;
if s.log.on() && crate::project_log::is_logged(s, command, &params) {
let entry = crate::project_log::entry(command, crate::project_log::kept_params(&params), &landed, at, start.elapsed());
s.log.push(entry);
Comment thread
pick2510 marked this conversation as resolved.
Resolved
}
landed
}

#[cfg(test)]
Expand Down
58 changes: 42 additions & 16 deletions crates/engine/src/cmd/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn commands() -> Vec<C> {
C::new("layout.seriesPage", "Map Series Page", "Publish › Map Frames", series_page).params(r#"{"layout"?, "page": number|"next"|"previous"}"#),
C::new("layout.seriesRefresh", "Refresh Map Series", "Publish › Map Frames", |s, p| series_page(s, &json!({"layout": p.get("layout"), "page": "current"})))
.params(r#"{"layout"?}"#),
C::new("layout.export", "Export Layout", "Publish › Output", export)
C::new("layout.export", "Export Layout", "Publish › Output", |s, p| super::exports::write_here(s, p, "layout.export"))
.view()
.params(r#"{"layout"?, "path": "*.png|*.pdf|*.svg" (PNG/SVG: current page; SVG: each element a group of its own; text and shapes as SVG, map frames and surrounds as vectors, rasters and tiles in them as pictures at dpi), "dpi"?: 72..600, "allPages"?: bool (PDF only; default true: every map series page, up to 500; false: the current page)}"#),
C::new("layout.save", "Save Layout File", "Publish › Save As", |s, p| {
Expand Down Expand Up @@ -3338,8 +3338,8 @@ fn series_page(s: &mut Session, p: &Value) -> CmdResult {

/// Renders a layout to PNG or PDF bytes. `all` renders every map series page (PDF only).
/// What an export of a layout whose texts name installed families is told while the app is still
/// indexing the fonts (its first seconds; an export runs on its interface thread, which mustn't
/// wait): those texts are drawn in their fallbacks. A script or the command line indexes them at
/// indexing the fonts (its first seconds; the app's exports don't wait for them): those texts
/// are drawn in their fallbacks. A script or the command line indexes them at
/// once.
fn named_fonts_unread(layout: &Layout) -> Option<&'static str> {
let named = layout.elements.iter().any(|e| matches!(&e.kind, ElementKind::Text { font_name: Some(_), .. }));
Expand Down Expand Up @@ -3379,6 +3379,10 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool)
let mut out = Vec::new();
let mut png = None;
let mut held = 0usize;
// How far a series' pages have come, when the export runs with progress.
if pages.len() > 1 {
rustgis_data::progress::begin(pages.len());
}
for page in &pages {
if stepping {
series_page(s, &json!({"layout": li, "page": page}))?;
Expand Down Expand Up @@ -3420,6 +3424,8 @@ pub fn layout_bytes(s: &mut Session, li: usize, pdf: bool, dpi: f64, all: bool)
let img = rustgis_render::layout::render(layout, &mut s.project.maps, dpi, &info).ok_or("the page is too large to draw")?;
png = Some(img.encode_png().map_err(|e| e.to_string())?);
}
// A cancelled export stops here, its layout put back below.
rustgis_data::progress::step(1).map_err(|e| e.to_string())?;
}
Ok((out, png))
};
Expand Down Expand Up @@ -3471,24 +3477,44 @@ pub(crate) fn pictured_notices(s: &mut Session, frames: &[(u32, String)], pages:
}
}

fn export(s: &mut Session, p: &Value) -> CmdResult {
/// `layout.export`: a layout to a PNG, PDF or SVG. It draws a copy of the project, so the project
/// can change meanwhile (a map series steps through its pages on the copy); a cancelled export
/// stops at the next page, or its map's wait for tiles, and writes nothing.
pub(crate) fn export_work(s: &Session, p: &Value) -> Result<super::exports::Work, String> {
let path = ps(p, "path").ok_or("path is required")?.to_string();
let li = index(s, p)?;
let lower = path.to_ascii_lowercase();
let pdf = lower.ends_with(".pdf");
if lower.ends_with(".svg") {
let text = layout_svg(s, li, f(p, "dpi").unwrap_or(150.0))?;
rustgis_data::io::write_atomic(Path::new(&path), text.as_bytes()).map_err(|e| format!("{path}: {e}"))?;
s.notify(format!("Exported {path}"), false);
return Ok(json!({"path": path, "bytes": text.len()}));
}
if !pdf && !lower.ends_with(".png") {
let (pdf, svg) = (lower.ends_with(".pdf"), lower.ends_with(".svg"));
if !pdf && !svg && !lower.ends_with(".png") {
return Err("export to a .png, .pdf or .svg file".into());
}
let bytes = layout_bytes(s, li, pdf, f(p, "dpi").unwrap_or(150.0), b(p, "allPages").unwrap_or(true))?;
rustgis_data::io::write_atomic(Path::new(&path), &bytes).map_err(|e| format!("{path}: {e}"))?;
s.notify(format!("Exported {path}"), false);
Ok(json!({"path": path, "bytes": bytes.len()}))
let (dpi, all) = (f(p, "dpi").unwrap_or(150.0), b(p, "allPages").unwrap_or(true));
let mut project = s.project.clone();
Comment thread
pick2510 marked this conversation as resolved.
Resolved
for m in &mut project.maps {
m.detach_reads();
}
Ok(Box::new(move || {
let mut copy = Session::new(project);
Comment thread
pick2510 marked this conversation as resolved.
Resolved
let written = (|| {
let bytes = if svg { layout_svg(&mut copy, li, dpi)?.into_bytes() } else { layout_bytes(&mut copy, li, pdf, dpi, all)? };
rustgis_data::progress::check().map_err(|e| e.to_string())?;
rustgis_data::io::write_atomic(Path::new(&path), &bytes).map_err(|e| format!("{path}: {e}"))?;
Ok(json!({"path": path, "bytes": bytes.len()}))
})();
// What drawing it said (frames drawn as pictures, fonts it lacks), as the command says it
// here; past the notices a session keeps, how many earlier ones there were.
let kept = std::mem::take(&mut copy.notices);
let earlier = copy.notices_given().saturating_sub(kept.len() as u64);
let mut said: Vec<_> = Vec::new();
if earlier > 0 {
said.push((
rustgis_data::diagnostics::Severity::Warning,
format!("{path}: {earlier} earlier notices from drawing it aren't shown"),
));
}
said.extend(kept.into_iter().map(|n| (n.severity, n.text)));
super::exports::drawn(&copy.project.maps, said, written, path)
}))
}

/// The layout `li` as it is now (a map series at its current page) as SVG, kept as vectors with
Expand Down
Loading
Loading