Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

4.3 KB
Raw Blame
import type {
  BlendMode as AgPsdBlendMode,
  Layer as AgPsdLayer,
  LayerMaskData as AgPsdMaskData,
} from 'ag-psd';
import {
  BlendMode,
  LayerItem,
  isFolderLikeLayer,
  hasTileBackedLayer,
} from '../../types/editor';
import type { LayerTreeNode } from '../layerTree';
import { toPsdLayerColor } from '../layerOrganization';
import {
  readNativeLayerBoundsRgba,
  readNativeLayerMaskRgba,
} from '../nativeEngineBridge';

/**
 * Maps Ink Studio BlendMode to ag-psd BlendMode format.
 */
export function toAgPsdBlendMode(mode: BlendMode): AgPsdBlendMode {
  switch (mode) {
    case 'color-dodge':
    case 'color-dodge-glow':
      return 'color dodge';
    case 'color-burn':
      return 'color burn';
    case 'linear-burn':
      return 'linear burn';
    case 'darker-color':
      return 'darker color';
    case 'add-glow':
      return 'linear dodge';
    case 'lighter-color':
      return 'lighter color';
    case 'hard-light':
      return 'hard light';
    case 'soft-light':
      return 'soft light';
    case 'vivid-light':
      return 'vivid light';
    case 'linear-light':
      return 'linear light';
    case 'pin-light':
      return 'pin light';
    case 'hard-mix':
      return 'hard mix';
    case 'pass-through':
      return 'pass through';
    case 'normal':
    case 'multiply':
    case 'screen':
    case 'overlay':
    case 'darken':
    case 'lighten':
    case 'difference':
    case 'exclusion':
    case 'subtract':
    case 'divide':
    case 'hue':
    case 'saturation':
    case 'color':
    case 'luminosity':
      return mode;
    default:
      return 'normal';
  }
}

async function extractNativeLayerMaskForPsd(
  layer: LayerItem,
  docId?: string | null
): Promise<AgPsdMaskData | undefined> {
  if (!layer.hasMask && !layer.maskCanvas) {
    return undefined;
  }
  const nativeMask = await readNativeLayerMaskRgba(layer.id, docId);
  if (!nativeMask || nativeMask.width <= 0 || nativeMask.height <= 0) {
    return undefined;
  }
  return {
    top: 0,
    left: 0,
    bottom: nativeMask.height,
    right: nativeMask.width,
    defaultColor: nativeMask.defaultColor,
    disabled: layer.maskEnabled === false,
    positionRelativeToLayer: layer.maskLinked === false,
    imageData: {
      data: nativeMask.rgba,
      width: nativeMask.width,
      height: nativeMask.height,
    },
  };
}

/**
 * Builds an `ag-psd` layer tree by reading tight per-layer bounding boxes and masks
 * directly from Rust `ink-engine` (`readNativeLayerBoundsRgba` / `readNativeLayerMaskRgba`)
 * with ZERO DOM `<canvas>` allocations.
 */
export async function buildPsdLayerTree(
  nodes: LayerTreeNode[],
  docId?: string | null
): Promise<AgPsdLayer[]> {
  const result: AgPsdLayer[] = [];

  for (let i = nodes.length - 1; i >= 0; i--) {
    const node = nodes[i];
    const layer = node.layer;
    const isFolder = isFolderLikeLayer(layer);
    if (!isFolder && !hasTileBackedLayer(layer)) continue;

    const psdLayer: AgPsdLayer = {
      name: layer.name,
      hidden: !layer.visible,
      opacity: Math.max(0, Math.min(1, layer.opacity ?? 1)),
      blendMode: toAgPsdBlendMode(layer.blendMode),
    };

    if (layer.clipped) {
      psdLayer.clipping = true;
    }

    const psdColor = toPsdLayerColor(layer.colorLabel);
    if (psdColor) {
      psdLayer.layerColor = psdColor;
    }

    if (layer.alphaLocked || layer.locked) {
      psdLayer.protected = {
        transparency: Boolean(layer.alphaLocked),
        composite: Boolean(layer.locked),
        position: Boolean(layer.locked),
      };
      if (layer.alphaLocked) {
        psdLayer.transparencyProtected = true;
      }
    }

    const mask = await extractNativeLayerMaskForPsd(layer, docId);
    if (mask) {
      psdLayer.mask = mask;
    }

    if (isFolder) {
      psdLayer.opened = !layer.collapsed;
      psdLayer.children = await buildPsdLayerTree(node.children, docId);
    } else {
      const bounds = await readNativeLayerBoundsRgba(layer.id, docId);
      if (bounds && bounds.width > 0 && bounds.height > 0) {
        psdLayer.left = bounds.x;
        psdLayer.top = bounds.y;
        psdLayer.right = bounds.x + bounds.width;
        psdLayer.bottom = bounds.y + bounds.height;
        psdLayer.imageData = {
          data: bounds.rgba,
          width: bounds.width,
          height: bounds.height,
        };
      }
    }

    result.push(psdLayer);
  }

  return result;
}