-
-
Notifications
You must be signed in to change notification settings - Fork 962
/
get-bounding-box.ts
210 lines (171 loc) · 4.28 KB
/
get-bounding-box.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import {removeATSHVQInstructions} from './helpers/remove-a-s-t-curves';
import type {
AbsoluteInstruction,
BoundingBox,
ReducedInstruction,
} from './helpers/types';
import {normalizeInstructions} from './normalize-path';
import {parsePath} from './parse-path';
type minMax = [min: number, max: number];
// Precision for consider cubic polynom as quadratic one
const CBEZIER_MINMAX_EPSILON = 0.00000001;
// https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L89
function minmaxQ(A: [number, number, number]): minMax {
const min = Math.min(A[0], A[2]);
const max = Math.max(A[0], A[2]);
if (A[1] > A[0] ? A[2] >= A[1] : A[2] <= A[1]) {
// if no extremum in ]0,1[
return [min, max];
}
// check if the extremum E is min or max
const E = (A[0] * A[2] - A[1] * A[1]) / (A[0] - 2 * A[1] + A[2]);
return E < min ? [E, max] : [min, E];
}
// https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L127
function minmaxC(A: [number, number, number, number]): minMax {
const K = A[0] - 3 * A[1] + 3 * A[2] - A[3];
// if the polynomial is (almost) quadratic and not cubic
if (Math.abs(K) < CBEZIER_MINMAX_EPSILON) {
if (A[0] === A[3] && A[0] === A[1]) {
// no curve, point targeting same location
return [A[0], A[3]];
}
return minmaxQ([
A[0],
-0.5 * A[0] + 1.5 * A[1],
A[0] - 3 * A[1] + 3 * A[2],
]);
}
// the reduced discriminant of the derivative
const T =
-A[0] * A[2] +
A[0] * A[3] -
A[1] * A[2] -
A[1] * A[3] +
A[1] * A[1] +
A[2] * A[2];
// if the polynomial is monotone in [0,1]
if (T <= 0) {
return [Math.min(A[0], A[3]), Math.max(A[0], A[3])];
}
const S = Math.sqrt(T);
// potential extrema
let min = Math.min(A[0], A[3]);
let max = Math.max(A[0], A[3]);
const L = A[0] - 2 * A[1] + A[2];
// check local extrema
for (let R = (L + S) / K, i = 1; i <= 2; R = (L - S) / K, i++) {
if (R > 0 && R < 1) {
// if the extrema is for R in [0,1]
const Q =
A[0] * (1 - R) * (1 - R) * (1 - R) +
A[1] * 3 * (1 - R) * (1 - R) * R +
A[2] * 3 * (1 - R) * R * R +
A[3] * R * R * R;
if (Q < min) {
min = Q;
}
if (Q > max) {
max = Q;
}
}
}
return [min, max];
}
export const getBoundingBoxFromInstructions = (
instructions: ReducedInstruction[],
): BoundingBox => {
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
let x = 0;
let y = 0;
let lastMoveX = 0;
let lastMoveY = 0;
for (const seg of instructions) {
switch (seg.type) {
case 'M': {
lastMoveX = seg.x;
lastMoveY = seg.y;
if (minX > seg.x) {
minX = seg.x;
}
if (minY > seg.y) {
minY = seg.y;
}
if (maxX < seg.x) {
maxX = seg.x;
}
if (maxY < seg.y) {
maxY = seg.y;
}
x = seg.x;
y = seg.y;
break;
}
case 'L': {
if (minX > seg.x) {
minX = seg.x;
}
if (minY > seg.y) {
minY = seg.y;
}
if (maxX < seg.x) {
maxX = seg.x;
}
if (maxY < seg.y) {
maxY = seg.y;
}
x = seg.x;
y = seg.y;
break;
}
case 'C': {
const cxMinMax = minmaxC([x, seg.cp1x, seg.cp2x, seg.x]);
if (minX > cxMinMax[0]) {
minX = cxMinMax[0];
}
if (maxX < cxMinMax[1]) {
maxX = cxMinMax[1];
}
const cyMinMax = minmaxC([y, seg.cp1y, seg.cp2y, seg.y]);
if (minY > cyMinMax[0]) {
minY = cyMinMax[0];
}
if (maxY < cyMinMax[1]) {
maxY = cyMinMax[1];
}
x = seg.x;
y = seg.y;
break;
}
case 'Z':
x = lastMoveX;
y = lastMoveY;
break;
default:
// @ts-expect-error
throw new Error(`Unknown instruction ${seg.type}`);
}
}
return {
x1: minX,
y1: minY,
x2: maxX,
y2: maxY,
viewBox: `${minX} ${minY} ${maxX - minX} ${maxY - minY}`,
width: maxX - minX,
height: maxY - minY,
};
};
/**
* @description Returns the bounding box of the given path, suitable for calculating the viewBox value that you need to pass to an SVG.
* @param {string} d
* @see [Documentation](https://www.remotion.dev/docs/paths/get-bounding-box)
*/
export const getBoundingBox = (d: string): BoundingBox => {
const parsed = parsePath(d) as AbsoluteInstruction[];
const unarced = removeATSHVQInstructions(normalizeInstructions(parsed));
return getBoundingBoxFromInstructions(unarced);
};