Skip to content

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

3.1 KB
Raw Blame
import type { NodeChange } from '@open-pencil/kiwi/fig/codec'
import type { GeometryPath, VectorNetwork } from '@open-pencil/scene-graph'
import type { Vector } from '@open-pencil/scene-graph/primitives'

import { decodeVectorNetworkBlob, type StyleOverride } from './network'

/** Scene winding rules name the even-odd case EVENODD; the archive names it ODD. */
export function toKiwiWindingRule(rule: string | undefined): 'ODD' | 'NONZERO' {
  return rule === 'EVENODD' ? 'ODD' : 'NONZERO'
}

/** The archive's ODD reads back as the scene's EVENODD. */
export function fromKiwiWindingRule(rule: string | undefined): 'EVENODD' | 'NONZERO' {
  return rule === 'ODD' || rule === 'EVENODD' ? 'EVENODD' : 'NONZERO'
}

export function alignGeometryWindingRules(
  geometry: GeometryPath[],
  vectorNetwork: VectorNetwork | null
): GeometryPath[] {
  const regions = vectorNetwork?.regions ?? []
  if (geometry.length === regions.length) {
    return geometry.map((path, index) => ({
      ...path,
      windingRule: regions[index].windingRule
    }))
  }
  if (
    geometry.length === 1 &&
    regions.length > 0 &&
    regions.every((region) => region.windingRule === regions[0].windingRule)
  ) {
    return [{ ...geometry[0], windingRule: regions[0].windingRule }]
  }
  return geometry
}

export function resolveVectorNetwork(nc: NodeChange, blobs: Uint8Array[]): VectorNetwork | null {
  const vectorData = nc.vectorData as
    | {
        vectorNetworkBlob?: number
        normalizedSize?: Vector
        styleOverrideTable?: StyleOverride[]
      }
    | undefined

  if (vectorData?.vectorNetworkBlob === undefined) return null
  const idx = vectorData.vectorNetworkBlob
  if (idx < 0 || idx >= blobs.length) return null

  try {
    const network = decodeVectorNetworkBlob(blobs[idx], vectorData.styleOverrideTable)

    const ns = vectorData.normalizedSize
    const nodeW = nc.size?.x ?? 0
    const nodeH = nc.size?.y ?? 0
    // Each axis scales on its own: a straight line has no extent across it to scale.
    const sx = ns && ns.x > 0 && nodeW > 0 ? nodeW / ns.x : 1
    const sy = ns && ns.y > 0 && nodeH > 0 ? nodeH / ns.y : 1
    if (sx !== 1 || sy !== 1) {
      for (const v of network.vertices) {
        v.x *= sx
        v.y *= sy
      }
      for (const seg of network.segments) {
        seg.tangentStart = { x: seg.tangentStart.x * sx, y: seg.tangentStart.y * sy }
        seg.tangentEnd = { x: seg.tangentEnd.x * sx, y: seg.tangentEnd.y * sy }
      }
    }

    return network
  } catch {
    return null
  }
}

interface KiwiPath {
  windingRule?: string
  commandsBlob?: number
}

/** Geometry outlines; a vector's per-region paints are on its network's regions. */
export function resolveGeometryPaths(
  paths: KiwiPath[] | undefined,
  blobs: Uint8Array[]
): GeometryPath[] {
  if (!paths || paths.length === 0) return []
  const result: GeometryPath[] = []
  for (const p of paths) {
    if (p.commandsBlob === undefined || p.commandsBlob < 0 || p.commandsBlob >= blobs.length)
      continue
    const blob = blobs[p.commandsBlob]
    if (blob.length === 0) continue
    result.push({ windingRule: fromKiwiWindingRule(p.windingRule), commandsBlob: blob })
  }
  return result
}