@thi.ng/hiccup-svg 4.3.39 → 5.0.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.
- package/CHANGELOG.md +21 -1
- package/README.md +1 -1
- package/convert.d.ts +15 -5
- package/convert.js +61 -27
- package/format.d.ts +13 -3
- package/format.js +13 -4
- package/package.json +9 -9
- package/svg.d.ts +4 -3
- package/svg.js +6 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2023-
|
|
3
|
+
- **Last updated**: 2023-04-08T11:09:50Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,26 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
# [5.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/hiccup-svg@5.0.0) (2023-04-08)
|
|
13
|
+
|
|
14
|
+
#### 🛑 Breaking changes
|
|
15
|
+
|
|
16
|
+
- update svgDoc() conversion handling ([f0e9092](https://github.com/thi-ng/umbrella/commit/f0e9092))
|
|
17
|
+
- BREAKING CHANGE: update svgDoc(), rename `convert` attrib => `__convert`
|
|
18
|
+
- for consistency, keep all control attribs prefixed as `__xxx`
|
|
19
|
+
|
|
20
|
+
#### 🚀 Features
|
|
21
|
+
|
|
22
|
+
- add support for precision attribute ([f81d0d8](https://github.com/thi-ng/umbrella/commit/f81d0d8))
|
|
23
|
+
- update convertTree() to allow dynamic floating point precision
|
|
24
|
+
handling via `__prec` control attrib
|
|
25
|
+
- update docs
|
|
26
|
+
- add tests
|
|
27
|
+
|
|
28
|
+
#### 🩹 Bug fixes
|
|
29
|
+
|
|
30
|
+
- update ff() to always return string ([ae1d844](https://github.com/thi-ng/umbrella/commit/ae1d844))
|
|
31
|
+
|
|
12
32
|
## [4.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/hiccup-svg@4.3.0) (2022-06-20)
|
|
13
33
|
|
|
14
34
|
#### 🚀 Features
|
package/README.md
CHANGED
package/convert.d.ts
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Takes a normalized hiccup tree of [`thi.ng/geom`](https://thi.ng/geom) or
|
|
2
|
+
* Takes a normalized hiccup tree of [`thi.ng/geom`](https://thi.ng/geom) and/or
|
|
3
3
|
* [thi.ng/hdom-canvas](https://thi.ng/hdom-canvas) shape definitions and
|
|
4
|
-
* recursively converts it into an hiccup flavor which is compatible for
|
|
5
|
-
* serialization. This conversion also involves translation
|
|
6
|
-
* attributes.
|
|
7
|
-
* unrecognized tree/shape nodes.
|
|
4
|
+
* recursively converts it into an hiccup flavor which is compatible for direct
|
|
5
|
+
* SVG serialization. This conversion also involves translation, stringification
|
|
6
|
+
* & reorg of various attributes. The function returns new tree. The original
|
|
7
|
+
* remains untouched, as will any unrecognized tree/shape nodes.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* The `__prec` control attribute can be used (on a per-shape basis) to control
|
|
11
|
+
* the formatting used for various floating point values (except color
|
|
12
|
+
* conversions). See {@link setPrecision}. Child shapes (of a group) inherit the
|
|
13
|
+
* precision setting of their parent.
|
|
14
|
+
*
|
|
15
|
+
* To control the formatting precision for colors, use [the relevant function in
|
|
16
|
+
* the thi.ng/color
|
|
17
|
+
* package](https://docs.thi.ng/umbrella/color/functions/setPrecision.html).
|
|
8
18
|
*
|
|
9
19
|
* @param tree - shape tree
|
|
10
20
|
*/
|
package/convert.js
CHANGED
|
@@ -2,7 +2,7 @@ import { implementsFunction } from "@thi.ng/checks/implements-function";
|
|
|
2
2
|
import { isArray } from "@thi.ng/checks/is-array";
|
|
3
3
|
import { circle } from "./circle.js";
|
|
4
4
|
import { ellipse } from "./ellipse.js";
|
|
5
|
-
import { fattribs } from "./format.js";
|
|
5
|
+
import { PRECISION, fattribs, setPrecision } from "./format.js";
|
|
6
6
|
import { linearGradient, radialGradient } from "./gradients.js";
|
|
7
7
|
import { image } from "./image.js";
|
|
8
8
|
import { hline, line, vline } from "./line.js";
|
|
@@ -32,13 +32,24 @@ const BASE_LINE = {
|
|
|
32
32
|
top: "text-top",
|
|
33
33
|
bottom: "text-bottom",
|
|
34
34
|
};
|
|
35
|
+
const precisionStack = [];
|
|
35
36
|
/**
|
|
36
|
-
* Takes a normalized hiccup tree of [`thi.ng/geom`](https://thi.ng/geom) or
|
|
37
|
+
* Takes a normalized hiccup tree of [`thi.ng/geom`](https://thi.ng/geom) and/or
|
|
37
38
|
* [thi.ng/hdom-canvas](https://thi.ng/hdom-canvas) shape definitions and
|
|
38
|
-
* recursively converts it into an hiccup flavor which is compatible for
|
|
39
|
-
* serialization. This conversion also involves translation
|
|
40
|
-
* attributes.
|
|
41
|
-
* unrecognized tree/shape nodes.
|
|
39
|
+
* recursively converts it into an hiccup flavor which is compatible for direct
|
|
40
|
+
* SVG serialization. This conversion also involves translation, stringification
|
|
41
|
+
* & reorg of various attributes. The function returns new tree. The original
|
|
42
|
+
* remains untouched, as will any unrecognized tree/shape nodes.
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* The `__prec` control attribute can be used (on a per-shape basis) to control
|
|
46
|
+
* the formatting used for various floating point values (except color
|
|
47
|
+
* conversions). See {@link setPrecision}. Child shapes (of a group) inherit the
|
|
48
|
+
* precision setting of their parent.
|
|
49
|
+
*
|
|
50
|
+
* To control the formatting precision for colors, use [the relevant function in
|
|
51
|
+
* the thi.ng/color
|
|
52
|
+
* package](https://docs.thi.ng/umbrella/color/functions/setPrecision.html).
|
|
42
53
|
*
|
|
43
54
|
* @param tree - shape tree
|
|
44
55
|
*/
|
|
@@ -53,63 +64,86 @@ export const convertTree = (tree) => {
|
|
|
53
64
|
return tree.map(convertTree);
|
|
54
65
|
}
|
|
55
66
|
let attribs = convertAttribs(tree[1]);
|
|
67
|
+
if (attribs.__prec) {
|
|
68
|
+
precisionStack.push(PRECISION);
|
|
69
|
+
setPrecision(attribs.__prec);
|
|
70
|
+
}
|
|
71
|
+
let result;
|
|
56
72
|
switch (tree[0]) {
|
|
57
73
|
case "svg":
|
|
58
74
|
case "defs":
|
|
59
75
|
case "a":
|
|
60
|
-
case "g":
|
|
61
|
-
|
|
76
|
+
case "g":
|
|
77
|
+
result = [type, fattribs(attribs)];
|
|
62
78
|
for (let i = 2, n = tree.length; i < n; i++) {
|
|
63
79
|
const c = convertTree(tree[i]);
|
|
64
|
-
c != null &&
|
|
80
|
+
c != null && result.push(c);
|
|
65
81
|
}
|
|
66
|
-
|
|
67
|
-
}
|
|
82
|
+
break;
|
|
68
83
|
case "linearGradient":
|
|
69
|
-
|
|
84
|
+
result = linearGradient(attribs.id, attribs.from, attribs.to, tree[2], {
|
|
70
85
|
gradientUnits: attribs.gradientUnits || "userSpaceOnUse",
|
|
71
86
|
...(attribs.gradientTransform
|
|
72
87
|
? { gradientTransform: attribs.gradientTransform }
|
|
73
88
|
: null),
|
|
74
89
|
});
|
|
90
|
+
break;
|
|
75
91
|
case "radialGradient":
|
|
76
|
-
|
|
92
|
+
result = radialGradient(attribs.id, attribs.from, attribs.to, attribs.r1, attribs.r2, tree[2], {
|
|
77
93
|
gradientUnits: attribs.gradientUnits || "userSpaceOnUse",
|
|
78
94
|
...(attribs.gradientTransform
|
|
79
95
|
? { gradientTransform: attribs.gradientTransform }
|
|
80
96
|
: null),
|
|
81
97
|
});
|
|
98
|
+
break;
|
|
82
99
|
case "circle":
|
|
83
|
-
|
|
100
|
+
result = circle(tree[2], tree[3], attribs, ...tree.slice(4));
|
|
101
|
+
break;
|
|
84
102
|
case "ellipse":
|
|
85
|
-
|
|
103
|
+
result = ellipse(tree[2], tree[3][0], tree[3][1], attribs, ...tree.slice(4));
|
|
104
|
+
break;
|
|
86
105
|
case "rect": {
|
|
87
106
|
const r = tree[5] || 0;
|
|
88
|
-
|
|
107
|
+
result = roundedRect(tree[2], tree[3], tree[4], r, r, attribs, ...tree.slice(6));
|
|
108
|
+
break;
|
|
89
109
|
}
|
|
90
110
|
case "line":
|
|
91
|
-
|
|
111
|
+
result = line(tree[2], tree[3], attribs, ...tree.slice(4));
|
|
112
|
+
break;
|
|
92
113
|
case "hline":
|
|
93
|
-
|
|
114
|
+
result = hline(tree[2], attribs);
|
|
115
|
+
break;
|
|
94
116
|
case "vline":
|
|
95
|
-
|
|
117
|
+
result = vline(tree[2], attribs);
|
|
118
|
+
break;
|
|
96
119
|
case "polyline":
|
|
97
|
-
|
|
120
|
+
result = polyline(tree[2], attribs, ...tree.slice(3));
|
|
121
|
+
break;
|
|
98
122
|
case "polygon":
|
|
99
|
-
|
|
123
|
+
result = polygon(tree[2], attribs, ...tree.slice(3));
|
|
124
|
+
break;
|
|
100
125
|
case "path":
|
|
101
|
-
|
|
126
|
+
result = path(tree[2], attribs, ...tree.slice(3));
|
|
127
|
+
break;
|
|
102
128
|
case "text":
|
|
103
|
-
|
|
129
|
+
result = text(tree[2], tree[3], attribs, ...tree.slice(4));
|
|
130
|
+
break;
|
|
104
131
|
case "img":
|
|
105
|
-
|
|
132
|
+
result = image(tree[3], tree[2].src, attribs, ...tree.slice(4));
|
|
133
|
+
break;
|
|
106
134
|
case "points":
|
|
107
|
-
|
|
135
|
+
result = points(tree[2], attribs.shape, attribs.size, attribs, ...tree.slice(3));
|
|
136
|
+
break;
|
|
108
137
|
case "packedPoints":
|
|
109
|
-
|
|
138
|
+
result = packedPoints(tree[2], attribs.shape, attribs.size, attribs, ...tree.slice(3));
|
|
139
|
+
break;
|
|
110
140
|
default:
|
|
111
|
-
|
|
141
|
+
result = tree;
|
|
142
|
+
}
|
|
143
|
+
if (attribs.__prec) {
|
|
144
|
+
setPrecision(precisionStack.pop());
|
|
112
145
|
}
|
|
146
|
+
return result;
|
|
113
147
|
};
|
|
114
148
|
const convertAttribs = (attribs) => {
|
|
115
149
|
const res = {};
|
package/format.d.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import type { Vec2Like } from "./api.js";
|
|
2
|
+
export declare let PRECISION: number;
|
|
3
|
+
/**
|
|
4
|
+
* Sets the number of fractional digits used for formatting various floating
|
|
5
|
+
* point values in the serialized SVG. The current value can be read via
|
|
6
|
+
* {@link PRECISION}.
|
|
7
|
+
*
|
|
8
|
+
* @param n
|
|
9
|
+
*/
|
|
2
10
|
export declare const setPrecision: (n: number) => number;
|
|
3
11
|
/** @internal */
|
|
4
|
-
export declare const ff: (x: number) => string
|
|
12
|
+
export declare const ff: (x: number) => string;
|
|
5
13
|
/** @internal */
|
|
6
14
|
export declare const fpoint: (p: Vec2Like) => string;
|
|
7
15
|
/** @internal */
|
|
@@ -27,7 +35,7 @@ export declare const fpoints: (pts: Vec2Like[], sep?: string) => string;
|
|
|
27
35
|
* number or an
|
|
28
36
|
* [`IColor`](https://docs.thi.ng/umbrella/color/interfaces/IColor.html)
|
|
29
37
|
* instance, it will be converted into a CSS color string using
|
|
30
|
-
* [`
|
|
38
|
+
* [`css()`](https://docs.thi.ng/umbrella/color/functions/css.html).
|
|
31
39
|
*
|
|
32
40
|
* String color attribs prefixed with `$` are replaced with `url(#...)` refs
|
|
33
41
|
* (used for referencing gradients).
|
|
@@ -46,7 +54,9 @@ export declare const fpoints: (pts: Vec2Like[], sep?: string) => string;
|
|
|
46
54
|
export declare const fattribs: (attribs: any, ...numericIDs: string[]) => any;
|
|
47
55
|
/**
|
|
48
56
|
* Attempts to convert a single color attrib value. If `col` is prefixed with
|
|
49
|
-
* `$`, the value will be converted into a `url(#...)` reference.
|
|
57
|
+
* `$`, the value will be converted into a `url(#...)` reference. If not a
|
|
58
|
+
* string already, it will be converted into a CSS color string using
|
|
59
|
+
* [`css()`](https://docs.thi.ng/umbrella/color/functions/css.html)
|
|
50
60
|
*
|
|
51
61
|
* {@link fattribs}
|
|
52
62
|
*
|
package/format.js
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import { isArrayLike } from "@thi.ng/checks/is-arraylike";
|
|
2
2
|
import { isString } from "@thi.ng/checks/is-string";
|
|
3
3
|
import { css } from "@thi.ng/color/css/css";
|
|
4
|
-
let PRECISION = 3;
|
|
4
|
+
export let PRECISION = 3;
|
|
5
|
+
/**
|
|
6
|
+
* Sets the number of fractional digits used for formatting various floating
|
|
7
|
+
* point values in the serialized SVG. The current value can be read via
|
|
8
|
+
* {@link PRECISION}.
|
|
9
|
+
*
|
|
10
|
+
* @param n
|
|
11
|
+
*/
|
|
5
12
|
export const setPrecision = (n) => (PRECISION = n);
|
|
6
13
|
/** @internal */
|
|
7
|
-
export const ff = (x) =>
|
|
14
|
+
export const ff = (x) => x === (x | 0) ? String(x) : x.toFixed(PRECISION);
|
|
8
15
|
/** @internal */
|
|
9
16
|
export const fpoint = (p) => ff(p[0]) + "," + ff(p[1]);
|
|
10
17
|
/** @internal */
|
|
@@ -52,7 +59,7 @@ const numericAttribs = (attribs, ids) => {
|
|
|
52
59
|
* number or an
|
|
53
60
|
* [`IColor`](https://docs.thi.ng/umbrella/color/interfaces/IColor.html)
|
|
54
61
|
* instance, it will be converted into a CSS color string using
|
|
55
|
-
* [`
|
|
62
|
+
* [`css()`](https://docs.thi.ng/umbrella/color/functions/css.html).
|
|
56
63
|
*
|
|
57
64
|
* String color attribs prefixed with `$` are replaced with `url(#...)` refs
|
|
58
65
|
* (used for referencing gradients).
|
|
@@ -132,7 +139,9 @@ const buildTransform = (attribs) => {
|
|
|
132
139
|
};
|
|
133
140
|
/**
|
|
134
141
|
* Attempts to convert a single color attrib value. If `col` is prefixed with
|
|
135
|
-
* `$`, the value will be converted into a `url(#...)` reference.
|
|
142
|
+
* `$`, the value will be converted into a `url(#...)` reference. If not a
|
|
143
|
+
* string already, it will be converted into a CSS color string using
|
|
144
|
+
* [`css()`](https://docs.thi.ng/umbrella/color/functions/css.html)
|
|
136
145
|
*
|
|
137
146
|
* {@link fattribs}
|
|
138
147
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/hiccup-svg",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "SVG element functions for @thi.ng/hiccup & related tooling",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -34,17 +34,17 @@
|
|
|
34
34
|
"test": "testament test"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@thi.ng/checks": "^3.3.
|
|
38
|
-
"@thi.ng/color": "^5.4.
|
|
39
|
-
"@thi.ng/prefixes": "^2.1.
|
|
37
|
+
"@thi.ng/checks": "^3.3.12",
|
|
38
|
+
"@thi.ng/color": "^5.4.7",
|
|
39
|
+
"@thi.ng/prefixes": "^2.1.22"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@microsoft/api-extractor": "^7.34.4",
|
|
43
|
-
"@thi.ng/testament": "^0.3.
|
|
44
|
-
"rimraf": "^4.4.
|
|
43
|
+
"@thi.ng/testament": "^0.3.15",
|
|
44
|
+
"rimraf": "^4.4.1",
|
|
45
45
|
"tools": "^0.0.1",
|
|
46
|
-
"typedoc": "^0.23.
|
|
47
|
-
"typescript": "^
|
|
46
|
+
"typedoc": "^0.23.28",
|
|
47
|
+
"typescript": "^5.0.4"
|
|
48
48
|
},
|
|
49
49
|
"keywords": [
|
|
50
50
|
"arc",
|
|
@@ -135,5 +135,5 @@
|
|
|
135
135
|
"parent": "@thi.ng/hiccup",
|
|
136
136
|
"year": 2016
|
|
137
137
|
},
|
|
138
|
-
"gitHead": "
|
|
138
|
+
"gitHead": "abcedd9e4e06a4b631f363610eec572f79b571c1\n"
|
|
139
139
|
}
|
package/svg.d.ts
CHANGED
|
@@ -4,9 +4,10 @@
|
|
|
4
4
|
* legacy tooling.
|
|
5
5
|
*
|
|
6
6
|
* @remarks
|
|
7
|
-
* If the `
|
|
8
|
-
* automatically converted using {@link convertTree}. The `
|
|
9
|
-
* NOT going to be serialized in the final
|
|
7
|
+
* If the `__convert` boolean attrib is enabled, all body elements will be
|
|
8
|
+
* automatically converted using {@link convertTree}. The `__convert` attrib
|
|
9
|
+
* will be removed afterward and is NOT going to be serialized in the final
|
|
10
|
+
* output.
|
|
10
11
|
*
|
|
11
12
|
* @param attribs - attributes object
|
|
12
13
|
* @param body - shape primitives
|
package/svg.js
CHANGED
|
@@ -7,9 +7,10 @@ import { fattribs } from "./format.js";
|
|
|
7
7
|
* legacy tooling.
|
|
8
8
|
*
|
|
9
9
|
* @remarks
|
|
10
|
-
* If the `
|
|
11
|
-
* automatically converted using {@link convertTree}. The `
|
|
12
|
-
* NOT going to be serialized in the final
|
|
10
|
+
* If the `__convert` boolean attrib is enabled, all body elements will be
|
|
11
|
+
* automatically converted using {@link convertTree}. The `__convert` attrib
|
|
12
|
+
* will be removed afterward and is NOT going to be serialized in the final
|
|
13
|
+
* output.
|
|
13
14
|
*
|
|
14
15
|
* @param attribs - attributes object
|
|
15
16
|
* @param body - shape primitives
|
|
@@ -21,8 +22,8 @@ export const svg = (attribs, ...body) => {
|
|
|
21
22
|
"xmlns:xlink": XML_XLINK,
|
|
22
23
|
...attribs,
|
|
23
24
|
}, "width", "height", "stroke-width");
|
|
24
|
-
if (attribs.
|
|
25
|
-
delete attribs.
|
|
25
|
+
if (attribs.__convert) {
|
|
26
|
+
delete attribs.__convert;
|
|
26
27
|
body = body.map(convertTree);
|
|
27
28
|
}
|
|
28
29
|
return ["svg", attribs, ...body];
|