-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
445 lines (400 loc) · 9.65 KB
/
script.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
function preload() {
let fontsToLoad = {
Wingdings: {
fileName: "Wingdings-Repaired.ttf",
keyboardLayouts: {
abc: keyboardLayouts.abc,
qwerty: keyboardLayouts.qwerty
}
},
AlienText: {
fileName: "AlientextV2-Regular-fixed.ttf",
keyboardLayouts: {
abc: keyboardLayouts.abc,
qwerty: keyboardLayouts.qwerty
}
},
BirbText: {
fileName: "TenretniOlleh.ttf",
keyboardLayouts: {
abc: keyboardLayouts.abc,
qwerty: keyboardLayouts.qwerty
}
},
OldBirbText: {
fileName: "Birbtext20-Regular.ttf",
keyboardLayouts: {
abc: keyboardLayouts.abc,
qwerty: keyboardLayouts.qwerty
}
}
};
for (let i in fontsToLoad) {
fonts[i] = {
font: loadFont('fonts/' + fontsToLoad[i].fileName),
keyboardLayouts: fontsToLoad[i].keyboardLayouts,
};
}
}
function charInFont(font, chr) {
if (typeof(font.font) !== 'string') {
let unicode = unchar(chr);
let glyphs = font.font.font.glyphs.glyphs;
for (let i in glyphs) {
if (glyphs[i].unicode === unicode) {
return true;
}
}
} else {
return true;
}
return false;
}
function updateSelectOptions() {
let bFElem = document.getElementById("baseFont");
let tFElem = document.getElementById("targetFont");
let tempBFV = bFElem.value;
let tempTFV = tFElem.value;
var options = "";
for (i in fonts) {
options += '<option value\"' + i + '\">' + i + '</option>'
}
bFElem.innerHTML = options;
tFElem.innerHTML = options;
bFElem.value = tempBFV;
tFElem.value = tempTFV;
}
function updateBaseFont() {
baseFont = fonts[document.getElementById("baseFont").value];
updateBaseFontLayoutOptions();
}
function updateTargetFont() {
targetFont = fonts[document.getElementById("targetFont").value];
updateTargetFontLayoutOptions();
}
function uploadFont() {
let files = document.getElementById("fontUpload").files;
if (files.length > 0) {
let file = files[0];
let ext = file.name.split('.').pop();
if (ext === "ttf" || ext === "otf") {
var reader = new FileReader();
reader.onload = ((evt) => {
loadFont(evt.target.result, ((font) => {
fonts[file.name.split('.')[0]] = {
font: font,
keyboardLayouts: fonts["Arial"].keyboardLayouts
};
updateSelectOptions();
}));
});
reader.readAsDataURL(file);
} else {
console.log("Unsupported format. Please use .ttf or .otf");
}
}
}
function uploadKeyboardLayout() {
let files = document.getElementById("layoutUpload").files;
if (files.length > 0) {
let file = files[0];
var reader = new FileReader();
reader.onload= ((evt) => {
let result = evt.target.result;
if (result.length === 45) {
for (let i in fonts) {
fonts[i].keyboardLayouts[file.name.split('.')[0]] = result;
}
updateBaseFontLayoutOptions();
updateTargetFontLayoutOptions();
} else {
console.log("Custom keyboard layout must be 45 characters long");
}
});
reader.readAsText(file);
}
}
function updateBaseFontLayoutOptions() {
let bFLElem = document.getElementById("baseFontLayout");
let tempBFLV = bFLElem.value;
var layouts = Object.keys(baseFont.keyboardLayouts);
var options = "";
for (i in baseFont.keyboardLayouts) {
options += '<option value\"' + i + '\">' + i + '</option>'
}
bFLElem.innerHTML = options;
if (layouts.indexOf(Object.keys(baseFont.keyboardLayouts).find(key => baseFont.keyboardLayouts[key] === encodeLayout)) === -1) {
encodeLayout = baseFont.keyboardLayouts[Object.keys(baseFont.keyboardLayouts)[0]];
}
}
function updateTargetFontLayoutOptions() {
let tFLElem = document.getElementById("targetFontLayout");
let tempBFLV = tFLElem.value;
var layouts = Object.keys(targetFont.keyboardLayouts);
var options = "";
for (i in targetFont.keyboardLayouts) {
options += '<option value\"' + i + '\">' + i + '</option>'
}
tFLElem.innerHTML = options;
if (layouts.indexOf(Object.keys(targetFont.keyboardLayouts).find(key => targetFont.keyboardLayouts[key] === decodeLayout)) === -1) {
decodeLayout = targetFont.keyboardLayouts[Object.keys(targetFont.keyboardLayouts)[0]];
}
}
function updateBaseFontLayout() {
encodeLayout = baseFont.keyboardLayouts[document.getElementById("baseFontLayout").value];
}
function updateTargetFontLayout() {
decodeLayout = targetFont.keyboardLayouts[document.getElementById("targetFontLayout").value];
}
function setup() {
s = windowWidth*0.9/bW > windowHeight*0.9/bH ? windowHeight*0.9/bH : windowWidth*0.9/bW;
var c = createCanvas(bW*s, bH*s);
c.parent('sketch-holder');
baseFont = fonts.Arial;
targetFont = fonts.BirbText;
updateSelectOptions();
document.getElementById("baseFont").value = "Arial";
document.getElementById("targetFont").value = "BirbText";
updateBaseFontLayoutOptions();
updateTargetFontLayoutOptions();
}
function windowResized() {
s = windowWidth*0.9/bW > windowHeight*0.9/bH ? windowHeight*0.9/bH : windowWidth*0.9/bW;
resizeCanvas(bW*s, bH*s);
}
var mClicked = false;
function mouseReleased() {
mClicked = true;
}
function getCharacterColor(chr) {
let unChr = unchar(chr);
if (unChr >= 48 && unChr <= 57) {
return ([7, 226, 255]);
} else if (
unChr === 42 ||
unChr === 43 ||
unChr === 45 ||
unChr === 47 ||
unChr === 61
) {
return ([249, 246, 29]);
} else if ((unChr >= 97 && unChr <=122) || (unChr >= 65 && unChr <= 90)) {
return ([224, 24, 24]);
} else {
return 255;
}
}
function drawText() {
push();
stroke(255);
fill(0);
rectMode(CENTER);
rect(0, -bH*0.15, bW*0.8, bH*0.5, bW*0.01);
let chrsPerRow = 20;
let spacing = (bW*0.8-bW*0.01*2)/20;
let sX = -spacing*(chrsPerRow/2-0.5);
let sY = -bH*0.15-bH*0.5/2+spacing/2;
textSize(spacing*0.9);
textAlign(CENTER, CENTER);
fill(255);
noStroke();
let txt = chrs + (Math.floor(millis()/750)%2 === 1 ? "_" : "");
for (let i in txt) {
let x = sX+spacing*(i-chrsPerRow*Math.floor(i/chrsPerRow));
let y = sY+spacing*Math.floor(i/chrsPerRow);
let chr = txt[i];
textFont((mode === "Encode" ? (charInFont(targetFont, chr) ? targetFont : baseFont) : baseFont).font);
fill(getCharacterColor(chr));
text(chr, x, y);
}
pop();
}
function keyPressed() {
if (key === "Shift") {
capitalize = true;
} else {
let k = capitalize ? key.toUpperCase() : key.toLowerCase();
if (k.length === 1) {
chrs += k;
} else if (key === "Backspace") {
chrs = chrs.substr(0, chrs.length-1);
} else if (!(
key === "Alt" ||
key === "Control" ||
key === "Meta" ||
key === "Contextmenu" ||
key.match("Arrow")
)) {
console.log("Unkown key: " + key + " typed.");
}
}
}
function keyReleased() {
if (key === "Shift") {
capitalize = false;
}
}
function draw() {
background(0);
push();
translate(width/2, height/2);
scale(s);
// Mode Text
push();
noStroke();
textFont(baseFont.font);
textSize(bH*0.0625);
textAlign(LEFT, TOP);
fill(255);
let margin = bW*0.005;
text("Mode: " + mode, -bW/2+margin, -bH/2+margin);
pop();
// Version Text
push();
noStroke();
textFont(baseFont.font);
textSize(bH*0.0375);
textAlign(RIGHT, TOP);
fill(255);
text("Version: " + version, bW/2-margin, -bH/2+margin);
pop();
// Text
drawText();
//Keyboard
push();
let spacing = bW*0.8/15;
let size = spacing*0.9;
let startingX = -spacing*7;
let startingY = bH*0.2;
rectMode(CENTER);
fill(122, 0, 0);
stroke(255);
fill(0);
for (let i = 0; i < 45; i++) {
let x = startingX+(i-15*Math.floor(i/15))*spacing;
let y = startingY+(spacing*Math.floor(i/15));
rect(x, y, size, size, size*0.1);
push();
let layout = (mode === "Encode" ? encodeLayout : decodeLayout);
let chr = capitalize ? layout[i].toUpperCase() : layout[i].toLowerCase();
fill(getCharacterColor(chr));
textFont((mode === "Encode" ? baseFont : (charInFont(targetFont, chr) ? targetFont : baseFont)).font);
noStroke();
textAlign(CENTER, CENTER);
textSize(size*0.75);
text(chr, x, y+size*0.05);
pop();
let absX = x*s+width/2;
let absY = y*s+height/2;
if (
mouseY >= absY-size/2*s &&
mouseY <= absY+size/2*s &&
mouseX >= absX-size/2*s &&
mouseX <= absX+size/2*s
) {
push();
noStroke();
fill(0, 0, 0, (mouseIsPressed ? 255*0.75 : 255*0.375));
rect(x, y, size, size);
if (mClicked) {
chrs += chr;
}
pop();
}
}
pop();
//Switch Mode
button(
(mode === "Encode" ? "Decode" : "Encode"),
-bW/2+spacing*((1.125+0.75)/2),
startingY,
size*1.75,
size,
size*0.5,
function () {mode = (mode === "Encode" ? "Decode" : "Encode");}
);
//Clear
button(
"Clear",
-bW/2+spacing*((1.125+0.75)/2),
startingY+spacing,
size*1.75,
size,
size*0.5,
function () {chrs = "";}
);
//Bcksp
button(
"Bcksp",
-bW/2+spacing*((1.125+0.75)/2),
startingY+spacing*2,
size*1.75,
size,
size*0.5,
function () {chrs = chrs.substr(0, chrs.length-1);}
);
//Copy
button(
"Copy",
bW/2-spacing*((1.125+0.75)/2),
startingY,
size*1.75,
size,
size*0.5,
function () {prompt("Copy:", chrs);}
)
//Paste
button(
"Paste",
bW/2-spacing*((1.125+0.75)/2),
startingY+spacing,
size*1.75,
size,
size*0.5,
function () {chrs = prompt("Paste:");}
)
//Shift
button(
capitalize ? "SHIFT" : "Shift",
bW/2-spacing*((1.125+0.75)/2),
startingY+spacing*2,
size*1.75,
size,
size*0.5,
function () {capitalize = !capitalize;}
);
pop();
mClicked = false;
}
function button(t, x, y, w, h, tS, f) {
push();
textFont(baseFont.font);
rectMode(CENTER);
fill(122, 0, 0);
stroke(255);
fill(0);
rect(x, y, w, h, h*0.1);
fill(255);
noStroke();
textAlign(CENTER, CENTER);
textSize(tS);
text(t, x, y+tS*0.1);
let absX = x*s+width/2;
let absY = y*s+height/2;
if (
mouseY >= absY-h/2*s &&
mouseY <= absY+h/2*s &&
mouseX >= absX-w/2*s &&
mouseX <= absX+w/2*s
) {
push();
noStroke();
fill(0, 0, 0, (mouseIsPressed ? 255*0.75 : 255*0.375));
rect(x, y, w, h);
if (mClicked) {
f();
}
pop();
}
pop();
}