-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipper.js
166 lines (108 loc) · 3.48 KB
/
clipper.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
'use strict'
var fs = require('fs')
var minimist = require('minimist')
var _ = require('lodash')
var cheerio = require('cheerio')
var parse = require('parse-svg-path')
var Shape = require('clipper-js')
//Getting the filename arg
var argv = 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
let groups = parseSVGGroups(path)
let clippedPaths = clipPaths(groups)
//console.log(clippedPaths.paths)
console.log(wrapSVG(clippedPaths.paths))
// Expects output from parseSVGGroups
function clipPaths(pathGroups){
let clipShape = new Shape(pathGroups['clip'])
let clippedShape = new Shape(pathGroups['subject']).difference(clipShape)
return clippedShape
}
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(pointPath(element))
})
strOutput += '</svg>'
return strOutput
}
// Format the path array that parse() returns for use with clipper-js
function formatPathArr(array){
let pathArr = []
_.each(array,(item) => {
if(item[0] == 'M' || item[0] == 'L'){
pathArr.push({X: item[1], Y: item[2]})
}
})
return pathArr
}
//Takes in a group, returns an array of paths
function parseGroupPaths(group){
let $ = cheerio.load(group)
var paths = _.reduce($('path'), function (items, path) {
let pathArr = parse($(path).attr('d'))
let formattedArr = formatPathArr(pathArr)
items.push(formatPathArr(pathArr))
return items
}, [])
return paths
}
//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 parseSVGGroups(filepath){
let $ = cheerio.load(fs.readFileSync(path))
var groups = _.reduce($('g'), function (items, group) {
//items.push(group)
let groupId = $(group).attr('id')
//items.push({ id: groupId ,paths : parseGroupPaths(group)})
items[groupId] = parseGroupPaths(group)
return items
}, [])
return groups
}
//Composes an SVG path from coordinates
function pointPath(pathPointsArr){
// console.log('pathArr')
// console.log(pathPointsArr)
// console.log('first elem')
// console.log(pathPointsArr[0])
// process.exit()
var pathDataString = 'M' + pathPointsArr[0].X + ',' + pathPointsArr[0].Y //+ ' '
_(pathPointsArr.slice(1)).forEach(function(element){
pathDataString += ' L' + element.X + ',' + element.Y //+ ' '
})
pathDataString +='Z'
return pathDataString
}
//http://jsclipper.sourceforge.net/6.1.3.1/index.html?p=sources_as_text/starter_boolean.txt
function paths2string (paths, scale) {
var svgpath = "", i, j;
if (!scale) scale = 1;
for(i = 0; i < paths.length; i++) {
for(j = 0; j < paths[i].length; j++){
if (!j) svgpath += "M";
else svgpath += "L";
svgpath += (paths[i][j].X / scale) + ", " + (paths[i][j].Y / scale);
}
svgpath += "Z";
}
if (svgpath=="") svgpath = "M0,0";
return svgpath;
}