AI-native design editor. Open-source Figma alternative.
fix(io): preserve SVG clip paths
Danila Poyarkov742522a69254
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 1049eac36..0918ec118 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -29,6 +29,7 @@- Remove the permanent CORS configuration action from cloud-storage settings and report connection results through standard toasts with clear browser-specific guidance.- Complete translated app, accessibility, font, color, collaboration, import, connection-test, and browser fallback text across all supported locales, and keep the document language synchronized with the selected locale.- Preserve effective nested instance text overrides when importing complex Figma component hierarchies. (#102)+- Preserve SVG clip paths, including clip shapes referenced through `<use>`, when importing editable vectors.- Preserve circles, ellipses, rectangles, lines, polylines, and polygons supplied as JSX children of inline SVG elements. (#452)## 0.14.0 — 2026-08-10diff --git a/packages/core/src/icons/svg.ts b/packages/core/src/icons/svg.tsindex 7ab25ad60..2325eb07b 100644--- a/packages/core/src/icons/svg.ts+++ b/packages/core/src/icons/svg.ts@@ -12,7 +12,7 @@ import type { Vector } from '@open-pencil/scene-graph/primitives'import { parseSVGFragment } from '#core/io/formats/svg/document'-import type { IconData, IconifyIconEntry, IconPathInfo } from './types'+import type { IconData, IconifyIconEntry, IconPathInfo, SVGClipPathInfo } from './types'interface SVGElementInput {type: string@@ -185,6 +185,7 @@ function appendShapePath(element: Element,presentation: PresentationAttributes,transform: string | null,+ clipPaths: SVGClipPathInfo[][],result: IconPathInfo[]): void {if (!SHAPE_NAMES.has(tagName)) return@@ -199,7 +200,8 @@ function appendShapePath(strokeCap: presentation.strokeCap,strokeJoin: presentation.strokeJoin,fillRule: presentation.fillRule === 'evenodd' ? 'EVENODD' : 'NONZERO',- transform+ transform,+ clipPaths: clipPaths.length > 0 ? clipPaths : undefined})}@@ -233,6 +235,28 @@ function collectUsePaths(return true}+function collectClipPaths(+ value: string | null,+ parentTransform: string | null,+ elementsById: ReadonlyMap<string, Element>+): SVGClipPathInfo[] {+ const match = value?.trim().match(/^url\(\s*['"]?#([^'")\s]+)['"]?\s*\)$/)+ const target = match ? elementsById.get(match[1]) : null+ if (!target || (target.localName || target.tagName) !== 'clipPath') return []++ const paths: IconPathInfo[] = []+ collectPaths(+ target,+ { ...DEFAULT_PRESENTATION, fill: '#000000' },+ parentTransform,+ paths,+ elementsById,+ new Set([target]),+ true+ )+ return paths.map(({ d, fillRule, transform }) => ({ d, fillRule, transform }))+}+function collectPaths(element: Element,inherited: PresentationAttributes,@@ -240,19 +264,32 @@ function collectPaths(result: IconPathInfo[],elementsById: ReadonlyMap<string, Element>,useStack: ReadonlySet<Element> = new Set(),- referenced = false+ referenced = false,+ inheritedClipPaths: SVGClipPathInfo[][] = []): void {const tagName = element.localName || element.tagNameif (NON_RENDERED_CONTAINERS.has(tagName) && !referenced) returnconst presentation = presentationFor(element, inherited)const transform = combinedTransform(parentTransform, element)+ const ownClipPaths = collectClipPaths(element.getAttribute('clip-path'), transform, elementsById)+ const clipPaths =+ ownClipPaths.length > 0 ? [...inheritedClipPaths, ownClipPaths] : inheritedClipPathsif (collectUsePaths(element, presentation, transform, result, elementsById, useStack)) return- appendShapePath(tagName, element, presentation, transform, result)+ appendShapePath(tagName, element, presentation, transform, clipPaths, result)for (const child of Array.from(element.childNodes)) {if (isElement(child)) {- collectPaths(child, presentation, transform, result, elementsById, useStack, referenced)+ collectPaths(+ child,+ presentation,+ transform,+ result,+ elementsById,+ useStack,+ referenced,+ clipPaths+ )}}}diff --git a/packages/core/src/icons/types.ts b/packages/core/src/icons/types.tsindex e8df7c89b..299964cab 100644--- a/packages/core/src/icons/types.ts+++ b/packages/core/src/icons/types.ts@@ -45,6 +45,10 @@ export interface IconPathInfo {strokeCap: stringstrokeJoin: stringfillRule: WindingRule+ /** Nested SVG clip regions, ordered from outermost to innermost. */+ clipPaths?: SVGClipPathInfo[][]/** Raw transform attribute from the source SVG element. */transform?: string | null}++export type SVGClipPathInfo = Pick<IconPathInfo, 'd' | 'fillRule' | 'transform'>diff --git a/packages/core/src/vector/vectorize/placement.ts b/packages/core/src/vector/vectorize/placement.tsindex 61750c2c7..4dbc7a6c3 100644--- a/packages/core/src/vector/vectorize/placement.ts+++ b/packages/core/src/vector/vectorize/placement.ts@@ -27,7 +27,8 @@ interface NormalizedVectorGeometry {bounds: Rect}-type VectorChildPaints = Pick<SceneNode, 'fillGeometry' | 'fills' | 'strokes'>+type VectorChildPaints = Pick<SceneNode, 'fillGeometry' | 'fills' | 'strokes'> &+ Partial<Pick<SceneNode, 'isMask' | 'maskType'>>function shouldTightenToContent(node: Pick<SceneNode, 'width' | 'height' | 'rotation'>,@@ -206,6 +207,48 @@ export function createVectorFrameChildren(}}+function createClipFrame(+ graph: SceneGraph,+ frameId: string,+ placement: VectorFramePlacement,+ index: number+): SceneNode {+ return graph.createNode('FRAME', frameId, {+ name: `clip ${index + 1}`,+ x: 0,+ y: 0,+ width: placement.width,+ height: placement.height,+ fills: []+ })+}++function createClipMaskChild(+ graph: SceneGraph,+ frameId: string,+ clipNetwork: VectorNetwork,+ placement: VectorFramePlacement,+ index: number+): void {+ const network = offsetVectorNetwork(clipNetwork, placement.offsetX, placement.offsetY)+ const normalized = normalizeVectorToNodeBounds(network)+ if (!normalized) return+ createNormalizedVectorChild(graph, frameId, normalized, index, {+ fillGeometry: [],+ fills: [+ {+ type: 'SOLID',+ color: { r: 1, g: 1, b: 1, a: 1 },+ opacity: 1,+ visible: true+ }+ ],+ strokes: [],+ isMask: true,+ maskType: 'VECTOR'+ })+}+function isFlattenableVectorPath(path: VectorizedPath): boolean {return path.fills.length > 0 && path.strokes.length === 0 && path.vectorNetwork.regions.length > 0}@@ -218,28 +261,46 @@ export function createFlattenedVectorFrameChildren(placement: VectorFramePlacement): void {let run: { path: VectorizedPath; index: number }[] = []+ let runClipNetwork: VectorNetwork | undefinedconst flush = () => {+ if (run.length === 0) return+ const targetFrameId = runClipNetwork+ ? createClipFrame(graph, frameId, placement, run[0].index).id+ : frameId+ if (runClipNetwork) {+ createClipMaskChild(graph, targetFrameId, runClipNetwork, placement, run[0].index)+ }if (run.length > 1) {createFlattenedVectorChild(graph,- frameId,+ targetFrameId,run.map(({ path }) => path),placement,run[0].index)} else if (run[0]) {- createVectorChild(graph, frameId, run[0].path, placement, run[0].index)+ createVectorChild(graph, targetFrameId, run[0].path, placement, run[0].index)}run = []+ runClipNetwork = undefined}for (const [index, path] of vectorized.paths.entries()) {+ const clipNetwork = path.clipNetworks?.at(-1)if (isFlattenableVectorPath(path)) {+ if (run.length > 0 && runClipNetwork !== clipNetwork) flush()+ runClipNetwork = clipNetworkrun.push({ path, index })continue}flush()- createVectorChild(graph, frameId, path, placement, index)+ if (clipNetwork) {+ const clipFrame = createClipFrame(graph, frameId, placement, index)+ createClipMaskChild(graph, clipFrame.id, clipNetwork, placement, index)+ createVectorChild(graph, clipFrame.id, path, placement, index)+ } else {+ createVectorChild(graph, frameId, path, placement, index)+ }}flush()}diff --git a/packages/core/src/vector/vectorize/svg/to-vectors.ts b/packages/core/src/vector/vectorize/svg/to-vectors.tsindex ae8a18969..fba617445 100644--- a/packages/core/src/vector/vectorize/svg/to-vectors.ts+++ b/packages/core/src/vector/vectorize/svg/to-vectors.ts@@ -6,6 +6,7 @@* (viewBox, else width/height) into the target node bounds before parsing.*/import type { Fill, Stroke, VectorNetwork, WindingRule } from '@open-pencil/scene-graph'+import { mergeVectorNetworks } from '@open-pencil/scene-graph'import { computeBounds } from '@open-pencil/scene-graph/geometry'import { parseSVGPath } from '@open-pencil/scene-graph/parse-path'import type { Rect, Size } from '@open-pencil/scene-graph/primitives'@@ -59,6 +60,7 @@ export interface VectorizedPath {vectorNetwork: VectorNetworkfills: Fill[]strokes: Stroke[]+ clipNetworks?: VectorNetwork[]}export interface SVGVectorizeResult {@@ -89,6 +91,7 @@ export function svgToVectorPaths(const strokeScale = Math.min(viewport.scaleX, viewport.scaleY)const vectorized: VectorizedPath[] = []+ const clipCache = new WeakMap<NonNullable<IconPathInfo['clipPaths']>, VectorNetwork[]>()for (const path of paths) {const fillRule: WindingRule = path.fillRuleconst transform = path.transform ?? null@@ -105,10 +108,26 @@ export function svgToVectorPaths(computeAccurateBounds(network)): null+ let clipNetworks: VectorNetwork[] | undefined+ if (path.clipPaths) {+ clipNetworks = clipCache.get(path.clipPaths)+ if (!clipNetworks) {+ clipNetworks = path.clipPaths.map((clipRegion) =>+ mergeVectorNetworks(+ clipRegion.map((clipPath) => {+ const clipData = applySVGTransformToPath(clipPath.d, clipPath.transform ?? null)+ return parseSVGPath(mapSVGPathToViewport(clipData, viewport), clipPath.fillRule)+ })+ )+ )+ clipCache.set(path.clipPaths, clipNetworks)+ }+ }vectorized.push({vectorNetwork: network,fills: gradientFill ? [gradientFill] : resolveFill(path, defaultColor),- strokes: resolveStrokes(path, defaultColor, strokeScale)+ strokes: resolveStrokes(path, defaultColor, strokeScale),+ clipNetworks})}diff --git a/tests/engine/io/svg/import.test.ts b/tests/engine/io/svg/import.test.tsindex 448dbedd9..31dc78401 100644--- a/tests/engine/io/svg/import.test.ts+++ b/tests/engine/io/svg/import.test.ts@@ -263,6 +263,32 @@ describe('import_svg', () => {expect(path.fills[0].color.b).toBeCloseTo(1)})+ test('imports clip paths as masks for clipped paint runs', async () => {+ const result = (await importSVG.execute(figma, {+ svg: `<svg viewBox="0 0 100 100">+ <defs><path id="mark" d="M10 10H90V90H10Z"/></defs>+ <g clip-path="url(#clip)">+ <defs><clipPath id="clip"><use href="#mark"/></clipPath></defs>+ <rect width="50" height="100" fill="#ff0000"/>+ <rect x="50" width="50" height="100" fill="#0000ff"/>+ </g>+ <circle cx="50" cy="50" r="10" fill="#ffffff"/>+ </svg>`+ })) as { id: string }++ const children = graph.getChildren(result.id)+ expect(children).toHaveLength(2)+ const clippedGroup = expectDefined(children[0])+ expect(clippedGroup.type).toBe('FRAME')+ const clippedChildren = graph.getChildren(clippedGroup.id)+ expect(clippedChildren).toHaveLength(2)+ expect(clippedChildren[0].isMask).toBe(true)+ expect(clippedChildren[0].maskType).toBe('VECTOR')+ expect(expectDefined(clippedChildren[0].vectorNetwork).regions).toHaveLength(1)+ expect(clippedChildren[1].fillGeometry).toHaveLength(2)+ expect(children[1].isMask).toBe(false)+ })+test('imports gradient fills through the shared SVG pipeline', async () => {const result = (await importSVG.execute(figma, {svg: `<svg viewBox="0 0 10 10"><defs><linearGradient id="g"><stop offset="0" stop-color="#000"/><stop offset="1" stop-color="#fff"/></linearGradient></defs><rect width="10" height="10" fill="url(#g)"/></svg>`