@jscad/svg-serializer 2.2.7 → 2.3.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 +35 -0
- package/README.md +4 -2
- package/index.js +33 -21
- package/package.json +3 -3
- package/tests/geom2.test.js +13 -11
- package/tests/path2.test.js +6 -4
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,41 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [2.3.0](https://github.com/jscad/OpenJSCAD.org/compare/@jscad/svg-serializer@2.2.10...@jscad/svg-serializer@2.3.0) (2022-01-23)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **svg-serializer:** enhanced to serialize id and class attributes to SVG ([ca91bb9](https://github.com/jscad/OpenJSCAD.org/commit/ca91bb9a38232b3ea355aeff905223290539d5c2))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## [2.2.10](https://github.com/jscad/OpenJSCAD.org/compare/@jscad/svg-serializer@2.2.9...@jscad/svg-serializer@2.2.10) (2021-12-26)
|
|
18
|
+
|
|
19
|
+
**Note:** Version bump only for package @jscad/svg-serializer
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
## [2.2.9](https://github.com/jscad/OpenJSCAD.org/compare/@jscad/svg-serializer@2.2.8...@jscad/svg-serializer@2.2.9) (2021-12-11)
|
|
26
|
+
|
|
27
|
+
**Note:** Version bump only for package @jscad/svg-serializer
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
## [2.2.8](https://github.com/jscad/OpenJSCAD.org/compare/@jscad/svg-serializer@2.2.7...@jscad/svg-serializer@2.2.8) (2021-11-07)
|
|
34
|
+
|
|
35
|
+
**Note:** Version bump only for package @jscad/svg-serializer
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
6
41
|
## [2.2.7](https://github.com/jscad/OpenJSCAD.org/compare/@jscad/svg-serializer@2.2.6...@jscad/svg-serializer@2.2.7) (2021-10-17)
|
|
7
42
|
|
|
8
43
|
**Note:** Version bump only for package @jscad/svg-serializer
|
package/README.md
CHANGED
|
@@ -23,8 +23,10 @@ This serializer outputs a 'blobable' array of data from one or more JSCAD geomet
|
|
|
23
23
|
The array of data can either be used to create a Blob (`new Blob(blobable)`), or converted to a Node.js buffer.
|
|
24
24
|
|
|
25
25
|
The serialization of the following geometries are possible.
|
|
26
|
-
- serialization of 2D geometry (geom2) to continous SVG paths
|
|
27
|
-
- serialization of 2D paths (path2) to individual SVG paths
|
|
26
|
+
- serialization of 2D geometry (geom2) to continous SVG paths, where the color (initial fill) is 'black' if not provided
|
|
27
|
+
- serialization of 2D paths (path2) to individual SVG paths, where the color (initial stroke) is 'none' if not provided
|
|
28
|
+
|
|
29
|
+
In addition, geometries can have special attributes (id, class) which will be passed on to the SVG paths.
|
|
28
30
|
|
|
29
31
|
## Table of Contents
|
|
30
32
|
|
package/index.js
CHANGED
|
@@ -9,7 +9,7 @@ All code released under MIT license
|
|
|
9
9
|
|
|
10
10
|
Notes:
|
|
11
11
|
1) geom2 conversion to:
|
|
12
|
-
SVG GROUP containing a SVG PATH
|
|
12
|
+
SVG GROUP containing a continous SVG PATH that contains the outlines of the geometry
|
|
13
13
|
2) geom3 conversion to:
|
|
14
14
|
none
|
|
15
15
|
3) path2 conversion to:
|
|
@@ -17,7 +17,15 @@ Notes:
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
|
-
* Serializer of JSCAD geometries to SVG
|
|
20
|
+
* Serializer of JSCAD geometries to SVG source (XML).
|
|
21
|
+
*
|
|
22
|
+
* The serialization of the following geometries are possible.
|
|
23
|
+
* - serialization of 2D geometry (geom2) to SVG path (a continous path containing the outlines of the geometry)
|
|
24
|
+
* - serialization of 2D geometry (path2) to SVG path
|
|
25
|
+
*
|
|
26
|
+
* Colors are added to SVG shapes when found on the geometry.
|
|
27
|
+
* Special attributes (id and class) are added to SVG shapes when found on the geometry.
|
|
28
|
+
*
|
|
21
29
|
* @module io/svg-serializer
|
|
22
30
|
* @example
|
|
23
31
|
* const { serializer, mimeType } = require('@jscad/svg-serializer')
|
|
@@ -32,12 +40,13 @@ const version = require('./package.json').version
|
|
|
32
40
|
const mimeType = 'image/svg+xml'
|
|
33
41
|
|
|
34
42
|
/**
|
|
35
|
-
* Serialize the give objects to SVG
|
|
43
|
+
* Serialize the give objects to SVG code (XML).
|
|
44
|
+
* @see https://www.w3.org/TR/SVG/Overview.html
|
|
36
45
|
* @param {Object} options - options for serialization, REQUIRED
|
|
37
46
|
* @param {String} [options.unit='mm'] - unit of design; em, ex, px, in, cm, mm, pt, pc
|
|
38
47
|
* @param {Function} [options.statusCallback] - call back function for progress ({ progress: 0-100 })
|
|
39
48
|
* @param {Object|Array} objects - objects to serialize as SVG
|
|
40
|
-
* @returns {Array} serialized contents
|
|
49
|
+
* @returns {Array} serialized contents, SVG code (XML string)
|
|
41
50
|
* @alias module:io/svg-serializer.serialize
|
|
42
51
|
* @example
|
|
43
52
|
* const geometry = primitives.square()
|
|
@@ -77,6 +86,9 @@ const serialize = (options, ...objects) => {
|
|
|
77
86
|
width: width + options.unit,
|
|
78
87
|
height: height + options.unit,
|
|
79
88
|
viewBox: ('0 0 ' + width + ' ' + height),
|
|
89
|
+
fill: "none",
|
|
90
|
+
'fill-rule': "evenodd",
|
|
91
|
+
'stroke-width': "0.1px",
|
|
80
92
|
version: '1.1',
|
|
81
93
|
baseProfile: 'tiny',
|
|
82
94
|
xmlns: 'http://www.w3.org/2000/svg',
|
|
@@ -143,32 +155,32 @@ const reflect = (x, y, px, py) => {
|
|
|
143
155
|
const convertGeom2 = (object, offsets, options) => {
|
|
144
156
|
const outlines = geometries.geom2.toOutlines(object)
|
|
145
157
|
const paths = outlines.map((outline) => geometries.path2.fromPoints({ closed: true }, outline))
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
158
|
+
|
|
159
|
+
options.color = "black" // SVG initial color
|
|
160
|
+
if (object.color) options.color = convertColor(object.color)
|
|
161
|
+
options.id = null
|
|
162
|
+
if (object.id) options.id = object.id
|
|
163
|
+
options.class = null
|
|
164
|
+
if (object.class) options.class = object.class
|
|
165
|
+
|
|
151
166
|
return convertToContinousPath(paths, offsets, options)
|
|
152
167
|
}
|
|
153
168
|
|
|
154
169
|
const convertToContinousPath = (paths, offsets, options) => {
|
|
155
170
|
let instructions = ''
|
|
156
171
|
paths.forEach((path) => (instructions += convertPath(path, offsets, options)))
|
|
157
|
-
|
|
158
|
-
if (
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
continouspath = ['path', { 'fill-rule': 'evenodd', fill: convertColor(path0.fill), d: instructions }]
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
return ['g', continouspath]
|
|
172
|
+
const d = { fill: options.color, d: instructions }
|
|
173
|
+
if (options.id) d.id = options.id
|
|
174
|
+
if (options.class) d.class = options.class
|
|
175
|
+
return ['g', ['path', d]]
|
|
165
176
|
}
|
|
166
177
|
|
|
167
178
|
const convertPaths = (paths, offsets, options) => paths.reduce((res, path, i) => {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
179
|
+
d = { d: convertPath(path, offsets, options) }
|
|
180
|
+
if (path.color) d.stroke = convertColor(path.color)
|
|
181
|
+
if (path.id) d.id = path.id
|
|
182
|
+
if (path.class) d.class = path.class
|
|
183
|
+
return res.concat([['path', d]])
|
|
172
184
|
}, ['g'])
|
|
173
185
|
|
|
174
186
|
const convertPath = (path, offsets, options) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jscad/svg-serializer",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "SVG Serializer for JSCAD",
|
|
5
5
|
"repository": "https://github.com/jscad/OpenJSCAD.org",
|
|
6
6
|
"main": "index.js",
|
|
@@ -31,12 +31,12 @@
|
|
|
31
31
|
],
|
|
32
32
|
"license": "MIT",
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@jscad/modeling": "2.
|
|
34
|
+
"@jscad/modeling": "2.7.1",
|
|
35
35
|
"onml": "1.2.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"ava": "3.15.0",
|
|
39
39
|
"nyc": "15.1.0"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "95f05c5f11a53bff469a305d69b99f674dc7d6d3"
|
|
42
42
|
}
|
package/tests/geom2.test.js
CHANGED
|
@@ -25,6 +25,8 @@ test('serialize 2D geometries (simple) to svg', (t) => {
|
|
|
25
25
|
test('serialize 2D geometries (color) to svg', (t) => {
|
|
26
26
|
let cag2 = primitives.rectangle({ size: [10, 20] })
|
|
27
27
|
cag2 = colors.colorize([0.5, 0.5, 0.5, 0.5], cag2)
|
|
28
|
+
cag2.id = 'r2'
|
|
29
|
+
cag2.class = 'gray-rect'
|
|
28
30
|
|
|
29
31
|
const observed2 = serializer.serialize({}, cag2)
|
|
30
32
|
t.deepEqual([expected4], observed2)
|
|
@@ -62,9 +64,9 @@ test('serialize 2D geometries (complex) to svg', (t) => {
|
|
|
62
64
|
const expected1 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
63
65
|
<!-- Created by JSCAD SVG Serializer -->
|
|
64
66
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">
|
|
65
|
-
<svg width="0mm" height="0mm" viewBox="0 0 0 0" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
67
|
+
<svg width="0mm" height="0mm" viewBox="0 0 0 0" fill="none" fill-rule="evenodd" stroke-width="0.1px" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
66
68
|
<g>
|
|
67
|
-
<path d=""/>
|
|
69
|
+
<path fill="black" d=""/>
|
|
68
70
|
</g>
|
|
69
71
|
</svg>
|
|
70
72
|
`
|
|
@@ -72,9 +74,9 @@ const expected1 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
72
74
|
const expected2 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
73
75
|
<!-- Created by JSCAD SVG Serializer -->
|
|
74
76
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">
|
|
75
|
-
<svg width="10mm" height="20mm" viewBox="0 0 10 20" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
77
|
+
<svg width="10mm" height="20mm" viewBox="0 0 10 20" fill="none" fill-rule="evenodd" stroke-width="0.1px" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
76
78
|
<g>
|
|
77
|
-
<path d="M0 20L10 20L10 0L0 0L0 20"/>
|
|
79
|
+
<path fill="black" d="M0 20L10 20L10 0L0 0L0 20"/>
|
|
78
80
|
</g>
|
|
79
81
|
</svg>
|
|
80
82
|
`
|
|
@@ -82,12 +84,12 @@ const expected2 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
82
84
|
const expected3 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
83
85
|
<!-- Created by JSCAD SVG Serializer -->
|
|
84
86
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">
|
|
85
|
-
<svg width="70mm" height="80mm" viewBox="0 0 70 80" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
87
|
+
<svg width="70mm" height="80mm" viewBox="0 0 70 80" fill="none" fill-rule="evenodd" stroke-width="0.1px" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
86
88
|
<g>
|
|
87
|
-
<path d="M0 80L10 80L10 60L0 60L0 80"/>
|
|
89
|
+
<path fill="black" d="M0 80L10 80L10 60L0 60L0 80"/>
|
|
88
90
|
</g>
|
|
89
91
|
<g>
|
|
90
|
-
<path d="M60 20L70 20L70 0L60 0L60 20"/>
|
|
92
|
+
<path fill="black" d="M60 20L70 20L70 0L60 0L60 20"/>
|
|
91
93
|
</g>
|
|
92
94
|
</svg>
|
|
93
95
|
`
|
|
@@ -95,9 +97,9 @@ const expected3 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
95
97
|
const expected4 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
96
98
|
<!-- Created by JSCAD SVG Serializer -->
|
|
97
99
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">
|
|
98
|
-
<svg width="10mm" height="20mm" viewBox="0 0 10 20" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
100
|
+
<svg width="10mm" height="20mm" viewBox="0 0 10 20" fill="none" fill-rule="evenodd" stroke-width="0.1px" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
99
101
|
<g>
|
|
100
|
-
<path fill
|
|
102
|
+
<path fill="rgb(127.5,127.5,127.5,127.5)" d="M0 20L10 20L10 0L0 0L0 20" id="r2" class="gray-rect"/>
|
|
101
103
|
</g>
|
|
102
104
|
</svg>
|
|
103
105
|
`
|
|
@@ -105,9 +107,9 @@ const expected4 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
105
107
|
const expected5 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
106
108
|
<!-- Created by JSCAD SVG Serializer -->
|
|
107
109
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">
|
|
108
|
-
<svg width="150mm" height="150mm" viewBox="0 0 150 150" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
110
|
+
<svg width="150mm" height="150mm" viewBox="0 0 150 150" fill="none" fill-rule="evenodd" stroke-width="0.1px" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
109
111
|
<g>
|
|
110
|
-
<path fill
|
|
112
|
+
<path fill="rgb(127.5,127.5,127.5,127.5)" d="M0 150L150 150L150 0L115 0L115 75L35 75L35 0L0 0L0 150M90 115L83 115L83 100L67 100L67 115L60 115L60 85L90 85L90 115M73 94L77 94L77 90L73 90L73 94"/>
|
|
111
113
|
</g>
|
|
112
114
|
</svg>
|
|
113
115
|
`
|
package/tests/path2.test.js
CHANGED
|
@@ -26,6 +26,8 @@ test('serialize 2D path (color) objects to svg', (t) => {
|
|
|
26
26
|
// simple open path
|
|
27
27
|
let object1 = primitives.line([[0, 0], [1, 1], [-3, 3]])
|
|
28
28
|
object1 = colors.colorize([0.5, 0.5, 0.5, 0.5], object1)
|
|
29
|
+
object1.id = 'l1'
|
|
30
|
+
object1.class = 'gray-line'
|
|
29
31
|
const observed = serializer.serialize({}, object1)
|
|
30
32
|
t.deepEqual(observed, [expected4])
|
|
31
33
|
})
|
|
@@ -37,7 +39,7 @@ test('serialize 2D path (color) objects to svg', (t) => {
|
|
|
37
39
|
const expected1 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
38
40
|
<!-- Created by JSCAD SVG Serializer -->
|
|
39
41
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">
|
|
40
|
-
<svg width="4mm" height="3mm" viewBox="0 0 4 3" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
42
|
+
<svg width="4mm" height="3mm" viewBox="0 0 4 3" fill="none" fill-rule="evenodd" stroke-width="0.1px" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
41
43
|
<g>
|
|
42
44
|
<path d="M3 3L4 2L0 0"/>
|
|
43
45
|
</g>
|
|
@@ -47,7 +49,7 @@ const expected1 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
47
49
|
const expected3 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
48
50
|
<!-- Created by JSCAD SVG Serializer -->
|
|
49
51
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">
|
|
50
|
-
<svg width="42.3333mm" height="56.4444mm" viewBox="0 0 42.3333 56.4444" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
52
|
+
<svg width="42.3333mm" height="56.4444mm" viewBox="0 0 42.3333 56.4444" fill="none" fill-rule="evenodd" stroke-width="0.1px" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
51
53
|
<g>
|
|
52
54
|
<path d="M21.1667 0L0 56.4444L42.3333 56.4444L21.1667 0"/>
|
|
53
55
|
</g>
|
|
@@ -57,9 +59,9 @@ const expected3 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
57
59
|
const expected4 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
58
60
|
<!-- Created by JSCAD SVG Serializer -->
|
|
59
61
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">
|
|
60
|
-
<svg width="4mm" height="3mm" viewBox="0 0 4 3" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
62
|
+
<svg width="4mm" height="3mm" viewBox="0 0 4 3" fill="none" fill-rule="evenodd" stroke-width="0.1px" version="1.1" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
61
63
|
<g>
|
|
62
|
-
<path stroke="rgb(127.5,127.5,127.5,127.5)"
|
|
64
|
+
<path d="M3 3L4 2L0 0" stroke="rgb(127.5,127.5,127.5,127.5)" id="l1" class="gray-line"/>
|
|
63
65
|
</g>
|
|
64
66
|
</svg>
|
|
65
67
|
`
|