1 | import { paramsParser } from '../parser/params-parser';
|
2 | import { fixArc } from '../process/fix-arc';
|
3 | import { normalizePath } from '../process/normalize-path';
|
4 | import { isCurveArray } from '../util/is-curve-array';
|
5 | import { segmentToCubic } from '../process/segment-2-cubic';
|
6 | import type { CurveArray, PathArray } from '../types';
|
7 |
|
8 |
|
9 | export function path2Curve(
|
10 | pathInput: string | PathArray,
|
11 | needZCommandIndexes = false,
|
12 | ): CurveArray | [CurveArray, number[]] {
|
13 | if (isCurveArray(pathInput)) {
|
14 | const cloned = [].concat(pathInput) as CurveArray;
|
15 | if (needZCommandIndexes) {
|
16 | return [cloned, []];
|
17 | } else {
|
18 | return cloned;
|
19 | }
|
20 | }
|
21 |
|
22 |
|
23 |
|
24 | const path = normalizePath(pathInput) as CurveArray;
|
25 |
|
26 | const params = { ...paramsParser };
|
27 | const allPathCommands = [];
|
28 | let pathCommand = '';
|
29 | let ii = path.length;
|
30 | let segment: any;
|
31 | let seglen: number;
|
32 | const zCommandIndexes: number[] = [];
|
33 |
|
34 | for (let i = 0; i < ii; i += 1) {
|
35 | if (path[i]) [pathCommand] = path[i];
|
36 |
|
37 | allPathCommands[i] = pathCommand;
|
38 | const curveSegment = segmentToCubic(path[i], params);
|
39 |
|
40 | path[i] = curveSegment;
|
41 |
|
42 | fixArc(path, allPathCommands, i);
|
43 | ii = path.length;
|
44 |
|
45 |
|
46 |
|
47 | if (pathCommand === 'Z') {
|
48 | zCommandIndexes.push(i);
|
49 | }
|
50 |
|
51 | segment = path[i];
|
52 | seglen = segment.length;
|
53 |
|
54 | params.x1 = +segment[seglen - 2];
|
55 | params.y1 = +segment[seglen - 1];
|
56 | params.x2 = +segment[seglen - 4] || params.x1;
|
57 | params.y2 = +segment[seglen - 3] || params.y1;
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 | if (needZCommandIndexes) {
|
63 | return [path, zCommandIndexes];
|
64 | } else {
|
65 | return path;
|
66 | }
|
67 | }
|