-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg.htm
228 lines (213 loc) · 6.79 KB
/
svg.htm
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<html>
<body>
<script language="javascript"><!--
function getPathToken(path){
if (path.length <= 0){ return ""; }
var token = path.shift();
while ((!token) && (path.length > 0)){
token = path.shift();
}
return token;
}
function processLine(outputBox, x, y){
/////
//
// no "!walls lineto" command, so use curveto instead
processCubicCurve(outputBox, x, y, x, y, x, y);
//
/////
}
function processQuadraticCurve(outputBox, x1, y1, x, y){
/////
//
// no "!walls quadraticto" or similar command, so use curveto instead
processCubicCurve(outputBox, x1, y1, x1, y1, x, y);
//
/////
}
function processCubicCurve(outputBox, x1, y1, x2, y2, x, y){
outputBox.innerHTML += "!walls curveto " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + x + " " + y + "\n";
}
function processCommand(outputBox, path, startPos, curPos, lastCtrl, cmd){
switch(cmd){
case "M":
curPos[0] = 0;
curPos[1] = 0;
// fall through to "m"
case "m":
// move to point
curPos[0] += parseFloat(getPathToken(path));
curPos[1] += parseFloat(getPathToken(path));
outputBox.innerHTML += "!walls moveto " + curPos[0] + " " + curPos[1] + "\n";
break;
case "Z":
case "z":
// close path (line to startPos)
curPos[0] = startPos[0];
curPos[1] = startPos[1];
processLine(outputBox, curPos[0], curPos[1]);
break;
case "L":
curPos[0] = 0;
curPos[1] = 0;
// fall through to "l"
case "l":
// line to point
curPos[0] += parseFloat(getPathToken(path));
curPos[1] += parseFloat(getPathToken(path));
processLine(outputBox, curPos[0], curPos[1]);
break;
case "H":
curPos[0] = 0;
// fall through to "h"
case "h":
// horizontal line to point
curPos[0] += parseFloat(getPathToken(path));
processLine(outputBox, curPos[0], curPos[1]);
break;
case "V":
curPos[1] = 0;
// fall through to "v"
case "v":
// horizontal line to point
curPos[1] += parseFloat(getPathToken(path));
processLine(outputBox, curPos[0], curPos[1]);
break;
case "C":
curPos[0] = 0;
curPos[1] = 0;
// fall through to "c"
case "c":
// cubic curve to point
var firstCtrl = [(curPos[0] + parseFloat(getPathToken(path))), (curPos[1] + parseFloat(getPathToken(path)))];
lastCtrl[0] = curPos[0] + parseFloat(getPathToken(path));
lastCtrl[1] = curPos[1] + parseFloat(getPathToken(path));
curPos[0] += parseFloat(getPathToken(path));
curPos[1] += parseFloat(getPathToken(path));
processCubicCurve(outputBox, firstCtrl[0], firstCtrl[1], lastCtrl[0], lastCtrl[1], curPos[0], curPos[1]);
break;
case "S":
case "s":
// smooth cubic curve to point (firstCtrl is reflection of lastCtrl about curPos)
var firstCtrl = [curPos[0] + curPos[0] - lastCtrl[0], curPos[1] + curPos[1] - lastCtrl[1]];
if (cmd == "S"){
curPos[0] = 0;
curPos[1] = 0;
}
lastCtrl[0] = curPos[0] + parseFloat(getPathToken(path));
lastCtrl[1] = curPos[1] + parseFloat(getPathToken(path));
curPos[0] += parseFloat(getPathToken(path));
curPos[1] += parseFloat(getPathToken(path));
processCubicCurve(outputBox, firstCtrl[0], firstCtrl[1], lastCtrl[0], lastCtrl[1], curPos[0], curPos[1]);
break;
case "Q":
curPos[0] = 0;
curPos[1] = 0;
// fall through to "q"
case "q":
// quadratic curve to point
lastCtrl[0] = curPos[0] + parseFloat(getPathToken(path));
lastCtrl[1] = curPos[1] + parseFloat(getPathToken(path));
curPos[0] += parseFloat(getPathToken(path));
curPos[1] += parseFloat(getPathToken(path));
processQuadraticCurve(outputBox, lastCtrl[0], lastCtrl[1], curPos[0], curPos[1]);
break;
case "T":
case "t":
// smooth quadratic curve to point (firstCtrl is reflection of lastCtrl about curPos)
lastCtrl[0] = curPos[0] + curPos[0] - lastCtrl[0];
lastCtrl[1] = curPos[1] + curPos[1] - lastCtrl[1];
if (cmd == "T"){
curPos[0] = 0;
curPos[1] = 0;
}
curPos[0] += parseFloat(getPathToken(path));
curPos[1] += parseFloat(getPathToken(path));
processQuadraticCurve(outputBox, lastCtrl[0], lastCtrl[1], curPos[0], curPos[1]);
break;
default:
outputBox.innerHTML += "Unrecognized command: " + cmd + "\n";
}
}
function processPath(outputBox, path){
path = path.replace(/[,\s]+/g, " ").replace(/\s*([a-zA-Z])\s*/g, " $1 ").split(" ");
var startPos = [0, 0];
var curPos = [0, 0];
var lastCtrl = [0, 0];
var curCmd = null;
for (var token = getPathToken(path); token; token = getPathToken(path)){
if (/^\d+\.?\d*$/.test(token)){
// number: push token back onto head of path and repeat previous command
path.splice(0, 0, token);
processCommand(outputBox, path, startPos, curPos, lastCtrl, curCmd);
}
else{
// command: save starting position and command, and process command
startPos[0] = curPos[0];
startPos[1] = curPos[1];
curCmd = token;
processCommand(outputBox, path, startPos, curPos, lastCtrl, curCmd);
}
}
}
function processSvg(svg){
var outputBox = document.getElementById("outputBox");
outputBox.innerHTML += "\n!walls begin\n!walls viewbox " + svg.attributes['viewBox'].nodeValue.replace(/^\s*0\s*0\s*(\d+\s*\d+)/, "$1") + "\n";
var paths = svg.getElementsByTagName("path");
for (var i = 0; i < paths.length; i++){
processPath(outputBox, paths[i].attributes['d'].nodeValue);
}
outputBox.innerHTML += "!walls end\n";
}
function handleFileLoad(reader){
if (!reader.result){
document.getElementById("outputBox").innerHTML = "Error loading file: " + reader.error;
return;
}
try{
var dom;
if (window.DOMParser){
var parser = new DOMParser();
dom = parser.parseFromString(reader.result, "text/xml");
}
else{
dom = new ActiveXObject("Microsoft.XMLDOM");
dom.async = false;
dom.loadXML(reader.result);
}
if (dom.parseError){
document.getElementById("outputBox").innerHTML = "Error parsing file:\n" + dom.parseError.reason;
return;
}
if (!dom.documentElement){
document.getElementById("outputBox").innerHTML = "Error parsing file";
}
if (dom.documentElement.nodeName == "parsererror"){
document.getElementById("outputBox").innerHTML = "Error parsing file:\n" + dom.documentElement.childNodes[0].nodeValue;
return;
}
document.getElementById("outputBox").innerHTML = "";
var svgs = dom.getElementsByTagName("svg");
for (var i = 0; i < svgs.length; i++){
processSvg(svgs[i]);
}
}
catch (e){
document.getElementById("outputBox").innerHTML = "Error parsing file:\n" + e.message;
return;
}
}
function handleFileSelect(){
document.getElementById("outputBox").innerHTML = "loading...";
var files = document.getElementById("fileBox").files;
for (var i = 0; i < files.length; i++){
var reader = new FileReader();
reader.onload = (function(r){ return function(){ handleFileLoad(r); }; })(reader);
reader.readAsText(files[i]);
}
}
//--></script>
<input type="file" id="fileBox" onChange="handleFileSelect()">
<div id="outputBox" style="white-space: pre"></div>
</body>
</html>