-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathapp.js
More file actions
289 lines (253 loc) · 7.93 KB
/
Copy pathapp.js
File metadata and controls
289 lines (253 loc) · 7.93 KB
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
"use strict";
var matrix;
var $table;
var rowMajor = false;
var msbendian = false;
var selectedFormat = 'c';
$(function() {
var savedWidth = parseInt(localStorage.getItem('dm_width'));
if (isNaN(savedWidth) || savedWidth <= 0) savedWidth = 16;
var savedHeight = parseInt(localStorage.getItem('dm_height'));
if (isNaN(savedHeight) || savedHeight <= 0) savedHeight = 16;
rowMajor = localStorage.getItem('dm_rowMajor') === '1';
msbendian = localStorage.getItem('dm_msbendian') === '1';
var savedFormat = localStorage.getItem('dm_format');
if (savedFormat === 'c' || savedFormat === 'bin' || savedFormat === 'ascii') selectedFormat = savedFormat;
matrix = createArray(savedHeight, savedWidth);
updateTable();
initOptions();
updateCode();
});
function updateTable() {
var width = matrix[0].length;
var height = matrix.length;
$('#_grid').html('');
$('#_grid').append(populateTable(null, height, width, ""));
// events
$table.on("mousedown", "td", function(e){ toggle.call(this, e); updateCode(); });
$table.on("mouseenter", "td", function(e){ toggle.call(this, e); if (e.buttons) updateCode(); });
$table.on("dragstart", function() { return false; });
}
function initOptions() {
$('#clearButton').click(function() { matrix = createArray(matrix.length,matrix[0].length); updateTable(); updateSummary(); updateCode(); });
$('#copyButton').click(function() {
var text = $('#_output').text();
if (!text) return;
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(text).catch(function(){});
return;
}
var ta = document.createElement('textarea');
ta.value = text;
ta.style.position = 'fixed';
ta.style.left = '-9999px';
document.body.appendChild(ta);
ta.focus();
ta.select();
try { document.execCommand('copy'); } catch (e) {}
document.body.removeChild(ta);
});
$('#widthDropDiv li a').click(function () {
var width = parseInt($(this).html());
var height = matrix.length;
matrix = createArray(height, width);
updateTable();
updateSummary();
updateCode();
try { localStorage.setItem('dm_width', width); } catch (e) {}
});
$('#heightDropDiv li a').click(function () {
var width = matrix[0].length;
var height = parseInt($(this).html());
matrix = createArray(height, width);
updateTable();
updateSummary();
updateCode();
try { localStorage.setItem('dm_height', height); } catch (e) {}
});
$('#byteDropDiv li a').click(function () {
var selection = $(this).html();
rowMajor = selection.startsWith("Row");
updateSummary();
updateCode();
try { localStorage.setItem('dm_rowMajor', rowMajor ? '1' : '0'); } catch (e) {}
});
$('#endianDropDiv li a').click(function () {
var selection = $(this).html();
msbendian = selection.startsWith("Big");
updateSummary();
updateCode();
try { localStorage.setItem('dm_msbendian', msbendian ? '1' : '0'); } catch (e) {}
});
// Output format buttons
$('.format-btn').click(function () {
selectedFormat = $(this).data('format');
$('.format-btn').removeClass('active');
$(this).addClass('active');
try { localStorage.setItem('dm_format', selectedFormat); } catch (e) {}
updateCode();
});
// Initialize active state from saved format
$('.format-btn').removeClass('active');
$('.format-btn[data-format="' + selectedFormat + '"]').addClass('active');
updateSummary();
}
function updateSummary() {
var width = matrix[0].length;
var height = matrix.length;
var summary = width + "px by " + height + "px, ";
if (rowMajor) summary += "row major, ";
else summary += "column major, ";
if (msbendian) summary += "big endian.";
else summary += "little endian.";
$('#_summary').html(summary);
}
function updateCode() {
$('#_output').show();
$('#outputPanel').show();
var output;
if (selectedFormat === 'ascii') {
output = generateAsciiArt();
$('#_output').removeClass('lang-c prettyprinted');
$('#_output').text(output);
return;
}
var bytes = buildBytes();
if (selectedFormat === 'bin') {
output = "static const uint8_t data[] =\n{\n" + formatBinary(bytes) + "\n};";
} else {
output = "static const uint8_t data[] =\n{\n" + formatHex(bytes) + "\n};";
}
$('#_output').addClass('lang-c').removeClass('prettyprinted');
// Prefer generating highlighted HTML directly for reliable live updates
if (window.PR && typeof window.PR.prettyPrintOne === 'function') {
$('#_output').html(window.PR.prettyPrintOne(output, 'c'));
} else if (typeof window.prettyPrintOne === 'function') {
$('#_output').html(window.prettyPrintOne(output, 'c'));
} else {
// Fallback to plain text and a best-effort prettyPrint scan
$('#_output').text(output);
var el = document.getElementById('_output');
if (window.PR && typeof window.PR.prettyPrint === 'function') {
window.PR.prettyPrint(null, el);
} else if (typeof window.prettyPrint === 'function') {
window.prettyPrint();
}
}
}
function buildBytes() {
var width = matrix[0].length;
var height = matrix.length;
var buffer = new Array(width * height);
var bytes = new Array((width * height) / 8);
// Column Major
var temp;
for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++) {
temp = matrix[y][x];
if (!temp) temp = 0;
// Row Major or Column Major?
if (!rowMajor) {
var page = Math.floor(y / 8);
var bit = y % 8;
buffer[(page * width + x) * 8 + bit] = temp;
}
else {
buffer[y * width + x] = temp;
}
}
}
// Read buffer 8-bits at a time
// and turn it into bytes
for (var i = 0; i < buffer.length; i+=8) {
var newByte = 0;
for (var j = 0; j < 8; j++) {
if (buffer[i+j]) {
if (msbendian) {
newByte |= 1 << (7-j);
}
else {
newByte |= 1 << j;
}
}
}
bytes[i / 8] = newByte;
}
return bytes;
}
function formatHex(bytes) {
var hexStrings = bytes.map(function (x) {
var hx = x.toString(16);
if (hx.length < 2) hx = '0' + hx;
return '0x' + hx;
});
var perLine = 12;
var lines = [];
for (var k = 0; k < hexStrings.length; k += perLine) {
lines.push(' ' + hexStrings.slice(k, k + perLine).join(', '));
}
return lines.join(',\n');
}
function formatBinary(bytes) {
var binStrings = bytes.map(function (x) {
var b = x.toString(2);
if (b.length < 8) b = Array(9 - b.length).join('0') + b;
return '0b' + b;
});
var perLine = 12;
var lines = [];
for (var k = 0; k < binStrings.length; k += perLine) {
lines.push(' ' + binStrings.slice(k, k + perLine).join(', '));
}
return lines.join(',\n');
}
function generateAsciiArt() {
var width = matrix[0].length;
var height = matrix.length;
var lines = [];
for (var y = 0; y < height; y++) {
var row = '';
for (var x = 0; x < width; x++) {
row += matrix[y][x] ? '#' : '.';
}
lines.push(row);
}
return lines.join('\n');
}
function toggle(e) {
var x = $(this).data('i');
var y = $(this).data('j');
if (e.buttons == 1 && !e.ctrlKey) {
matrix[x][y] = 1;
$(this).addClass('on');
}
else if (e.buttons == 2 || (e.buttons == 1 && e.ctrlKey)) {
matrix[x][y] = 0;
$(this).removeClass('on');
}
return false;
}
function populateTable(table, rows, cells, content) {
if (!table) table = document.createElement('table');
for (var i = 0; i < rows; ++i) {
var row = document.createElement('tr');
for (var j = 0; j < cells; ++j) {
row.appendChild(document.createElement('td'));
$(row.cells[j]).data('i', i);
$(row.cells[j]).data('j', j);
}
table.appendChild(row);
}
$table = $(table);
return table;
}
// (height, width)
function createArray(length) {
var arr = new Array(length || 0),
i = length;
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
while(i--) arr[length-1 - i] = createArray.apply(this, args);
}
return arr;
}