Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-image

Source of release 0.45.1

0.45.1 / src / canvas / layer_clip.lucb

#==============================================================================================
#
#   canvas/layer_clip - The layer clipboard
#
#   DESCRIPTION:
#       Whole layers — pixels past the canvas too, masks, styles, adjustments, text, groups
#       with all they hold — copied from one document and pasted into it or another, as
#       Photoshop's Copy and Paste with layers selected and its drag of a layer onto
#       another document. The layers are snapshots whose tiles are shared, so copying costs
#       little; they live for the process beside the system clipboard's pixels, and pasting
#       gives every layer a new id in the document it lands in. The app puts their pixels
#       (it copies those after) or, for layers without any, their names on the system
#       clipboard: Paste takes the layers while the clipboard still holds what copying them
#       put there (`copied_layers_held`), and anything another program copies since wins.
#
#==============================================================================================

extend Canvas:
    ## Copy layer `id` whole — pixels, mask, style, text, a group with all it
    ## holds — on top of the layers copied so far, to paste here or into
    ## another document; `forget_copied_layers` starts afresh. The app puts
    ## `copied_layer_names` on the system clipboard (not for a drag between
    ## documents).
    pub func copy_layer(id: i64) -> !:
        try self.require_open()
        try self.require_idle()
        let document = self.document else error(invalid, "draw the document once before copying layers")
        if let clip = layer_clip:
            if clip.width != self.extent_x or clip.height != self.extent_y:
                clear_layer_clip()
        if layer_clip == none:
            let records = try new Record[8] --- in memory.heap
            errdefer free(records) in memory.heap
            let layers = try new Layer*[4] --- in memory.heap
            errdefer free(layers) in memory.heap
            var names: str = ""
            with memory.heap:
                names = try strings.copy("")
            layer_clip = try new LayerClip(layers = layers, layer_count = 0, records = records, record_count = 0, width = self.extent_x, height = self.extent_y, names = names) in memory.heap
            # No pixels copied with these yet.
            copied_width = 0
            copied_height = 0
        let clip = layer_clip else trap("just made")
        if clip.layer_count == clip.layers.length:
            let grown = try new Layer*[clip.layers.length * 2] --- in memory.heap
            memory.copy(grown, clip.layers, clip.layer_count)
            free(clip.layers) in memory.heap
            clip.layers = grown
        let snapshot = try document.snapshot((u64)id)
        errdefer release_snapshot(snapshot)
        try keep_records(&self, clip, snapshot)
        clip.layers[clip.layer_count] = snapshot
        clip.layer_count += 1
        var names: str = ""
        with memory.heap:
            if clip.names.length > 0:
                let parts: str[2] = [clip.names, snapshot.name]
                names = try strings.join(parts, "\n")
            else:
                names = try strings.copy(snapshot.name)
        strings.release(clip.names)
        clip.names = names

    ## Forget the copied layers.
    pub static func forget_copied_layers():
        clear_layer_clip()

    ## The copied layers' names, a line each, as the system clipboard's text.
    pub static func copied_layer_names() -> interop.Owned[str]!:
        let clip = layer_clip else return try interop.copy_text("")
        return try interop.copy_text(clip.names)

    ## Whether Paste should take the copied layers, the system clipboard holding
    ## a picture of `width` by `height` (and `text` when it holds none, `picture`
    ## false): some are copied and the clipboard still holds what copying them
    ## put there, their pixels or their names.
    pub static func copied_layers_held(picture: bool, width: i64, height: i64, text: str) -> bool:
        let clip = layer_clip else return false
        if picture:
            return copied_width != 0 and width == (i64)copied_width and height == (i64)copied_height
        return text == clip.names

    ## How many layers are copied.
    pub static func copied_layers() -> i64:
        let clip = layer_clip else return 0
        return (i64)clip.layer_count

    ## The copied layers into group `parent` (0 the top level) from `position`
    ## up among its layers (negative: on top), as one step; the topmost's id.
    ## They land where they were on a canvas of the same size or `in_place`,
    ## else centred.
    pub func paste_layers(parent: i64, position: i64, in_place: bool = false) -> i64!:
        try self.begin_edit("Paste Layers")
        defer self.end_edit()
        return try paste_layer_clip(&self, parent, position, in_place)

struct LayerClip:
    ## Snapshots of the copied layers, bottom to top.
    var layers: Layer*[]
    var layer_count: usize
    ## The records of the copied layers and all they hold, with the ids they had.
    var records: Record[]
    var record_count: usize
    ## The canvas they came from, to centre them on one of another size.
    var width: u32
    var height: u32
    ## Their names, a line each, as the app puts them on the system clipboard.
    var names: str

var layer_clip: LayerClip*? = none

## Forget the copied layers.
func clear_layer_clip():
    let clip = layer_clip else return
    layer_clip = none
    for layer in clip.layers[..<clip.layer_count]:
        release_snapshot(layer)
    if clip.layers.length > 0:
        free(clip.layers) in memory.heap
    release_records(clip.records, clip.record_count)
    strings.release(clip.names)
    free(clip) in memory.heap

## The records of `layer` and all it holds, copied into `clip`.
func keep_records(canvas: Canvas*, clip: LayerClip*, layer: const Layer*) -> !:
    if clip.record_count == clip.records.length:
        let grown = try new Record[clip.records.length * 2] --- in memory.heap
        memory.copy(grown, clip.records, clip.record_count)
        free(clip.records) in memory.heap
        clip.records = grown
    let index = try canvas.index(layer.id)
    clip.records[clip.record_count] = try copied_record(canvas.records[index])
    clip.record_count += 1
    for child in layer.layers():
        try keep_records(canvas, clip, child)

## A record with its own name and text, and no picture waiting.
func copied_record(record: Record) -> Record!:
    var copy = record
    copy.picture = none
    copy.mask_picture = none
    with memory.heap:
        copy.name = try strings.copy(record.name)
    errdefer strings.release(copy.name)
    try copy_type(&copy)
    errdefer release_type(&copy)
    try copy_elements(&copy)
    return copy

## `Canvas.paste_layers`: the copied layers into group `parent` (0 the top
## level) from `position` up (negative: on top), each with a new id; the
## topmost's id. They land where they were when the canvases are the same
## size or `in_place`, else centred.
func paste_layer_clip(canvas: Canvas*, parent: i64, position: i64, in_place: bool) -> i64!:
    try canvas.require_open()
    try canvas.require_idle()
    let clip = layer_clip else error(invalid, "no layers are copied")
    let document = canvas.document else error(invalid, "draw the document once before pasting layers")
    var dx = 0.0
    var dy = 0.0
    if not in_place:
        dx = math.floor(((f64)canvas.extent_x - (f64)clip.width) / 2.0)
        dy = math.floor(((f64)canvas.extent_y - (f64)clip.height) / 2.0)
    var top: i64 = 0
    for at in 0..<clip.layer_count:
        let place = (usize)position + at if position >= 0 else (usize)1 << 40
        let first = try document.adopt(clip.layers[at], none if parent == 0 else (u64)parent, place, dx, dy)
        var next = first
        try add_clip_records(canvas, clip, clip.layers[at], (u64)parent, &next, dx, dy)
        canvas.next_id = next
        top = (i64)first
    try canvas.sync_records()
    return top

## Records for `layer` and all it holds as `Document.adopt` numbered them, in
## pre-order from `*next`, under `parent`.
func add_clip_records(canvas: Canvas*, clip: const LayerClip*, layer: const Layer*, parent: u64, next: u64*, dx: f64, dy: f64) -> !:
    var found: usize = 0
    while found < clip.record_count and clip.records[found].id != layer.id:
        found += 1
    if found == clip.record_count:
        error(invalid, "a copied layer lost its record")
    try canvas.record_room()
    var record = try copied_record(clip.records[found])
    record.id = *next
    record.parent = parent
    if record.typed:
        record.typeset.x += dx
        record.typeset.y += dy
    for at in 0..<record.elements.length:
        record.elements[at].spec.matrix[4] += dx
        record.elements[at].spec.matrix[5] += dy
    place_graph(&record, [1.0, 0.0, 0.0, 1.0, dx, dy])
    canvas.records[canvas.count] = record
    canvas.count += 1
    *next += 1
    for child in layer.layers():
        try add_clip_records(canvas, clip, child, record.id, next, dx, dy)