2024-05-21 07:36:30 -05:00
|
|
|
import { Command, CurveToCommand, LineToCommand, MoveToCommand, parseSVG } from 'svg-path-parser';
|
2024-04-16 09:08:39 -05:00
|
|
|
|
2024-05-06 01:06:14 -05:00
|
|
|
import { Segment } from '@ui/lib/types/shapes/pathShape';
|
2024-04-16 09:08:39 -05:00
|
|
|
|
|
|
|
export const translateVectorPaths = (
|
|
|
|
paths: VectorPaths,
|
|
|
|
baseX: number,
|
|
|
|
baseY: number
|
|
|
|
): Segment[] => {
|
|
|
|
let segments: Segment[] = [];
|
|
|
|
|
|
|
|
for (const path of paths) {
|
2024-05-21 07:36:30 -05:00
|
|
|
const normalizedPaths = parseSVG(path.data);
|
|
|
|
|
|
|
|
segments = [...segments, ...translateCommandsToSegments(normalizedPaths, baseX, baseY)];
|
2024-04-16 09:08:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return segments;
|
|
|
|
};
|
|
|
|
|
2024-05-21 07:36:30 -05:00
|
|
|
export const translateCommandsToSegments = (
|
|
|
|
commands: Command[],
|
|
|
|
baseX: number,
|
|
|
|
baseY: number
|
|
|
|
): Segment[] => {
|
|
|
|
return commands.map(command => {
|
2024-04-17 08:58:52 -05:00
|
|
|
switch (command.command) {
|
|
|
|
case 'moveto':
|
|
|
|
return translateMoveToCommand(command, baseX, baseY);
|
|
|
|
case 'lineto':
|
|
|
|
return translateLineToCommand(command, baseX, baseY);
|
|
|
|
case 'curveto':
|
|
|
|
return translateCurveToCommand(command, baseX, baseY);
|
|
|
|
case 'closepath':
|
|
|
|
default:
|
|
|
|
return {
|
2024-04-16 09:08:39 -05:00
|
|
|
command: 'close-path'
|
2024-04-17 08:58:52 -05:00
|
|
|
};
|
2024-04-16 09:08:39 -05:00
|
|
|
}
|
2024-04-17 08:58:52 -05:00
|
|
|
});
|
|
|
|
};
|
2024-04-16 09:08:39 -05:00
|
|
|
|
2024-04-17 08:58:52 -05:00
|
|
|
const translateMoveToCommand = (command: MoveToCommand, baseX: number, baseY: number): Segment => {
|
|
|
|
return {
|
|
|
|
command: 'move-to',
|
|
|
|
params: { x: command.x + baseX, y: command.y + baseY }
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const translateLineToCommand = (command: LineToCommand, baseX: number, baseY: number): Segment => {
|
|
|
|
return {
|
|
|
|
command: 'line-to',
|
|
|
|
params: { x: command.x + baseX, y: command.y + baseY }
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const translateCurveToCommand = (
|
|
|
|
command: CurveToCommand,
|
|
|
|
baseX: number,
|
|
|
|
baseY: number
|
|
|
|
): Segment => {
|
|
|
|
return {
|
|
|
|
command: 'curve-to',
|
|
|
|
params: {
|
|
|
|
c1x: command.x1 + baseX,
|
|
|
|
c1y: command.y1 + baseY,
|
|
|
|
c2x: command.x2 + baseX,
|
|
|
|
c2y: command.y2 + baseY,
|
|
|
|
x: command.x + baseX,
|
|
|
|
y: command.y + baseY
|
|
|
|
}
|
|
|
|
};
|
2024-04-16 09:08:39 -05:00
|
|
|
};
|