-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
350 lines (299 loc) · 8.87 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* Parser for icon shapes for Röntgen project.
*
* @author Sergey Vartanov
* @since 28 August 2022
* @see https://github.com/enzet/iconscript
*/
var scale = 4.1;
// Icons are 14 × 14 pixels, and this is size of icon with 1 pixel margin.
var size = 20.0;
// Number of rows (icons in a column).
var rows = 100;
// Number of columns (icons in a row).
var columns = Math.floor(800 / scale / size);
var shift = new Point(2.5 * scale, 2.5 * scale);
var current = new Point(0, 0);
var width = 1.0;
var uniting = true;
var sketchOpacity = 0.2;
var sketchColor = new Color(0, 0, 0);
var finalOpacity = 1;
var finalColor = new Color(0, 0, 0);
/**
* Convert position in icon shape coordinates to shifted and scaled coordinates
* of the canvas.
*
* @param {array} position coordinates to convert
* @returns point on the canvas
*/
function toCoordinates(position) {
if (position[0] === "+") {
position = position.slice(1).split(",");
p = new Point([Number(position[0]), Number(position[1])]);
current = current + p;
} else {
position = position.split(",");
p = new Point([Number(position[0]), Number(position[1])]);
current = p;
}
return current * scale + shift;
}
/**
* Add point represented as a circle.
*
* @param {Point} position center position of a circle
* @param {Number} radius circle radius
*/
function addPoint(position, radius) {
circle = new Path.Circle({
center: position,
radius: scale * width * 0.5 * radius,
fillColor: sketchColor,
opacity: sketchOpacity
});
combine(circle);
}
/**
* Add line represented as a rectangle between two circles.
*
* @param {array} from starting point
* @param {array} to ending point
*/
function addLine(from, to) {
var path = new Path();
path.strokeColor = sketchColor;
path.strokeWidth = scale;
path.opacity = sketchOpacity;
path.add(from, to);
var strokePath = PaperOffset.offsetStroke(
path, width * scale / 2, { cap: 'butt' }
);
strokePath.opacity = sketchOpacity;
combine(strokePath);
}
function arcPoint(center, p1, r) {
return center + new Point(Math.cos(p1), -Math.sin(p1)) * r * scale;
}
/**
* Draw the arc (part of the circle).
*
* @param {Point} center center point of the circle that defines the arc
* @param {Number} r radius of the circle
* @param {Number} p1 angle of arc start in radians
* @param {Number} p2 angle of arc end in radians
*/
function addArc(center, r, p1, p2) {
var h = (p1 + p2) / 2;
var a1 = arcPoint(center, p1, r);
var a2 = arcPoint(center, h, r);
var a3 = arcPoint(center, p2, r);
addPoint(a1, 1);
addPoint(a3, 1);
var arc = new Path.Arc({
from: a1,
through: a2,
to: a3,
strokeColor: sketchColor,
strokeWidth: scale,
opacity: sketchOpacity
});
var strokePath = PaperOffset.offsetStroke(arc, scale / 2, { cap: 'butt' });
strokePath.opacity = sketchOpacity;
combine(strokePath);
}
/**
* Draw rectangle by two points.
*
* @param {Point} from top left point
* @param {Point} to bottom right point
*/
function addRectangle(from, to) {
p1 = new Point(from.x, to.y);
p2 = new Point(to.x, from.y);
addPoint(from, 1);
addPoint(to, 1);
addPoint(p1, 1);
addPoint(p2, 1);
addLine(from, p1);
addLine(p1, to);
addLine(to, p2);
addLine(p2, from);
combine(new Path.Rectangle(from, to));
}
var shape = null;
var fill = null;
var filled = false;
/**
* Combine (unite or subtract) the object with the currently constructed shape.
*
* @param {Path} object path-like object
*/
function combine(object) {
if (!shape) {
shape = new Path({fillColor: "blue", insert: false})
}
if (uniting) {
shape = shape.unite(object, insert=false);
} else {
shape = shape.subtract(object, insert=false);
}
}
/**
* Combine (unite or subtract) the fill with the currently constructed shape.
*/
function combineFill() {
if (fill) {
if (uniting) {
shape = shape.unite(fill);
} else {
shape = shape.subtract(fill);
}
fill = null;
}
}
/**
* Parse shape description file line by line.
*/
function parse() {
project.activeLayer.removeChildren();
view.draw();
var current = new Path.Circle([0, 0], 10);
shift = new Point(2.5 * scale, 2.5 * scale);
addGrid();
var lines = area.value.split("\n");
var variables = {};
var lexemes = [];
var i;
var j;
for (i = 0; i < lines.length; i++) {
parts = lines[i].trim().split(" ");
if (parts[1] === "=") {
variables[parts[0]] = []; // parts.slice(2);
for (j = 2; j < parts.length; j++) {
part = parts[j];
if (part[0] === "@") {
variables[parts[0]] = variables[parts[0]]
.concat(variables[part.slice(1)]);
} else {
variables[parts[0]].push(part);
}
}
} else {
for (j = 0; j < parts.length; j++) {
part = parts[j];
if (part[0] === "@") {
lexemes = lexemes.concat(variables[part.slice(1)]);
} else {
lexemes.push(part);
}
}
}
}
var mode = null;
for (i = 0; i < lexemes.length; i++) {
var lexeme = lexemes[i];
var center;
var radius;
if (lexeme === "}" && shape) {
combineFill();
fakeShape = new Path({insert: true});
fakeShape = fakeShape.unite(shape);
fakeShape.translate([0, scale * size])
fakeShape.opacity = finalOpacity;
fakeShape.fillColor = finalColor;
shift += new Point(scale * size, 0);
if (shift.x > scale * columns * size) {
shift += new Point(0, scale * size * 2);
shift.x = 2.5 * scale;
}
shape.selected = true;
// console.log(shape.exportSVG());
shape = null;
} else if (lexeme === "l" || lexeme === "lf") {
combineFill();
filled = (lexeme === "lf");
fill = new Path();
fill.fillColor = sketchColor;
fill.opacity = sketchOpacity;
var last = null;
mode = "line";
} else if (mode === "width") {
width = Number(lexeme);
mode = null;
} else if (lexeme === "p") {
current = toCoordinates(lexemes[i + 1]);
i++;
} else if (lexeme === "r") {
combineFill();
uniting = false;
} else if (lexeme === "a") {
combineFill();
uniting = true;
} else if (lexeme === "w") {
width = Number(lexemes[i + 1]);
i++;
} else if (lexeme === "c") {
center = toCoordinates(lexemes[i + 1]);
radius = Number(lexemes[i + 2]);
addPoint(center, radius);
i += 2;
} else if (lexeme === "ar") {
center = toCoordinates(lexemes[i + 1]);
radius = Number(lexemes[i + 2]);
p1 = Number(lexemes[i + 3]);
p2 = Number(lexemes[i + 4]);
addArc(center, radius, p1, p2);
i += 4;
} else if (lexeme === "s") {
point_1 = toCoordinates(lexemes[i + 1]);
point_2 = toCoordinates(lexemes[i + 2]);
addRectangle(point_1, point_2);
i += 2;
} else if (lexeme.includes(",")) {
coordinates = toCoordinates(lexeme);
if (mode === "line") {
addPoint(coordinates, 1);
if (filled) {
fill.add(coordinates);
}
if (last) {
addLine(last, coordinates);
}
last = coordinates;
}
}
}
}
/**
* Draw grid part from (x1, y1) to (x2, y2).
*/
function addGridLine(x1, y1, x2, y2, color, width, opacity) {
var path = new Path();
path.strokeColor = color;
path.strokeWidth = width;
path.opacity = opacity;
path.add(new Point(x1, y1) * scale, new Point(x2, y2) * scale);
}
/**
* Draw rectangular grid for parsed icons.
*/
function addGrid() {
var i
for (i = 0; i < size * rows; i += size / 2) {
addGridLine(0, i, size * columns, i, 0.2, 0.2);
}
for (i = 0; i < size * columns; i += size / 2) {
addGridLine(i, 0, i, size * rows, 0.2, 0.2);
}
var gray = new Color(1, 1, 1);
for (i = 0; i <= size * rows; i += size) {
addGridLine(0, i, size * columns, i, gray, 6 * scale, 1);
}
for (i = 0; i <= size * columns; i += size) {
addGridLine(i, 0, i, size * rows, gray, 6 * scale, 1);
}
}
var area = document.getElementById("code");
area.addEventListener("input", parse, false);
parse();