-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
171 lines (110 loc) · 3.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var shapes = require('svg-path-shapes')
var normalize = require('normalize-svg-path')
var parse = require('parse-svg-path')
var _ = require('lodash')
var simplify = require('simplify-js')
var cheerio = require('cheerio')
var minimist = require('minimist')
var abs = require('abs-svg-path')
var thr = require('throw')
var fs = require('fs');
//Getting the filename arg
var argv = minimist(process.argv.slice(2))
var argv = require('minimist')(process.argv.slice(2))
var args = argv._
var opts = _.omit(argv, '_')
var fileName = args[0];
if(fileName == '' || fileName == undefined) thr('Enter a relative file path')
var bezierOutput = (opts.b ? true : false)
//Hardcoded filepath, to be replaced with command line arguments
var path = __dirname + '/' + fileName
var svgPaths = parseSVGPaths(path)
var seperatedPaths = seperatePaths(svgPaths)
console.log(wrapSVG(seperatedPaths))
//Takes in multi-shape path(s) and outputs an array of paths
function seperatePaths(paths){
var pathElements = [];
_(paths).forEach(function(layer){
_(shapes(layer)).forEach(function(shape){
var pathPoints = _.reduce(parse(shape), function (items, points) {
//if(points[0] == 'M') items.push({ x: values[0], y: values[1]})
if(points[0].toUpperCase() == 'M'){
//console.log('m path')
items.push('M' + points.slice(1))
}
else if(points[0].toUpperCase() == 'C'){
//console.log('c path')
items.push('C' + points.slice(1))
}
else if(points[0].toUpperCase() == 'L'){
//console.log('c path')
items.push('L' + points.slice(1))
}
else if(points[0].toUpperCase() == 'Z') items.push(['Z'])
else {
console.log('Unsupported SVG path token')
console.log(points)
process.exit()
}
return items
}, [])
pathElements.push(_.join(pathPoints,' '))
}) // end foreach multipath shapes
}) // end foreach path
return pathElements
}
//Wraps the pathData array of x,y coords in a line or bezier <path> element
function wrapPath(pathData){
var linePathString = pointPath(pathData)
//console.log(pathData);
//console.log(pathPointsToBezier(pathDataString))
var pathDataString = (bezierOutput ? pathStringToBezier(linePathString) : linePathString)
return '<path d="' + pathDataString + '" stroke="#000000" fill="none"/>'
}
function simpleWrapPath(pathString){
return '<path d="' + pathString + '" stroke="#000000" fill="none"/>'
}
function wrapSVG(paths){
var strOutput = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
strOutput += '<svg id="workspace" xmlns="http://www.w3.org/2000/svg" version="1.1">'
_(paths).forEach(function(element){
strOutput += simpleWrapPath(element)
})
strOutput += '</svg>'
return strOutput
}
//Takes in an SVG file, returns an array of paths elements
function parseSVGPaths(filepath){
$ = cheerio.load(fs.readFileSync(path));
var paths = _.reduce($('path'), function (items, path) {
items.push($(path).attr('d'))
return items
}, [])
return paths
}
function pointPath(pathPointsArr){
console.log(pathPointsArr)
process.exit()
var pathDataString = 'M ' + pathPointsArr[0].x + ' ' + pathPointsArr[0].y + ' '
_(pathPointsArr.slice(1)).forEach(function(element){
console.log('element')
console.log(element)
pathDataString += ' L ' + element.x + ' ' + element.y + ' '
})
console.log(pathDataString)
console.log('debugg')
process.exit()
return pathDataString
}
function pathStringToBezier(pathPointString){
var pathPieces = _.without(_.split(pathPointString,' '),'')
var pathChunks = abs(_.chunk(pathPieces,3))
var bezierPathArr = normalize(pathChunks)
var bezierPathString = ''
_(bezierPathArr).forEach(function(element){
bezierPathString += ' ' + _.join(element,' ')
});
//var bezierPathString = _.join([bezierPathArr],' ')
//return _.replace(bezierPathString,',',' ')
return bezierPathString
}