Skip to content

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

fix(kiwi): preserve unrelated raw metadata

Danila Poyarkov9ee36efa273b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7e3810af5..e1962a664 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -42,6 +42,7 @@
### Fixes
+- Preserve unrelated Figma prototype, library, export, and raw metadata when editing modeled `.fig` fields, while still overriding stale field-specific payloads.
- Make canvas text rendering demand missing font faces and verify CJK/Arabic fallback coverage from CanvasKit shaping results instead of coarse script predictions.
- Resolve fonts before loaded, pasted, imported, and tool-created nodes render; invalidate generation-stale text caches and use baked `.fig` glyphs only after live font resolution is exhausted.
- Load character-specific remote font subsets without Latin-only assumptions, preserve cumulative subset coverage, and reject unavailable desktop font styles instead of substituting the first family face.
diff --git a/packages/core/src/kiwi/fig/node-change/export-node.ts b/packages/core/src/kiwi/fig/node-change/export-node.ts
index da3c52133..6d4d145b6 100644
--- a/packages/core/src/kiwi/fig/node-change/export-node.ts
+++ b/packages/core/src/kiwi/fig/node-change/export-node.ts
@@ -351,7 +351,6 @@ const RAW_FIELDS_OVERRIDE_BLOCKLIST = new Set([
'pageType',
'derivedSymbolData',
'derivedSymbolDataLayoutVersion',
- 'componentPropAssignments',
'sourceLibraryKey',
// Variable consumption maps: explicit serialization always sets these when
// bindings exist, and our VARIABLE_BINDING_FIELDS mapping may produce different
@@ -539,6 +538,15 @@ function findComponentPropertyDefinition(graph: SceneGraph, id: string) {
return undefined
}
+function shouldSerializeRawBackedField(
+ node: SceneNode,
+ rawField: string,
+ hasValue: boolean,
+ alreadySerialized = false
+): boolean {
+ return hasValue && !(rawField in node.source.fig.rawNodeFields) && !alreadySerialized
+}
+
function applyComponentMetadata(
context: SceneNodeToKiwiContext,
node: SceneNode,
@@ -572,7 +580,9 @@ function applyComponentMetadata(
: null
})
.filter((def): def is NonNullable<typeof def> => def !== null)
- if (componentPropDefs.length > 0) nc.componentPropDefs = componentPropDefs
+ if (shouldSerializeRawBackedField(node, 'componentPropDefs', componentPropDefs.length > 0)) {
+ nc.componentPropDefs = componentPropDefs
+ }
const componentPropRefs = node.componentPropertyReferences
.map((ref) => {
@@ -581,7 +591,9 @@ function applyComponentMetadata(
return { defID, componentPropNodeField: componentPropertyNodeField(ref.field) }
})
.filter((ref): ref is NonNullable<typeof ref> => ref !== null)
- if (componentPropRefs.length > 0) nc.componentPropRefs = componentPropRefs
+ if (shouldSerializeRawBackedField(node, 'componentPropRefs', componentPropRefs.length > 0)) {
+ nc.componentPropRefs = componentPropRefs
+ }
const componentPropAssignments = Object.entries(node.componentPropertyAssignments)
.map(([propertyId, value]) => {
@@ -595,7 +607,14 @@ function applyComponentMetadata(
: null
})
.filter((assignment): assignment is NonNullable<typeof assignment> => assignment !== null)
- if (componentPropAssignments.length > 0) {
+ if (
+ shouldSerializeRawBackedField(
+ node,
+ 'componentPropAssignments',
+ componentPropAssignments.length > 0,
+ Boolean(nc.componentPropAssignments)
+ )
+ ) {
nc.componentPropAssignments = componentPropAssignments
}
@@ -605,7 +624,9 @@ function applyComponentMetadata(
return propDefId ? { propDefId, value: spec.value } : null
})
.filter((spec): spec is NonNullable<typeof spec> => spec !== null)
- if (variantPropSpecs.length > 0) nc.variantPropSpecs = variantPropSpecs
+ if (shouldSerializeRawBackedField(node, 'variantPropSpecs', variantPropSpecs.length > 0)) {
+ nc.variantPropSpecs = variantPropSpecs
+ }
}
function exportNodeSize(node: SceneNode): Vector {
@@ -669,9 +690,7 @@ function applySharedStyleProps(node: SceneNode, nc: KiwiNodeChange): void {
if (node.textStyleId) nc.styleIdForText = { guid: stringToGuid(node.textStyleId) }
if (node.effectStyleId) nc.styleIdForEffect = { guid: stringToGuid(node.effectStyleId) }
if (node.gridStyleId) nc.styleIdForGrid = { guid: stringToGuid(node.gridStyleId) }
- if (node.layoutGrids.length > 0 || 'layoutGrids' in node.source.fig.rawNodeFields) {
- nc.layoutGrids = structuredClone(node.layoutGrids)
- }
+ if (node.layoutGrids.length > 0) nc.layoutGrids = structuredClone(node.layoutGrids)
}
function applyNodeVisualProps(
diff --git a/packages/docs/development/roadmap.md b/packages/docs/development/roadmap.md
index be5157eac..ef5bc0500 100644
--- a/packages/docs/development/roadmap.md
+++ b/packages/docs/development/roadmap.md
@@ -189,7 +189,7 @@ OpenPencil deliberately preserves many Figma/Kiwi fields even when they are not
| ----------------------------------------------------------------------- | ------------: | -------: | --: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `source.fig.rawSize` | ✅ | Indirect | — | Preserves original Figma size for round-trip. Cleared when size is edited. |
| `source.fig.rawTransform` | ✅ | Indirect | — | Preserves exact Figma transform. Cleared when transform is edited. |
-| `source.fig.rawNodeFields` | ✅ | Mixed | — | Late-applied to exported NodeChange for round-trip fidelity; raw-field and schema coverage tests guard preservation drift. |
+| `source.fig.rawNodeFields` | ✅ | Mixed | — | Late-applied to exported NodeChange for round-trip fidelity; modeled edits invalidate only matching stale raw fields, while unrelated prototype/library metadata survives. Raw-field and schema coverage tests guard preservation drift. |
| `source.fig.layout` | ✅ | ✅ | ◐ | Preserves original Figma stack metadata while using normalized layout fields. |
| `source.fig.symbolOverrides` | ✅ | Indirect | — | Important for instance override fidelity. |
| `source.fig.componentPropAssignments` | ✅ | Indirect | ◐ | Used for component property fidelity; not raw-editable. |
@@ -223,11 +223,11 @@ These are parsed or visible in Figma docs and most likely to cause visible diffe
| Concern | Files |
| ---------------------------- | -------------------------------------------------------------------------------------------------------------- |
-| Scene graph fields | `packages/core/src/scene-graph/types.ts` |
-| Source metadata invalidation | `packages/core/src/scene-graph/source-metadata.ts` |
+| Scene graph fields | `packages/scene-graph/src/types.ts` |
+| Source metadata invalidation | `packages/scene-graph/src/source-metadata.ts` |
| Kiwi import mapping | `packages/core/src/kiwi/fig/node-change/convert.ts` |
| Kiwi export mapping | `packages/core/src/kiwi/fig/node-change/export-node.ts`, `packages/core/src/kiwi/fig/node-change/serialize.ts` |
-| Kiwi schema | `packages/core/src/kiwi/fig/codec/schema/fig.kiwi`, `tests/engine/io/fig/import/schema-coverage.test.ts` |
+| Kiwi schema | `packages/kiwi/src/fig/schema/fig.kiwi`, `tests/engine/io/fig/import/schema-coverage.test.ts` |
| Renderer dispatch | `packages/core/src/canvas/scene.ts` |
| Fills / images / gradients | `packages/core/src/canvas/fills.ts` |
| Strokes | `packages/core/src/canvas/strokes.ts` |
diff --git a/packages/scene-graph/src/source-metadata.ts b/packages/scene-graph/src/source-metadata.ts
index d1c8d9241..99fb3e6fe 100644
--- a/packages/scene-graph/src/source-metadata.ts
+++ b/packages/scene-graph/src/source-metadata.ts
@@ -6,109 +6,87 @@ const RAW_SIZE_KEYS = new Set(['width', 'height'])
const RAW_TRANSFORM_KEYS = new Set(['x', 'y', 'rotation', 'flipX', 'flipY'])
-const EDITED_RAW_FIELDS: Partial<Record<string, string>> = {
- fillStyleId: 'styleIdForFill',
- strokeStyleId: 'styleIdForStrokeFill',
- textStyleId: 'styleIdForText',
- effectStyleId: 'styleIdForEffect',
- gridStyleId: 'styleIdForGrid',
- componentPropertyDefinitions: 'componentPropDefs',
- componentPropertyReferences: 'componentPropRefs',
- componentPropertyAssignments: 'componentPropAssignments'
-}
+const TEXT_DERIVED_RAW_FIELDS = [
+ 'textData',
+ 'derivedTextData',
+ 'textUserLayoutVersion',
+ 'textExplicitLayoutVersion'
+] as const
+
+const STROKE_GEOMETRY_RAW_FIELDS = ['strokeGeometry', 'vectorData'] as const
-const RAW_NODE_FIELD_KEYS = new Set([
- 'visible',
- 'opacity',
- 'blendMode',
- 'fills',
- 'strokes',
- 'borderTopWeight',
- 'borderRightWeight',
- 'borderBottomWeight',
- 'borderLeftWeight',
- 'independentStrokeWeights',
- 'effects',
- 'layoutGrids',
- 'cornerRadius',
- 'topLeftRadius',
- 'topRightRadius',
- 'bottomRightRadius',
- 'bottomLeftRadius',
- 'independentCorners',
- 'cornerSmoothing',
- 'text',
- 'fontSize',
- 'fontFamily',
- 'fontWeight',
- 'italic',
- 'textAlignHorizontal',
- 'textAlignVertical',
- 'textAutoResize',
- 'textCase',
- 'textDecoration',
- 'textDecorationStyle',
- 'textDecorationThickness',
- 'textDecorationFills',
- 'textDecorationSkipInk',
- 'textUnderlineOffset',
- 'lineHeight',
- 'leadingTrim',
- 'letterSpacing',
- 'maxLines',
- 'styleRuns',
- 'fontVariations',
- 'fontFeatures',
- 'textTruncation',
- 'layoutMode',
- 'itemSpacing',
- 'paddingTop',
- 'paddingBottom',
- 'paddingLeft',
- 'paddingRight',
- 'primaryAxisSizing',
- 'counterAxisSizing',
- 'primaryAxisAlign',
- 'counterAxisAlign',
- 'layoutWrap',
- 'counterAxisSpacing',
- 'layoutPositioning',
- 'layoutGrow',
- 'layoutAlignSelf',
- 'counterAxisAlignContent',
- 'itemReverseZIndex',
- 'strokesIncludedInLayout',
- 'layoutDirection',
- 'horizontalConstraint',
- 'verticalConstraint',
- 'strokeCap',
- 'strokeJoin',
- 'strokeMiterLimit',
- 'dashPattern',
- 'arcData',
- 'vectorNetwork',
- 'fillGeometry',
- 'strokeGeometry',
- 'isMask',
- 'maskType',
- 'maskIsOutline',
- 'clipsContent'
-])
+const EDITED_RAW_FIELDS: Partial<Record<string, readonly string[]>> = {
+ fillStyleId: ['styleIdForFill'],
+ strokeStyleId: ['styleIdForStrokeFill'],
+ textStyleId: ['styleIdForText'],
+ effectStyleId: ['styleIdForEffect'],
+ gridStyleId: ['styleIdForGrid'],
+ fills: ['fillPaints', 'backgroundPaints', 'backgroundColor'],
+ strokes: ['strokePaints'],
+ effects: ['effects'],
+ layoutGrids: ['layoutGrids'],
+ exportSettings: ['exportSettings'],
+ borderTopWeight: ['borderTopWeight', ...STROKE_GEOMETRY_RAW_FIELDS],
+ borderRightWeight: ['borderRightWeight', ...STROKE_GEOMETRY_RAW_FIELDS],
+ borderBottomWeight: ['borderBottomWeight', ...STROKE_GEOMETRY_RAW_FIELDS],
+ borderLeftWeight: ['borderLeftWeight', ...STROKE_GEOMETRY_RAW_FIELDS],
+ independentStrokeWeights: [
+ 'borderStrokeWeightsIndependent',
+ 'borderTopWeight',
+ 'borderRightWeight',
+ 'borderBottomWeight',
+ 'borderLeftWeight',
+ ...STROKE_GEOMETRY_RAW_FIELDS
+ ],
+ strokeWeight: ['strokeWeight', ...STROKE_GEOMETRY_RAW_FIELDS],
+ strokeJoin: ['strokeJoin', ...STROKE_GEOMETRY_RAW_FIELDS],
+ strokeMiterLimit: ['miterLimit', ...STROKE_GEOMETRY_RAW_FIELDS],
+ strokeCap: [...STROKE_GEOMETRY_RAW_FIELDS],
+ dashPattern: [...STROKE_GEOMETRY_RAW_FIELDS],
+ text: [...TEXT_DERIVED_RAW_FIELDS],
+ styleRuns: [...TEXT_DERIVED_RAW_FIELDS],
+ fontSize: ['fontSize', ...TEXT_DERIVED_RAW_FIELDS],
+ fontFamily: ['fontName', 'fontVersion', ...TEXT_DERIVED_RAW_FIELDS],
+ fontWeight: ['semanticWeight', ...TEXT_DERIVED_RAW_FIELDS],
+ italic: ['semanticItalic', ...TEXT_DERIVED_RAW_FIELDS],
+ lineHeight: ['lineHeight', ...TEXT_DERIVED_RAW_FIELDS],
+ letterSpacing: ['letterSpacing', 'textTracking', ...TEXT_DERIVED_RAW_FIELDS],
+ textAutoResize: ['textAutoResize', ...TEXT_DERIVED_RAW_FIELDS],
+ textDecorationStyle: ['textDecorationStyle', ...TEXT_DERIVED_RAW_FIELDS],
+ textDecorationThickness: ['textDecorationThickness', ...TEXT_DERIVED_RAW_FIELDS],
+ textDecorationFills: ['textDecorationFillPaints', ...TEXT_DERIVED_RAW_FIELDS],
+ textUnderlineOffset: ['textUnderlineOffset', ...TEXT_DERIVED_RAW_FIELDS],
+ leadingTrim: ['leadingTrim', ...TEXT_DERIVED_RAW_FIELDS],
+ maxLines: ['maxLines', ...TEXT_DERIVED_RAW_FIELDS],
+ fontVariations: ['fontVariations', ...TEXT_DERIVED_RAW_FIELDS],
+ fontFeatures: [
+ 'fontVariantCommonLigatures',
+ 'fontVariantContextualLigatures',
+ 'toggledOnOTFeatures',
+ 'toggledOffOTFeatures',
+ ...TEXT_DERIVED_RAW_FIELDS
+ ],
+ minWidth: ['minSize'],
+ minHeight: ['minSize'],
+ maxWidth: ['maxSize'],
+ maxHeight: ['maxSize'],
+ vectorNetwork: ['vectorData', 'fillGeometry', 'strokeGeometry'],
+ fillGeometry: ['fillGeometry', 'vectorData'],
+ strokeGeometry: ['strokeGeometry', 'vectorData'],
+ isMask: ['mask'],
+ maskType: ['maskType'],
+ maskIsOutline: ['maskIsOutline'],
+ componentPropertyDefinitions: ['componentPropDefs'],
+ componentPropertyReferences: ['componentPropRefs'],
+ componentPropertyAssignments: ['componentPropAssignments'],
+ variantPropSpecs: ['variantPropSpecs']
+}
export function clearEditedSourceMetadata(node: SceneNode, changeKeys: string[]): void {
- const editedRawFields = changeKeys
- .map((key) => EDITED_RAW_FIELDS[key])
- .filter((field): field is string => field !== undefined)
+ const editedRawFields = [...new Set(changeKeys.flatMap((key) => EDITED_RAW_FIELDS[key] ?? []))]
if (editedRawFields.length > 0) {
node.source.fig.rawNodeFields = omit(node.source.fig.rawNodeFields, editedRawFields)
}
if (changeKeys.some((key) => RAW_SIZE_KEYS.has(key))) node.source.fig.rawSize = null
if (changeKeys.some((key) => RAW_TRANSFORM_KEYS.has(key))) node.source.fig.rawTransform = null
- if (changeKeys.some((key) => RAW_NODE_FIELD_KEYS.has(key))) node.source.fig.rawNodeFields = {}
- // Export settings are persisted via plugin data, not RAW_NODE_FIELD_KEYS. Once the
- // user edits them (including clearing every row) drop the raw native exportSettings
- // so they don't resurrect from the import fallback (extractExportSettings) on reopen.
- if (changeKeys.includes('exportSettings')) {
- delete node.source.fig.rawNodeFields.exportSettings
- }
}
diff --git a/tests/engine/io/fig/import/schema-coverage.test.ts b/tests/engine/io/fig/import/schema-coverage.test.ts
index 9e6fa461e..9b1ffbb87 100644
--- a/tests/engine/io/fig/import/schema-coverage.test.ts
+++ b/tests/engine/io/fig/import/schema-coverage.test.ts
@@ -377,9 +377,9 @@ describe('Figma Kiwi schema coverage', () => {
modeled: 112,
schemaTag: 60,
internalBookkeeping: 17,
- rawPreserved: 52,
+ rawPreserved: 53,
styleLibraryMetadata: 39,
- componentInstanceMetadata: 34,
+ componentInstanceMetadata: 33,
textMetadata: 23,
slideFigjamMetadata: 39,
visualGeometryMetadata: 38,
diff --git a/tests/engine/io/fig/roundtrip/exhaustive.test.ts b/tests/engine/io/fig/roundtrip/exhaustive.test.ts
index 7bd55a8f2..9997143e4 100644
--- a/tests/engine/io/fig/roundtrip/exhaustive.test.ts
+++ b/tests/engine/io/fig/roundtrip/exhaustive.test.ts
@@ -64,8 +64,8 @@ const SPECS: FixtureSpec[] = [
thumbnailHeight: 239,
imageCount: 3,
figKiwiVersion: 101,
- g1ExportSize: 496909,
- g2ExportSize: 496909
+ g1ExportSize: 595516,
+ g2ExportSize: 595516
}
]
diff --git a/tests/engine/io/fig/roundtrip/source-metadata.test.ts b/tests/engine/io/fig/roundtrip/source-metadata.test.ts
index aa221aea3..80eafed33 100644
--- a/tests/engine/io/fig/roundtrip/source-metadata.test.ts
+++ b/tests/engine/io/fig/roundtrip/source-metadata.test.ts
@@ -234,12 +234,13 @@ describe('fig roundtrip source metadata', () => {
expect(exported?.transitionInfo?.type).toBe('DISSOLVE')
})
- test('clears imported raw metadata when visual fields are edited', () => {
+ test('preserves unrelated raw metadata when visual fields are edited', () => {
const graph = new SceneGraph()
const page = graph.getPages()[0]
const frame = graph.createNode('FRAME', page.id, { name: 'Edited metadata frame' })
frame.source.format = 'fig'
frame.source.id = '4:506'
+ frame.source.fig.rawNodeFields.fillPaints = [{ type: 'SOLID' }]
frame.source.fig.rawNodeFields.layoutGrids = [{ type: 'MIN', axis: 'X', visible: true }]
frame.source.fig.rawNodeFields.exportSettings = [{ suffix: '@2x' }]
frame.source.fig.rawNodeFields.prototypeInteractions = [{ trigger: 'ON_CLICK' }]
@@ -255,7 +256,11 @@ describe('fig roundtrip source metadata', () => {
]
})
- expect(frame.source.fig.rawNodeFields).toEqual({})
+ expect(frame.source.fig.rawNodeFields).toEqual({
+ layoutGrids: [{ type: 'MIN', axis: 'X', visible: true }],
+ exportSettings: [{ suffix: '@2x' }],
+ prototypeInteractions: [{ trigger: 'ON_CLICK' }]
+ })
})
test('preserves imported unsupported effect payloads for round-trip', async () => {
@@ -354,7 +359,9 @@ describe('fig roundtrip source metadata', () => {
borderLeftWeight: 8
})
- expect(rect.source.fig.rawNodeFields).toEqual({})
+ expect(rect.source.fig.rawNodeFields).toEqual({
+ fillGeometry: [{ windingRule: 'NONZERO', commands: [] }]
+ })
})
test('clears raw font variation payloads when normalized axes are edited', async () => {