UNPKG

3.04 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.path2Absolute = void 0;
4var clone_path_1 = require("../process/clone-path");
5var is_absolute_array_1 = require("../util/is-absolute-array");
6var parse_path_string_1 = require("../parser/parse-path-string");
7function path2Absolute(pathInput) {
8 if ((0, is_absolute_array_1.isAbsoluteArray)(pathInput)) {
9 return (0, clone_path_1.clonePath)(pathInput);
10 }
11 var path = (0, parse_path_string_1.parsePathString)(pathInput);
12 // if (!path || !path.length) {
13 // return [['M', 0, 0]];
14 // }
15 var x = 0;
16 var y = 0;
17 var mx = 0;
18 var my = 0;
19 // @ts-ignore
20 return path.map(function (segment) {
21 var values = segment.slice(1).map(Number);
22 var pathCommand = segment[0];
23 var absCommand = pathCommand.toUpperCase();
24 if (pathCommand === 'M') {
25 x = values[0], y = values[1];
26 mx = x;
27 my = y;
28 return ['M', x, y];
29 }
30 var absoluteSegment;
31 if (pathCommand !== absCommand) {
32 switch (absCommand) {
33 case 'A':
34 absoluteSegment = [
35 absCommand,
36 values[0],
37 values[1],
38 values[2],
39 values[3],
40 values[4],
41 values[5] + x,
42 values[6] + y,
43 ];
44 break;
45 case 'V':
46 absoluteSegment = [absCommand, values[0] + y];
47 break;
48 case 'H':
49 absoluteSegment = [absCommand, values[0] + x];
50 break;
51 default: {
52 // use brakets for `eslint: no-case-declaration`
53 // https://stackoverflow.com/a/50753272/803358
54 var absValues = values.map(function (n, j) { return n + (j % 2 ? y : x); });
55 // for n, l, c, s, q, t
56 // @ts-ignore
57 absoluteSegment = [absCommand].concat(absValues);
58 }
59 }
60 }
61 else {
62 // @ts-ignore
63 absoluteSegment = [absCommand].concat(values);
64 }
65 var segLength = absoluteSegment.length;
66 switch (absCommand) {
67 case 'Z':
68 x = mx;
69 y = my;
70 break;
71 case 'H':
72 x = absoluteSegment[1];
73 break;
74 case 'V':
75 y = absoluteSegment[1];
76 break;
77 default:
78 x = absoluteSegment[segLength - 2];
79 y = absoluteSegment[segLength - 1];
80 if (absCommand === 'M') {
81 mx = x;
82 my = y;
83 }
84 }
85 return absoluteSegment;
86 });
87}
88exports.path2Absolute = path2Absolute;
89//# sourceMappingURL=path-2-absolute.js.map
\No newline at end of file