@remotion/paths 4.0.0-newpaths.13 → 4.0.0-newpaths.40

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.
@@ -19,10 +19,110 @@ export interface PointProperties {
19
19
  tangentX: number;
20
20
  tangentY: number;
21
21
  }
22
- export declare type pathOrders = 'a' | 'c' | 'h' | 'l' | 'm' | 'q' | 's' | 't' | 'v' | 'z';
23
22
  export declare type BoundingBox = {
24
23
  x1: number;
25
24
  y1: number;
26
25
  x2: number;
27
26
  y2: number;
28
27
  };
28
+ export declare type ReducesAbsoluteInstruction = {
29
+ type: 'M';
30
+ x: number;
31
+ y: number;
32
+ } | {
33
+ type: 'L';
34
+ x: number;
35
+ y: number;
36
+ } | {
37
+ type: 'H';
38
+ x: number;
39
+ } | {
40
+ type: 'V';
41
+ y: number;
42
+ } | {
43
+ type: 'C';
44
+ cp1x: number;
45
+ cp1y: number;
46
+ cp2x: number;
47
+ cp2y: number;
48
+ x: number;
49
+ y: number;
50
+ } | {
51
+ type: 'Q';
52
+ cpx: number;
53
+ cpy: number;
54
+ x: number;
55
+ y: number;
56
+ } | {
57
+ type: 'Z';
58
+ };
59
+ export declare type AbsoluteInstruction = ReducesAbsoluteInstruction | {
60
+ type: 'A';
61
+ rx: number;
62
+ ry: number;
63
+ xAxisRotation: number;
64
+ largeArcFlag: boolean;
65
+ sweepFlag: boolean;
66
+ x: number;
67
+ y: number;
68
+ } | {
69
+ type: 'S';
70
+ cpx: number;
71
+ cpy: number;
72
+ x: number;
73
+ y: number;
74
+ } | {
75
+ type: 'T';
76
+ x: number;
77
+ y: number;
78
+ };
79
+ export declare type Instruction = AbsoluteInstruction | {
80
+ type: 'm';
81
+ dx: number;
82
+ dy: number;
83
+ } | {
84
+ type: 'l';
85
+ dx: number;
86
+ dy: number;
87
+ } | {
88
+ type: 'h';
89
+ dx: number;
90
+ } | {
91
+ type: 'v';
92
+ dy: number;
93
+ } | {
94
+ type: 'c';
95
+ cp1dx: number;
96
+ cp1dy: number;
97
+ cp2dx: number;
98
+ cp2dy: number;
99
+ dx: number;
100
+ dy: number;
101
+ } | {
102
+ type: 's';
103
+ cpdx: number;
104
+ cpdy: number;
105
+ dx: number;
106
+ dy: number;
107
+ } | {
108
+ type: 'q';
109
+ cpdx: number;
110
+ cpdy: number;
111
+ dx: number;
112
+ dy: number;
113
+ } | {
114
+ type: 't';
115
+ dx: number;
116
+ dy: number;
117
+ } | {
118
+ type: 'a';
119
+ rx: number;
120
+ ry: number;
121
+ xAxisRotation: number;
122
+ largeArcFlag: boolean;
123
+ sweepFlag: boolean;
124
+ dx: number;
125
+ dy: number;
126
+ } | {
127
+ type: 'z';
128
+ };
@@ -0,0 +1,2 @@
1
+ import type { AbsoluteInstruction, UnarcedAbsoluteInstruction } from './types';
2
+ export declare const removeArcInstructions: (segments: AbsoluteInstruction[]) => UnarcedAbsoluteInstruction[];
@@ -0,0 +1,254 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.removeArcInstructions = void 0;
4
+ const iterate_1 = require("./iterate");
5
+ const TAU = Math.PI * 2;
6
+ function approximate_unit_arc(theta1, delta_theta) {
7
+ const alpha = (4 / 3) * Math.tan(delta_theta / 4);
8
+ const x1 = Math.cos(theta1);
9
+ const y1 = Math.sin(theta1);
10
+ const x2 = Math.cos(theta1 + delta_theta);
11
+ const y2 = Math.sin(theta1 + delta_theta);
12
+ return [
13
+ x1,
14
+ y1,
15
+ x1 - y1 * alpha,
16
+ y1 + x1 * alpha,
17
+ x2 + y2 * alpha,
18
+ y2 - x2 * alpha,
19
+ x2,
20
+ y2,
21
+ ];
22
+ }
23
+ function arcToCircle({ x1, y1, x2, y2, largeArcFlag, sweepFlag, rx, ry, phi, }) {
24
+ const sin_phi = Math.sin((phi * TAU) / 360);
25
+ const cos_phi = Math.cos((phi * TAU) / 360);
26
+ // Make sure radii are valid
27
+ //
28
+ const x1p = (cos_phi * (x1 - x2)) / 2 + (sin_phi * (y1 - y2)) / 2;
29
+ const y1p = (-sin_phi * (x1 - x2)) / 2 + (cos_phi * (y1 - y2)) / 2;
30
+ if (x1p === 0 && y1p === 0) {
31
+ // we're asked to draw line to itself
32
+ return [];
33
+ }
34
+ if (rx === 0 || ry === 0) {
35
+ // one of the radii is zero
36
+ return [];
37
+ }
38
+ // Compensate out-of-range radii
39
+ //
40
+ rx = Math.abs(rx);
41
+ ry = Math.abs(ry);
42
+ const lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
43
+ if (lambda > 1) {
44
+ rx *= Math.sqrt(lambda);
45
+ ry *= Math.sqrt(lambda);
46
+ }
47
+ // Get center parameters (cx, cy, theta1, delta_theta)
48
+ //
49
+ const cc = get_arc_center({
50
+ x1,
51
+ y1,
52
+ x2,
53
+ y2,
54
+ largeArcFlag,
55
+ sweepFlag,
56
+ rx,
57
+ ry,
58
+ sin_phi,
59
+ cos_phi,
60
+ });
61
+ const result = [];
62
+ let theta1 = cc[2];
63
+ let delta_theta = cc[3];
64
+ // Split an arc to multiple segments, so each segment
65
+ // will be less than τ/4 (= 90°)
66
+ //
67
+ const segments = Math.max(Math.ceil(Math.abs(delta_theta) / (TAU / 4)), 1);
68
+ delta_theta /= segments;
69
+ for (let i = 0; i < segments; i++) {
70
+ result.push(approximate_unit_arc(theta1, delta_theta));
71
+ theta1 += delta_theta;
72
+ }
73
+ // We have a bezier approximation of a unit circle,
74
+ // now need to transform back to the original ellipse
75
+ //
76
+ return result.map((curve) => {
77
+ for (let i = 0; i < curve.length; i += 2) {
78
+ let x = curve[i + 0];
79
+ let y = curve[i + 1];
80
+ // scale
81
+ x *= rx;
82
+ y *= ry;
83
+ // rotate
84
+ const xp = cos_phi * x - sin_phi * y;
85
+ const yp = sin_phi * x + cos_phi * y;
86
+ // translate
87
+ curve[i + 0] = xp + cc[0];
88
+ curve[i + 1] = yp + cc[1];
89
+ }
90
+ return curve;
91
+ });
92
+ }
93
+ // Requires path to be normalized
94
+ const removeArcInstructions = (segments) => {
95
+ let prevControlX = 0;
96
+ let prevControlY = 0;
97
+ let curControlX = 0;
98
+ let curControlY = 0;
99
+ return (0, iterate_1.iterateOverSegments)({
100
+ segments,
101
+ iterate: ({ segment, prevSegment, x, y }) => {
102
+ if (segment.type === 'A') {
103
+ const nextX = segment.x;
104
+ const nextY = segment.y;
105
+ const new_segments = arcToCircle({
106
+ x1: x,
107
+ y1: y,
108
+ x2: nextX,
109
+ y2: nextY,
110
+ largeArcFlag: segment.largeArcFlag,
111
+ sweepFlag: segment.sweepFlag,
112
+ rx: segment.rx,
113
+ ry: segment.ry,
114
+ phi: segment.xAxisRotation,
115
+ });
116
+ // Degenerated arcs can be ignored by renderer, but should not be dropped
117
+ // to avoid collisions with `S A S` and so on. Replace with empty line.
118
+ if (new_segments.length === 0) {
119
+ return [
120
+ {
121
+ type: 'L',
122
+ x: segment.x,
123
+ y: segment.y,
124
+ },
125
+ ];
126
+ }
127
+ const result = new_segments.map((_s) => {
128
+ return {
129
+ type: 'C',
130
+ cp1x: _s[2],
131
+ cp1y: _s[3],
132
+ cp2x: _s[4],
133
+ cp2y: _s[5],
134
+ x: _s[6],
135
+ y: _s[7],
136
+ };
137
+ });
138
+ return result;
139
+ }
140
+ if (segment.type === 'T') {
141
+ if (prevSegment && prevSegment.type === 'Q') {
142
+ prevControlX = prevSegment.cpx;
143
+ prevControlY = prevSegment.cpy;
144
+ }
145
+ else {
146
+ prevControlX = 0;
147
+ prevControlY = 0;
148
+ }
149
+ curControlX = -prevControlX;
150
+ curControlY = -prevControlY;
151
+ return [
152
+ {
153
+ type: 'Q',
154
+ cpx: curControlX,
155
+ cpy: curControlY,
156
+ x: segment.x,
157
+ y: segment.y,
158
+ },
159
+ ];
160
+ }
161
+ if (segment.type === 'S') {
162
+ if (prevSegment && prevSegment.type === 'C') {
163
+ prevControlX = prevSegment.cp2x;
164
+ prevControlY = prevSegment.cp2y;
165
+ }
166
+ else {
167
+ prevControlX = 0;
168
+ prevControlY = 0;
169
+ }
170
+ curControlX = -prevControlX;
171
+ curControlY = -prevControlY;
172
+ return [
173
+ {
174
+ type: 'C',
175
+ cp1x: curControlX,
176
+ cp1y: curControlY,
177
+ cp2x: segment.cpx,
178
+ cp2y: segment.cpy,
179
+ x: segment.x,
180
+ y: segment.y,
181
+ },
182
+ ];
183
+ }
184
+ return [segment];
185
+ },
186
+ });
187
+ };
188
+ exports.removeArcInstructions = removeArcInstructions;
189
+ function get_arc_center({ x1, y1, x2, y2, largeArcFlag, sweepFlag, rx, ry, sin_phi, cos_phi, }) {
190
+ // Step 1.
191
+ //
192
+ // Moving an ellipse so origin will be the middlepoint between our two
193
+ // points. After that, rotate it to line up ellipse axes with coordinate
194
+ // axes.
195
+ //
196
+ const x1p = (cos_phi * (x1 - x2)) / 2 + (sin_phi * (y1 - y2)) / 2;
197
+ const y1p = (-sin_phi * (x1 - x2)) / 2 + (cos_phi * (y1 - y2)) / 2;
198
+ const rx_sq = rx * rx;
199
+ const ry_sq = ry * ry;
200
+ const x1p_sq = x1p * x1p;
201
+ const y1p_sq = y1p * y1p;
202
+ // Step 2.
203
+ //
204
+ // Compute coordinates of the centre of this ellipse (cx', cy')
205
+ // in the new coordinate system.
206
+ //
207
+ let radicant = rx_sq * ry_sq - rx_sq * y1p_sq - ry_sq * x1p_sq;
208
+ if (radicant < 0) {
209
+ // due to rounding errors it might be e.g. -1.3877787807814457e-17
210
+ radicant = 0;
211
+ }
212
+ radicant /= rx_sq * y1p_sq + ry_sq * x1p_sq;
213
+ radicant = Math.sqrt(radicant) * (largeArcFlag === sweepFlag ? -1 : 1);
214
+ const cxp = ((radicant * rx) / ry) * y1p;
215
+ const cyp = ((radicant * -ry) / rx) * x1p;
216
+ // Step 3.
217
+ //
218
+ // Transform back to get centre coordinates (cx, cy) in the original
219
+ // coordinate system.
220
+ //
221
+ const cx = cos_phi * cxp - sin_phi * cyp + (x1 + x2) / 2;
222
+ const cy = sin_phi * cxp + cos_phi * cyp + (y1 + y2) / 2;
223
+ // Step 4.
224
+ //
225
+ // Compute angles (theta1, delta_theta).
226
+ //
227
+ const v1x = (x1p - cxp) / rx;
228
+ const v1y = (y1p - cyp) / ry;
229
+ const v2x = (-x1p - cxp) / rx;
230
+ const v2y = (-y1p - cyp) / ry;
231
+ const theta1 = unit_vector_angle(1, 0, v1x, v1y);
232
+ let delta_theta = unit_vector_angle(v1x, v1y, v2x, v2y);
233
+ if (sweepFlag === false && delta_theta > 0) {
234
+ delta_theta -= TAU;
235
+ }
236
+ if (sweepFlag === true && delta_theta < 0) {
237
+ delta_theta += TAU;
238
+ }
239
+ return [cx, cy, theta1, delta_theta];
240
+ }
241
+ function unit_vector_angle(ux, uy, vx, vy) {
242
+ const sign = ux * vy - uy * vx < 0 ? -1 : 1;
243
+ let dot = ux * vx + uy * vy;
244
+ // Add this to work with arbitrary vectors:
245
+ // dot /= Math.sqrt(ux * ux + uy * uy) * Math.sqrt(vx * vx + vy * vy);
246
+ // rounding errors, e.g. -1.0000000000000002 can screw up this
247
+ if (dot > 1.0) {
248
+ dot = 1.0;
249
+ }
250
+ if (dot < -1.0) {
251
+ dot = -1.0;
252
+ }
253
+ return sign * Math.acos(dot);
254
+ }
@@ -0,0 +1,2 @@
1
+ import type { Instruction } from './types';
2
+ export declare const unshort: (segments: Instruction[]) => Instruction[];
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unshort = void 0;
4
+ // Requires that the path is already normalized
5
+ const unshort = function (segments) {
6
+ let prevControlX = 0;
7
+ let prevControlY = 0;
8
+ let curControlX = 0;
9
+ let curControlY = 0;
10
+ const newSegments = segments.slice(0);
11
+ for (let i = 0; i < newSegments.length; i++) {
12
+ const s = newSegments[i];
13
+ // First command MUST be M|m, it's safe to skip.
14
+ // Protect from access to [-1] for sure.
15
+ if (!i) {
16
+ continue;
17
+ }
18
+ if (s.type === 'T') {
19
+ // quadratic curve
20
+ const prevSegment = newSegments[i - 1];
21
+ if (prevSegment.type === 'Q') {
22
+ prevControlX = prevSegment.cpx;
23
+ prevControlY = prevSegment.cpy;
24
+ }
25
+ else {
26
+ prevControlX = 0;
27
+ prevControlY = 0;
28
+ }
29
+ curControlX = -prevControlX;
30
+ curControlY = -prevControlY;
31
+ newSegments[i] = {
32
+ type: 'Q',
33
+ cpx: curControlX,
34
+ cpy: curControlY,
35
+ x: s.x,
36
+ y: s.y,
37
+ };
38
+ }
39
+ else if (s.type === 'S') {
40
+ // cubic curve
41
+ const prevSegment = newSegments[i - 1];
42
+ if (prevSegment.type === 'C') {
43
+ prevControlX = prevSegment.cp2x;
44
+ prevControlY = prevSegment.cp2y;
45
+ }
46
+ else {
47
+ prevControlX = 0;
48
+ prevControlY = 0;
49
+ }
50
+ curControlX = -prevControlX;
51
+ curControlY = -prevControlY;
52
+ newSegments[i] = {
53
+ type: 'C',
54
+ cp1x: curControlX,
55
+ cp1y: curControlY,
56
+ cp2x: s.cpx,
57
+ cp2y: s.cpy,
58
+ x: s.x,
59
+ y: s.y,
60
+ };
61
+ }
62
+ }
63
+ return newSegments;
64
+ };
65
+ exports.unshort = unshort;
package/dist/index.d.ts CHANGED
@@ -6,9 +6,11 @@ export { getParts } from './get-parts';
6
6
  export { getPointAtLength } from './get-point-at-length';
7
7
  export { getSubpaths } from './get-subpaths';
8
8
  export { getTangentAtLength } from './get-tangent-at-length';
9
- export { BoundingBox, Part } from './helpers/types';
9
+ export { BoundingBox, Instruction, Part } from './helpers/types';
10
10
  export { interpolatePath } from './interpolate-path';
11
11
  export { normalizePath } from './normalize-path';
12
+ export { parsePath } from './parse-path';
12
13
  export { resetPath } from './reset-path';
13
14
  export { reversePath } from './reverse-path';
15
+ export { serializeInstructions } from './serialize-instructions';
14
16
  export { translatePath } from './translate-path';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.translatePath = exports.reversePath = exports.resetPath = exports.normalizePath = exports.interpolatePath = exports.getTangentAtLength = exports.getSubpaths = exports.getPointAtLength = exports.getParts = exports.getLength = exports.getBoundingBox = exports.extendViewBox = exports.evolvePath = void 0;
3
+ exports.translatePath = exports.serializeInstructions = exports.reversePath = exports.resetPath = exports.parsePath = exports.normalizePath = exports.interpolatePath = exports.getTangentAtLength = exports.getSubpaths = exports.getPointAtLength = exports.getParts = exports.getLength = exports.getBoundingBox = exports.extendViewBox = exports.evolvePath = void 0;
4
4
  var evolve_path_1 = require("./evolve-path");
5
5
  Object.defineProperty(exports, "evolvePath", { enumerable: true, get: function () { return evolve_path_1.evolvePath; } });
6
6
  var extend_viewbox_1 = require("./extend-viewbox");
@@ -21,9 +21,13 @@ var interpolate_path_1 = require("./interpolate-path");
21
21
  Object.defineProperty(exports, "interpolatePath", { enumerable: true, get: function () { return interpolate_path_1.interpolatePath; } });
22
22
  var normalize_path_1 = require("./normalize-path");
23
23
  Object.defineProperty(exports, "normalizePath", { enumerable: true, get: function () { return normalize_path_1.normalizePath; } });
24
+ var parse_path_1 = require("./parse-path");
25
+ Object.defineProperty(exports, "parsePath", { enumerable: true, get: function () { return parse_path_1.parsePath; } });
24
26
  var reset_path_1 = require("./reset-path");
25
27
  Object.defineProperty(exports, "resetPath", { enumerable: true, get: function () { return reset_path_1.resetPath; } });
26
28
  var reverse_path_1 = require("./reverse-path");
27
29
  Object.defineProperty(exports, "reversePath", { enumerable: true, get: function () { return reverse_path_1.reversePath; } });
30
+ var serialize_instructions_1 = require("./serialize-instructions");
31
+ Object.defineProperty(exports, "serializeInstructions", { enumerable: true, get: function () { return serialize_instructions_1.serializeInstructions; } });
28
32
  var translate_path_1 = require("./translate-path");
29
33
  Object.defineProperty(exports, "translatePath", { enumerable: true, get: function () { return translate_path_1.translatePath; } });
@@ -0,0 +1,2 @@
1
+ import type { Instruction } from './helpers/types';
2
+ export declare const parsePath: (path: string) => Instruction[];