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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506 | #!/usr/bin/env -S vala --pkg sdl2 --pkg json-glib-1.0 --pkg posix sdl-ext.vapi
// valac --pkg sdl2 --pkg json-glib-1.0 --pkg posix sitelen-Mosu.vala sdl-ext.vapi --Xcc=-lm
using SDL;
using SDL.Video;
struct Glyph {
uint16 x;
uint16 y;
uint8 width;
uint8 height;
uint8 baseline;
bool extant;
unichar c;
Glyph(Json.Array data, uint8 baseline, Surface surf, int x, int y, unichar c) {
this.x = (uint16) x;
this.y = (uint16) y;
this.c = c;
this.baseline = baseline;
this.width = 0;
this.height = (uint8) data.get_length();
this.extant = true;
for (int dy = 0; dy < data.get_length(); dy++) {
string row = data.get_string_element(dy);
this.width = uint8.max(this.width, (uint8) row.length);
for (int dx = 0; dx < row.length; ++dx) {
uint8* ptr = surf.pixels;
int rx = x + dx;
int ry = y + dy;
ptr[ry * surf.pitch + rx / 8] |= (uint8) (row[dx] == '#') << (uint8) (rx % 8);
}
}
}
public Rect get_rect() {
return Rect() {
x = x,
y = y,
w = width,
h = height
};
}
}
struct Block {
Glyph[] characters;
}
struct Plane {
Block[] blocks;
}
class Morse {
uint8[] long_beep;
uint8[] short_beep;
Audio.AudioDevice audio_device;
Audio.AudioSpec spec;
// generates earrape
int16[] synthesize_sine(double frequency, double seconds, double amplitude) {
uint buf_length = (uint) (seconds * (double) spec.freq);
int16[] res = new int16[buf_length];
for (uint i = 0; i < buf_length; i++) {
double t = (double) i / (double) spec.freq;
res[i] = (int16) ((double) int16.MAX * Math.sin(2 * Math.PI * frequency * t) * amplitude);
print("res[%u]: %d t: %f\n", i, res[i], t);
}
return res;
}
public Morse() {
spec = Audio.AudioSpec() {
freq = 44100,
format = Audio.AudioFormat.S16,
channels = 1,
samples = 1024,
callback = null
};
uint32 len;
Audio.AudioSpec? x = Audio.load("short.wav", ref spec, out short_beep, out len);
if (x == null)
error("short load: %s", SDL.get_error());
x = Audio.load("long.wav", ref spec, out long_beep, out len);
if (x == null)
error("long load: %s", SDL.get_error());
audio_device = open_audio_device(null, false, ref spec, null, 0);
if (audio_device == 0)
error("%s", SDL.get_error());
}
}
class TextEditor {
private const int MAX_CODEPOINT = 0x10ffff;
private Plane characters[(MAX_CODEPOINT >> 16) + 1];
private int line_height;
private Surface surface;
public Texture texture;
public Texture itexture;
private unowned Renderer renderer;
private StringBuilder[] lines;
private int maxcharh;
private int max_baseline;
private Color fg;
private Color bg;
public Rect bounds { get; set; }
public int scale { get; set; }
public int cursor_line { get; set; default = 0; }
public int cursor_column { get; set; default = 0; }
public Glyph? lookup(unichar codepoint) {
unowned Plane? p = characters[codepoint >> 16];
if (p.blocks == null) return null;
unowned Block? b = p.blocks[codepoint >> 8 & 0xff];
if (b.characters == null) return null;
unowned Glyph? g = b.characters[codepoint & 0xff];
if (!g.extant) return null;
return g;
}
void insert(unichar codepoint, Glyph c) {
Plane* p = &characters[codepoint >> 16];
if (p->blocks == null)
p->blocks = new Block[(MAX_CODEPOINT >> 8 & 0xff) + 1];
Block* b = &p->blocks[codepoint >> 8 & 0xff];
if (b->characters == null)
b->characters = new Glyph[(MAX_CODEPOINT & 0xff) + 1];
b->characters[codepoint & 0xff] = c;
}
public int line_width(string s, bool selected) {
int result = 0;
int index = 0;
unichar codepoint;
bool selected_passed = false;
bool invert;
while (next_char(s, selected, ref index, ref selected_passed, out codepoint, out invert)) {
Glyph? g = lookup(codepoint);
if (g == null) continue;
result += (g.width + 2) * scale;
}
return result;
}
private bool next_char(string s, bool selected, ref int index, ref bool selected_passed, out unichar codepoint, out bool invert) {
int oldindex = index;
bool res = s.get_next_char(ref index, out codepoint);
invert = false;
if (selected && !selected_passed && oldindex == cursor_column) {
selected_passed = true;
invert = true;
if (!res) {
codepoint = ' ';
return true;
}
}
return res;
}
public void render_line(int x, int y, string s, bool selected) {
int index = 0;
unichar codepoint = 0;
bool selected_passed = false;
bool invert;
while (next_char(s, selected, ref index, ref selected_passed, out codepoint, out invert)) {
Glyph? g = lookup(codepoint);
if (g == null) continue;
var outline_rect = Rect() {
x = x,
y = y - g.baseline * scale,
w = (g.width + 2) * scale,
h = (g.height + 2) * scale
};
var target_rect = Rect() {
x = outline_rect.x + scale,
y = outline_rect.y + scale,
w = outline_rect.w - 2 * scale,
h = outline_rect.h - 2 * scale
};
int res;
if (invert) {
renderer.set_draw_color(fg.r, fg.g, fg.b, fg.a);
renderer.fill_rect(outline_rect);
res = renderer.copy(itexture, g.get_rect(), target_rect);
} else {
renderer.set_draw_color(bg.r, bg.g, bg.b, bg.a);
renderer.fill_rect(outline_rect);
res = renderer.copy(texture, g.get_rect(), target_rect);
}
if (res != 0) error("%s", SDL.get_error());
x += (int) outline_rect.w;
}
}
public void render() {
int scaled_line_diff = scale * (line_height + 2);
clear_error();
int e = set_clip_rect(renderer, bounds);
if (e != 0) error("%s", SDL.get_error());
int nlines = int.min((int) bounds.h / scaled_line_diff + 1, lines.length);
int y = bounds.y + (int) bounds.h - scale * maxcharh + scale * max_baseline - 50; // mystery factor
print(@"bounds.y: $(bounds.y) bounds.h: $(bounds.h) maxcharh: $maxcharh max_baseline: $max_baseline ystart: $y\n");
for (int i = lines.length - 1; i >= lines.length - nlines; i--) {
int linew = line_width(lines[i].str, i == cursor_line);
int x;
if (linew < bounds.w)
x = bounds.x;
else
x = bounds.x + (int) bounds.w - linew;
render_line(x, y, lines[i].str, i == cursor_line);
y -= scaled_line_diff;
}
e = set_clip_rect(renderer, null);
if (e != 0) error("%s", SDL.get_error());
}
public void set_colors(Color fg, Color bg) {
print("%d\n", surface.format.palette.colors.length);
// int status = surface.format.palette.set_colors({bg, fg}, 0); compiler bug?
surface.format.palette.colors[0] = bg;
surface.format.palette.colors[1] = fg;
texture = Texture.create_from_surface(renderer, surface);
surface.format.palette.colors[0] = fg;
surface.format.palette.colors[1] = bg;
itexture = Texture.create_from_surface(renderer, surface);
this.fg = fg;
this.bg = bg;
}
public void on_key(Input.Keycode keycode, out string? line) {
line = null;
switch (keycode) {
case Input.Keycode.BACKSPACE:
if (lines.length > 1 && lines[cursor_line].len == 0) {
lines.move(cursor_line+1, cursor_line, lines.length - cursor_line - 1);
lines.resize(lines.length - 1);
--cursor_line;
cursor_column = (int) lines[cursor_line].len;
} else if (cursor_column != 0) {
lines[cursor_line].erase(cursor_column - 1, 1);
--cursor_column;
}
break;
case Input.Keycode.RETURN:
int oldlen = lines.length;
lines.resize(lines.length + 1);
lines.move(cursor_line+1, cursor_line+2, oldlen - (cursor_line + 1));
lines[cursor_line + 1] = new StringBuilder(lines[cursor_line].str[cursor_column:]);
lines[cursor_line].truncate(cursor_column);
line = lines[cursor_line].str;
cursor_line++;
cursor_column = 0;
break;
}
}
public void handle(SDL.Event ev, out string? line) {
line = null;
switch (ev.type) {
case EventType.TEXTINPUT:
print("line_text %s\n", ev.text.text);
lines[cursor_line].insert(cursor_column, ev.text.text);
cursor_column += ev.text.text.length;
break;
case EventType.KEYDOWN:
on_key(ev.key.keysym.sym, out line);
break;
default:
break;
}
}
public TextEditor(Renderer r) throws GLib.Error {
renderer = r;
max_baseline = 0;
lines = { new StringBuilder() };
var p = new Json.Parser();
p.load_from_file("ultlf/ultlf-data/trimmed.json");
Json.Array data = p.get_root().get_array();
p.load_from_file("ultlf/ultlf-data/codepoints.json");
Json.Array codepoints = p.get_root().get_array();
p.load_from_file("ultlf/ultlf-data/trimmed_baselines.json");
Json.Array baselines = p.get_root().get_array();
int maxcharw = 0;
maxcharh = 0;
for (int i = 0; i < data.get_length(); i++) {
Json.Array a = data.get_array_element(i);
maxcharh = int.max(maxcharh, (int)a.get_length());
for (int y = 0; y < a.get_length(); y++) {
string row = a.get_string_element(y);
maxcharw = int.max(maxcharw, row.length);
}
}
print("len: %u, maxcharh: %d\n", codepoints.get_length(), maxcharh);
surface = new Surface.rgb_with_format(
(int)(maxcharw * 32),
(int)(maxcharh * (codepoints.get_length() / 32)),
1,
PixelRAWFormat.INDEX1LSB
);
Posix.memset(surface.pixels, surface.pitch * surface.h, 0);
print("surfw: %d, surfh: %d\n", surface.w, surface.h);
for (int i = 0; i < codepoints.get_length(); i++) {
Glyph c = Glyph(
data.get_array_element(i),
(uint8) baselines.get_int_element(i),
surface,
(i % 32) * maxcharw,
(i / 32) * maxcharh,
(unichar)codepoints.get_int_element(i)
);
insert((unichar) codepoints.get_int_element(i), c);
max_baseline = int.max(max_baseline, c.baseline);
}
line_height = maxcharh + 2;
}
}
public string[] morse_alphabet() {
string[] retval = new string[256];
retval['A'] = ".-";
retval['B'] = "-...";
retval['C'] = "-.-.";
retval['D'] = "-..";
retval['E'] = ".";
retval['F'] = "..-.";
retval['G'] = "--.";
retval['H'] = "....";
retval['I'] = "..";
retval['J'] = ".---";
retval['K'] = "-.-";
retval['L'] = ".-..";
retval['M'] = "--";
retval['N'] = "-.";
retval['O'] = "...";
retval['P'] = ".--.";
retval['Q'] = "--.-";
retval['R'] = ".-.";
retval['S'] = "...";
retval['T'] = ".";
retval['U'] = "..-";
retval['V'] = "...-";
retval['W'] = ".--";
retval['X'] = "-..-";
retval['Y'] = "-.--";
retval['Z'] = "--..";
retval['1'] = ".----";
retval['2'] = "..---";
retval['3'] = "...--";
retval['4'] = "....-";
retval['5'] = ".....";
retval['6'] = "-....";
retval['7'] = "--...";
retval['8'] = "---..";
retval['9'] = "----.";
retval['0'] = "-----";
return retval;
}
class MorseShow {
public unowned Renderer r;
public unowned MyRenderer myR; // just as about I was about to take my shoes
public Rect bound;
public void on(int delay) {
myR.render_normal();
r.set_draw_color(255, 255, 255, 255);
r.fill_rect(bound);
r.present();
SDL.Timer.delay(100 * delay);
}
public void off(int delay) {
myR.render_normal();
r.set_draw_color(0, 0, 0, 255);
r.fill_rect(bound);
r.present();
SDL.Timer.delay(100 * delay);
}
}
class MyRenderer {
private TextEditor tr;
private MorseShow morseShow;
private unowned Renderer renderer;
private unowned Window window;
private string morse_line;
private string[] alphabet;
public MyRenderer(Window w, Renderer r) throws GLib.Error {
alphabet = morse_alphabet();
window = w;
renderer = r;
morseShow = new MorseShow();
morseShow.r = r;
morseShow.myR = this;
tr = new TextEditor(r);
tr.set_colors(
Color() { r = 255, g = 255, b = 255, a = 255 },
Color() { r = 0, g = 0, b = 0, a = 0 }
);
tr.scale = 10;
}
public void on_event(Event ev) {
morse_line = null;
tr.handle(ev, out morse_line);
render();
}
public void render_normal() {
int winw, winh;
window.get_size(out winw, out winh);
renderer.set_draw_color(0, 0, 0, 255);
renderer.clear();
tr.bounds = Rect() { x = 0, y = 0, w = winw, h = winh - 100 };
tr.render();
}
public void render() {
render_normal();
renderer.present();
if (morse_line != null) {
int winw, winh;
window.get_size(out winw, out winh);
morseShow.bound = Rect() { x = 0, y = winh - 100, w = 100, h = 100 };
int index = 0;
unichar codepoint;
while (morse_line.get_next_char(ref index, out codepoint)) {
if (codepoint == ' ') {
morseShow.off(7);
continue;
}
string? a = alphabet[codepoint.toupper()];
if (a == null) continue;
for (int i = 0; i < a.length; i++) {
switch (a[i]) {
case '.':
morseShow.on(1);
break;
case '-':
morseShow.on(3);
break;
}
morseShow.off(3);
}
}
}
}
}
void main() {
SDL.init();
var win = new Window("sitelen Mosu", 0, 0, 400, 400, WindowFlags.RESIZABLE);
GL.set_attribute(GL.Attributes.CONTEXT_MAJOR_VERSION, 3);
GL.set_attribute(GL.Attributes.CONTEXT_MINOR_VERSION, 2);
GL.set_attribute(GL.Attributes.CONTEXT_PROFILE_MASK, GL.ProfileType.CORE);
var renderer = Renderer.create(win, -1, RendererFlags.ACCELERATED);
if (renderer == null) {
printerr("Error initializing SDL renderer");
return;
}
MyRenderer myRenderer;
try {
myRenderer = new MyRenderer(win, renderer);
} catch (GLib.Error e) {
printerr(e.message);
return;
}
Event ev;
while (Event.wait(out ev) != 0 && ev.type != QUIT) {
myRenderer.on_event(ev);
// Rect r = g.get_rect();
// print(@"x: $(r.x) y: $(r.y) w: $(r.w) h: $(r.h) gx: $(g.x) rcp: $(g.c)\n");
// Glyph? g = tr.lookup(codepoint);
// renderer.copy(tr.texture, g.get_rect(), null);
}
SDL.quit();
}
|
post a comment