1- // import { CSG } from '@jscad/csg'
21const { CSG } = require ( '@jscad/csg' )
32const stringify = require ( 'onml/lib/stringify' )
43
54const 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
7069module . exports = {
0 commit comments