2024-05-21 07:36:30 -05:00
|
|
|
import { parseSVG } from 'svg-path-parser';
|
|
|
|
|
2024-05-13 06:21:46 -05:00
|
|
|
import {
|
|
|
|
transformBlend,
|
|
|
|
transformDimensionAndPositionFromVectorPath,
|
|
|
|
transformEffects,
|
|
|
|
transformProportion,
|
|
|
|
transformSceneNode,
|
2024-05-21 07:36:30 -05:00
|
|
|
transformStrokesFromVector
|
2024-05-13 06:21:46 -05:00
|
|
|
} from '@plugin/transformers/partials';
|
2024-05-13 10:04:13 -05:00
|
|
|
import { translateFills } from '@plugin/translators/fills';
|
2024-05-21 07:36:30 -05:00
|
|
|
import {
|
|
|
|
translateCommandsToSegments,
|
|
|
|
translateLineNode,
|
|
|
|
translateVectorPaths,
|
|
|
|
translateWindingRule
|
|
|
|
} from '@plugin/translators/vectors';
|
2024-04-17 03:53:38 -05:00
|
|
|
|
2024-05-06 01:06:14 -05:00
|
|
|
import { PathAttributes } from '@ui/lib/types/shapes/pathShape';
|
2024-05-13 06:21:46 -05:00
|
|
|
import { PathShape } from '@ui/lib/types/shapes/pathShape';
|
2024-04-17 03:53:38 -05:00
|
|
|
|
2024-05-13 06:21:46 -05:00
|
|
|
export const transformVectorPathsAsContent = (
|
2024-05-21 07:36:30 -05:00
|
|
|
node: StarNode | LineNode | PolygonNode,
|
2024-04-17 03:53:38 -05:00
|
|
|
baseX: number,
|
|
|
|
baseY: number
|
|
|
|
): PathAttributes => {
|
|
|
|
const vectorPaths = getVectorPaths(node);
|
|
|
|
|
|
|
|
return {
|
|
|
|
content: translateVectorPaths(vectorPaths, baseX + node.x, baseY + node.y)
|
|
|
|
};
|
|
|
|
};
|
2024-05-13 06:21:46 -05:00
|
|
|
|
2024-06-05 05:36:49 -05:00
|
|
|
export const transformVectorPaths = (
|
2024-05-13 06:21:46 -05:00
|
|
|
node: VectorNode,
|
|
|
|
baseX: number,
|
|
|
|
baseY: number
|
2024-06-05 05:36:49 -05:00
|
|
|
): PathShape[] => {
|
|
|
|
const pathShapes = node.vectorPaths
|
|
|
|
.filter((vectorPath, index) => {
|
|
|
|
return (
|
|
|
|
nodeHasFills(node, vectorPath, (node.vectorNetwork.regions ?? [])[index]) ||
|
|
|
|
node.strokes.length > 0
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.map((vectorPath, index) =>
|
|
|
|
transformVectorPath(node, vectorPath, (node.vectorNetwork.regions ?? [])[index], baseX, baseY)
|
|
|
|
);
|
2024-05-21 07:36:30 -05:00
|
|
|
|
2024-06-05 05:36:49 -05:00
|
|
|
const geometryShapes = node.fillGeometry
|
|
|
|
.filter(
|
|
|
|
geometry =>
|
|
|
|
!node.vectorPaths.find(
|
|
|
|
vectorPath => normalizePath(vectorPath.data) === normalizePath(geometry.data)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.map(geometry => transformVectorPath(node, geometry, undefined, baseX, baseY));
|
2024-05-21 07:36:30 -05:00
|
|
|
|
|
|
|
return [...geometryShapes, ...pathShapes];
|
|
|
|
};
|
|
|
|
|
|
|
|
const getVectorPaths = (node: StarNode | LineNode | PolygonNode): VectorPaths => {
|
|
|
|
switch (node.type) {
|
|
|
|
case 'STAR':
|
|
|
|
case 'POLYGON':
|
|
|
|
return node.fillGeometry;
|
|
|
|
case 'LINE':
|
|
|
|
return translateLineNode(node);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const normalizePath = (path: string): string => {
|
|
|
|
// Round to 2 decimal places all numbers
|
|
|
|
const str = path.replace(/(\d+\.\d+|\d+)/g, (match: string) => {
|
|
|
|
return parseFloat(match).toFixed(2);
|
|
|
|
});
|
|
|
|
// remove spaces
|
|
|
|
return str.replace(/\s/g, '');
|
|
|
|
};
|
|
|
|
|
|
|
|
const nodeHasFills = (
|
|
|
|
node: VectorNode,
|
|
|
|
vectorPath: VectorPath,
|
|
|
|
vectorRegion: VectorRegion | undefined
|
|
|
|
): boolean => {
|
|
|
|
return !!(vectorPath.windingRule !== 'NONE' && (vectorRegion?.fills || node.fills));
|
2024-05-13 06:21:46 -05:00
|
|
|
};
|
|
|
|
|
2024-06-05 05:36:49 -05:00
|
|
|
const transformVectorPath = (
|
2024-05-13 06:21:46 -05:00
|
|
|
node: VectorNode,
|
|
|
|
vectorPath: VectorPath,
|
|
|
|
vectorRegion: VectorRegion | undefined,
|
|
|
|
baseX: number,
|
|
|
|
baseY: number
|
2024-06-05 05:36:49 -05:00
|
|
|
): PathShape => {
|
2024-05-21 07:36:30 -05:00
|
|
|
const normalizedPaths = parseSVG(vectorPath.data);
|
|
|
|
|
2024-05-13 06:21:46 -05:00
|
|
|
return {
|
|
|
|
type: 'path',
|
|
|
|
name: 'svg-path',
|
2024-05-21 07:36:30 -05:00
|
|
|
content: translateCommandsToSegments(normalizedPaths, baseX + node.x, baseY + node.y),
|
|
|
|
fills:
|
2024-06-05 05:36:49 -05:00
|
|
|
vectorPath.windingRule === 'NONE' ? [] : translateFills(vectorRegion?.fills ?? node.fills),
|
2024-05-21 07:36:30 -05:00
|
|
|
svgAttrs: {
|
|
|
|
fillRule: translateWindingRule(vectorPath.windingRule)
|
|
|
|
},
|
2024-06-05 05:36:49 -05:00
|
|
|
...transformStrokesFromVector(node, normalizedPaths, vectorRegion),
|
2024-05-13 06:21:46 -05:00
|
|
|
...transformEffects(node),
|
2024-05-13 09:44:00 -05:00
|
|
|
...transformDimensionAndPositionFromVectorPath(vectorPath, baseX, baseY),
|
2024-05-13 06:21:46 -05:00
|
|
|
...transformSceneNode(node),
|
|
|
|
...transformBlend(node),
|
|
|
|
...transformProportion(node)
|
|
|
|
};
|
|
|
|
};
|