UNPKG

1.87 kBPlain TextView Raw
1import { paramsParser } from '../parser/params-parser';
2import { fixArc } from '../process/fix-arc';
3import { normalizePath } from '../process/normalize-path';
4import { isCurveArray } from '../util/is-curve-array';
5import { segmentToCubic } from '../process/segment-2-cubic';
6import type { CurveArray, PathArray } from '../types';
7// import { fixPath } from '../process/fix-path';
8
9export 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 // fixPath will remove 'Z' command
23 // const path = fixPath(normalizePath(pathInput));
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; // solves curveArrays ending in Z
44
45 // keep Z command account for lineJoin
46 // @see https://github.com/antvis/util/issues/68
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 // validate
61
62 if (needZCommandIndexes) {
63 return [path, zCommandIndexes];
64 } else {
65 return path;
66 }
67}