2024-05-21 07:36:30 -05:00
|
|
|
import { Command } from 'svg-path-parser';
|
|
|
|
|
|
|
|
import { translateStrokeCap, translateStrokes } from '@plugin/translators';
|
2024-04-17 03:53:38 -05:00
|
|
|
|
2024-05-06 01:06:14 -05:00
|
|
|
import { ShapeAttributes } from '@ui/lib/types/shapes/shape';
|
2024-05-21 07:36:30 -05:00
|
|
|
import { Stroke } from '@ui/lib/types/utils/stroke';
|
2024-04-17 03:53:38 -05:00
|
|
|
|
|
|
|
const isVectorLike = (node: GeometryMixin | VectorLikeMixin): node is VectorLikeMixin => {
|
|
|
|
return 'vectorNetwork' in node;
|
|
|
|
};
|
|
|
|
|
2024-04-18 02:42:45 -05:00
|
|
|
const hasFillGeometry = (node: GeometryMixin): boolean => {
|
2024-04-17 03:53:38 -05:00
|
|
|
return node.fillGeometry.length > 0;
|
|
|
|
};
|
|
|
|
|
2024-06-05 05:36:49 -05:00
|
|
|
export const transformStrokes = (
|
2024-04-18 02:42:45 -05:00
|
|
|
node: GeometryMixin | (GeometryMixin & IndividualStrokesMixin)
|
2024-06-05 05:36:49 -05:00
|
|
|
): Pick<ShapeAttributes, 'strokes'> => {
|
2024-05-21 07:36:30 -05:00
|
|
|
const vectorNetwork = isVectorLike(node) ? node.vectorNetwork : undefined;
|
|
|
|
|
|
|
|
const strokeCaps = (stroke: Stroke) => {
|
|
|
|
if (!hasFillGeometry(node) && vectorNetwork && vectorNetwork.vertices.length > 0) {
|
|
|
|
stroke.strokeCapStart = translateStrokeCap(vectorNetwork.vertices[0]);
|
|
|
|
stroke.strokeCapEnd = translateStrokeCap(
|
|
|
|
vectorNetwork.vertices[vectorNetwork.vertices.length - 1]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stroke;
|
|
|
|
};
|
|
|
|
|
2024-04-17 03:53:38 -05:00
|
|
|
return {
|
2024-06-05 05:36:49 -05:00
|
|
|
strokes: translateStrokes(node, strokeCaps)
|
2024-04-17 03:53:38 -05:00
|
|
|
};
|
|
|
|
};
|
2024-05-21 07:36:30 -05:00
|
|
|
|
2024-06-05 05:36:49 -05:00
|
|
|
export const transformStrokesFromVector = (
|
2024-05-21 07:36:30 -05:00
|
|
|
node: VectorNode,
|
|
|
|
vector: Command[],
|
|
|
|
vectorRegion: VectorRegion | undefined
|
2024-06-05 05:36:49 -05:00
|
|
|
): Pick<ShapeAttributes, 'strokes'> => {
|
2024-05-21 07:36:30 -05:00
|
|
|
const strokeCaps = (stroke: Stroke) => {
|
|
|
|
if (vectorRegion !== undefined) return stroke;
|
|
|
|
|
|
|
|
const startVertex = findVertex(node.vectorNetwork.vertices, vector[0]);
|
|
|
|
const endVertex = findVertex(node.vectorNetwork.vertices, vector[vector.length - 1]);
|
|
|
|
|
|
|
|
if (!startVertex || !endVertex) return stroke;
|
|
|
|
|
|
|
|
stroke.strokeCapStart = translateStrokeCap(startVertex);
|
|
|
|
stroke.strokeCapEnd = translateStrokeCap(endVertex);
|
|
|
|
|
|
|
|
return stroke;
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2024-06-05 05:36:49 -05:00
|
|
|
strokes: translateStrokes(node, strokeCaps)
|
2024-05-21 07:36:30 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const findVertex = (
|
|
|
|
vertexs: readonly VectorVertex[],
|
|
|
|
command: Command
|
|
|
|
): VectorVertex | undefined => {
|
|
|
|
if (command.command !== 'moveto' && command.command !== 'lineto' && command.command !== 'curveto')
|
|
|
|
return;
|
|
|
|
|
|
|
|
return vertexs.find(vertex => vertex.x === command.x && vertex.y === command.y);
|
|
|
|
};
|