@remotion/paths 4.0.0-webhook.27 → 4.1.0-alpha2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +1 -2
  3. package/dist/evolve-path.d.ts +2 -2
  4. package/dist/evolve-path.js +2 -2
  5. package/dist/extend-viewbox.d.ts +2 -2
  6. package/dist/extend-viewbox.js +2 -2
  7. package/dist/get-bounding-box.d.ts +8 -0
  8. package/dist/get-bounding-box.js +185 -0
  9. package/dist/get-length.d.ts +2 -2
  10. package/dist/get-length.js +2 -2
  11. package/dist/get-point-at-length.d.ts +2 -2
  12. package/dist/get-point-at-length.js +2 -2
  13. package/dist/get-subpaths.d.ts +6 -0
  14. package/dist/get-subpaths.js +19 -0
  15. package/dist/get-tangent-at-length.d.ts +2 -2
  16. package/dist/get-tangent-at-length.js +2 -2
  17. package/dist/helpers/arc.d.ts +0 -0
  18. package/dist/helpers/arc.js +0 -0
  19. package/dist/helpers/bezier-functions.d.ts +0 -0
  20. package/dist/helpers/bezier-functions.js +0 -0
  21. package/dist/helpers/bezier-values.d.ts +0 -0
  22. package/dist/helpers/bezier-values.js +0 -0
  23. package/dist/helpers/bezier.d.ts +0 -0
  24. package/dist/helpers/bezier.js +0 -0
  25. package/dist/helpers/construct.d.ts +9 -1
  26. package/dist/helpers/construct.js +218 -128
  27. package/dist/helpers/get-part-at-length.d.ts +0 -0
  28. package/dist/helpers/get-part-at-length.js +0 -0
  29. package/dist/helpers/iterate.d.ts +14 -0
  30. package/dist/helpers/iterate.js +95 -0
  31. package/dist/helpers/linear.d.ts +6 -1
  32. package/dist/helpers/linear.js +1 -1
  33. package/dist/helpers/remove-a-s-t-curves.d.ts +2 -0
  34. package/dist/helpers/remove-a-s-t-curves.js +271 -0
  35. package/dist/helpers/split-curve.d.ts +1 -1
  36. package/dist/helpers/split-curve.js +0 -0
  37. package/dist/helpers/types.d.ts +109 -2
  38. package/dist/helpers/types.js +0 -0
  39. package/dist/index.d.ts +10 -2
  40. package/dist/index.js +19 -3
  41. package/dist/interpolate-path.d.ts +2 -2
  42. package/dist/interpolate-path.js +2 -2
  43. package/dist/normalize-path.d.ts +4 -2
  44. package/dist/normalize-path.js +141 -277
  45. package/dist/parse-path.d.ts +8 -0
  46. package/dist/parse-path.js +265 -0
  47. package/dist/reduce-instructions.d.ts +7 -0
  48. package/dist/reduce-instructions.js +15 -0
  49. package/dist/reset-path.d.ts +6 -0
  50. package/dist/reset-path.js +15 -0
  51. package/dist/reverse-path.d.ts +2 -2
  52. package/dist/reverse-path.js +73 -118
  53. package/dist/scale-path.d.ts +10 -0
  54. package/dist/scale-path.js +180 -0
  55. package/dist/serialize-instructions.d.ts +2 -0
  56. package/dist/serialize-instructions.js +78 -0
  57. package/dist/translate-path.d.ts +11 -0
  58. package/dist/translate-path.js +116 -0
  59. package/dist/warp-path/index.d.ts +10 -0
  60. package/dist/warp-path/index.js +26 -0
  61. package/dist/warp-path/warp-helpers.d.ts +14 -0
  62. package/dist/warp-path/warp-helpers.js +229 -0
  63. package/package.json +38 -38
  64. package/.prettierrc.js +0 -14
  65. package/dist/get-parts.d.ts +0 -7
  66. package/dist/get-parts.js +0 -31
  67. package/dist/helpers/parse.d.ts +0 -2
  68. package/dist/helpers/parse.js +0 -49
  69. package/tsconfig.json +0 -9
@@ -1,300 +1,164 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.normalizePath = void 0;
3
+ exports.normalizeInstructions = exports.normalizePath = void 0;
4
+ const parse_path_1 = require("./parse-path");
5
+ const serialize_instructions_1 = require("./serialize-instructions");
4
6
  /**
5
- * Removes all relative coordinates from an SVG path and converts them into absolute coordinates.
7
+ * @description Removes all relative coordinates from an SVG path and converts them into absolute coordinates.
6
8
  * @param {string} path A valid SVG path
7
- * @link https://remotion.dev/docs/paths/normalize-path
9
+ * @see [Documentation](https://remotion.dev/docs/paths/normalize-path)
8
10
  */
9
11
  const normalizePath = (path) => {
10
- // preprocess "d" so that we have spaces between values
11
- path = path
12
- .replace(/,/g, ' ')
13
- .replace(/([^eE])-/g, '$1 -')
14
- .replace(/\s*([achlmqstvzACHLMQSTVZ])\s*/g, ' $1 ')
15
- .replace(/\s+/g, ' ');
16
- // set up the variables used in this function
17
- const instructions = path
18
- .replace(/([achlmqstvzACHLMQSTVZ])\s?/g, '|$1')
19
- .split('|');
20
- const instructionLength = instructions.length;
21
- let i;
22
- let instruction;
23
- let op;
24
- let lop;
25
- let alen;
26
- let a;
27
- let sx = 0;
28
- let sy = 0;
12
+ const instructions = (0, parse_path_1.parsePath)(path);
13
+ const normalized = (0, exports.normalizeInstructions)(instructions);
14
+ return (0, serialize_instructions_1.serializeInstructions)(normalized);
15
+ };
16
+ exports.normalizePath = normalizePath;
17
+ const normalizeInstructions = (instructions) => {
18
+ // Extended properties must already be normalized
19
+ const normalized = [];
29
20
  let x = 0;
30
21
  let y = 0;
31
- let cx = 0;
32
- let cy = 0;
33
- let cx2 = 0;
34
- let cy2 = 0;
35
- let rx = 0;
36
- let ry = 0;
37
- let xrot = 0;
38
- let lflag = 0;
39
- let sweep = 0;
40
- let normalized = '';
41
- // we run through the instruction list starting at 1, not 0,
42
- // because we split up "|M x y ...." so the first element will
43
- // always be an empty string. By design.
44
- for (i = 1; i < instructionLength; i++) {
45
- // which instruction is this?
46
- instruction = instructions[i];
47
- op = instruction.substring(0, 1);
48
- lop = op.toLowerCase();
49
- // what are the arguments? note that we need to convert
50
- // all strings into numbers, or + will do silly things.
51
- const oargs = instruction
52
- .replace(op, '')
53
- .trim()
54
- .split(' ')
55
- .filter((v) => {
56
- return v !== '';
57
- });
58
- const args = oargs.map((_a) => parseFloat(String(_a)));
59
- alen = args.length;
60
- // we could use a switch, but elaborate code in a "case" with
61
- // fallthrough is just horrid to read. So let's use ifthen
62
- // statements instead.
63
- // moveto command (plus possible lineto)
64
- if (lop === 'm') {
65
- normalized += 'M ';
66
- if (op === 'm') {
67
- x += args[0];
68
- y += args[1];
22
+ let moveX = 0;
23
+ let moveY = 0;
24
+ for (let i = 0; i < instructions.length; i++) {
25
+ const instruction = instructions[i];
26
+ if (instruction.type === 'M') {
27
+ moveX = instruction.x;
28
+ moveY = instruction.y;
29
+ }
30
+ else if (instruction.type === 'm') {
31
+ moveX += instruction.dx;
32
+ moveY += instruction.dy;
33
+ }
34
+ if (instruction.type === 'A' ||
35
+ instruction.type === 'C' ||
36
+ instruction.type === 'L' ||
37
+ instruction.type === 'M' ||
38
+ instruction.type === 'Q' ||
39
+ instruction.type === 'S' ||
40
+ instruction.type === 'T') {
41
+ normalized.push(instruction);
42
+ x = instruction.x;
43
+ y = instruction.y;
44
+ continue;
45
+ }
46
+ if (instruction.type === 'a' ||
47
+ instruction.type === 'c' ||
48
+ instruction.type === 'l' ||
49
+ instruction.type === 'm' ||
50
+ instruction.type === 'q' ||
51
+ instruction.type === 's' ||
52
+ instruction.type === 't') {
53
+ const currentX = x;
54
+ const currentY = y;
55
+ x += instruction.dx;
56
+ y += instruction.dy;
57
+ if (instruction.type === 'a') {
58
+ normalized.push({
59
+ type: 'A',
60
+ largeArcFlag: instruction.largeArcFlag,
61
+ rx: instruction.rx,
62
+ ry: instruction.ry,
63
+ sweepFlag: instruction.sweepFlag,
64
+ xAxisRotation: instruction.xAxisRotation,
65
+ x,
66
+ y,
67
+ });
68
+ continue;
69
69
  }
70
- else {
71
- x = args[0];
72
- y = args[1];
70
+ if (instruction.type === 'c') {
71
+ normalized.push({
72
+ type: 'C',
73
+ cp1x: instruction.cp1dx + currentX,
74
+ cp1y: instruction.cp1dy + currentY,
75
+ cp2x: instruction.cp2dx + currentX,
76
+ cp2y: instruction.cp2dy + currentY,
77
+ x,
78
+ y,
79
+ });
80
+ continue;
73
81
  }
74
- // records start position, for dealing
75
- // with the shape close operator ('Z')
76
- sx = x;
77
- sy = y;
78
- normalized += x + ' ' + y + ' ';
79
- if (alen > 2) {
80
- for (a = 0; a < alen; a += 2) {
81
- // eslint-disable-next-line max-depth
82
- if (op === 'm') {
83
- x += args[a];
84
- y += args[a + 1];
85
- }
86
- else {
87
- x = args[a];
88
- y = args[a + 1];
89
- }
90
- normalized += 'L ' + x + ' ' + y + ' ';
91
- }
82
+ if (instruction.type === 'l') {
83
+ normalized.push({
84
+ type: 'L',
85
+ x,
86
+ y,
87
+ });
88
+ continue;
92
89
  }
93
- }
94
- // lineto commands
95
- else if (lop === 'l') {
96
- for (a = 0; a < alen; a += 2) {
97
- if (op === 'l') {
98
- x += args[a];
99
- y += args[a + 1];
100
- }
101
- else {
102
- x = args[a];
103
- y = args[a + 1];
104
- }
105
- normalized += 'L ' + x + ' ' + y + ' ';
90
+ if (instruction.type === 'm') {
91
+ normalized.push({
92
+ type: 'M',
93
+ x,
94
+ y,
95
+ });
96
+ continue;
106
97
  }
107
- }
108
- else if (lop === 'h') {
109
- for (a = 0; a < alen; a++) {
110
- if (op === 'h') {
111
- x += args[a];
112
- }
113
- else {
114
- x = args[a];
115
- }
116
- normalized += 'L ' + x + ' ' + y + ' ';
98
+ if (instruction.type === 'q') {
99
+ normalized.push({
100
+ type: 'Q',
101
+ cpx: instruction.cpdx + currentX,
102
+ cpy: instruction.cpdy + currentY,
103
+ x,
104
+ y,
105
+ });
106
+ continue;
117
107
  }
118
- }
119
- else if (lop === 'v') {
120
- for (a = 0; a < alen; a++) {
121
- if (op === 'v') {
122
- y += args[a];
123
- }
124
- else {
125
- y = args[a];
126
- }
127
- normalized += 'L ' + x + ' ' + y + ' ';
108
+ if (instruction.type === 's') {
109
+ normalized.push({
110
+ type: 'S',
111
+ cpx: instruction.cpdx + currentX,
112
+ cpy: instruction.cpdy + currentY,
113
+ x,
114
+ y,
115
+ });
116
+ continue;
128
117
  }
129
- }
130
- // quadratic curveto commands
131
- else if (lop === 'q') {
132
- for (a = 0; a < alen; a += 4) {
133
- if (op === 'q') {
134
- cx = x + args[a];
135
- cy = y + args[a + 1];
136
- x += args[a + 2];
137
- y += args[a + 3];
138
- }
139
- else {
140
- cx = args[a];
141
- cy = args[a + 1];
142
- x = args[a + 2];
143
- y = args[a + 3];
144
- }
145
- normalized += 'Q ' + cx + ' ' + cy + ' ' + x + ' ' + y + ' ';
118
+ if (instruction.type === 't') {
119
+ normalized.push({
120
+ type: 'T',
121
+ x,
122
+ y,
123
+ });
124
+ continue;
146
125
  }
147
126
  }
148
- else if (lop === 't') {
149
- for (a = 0; a < alen; a += 2) {
150
- // reflect previous cx/cy over x/y
151
- cx = x + (x - cx);
152
- cy = y + (y - cy);
153
- // then get real end point
154
- if (op === 't') {
155
- x += args[a];
156
- y += args[a + 1];
157
- }
158
- else {
159
- x = args[a];
160
- y = args[a + 1];
161
- }
162
- normalized += 'Q ' + cx + ' ' + cy + ' ' + x + ' ' + y + ' ';
163
- }
127
+ if (instruction.type === 'H') {
128
+ normalized.push(instruction);
129
+ x = instruction.x;
130
+ continue;
164
131
  }
165
- // cubic curveto commands
166
- else if (lop === 'c') {
167
- for (a = 0; a < alen; a += 6) {
168
- if (op === 'c') {
169
- cx = x + args[a];
170
- cy = y + args[a + 1];
171
- cx2 = x + args[a + 2];
172
- cy2 = y + args[a + 3];
173
- x += args[a + 4];
174
- y += args[a + 5];
175
- }
176
- else {
177
- cx = args[a];
178
- cy = args[a + 1];
179
- cx2 = args[a + 2];
180
- cy2 = args[a + 3];
181
- x = args[a + 4];
182
- y = args[a + 5];
183
- }
184
- normalized +=
185
- 'C ' +
186
- cx +
187
- ' ' +
188
- cy +
189
- ' ' +
190
- cx2 +
191
- ' ' +
192
- cy2 +
193
- ' ' +
194
- x +
195
- ' ' +
196
- y +
197
- ' ';
198
- }
132
+ if (instruction.type === 'V') {
133
+ normalized.push(instruction);
134
+ y = instruction.y;
135
+ continue;
199
136
  }
200
- else if (lop === 's') {
201
- for (a = 0; a < alen; a += 4) {
202
- // reflect previous cx2/cy2 over x/y
203
- cx = x + (x - cx2);
204
- cy = y + (y - cy2);
205
- // then get real control and end point
206
- if (op === 's') {
207
- cx2 = x + args[a];
208
- cy2 = y + args[a + 1];
209
- x += args[a + 2];
210
- y += args[a + 3];
211
- }
212
- else {
213
- cx2 = args[a];
214
- cy2 = args[a + 1];
215
- x = args[a + 2];
216
- y = args[a + 3];
217
- }
218
- normalized +=
219
- 'C ' +
220
- cx +
221
- ' ' +
222
- cy +
223
- ' ' +
224
- cx2 +
225
- ' ' +
226
- cy2 +
227
- ' ' +
228
- x +
229
- ' ' +
230
- y +
231
- ' ';
232
- }
137
+ if (instruction.type === 'Z') {
138
+ normalized.push(instruction);
139
+ x = moveX;
140
+ y = moveY;
141
+ continue;
233
142
  }
234
- // rx ry x-axis-rotation large-arc-flag sweep-flag x y
235
- // a 25,25 -30 0, 1 50,-25
236
- // arc command
237
- else if (lop === 'a') {
238
- for (a = 0; a < alen; a += 7) {
239
- rx = args[a];
240
- ry = args[a + 1];
241
- xrot = args[a + 2];
242
- lflag = oargs[a + 3]; // we need the original string to deal with leading zeroes
243
- let fixed = false;
244
- if (lflag.length > 1) {
245
- const b1 = parseInt(lflag[0], 10);
246
- const b2 = parseInt(lflag[1], 10);
247
- let rest;
248
- // eslint-disable-next-line max-depth
249
- if (lflag.length > 2)
250
- rest = parseFloat(lflag.substring(2));
251
- args[a + 3] = b1;
252
- args.splice(a + 4, 0, b2);
253
- // eslint-disable-next-line max-depth
254
- if (rest !== undefined)
255
- args.splice(a + 5, 0, rest);
256
- fixed = true;
257
- }
258
- lflag = args[a + 3];
259
- sweep = fixed ? args[a + 4] : oargs[a + 4]; // we need the original string to deal with leading zeroes
260
- if (!fixed && sweep.length > 1) {
261
- args[a + 4] = parseInt(sweep[0], 10);
262
- args.splice(a + 5, 0, parseFloat(sweep.substring(1)));
263
- }
264
- sweep = args[a + 4];
265
- if (op === 'a') {
266
- x += args[a + 5];
267
- y += args[a + 6];
268
- }
269
- else {
270
- x = args[a + 5];
271
- y = args[a + 6];
272
- }
273
- normalized +=
274
- 'A ' +
275
- rx +
276
- ' ' +
277
- ry +
278
- ' ' +
279
- xrot +
280
- ' ' +
281
- lflag +
282
- ' ' +
283
- sweep +
284
- ' ' +
285
- x +
286
- ' ' +
287
- y +
288
- ' ';
289
- }
143
+ if (instruction.type === 'h') {
144
+ x += instruction.dx;
145
+ normalized.push({
146
+ type: 'H',
147
+ x,
148
+ });
149
+ continue;
290
150
  }
291
- else if (lop === 'z') {
292
- normalized += 'Z ';
293
- // not unimportant: path closing changes the current x/y coordinate
294
- x = sx;
295
- y = sy;
151
+ if (instruction.type === 'v') {
152
+ y += instruction.dy;
153
+ normalized.push({
154
+ type: 'V',
155
+ y,
156
+ });
157
+ continue;
296
158
  }
159
+ // @ts-expect-error
160
+ throw new Error('Unknown instruction type: ' + instruction.type);
297
161
  }
298
- return normalized.trim();
162
+ return normalized;
299
163
  };
300
- exports.normalizePath = normalizePath;
164
+ exports.normalizeInstructions = normalizeInstructions;
@@ -0,0 +1,8 @@
1
+ import type { Instruction } from './helpers/types';
2
+ /**
3
+ * @description Parses an SVG string path into an array of Instruction's.
4
+ * @param {string} path
5
+ * @returns an array of objects containing the Instructions
6
+ * @see [Documentation](https://www.remotion.dev/docs/paths/parse-path)
7
+ */
8
+ export declare const parsePath: (path: string) => Instruction[];