Skip to content

AI-native design editor. Open-source Figma alternative.

14.0 KB
Raw Blame
import {
  cloneVectorNetwork,
  copyDerivedGlyphs,
  copyGeometryPaths,
  copyStroke,
  copyStrokes,
  scaleGeometryPaths
} from './copy'
import { enforcedAspectRatio } from './layout/aspect-ratio'
import type { Rect } from './primitives'
import type { ConstraintType, DerivedTextGlyph, SceneNode, Stroke, VectorNetwork } from './types'

/**
 * Drag-start state a live resize re-scales from. Path text needs more than the
 * layout rect: its OUTSIDE stroke silhouettes and glyph outlines are baked
 * geometry that must scale with the node, and textPathBox is the layout box the
 * glyphs were reflowed against.
 */
export type ResizeSnapshot = Pick<
  SceneNode,
  | 'type'
  | 'x'
  | 'y'
  | 'width'
  | 'height'
  | 'vectorNetwork'
  | 'fillGeometry'
  | 'strokeGeometry'
  | 'derivedTextGlyphs'
  | 'strokes'
  | 'textPathData'
  | 'textPathBox'
>

interface ResizeGraph {
  getNode(id: string): SceneNode | undefined
}

const CONSTRAINT_CONTAINER_TYPES = new Set([
  'FRAME',
  'COMPONENT',
  'COMPONENT_SET',
  'INSTANCE',
  'GROUP',
  'BOOLEAN_OPERATION'
])

function constrainedAxis(
  position: number,
  size: number,
  parentBefore: number,
  parentAfter: number,
  constraint: ConstraintType
): { position: number; size: number } {
  const delta = parentAfter - parentBefore
  if (constraint === 'MAX') return { position: position + delta, size }
  if (constraint === 'CENTER') return { position: position + delta / 2, size }
  if (constraint === 'STRETCH') return { position, size: Math.max(1, size + delta) }
  if (constraint === 'SCALE' && parentBefore > 0) {
    const scale = parentAfter / parentBefore
    return { position: position * scale, size: Math.max(1, size * scale) }
  }
  return { position, size }
}

type AxisPlacement = { position: number; size: number }

const FLEXIBLE_CONSTRAINTS: ReadonlySet<ConstraintType> = new Set(['STRETCH', 'SCALE'])

/** The axis at `size`, keeping the edge or centre its constraint pins where it was placed. */
function resizedInPlace(
  placed: AxisPlacement,
  size: number,
  constraint: ConstraintType
): AxisPlacement {
  if (constraint === 'MAX') return { position: placed.position + placed.size - size, size }
  if (constraint === 'CENTER') return { position: placed.position + (placed.size - size) / 2, size }
  return { position: placed.position, size }
}

/** Keep a placed box's constrained edges or center when its content determines its size. */
export function resizedChildRect(
  placed: Rect,
  size: Pick<Rect, 'width' | 'height'>,
  horizontal: ConstraintType,
  vertical: ConstraintType
): Rect {
  const x = resizedInPlace({ position: placed.x, size: placed.width }, size.width, horizontal)
  const y = resizedInPlace({ position: placed.y, size: placed.height }, size.height, vertical)
  return { x: x.position, y: y.position, width: x.size, height: y.size }
}

/**
 * A locked aspect ratio holds when exactly one axis follows the parent, as in Figma: that axis
 * sizes the other. With both axes following the parent, or neither, the ratio gives way.
 */
function keepLockedRatio(
  x: AxisPlacement,
  y: AxisPlacement,
  horizontal: ConstraintType,
  vertical: ConstraintType,
  aspectRatio: number | null
): { x: AxisPlacement; y: AxisPlacement } {
  const flexibleX = FLEXIBLE_CONSTRAINTS.has(horizontal)
  if (aspectRatio === null || flexibleX === FLEXIBLE_CONSTRAINTS.has(vertical)) return { x, y }
  return flexibleX
    ? { x, y: resizedInPlace(y, x.size / aspectRatio, vertical) }
    : { x: resizedInPlace(x, y.size * aspectRatio, horizontal), y }
}

/** A child's box after its parent resizes, keeping a locked aspect ratio as Figma does. */
export function constrainedChildRect(
  child: Rect,
  parentBefore: Pick<Rect, 'width' | 'height'>,
  parentAfter: Pick<Rect, 'width' | 'height'>,
  horizontal: ConstraintType,
  vertical: ConstraintType,
  aspectRatio: number | null = null
): Rect {
  let x = constrainedAxis(child.x, child.width, parentBefore.width, parentAfter.width, horizontal)
  let y = constrainedAxis(child.y, child.height, parentBefore.height, parentAfter.height, vertical)
  ;({ x, y } = keepLockedRatio(x, y, horizontal, vertical, aspectRatio))
  return {
    x: Math.round(x.position),
    y: Math.round(y.position),
    width: Math.round(x.size),
    height: Math.round(y.size)
  }
}

function layoutConstrainedAxis(
  position: number,
  size: number,
  parentBefore: number,
  parentAfter: number,
  constraint: ConstraintType
): { position: number; size: number } {
  const delta = Math.round(parentAfter) - Math.round(parentBefore)
  if (constraint === 'MAX') return { position: position + delta, size }
  if (constraint === 'CENTER') {
    return { position: position + Math.round(parentAfter / 2) - Math.round(parentBefore / 2), size }
  }
  if (constraint === 'STRETCH') return { position, size: Math.abs(size + delta) }
  if (constraint === 'SCALE' && parentBefore > 0) {
    const scale = parentAfter / parentBefore
    return { position: Math.round(position * scale * 128) / 128, size: size * scale }
  }
  return { position, size }
}

/**
 * Where a child goes when layout resizes its parent, as Figma places a layer that ignores auto
 * layout when its Hug frame grows (measured on desktop 126): Right and Left & right move or grow
 * it by the change in the parent's size rounded to whole pixels, Center by the change in its
 * rounded half, and Scale by the exact ratio, its position to 1/128 of a pixel. Sizes are not
 * rounded, so applying a run of changes one at a time lands where applying them at once does.
 */
export function layoutConstrainedChildRect(
  child: Rect,
  parentBefore: Pick<Rect, 'width' | 'height'>,
  parentAfter: Pick<Rect, 'width' | 'height'>,
  horizontal: ConstraintType,
  vertical: ConstraintType,
  aspectRatio: number | null = null
): Rect {
  const placed = keepLockedRatio(
    layoutConstrainedAxis(child.x, child.width, parentBefore.width, parentAfter.width, horizontal),
    layoutConstrainedAxis(child.y, child.height, parentBefore.height, parentAfter.height, vertical),
    horizontal,
    vertical,
    aspectRatio
  )
  return {
    x: placed.x.position,
    y: placed.y.position,
    width: placed.x.size,
    height: placed.y.size
  }
}

export function scaledChildRect(
  child: Rect,
  parentBefore: Pick<Rect, 'width' | 'height'>,
  parentAfter: Pick<Rect, 'width' | 'height'>
): Rect {
  return constrainedChildRect(child, parentBefore, parentAfter, 'SCALE', 'SCALE')
}

export function scaleVectorNetworkForResize(
  vectorNetwork: VectorNetwork | null,
  originalWidth: number,
  originalHeight: number,
  width: number,
  height: number
): VectorNetwork | null {
  if (!vectorNetwork || (originalWidth <= 0 && originalHeight <= 0)) return null

  // A flat side, as a straight line's, has nothing to scale along it.
  const scaleX = originalWidth > 0 ? width / originalWidth : 1
  const scaleY = originalHeight > 0 ? height / originalHeight : 1
  if (scaleX === 1 && scaleY === 1) return null

  return {
    vertices: vectorNetwork.vertices.map((vertex) => ({
      ...vertex,
      x: vertex.x * scaleX,
      y: vertex.y * scaleY
    })),
    segments: vectorNetwork.segments.map((segment) => ({
      ...segment,
      tangentStart: {
        x: segment.tangentStart.x * scaleX,
        y: segment.tangentStart.y * scaleY
      },
      tangentEnd: {
        x: segment.tangentEnd.x * scaleX,
        y: segment.tangentEnd.y * scaleY
      }
    })),
    regions: vectorNetwork.regions
  }
}

/** Snapshot one node's resize-relevant state (rect + geometry that must scale with it). */
export function createResizeSnapshot(node: SceneNode): ResizeSnapshot {
  return {
    type: node.type,
    x: node.x,
    y: node.y,
    width: node.width,
    height: node.height,
    vectorNetwork: node.vectorNetwork ? cloneVectorNetwork(node.vectorNetwork) : null,
    fillGeometry: copyGeometryPaths(node.fillGeometry),
    strokeGeometry: copyGeometryPaths(node.strokeGeometry),
    derivedTextGlyphs: copyDerivedGlyphs(node.derivedTextGlyphs),
    strokes: copyStrokes(node.strokes),
    textPathData: node.textPathData ? structuredClone(node.textPathData) : null,
    textPathBox: node.textPathBox ? { ...node.textPathBox } : null
  }
}

function scaleDerivedGlyphs(
  glyphs: DerivedTextGlyph[] | null,
  sx: number,
  sy: number
): DerivedTextGlyph[] | null {
  if (!glyphs?.length) return glyphs
  return glyphs.map((g) => ({
    ...g,
    x: g.x * sx,
    y: g.y * sy,
    scaleX: (g.scaleX ?? 1) * sx,
    scaleY: (g.scaleY ?? 1) * sy,
    commandsBlob: new Uint8Array(g.commandsBlob)
  }))
}

function hasPath(orig: Pick<ResizeSnapshot, 'vectorNetwork'>): boolean {
  return (orig.vectorNetwork?.segments.length ?? 0) > 0
}

function scaleStrokes(strokes: Stroke[], sx: number, sy: number): Stroke[] {
  if (strokes.length === 0) return strokes
  // Weight is a scalar; average scale is the usual 2D → 1D compromise.
  const weightScale = (Math.abs(sx) + Math.abs(sy)) / 2
  return strokes.map((s) => ({ ...copyStroke(s), weight: s.weight * weightScale }))
}

/**
 * All geometry that must track a width/height change. Historically only
 * vectorNetwork + fillGeometry were updated — path-text strokeGeometry and
 * glyphs stayed at the pre-resize size, so shrinking a sticker made the white
 * OUTSIDE outlines look massively thick (DomeSticker resize bug).
 */
export function scaledGeometryChanges(
  orig: Pick<
    ResizeSnapshot,
    | 'type'
    | 'vectorNetwork'
    | 'fillGeometry'
    | 'strokeGeometry'
    | 'derivedTextGlyphs'
    | 'strokes'
    | 'textPathData'
    | 'textPathBox'
  >,
  origWidth: number,
  origHeight: number,
  width: number,
  height: number
): Partial<SceneNode> {
  if (origWidth <= 0 || origHeight <= 0) return {}
  const sx = width / origWidth
  const sy = height / origHeight
  if (sx === 1 && sy === 1) return {}

  const changes: Partial<SceneNode> = {}
  const resizedVN = scaleVectorNetworkForResize(
    orig.vectorNetwork,
    origWidth,
    origHeight,
    width,
    height
  )
  if (resizedVN) changes.vectorNetwork = resizedVN

  if (orig.fillGeometry.length > 0) {
    changes.fillGeometry = scaleGeometryPaths(orig.fillGeometry, sx, sy)
  }
  // A stroke keeps its weight through a resize, as in Figma, so a saved stroke outline goes where
  // the stroke can be drawn again at its weight: from a path, or from a shape's own outline.
  // Text and a vector with only its outline keep scaling the outline, which is all they have.
  const outlineOnly = orig.type === 'TEXT' || (orig.type === 'VECTOR' && !hasPath(orig))
  if (orig.strokeGeometry.length > 0) {
    changes.strokeGeometry = outlineOnly ? scaleGeometryPaths(orig.strokeGeometry, sx, sy) : []
  }
  // Path text scales with its path, including baked path text that kept only rotated glyphs;
  // other text reflows in the new box, so its glyphs are dropped.
  const glyphs = orig.derivedTextGlyphs ?? []
  const pathText =
    !!orig.textPathData || !!orig.textPathBox || glyphs.some((glyph) => (glyph.rotation ?? 0) !== 0)
  if (glyphs.length > 0 && pathText) {
    changes.derivedTextGlyphs = scaleDerivedGlyphs(glyphs, sx, sy)
    // Keep the layout/selection box in sync with the scaled glyphs. reflow
    // overrides this when it applies; this covers the fallback where reflow
    // returns null (no path data) so the box doesn't carry a stale size.
    if (orig.textPathBox) {
      changes.textPathBox = {
        x: orig.textPathBox.x * sx,
        y: orig.textPathBox.y * sy,
        width: orig.textPathBox.width * sx,
        height: orig.textPathBox.height * sy
      }
    }
  }
  // Glyphs scaled as a picture take their strokes along; any other stroke keeps its weight, as
  // Figma reports it, even where only a scaled outline can draw it.
  if (orig.strokes.length > 0 && glyphs.length > 0 && pathText) {
    changes.strokes = scaleStrokes(orig.strokes, sx, sy)
  }
  return changes
}

export function collectResizeDescendants(
  graph: ResizeGraph,
  rootId: string
): Map<string, ResizeSnapshot> | null {
  const root = graph.getNode(rootId)
  if (!root || !CONSTRAINT_CONTAINER_TYPES.has(root.type)) return null
  const snapshots = new Map<string, ResizeSnapshot>()

  const collect = (parentId: string) => {
    const parent = graph.getNode(parentId)
    if (!parent) return
    for (const childId of parent.childIds) {
      const child = graph.getNode(childId)
      if (!child) continue
      snapshots.set(childId, createResizeSnapshot(child))
      collect(childId)
    }
  }

  collect(rootId)
  return snapshots.size > 0 ? snapshots : null
}

export function computeConstrainedResizeChanges(
  graph: ResizeGraph,
  rootId: string,
  rootBefore: Pick<Rect, 'width' | 'height'>,
  rootAfter: Pick<Rect, 'width' | 'height'>,
  originals: ReadonlyMap<string, ResizeSnapshot>
): Map<string, Partial<SceneNode>> {
  const changes = new Map<string, Partial<SceneNode>>()

  const compute = (
    parentId: string,
    parentBefore: Pick<Rect, 'width' | 'height'>,
    parentAfter: Pick<Rect, 'width' | 'height'>
  ) => {
    const parent = graph.getNode(parentId)
    if (!parent) return
    const scalesChildren = parent.type === 'GROUP' || parent.type === 'BOOLEAN_OPERATION'
    for (const childId of parent.childIds) {
      const original = originals.get(childId)
      const child = graph.getNode(childId)
      if (!original || !child) continue
      const isInFlow = parent.layoutMode !== 'NONE' && child.layoutPositioning !== 'ABSOLUTE'
      if (isInFlow) {
        compute(childId, original, child)
        continue
      }
      const rect = scalesChildren
        ? scaledChildRect(original, parentBefore, parentAfter)
        : constrainedChildRect(
            original,
            parentBefore,
            parentAfter,
            child.horizontalConstraint,
            child.verticalConstraint,
            enforcedAspectRatio(child)
          )
      const childChanges: Partial<SceneNode> = {
        ...rect,
        ...scaledGeometryChanges(original, original.width, original.height, rect.width, rect.height)
      }
      changes.set(childId, childChanges)
      // The final pass sees layout containers after Yoga has resolved HUG/FILL sizing.
      compute(childId, original, child.layoutMode === 'NONE' ? rect : child)
    }
  }

  compute(rootId, rootBefore, rootAfter)
  return changes
}