GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
//! File → Export → Export for Screens (`document.exportForScreens`): the chosen artboards (or the
//! whole document, the chosen assets of the Asset Export panel, layers, or the selection) in every
//! format row at each row's size, into a folder and its sub-folders, or returned for download (each
//! file, or one ZIP); the presets the dialog offers; and the settings the document remembers
//! (`document.exportSettings`), which the Asset Export panel shares ([`SHARED_KEYS`]).
//!
//! A batch export names every file from one pattern (`name`, see [`super::naming`]: the document's
//! and the item's names, a counter, the date), and sorts the files into folders by row, by format,
//! by item, or by item and format (`folders`).

use std::collections::HashSet;
use std::sync::Arc;

use serde::{Deserialize, Serialize};
use serde_json::{Map, Value, json};
use vectorcraft_doc::Document;
use vectorcraft_geom::Rect;

use super::super::{EngineError, NodeId, Result, Session, bad, bool_or, str_param};
use super::encode::{add_note, single_artboard};
use super::export::{export_crop, export_reads, isolated, selected_artwork, without_artboards};
use super::naming::{self, NameParts};
use super::{
    ARTBOARD_PARAMS, ArtboardPick, Format, Note, artboard_file_names_keeping_dots, create_dir, distinct_names_by, encode_with_notes, merge,
    note_texts, unique_file_names_keeping_dots, with_black_output, writable, write_file,
};

const C: &str = "document.exportForScreens";
/// The Asset Export panel's export, which exports through [`export`].
const ASSETS: &str = "assets.export";

/// How big a raster row's files are.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ScreenSize {
    /// Pixels per point (`2x`).
    Scale(f64),
    /// Pixels per inch, stored in the file (`144ppi`: twice 72, so `2x`).
    Ppi(f64),
    /// The width in pixels; the height follows the artboard's proportions (`100w`).
    Width(f64),
    /// The height in pixels (`100h`).
    Height(f64),
}

impl ScreenSize {
    /// `2x` (or `2`), `100w`, `100h` or `72ppi`, in any case; positive numbers only.
    pub fn parse(s: &str) -> Option<Self> {
        let s = s.trim().to_ascii_lowercase();
        // Digits and `.` only: `str::parse` would also take `+2`, `1e2`, `inf` and `nan`.
        let num = |t: &str| {
            let t = t.trim();
            if !t.bytes().all(|b| b.is_ascii_digit() || b == b'.') {
                return None;
            }
            t.parse::<f64>().ok().filter(|v| v.is_finite() && *v > 0.0)
        };
        if let Some(n) = s.strip_suffix("ppi") {
            return num(n).map(Self::Ppi);
        }
        if let Some(n) = s.strip_suffix('w') {
            return num(n).map(Self::Width);
        }
        if let Some(n) = s.strip_suffix('h') {
            return num(n).map(Self::Height);
        }
        num(s.strip_suffix('x').unwrap_or(&s)).map(Self::Scale)
    }

    /// The size a format row asks for: `width`, else `height`, else `ppi`, else `scale` (a number,
    /// or text such as `"2x"`, `"100w"`, `"72ppi"`); 1x when it names none.
    fn of_row(row: &Value) -> Result<Self> {
        let positive = |key: &str, make: fn(f64) -> Self| -> Option<Result<Self>> {
            let v = row.get(key).filter(|v| !v.is_null())?;
            Some(
                v.as_f64().filter(|n| n.is_finite() && *n > 0.0).map(make).ok_or_else(|| bad(C, format!("{key} must be a positive number, not {v}"))),
            )
        };
        if let Some(r) = positive("width", Self::Width).or_else(|| positive("height", Self::Height)).or_else(|| positive("ppi", Self::Ppi)) {
            return r;
        }
        match row.get("scale") {
            None | Some(Value::Null) => Ok(Self::Scale(1.0)),
            Some(Value::String(s)) => Self::parse(s)
                .ok_or_else(|| bad(C, format!("scale `{s}`: a factor such as 2x, a width (100w), a height (100h) or a resolution (72ppi)"))),
            Some(_) => positive("scale", Self::Scale).unwrap_or(Ok(Self::Scale(1.0))),
        }
    }

    /// The scale factor, for the sizes that have one of their own.
    fn factor(self) -> Option<f64> {
        match self {
            Self::Scale(s) => Some(s),
            Self::Ppi(p) => Some(p / 72.0),
            Self::Width(_) | Self::Height(_) => None,
        }
    }

    /// Pixels per point for `region` (`cmd` names the command in the error).
    pub(super) fn scale_for(self, cmd: &str, region: Rect) -> Result<f64> {
        let per = |px: f64, pt: f64| (pt > 0.0).then(|| px / pt).ok_or_else(|| bad(cmd, "an empty artboard has no width or height to scale to"));
        match self {
            Self::Width(w) => per(w, region.width()),
            Self::Height(h) => per(h, region.height()),
            _ => Ok(self.factor().unwrap_or(1.0)),
        }
    }

    /// The sub-folder files of this size go into: `1x`, `2x`, `100w`, `100h` (numbers to 3
    /// decimals: 80ppi is `1.111x`).
    pub fn label(self) -> String {
        let n = |v: f64| (v * 1000.0).round() / 1000.0;
        match self {
            Self::Width(w) => format!("{}w", n(w)),
            Self::Height(h) => format!("{}h", n(h)),
            _ => format!("{}x", n(self.factor().unwrap_or(1.0))),
        }
    }

    /// The file-name suffix by default: none at 1x, else `@2x`, `@100w`, `@100h` (a resolution
    /// names its scale: 144ppi is `@2x`).
    pub fn suffix(self) -> String {
        match self.factor() {
            Some(s) if (s - 1.0).abs() < 1e-9 => String::new(),
            _ => format!("@{}", self.label()),
        }
    }
}

/// A file-name suffix that names a pixel size (`@2x`, `@0.5x`, `@100w`, `@100h`): vector rows
/// leave it out.
fn is_size_suffix(s: &str) -> bool {
    s.strip_prefix('@').is_some_and(|t| t.ends_with(|c: char| c.is_ascii_alphabetic()) && ScreenSize::parse(t).is_some())
}

/// The presets of the dialog's format rows: (id, label).
pub const PRESETS: [(&str, &str); 2] = [("mobile", "Mobile 1x/2x/3x"), ("density", "Density buckets (0.75x–4x)")];

/// Screen density buckets: (sub-folder, scale).
const DENSITY: [(&str, f64); 6] = [("ldpi", 0.75), ("mdpi", 1.0), ("hdpi", 1.5), ("xhdpi", 2.0), ("xxhdpi", 3.0), ("xxxhdpi", 4.0)];

/// The format rows of preset `id` (see [`PRESETS`]): PNG at 1x, 2x and 3x (`@2x`, `@3x`), or PNG
/// at every density bucket, each in its bucket's sub-folder under the same name.
pub fn preset_rows(id: &str) -> Option<Vec<Value>> {
    let png = |s: f64| ScreenSize::Scale(s);
    match id {
        "mobile" => Some([1.0, 2.0, 3.0].iter().map(|s| json!({"format": "png", "scale": png(*s).label(), "suffix": png(*s).suffix()})).collect()),
        "density" => {
            Some(DENSITY.iter().map(|(folder, s)| json!({"format": "png", "scale": png(*s).label(), "suffix": "", "folder": folder})).collect())
        }
        _ => None,
    }
}

/// A name safe as one file or folder name: letters, digits, `-`, `_` and `.` kept, anything else
/// `-` (`None` when nothing but dots is left: `.` and `..` name other folders).
fn safe_name(s: &str) -> Option<String> {
    let n: String = s.trim().chars().map(|c| if c.is_alphanumeric() || "-_.".contains(c) { c } else { '-' }).collect();
    (!n.trim_matches('.').is_empty()).then_some(n)
}

/// Text that goes into a file name (prefix, suffix, a data set's name in Batch, a name pattern's text
/// and the names it puts in): the characters Windows does not allow in a file name (`/`, `\`, `:`,
/// `<`, `>`, `"`, `|`, `?`, `*` and the control characters U+0000 to U+001F,
/// <https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file>) become `-` on every
/// platform, so the name cannot make a subfolder or point outside the folder the file is written to.
pub(super) fn file_part(s: &str) -> String {
    s.chars().map(|c| if matches!(c, '/' | '\\' | ':' | '<' | '>' | '"' | '|' | '?' | '*') || c < ' ' { '-' } else { c }).collect()
}

/// A whole file or folder name (`Logo@2x.png`, a sub-folder) through [`file_part`], without
/// trailing periods and spaces, and with `-` after a Windows device name that is the whole name or
/// comes before its first period: `CON`, `PRN`, `AUX`, `NUL`, `COM1` to `COM9` and `LPT1` to `LPT9`,
/// also with `¹`, `²` or `³` for the digit, in any case (`CON.png` becomes `CON-.png`). The same on
/// every platform. Spaces between the device name and the period are dropped (`CON .png` becomes
/// `CON-.png`).
pub(crate) fn safe_file_name(s: &str) -> String {
    name_without_device(&file_part(s))
}

/// [`safe_file_name`] without [`file_part`]: drops trailing periods and spaces and puts `-` after a
/// Windows device name, and keeps every other character, so a source file name with `\` or `:` keeps
/// it where the file system allows it.
pub(super) fn name_without_device(name: &str) -> String {
    let name = name.trim_end_matches(['.', ' ']);
    let (base, rest) = name.split_at(name.find('.').unwrap_or(name.len()));
    // Windows ignores spaces before the extension, so `CON .png` is the CON device too.
    let base = base.trim_end_matches(' ');
    let upper = base.to_ascii_uppercase();
    let numbered = |n: &str| n.chars().count() == 1 && n.chars().all(|c| matches!(c, '1'..='9' | '¹' | '²' | '³'));
    let device = matches!(upper.as_str(), "CON" | "PRN" | "AUX" | "NUL")
        || upper.strip_prefix("COM").or_else(|| upper.strip_prefix("LPT")).is_some_and(numbered);
    if device { format!("{base}-{rest}") } else { name.to_string() }
}

/// Keys of a format row that aren't encoder options.
pub(crate) const ROW_KEYS: [&str; 7] = ["format", "scale", "ppi", "width", "height", "suffix", "folder"];

/// The writable formats a format row can't name.
pub(crate) const UNWRITTEN_FORMATS: [&str; 6] = ["template", "txt", "dxf", "emf", "wmf", "hpgl"];

/// One format row: what to write, at which size, with which suffix, into which sub-folder.
struct Row {
    format: &'static Format,
    size: ScreenSize,
    suffix: String,
    /// The sub-folder with Create Sub-folders: the row's `folder`, else its size (raster) or format.
    folder: String,
    /// The encoder options: the format's settings, then the row's own.
    options: Value,
    /// What makes two rows the same file: format, size, suffix and options.
    key: String,
    /// The row or the format's settings set `bleed` (a PDF row's own bleed, zero included).
    bleed_given: bool,
}

impl Row {
    fn parse(s: &Session, row: &Value, settings: &Value) -> Result<Self> {
        if !row.is_object() {
            return Err(bad(C, "each format is an object {format, scale?, suffix?}"));
        }
        let format = writable(C, Some(str_param(row, "format").unwrap_or("png")), None)?;
        if UNWRITTEN_FORMATS.contains(&format.id) {
            return Err(bad(C, "Export for Screens writes png, png8, jpg, webp, gif, tiff, bmp, tga, psd, svg, svgz, pdf, eps, ai or vectorcraft"));
        }
        // Vector formats have no pixel size: it doesn't apply and adds no @2x suffix (not even one
        // left over from a raster row switched to SVG or PDF).
        let size = if format.raster { ScreenSize::of_row(row)? } else { ScreenSize::Scale(1.0) };
        let suffix = match str_param(row, "suffix") {
            Some(s) if !format.raster && is_size_suffix(s) => String::new(),
            Some(s) => file_part(s),
            None => size.suffix(),
        };
        let folder = match str_param(row, "folder").and_then(safe_name) {
            Some(f) => f,
            None if format.raster => size.label(),
            None => format.id.to_uppercase(),
        };
        // The format's settings, without anything that picks artboards or sizes (rows do that).
        let mut options = Map::new();
        if let Some(o) = settings.get(format.id).and_then(Value::as_object) {
            options.extend(
                o.iter().filter(|(k, _)| !ROW_KEYS.contains(&k.as_str()) && k.as_str() != "useArtboards").map(|(k, v)| (k.clone(), v.clone())),
            );
        }
        if let Some(o) = without_artboards(row).as_object() {
            options.extend(o.iter().filter(|(k, _)| !ROW_KEYS.contains(&k.as_str())).map(|(k, v)| (k.clone(), v.clone())));
        }
        for k in ARTBOARD_PARAMS {
            options.remove(k);
        }
        // A file per artboard is for viewing: it doesn't carry the whole document to edit.
        options.entry("preserveEditing").or_insert(json!(false));
        let bleed_given = options.get("bleed").is_some_and(|b| !b.is_null());
        let options = Value::Object(options);
        // A PDF preset (built-in or saved) becomes its settings.
        let options = if format.id == "pdf" { super::pdf::expand_preset(s, C, &options)?.into_owned() } else { options };
        let mut row = Self { format, size, suffix, folder, options, key: String::new(), bleed_given };
        row.key = row.file_key();
        Ok(row)
    }

    /// What makes two rows the same file: two rows that would write the same bytes under the same
    /// name are one file.
    fn file_key(&self) -> String {
        format!("{}|{}|{}|{}", self.format.id, self.size.label(), self.suffix, self.options)
    }

    /// Formats that hold a whole document to edit (native, Illustrator, EPS): written from a
    /// document of the item alone, one artboard, rather than with an `artboard` option.
    fn whole_document(&self) -> bool {
        matches!(self.format.id, "vectorcraft" | "ai" | "eps")
    }

    /// The encoder options for `region`, `artboard` of the document written.
    fn options_for(&self, region: Rect, artboard: Option<usize>) -> Result<Value> {
        let mut o = self.options.clone();
        if let Some(b) = artboard.filter(|_| !self.whole_document()) {
            o["artboard"] = json!(b);
        }
        if self.format.raster {
            match self.size {
                // The resolution is stored in the file too.
                ScreenSize::Ppi(ppi) => o["ppi"] = json!(ppi),
                size => {
                    o["scale"] = json!(size.scale_for(C, region)?);
                    if let Some(m) = o.as_object_mut() {
                        m.remove("ppi");
                    }
                }
            }
        }
        Ok(o)
    }
}

/// The settings the Asset Export panel shares with the dialog: what is exported, not which art
/// or where to.
pub const SHARED_KEYS: [&str; 6] = ["formats", "preset", "settings", "prefix", "subfolders", "antiAlias"];

/// The keys `document.exportForScreens` reads: those of [`ScreenParams`], the formats and their
/// settings, `antiAlias`, `exportPreset` and `openLocation`, which the app reads.
pub(crate) const PARAM_KEYS: [&str; 25] = [
    "artboard",
    "artboards",
    "range",
    "folder",
    "zip",
    "fullDocument",
    "includeBleed",
    "includeArtboardFill",
    "subfolders",
    "preset",
    "prefix",
    "assets",
    "layers",
    "selection",
    "name",
    "counterStart",
    "counterPer",
    "date",
    "folders",
    "pdfs",
    "formats",
    "settings",
    "antiAlias",
    "exportPreset",
    "openLocation",
];

/// What `document.exportForScreens` reads besides the formats.
#[derive(Default, Deserialize)]
#[serde(default, rename_all = "camelCase")]
struct ScreenParams {
    #[serde(flatten)]
    boards: ArtboardPick,
    folder: Option<String>,
    zip: bool,
    full_document: bool,
    include_bleed: bool,
    /// Include Artboard Fill: the artboards' background colors behind the art (default on).
    include_artboard_fill: Option<bool>,
    subfolders: bool,
    preset: Option<String>,
    prefix: Option<String>,
    /// Asset ids (Asset Export) to write instead of artboards.
    assets: Option<Vec<u64>>,
    /// Layer ids to write instead of artboards: each layer's art alone.
    layers: Option<Vec<u64>>,
    /// The selection instead of artboards: `one` file of it all, or one for `each` object.
    selection: Option<String>,
    /// The file name pattern ([`naming`]); without it, `prefix` + name + suffix.
    name: Option<String>,
    /// The counter's first value (`####` in `name`).
    counter_start: Option<u64>,
    /// The counter counts `item`s (every file of an artboard has its number) or `file`s.
    counter_per: Option<String>,
    /// The date the pattern's date tokens use (`YYYY-MM-DD`); default today (UTC).
    date: Option<String>,
    /// Where files go: `none` (all in the folder), `rows` (the rows' sub-folders, as
    /// `subfolders`), `format` (a folder per extension), `item`, or `itemFormat`.
    folders: Option<String>,
    /// Export PDFs As: `single` (the artboards a PDF row writes are the pages of one file; refused
    /// with assets, layers or `selection: "each"`) or `multiple` (a file per artboard, the default).
    pdfs: Option<String>,
}

/// How exported files are sorted into folders.
#[derive(Clone, Copy, Debug, PartialEq)]
enum Folders {
    None,
    Rows,
    Format,
    Item,
    ItemFormat,
}

impl Folders {
    fn of(o: &ScreenParams) -> Result<Self> {
        Ok(match o.folders.as_deref() {
            None if o.subfolders || o.preset.is_some() => Self::Rows,
            None | Some("none" | "") => Self::None,
            Some("rows") => Self::Rows,
            Some("format") => Self::Format,
            Some("item") => Self::Item,
            Some("itemFormat") => Self::ItemFormat,
            Some(f) => return Err(bad(C, format!("folders `{f}`: none, rows, format, item or itemFormat"))),
        })
    }
}

/// One thing a batch export writes a file of per row: an artboard, an asset, a layer, the
/// selection or one selected object, or the whole document.
struct Item {
    /// Its name as the user gave it (`<name>`).
    name: String,
    /// Its name made safe and unique among the items (the default file name, the item folder).
    stem: String,
    /// The document written: the whole document with `artboard`, or the item alone.
    doc: Arc<Document>,
    artboard: Option<usize>,
    region: Rect,
    /// Full Document: what a PDF row writes (every artboard of the document).
    every_artboard: Option<Arc<Document>>,
}

impl ScreenParams {
    fn of(p: &Value) -> Result<Self> {
        if p.is_object() { Self::deserialize(p).map_err(|e| bad(C, e.to_string())) } else { Ok(Self::default()) }
    }

    /// An error when more than one of `assets`, `layers`, `selection` and `fullDocument` is given,
    /// and when `pdfs: "single"` is given with `assets`, `layers` or `selection: "each"`, whose
    /// files each hold the art of one item.
    fn one_source(&self) -> Result<()> {
        let sources = [self.full_document, self.assets.is_some(), self.layers.is_some(), self.selection.is_some()].iter().filter(|b| **b).count();
        if sources > 1 {
            return Err(bad(C, "give one of assets, layers, selection or fullDocument"));
        }
        let per_piece = self.assets.is_some() || self.layers.is_some() || self.selection.as_deref() == Some("each");
        if per_piece && self.pdfs.as_deref() == Some("single") {
            return Err(bad(C, "pdfs `single` writes artboards as the pages of one PDF: assets, layers and selection `each` write a PDF each"));
        }
        Ok(())
    }

    /// `selection`: `None` without it, `Some(false)` for `one` file, `Some(true)` for `each` object.
    fn selection_mode(&self) -> Result<Option<bool>> {
        match self.selection.as_deref() {
            None => Ok(None),
            Some("one") => Ok(Some(false)),
            Some("each") => Ok(Some(true)),
            Some(m) => Err(bad(C, format!("selection `{m}`: one or each"))),
        }
    }
}

/// How a batch export names its files and sorts them into folders.
struct Naming {
    /// The `name` pattern, when it holds more than spaces.
    pattern: Option<String>,
    /// The `prefix`, through [`file_part`].
    prefix: String,
    /// The document's name, safe as a file name (`<filename>`, the Full Document's item).
    filename: String,
    /// The date and time the pattern's tokens use.
    when: [i64; 6],
    counter_start: u64,
    /// The counter counts files rather than items.
    per_file: bool,
    folders: Folders,
    /// Export PDFs As › Single file.
    single_pdf: bool,
}

impl Naming {
    /// The naming of params `o` for a document named `title`: its file's path, or its title before it is saved.
    fn of(o: &ScreenParams, title: &str) -> Result<Self> {
        let folders = Folders::of(o)?;
        // A prefix names files, not folders.
        let prefix = file_part(o.prefix.as_deref().unwrap_or(""));
        let when = match o.date.as_deref() {
            Some(d) => naming::parse_date(d).ok_or_else(|| bad(C, format!("date `{d}`: YYYY-MM-DD")))?,
            None => vectorcraft_doc::metadata::output_time().map_or([1970, 1, 1, 0, 0, 0], vectorcraft_doc::metadata::civil),
        };
        let per_file = match o.counter_per.as_deref() {
            None | Some("item") => false,
            Some("file") => true,
            Some(c) => return Err(bad(C, format!("counterPer `{c}`: item or file"))),
        };
        let single_pdf = match o.pdfs.as_deref() {
            None | Some("multiple") => false,
            Some("single") => true,
            Some(m) => return Err(bad(C, format!("pdfs `{m}`: single or multiple"))),
        };
        let filename = safe_name(&super::file_stem(title)).unwrap_or_else(|| "Untitled".into());
        let pattern = o.name.clone().filter(|n| !n.trim().is_empty());
        Ok(Self { pattern, prefix, filename, when, counter_start: o.counter_start.unwrap_or(1), per_file, folders, single_pdf })
    }

    /// The sub-folder (`None` for the export folder itself) and the file name of the file row `r`
    /// writes of `item`, the `index`th item (from 0), when `files` files came before it.
    fn file(&self, item: &Item, index: usize, files: u64, r: &Row) -> (Option<String>, String) {
        let counter = self.counter_start.saturating_add(if self.per_file { files } else { index as u64 });
        let ext = r.format.extensions.first().copied().unwrap_or(r.format.id);
        let prefix = &self.prefix;
        let stem = match self.pattern.as_deref() {
            Some(pattern) => {
                let parts = NameParts {
                    filename: &self.filename,
                    name: &item.name,
                    index: index + 1,
                    counter,
                    format: ext,
                    scale: &if r.format.raster { r.size.label() } else { String::new() },
                    suffix: &r.suffix,
                    when: self.when,
                };
                let named = naming::render(pattern, &parts);
                let named = if named.is_empty() { item.stem.clone() } else { named };
                if naming::has_suffix_token(pattern) { format!("{prefix}{named}") } else { format!("{prefix}{named}{}", r.suffix) }
            }
            None => format!("{prefix}{}{}", item.stem, r.suffix),
        };
        let file = safe_file_name(&format!("{stem}.{ext}"));
        let sub = match self.folders {
            Folders::None => None,
            Folders::Rows => Some(safe_file_name(&r.folder)),
            Folders::Format => Some(ext.to_string()),
            Folders::Item => Some(item.stem.clone()),
            Folders::ItemFormat => Some(format!("{}/{ext}", item.stem)),
        };
        (sub, file)
    }
}

/// The format rows `p` asks for (`formats`, else `preset`'s, else one PNG at 1x), each with the
/// format's `settings` and the top-level `antiAlias`.
fn rows_of(s: &Session, p: &Value, preset: Option<&str>) -> Result<Vec<Row>> {
    let settings = p.get("settings").cloned().unwrap_or(Value::Null);
    let rows: Vec<Value> = match (preset, p.get("formats")) {
        (Some(_), Some(f)) if !f.is_null() => return Err(bad(C, "give formats or a preset, not both")),
        (Some(id), _) => preset_rows(id).ok_or_else(|| bad(C, format!("preset `{id}`: mobile or density")))?,
        (None, Some(Value::Array(rows))) => rows.clone(),
        (None, None | Some(Value::Null)) => vec![json!({})],
        (None, Some(v)) => return Err(bad(C, format!("formats must be an array of rows, not {v}"))),
    };
    let mut rows: Vec<Row> = rows.iter().map(|r| Row::parse(s, r, &settings)).collect::<Result<_>>()?;
    if rows.is_empty() {
        return Err(bad(C, "no formats to export"));
    }
    // A top-level anti-aliasing mode applies to every row that doesn't name its own.
    if let Some(aa) = p.get("antiAlias") {
        for r in &mut rows {
            if let Some(m) = r.options.as_object_mut() {
                m.entry("antiAlias").or_insert_with(|| aa.clone());
            }
            // Two rows whose options are now the same are one file.
            r.key = r.file_key();
        }
    }
    Ok(rows)
}

/// A note for each option of a format row (`formats`) or of a format's `settings` in the request
/// `asked` that some format takes but the file the row writes, or every row of that format writes,
/// doesn't read (`{format: png, quality: 80}`), in the wording of a save's note and named as the
/// request names it (`formats[0].quality`, `settings.png.quality`). The rows and settings the
/// export writes with are those of `p` (`asked` with a preset or the remembered settings laid
/// under it). A row's own keys (`scale`, `suffix`…), the keys the export reads for every format
/// (`includeArtboardFill`, `richBlack`, `params`) and an artboard choice (the export picks the
/// artboards) get none, and so does an exporter plug-in's format.
pub(crate) fn unused_option_notes(asked: &Value, p: &Value) -> Vec<String> {
    let listed = |k: &str| super::FORMATS.iter().filter(|f| f.write).flat_map(|f| f.options).any(|o| o.name == k);
    let own = |k: &str| {
        ROW_KEYS.contains(&k) || ARTBOARD_PARAMS.contains(&k) || matches!(k, "useArtboards" | "includeArtboardFill" | "richBlack" | "params")
    };
    // The keys of `given` that `f`, writing with `options` (the format's settings and the row's
    // own), doesn't read.
    let unread = |f: &Format, given: &Map<String, Value>, options: &Value| -> Vec<String> {
        if vectorcraft_plugins::export::by_format(f.id).is_some() {
            return Vec::new();
        }
        let legacy = super::save::legacy_file(C, f, options);
        given.iter().filter(|(k, v)| !v.is_null() && !own(k) && listed(k) && !export_reads(f, legacy, k)).map(|(k, _)| k.clone()).collect()
    };
    let note = |key: String, f: &Format, options: &Value| super::save::unused_option_note(&key, &super::save::written_as(C, f, options));
    let settings = p.get("settings").and_then(Value::as_object);
    let of_format = |id: &str| settings.and_then(|s| s.get(id)).and_then(Value::as_object).cloned().unwrap_or_default();
    // A row's format and the options it writes with.
    let row_of = |row: &Value| -> Option<(&'static Format, Value)> {
        let (Some(row), Ok(f)) = (row.as_object(), writable(C, Some(str_param(row, "format").unwrap_or("png")), None)) else { return None };
        let mut options = of_format(f.id);
        options.extend(row.iter().map(|(k, v)| (k.clone(), v.clone())));
        Some((f, Value::Object(options)))
    };
    let mut out: Vec<String> = Vec::new();
    if let Some(Value::Array(rows)) = asked.get("formats") {
        for (i, row) in rows.iter().enumerate() {
            let (Some((f, options)), Some(given)) = (row_of(row), row.as_object()) else { continue };
            out.extend(unread(f, given, &options).into_iter().map(|k| note(format!("formats[{i}].{k}"), f, &options)));
        }
    }
    // The rows written, each with its own keys: a format's settings are read as each of its rows
    // writes them (a row may name another `.ai` version), else as they are.
    let written: Vec<(&Format, Value, Map<String, Value>)> = match p.get("formats") {
        Some(Value::Array(rows)) => {
            rows.iter().filter_map(|row| row_of(row).map(|(f, options)| (f, options, row.as_object().cloned().unwrap_or_default()))).collect()
        }
        _ => Vec::new(),
    };
    for (id, given) in asked.get("settings").and_then(Value::as_object).into_iter().flatten() {
        let (Some(given), Ok(f)) = (given.as_object(), writable(C, Some(id), None)) else { continue };
        let mut rows: Vec<(&Value, Option<&Map<String, Value>>)> =
            written.iter().filter(|(g, ..)| g.id == f.id).map(|(_, o, row)| (o, Some(row))).collect();
        let alone = Value::Object(of_format(f.id));
        if rows.is_empty() {
            rows.push((&alone, None));
        }
        let unread_by: Vec<Vec<String>> = rows.iter().map(|(o, _)| unread(f, given, o)).collect();
        // A setting a row reads and doesn't give itself is used. One no row uses is noted when a
        // row doesn't read it, naming the first such row's file.
        for (k, v) in given {
            // A row's own key, null too, replaces the setting.
            let overridden = |row: Option<&Map<String, Value>>| row.is_some_and(|r| r.contains_key(k));
            let used = rows.iter().zip(&unread_by).any(|((_, row), u)| !u.contains(k) && !overridden(*row));
            let unread_row = rows.iter().zip(&unread_by).find(|(_, u)| u.contains(k));
            if let (false, false, Some(((options, _), _))) = (v.is_null(), used, unread_row) {
                out.push(note(format!("settings.{id}.{k}"), f, options));
            }
        }
    }
    out
}

/// Check the export settings in `p` (formats, preset, settings…) as an export would read them.
pub(crate) fn check_settings(s: &Session, p: &Value) -> Result<()> {
    rows_of(s, p, ScreenParams::of(p)?.preset.as_deref()).map(|_| ())
}

/// `name` the way a case-insensitive file system compares names: lower-cased, then each character
/// upper-cased where its upper case is one character. Names with the same lower case give the
/// same key, and so do `ΟΣ`, `ος` and `οσ` (`ΟΣ`); `ᾈ` and `ᾀ` give `ᾀ`, and `ß` stays `ß`.
/// Dotless `ı` stays `ı`, as NTFS and Unicode case folding outside Turkic languages keep it apart
/// from `i` and `I`.
fn fold(name: &str) -> String {
    name.to_lowercase()
        .chars()
        .map(|c| {
            if c == 'ı' {
                return c;
            }
            let mut upper = c.to_uppercase();
            match (upper.next(), upper.next()) {
                (Some(u), None) => u,
                _ => c,
            }
        })
        .collect()
}

/// The file names (and bytes) an export wrote, or the files it returns.
#[derive(Default)]
struct Output {
    /// Relative names already used, by their [`fold`]ed spelling, with what wrote each (two
    /// identical rows write one file; different files that would share a name get `-2`, `-3`…).
    taken: std::collections::HashMap<String, String>,
    /// Each sub-folder as the first file in it spells it, by its [`fold`]ed name.
    folders: std::collections::HashMap<String, String>,
    /// Sub-folders already made, by their [`fold`]ed names.
    made: HashSet<String>,
    /// For each [`fold`]ed name asked for, the number to try next: many files given one name
    /// would otherwise try `-2`, `-3`… from the start each time.
    next: std::collections::HashMap<String, u64>,
    /// Each [`fold`]ed name asked for with the file (`key`) that got a name from it.
    claimed: HashSet<(String, String)>,
    /// `(relative name, bytes)` kept to return or zip (empty when written to a folder).
    kept: Vec<(String, Vec<u8>)>,
    /// The paths written.
    written: Vec<String>,
}

impl Output {
    /// The name the file `key` writes as `name` takes: `name`, else `name-2`, `name-3`… when
    /// another file has it, with or without a name pattern; `None` when the same file (`key`) was
    /// written as `name` already. Names are compared without case, and a sub-folder keeps the
    /// spelling of the first file put in it.
    fn claim(&mut self, name: &str, key: &str) -> Option<String> {
        let name = match name.rsplit_once('/') {
            Some((dir, file)) => {
                let dir = self.folders.entry(fold(dir)).or_insert_with(|| dir.to_string());
                format!("{dir}/{file}")
            }
            None => name.to_string(),
        };
        let asked = (fold(&name), key.to_string());
        // The same file under the same name again: it has its name already.
        if self.claimed.contains(&asked) {
            return None;
        }
        let (base, ext) = name.rsplit_once('.').map_or((name.as_str(), ""), |(b, e)| (b, e));
        let start = self.next.get(&asked.0).copied().unwrap_or(1);
        for n in start.. {
            let candidate = if n == 1 {
                name.to_string()
            } else if ext.is_empty() {
                format!("{base}-{n}")
            } else {
                format!("{base}-{n}.{ext}")
            };
            match self.taken.get(&fold(&candidate)) {
                Some(k) if k == key => return None,
                Some(_) => continue,
                None => {
                    self.taken.insert(fold(&candidate), key.to_string());
                    self.next.insert(asked.0.clone(), n + 1);
                    self.claimed.insert(asked);
                    return Some(candidate);
                }
            }
        }
        None
    }

    /// Write `bytes` as `name` (relative) under `folder` with `writer`, or keep them.
    fn put(
        &mut self,
        folder: Option<&str>,
        keep: bool,
        name: String,
        bytes: Vec<u8>,
        writer: &mut dyn FnMut(&str, &[u8]) -> Result<()>,
    ) -> Result<()> {
        match folder {
            Some(dir) if !keep => {
                if let Some((sub, _)) = name.rsplit_once('/')
                    && self.made.insert(fold(sub))
                {
                    create_dir(&format!("{dir}/{sub}"))?;
                }
                let path = format!("{dir}/{name}");
                // A cancel that came while the file was encoded writes nothing more.
                vectorcraft_doc::cancel::check()?;
                writer(&path, &bytes)?;
                self.written.push(path);
            }
            _ => self.kept.push((name, bytes)),
        }
        Ok(())
    }
}

/// File → Export for Screens: [`export`] (over the settings of `exportPreset`, when given), with
/// the params remembered in the document (`document.exportSettings`).
pub(super) fn export_for_screens(s: &mut Session, p: &Value) -> Result<Value> {
    // A folder the client gave, as against the one its `exportPreset` keeps.
    let given = p.get("folder").is_some();
    let asked = p;
    let p = with_preset(s, p)?;
    let hidden = if given { None } else { str_param(&p, "folder").filter(|f| !f.is_empty() && !super::access::shown(f)) };
    let mut r = export(s, &p, asked).map_err(|e| match hidden {
        Some(folder) => super::access::naming_in_error(e, folder),
        None => e,
    })?;
    remember(s, &p);
    // A prepared export is written later, maybe on another thread: it names the folder as this
    // request may see it.
    if let (Some(folder), Some(Some(export))) = (hidden, s.screens_hand_off.as_mut()) {
        export.hidden = Some((folder.to_string(), super::access::error_name(folder)));
    }
    if !given && let Some(folder) = str_param(&p, "folder").filter(|f| !f.is_empty()) {
        leave_out_unreadable_folder(&mut r, folder);
    }
    Ok(r)
}

/// The reply `r` of an export into `folder`, a folder the client did not give, as the request being
/// served may see it: when `folder` is outside the read roots ([`super::access::shown`]), the ZIP's
/// `path` is left out and `files` names each file written relative to `folder` (`2x/Logo@2x.png`),
/// as the reply of an export to a ZIP names them.
fn leave_out_unreadable_folder(r: &mut Value, folder: &str) {
    if !super::access::shown(folder) {
        leave_out_folder(r, folder);
    }
}

/// The reply `r` of an export into `folder` with the ZIP's `path` left out and `files` named
/// relative to `folder` ([`leave_out_unreadable_folder`]).
fn leave_out_folder(r: &mut Value, folder: &str) {
    let Some(o) = r.as_object_mut() else { return };
    o.remove("path");
    let inside = format!("{folder}/");
    if let Some(Value::Array(files)) = o.get_mut("files") {
        for file in files {
            // A ZIP's files are named relative to it already.
            if let Some(name) = file.as_str().and_then(|path| path.strip_prefix(&inside)) {
                *file = json!(name);
            }
        }
    }
}

/// Export settings kept in the document or a preset, as the request being served may see them: a
/// `folder` outside the read roots ([`super::access::shown`]) is left out.
pub(super) fn leave_out_unreadable_settings_folder(mut settings: Map<String, Value>) -> Map<String, Value> {
    if settings.get("folder").and_then(Value::as_str).is_some_and(|f| !f.is_empty() && !super::access::shown(f)) {
        settings.remove("folder");
    }
    settings
}

// ---------- export presets ----------

/// A saved batch export: what to write and how to name and sort it, to use again
/// (`document.exportPresets.*`, `exportPreset`). Kept with the preferences.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ScreenExportPreset {
    pub name: String,
    /// The [`PRESET_KEYS`] of `document.exportForScreens` params.
    pub settings: Map<String, Value>,
}

/// What a preset keeps: the formats, their settings, the naming and the folders; not which art
/// (it changes with the document) nor where to (unless given).
pub const PRESET_KEYS: [&str; 14] = [
    "formats",
    "settings",
    "preset",
    "name",
    "prefix",
    "counterStart",
    "counterPer",
    "folders",
    "subfolders",
    "includeBleed",
    "includeArtboardFill",
    "antiAlias",
    "folder",
    "fullDocument",
];

/// The built-in export presets: (name, settings).
pub fn builtin_presets() -> Vec<ScreenExportPreset> {
    let marks = json!({"trim": true, "registration": true, "colorBars": true, "pageInfo": true});
    let preset = |name: &str, settings: Value| ScreenExportPreset { name: name.into(), settings: settings.as_object().cloned().unwrap_or_default() };
    vec![
        preset(
            "Logo Package",
            json!({
                "name": "<filename>-<name>",
                "folders": "format",
                "formats": [
                    {"format": "svg"},
                    {"format": "png", "scale": "1x", "suffix": ""},
                    {"format": "png", "scale": "2x", "suffix": "@2x"},
                    {"format": "pdf"},
                    {"format": "pdf", "suffix": "-print", "marks": marks},
                ],
            }),
        ),
        preset("Print-Ready PDF", json!({"formats": [{"format": "pdf", "suffix": "-print", "marks": marks, "bleed": {"useDocument": true}}]})),
    ]
}

/// The preset called `name`: a saved one, else a built-in one.
fn find_preset(s: &Session, name: &str) -> Option<ScreenExportPreset> {
    s.prefs.screen_export_presets.iter().find(|p| p.name == name).cloned().or_else(|| builtin_presets().into_iter().find(|p| p.name == name))
}

/// `p` over the settings of its `exportPreset`, when it names one (its own keys win).
pub(super) fn with_preset(s: &Session, p: &Value) -> Result<Value> {
    let Some(name) = str_param(p, "exportPreset") else { return Ok(p.clone()) };
    let preset = find_preset(s, name).ok_or_else(|| bad(C, format!("no export preset `{name}` (see document.exportPresets.list)")))?;
    let mut merged = preset.settings;
    if let Some(o) = p.as_object() {
        merged.extend(o.iter().filter(|(k, _)| k.as_str() != "exportPreset").map(|(k, v)| (k.clone(), v.clone())));
    }
    Ok(Value::Object(merged))
}

/// The rows of `document.exportPresets.list`: the built-in presets, then `saved`, each
/// `{name, builtIn, settings}`.
pub fn preset_list(saved: &[ScreenExportPreset]) -> Value {
    let row = |p: &ScreenExportPreset, built_in: bool| json!({"name": p.name, "builtIn": built_in, "settings": p.settings});
    Value::Array(builtin_presets().iter().map(|p| row(p, true)).chain(saved.iter().map(|p| row(p, false))).collect())
}

pub(super) fn presets_list(s: &mut Session, _: &Value) -> Result<Value> {
    let saved: Vec<ScreenExportPreset> = s
        .prefs
        .screen_export_presets
        .iter()
        .map(|p| ScreenExportPreset { name: p.name.clone(), settings: leave_out_unreadable_settings_folder(p.settings.clone()) })
        .collect();
    Ok(json!({ "presets": preset_list(&saved) }))
}

/// The [`PRESET_KEYS`] of `given`, refused as an error of command `cmd` when an export would
/// refuse them.
fn preset_settings(s: &Session, cmd: &str, given: &Map<String, Value>) -> Result<Map<String, Value>> {
    let settings: Map<String, Value> = given.iter().filter(|(k, _)| PRESET_KEYS.contains(&k.as_str())).map(|(k, v)| (k.clone(), v.clone())).collect();
    // Refuse what an export would refuse, now rather than when the preset is used.
    check_settings(s, &Value::Object(settings.clone())).map_err(|e| bad(cmd, e.to_string()))?;
    Folders::of(&ScreenParams::of(&Value::Object(settings.clone()))?).map_err(|e| bad(cmd, e.to_string()))?;
    let mut settings = settings;
    remove_passwords(&mut settings);
    Ok(settings)
}

/// The longest export preset name kept (characters), as for asset names (`assets::MAX_NAME`).
const MAX_PRESET_NAME: usize = 255;

/// `name` trimmed and at most [`MAX_PRESET_NAME`] characters long (`None` when empty), as
/// `assets::clean_name` keeps an asset name.
fn preset_name(name: &str) -> Option<String> {
    let n: String = name.trim().chars().take(MAX_PRESET_NAME).collect();
    (!n.is_empty()).then_some(n)
}

pub(super) fn presets_save(s: &mut Session, p: &Value) -> Result<Value> {
    const SAVE: &str = "document.exportPresets.save";
    let name = str_param(p, "name").and_then(preset_name).ok_or_else(|| bad(SAVE, "give the preset a name"))?;
    if builtin_presets().iter().any(|b| b.name == name) {
        return Err(bad(SAVE, format!("`{name}` is a built-in preset: save under another name")));
    }
    let given = p.get("settings").and_then(Value::as_object).ok_or_else(|| bad(SAVE, "settings is an object of document.exportForScreens params"))?;
    let settings = preset_settings(s, SAVE, given)?;
    let preset = ScreenExportPreset { name: name.clone(), settings };
    let created = match s.prefs.screen_export_presets.iter_mut().find(|q| q.name == name) {
        Some(slot) => {
            *slot = preset.clone();
            false
        }
        None => {
            s.prefs.screen_export_presets.push(preset.clone());
            true
        }
    };
    Ok(json!({ "name": name, "created": created, "settings": preset.settings }))
}

pub(super) fn presets_delete(s: &mut Session, p: &Value) -> Result<Value> {
    const DELETE: &str = "document.exportPresets.delete";
    let name = str_param(p, "name").ok_or_else(|| bad(DELETE, "missing `name`"))?;
    if builtin_presets().iter().any(|b| b.name == name) {
        return Err(bad(DELETE, format!("`{name}` is built in and stays")));
    }
    let before = s.prefs.screen_export_presets.len();
    s.prefs.screen_export_presets.retain(|q| q.name != name);
    if s.prefs.screen_export_presets.len() == before {
        return Err(bad(DELETE, format!("no saved export preset `{name}`")));
    }
    Ok(json!({ "deleted": name }))
}

/// What `document.exportPresets.export` writes and `document.exportPresets.import` reads.
#[derive(Serialize, Deserialize)]
struct PresetFile {
    format: String,
    #[serde(default)]
    presets: Vec<ScreenExportPreset>,
}

/// The `format` of an export presets file, also its extension.
pub const PRESET_FILE_FORMAT: &str = "vcexportpresets";

/// The extensions of export presets files (opening one imports it).
pub const PRESET_FILE_EXTS: &[&str] = &[PRESET_FILE_FORMAT];

/// The most presets one file imports.
const MAX_IMPORT: usize = 1000;

/// The most bytes in an export presets file.
const MAX_PRESET_FILE_BYTES: usize = 4 << 20;

pub(super) fn presets_export(s: &mut Session, p: &Value) -> Result<Value> {
    const EXPORT: &str = "document.exportPresets.export";
    // A file holds no more presets than an import reads.
    let too_many = || bad(EXPORT, format!("more than {MAX_IMPORT} presets"));
    let presets = match p.get("names") {
        Some(Value::Array(names)) if names.len() > MAX_IMPORT => return Err(too_many()),
        Some(Value::Array(names)) => {
            let find = |n: &Value| {
                let n = n.as_str().ok_or_else(|| bad(EXPORT, "`names` lists preset names"))?;
                find_preset(s, n).ok_or_else(|| bad(EXPORT, format!("no export preset `{n}` (see document.exportPresets.list)")))
            };
            names.iter().map(find).collect::<Result<Vec<_>>>()?
        }
        None | Some(Value::Null) => s.prefs.screen_export_presets.clone(),
        Some(_) => return Err(bad(EXPORT, "`names` is a list of preset names")),
    };
    if presets.is_empty() {
        return Err(bad(EXPORT, "no saved export presets to export (name built-in ones in `names`)"));
    }
    if presets.len() > MAX_IMPORT {
        return Err(too_many());
    }
    let count = presets.len();
    let presets: Vec<ScreenExportPreset> =
        presets.into_iter().map(|p| ScreenExportPreset { settings: leave_out_unreadable_settings_folder(p.settings), ..p }).collect();
    let text = serde_json::to_string_pretty(&PresetFile { format: PRESET_FILE_FORMAT.into(), presets }).map_err(|e| bad(EXPORT, e.to_string()))?;
    if text.len() > MAX_PRESET_FILE_BYTES {
        return Err(bad(EXPORT, format!("export presets file is larger than {MAX_PRESET_FILE_BYTES} bytes")));
    }
    match str_param(p, "path") {
        Some(path) => {
            write_file(path, text.as_bytes())?;
            Ok(json!({"path": path, "count": count}))
        }
        None => Ok(json!({"data": text, "count": count})),
    }
}

pub(super) fn presets_import(s: &mut Session, p: &Value) -> Result<Value> {
    const IMPORT: &str = "document.exportPresets.import";
    let bytes = match (str_param(p, "path"), str_param(p, "data"), str_param(p, "dataBase64")) {
        (Some(path), ..) => super::read_file_head(path, MAX_PRESET_FILE_BYTES + 1)?,
        (None, Some(text), _) => {
            if text.len() > MAX_PRESET_FILE_BYTES {
                return Err(bad(IMPORT, format!("export presets file is larger than {MAX_PRESET_FILE_BYTES} bytes")));
            }
            text.as_bytes().to_vec()
        }
        (None, None, Some(b64)) => {
            if b64.bytes().filter(|b| !b.is_ascii_whitespace()).count() > MAX_PRESET_FILE_BYTES.div_ceil(3) * 4 {
                return Err(bad(IMPORT, format!("export presets file is larger than {MAX_PRESET_FILE_BYTES} bytes")));
            }
            vectorcraft_format::base64_decode(b64).ok_or_else(|| bad(IMPORT, "bad dataBase64"))?
        }
        _ => return Err(bad(IMPORT, "give `path`, `data` or `dataBase64`")),
    };
    if bytes.len() > MAX_PRESET_FILE_BYTES {
        return Err(bad(IMPORT, format!("export presets file is larger than {MAX_PRESET_FILE_BYTES} bytes")));
    }
    let file: PresetFile = serde_json::from_slice(&bytes).map_err(|e| bad(IMPORT, format!("not an export presets file: {e}")))?;
    if file.format != PRESET_FILE_FORMAT {
        return Err(bad(IMPORT, format!("not an export presets file (format `{}`)", file.format)));
    }
    if file.presets.len() > MAX_IMPORT {
        return Err(bad(IMPORT, format!("more than {MAX_IMPORT} presets")));
    }
    // Settings a hand-edited file may carry are refused as document.exportPresets.save refuses
    // them, before any preset is added.
    let mut presets = vec![];
    for q in file.presets {
        let settings = preset_settings(s, IMPORT, &q.settings).map_err(|e| match e {
            EngineError::BadParams { msg, .. } => bad(IMPORT, format!("`{}`: {msg}", q.name)),
            e => e,
        })?;
        let name = preset_name(&q.name).unwrap_or_else(|| "Export Preset".to_string());
        presets.push((name, settings));
    }
    let replace = bool_or(p, "replace", false);
    let saved = &mut s.prefs.screen_export_presets;
    let saved_before = saved.len();
    // A built-in name, or a saved one that stays, gets a number.
    let mut taken: std::collections::HashSet<String> =
        builtin_presets().into_iter().map(|b| b.name).chain(saved.iter().map(|q| q.name.clone())).collect();
    let mut imported = vec![];
    for (base, settings) in presets {
        if replace && let Some(slot) = saved.iter_mut().take(saved_before).find(|q| q.name == base) {
            slot.settings = settings;
            imported.push(base);
            continue;
        }
        let mut name = super::import_name(&base, |n| taken.contains(n));
        if name.chars().count() > MAX_PRESET_NAME {
            // A number after a long name stays within the limit: the name is cut to leave room for
            // a space and the longest number.
            let short: String = base.chars().take(MAX_PRESET_NAME - 21).collect();
            let short = short.trim_end();
            name = super::import_name(short, |n| n == short || taken.contains(n));
        }
        taken.insert(name.clone());
        saved.push(ScreenExportPreset { name: name.clone(), settings });
        imported.push(name);
    }
    Ok(json!({ "imported": imported }))
}

/// `settings` (a document's export settings) with export preset `name` applied as the Asset
/// Export panel applies one: its formats (or mobile/density `preset`), its format settings over
/// the ones there, its prefix (none when it has none), its folders (`folders`, or `subfolders`)
/// with Create Sub-folders on for a folder per row, and its anti-aliasing.
pub(crate) fn with_export_preset(s: &Session, cmd: &str, settings: &mut Map<String, Value>, name: &str) -> Result<()> {
    let preset = find_preset(s, name).ok_or_else(|| bad(cmd, format!("no export preset `{name}` (see document.exportPresets.list)")))?.settings;
    if let Some(formats) = preset.get("formats") {
        settings.insert("formats".into(), formats.clone());
        settings.remove("preset");
    } else if let Some(p) = preset.get("preset") {
        settings.insert("preset".into(), p.clone());
        settings.remove("formats");
    }
    if let Some(given) = preset.get("settings").and_then(Value::as_object) {
        let all = settings.entry("settings").or_insert_with(|| json!({}));
        if !all.is_object() {
            *all = json!({});
        }
        if let Some(all) = all.as_object_mut() {
            for (id, o) in given {
                match (all.get_mut(id).and_then(Value::as_object_mut), o.as_object()) {
                    (Some(mine), Some(o)) => mine.extend(o.iter().map(|(k, v)| (k.clone(), v.clone()))),
                    _ => _ = all.insert(id.clone(), o.clone()),
                }
            }
        }
    }
    match preset.get("prefix") {
        Some(prefix) => _ = settings.insert("prefix".into(), prefix.clone()),
        None => _ = settings.remove("prefix"),
    }
    // Its `folders` (Export for Screens shows it; empty is none, as an export reads it), else its
    // `subfolders` (a folder per row or none). Create Sub-folders is on for a folder per row, as
    // the dialog sets it.
    let folders = preset
        .get("folders")
        .and_then(Value::as_str)
        .map(|f| if f.is_empty() { "none" } else { f })
        .or_else(|| preset.get("subfolders").and_then(Value::as_bool).map(|b| if b { "rows" } else { "none" }));
    if let Some(f) = folders {
        settings.insert("folders".into(), json!(f));
        settings.insert("subfolders".into(), json!(f == "rows"));
    }
    if let Some(aa) = preset.get("antiAlias") {
        settings.insert("antiAlias".into(), aa.clone());
    }
    Ok(())
}

/// Does the file of `item` that row `r` writes carry the native document, and with it the
/// JavaScript files the document links ([`super::access::names_linked_scripts`])? A PDF holds the
/// artboards it is written with: those of `single` (Export PDFs As › Single file), every artboard
/// (Full Document), else the item's artboard of its document.
fn names_scripts(item: &Item, r: &Row, single: Option<&[usize]>) -> bool {
    if r.format.id != "pdf" {
        return super::access::names_linked_scripts(C, r.format.id, &r.options, item.doc.artboards.len());
    }
    let (doc, options) = match (single, &item.every_artboard) {
        (Some(boards), _) => {
            let mut options = r.options.clone();
            options["range"] = json!(boards.iter().map(|b| (b + 1).to_string()).collect::<Vec<_>>().join(", "));
            (&item.doc, options)
        }
        (None, Some(all)) => (all, r.options.clone()),
        (None, None) => match r.options_for(item.region, item.artboard) {
            Ok(options) => (&item.doc, options),
            Err(_) => return true,
        },
    };
    super::access::names_linked_scripts(C, "pdf", &options, doc.artboards.len())
}

/// The document the file of `item` that row `r` writes carries when it carries one
/// ([`names_scripts`]): Full Document's PDF of every artboard, the item's artboard alone for a
/// native, `.ai` or EPS row ([`board_document`]), else the item's document.
fn carried_document<'a>(item: &'a Item, r: &Row, single: Option<&[usize]>) -> std::borrow::Cow<'a, Document> {
    match (&item.every_artboard, r.format.id, single) {
        (Some(all), "pdf", None) => std::borrow::Cow::Borrowed(&**all),
        _ if r.whole_document() && item.artboard.is_some() => std::borrow::Cow::Owned(board_document(item)),
        _ => std::borrow::Cow::Borrowed(&*item.doc),
    }
}

/// The document a native, `.ai` or EPS row writes for `item`'s artboard: that artboard alone, with
/// its background color and the art on it, as Save's separateArtboards writes it (#3868).
fn board_document(item: &Item) -> Document {
    let mut d = single_artboard(&item.doc, item.region, &item.name);
    if let (Some(a), Some(from)) = (d.artboards.first_mut(), item.doc.artboards.get(item.artboard.unwrap_or(0))) {
        a.color = from.color;
    }
    super::save::keep_art_on(&mut d, item.region);
    d
}

/// Every chosen artboard (asset, layer, selected object) in every format row, one file each (a PDF
/// holds its artboard alone), or with `fullDocument` one file per row (a PDF of every artboard,
/// else the bounds of all art). Items that share a name get `-2`, `-3`… instead of overwriting, and
/// so do different files that would share a name, with or without the `name` pattern. The reply's
/// `warnings` lists every warning the encoders give for the files, each once, what the files lose
/// first ([`super::note_texts`]), and a note for each option of the request `asked` (`p` before a
/// preset or the remembered settings were laid under it) that the files the rows write don't read
/// ([`unused_option_notes`]).
pub(crate) fn export(s: &mut Session, p: &Value, asked: &Value) -> Result<Value> {
    check_passwords(p)?;
    let unused = unused_option_notes(asked, p);
    let o = ScreenParams::of(p)?;
    let rows = rows_of(s, p, o.preset.as_deref())?;
    o.one_source()?;
    // `<filename>`: the document's file, as Save names it, or its title before it is saved.
    let st = s.doc()?;
    let naming = Naming::of(&o, st.path.as_deref().unwrap_or(&st.doc.title))?;
    let selection = o.selection_mode()?;
    let selected = if selection.is_some() { selected_artwork(s)? } else { vec![] };
    let original = s.doc()?.doc.clone();
    // Include Bleed: each artboard grown by the document's bleed.
    let grown = o.include_bleed && original.setup.has_bleed();
    let doc = if grown {
        let mut d = (*original).clone();
        for a in &mut d.artboards {
            a.rect = original.setup.bleed_rect(a.rect);
        }
        Arc::new(d)
    } else {
        original.clone()
    };
    // A PDF page of an artboard keeps the artboard as its TrimBox and puts its row's bleed around
    // it once: it is written from the document with the artboards as they are. Assets, layers and
    // a selection are written on a page of their own bounds, which Include Bleed doesn't grow.
    let ungrown = (grown && o.assets.is_none() && o.layers.is_none() && o.selection.is_none()).then_some(original);
    let crop = rows.iter().any(|r| r.format.id != "pdf");
    let items = items(&doc, &o, selection, &selected, &naming.filename, crop, true)?;
    // Before the folder is made or any row is written, with the artboards each file holds.
    let single: Option<Vec<usize>> = items.iter().map(|i| i.artboard).collect::<Option<Vec<_>>>().filter(|a| naming.single_pdf && a.len() > 1);
    let st = s.doc()?;
    super::check_scripts_of(C, &st.doc, st.path.as_deref(), || rows.iter().any(|r| items.iter().any(|i| names_scripts(i, r, single.as_deref()))))?;
    // The file each item writes for each SVG row, before the folder is made or any row is written.
    let base = crate::cmd::links::LinkBase::of(s.doc()?);
    for r in rows.iter().filter(|r| matches!(r.format.id, "svg" | "svgz")) {
        for item in &items {
            super::check_images_in(C, r.format.id, &item.doc, base, &r.options_for(item.region, item.artboard)?)?;
        }
    }
    // The native document the files of the other rows carry, each document looked through once.
    if crate::file_access::current().is_some() {
        let mut looked: HashSet<(usize, bool, Option<usize>)> = HashSet::new();
        for r in rows.iter().filter(|r| !matches!(r.format.id, "svg" | "svgz")) {
            // A single-file PDF carries the first item's document alone, as the writer writes it.
            let carried = items.iter().enumerate().filter(|(index, _)| !(single.is_some() && r.format.id == "pdf" && *index > 0));
            for (_, item) in carried.filter(|(_, i)| names_scripts(i, r, single.as_deref())) {
                let all = item.every_artboard.is_some() && r.format.id == "pdf" && single.is_none();
                let board = item.artboard.filter(|_| !all && r.whole_document());
                if looked.insert((Arc::as_ptr(&item.doc) as usize, all, board))
                    && crate::cmd::links::names_hidden_file(&carried_document(item, r, single.as_deref()), base)
                {
                    return Err(bad(C, super::access::IMAGE_OUTSIDE));
                }
            }
        }
    }
    // Appearance of Black › Printing / Exporting for RGB raster rows.
    // Include Artboard Fill off: the encoders leave the background colors out.
    let fill = if o.include_artboard_fill == Some(false) { json!({ "includeArtboardFill": false }) } else { json!({}) };
    let rows = rows
        .into_iter()
        .map(|mut r| {
            // Include Bleed: a PDF row that sets no bleed of its own, and whose preset gives none,
            // puts the document's bleed around the artboard, and is the same file as a row that
            // asks for the document's bleed. A saved preset's bleed values stay, as they do in
            // that row.
            if ungrown.is_some() && r.format.id == "pdf" && !r.bleed_given && !pdf_bleeds(&r.options) {
                match r.options.get_mut("bleed").and_then(Value::as_object_mut) {
                    Some(b) => {
                        b.insert("useDocument".into(), json!(true));
                    }
                    None => r.options["bleed"] = json!({ "useDocument": true }),
                }
                r.key = r.file_key();
            }
            Row { options: merge(with_black_output(s, &r.options), fill.clone()), ..r }
        })
        .collect();
    let export = ScreenExport { cmd: C, o, rows, naming, items, unused, hidden: None, ungrown };
    match &mut s.screens_hand_off {
        Some(slot) => {
            *slot = Some(export);
            Ok(Value::Null)
        }
        None => export.write(&mut |path, bytes| write_file(path, bytes)),
    }
}

/// Do the PDF `options` (a built-in preset's settings under them) give the pages a bleed: the
/// document's, or a value above 0 on some side? Options the PDF encoder refuses count as giving
/// one, so they are left as they are for the encoder to refuse.
fn pdf_bleeds(options: &Value) -> bool {
    super::pdf::settings(C, options)
        .map_or(true, |s| s.bleed.use_document || [s.bleed.top, s.bleed.bottom, s.bleed.left, s.bleed.right].iter().any(|b| *b > 0.0))
}

/// An export as `document.exportForScreens` and `assets.export` take it from the session: the
/// params, the format rows, the naming, and each item with the document it writes
/// ([`Session::prepare_screen_export`]). [`ScreenExport::write`] encodes and writes its files
/// without the session, on any thread.
pub struct ScreenExport {
    /// The command whose errors it gives: `document.exportForScreens` or `assets.export`.
    cmd: &'static str,
    o: ScreenParams,
    rows: Vec<Row>,
    naming: Naming,
    items: Vec<Item>,
    /// The notes on options the files don't read ([`unused_option_notes`]).
    unused: Vec<String>,
    /// The export folder and the name the request that prepared the export may see it by, when the
    /// folder came from the `exportPreset` and is outside that request's read roots: the reply and
    /// the errors of [`ScreenExport::write`] name it as [`export_for_screens`] does.
    hidden: Option<(String, String)>,
    /// Include Bleed with artboards: the document with the artboards as they are, which PDF rows
    /// write (the items hold the artboards grown by the bleed).
    ungrown: Option<Arc<Document>>,
}

impl Session {
    /// The folder `document.exportForScreens` (`id`) with `params` writes into, the client's
    /// `folder` or else the one its `exportPreset` keeps, with the name the request being served may
    /// see it by when the reply leaves it out: a preset's folder outside the read roots, named by
    /// its own name ([`super::access::shown_path`]). `assets.export` takes no preset, so for it the
    /// folder is the client's. → `None` without a folder (the files come back in the reply) or for
    /// an unknown preset, which the export itself refuses.
    pub fn screen_export_folder(&self, id: &str, params: &Value) -> Option<(String, Option<String>)> {
        let given = params.get("folder").is_some();
        let p = if given || id == ASSETS { params.clone() } else { with_preset(self, params).ok()? };
        let folder = str_param(&p, "folder").filter(|f| !f.is_empty())?.to_string();
        let hidden = (!given && !super::access::shown(&folder)).then(|| super::access::shown_path(&folder).into_owned());
        Some((folder, hidden))
    }

    /// Run `assets.export` when `id` names it, else `document.exportForScreens`, with `params` as
    /// a command (checked and journaled as any other; Export for Screens remembers its settings)
    /// that encodes and writes nothing → the export it prepared, which [`ScreenExport::write`]
    /// encodes and writes (on the desktop with Background Export on, Export for Screens and the
    /// Asset Export panel do this on the background thread).
    pub fn prepare_screen_export(&mut self, id: &str, params: &Value) -> Result<ScreenExport> {
        let cmd = if id == ASSETS { ASSETS } else { C };
        self.screens_hand_off = Some(None);
        let r = self.execute(cmd, params);
        let handed = self.screens_hand_off.take().flatten();
        match (r, handed) {
            (Err(e), _) => Err(e),
            (Ok(_), Some(export)) => Ok(ScreenExport { cmd, ..export }),
            // Every run that succeeds hands one over.
            (Ok(_), None) => Err(bad(cmd, "nothing to export")),
        }
    }
}

impl ScreenExport {
    /// Make the export folder, encode the files and write each with `writer` into the folder and
    /// its sub-folders, or keep them to zip (the zip written with `writer` into the folder) or to
    /// return → the reply of `document.exportForScreens`. It reports one progress step per file
    /// ([`vectorcraft_doc::cancel::progress`]), and a cancel stops it before it encodes or writes
    /// another file.
    pub fn write(mut self, writer: &mut dyn FnMut(&str, &[u8]) -> Result<()>) -> Result<Value> {
        let cmd = self.cmd;
        let hidden = self.hidden.take();
        // A refusal names `assets.export` when that command prepared the export, as its reply does.
        let r = encode_and_write(self, writer).map_err(|e| if cmd == C { e } else { crate::cmd::assets::of_cmd(cmd, e) });
        match hidden {
            Some((folder, name)) => {
                let mut r = r.map_err(|e| super::access::naming_as(e, &folder, &name))?;
                leave_out_folder(&mut r, &folder);
                Ok(r)
            }
            None => r,
        }
    }
}

/// [`ScreenExport::write`].
fn encode_and_write(export: ScreenExport, writer: &mut dyn FnMut(&str, &[u8]) -> Result<()>) -> Result<Value> {
    let ScreenExport { o, rows, naming, items, unused, ungrown, .. } = export;
    let folder = o.folder.as_deref().filter(|f| !f.is_empty());
    if let Some(dir) = folder {
        create_dir(dir)?;
    }
    let mut out = Output::default();
    // The notes on the options, then the encoders' notes for every file, each once.
    let mut notes: Vec<Note> = Vec::new();
    for text in unused {
        add_note(&mut notes, Note::setting(text));
    }
    // The document a PDF row writes: with Include Bleed, the artboards as they are.
    let pdf_doc = ungrown.as_deref();
    // A PDF page of an artboard has the artboard as its trim, with or without Include Bleed. A
    // page of its art's bounds has every object at its edge: it doesn't say whether art stops
    // short of the bleed.
    let artboard_page = o.assets.is_none() && o.layers.is_none() && o.selection.is_none();
    let mut write = |doc: &Document, format: &str, options: &Value| -> Result<Vec<u8>> {
        // The steps are the files: an encoder's own steps (artboards, pages) are not reported.
        let (bytes, given) = vectorcraft_doc::cancel::quiet(|| encode_with_notes(doc, format, options))?;
        for n in given.into_iter().filter(|n| artboard_page || !super::pdf::is_short_of_bleed(&n.text)) {
            add_note(&mut notes, n);
        }
        Ok(bytes)
    };
    // A whole-document copy of an artboard of an item's document, for the formats that write
    // documents to edit.
    let mut board_docs: std::collections::HashMap<(usize, usize), Arc<Document>> = std::collections::HashMap::new();
    let mut files = 0u64;
    // One step per item and row, the files a name collision skips included.
    let steps = (items.len() * rows.len()) as u64;
    let mut step = 0u64;
    // Export PDFs As › Single file: with several artboards, a PDF row writes them all as the pages
    // of one file, named as the document, with the first of them.
    let artboards: Option<Vec<usize>> = items.iter().map(|i| i.artboard).collect::<Option<Vec<_>>>().filter(|a| naming.single_pdf && a.len() > 1);
    for (index, item) in items.iter().enumerate() {
        for r in &rows {
            vectorcraft_doc::cancel::check()?;
            vectorcraft_doc::cancel::progress(step, steps, &format!("Exporting file {} of {steps}", step + 1));
            step += 1;
            let single = artboards.as_ref().filter(|_| r.format.id == "pdf");
            if single.is_some() && index > 0 {
                continue;
            }
            let document = single.map(|_| Item {
                name: naming.filename.clone(),
                stem: naming.filename.clone(),
                doc: item.doc.clone(),
                artboard: None,
                region: item.region,
                every_artboard: None,
            });
            let (sub, file) = naming.file(document.as_ref().unwrap_or(item), index, files, r);
            let name = sub.map_or_else(|| file.clone(), |d| format!("{d}/{file}"));
            let Some(name) = out.claim(&name, &format!("{}|{}", item.stem, r.key)) else { continue };
            let bytes = if let Some(boards) = single {
                let mut options = r.options.clone();
                options["range"] = json!(boards.iter().map(|b| (b + 1).to_string()).collect::<Vec<_>>().join(", "));
                write(pdf_doc.unwrap_or(&item.doc), "pdf", &options)?
            } else if let (Some(all), "pdf") = (&item.every_artboard, r.format.id) {
                // One page per artboard.
                let mut options = r.options.clone();
                options["range"] = json!("all");
                write(pdf_doc.unwrap_or(all), "pdf", &options)?
            } else if r.whole_document() && item.artboard.is_some() {
                let b = item.artboard.unwrap_or(0);
                let d = board_docs.entry((Arc::as_ptr(&item.doc) as usize, b)).or_insert_with(|| Arc::new(board_document(item))).clone();
                let mut options = r.options_for(item.region, None)?;
                // An EPS page is the artboard, not its art's bounds: an artboard without art still writes.
                if r.format.id == "eps" {
                    options["useArtboards"] = json!(true);
                }
                write(&d, r.format.id, &options)?
            } else {
                let doc = if r.format.id == "pdf" { pdf_doc.unwrap_or(&item.doc) } else { &item.doc };
                write(doc, r.format.id, &r.options_for(item.region, item.artboard)?)?
            };
            out.put(folder, o.zip, name, bytes, writer)?;
            files += 1;
        }
    }
    // A cancel that came while the last file was encoded returns no files. A folder export
    // without a ZIP has written every file by now, so it returns them.
    if o.zip || folder.is_none() {
        vectorcraft_doc::cancel::check()?;
    }
    // What the files lose first, across every file.
    let warnings = note_texts(&notes);
    let names: Vec<&str> = out.kept.iter().map(|(n, _)| n.as_str()).collect();
    if o.zip {
        let zip = super::zip::deflate(&out.kept, super::zip::FIXED_TIME, vectorcraft_doc::cancel::is_cancelled);
        // A cancel that came while the files were deflated returns and writes no archive.
        vectorcraft_doc::cancel::check()?;
        let zip = zip.map_err(|e| bad(C, e))?;
        let zip_name = safe_file_name(&format!("{}{}.zip", naming.prefix, naming.filename));
        return match folder {
            Some(dir) => {
                let path = format!("{dir}/{zip_name}");
                writer(&path, &zip)?;
                Ok(json!({ "path": path, "bytes": zip.len(), "files": names, "warnings": warnings }))
            }
            None => Ok(
                json!({ "name": zip_name, "dataBase64": vectorcraft_format::base64_encode(&zip), "bytes": zip.len(), "files": names, "warnings": warnings }),
            ),
        };
    }
    Ok(match folder {
        Some(_) => json!({ "files": out.written, "warnings": warnings }),
        None => {
            let files: Vec<Value> =
                out.kept.iter().map(|(name, bytes)| json!({ "name": name, "dataBase64": vectorcraft_format::base64_encode(bytes) })).collect();
            json!({ "files": files, "warnings": warnings })
        }
    })
}

/// The path, relative to the export folder, of the first file `document.exportForScreens` with
/// params `p` writes (sub-folders included: `2x/Logo@2x.png`): the first format row's file of the
/// first artboard, asset, layer or selected object, or of the whole document, named as [`export`]
/// names it. It encodes and writes nothing and cuts no art out of the document: of the objects
/// selected for a file each, it measures only the first ones, up to the first that draws something.
/// It does not refuse what the export refuses for drawing nothing: a checked asset or layer, the
/// selection as one file, or the Full Document with no visible art.
/// The Export for Screens dialog shows it on its Example line.
pub fn first_file(s: &Session, p: &Value) -> Result<String> {
    let p = with_preset(s, p)?;
    let o = ScreenParams::of(&p)?;
    let rows = rows_of(s, &p, o.preset.as_deref())?;
    o.one_source()?;
    let selection = o.selection_mode()?;
    let st = s.doc()?;
    let doc = &st.doc;
    let mut selected = if selection.is_some() { selected_artwork(s)? } else { vec![] };
    // A file per object: objects that draw nothing write no file.
    if selection == Some(true) {
        selected = selected.into_iter().find(|id| export_crop(doc, &[*id]).is_some()).into_iter().collect();
    }
    let naming = Naming::of(&o, st.path.as_deref().unwrap_or(&doc.title))?;
    let items = items(doc, &o, selection, &selected, &naming.filename, false, false)?;
    let (Some(item), Some(r)) = (items.first(), rows.first()) else { return Err(bad(C, "nothing to export")) };
    let (sub, file) = naming.file(item, 0, 0, r);
    Ok(sub.map_or_else(|| file.clone(), |d| format!("{d}/{file}")))
}

/// What `o` exports a file of per row ([`Item`]): the whole document, the assets, the layers, the
/// selection (one item, or one per object), else the chosen artboards.
/// `crop`: some row needs the Full Document's art bounds (a format other than PDF). `art`: each
/// asset, layer and selected object is cut out of the document; without it, only the items' names
/// are made, every item holds the whole document, and none is left out for drawing nothing.
fn items(
    doc: &Arc<Document>,
    o: &ScreenParams,
    selection: Option<bool>,
    selected: &[NodeId],
    filename: &str,
    crop: bool,
    art: bool,
) -> Result<Vec<Item>> {
    // The art of `ids` alone, as an item named `name`.
    let alone = |ids: &[NodeId], name: &str| -> Option<(Arc<Document>, Rect)> {
        if !art {
            return Some((doc.clone(), Rect::ZERO));
        }
        isolated(doc, &doc.paint_order(ids.iter().copied()), name).map(|(d, r)| (Arc::new(d), r))
    };
    // Each item's stem follows Windows' file-naming rules, and no two items share one, compared as
    // the item folders are (`fold`).
    let safe = |stems: Vec<String>| distinct_names_by(stems.iter().map(|s| safe_file_name(s)), fold);
    let named = |pieces: Vec<(String, Arc<Document>, Option<usize>, Rect)>, fallback: &str| -> Vec<Item> {
        let stems = safe(unique_file_names_keeping_dots(pieces.iter().map(|(n, ..)| (n.as_str(), fallback.to_string()))));
        pieces
            .into_iter()
            .zip(stems)
            .map(|((name, doc, artboard, region), stem)| Item { name, stem, doc, artboard, region, every_artboard: None })
            .collect()
    };
    if o.full_document {
        // The bounds of all visible art, as the one artboard of the files that aren't PDFs (a PDF
        // row writes every artboard of the document itself). It is named after the document: an
        // SVG of one artboard takes the artboard's name as its title (#3544), and this one stands
        // for the whole document.
        let (whole, region) = if crop {
            let bounds = vectorcraft_render::encode::art_bounds(doc).ok_or_else(|| bad(C, "nothing to export: the document has no visible art"))?;
            (Arc::new(single_artboard(doc, bounds, &doc.title)), bounds)
        } else {
            (doc.clone(), Rect::ZERO)
        };
        let name = filename.to_string();
        let stem = safe_file_name(&name);
        return Ok(vec![Item { name, stem, doc: whole, artboard: Some(0), region, every_artboard: Some(doc.clone()) }]);
    }
    if let Some(ids) = &o.assets {
        let mut pieces = vec![];
        for id in ids {
            let a = doc.asset(*id).ok_or_else(|| bad(C, format!("no asset {id} (see assets.list)")))?;
            if a.format_3d.is_some() {
                return Err(bad(C, format!("asset {id} is a 3D asset: assets.export3D writes it")));
            }
            let (d, r) = alone(&a.nodes, &a.name).ok_or_else(|| bad(C, format!("asset `{}` has no art to export", a.name)))?;
            pieces.push((a.name.clone(), d, Some(0), r));
        }
        if pieces.is_empty() {
            return Err(bad(C, "no assets to export"));
        }
        return Ok(named(pieces, "Asset"));
    }
    if let Some(ids) = &o.layers {
        let mut pieces = vec![];
        for id in ids {
            let layer = doc.node(NodeId(*id)).filter(|n| n.is_layer()).ok_or_else(|| bad(C, format!("no layer {id} (see layer.list)")))?;
            let mut art = vec![];
            crate::cmd::assets::art_under(doc, layer.id, &mut art);
            let name = layer.display_name();
            let (d, r) = alone(&art, &name).ok_or_else(|| bad(C, format!("layer `{name}` has no art to export")))?;
            pieces.push((name, d, Some(0), r));
        }
        if pieces.is_empty() {
            return Err(bad(C, "no layers to export"));
        }
        return Ok(named(pieces, "Layer"));
    }
    if let Some(each) = selection {
        if selected.is_empty() {
            return Err(bad(C, "select something to export"));
        }
        let mut pieces = vec![];
        if each {
            for id in selected {
                let name = doc.node(*id).map(|n| n.display_name()).filter(|n| !n.is_empty()).unwrap_or_else(|| "Selection".into());
                if let Some((d, r)) = alone(&[*id], &name) {
                    pieces.push((name, d, Some(0), r));
                }
            }
        } else if let Some((d, r)) = alone(selected, "Selection") {
            pieces.push(("Selection".to_string(), d, Some(0), r));
        }
        if pieces.is_empty() {
            return Err(bad(C, "the selection has no art to export"));
        }
        return Ok(named(pieces, "Selection"));
    }
    let n = doc.artboards.len();
    let boards = o.boards.resolve(n).map_err(|e| bad(C, e))?.unwrap_or_else(|| (0..n).collect());
    if boards.is_empty() {
        return Err(bad(C, "the document has no artboard"));
    }
    let stems = safe(artboard_file_names_keeping_dots(doc, &boards));
    Ok(boards
        .into_iter()
        .zip(stems)
        .map(|(b, stem)| {
            let a = doc.artboards.get(b);
            let name = a.map(|a| a.name.clone()).filter(|n| !n.is_empty()).unwrap_or_else(|| stem.clone());
            Item { name, stem, doc: doc.clone(), artboard: Some(b), region: a.map_or(Rect::ZERO, |a| a.rect), every_artboard: None }
        })
        .collect())
}

/// Keep the params of a successful export in the document (without `zip`, a delivery detail, and
/// `assets`, a choice of art), so the dialog reopens on them.
fn remember(s: &mut Session, p: &Value) {
    let Some(o) = p.as_object() else { return };
    let mut settings = o.clone();
    settings.remove("zip");
    settings.remove("assets");
    // Only after a successful export, which had a document.
    let _ = store_settings(s, settings);
}

/// The PDF passwords an export may be given (`security` of a PDF row or of `settings.pdf`).
const PASSWORD_KEYS: [&str; 2] = ["openPassword", "permissionsPassword"];

/// Left where [`remove_passwords`] took a password out: an export of these settings is refused
/// until a password is given again ([`check_passwords`]), rather than written unprotected.
const PASSWORD_REQUIRED: &str = "passwordRequired";

/// `settings` without the PDF passwords anywhere in them: settings kept in the document (which
/// native saves write) or in the preferences hold the export's choices, never its secrets, as
/// the PDF settings themselves are saved without them. Where one was given, [`PASSWORD_REQUIRED`]
/// says so.
fn remove_passwords(settings: &mut Map<String, Value>) {
    let mut removed = false;
    for key in PASSWORD_KEYS {
        removed |= settings.remove(key).is_some_and(|v| v.as_str().is_none_or(|p| !p.is_empty()));
    }
    if removed {
        settings.insert(PASSWORD_REQUIRED.into(), Value::Bool(true));
    }
    for v in settings.values_mut() {
        remove_passwords_in(v);
    }
}

/// Refuse export params `v` that hold settings whose PDF passwords were left out
/// ([`PASSWORD_REQUIRED`]) without a password given again.
fn check_passwords(v: &Value) -> Result<()> {
    match v {
        Value::Object(m) => {
            let given = PASSWORD_KEYS.iter().any(|k| m.get(*k).and_then(Value::as_str).is_some_and(|p| !p.is_empty()));
            if m.get(PASSWORD_REQUIRED) == Some(&Value::Bool(true)) && !given {
                return Err(bad(
                    C,
                    "the PDF security settings were saved without their passwords: give openPassword or permissionsPassword again, or security without passwordRequired to export unprotected",
                ));
            }
            m.values().try_for_each(check_passwords)
        }
        Value::Array(items) => items.iter().try_for_each(check_passwords),
        _ => Ok(()),
    }
}

fn remove_passwords_in(v: &mut Value) {
    match v {
        Value::Object(m) => remove_passwords(m),
        Value::Array(items) => items.iter_mut().for_each(remove_passwords_in),
        _ => {}
    }
}

/// Make `settings` the document's export settings. Not an undo step (like the settings of any
/// export); the document has changed only if they differ.
pub(crate) fn store_settings(s: &mut Session, mut settings: Map<String, Value>) -> Result<()> {
    remove_passwords(&mut settings);
    let st = s.doc_mut()?;
    if st.doc.export_settings != settings {
        Arc::make_mut(&mut st.doc).export_settings = settings;
        st.revision += 1;
    }
    Ok(())
}

/// `document.exportSettings`: the Export for Screens settings the document last exported with.
pub(super) fn export_settings(s: &mut Session, _: &Value) -> Result<Value> {
    Ok(json!({ "settings": leave_out_unreadable_settings_folder(s.doc()?.doc.export_settings.clone()) }))
}

#[cfg(test)]
mod tests {
    use super::super::access::naming_in_message;
    use super::{Output, ScreenSize, safe_file_name};

    /// A hidden export folder is named by its name where a message is about it, and a root that
    /// starts with the same text, or a longer path, stays whole.
    #[test]
    fn a_hidden_folder_is_named_by_its_name_in_a_message() {
        let f = "/work/out";
        let cases = [
            (
                "automation path rejected: outside the write root /work/out/in: /work/out",
                "automation path rejected: outside the write root /work/out/in: out",
            ),
            ("automation path rejected: cannot resolve /work/out: denied", "automation path rejected: cannot resolve out: denied"),
            ("/work/out/2x/a.png: disk full", "out/2x/a.png: disk full"),
            ("automation path rejected: outside the write root /a: /work/outer", "automation path rejected: outside the write root /a: /work/outer"),
        ];
        for (msg, want) in cases {
            assert_eq!(naming_in_message(msg, f, "out"), want, "{msg}");
        }
    }

    /// Files given one name are numbered in turn, each numbering going on from the last one (so
    /// thousands of them don't each try every number used before), and a file asking again for
    /// the name it got is told it has it.
    #[test]
    fn taken_names_are_numbered_from_the_last_number() {
        let mut out = Output::default();
        assert_eq!(out.claim("a.png", "1").as_deref(), Some("a.png"));
        assert_eq!(out.claim("A.png", "2").as_deref(), Some("A-2.png"));
        assert_eq!(out.claim("a-3.png", "x").as_deref(), Some("a-3.png"));
        assert_eq!(out.claim("a.png", "3").as_deref(), Some("a-4.png"));
        assert_eq!(out.claim("a.png", "2"), None);
        assert_eq!(out.claim("a.png", "1"), None);
        assert_eq!(out.claim("sub/a.png", "1").as_deref(), Some("sub/a.png"));
        for k in 0..20_000 {
            assert!(out.claim("same.png", &k.to_string()).is_some());
        }
        assert_eq!(out.claim("same.png", "last").as_deref(), Some("same-20001.png"));
    }

    #[test]
    fn size_parse_refuses_signs_and_exponents() {
        for s in ["+2x", "+2", "+100w", "+100h", "+72ppi", "-2x", "1e2", "2e0w"] {
            assert_eq!(ScreenSize::parse(s), None, "{s}");
        }
        assert_eq!(ScreenSize::parse("2x"), Some(ScreenSize::Scale(2.0)));
        assert_eq!(ScreenSize::parse("1.5"), Some(ScreenSize::Scale(1.5)));
        assert_eq!(ScreenSize::parse("100W"), Some(ScreenSize::Width(100.0)));
        assert_eq!(ScreenSize::parse("72ppi"), Some(ScreenSize::Ppi(72.0)));
    }

    #[test]
    fn device_name_with_space_before_extension_is_protected() {
        assert_eq!(safe_file_name("CON.png"), "CON-.png");
        assert_eq!(safe_file_name("CON .png"), "CON-.png");
        assert_eq!(safe_file_name("NUL .png"), "NUL-.png");
        assert_eq!(safe_file_name("COM1 .png"), "COM1-.png");
        assert_eq!(safe_file_name("lpt9  .png"), "lpt9-.png");
        assert_eq!(safe_file_name("My CON .png"), "My CON .png");
    }

    /// The settings an export leaves in the document keep its PDF security choices but not its
    /// passwords, in a row or in `settings.pdf`.
    #[test]
    fn stored_settings_leave_out_pdf_passwords() {
        let mut s = crate::Session::new();
        s.execute("file.new", &serde_json::json!({"width": 100, "height": 100})).unwrap();
        let given = serde_json::json!({
            "formats": [{"format": "pdf", "security": {"openPassword": "open sesame", "permissionsPassword": "owner", "printing": "low"}}],
            "settings": {"pdf": {"security": {"openPassword": "open sesame", "copy": false}}},
        });
        super::store_settings(&mut s, given.as_object().unwrap().clone()).unwrap();
        let kept = serde_json::Value::Object(s.doc().unwrap().doc.export_settings.clone());
        assert!(!kept.to_string().contains("Password"), "{kept}");
        assert_eq!(kept["formats"][0]["security"]["printing"], "low");
        assert_eq!(kept["settings"]["pdf"]["security"]["copy"], false);
        assert_eq!(kept["formats"][0]["security"]["passwordRequired"], true);
    }
}