Skip to content

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

fix(kiwi): preserve schema paint fields

Danila Poyarkov84cf18ba8095
diff --git a/packages/core/src/canvas/fills.ts b/packages/core/src/canvas/fills.ts
index 9aa56cff1..710c6be97 100644
--- a/packages/core/src/canvas/fills.ts
+++ b/packages/core/src/canvas/fills.ts
@@ -87,6 +87,12 @@ export function applyFill(
return r.applyImageFill(fill, node, graph)
}
+ if (fill.type === 'PATTERN' || fill.type === 'NOISE' || fill.type === 'CUSTOM') {
+ const c = r.resolveFillColor(fill, fillIndex, node, graph)
+ r.fillPaint.setColor(r.ck.Color4f(c.r, c.g, c.b, c.a))
+ return true
+ }
+
return false
}
diff --git a/packages/core/src/kiwi/fig/codec/index.ts b/packages/core/src/kiwi/fig/codec/index.ts
index 18d43c39d..506fe68b4 100644
--- a/packages/core/src/kiwi/fig/codec/index.ts
+++ b/packages/core/src/kiwi/fig/codec/index.ts
@@ -190,6 +190,10 @@ export interface Paint {
| 'GRADIENT_ANGULAR'
| 'GRADIENT_DIAMOND'
| 'IMAGE'
+ | 'VIDEO'
+ | 'PATTERN'
+ | 'NOISE'
+ | 'CUSTOM'
color?: Color
opacity?: number
visible?: boolean
@@ -198,6 +202,18 @@ export interface Paint {
transform?: Matrix
image?: { hash: string | Uint8Array }
imageScaleMode?: string
+ sourceNodeId?: GUID
+ spacing?: number
+ patternSpacing?: Vector
+ patternTileType?: string
+ verticalAlignment?: string
+ horizontalAlignment?: string
+ id?: GUID
+ altText?: string
+ noiseType?: string
+ density?: number
+ noiseSize?: Vector
+ customEffectId?: { guid?: GUID }
colorVariableBinding?: VariableBinding
colorVar?: {
value?: {
diff --git a/packages/core/src/kiwi/fig/node-change/paint.ts b/packages/core/src/kiwi/fig/node-change/paint.ts
index b3328279f..ac3a7fce1 100644
--- a/packages/core/src/kiwi/fig/node-change/paint.ts
+++ b/packages/core/src/kiwi/fig/node-change/paint.ts
@@ -1,5 +1,6 @@
import { normalizeColor } from '#core/color'
import type { Paint, Effect as KiwiEffect } from '#core/kiwi/fig/codec'
+import { guidToString } from '#core/kiwi/fig/node-change/guid'
import type {
Fill,
FillType,
@@ -43,43 +44,60 @@ function resolveColorVar(paint: Paint): Color | undefined {
return variableColorResolver(alias) ?? undefined
}
-export function convertFills(paints?: Paint[]): Fill[] {
- if (!paints) return []
- return paints.map((p) => {
- const base: Fill = {
- type: p.type as FillType,
- color: convertColor(resolveColorVar(p) ?? p.color),
- opacity: p.opacity ?? 1,
- visible: p.visible ?? true,
- blendMode: (p.blendMode ?? 'NORMAL') as BlendMode
- }
+function convertBaseFill(p: Paint): Fill {
+ return {
+ type: p.type as FillType,
+ color: convertColor(resolveColorVar(p) ?? p.color),
+ opacity: p.opacity ?? 1,
+ visible: p.visible ?? true,
+ blendMode: (p.blendMode ?? 'NORMAL') as BlendMode
+ }
+}
- if (p.type.startsWith('GRADIENT') && p.stops) {
- base.gradientStops = p.stops.map((s) => ({
- color: convertColor(s.color),
- position: s.position
- }))
- if (p.transform) {
- base.gradientTransform = convertGradientTransform(p.transform)
- }
- }
+function applyGradientPaintFields(fill: Fill, p: Paint): void {
+ if (!p.type.startsWith('GRADIENT') || !p.stops) return
+ fill.gradientStops = p.stops.map((s) => ({
+ color: convertColor(s.color),
+ position: s.position
+ }))
+ if (p.transform) fill.gradientTransform = convertGradientTransform(p.transform)
+}
- if (p.type === 'IMAGE') {
- if (p.image && typeof p.image === 'object') {
- const img = p.image as { hash: string | Record<string, number> }
- if (typeof img.hash === 'object') {
- base.imageHash = imageHashToString(img.hash)
- } else if (typeof img.hash === 'string') {
- base.imageHash = img.hash
- }
- }
- base.imageScaleMode = (p.imageScaleMode ?? 'FILL') as ImageScaleMode
- if (p.transform) {
- base.imageTransform = convertGradientTransform(p.transform)
- }
+function applyImagePaintFields(fill: Fill, p: Paint): void {
+ if (p.type !== 'IMAGE') return
+ if (p.image && typeof p.image === 'object') {
+ const img = p.image as { hash: string | Record<string, number> }
+ if (typeof img.hash === 'object') {
+ fill.imageHash = imageHashToString(img.hash)
+ } else if (typeof img.hash === 'string') {
+ fill.imageHash = img.hash
}
+ }
+ fill.imageScaleMode = (p.imageScaleMode ?? 'FILL') as ImageScaleMode
+ if (p.transform) fill.imageTransform = convertGradientTransform(p.transform)
+}
+
+function applySchemaPaintFields(fill: Fill, p: Paint): void {
+ if (p.sourceNodeId) fill.sourceNodeId = guidToString(p.sourceNodeId)
+ if (p.patternSpacing) fill.patternSpacing = p.patternSpacing
+ if (p.patternTileType) fill.patternTileType = p.patternTileType as Fill['patternTileType']
+ if (p.verticalAlignment) fill.verticalAlignment = p.verticalAlignment as Fill['verticalAlignment']
+ if (p.horizontalAlignment)
+ fill.horizontalAlignment = p.horizontalAlignment as Fill['horizontalAlignment']
+ if (p.noiseType) fill.noiseType = p.noiseType as Fill['noiseType']
+ if (p.density !== undefined) fill.density = p.density
+ if (p.noiseSize) fill.noiseSize = p.noiseSize
+ if (p.customEffectId?.guid) fill.customEffectId = guidToString(p.customEffectId.guid)
+}
- return base
+export function convertFills(paints?: Paint[]): Fill[] {
+ if (!paints) return []
+ return paints.map((p) => {
+ const fill = convertBaseFill(p)
+ applyGradientPaintFields(fill, p)
+ applyImagePaintFields(fill, p)
+ applySchemaPaintFields(fill, p)
+ return fill
})
}
diff --git a/packages/core/src/kiwi/fig/node-change/serialize.ts b/packages/core/src/kiwi/fig/node-change/serialize.ts
index 969993bf9..f166b7f61 100644
--- a/packages/core/src/kiwi/fig/node-change/serialize.ts
+++ b/packages/core/src/kiwi/fig/node-change/serialize.ts
@@ -217,6 +217,15 @@ function fillToKiwiPaint(f: SceneNode['fills'][number]): Paint {
if (f.imageHash) paint.image = { hash: hexToBytes(f.imageHash) }
if (f.imageScaleMode) paint.imageScaleMode = f.imageScaleMode
if (f.imageTransform) paint.transform = f.imageTransform
+ if (f.sourceNodeId) paint.sourceNodeId = stringToGuid(f.sourceNodeId)
+ if (f.patternSpacing) paint.patternSpacing = f.patternSpacing
+ if (f.patternTileType) paint.patternTileType = f.patternTileType
+ if (f.verticalAlignment) paint.verticalAlignment = f.verticalAlignment
+ if (f.horizontalAlignment) paint.horizontalAlignment = f.horizontalAlignment
+ if (f.noiseType) paint.noiseType = f.noiseType
+ if (f.density !== undefined) paint.density = f.density
+ if (f.noiseSize) paint.noiseSize = f.noiseSize
+ if (f.customEffectId) paint.customEffectId = { guid: stringToGuid(f.customEffectId) }
return paint
}
diff --git a/packages/core/src/scene-graph/types.ts b/packages/core/src/scene-graph/types.ts
index 3666a148d..b3a1dc46f 100644
--- a/packages/core/src/scene-graph/types.ts
+++ b/packages/core/src/scene-graph/types.ts
@@ -99,6 +99,10 @@ export type FillType =
| 'GRADIENT_ANGULAR'
| 'GRADIENT_DIAMOND'
| 'IMAGE'
+ | 'VIDEO'
+ | 'PATTERN'
+ | 'NOISE'
+ | 'CUSTOM'
export type BlendMode =
| 'NORMAL'
| 'DARKEN'
@@ -118,6 +122,9 @@ export type BlendMode =
| 'LUMINOSITY'
| 'PASS_THROUGH'
export type ImageScaleMode = 'FILL' | 'FIT' | 'CROP' | 'TILE'
+export type NoiseType = 'MULTITONE' | 'MONOTONE' | 'DUOTONE'
+export type PatternTileType = 'RECTANGULAR' | 'HORIZONTAL_HEXAGONAL' | 'VERTICAL_HEXAGONAL'
+export type PatternAlignment = 'START' | 'CENTER' | 'END'
export interface GradientStop {
color: Color
@@ -137,6 +144,15 @@ export interface Fill {
imageHash?: string
imageScaleMode?: ImageScaleMode
imageTransform?: GradientTransform
+ sourceNodeId?: string
+ patternSpacing?: Vector
+ patternTileType?: PatternTileType
+ verticalAlignment?: PatternAlignment
+ horizontalAlignment?: PatternAlignment
+ noiseType?: NoiseType
+ density?: number
+ noiseSize?: Vector
+ customEffectId?: string
}
export type StrokeCap = 'NONE' | 'ROUND' | 'SQUARE' | 'ARROW_LINES' | 'ARROW_EQUILATERAL'
diff --git a/tests/engine/io/fig/export/paint-schema-fields.test.ts b/tests/engine/io/fig/export/paint-schema-fields.test.ts
new file mode 100644
index 000000000..0760478d3
--- /dev/null
+++ b/tests/engine/io/fig/export/paint-schema-fields.test.ts
@@ -0,0 +1,64 @@
+import { describe, expect, test } from 'bun:test'
+
+import { sceneNodeToKiwi } from '#core/kiwi/fig/node-change/serialize'
+import { SceneGraph } from '#core/scene-graph'
+
+describe('Figma paint schema field export', () => {
+ test('exports pattern, noise, and custom fill metadata', () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const node = graph.createNode('RECTANGLE', page.id, {
+ fills: [
+ {
+ type: 'PATTERN',
+ color: { r: 0.2, g: 0.3, b: 0.4, a: 1 },
+ opacity: 0.75,
+ visible: true,
+ sourceNodeId: '12:34',
+ patternSpacing: { x: 8, y: 12 },
+ patternTileType: 'VERTICAL_HEXAGONAL',
+ verticalAlignment: 'CENTER',
+ horizontalAlignment: 'END'
+ },
+ {
+ type: 'NOISE',
+ color: { r: 0.5, g: 0.5, b: 0.5, a: 1 },
+ opacity: 1,
+ visible: true,
+ noiseType: 'MONOTONE',
+ density: 0.25,
+ noiseSize: { x: 14, y: 18 }
+ },
+ {
+ type: 'CUSTOM',
+ color: { r: 0.1, g: 0.1, b: 0.1, a: 1 },
+ opacity: 1,
+ visible: true,
+ customEffectId: '56:78'
+ }
+ ]
+ })
+
+ const changes = sceneNodeToKiwi(node, { sessionID: 1, localID: 1 }, 0, { value: 2 }, graph, [])
+ const fills = changes[0].fillPaints
+
+ expect(fills?.[0]).toMatchObject({
+ type: 'PATTERN',
+ sourceNodeId: { sessionID: 12, localID: 34 },
+ patternSpacing: { x: 8, y: 12 },
+ patternTileType: 'VERTICAL_HEXAGONAL',
+ verticalAlignment: 'CENTER',
+ horizontalAlignment: 'END'
+ })
+ expect(fills?.[1]).toMatchObject({
+ type: 'NOISE',
+ noiseType: 'MONOTONE',
+ density: 0.25,
+ noiseSize: { x: 14, y: 18 }
+ })
+ expect(fills?.[2]).toMatchObject({
+ type: 'CUSTOM',
+ customEffectId: { guid: { sessionID: 56, localID: 78 } }
+ })
+ })
+})
diff --git a/tests/engine/io/fig/import/paint-schema-fields.test.ts b/tests/engine/io/fig/import/paint-schema-fields.test.ts
new file mode 100644
index 000000000..8cbe9c764
--- /dev/null
+++ b/tests/engine/io/fig/import/paint-schema-fields.test.ts
@@ -0,0 +1,72 @@
+import { describe, expect, test } from 'bun:test'
+
+import type { NodeChange } from '#core/kiwi/fig/codec'
+import { nodeChangeToProps } from '#core/kiwi/fig/node-change/convert'
+
+const sourceNodeId = { sessionID: 12, localID: 34 }
+const customEffectId = { guid: { sessionID: 56, localID: 78 } }
+
+describe('Figma paint schema field import', () => {
+ test('imports pattern fill metadata for round-trip and fallback rendering', () => {
+ const props = nodeChangeToProps(
+ {
+ type: 'RECTANGLE',
+ fillPaints: [
+ {
+ type: 'PATTERN',
+ color: { r: 0.2, g: 0.3, b: 0.4, a: 1 },
+ opacity: 0.75,
+ visible: true,
+ sourceNodeId,
+ patternSpacing: { x: 8, y: 12 },
+ patternTileType: 'HORIZONTAL_HEXAGONAL',
+ verticalAlignment: 'CENTER',
+ horizontalAlignment: 'END'
+ }
+ ]
+ } as NodeChange,
+ []
+ )
+
+ expect(props.fills?.[0]).toMatchObject({
+ type: 'PATTERN',
+ opacity: 0.75,
+ sourceNodeId: '12:34',
+ patternSpacing: { x: 8, y: 12 },
+ patternTileType: 'HORIZONTAL_HEXAGONAL',
+ verticalAlignment: 'CENTER',
+ horizontalAlignment: 'END'
+ })
+ })
+
+ test('imports noise and custom fill metadata', () => {
+ const props = nodeChangeToProps(
+ {
+ type: 'RECTANGLE',
+ fillPaints: [
+ {
+ type: 'NOISE',
+ color: { r: 0.5, g: 0.5, b: 0.5, a: 1 },
+ noiseType: 'DUOTONE',
+ density: 0.42,
+ noiseSize: { x: 16, y: 16 }
+ },
+ {
+ type: 'CUSTOM',
+ color: { r: 0.1, g: 0.1, b: 0.1, a: 1 },
+ customEffectId
+ }
+ ]
+ } as NodeChange,
+ []
+ )
+
+ expect(props.fills?.[0]).toMatchObject({
+ type: 'NOISE',
+ noiseType: 'DUOTONE',
+ density: 0.42,
+ noiseSize: { x: 16, y: 16 }
+ })
+ expect(props.fills?.[1]).toMatchObject({ type: 'CUSTOM', customEffectId: '56:78' })
+ })
+})
diff --git a/tests/engine/render/canvas/schema-fill-fallback.test.ts b/tests/engine/render/canvas/schema-fill-fallback.test.ts
new file mode 100644
index 000000000..9cae8f46c
--- /dev/null
+++ b/tests/engine/render/canvas/schema-fill-fallback.test.ts
@@ -0,0 +1,39 @@
+import { describe, expect, mock, test } from 'bun:test'
+
+import { applyFill } from '#core/canvas/fills'
+import type { SkiaRenderer } from '#core/canvas/renderer'
+import { SceneGraph } from '#core/scene-graph'
+import type { Fill, SceneNode } from '#core/scene-graph'
+
+function createRenderer() {
+ return {
+ fillPaint: {
+ setShader: mock(() => undefined),
+ setColor: mock(() => undefined)
+ },
+ ck: {
+ Color4f: mock((r, g, b, a) => ['color', r, g, b, a])
+ },
+ resolveFillColor: mock((fill: Fill) => fill.color)
+ } as SkiaRenderer
+}
+
+const node = { id: '1:2', width: 100, height: 100 } as SceneNode
+
+describe('schema fill fallback rendering', () => {
+ test.each(['PATTERN', 'NOISE', 'CUSTOM'] as const)(
+ 'renders %s fills as solid fallback',
+ (type) => {
+ const renderer = createRenderer()
+ const fill: Fill = {
+ type,
+ color: { r: 0.2, g: 0.3, b: 0.4, a: 0.8 },
+ opacity: 1,
+ visible: true
+ }
+
+ expect(applyFill(renderer, fill, node, new SceneGraph())).toBe(true)
+ expect(renderer.fillPaint.setColor).toHaveBeenCalledWith(['color', 0.2, 0.3, 0.4, 0.8])
+ }
+ )
+})