billboard.js 4.0.2 → 4.1.0-next.1
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.
- package/README.md +82 -7
- package/dist/billboard.css +1 -1
- package/dist/billboard.js +1938 -557
- package/dist/billboard.min.css +1 -1
- package/dist/billboard.min.js +19 -25
- package/dist/billboard.pkgd.js +1934 -742
- package/dist/billboard.pkgd.min.js +24 -30
- package/dist/billboard.react.js +205 -0
- package/dist/billboard.react.min.js +9 -0
- package/dist/billboard.worker.js +10 -0
- package/dist/plugin/billboardjs-plugin-bubblecompare.js +2 -2
- package/dist/plugin/billboardjs-plugin-bubblecompare.min.js +2 -2
- package/dist/plugin/billboardjs-plugin-sparkline.js +3 -2
- package/dist/plugin/billboardjs-plugin-sparkline.min.js +3 -3
- package/dist/plugin/billboardjs-plugin-stanford.js +3 -2
- package/dist/plugin/billboardjs-plugin-stanford.min.js +2 -2
- package/dist/plugin/billboardjs-plugin-tableview.js +57 -27
- package/dist/plugin/billboardjs-plugin-tableview.min.js +5 -5
- package/dist/plugin/billboardjs-plugin-textoverlap.js +72 -54
- package/dist/plugin/billboardjs-plugin-textoverlap.min.js +2 -2
- package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.js +2 -2
- package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.min.js +2 -2
- package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.js +3 -2
- package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.min.js +3 -3
- package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.js +3 -2
- package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.min.js +2 -2
- package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.js +57 -27
- package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.min.js +11 -11
- package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.js +65 -3556
- package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.min.js +8 -8
- package/dist/theme/dark.css +1 -1
- package/dist/theme/dark.min.css +1 -1
- package/dist/theme/datalab.css +1 -1
- package/dist/theme/datalab.min.css +1 -1
- package/dist/theme/graph.css +1 -1
- package/dist/theme/graph.min.css +1 -1
- package/dist/theme/insight.css +1 -1
- package/dist/theme/insight.min.css +1 -1
- package/dist/theme/modern.css +1 -1
- package/dist/theme/modern.min.css +1 -1
- package/dist-esm/Chart/Chart.js +134 -152
- package/dist-esm/Chart/api/axis.js +217 -237
- package/dist-esm/Chart/api/category.js +54 -54
- package/dist-esm/Chart/api/chart.js +172 -215
- package/dist-esm/Chart/api/color.js +21 -21
- package/dist-esm/Chart/api/data.js +172 -173
- package/dist-esm/Chart/api/export.js +242 -262
- package/dist-esm/Chart/api/flow.js +198 -254
- package/dist-esm/Chart/api/focus.js +136 -147
- package/dist-esm/Chart/api/grid.js +191 -190
- package/dist-esm/Chart/api/group.js +31 -34
- package/dist-esm/Chart/api/legend.js +54 -63
- package/dist-esm/Chart/api/load.js +237 -257
- package/dist-esm/Chart/api/regions.js +148 -168
- package/dist-esm/Chart/api/selection.js +181 -139
- package/dist-esm/Chart/api/show.js +133 -147
- package/dist-esm/Chart/api/stubs.js +43 -42
- package/dist-esm/Chart/api/subchart.js +155 -172
- package/dist-esm/Chart/api/tooltip.js +132 -177
- package/dist-esm/Chart/api/x.js +67 -71
- package/dist-esm/Chart/api/zoom.js +183 -220
- package/dist-esm/ChartInternal/Axis/Axis.js +1059 -1266
- package/dist-esm/ChartInternal/Axis/AxisRenderer.js +408 -540
- package/dist-esm/ChartInternal/Axis/AxisRendererHelper.js +113 -149
- package/dist-esm/ChartInternal/ChartInternal.js +573 -749
- package/dist-esm/ChartInternal/data/convert.helper.js +133 -159
- package/dist-esm/ChartInternal/data/convert.js +200 -229
- package/dist-esm/ChartInternal/data/data.js +970 -1147
- package/dist-esm/ChartInternal/data/load.js +145 -197
- package/dist-esm/ChartInternal/interactions/drag.js +86 -122
- package/dist-esm/ChartInternal/interactions/eventrect.js +455 -621
- package/dist-esm/ChartInternal/interactions/flow.js +151 -215
- package/dist-esm/ChartInternal/interactions/interaction.js +214 -252
- package/dist-esm/ChartInternal/interactions/subchart.js +425 -300
- package/dist-esm/ChartInternal/interactions/zoom.js +321 -440
- package/dist-esm/ChartInternal/internals/category.js +18 -18
- package/dist-esm/ChartInternal/internals/class.js +89 -99
- package/dist-esm/ChartInternal/internals/clip.js +88 -119
- package/dist-esm/ChartInternal/internals/color.js +233 -288
- package/dist-esm/ChartInternal/internals/domain.js +452 -588
- package/dist-esm/ChartInternal/internals/format.js +64 -77
- package/dist-esm/ChartInternal/internals/grid.js +333 -457
- package/dist-esm/ChartInternal/internals/legend.js +675 -927
- package/dist-esm/ChartInternal/internals/redraw.js +200 -289
- package/dist-esm/ChartInternal/internals/region.js +106 -159
- package/dist-esm/ChartInternal/internals/scale.js +186 -215
- package/dist-esm/ChartInternal/internals/selection.js +132 -159
- package/dist-esm/ChartInternal/internals/size.axis.js +92 -139
- package/dist-esm/ChartInternal/internals/size.js +286 -394
- package/dist-esm/ChartInternal/internals/style.js +39 -41
- package/dist-esm/ChartInternal/internals/subchart.util.js +49 -0
- package/dist-esm/ChartInternal/internals/text.arc.js +155 -219
- package/dist-esm/ChartInternal/internals/text.js +368 -528
- package/dist-esm/ChartInternal/internals/text.util.js +179 -239
- package/dist-esm/ChartInternal/internals/title.js +96 -121
- package/dist-esm/ChartInternal/internals/tooltip.js +450 -586
- package/dist-esm/ChartInternal/internals/transform.js +79 -97
- package/dist-esm/ChartInternal/internals/type.js +286 -245
- package/dist-esm/ChartInternal/shape/arc.js +825 -1112
- package/dist-esm/ChartInternal/shape/area.js +73 -94
- package/dist-esm/ChartInternal/shape/bar.js +227 -272
- package/dist-esm/ChartInternal/shape/bubble.js +9 -8
- package/dist-esm/ChartInternal/shape/candlestick.js +110 -138
- package/dist-esm/ChartInternal/shape/core/barRadius.js +197 -133
- package/dist-esm/ChartInternal/shape/core/bubble.js +64 -73
- package/dist-esm/ChartInternal/shape/core/candlestick.js +93 -86
- package/dist-esm/ChartInternal/shape/core/dataRegion.js +68 -75
- package/dist-esm/ChartInternal/shape/core/geometry.js +112 -112
- package/dist-esm/ChartInternal/shape/core/path.js +142 -115
- package/dist-esm/ChartInternal/shape/core/point.js +105 -125
- package/dist-esm/ChartInternal/shape/core/treemap.js +83 -88
- package/dist-esm/ChartInternal/shape/funnel.js +310 -338
- package/dist-esm/ChartInternal/shape/gauge.js +71 -103
- package/dist-esm/ChartInternal/shape/line.js +208 -278
- package/dist-esm/ChartInternal/shape/point.js +310 -413
- package/dist-esm/ChartInternal/shape/polar.js +84 -109
- package/dist-esm/ChartInternal/shape/radar.js +228 -318
- package/dist-esm/ChartInternal/shape/shape.js +782 -828
- package/dist-esm/ChartInternal/shape/treemap.js +152 -190
- package/dist-esm/canvas/CanvasAxisRenderer.js +1180 -1328
- package/dist-esm/canvas/CanvasEngine.js +148 -166
- package/dist-esm/canvas/CanvasPainter.js +395 -411
- package/dist-esm/canvas/CanvasRenderer.js +1584 -1834
- package/dist-esm/canvas/CanvasTheme.js +899 -802
- package/dist-esm/canvas/HitDetector.js +394 -463
- package/dist-esm/canvas/axisTicks.js +672 -786
- package/dist-esm/canvas/classes.js +19 -17
- package/dist-esm/canvas/color.js +40 -59
- package/dist-esm/canvas/geometry.js +38 -38
- package/dist-esm/canvas/labels.js +318 -371
- package/dist-esm/canvas/pointPattern.js +578 -580
- package/dist-esm/canvas/util.js +242 -262
- package/dist-esm/config/Options/Options.js +29 -32
- package/dist-esm/config/Options/axis/axis.js +101 -101
- package/dist-esm/config/Options/axis/x.js +759 -755
- package/dist-esm/config/Options/axis/y.js +525 -522
- package/dist-esm/config/Options/axis/y2.js +501 -485
- package/dist-esm/config/Options/common/boost.js +93 -32
- package/dist-esm/config/Options/common/color.js +88 -87
- package/dist-esm/config/Options/common/grid.js +74 -73
- package/dist-esm/config/Options/common/legend.js +180 -179
- package/dist-esm/config/Options/common/main.js +474 -473
- package/dist-esm/config/Options/common/point.js +160 -159
- package/dist-esm/config/Options/common/title.js +47 -46
- package/dist-esm/config/Options/common/tooltip.js +253 -252
- package/dist-esm/config/Options/data/axis.js +173 -172
- package/dist-esm/config/Options/data/data.js +958 -957
- package/dist-esm/config/Options/data/selection.js +131 -130
- package/dist-esm/config/Options/interaction/interaction.js +53 -52
- package/dist-esm/config/Options/interaction/subchart.js +164 -73
- package/dist-esm/config/Options/interaction/zoom.js +86 -85
- package/dist-esm/config/Options/shape/arc.js +150 -149
- package/dist-esm/config/Options/shape/area.js +69 -68
- package/dist-esm/config/Options/shape/bar.js +147 -146
- package/dist-esm/config/Options/shape/bubble.js +35 -34
- package/dist-esm/config/Options/shape/candlestick.js +61 -60
- package/dist-esm/config/Options/shape/donut.js +181 -180
- package/dist-esm/config/Options/shape/funnel.js +49 -48
- package/dist-esm/config/Options/shape/gauge.js +237 -236
- package/dist-esm/config/Options/shape/line.js +58 -57
- package/dist-esm/config/Options/shape/pie.js +185 -184
- package/dist-esm/config/Options/shape/polar.js +167 -166
- package/dist-esm/config/Options/shape/radar.js +76 -75
- package/dist-esm/config/Options/shape/scatter.js +36 -36
- package/dist-esm/config/Options/shape/spline.js +46 -46
- package/dist-esm/config/Options/shape/treemap.js +80 -79
- package/dist-esm/config/Store/Element.js +64 -62
- package/dist-esm/config/Store/State.js +189 -200
- package/dist-esm/config/Store/Store.js +25 -26
- package/dist-esm/config/classes.js +157 -155
- package/dist-esm/config/config.js +26 -35
- package/dist-esm/config/const.js +120 -128
- package/dist-esm/config/resolver/axis.core.js +25 -25
- package/dist-esm/config/resolver/axis.js +26 -27
- package/dist-esm/config/resolver/canvas.js +2071 -2472
- package/dist-esm/config/resolver/canvasShape.js +201 -177
- package/dist-esm/config/resolver/category.js +26 -27
- package/dist-esm/config/resolver/export.js +24 -26
- package/dist-esm/config/resolver/flow.js +29 -31
- package/dist-esm/config/resolver/grid.js +34 -35
- package/dist-esm/config/resolver/interaction/selection.js +17 -17
- package/dist-esm/config/resolver/interaction/subchart.js +17 -17
- package/dist-esm/config/resolver/interaction/zoom.js +17 -17
- package/dist-esm/config/resolver/regions.js +30 -31
- package/dist-esm/config/resolver/shape/arc.helpers.js +19 -19
- package/dist-esm/config/resolver/shape/area.js +17 -17
- package/dist-esm/config/resolver/shape/axis.helpers.js +28 -25
- package/dist-esm/config/resolver/shape/bar.js +13 -13
- package/dist-esm/config/resolver/shape/bubble.js +18 -14
- package/dist-esm/config/resolver/shape/candlestick.js +13 -13
- package/dist-esm/config/resolver/shape/donut.js +11 -11
- package/dist-esm/config/resolver/shape/funnel.js +11 -11
- package/dist-esm/config/resolver/shape/gauge.js +12 -12
- package/dist-esm/config/resolver/shape/line.js +10 -10
- package/dist-esm/config/resolver/shape/pie.js +11 -11
- package/dist-esm/config/resolver/shape/polar.js +12 -12
- package/dist-esm/config/resolver/shape/radar.js +22 -14
- package/dist-esm/config/resolver/shape/scatter.js +13 -13
- package/dist-esm/config/resolver/shape/treemap.js +11 -11
- package/dist-esm/core.js +208 -211
- package/dist-esm/index.canvas.js +14 -13
- package/dist-esm/index.esm.js +25 -24
- package/dist-esm/module/Cache.js +117 -129
- package/dist-esm/module/browser.js +30 -35
- package/dist-esm/module/dsv.js +107 -127
- package/dist-esm/module/error.js +45 -52
- package/dist-esm/module/generator.js +75 -83
- package/dist-esm/module/sanitize.js +302 -399
- package/dist-esm/module/util/brush.js +27 -38
- package/dist-esm/module/util/dom.js +288 -348
- package/dist-esm/module/util/object.js +277 -341
- package/dist-esm/module/util/type-checks.js +21 -25
- package/dist-esm/module/worker.js +312 -130
- package/dist-esm/plugin/billboardjs-plugin-bubblecompare.js +185 -198
- package/dist-esm/plugin/billboardjs-plugin-sparkline.js +442 -450
- package/dist-esm/plugin/billboardjs-plugin-stanford.js +813 -950
- package/dist-esm/plugin/billboardjs-plugin-tableview.js +708 -788
- package/dist-esm/plugin/billboardjs-plugin-textoverlap.js +344 -301
- package/dist-esm/react/index.js +157 -0
- package/package.json +28 -8
- package/src/Chart/api/focus.ts +8 -12
- package/src/Chart/api/selection.ts +130 -5
- package/src/ChartInternal/Axis/Axis.ts +164 -65
- package/src/ChartInternal/Axis/AxisRenderer.ts +41 -8
- package/src/ChartInternal/Axis/AxisRendererHelper.ts +1 -1
- package/src/ChartInternal/ChartInternal.ts +1 -1
- package/src/ChartInternal/data/convert.ts +54 -4
- package/src/ChartInternal/data/data.ts +19 -6
- package/src/ChartInternal/interactions/eventrect.ts +6 -0
- package/src/ChartInternal/interactions/subchart.ts +437 -66
- package/src/ChartInternal/internals/color.ts +7 -6
- package/src/ChartInternal/internals/domain.ts +15 -3
- package/src/ChartInternal/internals/grid.ts +21 -1
- package/src/ChartInternal/internals/scale.ts +9 -2
- package/src/ChartInternal/internals/selection.ts +1 -1
- package/src/ChartInternal/internals/subchart.util.ts +55 -0
- package/src/ChartInternal/internals/text.arc.ts +3 -1
- package/src/ChartInternal/internals/text.ts +19 -6
- package/src/ChartInternal/internals/transform.ts +8 -2
- package/src/ChartInternal/internals/type.ts +131 -33
- package/src/ChartInternal/shape/IShape.ts +6 -3
- package/src/ChartInternal/shape/bar.ts +87 -37
- package/src/ChartInternal/shape/core/barRadius.ts +129 -0
- package/src/ChartInternal/shape/core/candlestick.ts +1 -1
- package/src/ChartInternal/shape/core/path.ts +76 -9
- package/src/ChartInternal/shape/funnel.ts +2 -3
- package/src/ChartInternal/shape/line.ts +1 -1
- package/src/ChartInternal/shape/point.ts +27 -13
- package/src/ChartInternal/shape/shape.ts +172 -18
- package/src/Plugin/stanford/index.ts +0 -4
- package/src/Plugin/tableview/index.ts +85 -3
- package/src/Plugin/textoverlap/index.ts +9 -15
- package/src/canvas/CanvasAxisRenderer.ts +184 -36
- package/src/canvas/CanvasRenderer.ts +247 -160
- package/src/canvas/CanvasTheme.ts +212 -0
- package/src/canvas/axisTicks.ts +122 -40
- package/src/config/Options/axis/y2.ts +13 -0
- package/src/config/Options/common/boost.ts +64 -4
- package/src/config/Options/common/color.ts +2 -2
- package/src/config/Options/interaction/subchart.ts +98 -1
- package/src/config/Options/shape/gauge.ts +1 -1
- package/src/config/Options/shape/treemap.ts +2 -2
- package/src/config/Store/Element.ts +3 -1
- package/src/config/Store/State.ts +1 -0
- package/src/config/classes.ts +1 -0
- package/src/config/resolver/canvas.ts +92 -10
- package/src/module/voronoi.ts +119 -0
- package/src/module/worker.entry.ts +61 -0
- package/src/module/worker.ts +346 -63
- package/src/react/index.ts +193 -0
- package/types/options.d.ts +206 -3
- package/types/react/index.d.ts +27 -0
|
@@ -5,654 +5,652 @@
|
|
|
5
5
|
* billboard.js, JavaScript chart library
|
|
6
6
|
* https://naver.github.io/billboard.js/
|
|
7
7
|
*
|
|
8
|
-
* @version 4.0.
|
|
8
|
+
* @version 4.1.0-next.1
|
|
9
9
|
*/
|
|
10
|
-
import { window as win } from
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
|
|
10
|
+
import { window as win } from "../module/browser.js";
|
|
11
|
+
import { sanitize } from "../module/sanitize.js";
|
|
12
|
+
import { withOpacity } from "./color.js";
|
|
13
|
+
//#region src/canvas/pointPattern.ts
|
|
14
14
|
/**
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
* Copyright (c) 2017 ~ present NAVER Corp.
|
|
16
|
+
* billboard.js project is licensed under the MIT license
|
|
17
|
+
*/
|
|
18
|
+
const DEFAULT_POINT_VIEWBOX = {
|
|
19
|
+
x: 0,
|
|
20
|
+
y: 0,
|
|
21
|
+
w: 8,
|
|
22
|
+
h: 8
|
|
23
|
+
};
|
|
24
|
+
const customPointPatternCache = /* @__PURE__ */ new Map();
|
|
25
|
+
const IDENTITY_POINT_MATRIX = [
|
|
26
|
+
1,
|
|
27
|
+
0,
|
|
28
|
+
0,
|
|
29
|
+
1,
|
|
30
|
+
0,
|
|
31
|
+
0
|
|
32
|
+
];
|
|
23
33
|
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
34
|
+
* Check if point type can be drawn by the built-in point renderer.
|
|
35
|
+
* @param {string} type Point type
|
|
36
|
+
* @returns {boolean} Whether type is built-in
|
|
37
|
+
* @private
|
|
38
|
+
*/
|
|
29
39
|
function isBuiltinPointType(type) {
|
|
30
|
-
|
|
40
|
+
return type === "circle" || type === "rectangle";
|
|
31
41
|
}
|
|
32
42
|
/**
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
43
|
+
* Get numeric SVG attribute.
|
|
44
|
+
* @param {Element} node SVG element
|
|
45
|
+
* @param {string} name Attribute name
|
|
46
|
+
* @param {number} fallback Fallback value
|
|
47
|
+
* @returns {number} Numeric attribute value
|
|
48
|
+
* @private
|
|
49
|
+
*/
|
|
40
50
|
function getSvgNumber(node, name, fallback = 0) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
51
|
+
const value = node.getAttribute(name);
|
|
52
|
+
const parsed = value === null ? NaN : parseFloat(value);
|
|
53
|
+
return Number.isFinite(parsed) ? parsed : fallback;
|
|
44
54
|
}
|
|
45
55
|
/**
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
56
|
+
* Parse an SVG points attribute.
|
|
57
|
+
* @param {string} value Points attribute
|
|
58
|
+
* @returns {Array} Coordinate pairs
|
|
59
|
+
* @private
|
|
60
|
+
*/
|
|
51
61
|
function parseSvgPoints(value) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
for (let i = 0; i < nums.length - 1; i += 2) {
|
|
57
|
-
points.push([nums[i], nums[i + 1]]);
|
|
58
|
-
}
|
|
59
|
-
return points;
|
|
62
|
+
const nums = (value.match(/[-+]?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi) || []).map(Number).filter(Number.isFinite);
|
|
63
|
+
const points = [];
|
|
64
|
+
for (let i = 0; i < nums.length - 1; i += 2) points.push([nums[i], nums[i + 1]]);
|
|
65
|
+
return points;
|
|
60
66
|
}
|
|
61
67
|
/**
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
68
|
+
* Parse an SVG viewBox attribute.
|
|
69
|
+
* @param {string|null} value viewBox value
|
|
70
|
+
* @returns {object|null} Parsed box
|
|
71
|
+
* @private
|
|
72
|
+
*/
|
|
67
73
|
function parseViewBox(value) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
} :
|
|
76
|
-
null;
|
|
74
|
+
const values = value?.trim().split(/[\s,]+/).map(Number).filter(Number.isFinite) || [];
|
|
75
|
+
return values.length === 4 ? {
|
|
76
|
+
x: values[0],
|
|
77
|
+
y: values[1],
|
|
78
|
+
w: values[2],
|
|
79
|
+
h: values[3]
|
|
80
|
+
} : null;
|
|
77
81
|
}
|
|
78
82
|
/**
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
* Get bounds for coordinate pairs.
|
|
84
|
+
* @param {Array} points Coordinate pairs
|
|
85
|
+
* @returns {object} Bounds
|
|
86
|
+
* @private
|
|
87
|
+
*/
|
|
84
88
|
function getPointsBox(points) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
89
|
+
const xs = points.map(([x]) => x);
|
|
90
|
+
const ys = points.map(([, y]) => y);
|
|
91
|
+
const x = Math.min(...xs);
|
|
92
|
+
const y = Math.min(...ys);
|
|
93
|
+
return {
|
|
94
|
+
x,
|
|
95
|
+
y,
|
|
96
|
+
w: Math.max(...xs) - x,
|
|
97
|
+
h: Math.max(...ys) - y
|
|
98
|
+
};
|
|
95
99
|
}
|
|
96
100
|
/**
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
101
|
+
* Merge multiple bounds.
|
|
102
|
+
* @param {Array} boxes Bounds
|
|
103
|
+
* @returns {object} Merged bounds
|
|
104
|
+
* @private
|
|
105
|
+
*/
|
|
102
106
|
function mergePointBoxes(boxes) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
x,
|
|
116
|
-
y,
|
|
117
|
-
w: right - x,
|
|
118
|
-
h: bottom - y
|
|
119
|
-
};
|
|
107
|
+
const validBoxes = boxes.filter((box) => Number.isFinite(box.x) && Number.isFinite(box.y) && Number.isFinite(box.w) && Number.isFinite(box.h));
|
|
108
|
+
if (!validBoxes.length) return { ...DEFAULT_POINT_VIEWBOX };
|
|
109
|
+
const x = Math.min(...validBoxes.map((box) => box.x));
|
|
110
|
+
const y = Math.min(...validBoxes.map((box) => box.y));
|
|
111
|
+
const right = Math.max(...validBoxes.map((box) => box.x + box.w));
|
|
112
|
+
const bottom = Math.max(...validBoxes.map((box) => box.y + box.h));
|
|
113
|
+
return {
|
|
114
|
+
x,
|
|
115
|
+
y,
|
|
116
|
+
w: right - x,
|
|
117
|
+
h: bottom - y
|
|
118
|
+
};
|
|
120
119
|
}
|
|
121
120
|
/**
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
121
|
+
* Multiply SVG affine matrices.
|
|
122
|
+
* @param {Array} a Base matrix
|
|
123
|
+
* @param {Array} b Matrix to append
|
|
124
|
+
* @returns {Array} Combined matrix
|
|
125
|
+
* @private
|
|
126
|
+
*/
|
|
128
127
|
function multiplyPointMatrix(a, b) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
128
|
+
return [
|
|
129
|
+
a[0] * b[0] + a[2] * b[1],
|
|
130
|
+
a[1] * b[0] + a[3] * b[1],
|
|
131
|
+
a[0] * b[2] + a[2] * b[3],
|
|
132
|
+
a[1] * b[2] + a[3] * b[3],
|
|
133
|
+
a[0] * b[4] + a[2] * b[5] + a[4],
|
|
134
|
+
a[1] * b[4] + a[3] * b[5] + a[5]
|
|
135
|
+
];
|
|
137
136
|
}
|
|
138
137
|
/**
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
138
|
+
* Parse SVG transform attribute into a canvas matrix.
|
|
139
|
+
* @param {string|null} value Transform attribute
|
|
140
|
+
* @returns {Array} Matrix
|
|
141
|
+
* @private
|
|
142
|
+
*/
|
|
144
143
|
function parsePointTransform(value) {
|
|
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
|
-
|
|
144
|
+
let matrix = [...IDENTITY_POINT_MATRIX];
|
|
145
|
+
(value?.match(/[a-z]+\([^)]*\)/gi) || []).forEach((token) => {
|
|
146
|
+
const [, rawName, body] = token.match(/^([a-z]+)\(([^)]*)\)$/i) || [];
|
|
147
|
+
const name = rawName?.toLowerCase();
|
|
148
|
+
const values = (body?.match(/[-+]?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi) || []).map(Number).filter(Number.isFinite);
|
|
149
|
+
let next = null;
|
|
150
|
+
if (name === "matrix" && values.length >= 6) next = values.slice(0, 6);
|
|
151
|
+
else if (name === "translate" && values.length) next = [
|
|
152
|
+
1,
|
|
153
|
+
0,
|
|
154
|
+
0,
|
|
155
|
+
1,
|
|
156
|
+
values[0],
|
|
157
|
+
values[1] || 0
|
|
158
|
+
];
|
|
159
|
+
else if (name === "scale" && values.length) next = [
|
|
160
|
+
values[0],
|
|
161
|
+
0,
|
|
162
|
+
0,
|
|
163
|
+
values[1] ?? values[0],
|
|
164
|
+
0,
|
|
165
|
+
0
|
|
166
|
+
];
|
|
167
|
+
else if (name === "rotate" && values.length) {
|
|
168
|
+
const angle = values[0] * Math.PI / 180;
|
|
169
|
+
const cos = Math.cos(angle);
|
|
170
|
+
const sin = Math.sin(angle);
|
|
171
|
+
const rotate = [
|
|
172
|
+
cos,
|
|
173
|
+
sin,
|
|
174
|
+
-sin,
|
|
175
|
+
cos,
|
|
176
|
+
0,
|
|
177
|
+
0
|
|
178
|
+
];
|
|
179
|
+
next = values.length >= 3 ? multiplyPointMatrix(multiplyPointMatrix([
|
|
180
|
+
1,
|
|
181
|
+
0,
|
|
182
|
+
0,
|
|
183
|
+
1,
|
|
184
|
+
values[1],
|
|
185
|
+
values[2]
|
|
186
|
+
], rotate), [
|
|
187
|
+
1,
|
|
188
|
+
0,
|
|
189
|
+
0,
|
|
190
|
+
1,
|
|
191
|
+
-values[1],
|
|
192
|
+
-values[2]
|
|
193
|
+
]) : rotate;
|
|
194
|
+
} else if (name === "skewx" && values.length) next = [
|
|
195
|
+
1,
|
|
196
|
+
0,
|
|
197
|
+
Math.tan(values[0] * Math.PI / 180),
|
|
198
|
+
1,
|
|
199
|
+
0,
|
|
200
|
+
0
|
|
201
|
+
];
|
|
202
|
+
else if (name === "skewy" && values.length) next = [
|
|
203
|
+
1,
|
|
204
|
+
Math.tan(values[0] * Math.PI / 180),
|
|
205
|
+
0,
|
|
206
|
+
1,
|
|
207
|
+
0,
|
|
208
|
+
0
|
|
209
|
+
];
|
|
210
|
+
next && (matrix = multiplyPointMatrix(matrix, next));
|
|
211
|
+
});
|
|
212
|
+
return matrix;
|
|
180
213
|
}
|
|
181
214
|
/**
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
215
|
+
* Read style or presentation attribute.
|
|
216
|
+
* @param {Element} node SVG element
|
|
217
|
+
* @param {string} name Property name
|
|
218
|
+
* @returns {string|null} Property value
|
|
219
|
+
* @private
|
|
220
|
+
*/
|
|
188
221
|
function getPointPresentationValue(node, name) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
return (match?.[1] || attr || "").trim() || null;
|
|
222
|
+
const style = node.getAttribute("style");
|
|
223
|
+
const attr = node.getAttribute(name);
|
|
224
|
+
return ((style?.match(new RegExp(`(?:^|;)\\s*${name}\\s*:\\s*([^;]+)`, "i")))?.[1] || attr || "").trim() || null;
|
|
193
225
|
}
|
|
194
226
|
/**
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
227
|
+
* Parse inherited point style from SVG presentation attributes.
|
|
228
|
+
* @param {Element} node SVG element
|
|
229
|
+
* @param {object} inherited Inherited style
|
|
230
|
+
* @returns {object} Resolved style
|
|
231
|
+
* @private
|
|
232
|
+
*/
|
|
201
233
|
function parsePointStyle(node, inherited) {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
234
|
+
const style = { ...inherited };
|
|
235
|
+
const fill = getPointPresentationValue(node, "fill");
|
|
236
|
+
const stroke = getPointPresentationValue(node, "stroke");
|
|
237
|
+
const strokeWidth = getPointPresentationValue(node, "stroke-width");
|
|
238
|
+
const opacity = getPointPresentationValue(node, "opacity");
|
|
239
|
+
const fillOpacity = getPointPresentationValue(node, "fill-opacity");
|
|
240
|
+
const strokeOpacity = getPointPresentationValue(node, "stroke-opacity");
|
|
241
|
+
if (fill) style.fill = fill === "none" ? null : fill;
|
|
242
|
+
if (stroke) style.stroke = stroke === "none" ? null : stroke;
|
|
243
|
+
if (strokeWidth) {
|
|
244
|
+
const lineWidth = parseFloat(strokeWidth);
|
|
245
|
+
Number.isFinite(lineWidth) && (style.lineWidth = lineWidth);
|
|
246
|
+
}
|
|
247
|
+
[
|
|
248
|
+
opacity,
|
|
249
|
+
fillOpacity,
|
|
250
|
+
strokeOpacity
|
|
251
|
+
].forEach((value) => {
|
|
252
|
+
const alpha = value === null ? NaN : parseFloat(value);
|
|
253
|
+
Number.isFinite(alpha) && (style.alpha = (style.alpha ?? 1) * alpha);
|
|
254
|
+
});
|
|
255
|
+
return style;
|
|
224
256
|
}
|
|
225
257
|
/**
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
258
|
+
* Collect local SVG definitions addressable by id.
|
|
259
|
+
* @param {Element} root SVG root element
|
|
260
|
+
* @returns {Map} Local definition map
|
|
261
|
+
* @private
|
|
262
|
+
*/
|
|
231
263
|
function collectPointDefs(root) {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
264
|
+
const defs = /* @__PURE__ */ new Map();
|
|
265
|
+
Array.from(root.querySelectorAll("[id]")).forEach((node) => {
|
|
266
|
+
const id = node.getAttribute("id");
|
|
267
|
+
id && defs.set(id, node);
|
|
268
|
+
});
|
|
269
|
+
return defs;
|
|
238
270
|
}
|
|
239
271
|
/**
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
272
|
+
* Parse SVG length or percentage for custom point paint coordinates.
|
|
273
|
+
* @param {string|null} value SVG length value
|
|
274
|
+
* @param {string|number} fallback Fallback value
|
|
275
|
+
* @returns {string|number} Parsed value
|
|
276
|
+
* @private
|
|
277
|
+
*/
|
|
246
278
|
function parsePointPaintLength(value, fallback) {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
}
|
|
251
|
-
return /%$/.test(text) ? text : parseFloat(text);
|
|
279
|
+
const text = value?.trim();
|
|
280
|
+
if (!text) return fallback;
|
|
281
|
+
return /%$/.test(text) ? text : parseFloat(text);
|
|
252
282
|
}
|
|
253
283
|
/**
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
284
|
+
* Convert SVG length/percentage into custom point coordinate space.
|
|
285
|
+
* @param {string|number|undefined} value Length value
|
|
286
|
+
* @param {number} origin Box origin
|
|
287
|
+
* @param {number} size Box size
|
|
288
|
+
* @param {number} fallback Fallback value
|
|
289
|
+
* @returns {number} Coordinate
|
|
290
|
+
* @private
|
|
291
|
+
*/
|
|
262
292
|
function pointPaintCoord(value, origin, size, fallback) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const parsed = parseFloat(value ?? "");
|
|
271
|
-
return Number.isFinite(parsed) ? parsed : fallback;
|
|
293
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : fallback;
|
|
294
|
+
if (typeof value === "string" && /%$/.test(value)) {
|
|
295
|
+
const percent = parseFloat(value);
|
|
296
|
+
return Number.isFinite(percent) ? origin + size * percent / 100 : fallback;
|
|
297
|
+
}
|
|
298
|
+
const parsed = parseFloat(value ?? "");
|
|
299
|
+
return Number.isFinite(parsed) ? parsed : fallback;
|
|
272
300
|
}
|
|
273
301
|
/**
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
302
|
+
* Parse SVG gradient stop offset.
|
|
303
|
+
* @param {string|null} value Stop offset
|
|
304
|
+
* @returns {number} Offset
|
|
305
|
+
* @private
|
|
306
|
+
*/
|
|
279
307
|
function parsePointPaintOffset(value) {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
308
|
+
const text = value?.trim() || "0";
|
|
309
|
+
const parsed = parseFloat(text);
|
|
310
|
+
const offset = /%$/.test(text) ? parsed / 100 : parsed;
|
|
311
|
+
return Math.max(0, Math.min(1, Number.isFinite(offset) ? offset : 0));
|
|
284
312
|
}
|
|
285
313
|
/**
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
314
|
+
* Parse SVG gradient stops.
|
|
315
|
+
* @param {Element} node Gradient node
|
|
316
|
+
* @returns {Array} Stops
|
|
317
|
+
* @private
|
|
318
|
+
*/
|
|
291
319
|
function parsePointPaintStops(node) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
};
|
|
303
|
-
});
|
|
320
|
+
return Array.from(node.children || []).filter((child) => child.tagName.toLowerCase() === "stop").map((stop) => {
|
|
321
|
+
const color = getPointPresentationValue(stop, "stop-color") || "#000";
|
|
322
|
+
const opacity = getPointPresentationValue(stop, "stop-opacity");
|
|
323
|
+
const parsedOpacity = opacity === null ? NaN : parseFloat(opacity);
|
|
324
|
+
return {
|
|
325
|
+
offset: parsePointPaintOffset(stop.getAttribute("offset")),
|
|
326
|
+
color,
|
|
327
|
+
opacity: Number.isFinite(parsedOpacity) ? parsedOpacity : void 0
|
|
328
|
+
};
|
|
329
|
+
});
|
|
304
330
|
}
|
|
305
331
|
/**
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
332
|
+
* Collect local linear/radial gradients addressable by id.
|
|
333
|
+
* @param {Map} defs Local definition map
|
|
334
|
+
* @returns {Map} Paint map
|
|
335
|
+
* @private
|
|
336
|
+
*/
|
|
311
337
|
function collectPointPaints(defs) {
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
fr: parsePointPaintLength(node.getAttribute("fr"), 0),
|
|
338
|
-
stops
|
|
339
|
-
});
|
|
340
|
-
}
|
|
341
|
-
});
|
|
342
|
-
return paints;
|
|
338
|
+
const paints = /* @__PURE__ */ new Map();
|
|
339
|
+
defs.forEach((node, id) => {
|
|
340
|
+
const tagName = node.tagName.toLowerCase();
|
|
341
|
+
const stops = parsePointPaintStops(node);
|
|
342
|
+
if (!stops.length) return;
|
|
343
|
+
if (tagName === "lineargradient") paints.set(id, {
|
|
344
|
+
type: "linearGradient",
|
|
345
|
+
x1: parsePointPaintLength(node.getAttribute("x1"), "0%"),
|
|
346
|
+
y1: parsePointPaintLength(node.getAttribute("y1"), "0%"),
|
|
347
|
+
x2: parsePointPaintLength(node.getAttribute("x2"), "100%"),
|
|
348
|
+
y2: parsePointPaintLength(node.getAttribute("y2"), "0%"),
|
|
349
|
+
stops
|
|
350
|
+
});
|
|
351
|
+
else if (tagName === "radialgradient") paints.set(id, {
|
|
352
|
+
type: "radialGradient",
|
|
353
|
+
cx: parsePointPaintLength(node.getAttribute("cx"), "50%"),
|
|
354
|
+
cy: parsePointPaintLength(node.getAttribute("cy"), "50%"),
|
|
355
|
+
r: parsePointPaintLength(node.getAttribute("r"), "50%"),
|
|
356
|
+
fx: parsePointPaintLength(node.getAttribute("fx"), "50%"),
|
|
357
|
+
fy: parsePointPaintLength(node.getAttribute("fy"), "50%"),
|
|
358
|
+
fr: parsePointPaintLength(node.getAttribute("fr"), 0),
|
|
359
|
+
stops
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
return paints;
|
|
343
363
|
}
|
|
344
364
|
/**
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
function parseCustomPointNode(node, fallbackBox, defs, matrix = IDENTITY_POINT_MATRIX, inheritedStyle = {}, seen = new Set()) {
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
365
|
+
* Parse custom SVG point element into canvas drawable shapes.
|
|
366
|
+
* @param {Element} node SVG element
|
|
367
|
+
* @param {object} fallbackBox Fallback bounds for path-only shapes
|
|
368
|
+
* @param {Map} defs Local definitions
|
|
369
|
+
* @param {Array} matrix Current transform matrix
|
|
370
|
+
* @param {object} inheritedStyle Current inherited style
|
|
371
|
+
* @param {Set} seen Already resolved definition ids
|
|
372
|
+
* @returns {Array} Parsed shapes
|
|
373
|
+
* @private
|
|
374
|
+
*/
|
|
375
|
+
function parseCustomPointNode(node, fallbackBox, defs, matrix = IDENTITY_POINT_MATRIX, inheritedStyle = {}, seen = /* @__PURE__ */ new Set()) {
|
|
376
|
+
const tagName = node.tagName.toLowerCase();
|
|
377
|
+
const children = Array.from(node.children || []);
|
|
378
|
+
const style = parsePointStyle(node, inheritedStyle);
|
|
379
|
+
const transform = multiplyPointMatrix(matrix, parsePointTransform(node.getAttribute("transform")));
|
|
380
|
+
const shapeBase = {
|
|
381
|
+
matrix: transform,
|
|
382
|
+
style
|
|
383
|
+
};
|
|
384
|
+
if (tagName === "defs") return [];
|
|
385
|
+
if (tagName === "svg" || tagName === "g" || tagName === "symbol") return children.reduce((shapes, child) => shapes.concat(parseCustomPointNode(child, fallbackBox, defs, transform, style, seen)), []);
|
|
386
|
+
if (tagName === "use") {
|
|
387
|
+
const href = node.getAttribute("href") || node.getAttribute("xlink:href") || "";
|
|
388
|
+
const id = href.charAt(0) === "#" ? href.slice(1) : "";
|
|
389
|
+
const target = id && defs.get(id);
|
|
390
|
+
if (!target || seen.has(id)) return [];
|
|
391
|
+
const useTransform = multiplyPointMatrix(transform, [
|
|
392
|
+
1,
|
|
393
|
+
0,
|
|
394
|
+
0,
|
|
395
|
+
1,
|
|
396
|
+
getSvgNumber(node, "x", 0),
|
|
397
|
+
getSvgNumber(node, "y", 0)
|
|
398
|
+
]);
|
|
399
|
+
seen.add(id);
|
|
400
|
+
const shapes = parseCustomPointNode(target, fallbackBox, defs, useTransform, style, seen);
|
|
401
|
+
seen.delete(id);
|
|
402
|
+
return shapes;
|
|
403
|
+
}
|
|
404
|
+
if (tagName === "polygon" || tagName === "polyline") {
|
|
405
|
+
const points = parseSvgPoints(node.getAttribute("points") || "");
|
|
406
|
+
return points.length ? [{
|
|
407
|
+
type: tagName,
|
|
408
|
+
points,
|
|
409
|
+
box: getPointsBox(points),
|
|
410
|
+
...shapeBase
|
|
411
|
+
}] : [];
|
|
412
|
+
}
|
|
413
|
+
if (tagName === "circle") {
|
|
414
|
+
const cx = getSvgNumber(node, "cx", 0);
|
|
415
|
+
const cy = getSvgNumber(node, "cy", 0);
|
|
416
|
+
const r = getSvgNumber(node, "r", 0);
|
|
417
|
+
return r > 0 ? [{
|
|
418
|
+
type: "circle",
|
|
419
|
+
cx,
|
|
420
|
+
cy,
|
|
421
|
+
r,
|
|
422
|
+
box: {
|
|
423
|
+
x: cx - r,
|
|
424
|
+
y: cy - r,
|
|
425
|
+
w: r * 2,
|
|
426
|
+
h: r * 2
|
|
427
|
+
},
|
|
428
|
+
...shapeBase
|
|
429
|
+
}] : [];
|
|
430
|
+
}
|
|
431
|
+
if (tagName === "ellipse") {
|
|
432
|
+
const cx = getSvgNumber(node, "cx", 0);
|
|
433
|
+
const cy = getSvgNumber(node, "cy", 0);
|
|
434
|
+
const rx = getSvgNumber(node, "rx", 0);
|
|
435
|
+
const ry = getSvgNumber(node, "ry", 0);
|
|
436
|
+
return rx > 0 && ry > 0 ? [{
|
|
437
|
+
type: "ellipse",
|
|
438
|
+
cx,
|
|
439
|
+
cy,
|
|
440
|
+
rx,
|
|
441
|
+
ry,
|
|
442
|
+
box: {
|
|
443
|
+
x: cx - rx,
|
|
444
|
+
y: cy - ry,
|
|
445
|
+
w: rx * 2,
|
|
446
|
+
h: ry * 2
|
|
447
|
+
},
|
|
448
|
+
...shapeBase
|
|
449
|
+
}] : [];
|
|
450
|
+
}
|
|
451
|
+
if (tagName === "rect") {
|
|
452
|
+
const x = getSvgNumber(node, "x", 0);
|
|
453
|
+
const y = getSvgNumber(node, "y", 0);
|
|
454
|
+
const w = getSvgNumber(node, "width", 0);
|
|
455
|
+
const h = getSvgNumber(node, "height", 0);
|
|
456
|
+
return w > 0 && h > 0 ? [{
|
|
457
|
+
type: "rect",
|
|
458
|
+
x,
|
|
459
|
+
y,
|
|
460
|
+
w,
|
|
461
|
+
h,
|
|
462
|
+
box: {
|
|
463
|
+
x,
|
|
464
|
+
y,
|
|
465
|
+
w,
|
|
466
|
+
h
|
|
467
|
+
},
|
|
468
|
+
...shapeBase
|
|
469
|
+
}] : [];
|
|
470
|
+
}
|
|
471
|
+
if (tagName === "line") {
|
|
472
|
+
const x1 = getSvgNumber(node, "x1", 0);
|
|
473
|
+
const y1 = getSvgNumber(node, "y1", 0);
|
|
474
|
+
const x2 = getSvgNumber(node, "x2", 0);
|
|
475
|
+
const y2 = getSvgNumber(node, "y2", 0);
|
|
476
|
+
return [{
|
|
477
|
+
type: "line",
|
|
478
|
+
x1,
|
|
479
|
+
y1,
|
|
480
|
+
x2,
|
|
481
|
+
y2,
|
|
482
|
+
box: getPointsBox([[x1, y1], [x2, y2]]),
|
|
483
|
+
...shapeBase
|
|
484
|
+
}];
|
|
485
|
+
}
|
|
486
|
+
if (tagName === "path") {
|
|
487
|
+
const d = node.getAttribute("d") || "";
|
|
488
|
+
return d ? [{
|
|
489
|
+
type: "path",
|
|
490
|
+
d,
|
|
491
|
+
box: fallbackBox,
|
|
492
|
+
...shapeBase
|
|
493
|
+
}] : [];
|
|
494
|
+
}
|
|
495
|
+
return [];
|
|
469
496
|
}
|
|
470
497
|
/**
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
498
|
+
* Parse a custom SVG point pattern string.
|
|
499
|
+
* @param {string} pattern SVG pattern
|
|
500
|
+
* @returns {object|null} Parsed pattern
|
|
501
|
+
* @private
|
|
502
|
+
*/
|
|
476
503
|
function parseCustomPointPattern(pattern) {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
mergePointBoxes(shapes.map(shape => shape.box)),
|
|
496
|
-
paints: collectPointPaints(defs)
|
|
497
|
-
};
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
catch {
|
|
501
|
-
parsed = null;
|
|
502
|
-
}
|
|
503
|
-
customPointPatternCache.set(pattern, parsed);
|
|
504
|
-
return parsed;
|
|
504
|
+
if (!/^</.test(pattern)) return null;
|
|
505
|
+
if (customPointPatternCache.has(pattern)) return customPointPatternCache.get(pattern) || null;
|
|
506
|
+
let parsed = null;
|
|
507
|
+
try {
|
|
508
|
+
const root = new win.DOMParser().parseFromString(sanitize(pattern), "image/svg+xml").documentElement;
|
|
509
|
+
const fallbackBox = parseViewBox(root.getAttribute("viewBox")) || DEFAULT_POINT_VIEWBOX;
|
|
510
|
+
const defs = collectPointDefs(root);
|
|
511
|
+
const shapes = parseCustomPointNode(root, fallbackBox, defs);
|
|
512
|
+
if (shapes.length) parsed = {
|
|
513
|
+
shapes,
|
|
514
|
+
box: root.tagName.toLowerCase() === "svg" ? fallbackBox : mergePointBoxes(shapes.map((shape) => shape.box)),
|
|
515
|
+
paints: collectPointPaints(defs)
|
|
516
|
+
};
|
|
517
|
+
} catch {
|
|
518
|
+
parsed = null;
|
|
519
|
+
}
|
|
520
|
+
customPointPatternCache.set(pattern, parsed);
|
|
521
|
+
return parsed;
|
|
505
522
|
}
|
|
506
523
|
/**
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
524
|
+
* Trace a parsed custom SVG point shape.
|
|
525
|
+
* @param {CanvasRenderingContext2D} ctx Canvas context
|
|
526
|
+
* @param {object} shape Parsed shape
|
|
527
|
+
* @private
|
|
528
|
+
*/
|
|
512
529
|
function traceCustomPointShape(ctx, shape) {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
}
|
|
530
|
-
else if (shape.type === "line") {
|
|
531
|
-
ctx.moveTo(shape.x1, shape.y1);
|
|
532
|
-
ctx.lineTo(shape.x2, shape.y2);
|
|
533
|
-
}
|
|
530
|
+
if (shape.type === "polygon" || shape.type === "polyline") {
|
|
531
|
+
const [start, ...rest] = shape.points;
|
|
532
|
+
ctx.moveTo(start[0], start[1]);
|
|
533
|
+
rest.forEach(([x, y]) => ctx.lineTo(x, y));
|
|
534
|
+
shape.type === "polygon" && ctx.closePath();
|
|
535
|
+
} else if (shape.type === "circle") {
|
|
536
|
+
ctx.moveTo(shape.cx + shape.r, shape.cy);
|
|
537
|
+
ctx.arc(shape.cx, shape.cy, shape.r, 0, Math.PI * 2);
|
|
538
|
+
} else if (shape.type === "ellipse") {
|
|
539
|
+
ctx.moveTo(shape.cx + shape.rx, shape.cy);
|
|
540
|
+
ctx.ellipse(shape.cx, shape.cy, shape.rx, shape.ry, 0, 0, Math.PI * 2);
|
|
541
|
+
} else if (shape.type === "rect") ctx.rect(shape.x, shape.y, shape.w, shape.h);
|
|
542
|
+
else if (shape.type === "line") {
|
|
543
|
+
ctx.moveTo(shape.x1, shape.y1);
|
|
544
|
+
ctx.lineTo(shape.x2, shape.y2);
|
|
545
|
+
}
|
|
534
546
|
}
|
|
535
547
|
/**
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
548
|
+
* Extract local SVG paint server id.
|
|
549
|
+
* @param {string|null|undefined} value Paint value
|
|
550
|
+
* @returns {string|null} Paint id
|
|
551
|
+
* @private
|
|
552
|
+
*/
|
|
541
553
|
function getCustomPointPaintId(value) {
|
|
542
|
-
|
|
543
|
-
return match?.[1] || null;
|
|
554
|
+
return (value?.match(/^url\(#([^)]+)\)$/))?.[1] || null;
|
|
544
555
|
}
|
|
545
556
|
/**
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
557
|
+
* Create canvas gradient for a local custom point paint server.
|
|
558
|
+
* @param {CanvasRenderingContext2D} ctx Canvas context
|
|
559
|
+
* @param {object} paint Paint server
|
|
560
|
+
* @param {object} box Pattern box
|
|
561
|
+
* @returns {CanvasGradient} Canvas gradient
|
|
562
|
+
* @private
|
|
563
|
+
*/
|
|
553
564
|
function createCustomPointPaint(ctx, paint, box) {
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
});
|
|
561
|
-
return gradient;
|
|
565
|
+
const { x, y, w, h } = box;
|
|
566
|
+
const gradient = paint.type === "linearGradient" ? ctx.createLinearGradient(pointPaintCoord(paint.x1, x, w, x), pointPaintCoord(paint.y1, y, h, y), pointPaintCoord(paint.x2, x, w, x + w), pointPaintCoord(paint.y2, y, h, y)) : ctx.createRadialGradient(pointPaintCoord(paint.fx, x, w, x + w / 2), pointPaintCoord(paint.fy, y, h, y + h / 2), pointPaintCoord(paint.fr, x, Math.max(w, h), 0), pointPaintCoord(paint.cx, x, w, x + w / 2), pointPaintCoord(paint.cy, y, h, y + h / 2), Math.max(.01, pointPaintCoord(paint.r, 0, Math.max(w, h), Math.max(w, h) / 2)));
|
|
567
|
+
paint.stops.forEach(({ offset, color, opacity }) => {
|
|
568
|
+
gradient.addColorStop(offset, opacity === void 0 ? color : withOpacity(color, opacity));
|
|
569
|
+
});
|
|
570
|
+
return gradient;
|
|
562
571
|
}
|
|
563
572
|
/**
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
+
* Resolve custom SVG point shape style against the caller-provided canvas style.
|
|
574
|
+
* @param {CanvasRenderingContext2D} ctx Canvas context
|
|
575
|
+
* @param {object} base Base canvas style
|
|
576
|
+
* @param {object} shapeStyle SVG shape style
|
|
577
|
+
* @param {Map} paints Local paint servers
|
|
578
|
+
* @param {object} box Pattern box
|
|
579
|
+
* @returns {object} Draw style and paint flags
|
|
580
|
+
* @private
|
|
581
|
+
*/
|
|
573
582
|
function getCustomPointDrawStyle(ctx, base, shapeStyle, paints, box) {
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
style.stroke = shapeStyle.stroke;
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
shapeStyle.lineWidth !== undefined && (style.lineWidth = shapeStyle.lineWidth);
|
|
600
|
-
return {
|
|
601
|
-
style,
|
|
602
|
-
shouldFill: shapeStyle.fill !== null && (!style.stroke || style.fill !== undefined),
|
|
603
|
-
shouldStroke: shapeStyle.stroke !== null && style.stroke !== undefined,
|
|
604
|
-
alpha: shapeStyle.alpha
|
|
605
|
-
};
|
|
583
|
+
const style = { ...base || {} };
|
|
584
|
+
if (shapeStyle.fill !== void 0) {
|
|
585
|
+
if (shapeStyle.fill === null) delete style.fill;
|
|
586
|
+
else if (getCustomPointPaintId(shapeStyle.fill)) {
|
|
587
|
+
const paint = paints.get(getCustomPointPaintId(shapeStyle.fill));
|
|
588
|
+
paint && (style.fill = createCustomPointPaint(ctx, paint, box));
|
|
589
|
+
} else if (!/^url\(/.test(shapeStyle.fill)) style.fill = shapeStyle.fill;
|
|
590
|
+
}
|
|
591
|
+
if (shapeStyle.stroke !== void 0) {
|
|
592
|
+
if (shapeStyle.stroke === null) delete style.stroke;
|
|
593
|
+
else if (getCustomPointPaintId(shapeStyle.stroke)) {
|
|
594
|
+
const paint = paints.get(getCustomPointPaintId(shapeStyle.stroke));
|
|
595
|
+
paint && (style.stroke = createCustomPointPaint(ctx, paint, box));
|
|
596
|
+
} else if (!/^url\(/.test(shapeStyle.stroke)) style.stroke = shapeStyle.stroke;
|
|
597
|
+
}
|
|
598
|
+
shapeStyle.lineWidth !== void 0 && (style.lineWidth = shapeStyle.lineWidth);
|
|
599
|
+
return {
|
|
600
|
+
style,
|
|
601
|
+
shouldFill: shapeStyle.fill !== null && (!style.stroke || style.fill !== void 0),
|
|
602
|
+
shouldStroke: shapeStyle.stroke !== null && style.stroke !== void 0,
|
|
603
|
+
alpha: shapeStyle.alpha
|
|
604
|
+
};
|
|
606
605
|
}
|
|
607
606
|
/**
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
607
|
+
* Draw built-in or custom SVG point pattern.
|
|
608
|
+
* @param {CanvasPainter} painter Canvas painter
|
|
609
|
+
* @param {string} pattern Point type or SVG pattern
|
|
610
|
+
* @param {number} x X coordinate
|
|
611
|
+
* @param {number} y Y coordinate
|
|
612
|
+
* @param {number} r Radius
|
|
613
|
+
* @param {object} style Optional style
|
|
614
|
+
* @param {number} baseR Unexpanded radius used to resolve custom pattern scale
|
|
615
|
+
* @private
|
|
616
|
+
*/
|
|
618
617
|
function drawPointPattern(painter, pattern, x, y, r, style, baseR = r) {
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
});
|
|
618
|
+
if (isBuiltinPointType(pattern)) {
|
|
619
|
+
painter.point(pattern, x, y, r, style);
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
const parsed = parseCustomPointPattern(pattern);
|
|
623
|
+
if (!parsed || r <= 0) {
|
|
624
|
+
painter.point("circle", x, y, r, style);
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
painter.withState((ctx) => {
|
|
628
|
+
const { box, paints, shapes } = parsed;
|
|
629
|
+
const scale = baseR > 0 ? r / baseR : 1;
|
|
630
|
+
const drawShape = (shape) => {
|
|
631
|
+
const { style: drawStyle, shouldFill, shouldStroke, alpha } = getCustomPointDrawStyle(ctx, style, shape.style, paints, box);
|
|
632
|
+
ctx.save();
|
|
633
|
+
painter.applyStyle(drawStyle);
|
|
634
|
+
alpha !== void 0 && (ctx.globalAlpha *= alpha);
|
|
635
|
+
ctx.transform(...shape.matrix);
|
|
636
|
+
ctx.beginPath();
|
|
637
|
+
if (shape.type === "path") {
|
|
638
|
+
if (win.Path2D) {
|
|
639
|
+
const path = shape.path2D || (shape.path2D = new win.Path2D(shape.d));
|
|
640
|
+
shouldFill && ctx.fill(path);
|
|
641
|
+
shouldStroke && ctx.stroke(path);
|
|
642
|
+
}
|
|
643
|
+
} else {
|
|
644
|
+
traceCustomPointShape(ctx, shape);
|
|
645
|
+
shouldFill && ctx.fill();
|
|
646
|
+
shouldStroke && ctx.stroke();
|
|
647
|
+
}
|
|
648
|
+
ctx.restore();
|
|
649
|
+
};
|
|
650
|
+
ctx.translate(x - (box.x + box.w / 2) * scale, y - (box.y + box.h / 2) * scale);
|
|
651
|
+
ctx.scale(scale, scale);
|
|
652
|
+
shapes.forEach(drawShape);
|
|
653
|
+
});
|
|
656
654
|
}
|
|
657
|
-
|
|
655
|
+
//#endregion
|
|
658
656
|
export { drawPointPattern };
|