forked from evanstade/skiafy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
302 lines (272 loc) · 10.5 KB
/
main.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2019 The Skiafy Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
function $(id) {
return document.getElementById(id);
}
function ToCommand(letter) {
switch (letter) {
case 'M': return 'MOVE_TO';
case 'm': return 'R_MOVE_TO';
case 'L': return 'LINE_TO';
case 'l': return 'R_LINE_TO';
case 'H': return 'H_LINE_TO';
case 'h': return 'R_H_LINE_TO';
case 'V': return 'V_LINE_TO';
case 'v': return 'R_V_LINE_TO';
case 'A': return 'ARC_TO';
case 'a': return 'R_ARC_TO';
case 'C': return 'CUBIC_TO';
case 'S': return 'CUBIC_TO_SHORTHAND';
case 'c':
case 's':
return 'R_CUBIC_TO';
case 'Q': return 'QUADRATIC_TO';
case 'T': return 'QUADRATIC_TO_SHORTHAND';
case 'q':
case 't':
return 'R_QUADRATIC_TO';
case 'Z':
case 'z':
return 'CLOSE';
}
return '~UNKNOWN~';
}
function LengthForSvgDirective(letter) {
switch (letter) {
case 'C':
case 'c':
case 's':
return 6;
case 'S':
case 'Q':
case 'q':
case 't':
return 4;
case 'T':
case 'L':
case 'l':
case 'H':
case 'h':
case 'V':
case 'v':
return 2;
case 'A':
case 'a':
return 7;
};
return 999;
}
function RoundToHundredths(x) {
return Math.floor(x * 100 + 0.5) / 100;
}
function HandleNode(svgNode, scaleX, scaleY, translateX, translateY) {
var output = '';
for (var idx = 0; idx < svgNode.children.length; ++idx) {
var svgElement = svgNode.children[idx];
switch (svgElement.tagName) {
// g ---------------------------------------------------------------------
case 'g':
if (svgElement.getAttribute('transform')) {
output += "<g> with a transform not handled\n";
break;
}
return HandleNode(svgElement, scaleX, scaleY, translateX, translateY);
// PATH ------------------------------------------------------------------
case 'path':
var isStrokePath = svgElement.getAttribute('stroke') &&
svgElement.getAttribute('stroke') != 'none';
// If fill is none and doesn't have stroke, this is probably one of those worthless paths
// of the form <path fill="none" d="M0 0h24v24H0z"/>
if (svgElement.getAttribute('fill') == 'none' && !isStrokePath)
break;
var commands = [];
var path = svgElement.getAttribute('d').replace(/,/g, ' ').trim();
if (path.slice(-1).toLowerCase() !== 'z')
path += 'z';
while (path) {
var point = parseFloat(path);
if (isNaN(point)) {
var letter = path[0];
path = path.substr(1);
commands.push({ 'command': letter, 'args': [] });
} else {
var currentCommand = commands[commands.length - 1];
var svgDirective = currentCommand.command;
if (currentCommand.args.length == LengthForSvgDirective(svgDirective)) {
commands.push({ 'command': svgDirective, 'args': [] });
currentCommand = commands[commands.length - 1];
svgDirective = currentCommand.command;
}
var pathNeedsPruning = true;
if (svgDirective.toLowerCase() == 'a' &&
currentCommand.args.length >= 3 &&
currentCommand.args.length <= 4) {
point = parseInt(path[0]);
console.assert(point == 0 || point == 1, "Unexpected arc argument " << point);
path = path.substr(1);
pathNeedsPruning = false;
}
// Insert implicit points for cubic and quadratic curves.
var isQuadraticOrCubic = svgDirective.toLowerCase() == 's' || svgDirective.toLowerCase() == 't';
if (isQuadraticOrCubic && currentCommand.args.length == 0) {
if (svgDirective == 's' || svgDirective == 't') {
var lastCommand = commands[commands.length - 2];
// Make sure relative 's' directives can only match with
// previous cubic commands, and that relative 't' directives can
// only match with previous quadratic commands.
if ((svgDirective == 's' && ToCommand(lastCommand.command).search('CUBIC_TO') >= 0) ||
(svgDirective == 't' && ToCommand(lastCommand.command).search('QUADRATIC_TO') >= 0)) {
// The first control point is assumed to be the reflection of
// the last control point on the previous command relative
// to the current point.
var lgth = lastCommand.args.length;
currentCommand.args.push(RoundToHundredths(lastCommand.args[lgth - 2] - lastCommand.args[lgth - 4]));
currentCommand.args.push(RoundToHundredths(lastCommand.args[lgth - 1] - lastCommand.args[lgth - 3]));
} else {
// If there is no previous command or if the previous command
// was not an C, c, S or s for cubics, or Q, q, T, t for
// quadratics, assume the first control point is coincident with
// the current point.
currentCommand.args.push(0);
currentCommand.args.push(0);
}
}
}
// Whether to apply flipping and translating transforms to the
// argument. Only the last two arguments (out of 7) in an arc
// command are coordinates.
var transformArg = true;
// xAxis is true when the current coordinate refers to the xAxis.
var xAxis = currentCommand.args.length % 2 == 0;
if (svgDirective.toLowerCase() == 'a') {
if (currentCommand.args.length < 5)
transformArg = false;
xAxis = currentCommand.args.length % 2 == 1;
} else if (svgDirective.toLowerCase() == 'v') {
xAxis = false;
}
if (transformArg) {
point *= xAxis ? scaleX : scaleY;
if (svgDirective != svgDirective.toLowerCase())
point += xAxis ? translateX : translateY;
}
point = RoundToHundredths(point);
currentCommand.args.push(point);
if (pathNeedsPruning) {
var dotsSeen = 0;
for (var i = 0; i < path.length; ++i) {
if (i == 0 && path[i] == '-')
continue;
if (!isNaN(parseInt(path[i])))
continue;
if (path[i] == '.' && ++dotsSeen == 1)
continue;
path = path.substr(i);
break;
}
}
}
path = path.trim();
}
if (isStrokePath) {
var strokeWidth = svgElement.getAttribute('stroke-width');
if (!strokeWidth || isNan(strokeWidth))
strokeWidth = 1;
output += 'STROKE, ' + strokeWidth + ',\n';
}
for (command_idx in commands) {
var command = commands[command_idx];
output += ToCommand(command.command) + ', ';
for (i in command.args) {
var point = command.args[i];
output += point;
if (typeof point == 'number' && ((point * 10) % 10 != 0))
output += 'f';
output += ', ';
}
output = output.trim() + '\n';
}
break;
// CIRCLE ----------------------------------------------------------------
case 'circle':
var cx = parseFloat(svgElement.getAttribute('cx'));
cx *= scaleX;
cx += translateX;
var cy = parseFloat(svgElement.getAttribute('cy'));
cy *= scaleY;
cy += translateY;
var rad = parseFloat(svgElement.getAttribute('r'));
output += 'CIRCLE, ' + cx + ', ' + cy + ', ' + rad + ',\n';
break;
// RECT ------------------------------------------------------------------
case 'rect':
var x = parseFloat(svgElement.getAttribute('x')) || 0;
x *= scaleX;
x += translateX;
var y = parseFloat(svgElement.getAttribute('y')) || 0;
y *= scaleY;
y += translateY;
var width = parseFloat(svgElement.getAttribute('width'));
var height = parseFloat(svgElement.getAttribute('height'));
output += 'ROUND_RECT, ' + x + ', ' + y + ', ' + width + ', ' + height +
', ';
var round = svgElement.getAttribute('rx');
if (!round)
round = '0';
output += round + ',\n';
break;
// OVAL ----------------------------------------------------------------
case 'ellipse':
var cx = parseFloat(svgElement.attr('cx')) || 0;
cx *= scaleX;
cx += translateX;
var cy = parseFloat(svgElement.attr('cy')) || 0;
cy *= scaleY;
cy += translateY;
var rx = parseFloat(svgElement.attr('rx')) || 0;
var ry = parseFloat(svgElement.attr('ry')) || 0;
output += 'OVAL, ' + cx + ', ' + cy + ', ' + rx + ', ' + ry + ',\n';
break;
}
}
return output;
}
function ConvertInput() {
var translateX = parseFloat($('transform-x').value);
var translateY = parseFloat($('transform-y').value);
if (isNaN(translateX))
translateX = 0;
if (isNaN(translateY))
translateY = 0;
var scaleX = $('flip-x').checked ? -1 : 1;
var scaleY = $('flip-y').checked ? -1 : 1;
var input = $('user-input').value;
$('svg-anchor').innerHTML = input;
var output = '';
var svgNode = $('svg-anchor').querySelector('svg');
var canvasSize = svgNode.viewBox.baseVal.width;
if (canvasSize == 0)
canvasSize = svgNode.width.baseVal.value;
if (canvasSize != 48)
output += 'CANVAS_DIMENSIONS, ' + canvasSize + ',\n';
output += HandleNode(svgNode, scaleX, scaleY, translateX, translateY);
// Truncate final comma and newline.
$('output-span').textContent = output.slice(0, -2);
}
function init() {
$('go-button').addEventListener('click', ConvertInput);
if (navigator.userAgent.indexOf("WebKit") >= 0)
$('use-webkit').hidden = true;
}
window.onload = init;