@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-03-14T13:27:19Z
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
@@ -159,7 +159,7 @@ For Node.js REPL:
159
159
  const hiccupSvg = await import("@thi.ng/hiccup-svg");
160
160
  ```
161
161
 
162
- Package sizes (brotli'd, pre-treeshake): ESM: 2.31 KB
162
+ Package sizes (brotli'd, pre-treeshake): ESM: 2.36 KB
163
163
 
164
164
  ## Dependencies
165
165
 
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 SVG
5
- * serialization. This conversion also involves translation & reorg of various
6
- * attributes. Returns new tree. The original remains untouched, as will any
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 SVG
39
- * serialization. This conversion also involves translation & reorg of various
40
- * attributes. Returns new tree. The original remains untouched, as will any
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
- const res = [type, fattribs(attribs)];
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 && res.push(c);
80
+ c != null && result.push(c);
65
81
  }
66
- return res;
67
- }
82
+ break;
68
83
  case "linearGradient":
69
- return linearGradient(attribs.id, attribs.from, attribs.to, tree[2], {
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
- return radialGradient(attribs.id, attribs.from, attribs.to, attribs.r1, attribs.r2, tree[2], {
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
- return circle(tree[2], tree[3], attribs, ...tree.slice(4));
100
+ result = circle(tree[2], tree[3], attribs, ...tree.slice(4));
101
+ break;
84
102
  case "ellipse":
85
- return ellipse(tree[2], tree[3][0], tree[3][1], attribs, ...tree.slice(4));
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
- return roundedRect(tree[2], tree[3], tree[4], r, r, attribs, ...tree.slice(6));
107
+ result = roundedRect(tree[2], tree[3], tree[4], r, r, attribs, ...tree.slice(6));
108
+ break;
89
109
  }
90
110
  case "line":
91
- return line(tree[2], tree[3], attribs, ...tree.slice(4));
111
+ result = line(tree[2], tree[3], attribs, ...tree.slice(4));
112
+ break;
92
113
  case "hline":
93
- return hline(tree[2], attribs);
114
+ result = hline(tree[2], attribs);
115
+ break;
94
116
  case "vline":
95
- return vline(tree[2], attribs);
117
+ result = vline(tree[2], attribs);
118
+ break;
96
119
  case "polyline":
97
- return polyline(tree[2], attribs, ...tree.slice(3));
120
+ result = polyline(tree[2], attribs, ...tree.slice(3));
121
+ break;
98
122
  case "polygon":
99
- return polygon(tree[2], attribs, ...tree.slice(3));
123
+ result = polygon(tree[2], attribs, ...tree.slice(3));
124
+ break;
100
125
  case "path":
101
- return path(tree[2], attribs, ...tree.slice(3));
126
+ result = path(tree[2], attribs, ...tree.slice(3));
127
+ break;
102
128
  case "text":
103
- return text(tree[2], tree[3], attribs, ...tree.slice(4));
129
+ result = text(tree[2], tree[3], attribs, ...tree.slice(4));
130
+ break;
104
131
  case "img":
105
- return image(tree[3], tree[2].src, attribs, ...tree.slice(4));
132
+ result = image(tree[3], tree[2].src, attribs, ...tree.slice(4));
133
+ break;
106
134
  case "points":
107
- return points(tree[2], attribs.shape, attribs.size, attribs, ...tree.slice(3));
135
+ result = points(tree[2], attribs.shape, attribs.size, attribs, ...tree.slice(3));
136
+ break;
108
137
  case "packedPoints":
109
- return packedPoints(tree[2], attribs.shape, attribs.size, attribs, ...tree.slice(3));
138
+ result = packedPoints(tree[2], attribs.shape, attribs.size, attribs, ...tree.slice(3));
139
+ break;
110
140
  default:
111
- return tree;
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 | number;
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
- * [`asCSS()`](https://docs.thi.ng/umbrella/color/functions/asCSS.html).
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) => (x === (x | 0) ? x : x.toFixed(PRECISION));
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
- * [`asCSS()`](https://docs.thi.ng/umbrella/color/functions/asCSS.html).
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": "4.3.39",
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.10",
38
- "@thi.ng/color": "^5.4.5",
39
- "@thi.ng/prefixes": "^2.1.20"
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.13",
44
- "rimraf": "^4.4.0",
43
+ "@thi.ng/testament": "^0.3.15",
44
+ "rimraf": "^4.4.1",
45
45
  "tools": "^0.0.1",
46
- "typedoc": "^0.23.26",
47
- "typescript": "^4.9.5"
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": "1359645f3af8a7d0d43fe7944ea5cd865832f8ee\n"
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 `convert: true` attrib is given, all body elements will be
8
- * automatically converted using {@link convertTree}. The `convert` attrib is
9
- * NOT going to be serialized in the final output.
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 `convert: true` attrib is given, all body elements will be
11
- * automatically converted using {@link convertTree}. The `convert` attrib is
12
- * NOT going to be serialized in the final output.
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.convert) {
25
- delete attribs.convert;
25
+ if (attribs.__convert) {
26
+ delete attribs.__convert;
26
27
  body = body.map(convertTree);
27
28
  }
28
29
  return ["svg", attribs, ...body];