-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclayterm.c
More file actions
710 lines (607 loc) · 20.7 KB
/
clayterm.c
File metadata and controls
710 lines (607 loc) · 20.7 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
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/* clayterm.c — WASM terminal rendering engine for Clay UI
*
* Public API (exported to WASM):
* clayterm_size — compute arena size for given dimensions
* init — initialize a Clayterm instance in provided memory
* reduce — decode command buffer, run Clay layout, render to ANSI
* output — pointer to output byte buffer
* length — length of output byte buffer
* measure — Clay text measurement callback
* error_count, error_type, error_message_length, error_message_ptr
* — per-render Clay error accessors
*/
#include "clayterm.h"
#include "../clay/clay.h"
#include "buffer.h"
#include "cell.h"
#include "mem.h"
#include "utf8.h"
#include "wcwidth.h"
/* ── Command buffer protocol ──────────────────────────────────────── */
#define OP_BEGIN_LAYOUT 0x01
#define OP_OPEN_ELEMENT 0x02
#define OP_TEXT 0x03
#define OP_CLOSE_ELEMENT 0x04
#define OP_END_LAYOUT 0x05
#define PROP_LAYOUT 0x01
#define PROP_BG_COLOR 0x02
#define PROP_CORNER_RADIUS 0x04
#define PROP_BORDER 0x08
#define PROP_CLIP 0x10
#define PROP_FLOATING 0x20
/* ── Instance state ───────────────────────────────────────────────── */
#define MAX_ERRORS 32
struct Clayterm {
int w, h;
Cell *front;
Cell *back;
Buffer out;
uint32_t lastfg, lastbg;
int lastx, lasty;
/* clip region */
int clipx, clipy, clipw, cliph;
int clipping;
/* error collection */
Clay_ErrorData errors[MAX_ERRORS];
int error_count;
};
/* Memory layout inside the arena provided by the host:
* [Clayterm struct] [front cells] [back cells] [output buffer]
*
* Output buffer is sized at 64 bytes per cell — enough for worst-case
* full-screen redraws with truecolor SGR sequences on every cell.
*/
#define OUT_BYTES_PER_CELL 64
/* ── Cell buffer ops ──────────────────────────────────────────────── */
static Cell *cell_at(struct Clayterm *ct, Cell *buf, int x, int y) {
return &buf[y * ct->w + x];
}
static void setcell(struct Clayterm *ct, int x, int y, uint32_t ch, uint32_t fg,
uint32_t bg) {
if (x < 0 || x >= ct->w || y < 0 || y >= ct->h)
return;
if (ct->clipping) {
if (x < ct->clipx || x >= ct->clipx + ct->clipw)
return;
if (y < ct->clipy || y >= ct->clipy + ct->cliph)
return;
}
Cell *c = cell_at(ct, ct->back, x, y);
c->ch = ch;
c->fg = fg;
if (!(bg & ATTR_DEFAULT)) {
c->bg = bg;
}
}
/* ── Escape sequence generation ───────────────────────────────────── */
static void emit_attr(struct Clayterm *ct, uint32_t fg, uint32_t bg) {
if (fg == ct->lastfg && bg == ct->lastbg)
return;
/* SGR reset */
buf_str(&ct->out, "\x1b[0m");
/* style attributes from fg high byte */
if (fg & ATTR_BOLD)
buf_str(&ct->out, "\x1b[1m");
if (fg & ATTR_DIM)
buf_str(&ct->out, "\x1b[2m");
if (fg & ATTR_ITALIC)
buf_str(&ct->out, "\x1b[3m");
if (fg & ATTR_UNDERLINE)
buf_str(&ct->out, "\x1b[4m");
if (fg & ATTR_BLINK)
buf_str(&ct->out, "\x1b[5m");
if (fg & ATTR_REVERSE)
buf_str(&ct->out, "\x1b[7m");
if (fg & ATTR_STRIKEOUT)
buf_str(&ct->out, "\x1b[9m");
/* foreground truecolor */
if (!(fg & ATTR_DEFAULT)) {
buf_str(&ct->out, "\x1b[38;2;");
buf_num(&ct->out, (fg >> 16) & 0xff);
buf_put(&ct->out, ";", 1);
buf_num(&ct->out, (fg >> 8) & 0xff);
buf_put(&ct->out, ";", 1);
buf_num(&ct->out, fg & 0xff);
buf_put(&ct->out, "m", 1);
}
/* background truecolor */
if (!(bg & ATTR_DEFAULT)) {
buf_str(&ct->out, "\x1b[48;2;");
buf_num(&ct->out, (bg >> 16) & 0xff);
buf_put(&ct->out, ";", 1);
buf_num(&ct->out, (bg >> 8) & 0xff);
buf_put(&ct->out, ";", 1);
buf_num(&ct->out, bg & 0xff);
buf_put(&ct->out, "m", 1);
}
ct->lastfg = fg;
ct->lastbg = bg;
}
/** Emit CUP sequence. `row` is the 1-based terminal row of the region. */
static void emit_cursor(struct Clayterm *ct, int x, int y, int row) {
buf_str(&ct->out, "\x1b[");
buf_num(&ct->out, y + row);
buf_put(&ct->out, ";", 1);
buf_num(&ct->out, x + 1);
buf_put(&ct->out, "H", 1);
}
static void emit_ch(struct Clayterm *ct, int x, int y, int row, uint32_t ch) {
if (ct->lastx != x - 1 || ct->lasty != y) {
emit_cursor(ct, x, y, row);
}
ct->lastx = x;
ct->lasty = y;
if (!iswprint(ch))
ch = 0xfffd;
buf_char(&ct->out, ch);
}
/**
* Diff back buffer against front buffer and emit only changed cells
* using absolute CUP positioning (\x1b[row;colH). Unchanged cells are
* skipped, making this efficient for subsequent frames where most of
* the screen is static. Derived from termbox2 tb_present.
*/
static void present_cups(struct Clayterm *ct, int row) {
ct->lastx = -1;
ct->lasty = -1;
for (int y = 0; y < ct->h; y++) {
for (int x = 0; x < ct->w;) {
Cell *back = cell_at(ct, ct->back, x, y);
Cell *front = cell_at(ct, ct->front, x, y);
int w = wcwidth(back->ch);
if (w < 1)
w = 1;
if (cell_cmp(back, front)) {
/* copy to front */
*front = *back;
emit_attr(ct, back->fg, back->bg);
if (w > 1 && x >= ct->w - (w - 1)) {
/* wide char doesn't fit, send spaces */
for (int i = x; i < ct->w; i++)
emit_ch(ct, i, y, row, ' ');
} else {
emit_ch(ct, x, y, row, back->ch);
/* mark trailing cells of wide char as invalid in front
* so they'll diff when overwritten by narrow chars */
for (int i = 1; i < w; i++) {
Cell *fw = cell_at(ct, ct->front, x + i, y);
fw->ch = 0xffffffff;
fw->fg = 0xffffffff;
fw->bg = 0xffffffff;
}
}
}
x += w;
}
}
}
/**
* Emit back buffer as newline-separated rows without CUP positioning.
* Every cell is written (no diffing), and the front buffer is primed
* so that a subsequent present() call can diff efficiently. This is
* used for inline "region" rendering where the caller manages cursor
* positioning externally and the output must work in pipes.
*/
static void present_lines(struct Clayterm *ct) {
for (int y = 0; y < ct->h; y++) {
if (y > 0)
buf_put(&ct->out, "\n", 1);
for (int x = 0; x < ct->w;) {
Cell *back = cell_at(ct, ct->back, x, y);
Cell *front = cell_at(ct, ct->front, x, y);
int w = wcwidth(back->ch);
if (w < 1)
w = 1;
*front = *back;
emit_attr(ct, back->fg, back->bg);
if (w > 1 && x >= ct->w - (w - 1)) {
for (int i = x; i < ct->w; i++) {
buf_char(&ct->out, ' ');
}
} else {
uint32_t ch = back->ch;
if (!iswprint(ch))
ch = 0xfffd;
buf_char(&ct->out, ch);
for (int i = 1; i < w; i++) {
Cell *fw = cell_at(ct, ct->front, x + i, y);
fw->ch = 0xffffffff;
fw->fg = 0xffffffff;
fw->bg = 0xffffffff;
}
}
x += w;
}
}
}
/* ── Color conversion ─────────────────────────────────────────────── */
static uint32_t color(Clay_Color c) {
uint8_t r = (uint8_t)c.r;
uint8_t g = (uint8_t)c.g;
uint8_t b = (uint8_t)c.b;
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | (uint32_t)b;
}
/* ── Clay render backend ──────────────────────────────────────────── */
static void render_rect(struct Clayterm *ct, int x0, int y0, int x1, int y1,
Clay_RectangleRenderData *r) {
uint32_t bg = color(r->backgroundColor);
for (int y = y0; y < y1; y++)
for (int x = x0; x < x1; x++)
setcell(ct, x, y, ' ', ATTR_DEFAULT, bg);
}
static void render_text(struct Clayterm *ct, int x0, int y0,
Clay_TextRenderData *t) {
uint32_t fg = color(t->textColor);
/* text attrs are packed into the alpha channel by reduce() */
uint32_t attrs = ((uint32_t)(uint8_t)t->textColor.a) << 24;
fg |= attrs;
const char *p = t->stringContents.chars;
int rem = t->stringContents.length;
int x = x0;
while (rem > 0) {
uint32_t cp;
int n = utf8_decode(&cp, p);
if (n <= 0) {
n = 1;
cp = 0xfffd;
}
int cw = wcwidth(cp);
if (cw < 0)
cw = 1;
if (cw > 0) {
setcell(ct, x, y0, cp, fg, ATTR_DEFAULT);
x += cw;
}
p += n;
rem -= n;
}
}
static void render_border(struct Clayterm *ct, int x0, int y0, int x1, int y1,
Clay_BorderRenderData *b) {
uint32_t fg = color(b->color);
uint32_t bg = ATTR_DEFAULT;
int top = b->width.top > 0;
int bot = b->width.bottom > 0;
int left = b->width.left > 0;
int right = b->width.right > 0;
/* corners — rounded when corner radius > 0 */
uint32_t tl = b->cornerRadius.topLeft > 0 ? 0x256d : 0x250c;
uint32_t tr = b->cornerRadius.topRight > 0 ? 0x256e : 0x2510;
uint32_t bl = b->cornerRadius.bottomLeft > 0 ? 0x2570 : 0x2514;
uint32_t br = b->cornerRadius.bottomRight > 0 ? 0x256f : 0x2518;
if (top && left)
setcell(ct, x0, y0, tl, fg, bg);
if (top && right)
setcell(ct, x1 - 1, y0, tr, fg, bg);
if (bot && left)
setcell(ct, x0, y1 - 1, bl, fg, bg);
if (bot && right)
setcell(ct, x1 - 1, y1 - 1, br, fg, bg);
/* horizontal edges */
if (top)
for (int x = x0 + left; x < x1 - right; x++)
setcell(ct, x, y0, 0x2500, fg, bg);
if (bot)
for (int x = x0 + left; x < x1 - right; x++)
setcell(ct, x, y1 - 1, 0x2500, fg, bg);
/* vertical edges */
if (left)
for (int y = y0 + top; y < y1 - bot; y++)
setcell(ct, x0, y, 0x2502, fg, bg);
if (right)
for (int y = y0 + top; y < y1 - bot; y++)
setcell(ct, x1 - 1, y, 0x2502, fg, bg);
}
/* ── Command buffer helpers ───────────────────────────────────────── */
static uint32_t rd(uint32_t *buf, int len, int *i) {
if (*i < len)
return buf[(*i)++];
return 0;
}
static float rdf(uint32_t *buf, int len, int *i) {
uint32_t v = rd(buf, len, i);
float f;
memcpy(&f, &v, 4);
return f;
}
static Clay_Color unpack_color(uint32_t c) {
return (Clay_Color){
.r = (float)((c >> 16) & 0xff),
.g = (float)((c >> 8) & 0xff),
.b = (float)(c & 0xff),
.a = (float)((c >> 24) & 0xff),
};
}
static Clay_SizingAxis decode_axis(uint32_t *buf, int len, int *i) {
uint32_t type = rd(buf, len, i);
float a = rdf(buf, len, i);
float b = rdf(buf, len, i);
Clay_SizingAxis axis = {0};
switch (type) {
case 0: /* FIT */
axis.type = CLAY__SIZING_TYPE_FIT;
axis.size.minMax.min = a;
axis.size.minMax.max = b;
break;
case 1: /* GROW */
axis.type = CLAY__SIZING_TYPE_GROW;
axis.size.minMax.min = a;
axis.size.minMax.max = b;
break;
case 2: /* PERCENT */
axis.type = CLAY__SIZING_TYPE_PERCENT;
axis.size.percent = a;
break;
case 3: /* FIXED */
axis.type = CLAY__SIZING_TYPE_FIXED;
axis.size.minMax.min = a;
axis.size.minMax.max = a;
break;
}
return axis;
}
/* ── Public API ───────────────────────────────────────────────────── */
static int align64(int n) { return (n + 63) & ~63; }
int clayterm_size(int w, int h) {
int cell_count = w * h;
int cell_bytes = cell_count * (int)sizeof(Cell);
int out_bytes = cell_count * OUT_BYTES_PER_CELL;
int clay_bytes = (int)Clay_MinMemorySize();
return align8((int)sizeof(struct Clayterm)) + align8(cell_bytes) /* front */
+ align8(cell_bytes) /* back */
+ align8(out_bytes) /* output buffer */
+ align64(clay_bytes); /* Clay arena */
}
static void clay_error(Clay_ErrorData err) {
struct Clayterm *ct = (struct Clayterm *)err.userData;
if (ct->error_count < MAX_ERRORS) {
ct->errors[ct->error_count++] = err;
}
}
int error_count(struct Clayterm *ct) { return ct->error_count; }
int error_type(struct Clayterm *ct, int index) {
if (index < 0 || index >= ct->error_count)
return -1;
return (int)ct->errors[index].errorType;
}
int error_message_length(struct Clayterm *ct, int index) {
if (index < 0 || index >= ct->error_count)
return 0;
return ct->errors[index].errorText.length;
}
int error_message_ptr(struct Clayterm *ct, int index) {
if (index < 0 || index >= ct->error_count)
return 0;
return (int)ct->errors[index].errorText.chars;
}
struct Clayterm *init(void *mem, int w, int h) {
struct Clayterm *ct = (struct Clayterm *)mem;
int cell_count = w * h;
int cell_bytes = align8(cell_count * (int)sizeof(Cell));
int out_bytes = align8(cell_count * OUT_BYTES_PER_CELL);
char *base = (char *)mem + align8((int)sizeof(struct Clayterm));
char *clay_mem = base + cell_bytes * 2 + out_bytes;
int clay_bytes = align64((int)Clay_MinMemorySize());
Clay_Arena arena =
Clay_CreateArenaWithCapacityAndMemory(clay_bytes, clay_mem);
Clay_Initialize(arena, (Clay_Dimensions){(float)w, (float)h},
(Clay_ErrorHandler){clay_error, ct});
*ct = (struct Clayterm){
.w = w,
.h = h,
.front = (Cell *)base,
.back = (Cell *)(base + cell_bytes),
.out = {base + cell_bytes * 2, 0, cell_count * OUT_BYTES_PER_CELL},
.lastfg = 0xffffffff,
.lastbg = 0xffffffff,
.lastx = -1,
.lasty = -1,
};
// initialize back buffer with spaces and default fg/bg
cells_fill(ct->back, w, h, ' ', ATTR_DEFAULT, ATTR_DEFAULT);
// initialize front buffer with zeros. Every cell will be
cells_fill(ct->front, w, h, 0, 0, 0);
return ct;
}
void reduce(struct Clayterm *ct, uint32_t *buf, int len, int mode, int row) {
int i = 0;
ct->error_count = 0;
Clay_BeginLayout();
while (i < len) {
uint32_t op = rd(buf, len, &i);
switch (op) {
case OP_OPEN_ELEMENT: {
/* read id string */
uint32_t id_len = rd(buf, len, &i);
int id_words = (id_len + 3) / 4;
char *id_chars = (char *)&buf[i];
i += id_words;
if (id_len > 0) {
Clay_String str = {.length = (int32_t)id_len, .chars = id_chars};
Clay_ElementId eid = Clay__HashString(str, 0);
Clay__OpenElementWithId(eid);
} else {
Clay__OpenElement();
}
/* read property mask */
uint32_t mask = rd(buf, len, &i);
Clay_ElementDeclaration decl = {0};
if (mask & PROP_LAYOUT) {
decl.layout.sizing.width = decode_axis(buf, len, &i);
decl.layout.sizing.height = decode_axis(buf, len, &i);
uint32_t pad = rd(buf, len, &i);
decl.layout.padding.left = pad & 0xff;
decl.layout.padding.right = (pad >> 8) & 0xff;
decl.layout.padding.top = (pad >> 16) & 0xff;
decl.layout.padding.bottom = (pad >> 24) & 0xff;
uint32_t gd = rd(buf, len, &i);
decl.layout.childGap = gd & 0xffff;
decl.layout.layoutDirection = (gd >> 16) & 0xff;
uint32_t al = rd(buf, len, &i);
decl.layout.childAlignment.x = al & 0xff;
decl.layout.childAlignment.y = (al >> 8) & 0xff;
}
if (mask & PROP_BG_COLOR) {
decl.backgroundColor = unpack_color(rd(buf, len, &i));
}
if (mask & PROP_CORNER_RADIUS) {
uint32_t cr = rd(buf, len, &i);
decl.cornerRadius.topLeft = (float)(cr & 0xff);
decl.cornerRadius.topRight = (float)((cr >> 8) & 0xff);
decl.cornerRadius.bottomLeft = (float)((cr >> 16) & 0xff);
decl.cornerRadius.bottomRight = (float)((cr >> 24) & 0xff);
}
if (mask & PROP_BORDER) {
decl.border.color = unpack_color(rd(buf, len, &i));
uint32_t bw = rd(buf, len, &i);
decl.border.width.left = bw & 0xff;
decl.border.width.right = (bw >> 8) & 0xff;
decl.border.width.top = (bw >> 16) & 0xff;
decl.border.width.bottom = (bw >> 24) & 0xff;
}
if (mask & PROP_CLIP) {
uint32_t cl = rd(buf, len, &i);
decl.clip.horizontal = cl & 0xff;
decl.clip.vertical = (cl >> 8) & 0xff;
}
if (mask & PROP_FLOATING) {
decl.floating.offset.x = rdf(buf, len, &i);
decl.floating.offset.y = rdf(buf, len, &i);
decl.floating.expand.width = rdf(buf, len, &i);
decl.floating.expand.height = rdf(buf, len, &i);
decl.floating.parentId = rd(buf, len, &i);
uint32_t fc = rd(buf, len, &i);
decl.floating.attachTo = fc & 0xff;
decl.floating.attachPoints.element = (fc >> 8) & 0xff;
decl.floating.attachPoints.parent = (fc >> 16) & 0xff;
decl.floating.pointerCaptureMode = (fc >> 24) & 0xff;
uint32_t fd = rd(buf, len, &i);
decl.floating.clipTo = fd & 0xff;
decl.floating.zIndex = (int16_t)(fd >> 8);
}
Clay__ConfigureOpenElement(decl);
break;
}
case OP_TEXT: {
uint32_t col = rd(buf, len, &i);
uint32_t cfg = rd(buf, len, &i);
uint32_t str_len = rd(buf, len, &i);
int str_words = (str_len + 3) / 4;
char *str_chars = (char *)&buf[i];
i += str_words;
Clay_String text = {.length = (int32_t)str_len, .chars = str_chars};
Clay_TextElementConfig config = {0};
config.textColor = unpack_color(col);
config.fontSize = cfg & 0xff;
config.fontId = (cfg >> 8) & 0xff;
config.wrapMode = (cfg >> 16) & 0xff;
/* attrs byte -> alpha channel for render_text to extract */
config.textColor.a = (float)((cfg >> 24) & 0xff);
Clay__OpenTextElement(text, Clay__StoreTextElementConfig(config));
break;
}
case OP_CLOSE_ELEMENT:
Clay__CloseElement();
break;
default:
break;
}
}
Clay_RenderCommandArray cmds = Clay_EndLayout();
/* reset output state */
ct->out.length = 0;
ct->lastfg = ct->lastbg = 0xffffffff;
ct->lastx = ct->lasty = -1;
cells_fill(ct->back, ct->w, ct->h, ' ', ATTR_DEFAULT, ATTR_DEFAULT);
/* walk Clay render commands into back buffer */
for (int32_t j = 0; j < cmds.length; j++) {
Clay_RenderCommand *cmd = Clay_RenderCommandArray_Get(&cmds, j);
Clay_BoundingBox box = cmd->boundingBox;
int x0 = (int)box.x;
int y0 = (int)box.y;
int x1 = (int)(box.x + box.width);
int y1 = (int)(box.y + box.height);
switch (cmd->commandType) {
case CLAY_RENDER_COMMAND_TYPE_RECTANGLE:
render_rect(ct, x0, y0, x1, y1, &cmd->renderData.rectangle);
break;
case CLAY_RENDER_COMMAND_TYPE_TEXT:
render_text(ct, x0, y0, &cmd->renderData.text);
break;
case CLAY_RENDER_COMMAND_TYPE_BORDER:
render_border(ct, x0, y0, x1, y1, &cmd->renderData.border);
break;
case CLAY_RENDER_COMMAND_TYPE_SCISSOR_START:
ct->clipping = 1;
ct->clipx = x0;
ct->clipy = y0;
ct->clipw = x1 - x0;
ct->cliph = y1 - y0;
break;
case CLAY_RENDER_COMMAND_TYPE_SCISSOR_END:
ct->clipping = 0;
break;
default:
break;
}
}
if (mode == 1) {
present_lines(ct);
} else {
present_cups(ct, row);
}
}
char *output(struct Clayterm *ct) { return ct->out.data; }
int length(struct Clayterm *ct) { return ct->out.length; }
int get_element_bounds(const char *name, int name_len, float *out) {
Clay_String str = {.length = name_len, .chars = name};
Clay_ElementId eid = Clay__HashString(str, 0);
Clay_ElementData data = Clay_GetElementData(eid);
if (!data.found) {
return 0;
}
out[0] = data.boundingBox.x;
out[1] = data.boundingBox.y;
out[2] = data.boundingBox.width;
out[3] = data.boundingBox.height;
return 1;
}
int pointer_over_count(void) { return Clay_GetPointerOverIds().length; }
int pointer_over_id_string_length(int index) {
Clay_ElementIdArray ids = Clay_GetPointerOverIds();
if (index >= ids.length)
return 0;
return ids.internalArray[index].stringId.length;
}
int pointer_over_id_string_ptr(int index) {
Clay_ElementIdArray ids = Clay_GetPointerOverIds();
if (index >= ids.length)
return 0;
return (int)ids.internalArray[index].stringId.chars;
}
void measure(int ret, int txt) {
/* Read Clay_StringSlice from txt address.
* Clay_StringSlice layout: { int32_t length, const char *chars, ... }
* We only need length and chars. */
int32_t slen = *(int32_t *)txt;
const char *chars = *(const char **)(txt + 4);
int w = 0;
const char *p = chars;
int rem = slen;
while (rem > 0) {
uint32_t cp;
int n = utf8_decode(&cp, p);
if (n <= 0) {
n = 1;
}
int cw = wcwidth(cp);
if (cw > 0)
w += cw;
p += n;
rem -= n;
}
/* Write Clay_Dimensions { float width, float height } to ret */
float *dims = (float *)ret;
dims[0] = (float)w;
dims[1] = 1.0f;
}