@remotion/paths 3.3.38 → 3.3.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.
Files changed (42) hide show
  1. package/dist/get-bounding-box.d.ts +2 -0
  2. package/dist/get-bounding-box.js +146 -0
  3. package/dist/get-subpaths.js +4 -5
  4. package/dist/helpers/construct.d.ts +1 -2
  5. package/dist/helpers/construct.js +199 -123
  6. package/dist/helpers/iterate.d.ts +12 -0
  7. package/dist/helpers/iterate.js +53 -0
  8. package/dist/helpers/linear.d.ts +6 -1
  9. package/dist/helpers/linear.js +1 -1
  10. package/dist/helpers/remove-a-s-t-curves.d.ts +2 -0
  11. package/dist/helpers/remove-a-s-t-curves.js +260 -0
  12. package/dist/helpers/serialize.d.ts +2 -0
  13. package/dist/helpers/serialize.js +75 -0
  14. package/dist/helpers/types.d.ts +105 -1
  15. package/dist/helpers/unarc.d.ts +2 -0
  16. package/dist/helpers/unarc.js +254 -0
  17. package/dist/helpers/unshort.d.ts +2 -0
  18. package/dist/helpers/unshort.js +65 -0
  19. package/dist/index.d.ts +6 -1
  20. package/dist/index.js +11 -1
  21. package/dist/normalize-path.d.ts +2 -0
  22. package/dist/normalize-path.js +129 -275
  23. package/dist/parse-path.d.ts +2 -0
  24. package/dist/parse-path.js +259 -0
  25. package/dist/parse.d.ts +2 -0
  26. package/dist/parse.js +266 -0
  27. package/dist/reduce-instructions.d.ts +2 -0
  28. package/dist/reduce-instructions.js +10 -0
  29. package/dist/reset-path.d.ts +1 -0
  30. package/dist/reset-path.js +10 -0
  31. package/dist/serialize-instructions.d.ts +2 -0
  32. package/dist/serialize-instructions.js +72 -0
  33. package/dist/serialize.d.ts +2 -0
  34. package/dist/serialize.js +75 -0
  35. package/dist/simplify-instructions.d.ts +2 -0
  36. package/dist/simplify-instructions.js +10 -0
  37. package/dist/translate-path.js +42 -25
  38. package/dist/unarc.d.ts +2 -0
  39. package/dist/unarc.js +180 -0
  40. package/dist/unshort.d.ts +2 -0
  41. package/dist/unshort.js +118 -0
  42. package/package.json +2 -2
@@ -0,0 +1,2 @@
1
+ import type { BoundingBox } from './helpers/types';
2
+ export declare const getBoundingBox: (d: string) => BoundingBox;
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getBoundingBox = void 0;
4
+ const remove_a_s_t_curves_1 = require("./helpers/remove-a-s-t-curves");
5
+ const normalize_path_1 = require("./normalize-path");
6
+ const parse_path_1 = require("./parse-path");
7
+ // Precision for consider cubic polynom as quadratic one
8
+ const CBEZIER_MINMAX_EPSILON = 0.00000001;
9
+ // https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L89
10
+ function minmaxQ(A) {
11
+ const min = Math.min(A[0], A[2]);
12
+ const max = Math.max(A[0], A[2]);
13
+ if (A[1] > A[0] ? A[2] >= A[1] : A[2] <= A[1]) {
14
+ // if no extremum in ]0,1[
15
+ return [min, max];
16
+ }
17
+ // check if the extremum E is min or max
18
+ const E = (A[0] * A[2] - A[1] * A[1]) / (A[0] - 2 * A[1] + A[2]);
19
+ return E < min ? [E, max] : [min, E];
20
+ }
21
+ // https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L127
22
+ function minmaxC(A) {
23
+ const K = A[0] - 3 * A[1] + 3 * A[2] - A[3];
24
+ // if the polynomial is (almost) quadratic and not cubic
25
+ if (Math.abs(K) < CBEZIER_MINMAX_EPSILON) {
26
+ if (A[0] === A[3] && A[0] === A[1]) {
27
+ // no curve, point targeting same location
28
+ return [A[0], A[3]];
29
+ }
30
+ return minmaxQ([
31
+ A[0],
32
+ -0.5 * A[0] + 1.5 * A[1],
33
+ A[0] - 3 * A[1] + 3 * A[2],
34
+ ]);
35
+ }
36
+ // the reduced discriminant of the derivative
37
+ const T = -A[0] * A[2] +
38
+ A[0] * A[3] -
39
+ A[1] * A[2] -
40
+ A[1] * A[3] +
41
+ A[1] * A[1] +
42
+ A[2] * A[2];
43
+ // if the polynomial is monotone in [0,1]
44
+ if (T <= 0) {
45
+ return [Math.min(A[0], A[3]), Math.max(A[0], A[3])];
46
+ }
47
+ const S = Math.sqrt(T);
48
+ // potential extrema
49
+ let min = Math.min(A[0], A[3]);
50
+ let max = Math.max(A[0], A[3]);
51
+ const L = A[0] - 2 * A[1] + A[2];
52
+ // check local extrema
53
+ for (let R = (L + S) / K, i = 1; i <= 2; R = (L - S) / K, i++) {
54
+ if (R > 0 && R < 1) {
55
+ // if the extrema is for R in [0,1]
56
+ const Q = A[0] * (1 - R) * (1 - R) * (1 - R) +
57
+ A[1] * 3 * (1 - R) * (1 - R) * R +
58
+ A[2] * 3 * (1 - R) * R * R +
59
+ A[3] * R * R * R;
60
+ if (Q < min) {
61
+ min = Q;
62
+ }
63
+ if (Q > max) {
64
+ max = Q;
65
+ }
66
+ }
67
+ }
68
+ return [min, max];
69
+ }
70
+ const getBoundingBox = (d) => {
71
+ let minX = Infinity;
72
+ let minY = Infinity;
73
+ let maxX = -Infinity;
74
+ let maxY = -Infinity;
75
+ const parsed = (0, parse_path_1.parsePath)((0, normalize_path_1.normalizePath)(d));
76
+ const unarced = (0, remove_a_s_t_curves_1.removeATSHVInstructions)(parsed);
77
+ let x = 0;
78
+ let y = 0;
79
+ for (const seg of unarced) {
80
+ switch (seg.type) {
81
+ case 'M':
82
+ case 'L': {
83
+ if (minX > seg.x) {
84
+ minX = seg.x;
85
+ }
86
+ if (minY > seg.y) {
87
+ minY = seg.y;
88
+ }
89
+ if (maxX < seg.x) {
90
+ maxX = seg.x;
91
+ }
92
+ if (maxY < seg.y) {
93
+ maxY = seg.y;
94
+ }
95
+ x = seg.x;
96
+ y = seg.y;
97
+ break;
98
+ }
99
+ case 'C': {
100
+ const cxMinMax = minmaxC([x, seg.cp1x, seg.cp2x, seg.x]);
101
+ if (minX > cxMinMax[0]) {
102
+ minX = cxMinMax[0];
103
+ }
104
+ if (maxX < cxMinMax[1]) {
105
+ maxX = cxMinMax[1];
106
+ }
107
+ const cyMinMax = minmaxC([y, seg.cp1y, seg.cp2y, seg.y]);
108
+ if (minY > cyMinMax[0]) {
109
+ minY = cyMinMax[0];
110
+ }
111
+ if (maxY < cyMinMax[1]) {
112
+ maxY = cyMinMax[1];
113
+ }
114
+ x = seg.x;
115
+ y = seg.y;
116
+ break;
117
+ }
118
+ case 'Q': {
119
+ const qxMinMax = minmaxQ([x, seg.cpx, seg.x]);
120
+ if (minX > qxMinMax[0]) {
121
+ minX = qxMinMax[0];
122
+ }
123
+ if (maxX < qxMinMax[1]) {
124
+ maxX = qxMinMax[1];
125
+ }
126
+ const qyMinMax = minmaxQ([y, seg.cpy, seg.y]);
127
+ if (minY > qyMinMax[0]) {
128
+ minY = qyMinMax[0];
129
+ }
130
+ if (maxY < qyMinMax[1]) {
131
+ maxY = qyMinMax[1];
132
+ }
133
+ x = seg.x;
134
+ y = seg.y;
135
+ break;
136
+ }
137
+ case 'Z':
138
+ break;
139
+ default:
140
+ // @ts-expect-error
141
+ throw new Error(`Unknown instruction ${seg.type}`);
142
+ }
143
+ }
144
+ return { x1: minX, y1: minY, x2: maxX, y2: maxY };
145
+ };
146
+ exports.getBoundingBox = getBoundingBox;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getSubpaths = void 0;
4
4
  const construct_1 = require("./helpers/construct");
5
+ const serialize_instructions_1 = require("./serialize-instructions");
5
6
  /**
6
7
  * Splits a valid SVG path into it's parts.
7
8
  * @param {string} path A valid SVG path
@@ -9,10 +10,8 @@ const construct_1 = require("./helpers/construct");
9
10
  */
10
11
  const getSubpaths = (path) => {
11
12
  const { segments } = (0, construct_1.construct)(path);
12
- return segments
13
- .map((seg) => {
14
- return seg.map((s) => s.join(' '));
15
- })
16
- .map((_s) => _s.join(' '));
13
+ return segments.map((seg) => {
14
+ return (0, serialize_instructions_1.serializeInstructions)(seg);
15
+ });
17
16
  };
18
17
  exports.getSubpaths = getSubpaths;
@@ -1,5 +1,4 @@
1
- import type { Instruction } from './parse';
2
- import type { Point, Properties } from './types';
1
+ import type { Instruction, Point, Properties } from './types';
3
2
  export declare const construct: (string: string) => {
4
3
  segments: Instruction[][];
5
4
  initial_point: Point | null;