AI-native design editor. Open-source Figma alternative.
fix(fig): scale instance stroke weights
Danila Poyarkovedf28f365910
diff --git a/packages/core/src/kiwi/instance-overrides/constraints.ts b/packages/core/src/kiwi/instance-overrides/constraints.tsindex 689788498..4f1cb7bcb 100644--- a/packages/core/src/kiwi/instance-overrides/constraints.ts+++ b/packages/core/src/kiwi/instance-overrides/constraints.ts@@ -28,7 +28,9 @@ export function applyConstraintScaling(ctx: OverrideContext): void {const sy = node.height / basis.heightif (Math.abs(sx - 1) < 0.001 && Math.abs(sy - 1) < 0.001) continue- scaleChildren(graph, node, comp, sx, sy, scaled, basis !== comp)+ const figmaId = ctx.nodeIdToGuid.get(node.id)+ const strokeScale = figmaId ? ctx.changeMap.get(figmaId)?.strokeWeight : undefined+ scaleChildren(graph, node, comp, sx, sy, scaled, basis !== comp, strokeScale)}if (scaled.size > 0) propagateScaling(ctx, scaled)@@ -91,12 +93,19 @@ function scaleVectorNetwork(}}-function scaledStrokes(source: SceneNode, child: SceneNode, shapeScaleX: number, shapeScaleY: number) {+function scaledStrokes(+ source: SceneNode,+ child: SceneNode,+ shapeScaleX: number,+ shapeScaleY: number,+ strokeScale?: number+) {if (source.strokes.length !== child.strokes.length) return undefinedif (Math.abs(shapeScaleX - shapeScaleY) >= 0.001) return undefined+ const scale = strokeScale ?? 1return child.strokes.map((stroke, strokeIndex) => ({...stroke,- weight: source.strokes[strokeIndex].weight+ weight: source.strokes[strokeIndex].weight * scale}))}@@ -107,7 +116,8 @@ function scaleChildren(sx: number,sy: number,scaled: Set<string>,- useCurrentChildAsSource = false+ useCurrentChildAsSource = false,+ strokeScale?: number): void {const len = Math.min(instance.childIds.length, comp.childIds.length)for (let i = 0; i < len; i++) {@@ -140,7 +150,7 @@ function scaleChildren(if (source.vectorNetwork) {updates.vectorNetwork = scaleVectorNetwork(source.vectorNetwork, shapeScaleX, shapeScaleY)}- updates.strokes = scaledStrokes(source, child, shapeScaleX, shapeScaleY)+ updates.strokes = scaledStrokes(source, child, shapeScaleX, shapeScaleY, strokeScale)graph.updateNode(child.id, updates)scaled.add(child.id)@@ -152,7 +162,8 @@ function scaleChildren(hScale ? sx : 1,vScale ? sy : 1,scaled,- useCurrentChildAsSource+ useCurrentChildAsSource,+ strokeScale)}}diff --git a/packages/core/src/kiwi/instance-overrides/types.ts b/packages/core/src/kiwi/instance-overrides/types.tsindex 8638a1d01..7cf5f1693 100644--- a/packages/core/src/kiwi/instance-overrides/types.ts+++ b/packages/core/src/kiwi/instance-overrides/types.ts@@ -78,6 +78,7 @@ export interface InstanceNodeChange {fillPaints?: NodeChange['fillPaints']fillGeometry?: Array<{ windingRule?: string; commandsBlob?: number }>strokeGeometry?: Array<{ windingRule?: string; commandsBlob?: number }>+ strokeWeight?: numberderivedSymbolData?: DerivedSymbolOverride[]}diff --git a/tests/engine/io/fig/import/scaled-instance-strokes.test.ts b/tests/engine/io/fig/import/scaled-instance-strokes.test.tsindex 76fd68c8f..f1fbc27cb 100644--- a/tests/engine/io/fig/import/scaled-instance-strokes.test.ts+++ b/tests/engine/io/fig/import/scaled-instance-strokes.test.ts@@ -53,4 +53,50 @@ describe('fig import scaled instance strokes', () => {expect(vector?.strokes[0]?.weight).toBe(2)expect(vector?.strokes[0]?.color).toEqual({ r: 0.2, g: 0.25, b: 0.33, a: 1 })})++ test('applies explicit instance stroke scale to scaled vectors', () => {+ const componentGuid = { sessionID: 1, localID: 30 }+ const vectorGuid = { sessionID: 1, localID: 31 }++ const graph = importNodeChanges(+ [+ doc(),+ canvas(),+ node('SYMBOL', 30, 1, {+ guid: componentGuid,+ size: { x: 24, y: 24 }+ } as Partial<NodeChange>),+ node('VECTOR', 31, 1, {+ guid: vectorGuid,+ parentIndex: { guid: componentGuid, position: '!' },+ size: { x: 4, y: 8 },+ horizontalConstraint: 'SCALE',+ verticalConstraint: 'SCALE',+ strokeWeight: 2,+ strokePaints: [+ {+ type: 'SOLID',+ color: { r: 0.2, g: 0.25, b: 0.33, a: 1 },+ opacity: 1,+ visible: true,+ blendMode: 'NORMAL'+ }+ ]+ } as Partial<NodeChange>),+ node('INSTANCE', 40, 1, {+ size: { x: 16, y: 16 },+ strokeWeight: 2 / 3,+ symbolData: { symbolID: componentGuid }+ } as Partial<NodeChange>)+ ],+ [],+ undefined,+ { populate: 'all' }+ )++ const instance = Array.from(graph.getAllNodes()).find((sceneNode) => sceneNode.name === 'INSTANCE_40')+ const vector = instance?.childIds.map((id) => graph.getNode(id)).find(Boolean)++ expect(vector?.strokes[0]?.weight).toBeCloseTo(4 / 3, 6)+ })})