@iyio/svg-charts 0.7.43 → 0.8.0

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 (40) hide show
  1. package/package.json +8 -13
  2. package/src/index.d.ts +5 -5
  3. package/src/index.js +5 -5
  4. package/src/index.js.map +1 -1
  5. package/src/lib/SvgBarChartCtrl.d.ts +2 -2
  6. package/src/lib/SvgBarChartCtrl.js +23 -14
  7. package/src/lib/SvgBarChartCtrl.js.map +1 -1
  8. package/src/lib/SvgBaseChartCtrl.d.ts +1 -1
  9. package/src/lib/SvgBaseChartCtrl.js +47 -34
  10. package/src/lib/SvgBaseChartCtrl.js.map +1 -1
  11. package/src/lib/SvgLineChartCtrl.d.ts +2 -2
  12. package/src/lib/SvgLineChartCtrl.js +29 -20
  13. package/src/lib/SvgLineChartCtrl.js.map +1 -1
  14. package/src/lib/path-intersection.js +6 -5
  15. package/src/lib/path-intersection.js.map +1 -1
  16. package/src/lib/svg-charts-lib.d.ts +1 -1
  17. package/src/package.json +1 -4
  18. package/tsconfig.tsbuildinfo +1 -0
  19. package/esm/package.json +0 -3
  20. package/esm/src/index.d.ts +0 -5
  21. package/esm/src/index.js +0 -6
  22. package/esm/src/index.js.map +0 -1
  23. package/esm/src/lib/SvgBarChartCtrl.d.ts +0 -11
  24. package/esm/src/lib/SvgBarChartCtrl.js +0 -77
  25. package/esm/src/lib/SvgBarChartCtrl.js.map +0 -1
  26. package/esm/src/lib/SvgBaseChartCtrl.d.ts +0 -69
  27. package/esm/src/lib/SvgBaseChartCtrl.js +0 -553
  28. package/esm/src/lib/SvgBaseChartCtrl.js.map +0 -1
  29. package/esm/src/lib/SvgLineChartCtrl.d.ts +0 -14
  30. package/esm/src/lib/SvgLineChartCtrl.js +0 -217
  31. package/esm/src/lib/SvgLineChartCtrl.js.map +0 -1
  32. package/esm/src/lib/path-intersection.d.ts +0 -35
  33. package/esm/src/lib/path-intersection.js +0 -657
  34. package/esm/src/lib/path-intersection.js.map +0 -1
  35. package/esm/src/lib/svg-charts-lib.d.ts +0 -8
  36. package/esm/src/lib/svg-charts-lib.js +0 -64
  37. package/esm/src/lib/svg-charts-lib.js.map +0 -1
  38. package/esm/src/lib/svg-charts-types.d.ts +0 -82
  39. package/esm/src/lib/svg-charts-types.js +0 -2
  40. package/esm/src/lib/svg-charts-types.js.map +0 -1
@@ -1,657 +0,0 @@
1
- /* eslint-disable prefer-spread */
2
- // Sorry I know this file is mess. It is a direct port of old style javascript,
3
- // but it does work really well. So since it's not broke, I'm not fixing it.
4
- /**
5
- * This file contains source code adapted from Snap.svg (licensed Apache-2.0).
6
- *
7
- * @see https://github.com/adobe-webplatform/Snap.svg/blob/master/src/path.js
8
- */
9
- /* eslint no-fallthrough: "off" */
10
- const p2s = /,?([a-z]),?/gi, toFloat = parseFloat, math = Math, PI = math.PI, mmin = math.min, mmax = math.max, pow = math.pow, abs = math.abs, pathCommand = /([a-z])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?[\s]*,?[\s]*)+)/ig, pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)[\s]*,?[\s]*/ig;
11
- const isArray = Array.isArray || function (o) { return o instanceof Array; }; // todo - remove
12
- const hasProperty = (obj, property) => {
13
- return Object.prototype.hasOwnProperty.call(obj, property);
14
- };
15
- function clone(obj) {
16
- if (typeof obj == 'function' || Object(obj) !== obj) {
17
- return obj;
18
- }
19
- const res = new obj.constructor;
20
- for (const key in obj) {
21
- if (hasProperty(obj, key)) {
22
- res[key] = clone(obj[key]);
23
- }
24
- }
25
- return res;
26
- }
27
- function repush(array, item) {
28
- for (let i = 0, ii = array.length; i < ii; i++)
29
- if (array[i] === item) {
30
- return array.push(array.splice(i, 1)[0]);
31
- }
32
- return undefined;
33
- }
34
- function cacher(f) {
35
- function newf() {
36
- // eslint-disable-next-line prefer-rest-params
37
- const arg = Array.prototype.slice.call(arguments, 0), args = arg.join('\u2400'), cache = newf.cache = newf.cache || {}, count = newf.count = newf.count || [];
38
- if (hasProperty(cache, args)) {
39
- repush(count, args);
40
- return cache[args];
41
- }
42
- count.length >= 1e3 && delete cache[count.shift()];
43
- count.push(args);
44
- cache[args] = f.apply(0, arg);
45
- return cache[args];
46
- }
47
- return newf;
48
- }
49
- function parsePathString(pathString) {
50
- if (!pathString) {
51
- return null;
52
- }
53
- const pth = paths(pathString);
54
- if (pth.arr) {
55
- return clone(pth.arr);
56
- }
57
- const paramCounts = { a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0 };
58
- let data = [];
59
- if (isArray(pathString) && isArray(pathString[0])) { // rough assumption
60
- data = clone(pathString);
61
- }
62
- if (!data.length) {
63
- String(pathString).replace(pathCommand, function (a, b, c) {
64
- const params = [];
65
- let name = b.toLowerCase();
66
- c.replace(pathValues, function (a, b) {
67
- b && params.push(+b);
68
- });
69
- if (name == 'm' && params.length > 2) {
70
- data.push([b].concat(params.splice(0, 2)));
71
- name = 'l';
72
- b = b == 'm' ? 'l' : 'L';
73
- }
74
- while (params.length >= paramCounts[name]) {
75
- data.push([b].concat(params.splice(0, paramCounts[name])));
76
- if (!paramCounts[name]) {
77
- break;
78
- }
79
- }
80
- return '';
81
- });
82
- }
83
- data.toString = paths.toString;
84
- pth.arr = clone(data);
85
- return data;
86
- }
87
- function paths(ps) {
88
- const p = paths.ps = paths.ps || {};
89
- if (p[ps]) {
90
- p[ps].sleep = 100;
91
- }
92
- else {
93
- p[ps] = {
94
- sleep: 100
95
- };
96
- }
97
- setTimeout(function () {
98
- for (const key in p) {
99
- if (hasProperty(p, key) && key != ps) {
100
- p[key].sleep--;
101
- !p[key].sleep && delete p[key];
102
- }
103
- }
104
- });
105
- return p[ps];
106
- }
107
- function rectBBox(x, y, width, height) {
108
- if (arguments.length === 1) {
109
- y = x.y;
110
- width = x.width;
111
- height = x.height;
112
- x = x.x;
113
- }
114
- return {
115
- x: x,
116
- y: y,
117
- width: width,
118
- height: height,
119
- x2: x + width,
120
- y2: y + height
121
- };
122
- }
123
- function pathToString() {
124
- return this.join(',').replace(p2s, '$1');
125
- }
126
- function pathClone(pathArray) {
127
- const res = clone(pathArray);
128
- res.toString = pathToString;
129
- return res;
130
- }
131
- function findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
132
- const t1 = 1 - t, t13 = pow(t1, 3), t12 = pow(t1, 2), t2 = t * t, t3 = t2 * t, x = t13 * p1x + t12 * 3 * t * c1x + t1 * 3 * t * t * c2x + t3 * p2x, y = t13 * p1y + t12 * 3 * t * c1y + t1 * 3 * t * t * c2y + t3 * p2y;
133
- return {
134
- x: fixError(x),
135
- y: fixError(y)
136
- };
137
- }
138
- function bezierBBox(points) {
139
- const bbox = curveBBox.apply(null, points);
140
- return rectBBox(bbox.x0, bbox.y0, bbox.x1 - bbox.x0, bbox.y1 - bbox.y0);
141
- }
142
- function isPointInsideBBox(bbox, x, y) {
143
- return x >= bbox.x &&
144
- x <= bbox.x + bbox.width &&
145
- y >= bbox.y &&
146
- y <= bbox.y + bbox.height;
147
- }
148
- function isBBoxIntersect(bbox1, bbox2) {
149
- bbox1 = rectBBox(bbox1);
150
- bbox2 = rectBBox(bbox2);
151
- return isPointInsideBBox(bbox2, bbox1.x, bbox1.y)
152
- || isPointInsideBBox(bbox2, bbox1.x2, bbox1.y)
153
- || isPointInsideBBox(bbox2, bbox1.x, bbox1.y2)
154
- || isPointInsideBBox(bbox2, bbox1.x2, bbox1.y2)
155
- || isPointInsideBBox(bbox1, bbox2.x, bbox2.y)
156
- || isPointInsideBBox(bbox1, bbox2.x2, bbox2.y)
157
- || isPointInsideBBox(bbox1, bbox2.x, bbox2.y2)
158
- || isPointInsideBBox(bbox1, bbox2.x2, bbox2.y2)
159
- || (bbox1.x < bbox2.x2 && bbox1.x > bbox2.x
160
- || bbox2.x < bbox1.x2 && bbox2.x > bbox1.x)
161
- && (bbox1.y < bbox2.y2 && bbox1.y > bbox2.y
162
- || bbox2.y < bbox1.y2 && bbox2.y > bbox1.y);
163
- }
164
- function base3(t, p1, p2, p3, p4) {
165
- const t1 = -3 * p1 + 9 * p2 - 9 * p3 + 3 * p4, t2 = t * t1 + 6 * p1 - 12 * p2 + 6 * p3;
166
- return t * t2 - 3 * p1 + 3 * p2;
167
- }
168
- function bezlen(x1, y1, x2, y2, x3, y3, x4, y4, z) {
169
- if (z == null) {
170
- z = 1;
171
- }
172
- z = z > 1 ? 1 : z < 0 ? 0 : z;
173
- const z2 = z / 2, n = 12, Tvalues = [-.1252, .1252, -.3678, .3678, -.5873, .5873, -.7699, .7699, -.9041, .9041, -.9816, .9816], Cvalues = [0.2491, 0.2491, 0.2335, 0.2335, 0.2032, 0.2032, 0.1601, 0.1601, 0.1069, 0.1069, 0.0472, 0.0472];
174
- let sum = 0;
175
- for (let i = 0; i < n; i++) {
176
- const ct = z2 * Tvalues[i] + z2, xbase = base3(ct, x1, x2, x3, x4), ybase = base3(ct, y1, y2, y3, y4), comb = xbase * xbase + ybase * ybase;
177
- sum += Cvalues[i] * math.sqrt(comb);
178
- }
179
- return z2 * sum;
180
- }
181
- function intersectLines(x1, y1, x2, y2, x3, y3, x4, y4) {
182
- if (mmax(x1, x2) < mmin(x3, x4) ||
183
- mmin(x1, x2) > mmax(x3, x4) ||
184
- mmax(y1, y2) < mmin(y3, y4) ||
185
- mmin(y1, y2) > mmax(y3, y4)) {
186
- return;
187
- }
188
- const nx = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4), ny = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4), denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
189
- if (!denominator) {
190
- return;
191
- }
192
- const px = fixError(nx / denominator), py = fixError(ny / denominator), px2 = +px.toFixed(2), py2 = +py.toFixed(2);
193
- if (px2 < +mmin(x1, x2).toFixed(2) ||
194
- px2 > +mmax(x1, x2).toFixed(2) ||
195
- px2 < +mmin(x3, x4).toFixed(2) ||
196
- px2 > +mmax(x3, x4).toFixed(2) ||
197
- py2 < +mmin(y1, y2).toFixed(2) ||
198
- py2 > +mmax(y1, y2).toFixed(2) ||
199
- py2 < +mmin(y3, y4).toFixed(2) ||
200
- py2 > +mmax(y3, y4).toFixed(2)) {
201
- return;
202
- }
203
- return { x: px, y: py };
204
- }
205
- function fixError(number) {
206
- return Math.round(number * 100000000000) / 100000000000;
207
- }
208
- function findBezierIntersections(bez1, bez2, justCount) {
209
- const bbox1 = bezierBBox(bez1), bbox2 = bezierBBox(bez2);
210
- if (!isBBoxIntersect(bbox1, bbox2)) {
211
- return justCount ? 0 : [];
212
- }
213
- // As an optimization, lines will have only 1 segment
214
- const l1 = bezlen.apply(0, bez1), l2 = bezlen.apply(0, bez2), n1 = isLine(bez1) ? 1 : ~~(l1 / 5) || 1, n2 = isLine(bez2) ? 1 : ~~(l2 / 5) || 1, dots1 = [], dots2 = [], xy = {};
215
- let res = justCount ? 0 : [];
216
- let i = 0;
217
- for (i = 0; i < n1 + 1; i++) {
218
- const p = findDotsAtSegment.apply(0, bez1.concat(i / n1));
219
- dots1.push({ x: p.x, y: p.y, t: i / n1 });
220
- }
221
- for (i = 0; i < n2 + 1; i++) {
222
- const p = findDotsAtSegment.apply(0, bez2.concat(i / n2));
223
- dots2.push({ x: p.x, y: p.y, t: i / n2 });
224
- }
225
- for (i = 0; i < n1; i++) {
226
- for (let j = 0; j < n2; j++) {
227
- const di = dots1[i], di1 = dots1[i + 1], dj = dots2[j], dj1 = dots2[j + 1], ci = abs(di1.x - di.x) < .01 ? 'y' : 'x', cj = abs(dj1.x - dj.x) < .01 ? 'y' : 'x', is = intersectLines(di.x, di.y, di1.x, di1.y, dj.x, dj.y, dj1.x, dj1.y);
228
- let key;
229
- if (is) {
230
- key = is.x.toFixed(9) + '#' + is.y.toFixed(9);
231
- if (xy[key]) {
232
- continue;
233
- }
234
- xy[key] = true;
235
- const t1 = di.t + abs((is[ci] - di[ci]) / (di1[ci] - di[ci])) * (di1.t - di.t), t2 = dj.t + abs((is[cj] - dj[cj]) / (dj1[cj] - dj[cj])) * (dj1.t - dj.t);
236
- if (t1 >= 0 && t1 <= 1 && t2 >= 0 && t2 <= 1) {
237
- if (justCount) {
238
- res++;
239
- }
240
- else {
241
- res.push({
242
- x: is.x,
243
- y: is.y,
244
- t1: t1,
245
- t2: t2
246
- });
247
- }
248
- }
249
- }
250
- }
251
- }
252
- return res;
253
- }
254
- export function findPathIntersections(path1, path2, justCount) {
255
- path1 = pathToCurve(path1);
256
- path2 = pathToCurve(path2);
257
- let x1, y1, x2, y2, x1m, y1m, x2m, y2m, bez1, bez2, res = justCount ? 0 : [];
258
- for (let i = 0, ii = path1.length; i < ii; i++) {
259
- const pi = path1[i];
260
- if (pi[0] == 'M') {
261
- x1 = x1m = pi[1];
262
- y1 = y1m = pi[2];
263
- }
264
- else {
265
- if (pi[0] == 'C') {
266
- bez1 = [x1, y1].concat(pi.slice(1));
267
- x1 = bez1[6];
268
- y1 = bez1[7];
269
- }
270
- else {
271
- bez1 = [x1, y1, x1, y1, x1m, y1m, x1m, y1m];
272
- x1 = x1m;
273
- y1 = y1m;
274
- }
275
- for (let j = 0, jj = path2.length; j < jj; j++) {
276
- const pj = path2[j];
277
- if (pj[0] == 'M') {
278
- x2 = x2m = pj[1];
279
- y2 = y2m = pj[2];
280
- }
281
- else {
282
- if (pj[0] == 'C') {
283
- bez2 = [x2, y2].concat(pj.slice(1));
284
- x2 = bez2[6];
285
- y2 = bez2[7];
286
- }
287
- else {
288
- bez2 = [x2, y2, x2, y2, x2m, y2m, x2m, y2m];
289
- x2 = x2m;
290
- y2 = y2m;
291
- }
292
- const intr = findBezierIntersections(bez1, bez2, justCount);
293
- if (justCount) {
294
- res += intr;
295
- }
296
- else {
297
- for (let k = 0, kk = intr.length; k < kk; k++) {
298
- intr[k].segment1 = i;
299
- intr[k].segment2 = j;
300
- intr[k].bez1 = bez1;
301
- intr[k].bez2 = bez2;
302
- }
303
- res = res.concat(intr);
304
- }
305
- }
306
- }
307
- }
308
- }
309
- return res;
310
- }
311
- function pathToAbsolute(pathArray) {
312
- const pth = paths(pathArray);
313
- if (pth.abs) {
314
- return pathClone(pth.abs);
315
- }
316
- if (!isArray(pathArray) || !isArray(pathArray && pathArray[0])) { // rough assumption
317
- pathArray = parsePathString(pathArray);
318
- }
319
- if (!pathArray || !pathArray.length) {
320
- return [['M', 0, 0]];
321
- }
322
- const res = [];
323
- let x = 0, y = 0, mx = 0, my = 0, start = 0, pa0;
324
- if (pathArray[0][0] == 'M') {
325
- x = +pathArray[0][1];
326
- y = +pathArray[0][2];
327
- mx = x;
328
- my = y;
329
- start++;
330
- res[0] = ['M', x, y];
331
- }
332
- for (let r, pa, i = start, ii = pathArray.length; i < ii; i++) {
333
- res.push(r = []);
334
- pa = pathArray[i];
335
- pa0 = pa[0];
336
- if (pa0 != pa0.toUpperCase()) {
337
- r[0] = pa0.toUpperCase();
338
- switch (r[0]) {
339
- case 'A':
340
- r[1] = pa[1];
341
- r[2] = pa[2];
342
- r[3] = pa[3];
343
- r[4] = pa[4];
344
- r[5] = pa[5];
345
- r[6] = +pa[6] + x;
346
- r[7] = +pa[7] + y;
347
- break;
348
- case 'V':
349
- r[1] = +pa[1] + y;
350
- break;
351
- case 'H':
352
- r[1] = +pa[1] + x;
353
- break;
354
- default:
355
- if (r[0] === 'M') {
356
- mx = +pa[1] + x;
357
- my = +pa[2] + y;
358
- }
359
- for (let j = 1, jj = pa.length; j < jj; j++) {
360
- r[j] = +pa[j] + ((j % 2) ? x : y);
361
- }
362
- }
363
- }
364
- else {
365
- for (let k = 0, kk = pa.length; k < kk; k++) {
366
- r[k] = pa[k];
367
- }
368
- }
369
- pa0 = pa0.toUpperCase();
370
- switch (r[0]) {
371
- case 'Z':
372
- x = +mx;
373
- y = +my;
374
- break;
375
- case 'H':
376
- x = r[1];
377
- break;
378
- case 'V':
379
- y = r[1];
380
- break;
381
- default:
382
- if (r[0] === 'M') {
383
- mx = r[r.length - 2];
384
- my = r[r.length - 1];
385
- }
386
- x = r[r.length - 2];
387
- y = r[r.length - 1];
388
- }
389
- }
390
- res.toString = pathToString;
391
- pth.abs = pathClone(res);
392
- return res;
393
- }
394
- function isLine(bez) {
395
- return (bez[0] === bez[2] &&
396
- bez[1] === bez[3] &&
397
- bez[4] === bez[6] &&
398
- bez[5] === bez[7]);
399
- }
400
- function lineToCurve(x1, y1, x2, y2) {
401
- return [
402
- x1, y1, x2,
403
- y2, x2, y2
404
- ];
405
- }
406
- function qubicToCurve(x1, y1, ax, ay, x2, y2) {
407
- const _13 = 1 / 3, _23 = 2 / 3;
408
- return [
409
- _13 * x1 + _23 * ax,
410
- _13 * y1 + _23 * ay,
411
- _13 * x2 + _23 * ax,
412
- _13 * y2 + _23 * ay,
413
- x2,
414
- y2
415
- ];
416
- }
417
- function arcToCurve(x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
418
- // for more information of where this math came from visit:
419
- // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
420
- const _120 = PI * 120 / 180, rad = PI / 180 * (+angle || 0);
421
- let res = [], xy;
422
- const rotate = cacher(function (x, y, rad) {
423
- const X = x * math.cos(rad) - y * math.sin(rad), Y = x * math.sin(rad) + y * math.cos(rad);
424
- return { x: X, y: Y };
425
- });
426
- let f1, f2, cx, cy;
427
- if (!recursive) {
428
- xy = rotate(x1, y1, -rad);
429
- x1 = xy.x;
430
- y1 = xy.y;
431
- xy = rotate(x2, y2, -rad);
432
- x2 = xy.x;
433
- y2 = xy.y;
434
- const x = (x1 - x2) / 2, y = (y1 - y2) / 2;
435
- let h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
436
- if (h > 1) {
437
- h = math.sqrt(h);
438
- rx = h * rx;
439
- ry = h * ry;
440
- }
441
- const rx2 = rx * rx, ry2 = ry * ry, k = (large_arc_flag == sweep_flag ? -1 : 1) *
442
- math.sqrt(abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x)));
443
- cx = k * rx * y / ry + (x1 + x2) / 2;
444
- cy = k * -ry * x / rx + (y1 + y2) / 2;
445
- f1 = math.asin(Number(((y1 - cy) / ry).toFixed(9)));
446
- f2 = math.asin(Number(((y2 - cy) / ry).toFixed(9)));
447
- f1 = x1 < cx ? PI - f1 : f1;
448
- f2 = x2 < cx ? PI - f2 : f2;
449
- f1 < 0 && (f1 = PI * 2 + f1);
450
- f2 < 0 && (f2 = PI * 2 + f2);
451
- if (sweep_flag && f1 > f2) {
452
- f1 = f1 - PI * 2;
453
- }
454
- if (!sweep_flag && f2 > f1) {
455
- f2 = f2 - PI * 2;
456
- }
457
- }
458
- else {
459
- f1 = recursive[0];
460
- f2 = recursive[1];
461
- cx = recursive[2];
462
- cy = recursive[3];
463
- }
464
- let df = f2 - f1;
465
- if (abs(df) > _120) {
466
- const f2old = f2, x2old = x2, y2old = y2;
467
- f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
468
- x2 = cx + rx * math.cos(f2);
469
- y2 = cy + ry * math.sin(f2);
470
- res = arcToCurve(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
471
- }
472
- df = f2 - f1;
473
- const c1 = math.cos(f1), s1 = math.sin(f1), c2 = math.cos(f2), s2 = math.sin(f2), t = math.tan(df / 4), hx = 4 / 3 * rx * t, hy = 4 / 3 * ry * t, m1 = [x1, y1], m2 = [x1 + hx * s1, y1 - hy * c1], m3 = [x2 + hx * s2, y2 - hy * c2], m4 = [x2, y2];
474
- m2[0] = 2 * m1[0] - m2[0];
475
- m2[1] = 2 * m1[1] - m2[1];
476
- if (recursive) {
477
- return [m2, m3, m4].concat(res);
478
- }
479
- else {
480
- res = [m2, m3, m4].concat(res).join().split(',');
481
- const newres = [];
482
- for (let i = 0, ii = res.length; i < ii; i++) {
483
- newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
484
- }
485
- return newres;
486
- }
487
- }
488
- // Returns bounding box of cubic bezier curve.
489
- // Source: http://blog.hackers-cafe.net/2009/06/how-to-calculate-bezier-curves-bounding.html
490
- // Original version: NISHIO Hirokazu
491
- // Modifications: https://github.com/timo22345
492
- function curveBBox(x0, y0, x1, y1, x2, y2, x3, y3) {
493
- const tvalues = [], bounds = [[], []];
494
- let a, b, c, t, t1, t2, b2ac, sqrtb2ac;
495
- for (let i = 0; i < 2; ++i) {
496
- if (i == 0) {
497
- b = 6 * x0 - 12 * x1 + 6 * x2;
498
- a = -3 * x0 + 9 * x1 - 9 * x2 + 3 * x3;
499
- c = 3 * x1 - 3 * x0;
500
- }
501
- else {
502
- b = 6 * y0 - 12 * y1 + 6 * y2;
503
- a = -3 * y0 + 9 * y1 - 9 * y2 + 3 * y3;
504
- c = 3 * y1 - 3 * y0;
505
- }
506
- if (abs(a) < 1e-12) {
507
- if (abs(b) < 1e-12) {
508
- continue;
509
- }
510
- t = -c / b;
511
- if (0 < t && t < 1) {
512
- tvalues.push(t);
513
- }
514
- continue;
515
- }
516
- b2ac = b * b - 4 * c * a;
517
- sqrtb2ac = math.sqrt(b2ac);
518
- if (b2ac < 0) {
519
- continue;
520
- }
521
- t1 = (-b + sqrtb2ac) / (2 * a);
522
- if (0 < t1 && t1 < 1) {
523
- tvalues.push(t1);
524
- }
525
- t2 = (-b - sqrtb2ac) / (2 * a);
526
- if (0 < t2 && t2 < 1) {
527
- tvalues.push(t2);
528
- }
529
- }
530
- let j = tvalues.length;
531
- const jlen = j;
532
- let mt;
533
- while (j--) {
534
- t = tvalues[j];
535
- mt = 1 - t;
536
- bounds[0][j] = (mt * mt * mt * x0) + (3 * mt * mt * t * x1) + (3 * mt * t * t * x2) + (t * t * t * x3);
537
- bounds[1][j] = (mt * mt * mt * y0) + (3 * mt * mt * t * y1) + (3 * mt * t * t * y2) + (t * t * t * y3);
538
- }
539
- bounds[0][jlen] = x0;
540
- bounds[1][jlen] = y0;
541
- bounds[0][jlen + 1] = x3;
542
- bounds[1][jlen + 1] = y3;
543
- bounds[0].length = bounds[1].length = jlen + 2;
544
- return {
545
- x0: mmin.apply(0, bounds[0]),
546
- y0: mmin.apply(0, bounds[1]),
547
- x1: mmax.apply(0, bounds[0]),
548
- y1: mmax.apply(0, bounds[1])
549
- };
550
- }
551
- function pathToCurve(path) {
552
- const pth = paths(path);
553
- // return cached curve, if existing
554
- if (pth.curve) {
555
- return pathClone(pth.curve);
556
- }
557
- const curvedPath = pathToAbsolute(path), attrs = { x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null }, processPath = function (path, d, pathCommand) {
558
- let nx, ny;
559
- if (!path) {
560
- return ['C', d.x, d.y, d.x, d.y, d.x, d.y];
561
- }
562
- !(path[0] in { T: 1, Q: 1 }) && (d.qx = d.qy = null);
563
- switch (path[0]) {
564
- case 'M':
565
- d.X = path[1];
566
- d.Y = path[2];
567
- break;
568
- case 'A':
569
- path = ['C'].concat(arcToCurve.apply(0, [d.x, d.y].concat(path.slice(1))));
570
- break;
571
- case 'S':
572
- if (pathCommand == 'C' || pathCommand == 'S') {
573
- // In 'S' case we have to take into account, if the previous command is C/S.
574
- nx = d.x * 2 - d.bx;
575
- // And reflect the previous
576
- ny = d.y * 2 - d.by;
577
- // command's control point relative to the current point.
578
- }
579
- else {
580
- // or some else or nothing
581
- nx = d.x;
582
- ny = d.y;
583
- }
584
- path = ['C', nx, ny].concat(path.slice(1));
585
- break;
586
- case 'T':
587
- if (pathCommand == 'Q' || pathCommand == 'T') {
588
- // In 'T' case we have to take into account, if the previous command is Q/T.
589
- d.qx = d.x * 2 - d.qx;
590
- // And make a reflection similar
591
- d.qy = d.y * 2 - d.qy;
592
- // to case 'S'.
593
- }
594
- else {
595
- // or something else or nothing
596
- d.qx = d.x;
597
- d.qy = d.y;
598
- }
599
- path = ['C'].concat(qubicToCurve(d.x, d.y, d.qx, d.qy, path[1], path[2]));
600
- break;
601
- case 'Q':
602
- d.qx = path[1];
603
- d.qy = path[2];
604
- path = ['C'].concat(qubicToCurve(d.x, d.y, path[1], path[2], path[3], path[4]));
605
- break;
606
- case 'L':
607
- path = ['C'].concat(lineToCurve(d.x, d.y, path[1], path[2]));
608
- break;
609
- case 'H':
610
- path = ['C'].concat(lineToCurve(d.x, d.y, path[1], d.y));
611
- break;
612
- case 'V':
613
- path = ['C'].concat(lineToCurve(d.x, d.y, d.x, path[1]));
614
- break;
615
- case 'Z':
616
- path = ['C'].concat(lineToCurve(d.x, d.y, d.X, d.Y));
617
- break;
618
- }
619
- return path;
620
- }, fixArc = function (pp, i) {
621
- if (pp[i].length > 7) {
622
- pp[i].shift();
623
- const pi = pp[i];
624
- while (pi.length) {
625
- pathCommands[i] = 'A'; // if created multiple C:s, their original seg is saved
626
- pp.splice(i++, 0, ['C'].concat(pi.splice(0, 6)));
627
- }
628
- pp.splice(i, 1);
629
- //ii = curvedPath.length;
630
- }
631
- }, pathCommands = []; // path commands of original path p
632
- let pfirst = '', // temporary holder for original path command
633
- pathCommand = ''; // holder for previous path command of original path
634
- for (let i = 0, ii = curvedPath.length; i < ii; i++) {
635
- curvedPath[i] && (pfirst = curvedPath[i][0]); // save current path command
636
- if (pfirst != 'C') // C is not saved yet, because it may be result of conversion
637
- {
638
- pathCommands[i] = pfirst; // Save current path command
639
- i && (pathCommand = pathCommands[i - 1]); // Get previous path command pathCommand
640
- }
641
- curvedPath[i] = processPath(curvedPath[i], attrs, pathCommand); // Previous path command is inputted to processPath
642
- if (pathCommands[i] != 'A' && pfirst == 'C')
643
- pathCommands[i] = 'C'; // A is the only command
644
- // which may produce multiple C:s
645
- // so we have to make sure that C is also C in original path
646
- fixArc(curvedPath, i); // fixArc adds also the right amount of A:s to pathCommands
647
- const seg = curvedPath[i], seglen = seg.length;
648
- attrs.x = seg[seglen - 2];
649
- attrs.y = seg[seglen - 1];
650
- attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
651
- attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
652
- }
653
- // cache curve
654
- pth.curve = pathClone(curvedPath);
655
- return curvedPath;
656
- }
657
- //# sourceMappingURL=path-intersection.js.map