AI-native design editor. Open-source Figma alternative.
fix(fig): give exported vector regions their fill style (#1183)
rcoenen5d3e08516e41
diff --git a/changes/1183/fixed.md b/changes/1183/fixed.mdnew file mode 100644index 000000000..250f40f35--- /dev/null+++ b/changes/1183/fixed.md@@ -0,0 +1 @@+- Vectors whose regions have their own colors keep them in Figma's vector edit mode after a `.fig` export.diff --git a/packages/fig/src/node-change/serialize.ts b/packages/fig/src/node-change/serialize.tsindex 53198f5e3..2bd9dcaf5 100644--- a/packages/fig/src/node-change/serialize.ts+++ b/packages/fig/src/node-change/serialize.ts@@ -363,33 +363,38 @@ function serializeGeometry(node: SceneNode, nc: KiwiNodeChange, blobs: Uint8Arraif (node.maskIsOutline) nc.maskIsOutline = true}- let styleOverrides: StyleOverride[] = []+ const network = node.type === 'VECTOR' ? node.vectorNetwork : null+ const { table: styleOverrides, styleToId } = network+ ? buildStyleOverrideTable(network)+ : { table: [] as StyleOverride[], styleToId: undefined }+ // A fill geometry entry with its own paints gets a style override of its own.+ const fillStyleIds = node.fillGeometry.map((geometry) => {+ if (!geometry.fills || geometry.fills.length === 0) return 0+ const styleID = styleOverrides.length + 1+ styleOverrides.push({ styleID, fillPaints: geometry.fills.map(fillToKiwiPaint) })+ return styleID+ })+const vectorData: Record<string, unknown> = {}- if (node.vectorNetwork && node.type === 'VECTOR') {- const { table, styleToId } = buildStyleOverrideTable(node.vectorNetwork)- styleOverrides = table+ if (network) {+ // fillGeometry[i] is region i's fill when the counts match (see regenerateFillGeometry), and+ // Figma gives both the same style ID: its edit mode refills regions from the network alone.+ const regionStyleIds = network.regions.length === fillStyleIds.length ? fillStyleIds : undefinedconst blobIdx = blobs.length- blobs.push(encodeVectorNetworkBlob(node.vectorNetwork, styleToId))+ blobs.push(encodeVectorNetworkBlob(network, styleToId, regionStyleIds))vectorData.vectorNetworkBlob = blobIdxvectorData.normalizedSize = { x: node.width, y: node.height }}if (node.fillGeometry.length > 0) {- nc.fillGeometry = node.fillGeometry.map((geometry) => {+ nc.fillGeometry = node.fillGeometry.map((geometry, index) => {const blobIdx = blobs.lengthblobs.push(geometry.commandsBlob)- if (!geometry.fills || geometry.fills.length === 0) {- return {- windingRule: toKiwiWindingRule(geometry.windingRule),- commandsBlob: blobIdx- }- }- const styleID = styleOverrides.length + 1- styleOverrides.push({ styleID, fillPaints: geometry.fills.map(fillToKiwiPaint) })+ const styleID = fillStyleIds[index]return {windingRule: toKiwiWindingRule(geometry.windingRule),commandsBlob: blobIdx,- styleID+ ...(styleID ? { styleID } : {})}})}diff --git a/packages/fig/src/node-change/vector/network.ts b/packages/fig/src/node-change/vector/network.tsindex bececce83..3fea880b3 100644--- a/packages/fig/src/node-change/vector/network.ts+++ b/packages/fig/src/node-change/vector/network.ts@@ -151,9 +151,14 @@ export function buildStyleOverrideTable(network: VectorNetwork): {return { table, styleToId }}+/**+ * Encodes a network as Figma's vector network blob. `regionStyleIds[i]` names the style override+ * that fills region `i`; Figma rebuilds a region's fill from it when the vector is edited.+ */export function encodeVectorNetworkBlob(network: VectorNetwork,- styleToId?: Map<string, number>+ styleToId?: Map<string, number>,+ regionStyleIds?: readonly number[]): Uint8Array {let regionBytes = 0for (const region of network.regions) {@@ -199,8 +204,10 @@ export function encodeVectorNetworkBlob(offset += 4}- for (const region of network.regions) {- view.setUint32(offset, region.windingRule === 'EVENODD' ? 0 : 1, true)+ for (const [index, region] of network.regions.entries()) {+ // The low bit is the winding rule; the rest is the region's style ID.+ const styleID = regionStyleIds?.[index] ?? 0+ view.setUint32(offset, (styleID << 1) | (region.windingRule === 'EVENODD' ? 0 : 1), true)offset += 4view.setUint32(offset, region.loops.length, true)offset += 4diff --git a/packages/fig/tests/node-change/vector/region-fills.test.ts b/packages/fig/tests/node-change/vector/region-fills.test.tsnew file mode 100644index 000000000..490e674a8--- /dev/null+++ b/packages/fig/tests/node-change/vector/region-fills.test.ts@@ -0,0 +1,72 @@+import { describe, expect, test } from 'bun:test'++import { sceneNodeToKiwi } from '#fig/node-change/index'++import { SceneGraph } from '@open-pencil/scene-graph'++const ROOT_GUID = { sessionID: 1, localID: 0 }++const zero = { x: 0, y: 0 }+const line = (start: number, end: number) => ({ start, end, tangentStart: zero, tangentEnd: zero })+const solid = (r: number) => ({+ type: 'SOLID' as const,+ color: { r, g: 0, b: 0, a: 1 },+ opacity: 1,+ visible: true+})++/** The style IDs in a vector network blob's region words, which keep the winding in bit 0. */+function regionStyleIds(blob: Uint8Array): number[] {+ const view = new DataView(blob.buffer, blob.byteOffset, blob.byteLength)+ const regions = view.getUint32(8, true)+ let offset = 12 + view.getUint32(0, true) * 12 + view.getUint32(4, true) * 28+ const ids: number[] = []+ for (let index = 0; index < regions; index++) {+ ids.push(view.getUint32(offset, true) >> 1)+ offset += 4+ const loops = view.getUint32(offset, true)+ offset += 4+ for (let loop = 0; loop < loops; loop++) offset += 4 + view.getUint32(offset, true) * 4+ }+ return ids+}++describe('a vector whose regions have their own colors', () => {+ test('names each region’s style in the network, as Figma does, so its edit mode keeps them', () => {+ const graph = new SceneGraph()+ // Two triangles sharing an edge, one red and one plain.+ const node = graph.createNode('VECTOR', graph.getPages()[0].id, {+ width: 20,+ height: 10,+ vectorNetwork: {+ vertices: [+ { x: 0, y: 0 },+ { x: 10, y: 0 },+ { x: 10, y: 10 },+ { x: 20, y: 0 }+ ],+ segments: [line(0, 1), line(1, 2), line(2, 0), line(1, 3), line(3, 2)],+ regions: [+ { windingRule: 'NONZERO', loops: [[0, 1, 2]] },+ { windingRule: 'EVENODD', loops: [[3, 4, 1]] }+ ]+ },+ fillGeometry: [+ { windingRule: 'NONZERO', commandsBlob: new Uint8Array([0]), fills: [solid(1)] },+ { windingRule: 'EVENODD', commandsBlob: new Uint8Array([0]) }+ ]+ })+ const blobs: Uint8Array[] = []+ const [change] = sceneNodeToKiwi(node, ROOT_GUID, 0, { value: 100 }, graph, blobs)+ const vectorData = change.vectorData as {+ vectorNetworkBlob: number+ styleOverrideTable: Array<{ styleID: number }>+ }++ const fillStyle = change.fillGeometry?.[0].styleID ?? 0+ expect(fillStyle).toBeGreaterThan(0)+ expect(vectorData.styleOverrideTable.map((entry) => entry.styleID)).toContain(fillStyle)+ // The red region takes the red fill's style; the plain one takes the layer's fill.+ expect(regionStyleIds(blobs[vectorData.vectorNetworkBlob])).toEqual([fillStyle, 0])+ })+})