Skip to content
This repository was archived by the owner on Jul 24, 2022. It is now read-only.

Commit 28570a9

Browse files
z3devkaosat-dev
authored andcommitted
fix(svg-serializer): return array instead of single item (#60)
* Corrected return of serialize() to array Use ES6 string templates Slight reorg * Updated expected results in test to reflect changes to serialize()
1 parent 360cf8b commit 28570a9

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

packages/svg-serializer/index.js

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,9 @@
1-
// import { CSG } from '@jscad/csg'
21
const {CSG} = require('@jscad/csg')
32
const stringify = require('onml/lib/stringify')
43

54
const mimeType = 'image/svg+xml'
65

7-
function dPath (path, xoffset, yoffset) {
8-
var pointindex
9-
var str = ''
10-
var numpointsClosed = path.points.length + (path.closed ? 1 : 0)
11-
for (pointindex = 0; pointindex < numpointsClosed; pointindex++) {
12-
var pointindexwrapped = pointindex
13-
if (pointindexwrapped >= path.points.length) pointindexwrapped -= path.points.length
14-
var point = path.points[pointindexwrapped]
15-
if (pointindex > 0) {
16-
str += 'L' + (point.x + xoffset) + ' ' + (point.y + yoffset)
17-
} else {
18-
str += 'M' + (point.x + xoffset) + ' ' + (point.y + yoffset)
19-
}
20-
}
21-
return str
22-
}
23-
24-
function PathsToSvg (paths, bounds, options) {
25-
// calculate offsets in order to create paths orientated from the 0,0 axis
26-
var xoffset = 0 - bounds[0].x
27-
var yoffset = 0 - bounds[0].y
28-
29-
return paths.reduce(function (res, path, i) {
30-
options && options.statusCallback && options.statusCallback({progress: 100 * i / paths.length})
31-
return res.concat([['path', {d: dPath(path, xoffset, yoffset)}]])
32-
}, ['g'])
33-
}
34-
35-
function serialize (cagObject, options) {
6+
const serialize = function (cagObject, options) {
367
options && options.statusCallback && options.statusCallback({progress: 0})
378
var decimals = 1000
389

@@ -58,13 +29,41 @@ function serialize (cagObject, options) {
5829
PathsToSvg(paths, bounds)
5930
]
6031

61-
var svg = '<?xml version="1.0" encoding="UTF-8"?>\n'
62-
svg += '<!-- Generated by OpenJSCAD.org -->\n'
63-
svg += '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">\n'
64-
svg += stringify(body)
32+
var svg = `<?xml version="1.0" encoding="UTF-8"?>
33+
<!-- Generated by OpenJSCAD.org -->
34+
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">
35+
${stringify(body)}`
6536

6637
options && options.statusCallback && options.statusCallback({progress: 100})
67-
return svg
38+
return [svg]
39+
}
40+
41+
const PathsToSvg = function (paths, bounds, options) {
42+
// calculate offsets in order to create paths orientated from the 0,0 axis
43+
var xoffset = 0 - bounds[0].x
44+
var yoffset = 0 - bounds[0].y
45+
46+
return paths.reduce(function (res, path, i) {
47+
options && options.statusCallback && options.statusCallback({progress: 100 * i / paths.length})
48+
return res.concat([['path', {d: dPath(path, xoffset, yoffset)}]])
49+
}, ['g'])
50+
}
51+
52+
const dPath = function (path, xoffset, yoffset) {
53+
var pointindex
54+
var str = ''
55+
var numpointsClosed = path.points.length + (path.closed ? 1 : 0)
56+
for (pointindex = 0; pointindex < numpointsClosed; pointindex++) {
57+
var pointindexwrapped = pointindex
58+
if (pointindexwrapped >= path.points.length) pointindexwrapped -= path.points.length
59+
var point = path.points[pointindexwrapped]
60+
if (pointindex > 0) {
61+
str += `L${(point.x + xoffset)} ${(point.y + yoffset)}`
62+
} else {
63+
str += `M${(point.x + xoffset)} ${(point.y + yoffset)}`
64+
}
65+
}
66+
return str
6867
}
6968

7069
module.exports = {

packages/svg-serializer/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ test('serialize ag/csg objects to svg (path: simple)', function (t) {
2727
`
2828

2929
const observed = serializer.serialize(source(), undefined, {output: 'jscad', addMetaData: false})
30-
t.deepEqual(expected, observed)
30+
t.deepEqual([expected], observed)
3131
})

0 commit comments

Comments
 (0)