AI-native design editor. Open-source Figma alternative.
fix(fig): keep a vector point's corner radius and join (#1148)
rcoenenaab10cdfcc07
diff --git a/changes/1148/fixed.md b/changes/1148/fixed.mdnew file mode 100644index 000000000..b214226bf--- /dev/null+++ b/changes/1148/fixed.md@@ -0,0 +1 @@+- Rounded points in a vector imported from a `.fig` file keep their own corner radius and join when the file is saved, and the plugin API reports their radius instead of 0.diff --git a/packages/fig/src/node-change/vector/network.ts b/packages/fig/src/node-change/vector/network.tsindex d86f8bbea..27a336c87 100644--- a/packages/fig/src/node-change/vector/network.ts+++ b/packages/fig/src/node-change/vector/network.ts@@ -12,6 +12,8 @@ export interface StyleOverride {styleID: numberhandleMirroring?: stringstrokeCap?: string+ strokeJoin?: string+ cornerRadius?: numberfillPaints?: Paint[]}@@ -47,6 +49,8 @@ export function decodeVectorNetworkBlob(handleMirroring: (style?.handleMirroring as HandleMirroring | undefined) ?? 'NONE'}if (style?.strokeCap) vertex.strokeCap = style.strokeCap+ if (style?.strokeJoin) vertex.strokeJoin = style.strokeJoin+ if (style?.cornerRadius !== undefined) vertex.cornerRadius = style.cornerRadiusvertices.push(vertex)}@@ -75,7 +79,9 @@ export function decodeVectorNetworkBlob(const regions: VectorRegion[] = []for (let index = 0; index < regionCount; index++) {- const windingRule: WindingRule = view.getUint32(offset, true) === 0 ? 'EVENODD' : 'NONZERO'+ // The low bit is the winding rule; the rest is the region's style ID, which names its fill.+ const windingRule: WindingRule =+ (view.getUint32(offset, true) & 1) === 1 ? 'NONZERO' : 'EVENODD'offset += 4const loopCount = view.getUint32(offset, true)offset += 4@@ -97,7 +103,12 @@ export function decodeVectorNetworkBlob(}function vertexStyleKey(vertex: VectorVertex): string {- return `${vertex.handleMirroring ?? 'NONE'}|${vertex.strokeCap ?? ''}`+ return [+ vertex.handleMirroring ?? 'NONE',+ vertex.strokeCap ?? '',+ vertex.strokeJoin ?? '',+ vertex.cornerRadius ?? ''+ ].join('|')}export function buildStyleOverrideTable(network: VectorNetwork): {@@ -109,13 +120,21 @@ export function buildStyleOverrideTable(network: VectorNetwork): {let nextId = 1for (const vertex of network.vertices) {const mirroring = vertex.handleMirroring ?? 'NONE'- if (mirroring === 'NONE' && !vertex.strokeCap) continue+ if (+ mirroring === 'NONE' &&+ !vertex.strokeCap &&+ !vertex.strokeJoin &&+ vertex.cornerRadius === undefined+ )+ continueconst key = vertexStyleKey(vertex)if (styleToId.has(key)) continuestyleToId.set(key, nextId)const entry: StyleOverride = { styleID: nextId }if (mirroring !== 'NONE') entry.handleMirroring = mirroringif (vertex.strokeCap) entry.strokeCap = vertex.strokeCap+ if (vertex.strokeJoin) entry.strokeJoin = vertex.strokeJoin+ if (vertex.cornerRadius !== undefined) entry.cornerRadius = vertex.cornerRadiustable.push(entry)nextId++}diff --git a/tests/engine/vector/blob/roundtrip.test.ts b/tests/engine/vector/blob/roundtrip.test.tsindex aac948f63..707a9c26d 100644--- a/tests/engine/vector/blob/roundtrip.test.ts+++ b/tests/engine/vector/blob/roundtrip.test.ts@@ -137,3 +137,49 @@ describe('vertex stroke caps in the style override table', () => {expect(decoded.vertices[1].handleMirroring).toBe('ANGLE')})})++describe('vertex corners in the style override table', () => {+ test('a per-vertex corner radius and join round-trip', () => {+ const network = vectorNetwork(+ [+ { ...vectorVertex(0, 0), cornerRadius: 8, strokeJoin: 'ROUND' },+ vectorVertex(100, 0),+ { ...vectorVertex(100, 100), cornerRadius: 4 }+ ],+ [vectorSegment(0, 1), vectorSegment(1, 2)]+ )+ const { table, styleToId } = buildStyleOverrideTable(network)+ const decoded = decodeVectorNetworkBlob(encodeVectorNetworkBlob(network, styleToId), table)+ expect(decoded.vertices.map((vertex) => vertex.cornerRadius)).toEqual([8, undefined, 4])+ expect(decoded.vertices[0].strokeJoin).toBe('ROUND')+ })++ test('an explicit zero radius survives, so a rounded layer keeps that point sharp', () => {+ const network = vectorNetwork(+ [{ ...vectorVertex(0, 0), cornerRadius: 0 }, vectorVertex(100, 0), vectorVertex(100, 100)],+ [vectorSegment(0, 1), vectorSegment(1, 2)]+ )+ const { table, styleToId } = buildStyleOverrideTable(network)+ expect(table).toEqual([{ styleID: 1, cornerRadius: 0 }])+ const decoded = decodeVectorNetworkBlob(encodeVectorNetworkBlob(network, styleToId), table)+ expect(decoded.vertices.map((vertex) => vertex.cornerRadius)).toEqual([0, undefined, undefined])+ })+})++describe('region style words', () => {+ /** A one-region triangle blob whose region word is `word`. */+ function regionBlob(word: number) {+ const blob = encodeVectorNetworkBlob(triangleNetwork())+ const view = new DataView(blob.buffer)+ // Header 12 bytes, three 12-byte vertices and three 28-byte segments come before the region.+ view.setUint32(12 + 3 * 12 + 3 * 28, word, true)+ return blob+ }++ test('the winding rule is the low bit, under the region’s style ID', () => {+ // Style 3 with even-odd winding, then with nonzero winding. Every styled region in the fixtures+ // is nonzero, so the even-odd case follows the bit layout rather than a Figma file.+ expect(decodeVectorNetworkBlob(regionBlob((3 << 1) | 0)).regions[0].windingRule).toBe('EVENODD')+ expect(decodeVectorNetworkBlob(regionBlob((3 << 1) | 1)).regions[0].windingRule).toBe('NONZERO')+ })+})