GitCorner

Code

ossartcraftphotocraft🌐
856f865cf98d
//! Copy layers into another open document (#589): what Photoshop does when layers are dragged from
//! the Layers panel, or with the Move tool from the canvas, onto another document's tab or window.
//! Photoshop has no menu item for it; `layer.copyToDocument` is the command the drag dispatches and
//! agents call.
//!
//! The copies keep their names, content, masks, styles and blending; they land above the
//! destination's active layer as one history step there, and the destination becomes the active
//! document. Pixels are converted to the destination's colour profile (Color Settings' intent and
//! black point compensation) and bit depth, as Photoshop converts dragged layers.

use std::collections::{BTreeSet, HashMap, HashSet};

use photocraft_color::ColorMode;
use photocraft_doc::text_styles::{CharacterStyleDef, ParagraphStyleDef};
use photocraft_doc::{Document, Layer, LayerContent, LayerId, SmartContentsId, TextStyles};
use photocraft_geom::Rect;
use serde_json::{Value, json};

use crate::commands::CommandSpec;
use crate::{EngineError, Result, Session};

const CMD: &str = "layer.copyToDocument";

fn bad(msg: impl Into<String>) -> EngineError {
    EngineError::BadParams { cmd: CMD.into(), msg: msg.into() }
}

fn enabled(s: &Session) -> std::result::Result<(), String> {
    let d = s.active().ok_or("no document open")?;
    d.active_layer.ok_or("no active layer")?;
    if s.documents().len() < 2 { Err("no other document is open".into()) } else { Ok(()) }
}

/// A document index param (`None` when absent), checked against the open documents.
fn doc_index(s: &Session, p: &Value, key: &str) -> Result<Option<usize>> {
    let Some(v) = p.get(key) else { return Ok(None) };
    let i = v.as_u64().and_then(|i| usize::try_from(i).ok()).ok_or_else(|| bad(format!("`{key}` must be a document index")))?;
    if i >= s.documents().len() {
        return Err(bad(format!("no document {i} (there are {})", s.documents().len())));
    }
    Ok(Some(i))
}

/// An `[x, y]` param of finite numbers within ±10⁷ pixels.
fn point(p: &Value, key: &str) -> Result<Option<[f64; 2]>> {
    let Some(v) = p.get(key) else { return Ok(None) };
    let xy = v.as_array().filter(|a| a.len() == 2).and_then(|a| Some([a[0].as_f64()?, a[1].as_f64()?]));
    match xy {
        Some(xy) if xy.iter().all(|c| c.is_finite() && c.abs() <= 1e7) => Ok(Some(xy)),
        _ => Err(bad(format!("`{key}` must be [x, y] (pixels)"))),
    }
}

/// Modes whose layers convert through the colour engine.
pub(crate) fn layered(mode: ColorMode) -> bool {
    matches!(mode, ColorMode::Rgb | ColorMode::Grayscale | ColorMode::Cmyk | ColorMode::Lab)
}

fn center(r: Rect) -> [f64; 2] {
    [(f64::from(r.x0) + f64::from(r.x1)) / 2.0, (f64::from(r.y0) + f64::from(r.y1)) / 2.0]
}

/// Restore missing shape caches, or refresh every shape for a changed canvas, including groups.
fn prepare_shapes(layers: &mut [Layer], doc: &Document, new_canvas: bool) {
    for layer in layers {
        match &mut layer.content {
            LayerContent::Shape(shape) if new_canvas || shape.cache.is_none() => crate::vector_cmds::refresh_shape(doc, shape),
            LayerContent::Group(group) => prepare_shapes(&mut group.children, doc, new_canvas),
            _ => {}
        }
    }
}

/// Readies `copies` (a scratch document of the source's colour holding duplicated layers) for
/// landing in `dest`: smart objects get fresh contents ids, keeping only the sharing within the
/// batch (contents ids belong to a document, so the copies never join objects already in `dest`,
/// including another opening of the same file), and the pixels convert to `dest`'s profile and
/// bit depth.
pub(crate) fn adapt_copies(s: &Session, copies: &mut Document, dest: &Document) -> Result<()> {
    // Link-group IDs are scoped to a document. Two independently linked pairs can both
    // use group 1; copying one must not link it to the other's existing layers (#3045).
    // Remap each imported group once so links within the copied batch stay intact.
    let mut used: HashSet<u64> = dest.walk().into_iter().filter_map(|(_, _, l)| l.link_group).collect();
    let mut next_group = dest.unused_link_group().ok_or_else(|| EngineError::Other("no free linked-layer group id".into()))?;
    let groups: Vec<_> = copies.walk().into_iter().filter_map(|(_, _, l)| l.link_group.map(|group| (l.id, group))).collect();
    let mut remapped = HashMap::new();
    for (_, group) in &groups {
        if remapped.contains_key(group) {
            continue;
        }
        while used.contains(&next_group) {
            next_group = next_group.checked_add(1).unwrap_or(1);
        }
        remapped.insert(*group, next_group);
        used.insert(next_group);
        next_group = next_group.checked_add(1).unwrap_or(1);
    }
    for (id, group) in groups {
        if let Some(layer) = copies.layer_mut(id) {
            layer.link_group = remapped.get(&group).copied();
        }
    }
    let mut contents = HashMap::new();
    let smart_ids: Vec<_> = copies.walk().into_iter().filter_map(|(_, _, l)| matches!(l.content, LayerContent::Smart(_)).then_some(l.id)).collect();
    for id in smart_ids {
        if let Some(l) = copies.layer_mut(id)
            && let LayerContent::Smart(sm) = &mut l.content
        {
            sm.contents_id = *contents.entry(sm.contents_id).or_insert_with(SmartContentsId::fresh);
        }
    }
    if (copies.mode, &copies.icc_profile) != (dest.mode, &dest.icc_profile) {
        let profile = crate::color_cmds::document_profile(dest);
        crate::color_cmds::convert_document(copies, &profile, s.color.settings.intent(), s.color.settings.bpc)?;
    }
    if copies.depth != dest.depth {
        crate::image_cmds::for_each_surface(&mut copies.layers, true, &mut |surf, _| {
            let f = surf.format().with_sample(dest.depth);
            *surf = surf.convert(f);
        });
    }
    // Type keeps its pixels and takes the destination's point size, as in the reference
    // application (24 pt at 72 ppi becomes 5.76 pt at 300 ppi, with no transform). Left at the
    // source's, a copy re-laid out at the destination's resolution on its next edit (#9560).
    crate::canvas_geom::retarget_type_resolution(copies, dest.resolution_dpi);
    adopt_text_styles(copies, &dest.text_styles)?;
    let new_canvas = copies.bounds() != dest.bounds();
    prepare_shapes(&mut copies.layers, dest, new_canvas);
    Ok(())
}

/// Points the copies' type at `dest`'s character and paragraph styles, as the reference
/// application does: a style is matched by name and the destination's definition wins (a copy
/// whose rewritten size differs from it shows a local override), and a style `dest` lacks comes
/// along (already scaled by [`crate::canvas_geom::retarget_type_resolution`]) under a fresh id.
/// Afterwards `copies.text_styles` holds just the styles to add; [`land_text_styles`] adds them.
fn adopt_text_styles(copies: &mut Document, dest: &TextStyles) -> Result<()> {
    let src = std::mem::take(&mut copies.text_styles);
    let mut used = (BTreeSet::new(), BTreeSet::new());
    crate::image_cmds::for_each_layer(&mut copies.layers, &mut |l| {
        if let LayerContent::Text(t) = &l.content {
            used.0.extend(t.runs.iter().filter_map(|r| r.style.style_sheet));
            used.1.extend(t.paragraphs.iter().filter_map(|p| p.style.style_sheet));
        }
    });
    let full = || EngineError::Other("no free text style id".into());
    let (mut chars, mut paras) = (HashMap::new(), HashMap::new());
    let mut next = dest.next_char_id();
    for id in used.0 {
        let Some(def) = src.char_style(id) else { continue };
        let to = match dest.character.iter().find(|d| d.name == def.name) {
            Some(d) => d.id,
            None => {
                let fresh = next.ok_or_else(full)?;
                next = fresh.checked_add(1);
                copies.text_styles.character.push(CharacterStyleDef { id: fresh, ..def.clone() });
                fresh
            }
        };
        chars.insert(id, to);
    }
    let mut next = dest.next_para_id();
    for id in used.1 {
        // Basic Paragraph (id 0) is implicit; every document has its own.
        let Some(def) = src.para_style(id).filter(|_| id != 0) else { continue };
        let to = match dest.paragraph.iter().find(|d| d.name == def.name) {
            Some(d) => d.id,
            None => {
                let fresh = next.ok_or_else(full)?;
                next = fresh.checked_add(1);
                copies.text_styles.paragraph.push(ParagraphStyleDef { id: fresh, ..def.clone() });
                fresh
            }
        };
        paras.insert(id, to);
    }
    crate::image_cmds::for_each_layer(&mut copies.layers, &mut |l| {
        if let LayerContent::Text(t) = &mut l.content {
            for r in &mut t.runs {
                r.style.style_sheet = r.style.style_sheet.and_then(|id| chars.get(&id).copied());
            }
            for p in &mut t.paragraphs {
                p.style.style_sheet = p.style.style_sheet.and_then(|id| paras.get(&id).copied());
            }
        }
    });
    Ok(())
}

/// Adds the styles [`adopt_text_styles`] left in `styles` to `doc`.
pub(crate) fn land_text_styles(doc: &mut Document, styles: TextStyles) {
    doc.text_styles.character.extend(styles.character);
    doc.text_styles.paragraph.extend(styles.paragraph);
}

/// Moves every layer of `copies` by whole pixels (content, masks, vectors and smart placement).
pub(crate) fn translate_copies(copies: &mut Document, destination: &Document, dx: i32, dy: i32) {
    if dx != 0 || dy != 0 {
        let mut snapshot = copies.clone();
        // Vector caches are clipped and quantized for the destination, not the clipboard's source.
        snapshot.size = destination.size;
        snapshot.mode = destination.mode;
        snapshot.depth = destination.depth;
        snapshot.icc_profile = destination.icc_profile.clone();
        for l in &mut copies.layers {
            crate::commands::translate_layer(&snapshot, l, dx, dy);
            crate::vector_cmds::translate_vectors(&snapshot, l, f64::from(dx), f64::from(dy));
        }
    }
}

/// `layer.copyToDocument`.
fn copy_to_document(s: &mut Session, p: &Value) -> Result<Value> {
    let dest = doc_index(s, p, "document")?.ok_or_else(|| bad("missing `document` (the destination's index)"))?;
    let src = match doc_index(s, p, "source")? {
        Some(i) => i,
        None => s.active_index().ok_or(EngineError::NoDocument)?,
    };
    if src == dest {
        return Err(bad("the destination is the source document (use layer.duplicate)"));
    }
    let (from, to) = (&s.documents()[src], &s.documents()[dest]);
    let (sdoc, ddoc) = (from.doc.clone(), to.doc.clone());
    let floating = crate::float_cmds::floating(from).cloned();
    let ids: Vec<LayerId> = match p.get("layers") {
        None => from.selected_layers(),
        Some(v) => v
            .as_array()
            .map(|a| a.iter().map(|id| id.as_u64().map(LayerId)).collect::<Option<Vec<_>>>())
            .and_then(|ids| ids)
            .ok_or_else(|| bad("`layers` must be an array of layer ids"))?,
    };
    if let Some(missing) = ids.iter().find(|id| sdoc.layer(**id).is_none()) {
        return Err(EngineError::NoLayer(*missing));
    }
    let ids = crate::layer_multi_cmds::top_level(&sdoc, &ids);
    if ids.is_empty() {
        return Err(bad("no layers to copy"));
    }
    let selection_only = match p.get("selectionOnly") {
        None => false,
        Some(Value::Bool(on)) => *on,
        Some(_) => return Err(bad("`selectionOnly` must be a boolean")),
    };
    if selection_only && (ids.len() != 1 || sdoc.selection.is_none()) {
        return Err(bad("copying selected pixels requires a selection and one pixel layer"));
    }
    for (doc, role) in [(&sdoc, "source"), (&ddoc, "destination")] {
        if !layered(doc.mode) {
            return Err(EngineError::Other(format!("layers can't be copied with a {:?} {role} document", doc.mode)));
        }
    }
    // The copies, bottom to top, in a scratch document of the source's colour so they convert
    // the way a whole document does.
    let mut copies = Document::new("", sdoc.size, sdoc.mode, sdoc.depth);
    copies.icc_profile = sdoc.icc_profile.clone();
    // Type re-lays out at the source's resolution, so it keeps its size in pixels.
    copies.resolution_dpi = sdoc.resolution_dpi;
    copies.text_styles.clone_from(&sdoc.text_styles);
    for id in &ids {
        let src_layer = sdoc.layer(*id).ok_or(EngineError::NoLayer(*id))?;
        let mut l = if selection_only {
            let parts = match floating.as_ref().filter(|f| f.layer == *id) {
                Some(f) if f.parts.is_mask() => return Err(bad("a floating layer mask cannot be transferred as selected pixels")),
                Some(f) => f.parts.clone(),
                None => std::sync::Arc::new(crate::float_cmds::CutParts::new(&sdoc, *id, true, s.tools.background, false)?),
            };
            if parts.selected_pixels().content_bounds().is_empty() {
                return Err(bad("the selected pixels are empty"));
            }
            let mut layer = Layer::new(src_layer.name.clone(), LayerContent::Raster(parts.selected_pixels().clone()));
            layer.opacity = src_layer.opacity;
            if let Some(f) = floating.as_ref().filter(|f| f.layer == *id) {
                crate::commands::translate_layer(&sdoc, &mut layer, f.offset.0, f.offset.1);
            }
            layer
        } else {
            src_layer.duplicate()
        };
        // A dragged Background arrives as an ordinary layer (the destination keeps its own).
        if sdoc.is_background(*id) {
            l.name = ddoc.next_layer_name("Layer");
            l.locks = Default::default();
        }
        copies.layers.push(l);
    }
    // A name other than the copy's own was typed (#9023).
    let mut typed = None;
    if let (Some(name), [l]) = (p.get("name").and_then(Value::as_str).filter(|n| !n.is_empty()), copies.layers.as_mut_slice()) {
        if l.name != name {
            typed = Some(name.to_string());
        }
        l.name = name.to_string();
    }
    // Measure in source space: destination refresh may clip a shape before it is moved.
    // Missing caches must participate too, including shapes nested inside groups.
    prepare_shapes(&mut copies.layers, &sdoc, false);
    let bounds = copies.layers.iter().filter_map(crate::layer_multi_cmds::layer_bounds).fold(Rect::EMPTY, |a, b| a.union(&b));
    let content = if bounds.is_empty() { sdoc.bounds() } else { bounds };
    adapt_copies(s, &mut copies, &ddoc)?;
    // Placement: centred on the destination's canvas, centred on a point, or offset from where
    // the layers are in the source (default: the same canvas position).
    let (dx, dy) = if p.get("center").and_then(Value::as_bool).unwrap_or(false) {
        let (c, d) = (center(content), center(ddoc.bounds()));
        (d[0] - c[0], d[1] - c[1])
    } else if let Some(at) = point(p, "at")? {
        let c = center(content);
        (at[0] - c[0], at[1] - c[1])
    } else {
        point(p, "offset")?.map_or((0.0, 0.0), |o| (o[0], o[1]))
    };
    let (dx, dy) = (dx.round() as i32, dy.round() as i32);
    translate_copies(&mut copies, &ddoc, dx, dy);
    let patterns: Vec<_> = sdoc.patterns.iter().filter(|pat| !ddoc.patterns.iter().any(|d| d.id == pat.id)).cloned().collect();
    let styles = std::mem::take(&mut copies.text_styles);
    s.set_active(dest);
    let label = if ids.len() == 1 { "Duplicate Layer" } else { "Duplicate Layers" };
    let edited = s.edit(label, |doc, active| {
        doc.patterns.extend(patterns);
        land_text_styles(doc, styles);
        let mut above = *active;
        let mut new = Vec::with_capacity(copies.layers.len());
        // The first copy goes where a new layer would; the rest stack above it.
        let mut place = crate::layer_place::Place::AutoEditable;
        for l in copies.layers {
            let id = crate::layer_place::insert_new(doc, above, l, place);
            place = crate::layer_place::Place::Above;
            above = Some(id);
            new.push(id);
        }
        *active = above;
        // Copied groups keep their own depth: repeated copies would otherwise nest without end.
        crate::layer_multi_cmds::check_group_depth(doc, "Copy Layers")?;
        Ok(new)
    });
    // Nothing changed: the source stays the active document.
    if edited.is_err() {
        s.set_active(src);
    }
    let new = edited?;
    if let (Some(name), [id]) = (typed, new.as_slice()) {
        crate::type_cmds::note_typed_name(s, *id, &name);
    }
    let last = new.last().copied();
    crate::layer_multi_cmds::reselect(s, new.clone(), last);
    Ok(json!({"document": dest, "layers": new.iter().map(|l| l.0).collect::<Vec<_>>(), "offset": [dx, dy]}))
}

/// `layer.duplicate` with `"document": "new"`: Photoshop's Duplicate Layer… ›
/// Document: New. A new document the size of the source's canvas holds just the duplicated
/// layers at their canvas positions (a transparent background elsewhere); the source is
/// untouched and the new document becomes active. Mode, depth, resolution, profile,
/// patterns and global light come along so the copies render the same.
pub(crate) fn duplicate_to_new_document(s: &mut Session, p: &Value) -> Result<Value> {
    let src = s.active_index().ok_or(EngineError::NoDocument)?;
    let sdoc = s.documents().get(src).ok_or(EngineError::NoDocument)?.doc.clone();
    let ids = match crate::commands::explicit_layer(s, p)? {
        Some(id) => vec![id],
        None => s.active().ok_or(EngineError::NoDocument)?.selected_layers(),
    };
    if let Some(missing) = ids.iter().find(|id| sdoc.layer(**id).is_none()) {
        return Err(EngineError::NoLayer(*missing));
    }
    let ids = crate::layer_multi_cmds::top_level(&sdoc, &ids);
    if ids.is_empty() {
        return Err(bad("no layers to copy"));
    }
    if !layered(sdoc.mode) {
        return Err(EngineError::Other(format!("layers can't be duplicated with a {:?} document", sdoc.mode)));
    }
    let doc_name = p.get("documentName").and_then(Value::as_str).filter(|n| !n.is_empty()).map(str::to_string).unwrap_or_else(|| format!("{} copy", sdoc.name));
    let mut nd = Document::new(doc_name, sdoc.size, sdoc.mode, sdoc.depth);
    nd.resolution_dpi = sdoc.resolution_dpi;
    nd.icc_profile.clone_from(&sdoc.icc_profile);
    nd.patterns.clone_from(&sdoc.patterns);
    nd.global_light = sdoc.global_light;
    nd.text_styles.clone_from(&sdoc.text_styles);
    // A name other than the copy's own was typed (#9023).
    let mut typed = None;
    for id in &ids {
        let mut l = sdoc.layer(*id).ok_or(EngineError::NoLayer(*id))?.duplicate();
        if ids.len() == 1
            && let Some(name) = p.get("name").and_then(Value::as_str).filter(|n| !n.is_empty())
        {
            if l.name != name {
                typed = Some(name.to_string());
            }
            l.name = name.to_string();
        }
        nd.layers.push(l);
    }
    // Contents ids belong to a document: fresh ones keep the copies independent of the
    // source while preserving sharing within the batch (as `adapt_copies` does).
    let mut contents = HashMap::new();
    let smart_ids: Vec<_> = nd.walk().into_iter().filter_map(|(_, _, l)| matches!(l.content, LayerContent::Smart(_)).then_some(l.id)).collect();
    for id in smart_ids {
        if let Some(l) = nd.layer_mut(id)
            && let LayerContent::Smart(sm) = &mut l.content
        {
            sm.contents_id = *contents.entry(sm.contents_id).or_insert_with(SmartContentsId::fresh);
        }
    }
    // The new document gets the styles its type uses.
    adopt_text_styles(&mut nd, &TextStyles::default())?;
    // Its own Basic Paragraph is the source's: type that inherits from it must not gain overrides.
    nd.text_styles.basic.clone_from(&sdoc.text_styles.basic);
    let new_ids: Vec<LayerId> = nd.layers.iter().map(|l| l.id).collect();
    let index = s.add_document(nd, None);
    if let (Some(name), [id]) = (typed, new_ids.as_slice()) {
        crate::type_cmds::note_typed_name(s, *id, &name);
    }
    crate::layer_multi_cmds::reselect(s, new_ids.clone(), new_ids.last().copied());
    let mut out = json!({"document": index, "layers": new_ids.iter().map(|l| l.0).collect::<Vec<_>>()});
    if let [one] = new_ids.as_slice()
        && let Some(map) = out.as_object_mut()
    {
        map.insert("layer".into(), json!(one.0));
    }
    Ok(out)
}

pub fn specs() -> Vec<CommandSpec> {
    vec![CommandSpec {
        id: CMD,
        label: "Copy Layers to Document",
        menu: &[],
        shortcut: None,
        params: r##"{"document":index (destination),"layers":[id,…]?=the source's selected layers,"source":index?=active,"selectionOnly":bool=false (copy selected pixels from one raster layer),"center":bool=false (centre on the destination's canvas),"at":[x,y]? (centre the copies on this point),"offset":[dx,dy]?=[0,0] (from their place in the source),"name":str? (one layer: the copy's name)}"##,
        enabled,
        run: copy_to_document,
        journal: true,
    }]
}

#[cfg(test)]
mod tests;