-
Notifications
You must be signed in to change notification settings - Fork 12
/
rob_code.html
424 lines (379 loc) · 16.7 KB
/
rob_code.html
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<html>
<head>
<title>RoBCode (SVG)</title>
<script>
var base_colour_array = ["#FF0000", "#FFB300", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#7700D8", "#FF00FF", "#000000", "#000000"] ;
var black_array = ["black", "black", "black", "black", "black", "black", "black", "black", "black", "black"];
function set_table_colour() {
for(var i=0; i<9; i += 1) {
document.getElementById("table1_cell_colour_" + i).value = base_colour_array[i];
}
}
function reverse_colours() {
var tmp;
for(var i=0; i < 4; i += 1) {
tmp = base_colour_array[i];
base_colour_array[i] = base_colour_array[7 - i];
base_colour_array[7 - i] = tmp;
}
set_table_colour();
}
function set_colour(colour_id) {
var index = parseInt(colour_id.id.slice(-1));
base_colour_array[index] = colour_id.value;
return true;
}
function polarToCartesianCoordinates(centerX, centerY, radius, angleInDegrees) {
var angleInRadians = (angleInDegrees-90) * Math.PI / 180.0;
return {
x: centerX + (radius * Math.cos(angleInRadians)),
y: centerY + (radius * Math.sin(angleInRadians))
};
}
function describeSVGArc(center_x, center_y, inner_radius, outer_radius, startAngle, endAngle){
var inner_start = polarToCartesianCoordinates(center_x, center_y, inner_radius, endAngle);
var inner_end = polarToCartesianCoordinates(center_x, center_y, inner_radius, startAngle);
var outer_start = polarToCartesianCoordinates(center_x, center_y, outer_radius, endAngle);
var outer_end = polarToCartesianCoordinates(center_x, center_y, outer_radius, startAngle);
var arcSweep = endAngle - startAngle <= 180 ? "0" : "1";
var d = [
"M", outer_start.x, outer_start.y,
"A", outer_radius, outer_radius, 0, arcSweep, 0, outer_end.x, outer_end.y,
"L", inner_end.x, inner_end.y,
"A", inner_radius, inner_radius, 0, arcSweep, 1, inner_start.x, inner_start.y,
"L", outer_start.x, outer_start.y
].join(" ");
return d;
}
function drawCircleArc(rp, inner_radius, outer_radius, start_angle, end_angle, colour) {
var svg_ = document.getElementById("thesvg");
var newPath = document.createElementNS("http://www.w3.org/2000/svg", 'path');
if(rp.counter_clockwise) {
t = 360 - start_angle;
start_angle = 360 - end_angle;
end_angle = t;
}
newPath.setAttribute("d", describeSVGArc(rp.center_x, rp.center_y, inner_radius, outer_radius, start_angle, end_angle)); //Set path's data
newPath.style.stroke = colour; //Set stroke colour
newPath.style.fill = colour; //Set fill colour
newPath.style.strokeWidth = "0.25"; //Set stroke width
svg_.appendChild(newPath);
}
function drawCircleBit(rp, ring, sectors_in_ring, bit_index, colour) {
var radius = rp.ring_width * ring;
var arc_angle = 360 / (rp.bytes_per_sector * sectors_in_ring * rp.bits_per_byte); //ring * ring_increment
var start_angle = arc_angle * bit_index ;
drawCircleArc(rp, radius, radius + rp.ring_width, start_angle, start_angle+arc_angle, colour)
}
function drawRectBit(center_x, center_y, ring, rect_width, bit_index, colour) {
var svg_ = document.getElementById("thesvg");
var radius = rect_width * ring;
var newRect = document.createElementNS("http://www.w3.org/2000/svg", 'rect');
newRect.setAttribute("x", center_x + rect_width/1.6 * bit_index);
newRect.setAttribute("y", center_y + radius);
newRect.setAttribute("width", rect_width/1.6);
newRect.setAttribute("height", rect_width);
newRect.style.fill = colour; //Set fill colour
newRect.style.stroke = "black"; //Set stroke colour
newRect.style.strokeWidth = "0.25"; //Set stroke width
svg_.appendChild(newRect);
}
function drawCircle(center_x, center_y, radius, stroke_width, colour) {
var svg_ = document.getElementById("thesvg");
var newCircle = document.createElementNS("http://www.w3.org/2000/svg", 'circle');
newCircle.setAttribute("cx", center_x);
newCircle.setAttribute("cy", center_y);
newCircle.setAttribute("r", radius);
if(stroke_width == 0) {
newCircle.style.fill = colour; //Set fill colour
newCircle.style.stroke = colour; //Set stroke colour
newCircle.style.strokeWidth = "0.25"; //Set stroke width
} else {
newCircle.style.fill = "none"; //Set fill colour
newCircle.style.stroke = colour; //Set stroke colour
newCircle.style.strokeWidth = stroke_width; //Set stroke width
}
svg_.appendChild(newCircle);
}
function drawRing(center_x, center_y, outer_radius, stroke_width, colour) {
drawCircle(center_x, center_y, outer_radius, stroke_width, colour);
}
function drawDoubleCircle(center_x, center_y, ring_width, colour) {
drawCircle(center_x, center_y, ring_width/2, 0, colour);
drawRing(center_x, center_y, ring_width * 0.7, ring_width * 0.15, colour);
}
function encode_string_to_bits(the_string) {
var array_of_bytes = [];
for(var i=0; i < the_string.length; i++) {
array_of_bytes[i] = the_string.charCodeAt(i) & 0xFF;
}
return array_of_bytes;
}
function first_sector_for_ring(ring, ring_increment, exponential) { //base = rp.ring_increment for log
if(ring <= 0) return 0;
if(ring_increment == 0) return ring;
ring -= 1; //As we need to calculate the capacity up to this ring.
if(exponential) {
return ring_increment == 1 ? ring : ((1-Math.pow(ring_increment,ring))/(1-ring_increment))*ring_increment + 1;
}
return ring_increment/2.0 * (ring*ring+ring) + 1;
}
function bit_index(sector_in_ring, bytes_per_sector, byte_in_sector, bits_per_byte, bit_in_byte) {
return (sector_in_ring * bytes_per_sector + byte_in_sector ) * bits_per_byte + bit_in_byte;
}
function render_a_byte(the_byte, rp, ring, sectors_in_ring, sector_in_ring, byte_in_sector) {
var parity = rp.which_parity == 'even' ? 0 : 1; //even or odd, ignore none
var mask ;
var lsb;
if(rp.bit_order == 'lsb') {
mask = 1;
lsb = true;
} else {
mask = 0x80;
lsb = false;
}
for(var j=0; j < 8; j++) {
bit_i = bit_index(sector_in_ring, rp.bytes_per_sector, byte_in_sector, rp.bits_per_byte, j);
if((the_byte & mask) == mask) {
parity ^= 1;
if(rp.unrolled) {
drawRectBit(0, 0, ring, rp.ring_width, bit_i, rp.colour_array[j]);
}
else {
drawCircleBit(rp, ring, sectors_in_ring, bit_i, rp.colour_array[j]);
}
} else if(rp.unrolled) {
drawRectBit(0, 0, ring, rp.ring_width, bit_i, "white");
}
if(lsb) the_byte >>>= 1; else mask >>>= 1;
}
if(rp.which_parity != "none") {
if(rp.which_parity == "space") parity = 0;
else if(rp.which_parity == "mark") parity = 1;
bit_i = bit_index(sector_in_ring, rp.bytes_per_sector, byte_in_sector, rp.bits_per_byte, 8);
if(parity == 1 && ring != null) {
if(rp.unrolled) {
drawRectBit(0, 0, ring, rp.ring_width, bit_i, rp.colour_array[8]);
}
else {
drawCircleBit(rp, ring, sectors_in_ring, bit_i, rp.colour_array[8]);
}
} else if(rp.unrolled) {
drawRectBit(0, 0, ring, rp.ring_width, bit_i, "white");
}
}
}
function sectors_in_a_ring(ring, rp) {
if(rp.ring_increment == 0) return 1;
if(ring == 0) return 1;
return rp.exponential ? Math.pow(rp.ring_increment, ring) : (rp.ring_increment * ring);
}
function render_bytes(array_of_bytes, rp) {
var byte_in_sector = 0;
var sector_in_ring = 0;
var ring = rp.start_ring;
var sectors_in_this_ring = sectors_in_a_ring(ring, rp);
for(var i = 0; i < array_of_bytes.length; i++ ) { //each character
var v = array_of_bytes[i] ^ rp.xor_value; //every second bit in byte, to remove runs of 0's
render_a_byte(v, rp, ring, sectors_in_this_ring, sector_in_ring, byte_in_sector);
byte_in_sector += 1;
if(byte_in_sector == rp.bytes_per_sector)
{ /*Move to next ring*/
sector_in_ring += 1;
if(sector_in_ring == sectors_in_this_ring) {
ring += 1;
sectors_in_this_ring = sectors_in_a_ring(ring, rp);
sector_in_ring = 0;
}
byte_in_sector = 0;
}
}
var new_ring = (byte_in_sector == 0 && sector_in_ring == 0);
if(rp.xor_value != 0 && !new_ring) {
while(byte_in_sector < rp.bytes_per_sector) { //fill the last sector
render_a_byte(rp.xor_value, rp, ring, sectors_in_this_ring, sector_in_ring, byte_in_sector);
byte_in_sector += 1
}
sector_in_ring += 1; //move to next sector
while(sector_in_ring < sectors_in_this_ring) { //remainder of sectors in this ring
byte_in_sector = 0;
while(byte_in_sector < rp.bytes_per_sector) {
render_a_byte(rp.xor_value, rp, ring, sectors_in_this_ring, sector_in_ring, byte_in_sector);
byte_in_sector += 1
}
sector_in_ring += 1; //move to next sector
}
}
if(rp.bounding_circle) {
if(new_ring) ring -= 1;
drawRing(rp.center_x, rp.center_y, rp.ring_width * (ring + 1.2), rp.ring_width * 0.2, "black"); //bounding circle
}
}
function render_centre_byte(rp, the_byte) {
var parity = rp.which_parity == 'even' ? 0 : 1; //even or odd, ignore none
var mask ;
var lsb;
if(rp.bit_order == 'lsb') {
mask = 1;
lsb = true;
} else {
mask = 0x80;
lsb = false;
}
for(var j=0; j < 8; j++) {
if((the_byte & mask) == mask) {
parity ^= 1;
drawCircleArc(rp, 0, rp.ring_width, 360/rp.bits_per_byte * j, 360/rp.bits_per_byte * (j+1), rp.colour_array[j]) ;
}
if(lsb) the_byte >>>= 1; else mask >>>= 1;
}
if(parity == 1) {
drawCircleArc(rp, 0, rp.ring_width, 360/rp.bits_per_byte*8, 360, rp.colour_array[8]);
}
}
function parseInt_local(s) {
if(s.substring(0, 2) == "0b") { return parseInt(s.substring(2),2); } //0b doesn't work in my browser.
if(s.substring(0, 2) == "0a") { return s.substring(2,3).charCodeAt(0) & 0xFF; } //hack to indicate ascii
return parseInt(s); //assume int10, 0x or 0
}
function draw_center(rp) {
if(!rp.unrolled) {
switch(the_form.center.value) {
case "double_circle":
drawDoubleCircle(rp.center_x, rp.center_y, rp.ring_width, "black");
break;
case "center_circle":
drawCircle(rp.center_x, rp.center_y, rp.ring_width, 0, "black");
break;
case "ring":
drawRing(rp.center_x, rp.center_y, rp.ring_width * 0.9, rp.ring_width*0.2, "black");
break;
case "center_byte":
render_centre_byte(rp, parseInt_local(the_form.center_byte_text.value));
break;
}
}
}
function encode_text(the_form) {
var _svg = document.getElementById("thesvg");
while (_svg.lastChild) {
_svg.removeChild(_svg.lastChild);
}
var start_ring = parseInt(the_form.start_ring.value);
var ring_increment = parseInt(the_form.step_size.value);
var ring_params = {
center_x: parseInt(the_form.center_x.value),
center_y: parseInt(the_form.center_y.value),
start_ring: start_ring,
bytes_per_sector: parseInt(the_form.bytes_per_sector.value),
ring_increment: ring_increment,
first_ring_offset: first_sector_for_ring(start_ring, ring_increment, the_form.exponential.checked),
ring_width: parseInt(the_form.ring_width.value),
counter_clockwise: the_form.counter_clockwise.checked,
bit_order: the_form.bit_order.value,
which_parity: the_form.parity.value,
bits_per_byte: the_form.parity.value == "none" ? 8 : 9,
xor_value: the_form.xor.checked ? parseInt_local(the_form.xor_text.value) : 0,
exponential: the_form.exponential.checked,
colour_array: the_form.colour.checked ? base_colour_array : black_array,
unrolled: the_form.unroll.checked,
bounding_circle: the_form.bounding_circle.checked && !the_form.unroll.checked
};
draw_center(ring_params);
render_bytes(encode_string_to_bits(the_form.the_text.value), ring_params);
}
function init() {
var the_form = document.getElementById("the_form");
set_table_colour();
encode_text(the_form);
}
</script>
</head>
<body onload="init();">
<h1>Playing with RoBCodes</h1>
<div align="right">See: <a href="https://github.com/rbur004/svg_rob_code">https://github.com/rbur004/svg_rob_code</a></div>
<form id="the_form" action="" onsubmit="encode_text(this);return false;">
<textarea rows="5" cols="80" name="the_text">
PUCK
If we shadows have offended,
Think but this, and all is mended,
That you have but slumber'd here
While these visions did appear.
And this weak and idle theme,
No more yielding but a dream,
Gentles, do not reprehend:
if you pardon, we will mend:
And, as I am an honest Puck,
If we have unearned luck
Now to 'scape the serpent's tongue,
We will make amends ere long;
Else the Puck a liar call;
So, good night unto you all.
Give me your hands, if we be friends,
And Robin shall restore amends.
</textarea>
<br>
<input type="checkbox" id="exponential" onchange="encode_text(this.form);return false;">Exponential
<select name="step_size" onchange="encode_text(this.form);return false;">
<option value="0">0</option>
<option value="1">1</option>
<option value="2" selected>2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select> Step Size (or Base)
start at Ring <input type="text" size="5" name="start_ring" value="1">
<br>
<input type="text" width="16" name="xor_text" value="0xAA">
<input type="checkbox" id="xor" checked="true" onchange="encode_text(this.form);return false;">XOR (0b for binary, 0a for a Character, 0x for Hex)
<br>
Center X <input type="text" size="5" name="center_x" value="250">
Y <input type="text" size="5" name="center_y" value="250">
Ring Width<input type="text" size="3" name="ring_width" value="20">
Bytes per Sector<input type="text" size="5" name="bytes_per_sector" value="1">
<br>
<input type="radio" name="center" value="no_center" checked onchange="encode_text(this.form);return false;"> No Center
<input type="radio" name="center" value="center_circle" onchange="encode_text(this.form);return false;"> Solid
<input type="radio" name="center" value="ring" onchange="encode_text(this.form);return false;"> Ring
<input type="radio" name="center" value="double_circle" onchange="encode_text(this.form);return false;"> Circle with Ring
<input type="radio" name="center" value="center_byte" onchange="encode_text(this.form);return false;"> Center Byte
<input type="text" size="12" name="center_byte_text" value="0xAA">
<br>
<input type="radio" name="bit_order" value="lsb" onchange="encode_text(this.form);return false;"> LSB
<input type="radio" name="bit_order" value="msb" checked onchange="encode_text(this.form);return false;"> MSB
<input type="radio" name="parity" value="even" checked onchange="encode_text(this.form);return false;"> Even Parity
<input type="radio" name="parity" value="odd" onchange="encode_text(this.form);return false;"> Odd Parity
<input type="radio" name="parity" value="none" onchange="encode_text(this.form);return false;"> No Parity
<input type="radio" name="parity" value="mark" onchange="encode_text(this.form);return false;"> Mark
<input type="radio" name="parity" value="space" onchange="encode_text(this.form);return false;"> Space
<br>
<input type="checkbox" id="counter_clockwise" onchange="encode_text(this.form);return false;">Counter Clockwise
<input type="checkbox" id="bounding_circle" checked="true" onchange="encode_text(this.form);return false;">Bounding Circle
<input type="checkbox" id="colour" checked="true" onchange="encode_text(this.form);return false;">Colour
<input type="checkbox" id="unroll" onchange="encode_text(this.form);return false;">Unroll
<br><br>
<table><tr>
<td><input type="color" id="table1_cell_colour_0" value="#ff0000" onchange="set_colour(this);"></td>
<td><input type="color" id="table1_cell_colour_1" value="#ff0000" onchange="set_colour(this);"></td>
<td><input type="color" id="table1_cell_colour_2" value="#ff0000" onchange="set_colour(this);"></td>
<td><input type="color" id="table1_cell_colour_3" value="#ff0000" onchange="set_colour(this);"></td>
<td><input type="color" id="table1_cell_colour_4" value="#ff0000" onchange="set_colour(this);"></td>
<td><input type="color" id="table1_cell_colour_5" value="#ff0000" onchange="set_colour(this);"></td>
<td><input type="color" id="table1_cell_colour_6" value="#ff0000" onchange="set_colour(this);"></td>
<td><input type="color" id="table1_cell_colour_7" value="#ff0000" onchange="set_colour(this);"></td>
<td><input type="color" id="table1_cell_colour_8" value="#ff0000" onchange="set_colour(this);"></td>
<td><input type="button" value="reverse" onClick="reverse_colours();this.blur();encode_text(this.form);return false;"></td>
</tr>
</table>
<br>
<input type="submit" id="draw" name="draw" onClick="this.blur();">
</form>
<div align="left">
<svg id="thesvg" width="100%" height="100%" >
</svg>
</div>
</body>
</head>