-
Notifications
You must be signed in to change notification settings - Fork 46
/
painter.c
660 lines (533 loc) · 15.7 KB
/
painter.c
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <retro_miscellaneous.h>
#include "lutro.h"
#include "painter.h"
#include "image.h"
#include "lutro_stb_image.h"
#include "encodings/utf.h"
#define _USE_MATH_DEFINES
#include <math.h>
#ifdef HAVE_COMPOSITION
/* from http://www.codeguru.com/cpp/cpp/algorithms/general/article.php/c15989/Tip-An-Optimized-Formula-for-Alpha-Blending-Pixels.htm */
#define COMPOSE_FAST(S, D, A) (((S * A) + (D * (256U - A))) >> 8U)
#define DISASSEMBLE_RGB(COLOR, R, G, B) \
R = ((COLOR & RED_MASK) >> RED_SHIFT);\
G = ((COLOR & GREEN_MASK) >> GREEN_SHIFT);\
B = ((COLOR & BLUE_MASK) >> BLUE_SHIFT);
#endif
static int strpos(const uint32_t *haystack, uint32_t needle)
{
// Note on performance a hash table would be much faster here
for (int i = 0; i < MAX_FONT_CHAR; i++) {
if (haystack[i] == 0)
break;
if (haystack[i] == needle)
return i;
}
return -1;
}
void pntr_reset(painter_t *p)
{
p->background = 0xff000000;
p->foreground = 0xffffffff;
p->clip.x = 0;
p->clip.y = 0;
p->clip.width = p->target->width;
p->clip.height = p->target->height;
pntr_origin(p, true);
}
void pntr_clear(painter_t *p)
{
if (!p->target->data)
return;
uint32_t *begin = p->target->data;
uint32_t *end = p->target->data + p->target->height * (p->target->pitch >> 2);
uint32_t color = p->background;
while (begin < end)
*begin++ = color;
}
// call this after setting the clip.
void pntr_sanitize_clip(painter_t *p)
{
rect_t target_rect = {
0, 0, p->target->width, p->target->height
};
p->clip = rect_intersect(&p->clip, &target_rect);
}
void pntr_strike_line(painter_t *p, int x1, int y1, int x2, int y2)
{
if (!p->target->data)
return;
uint32_t color = p->foreground;
if ((color & 0xff000000) == 0)
return;
int dx = abs(x2-x1), sx = x1<x2 ? 1 : -1;
int dy = abs(y2-y1), sy = y1<y2 ? 1 : -1;
int err = (dx>dy ? dx : -dy)/2, e2;
for (;;) {
if (y1 >= 0 && y1 < p->target->height)
if (x1 >= 0 && x1 < p->target->width)
p->target->data[y1 * (p->target->pitch >> 2) + x1] = color;
if (x1==x2 && y1==y2) break;
e2 = err;
if (e2 >-dx) { err -= dy; x1 += sx; }
if (e2 < dy) { err += dx; y1 += sy; }
}
}
void pntr_strike_rect(painter_t *p, const rect_t *rect)
{
int x1 = rect->x;
int y1 = rect->y;
int x2 = x1 + rect->width;
int y2 = y1 + rect->height;
pntr_strike_line(p, x1, y1, x1, y2);
pntr_strike_line(p, x1, y2, x2, y2);
pntr_strike_line(p, x2, y2, x2, y1);
pntr_strike_line(p, x2, y1, x1, y1);
}
void pntr_fill_rect(painter_t *p, const rect_t *rect)
{
if (!p->target->data)
return;
size_t row_size = p->target->pitch >> 2;
uint32_t color = p->foreground;
rect_t drect = {
rect->x + p->trans->tx, rect->y + p->trans->ty,
rect->width, rect->height
};
drect = rect_intersect(&p->clip, &drect);
if (rect_is_null(&drect))
return;
int x;
int xend = drect.x + drect.width;
uint32_t *row = p->target->data + drect.y * row_size;
uint32_t *end = row + row_size * drect.height;
if ((color & 0xff000000) == 0)
return;
#ifdef HAVE_COMPOSITION
uint32_t sa, sr, sg, sb, da, dr, dg, db, s, d;
do
{
for (x = drect.x; x < xend; ++x)
{
s = color;
d = row[x];
sa = s >> 24;
da = d >> 24;
DISASSEMBLE_RGB(s, sr, sg, sb);
DISASSEMBLE_RGB(d, dr, dg, db);
row[x] = ((sa + da * (255 - sa)) << 24) | (COMPOSE_FAST(sr, dr, sa) << 16) | (COMPOSE_FAST(sg, dg, sa) << 8) | (COMPOSE_FAST(sb, db, sa));
}
row += row_size;
} while (row < end);
#else
do
{
for (x = drect.x; x < xend; ++x)
row[x] = color;
row += row_size;
} while (row < end);
#endif
}
rect_t rect_intersect(const rect_t *a, const rect_t *b)
{
int left = MAX(a->x, b->x);
int right = MIN(a->x + a->width, b->x + b->width);
int top = MAX(a->y, b->y);
int bottom = MIN(a->y + a->height, b->y + b->height);
int width = right - left;
int height = bottom - top;
rect_t c = { left, top, MAX(width, 0), MAX(height, 0) };
return c;
}
int rect_is_null(const rect_t *r)
{
return r->width <= 0 || r->height <= 0;
}
void pntr_strike_poly(painter_t *p, const int *points, int nb_points)
{
if ((nb_points % 2) != 0)
return;
uint32_t color = p->foreground;
if ((color & 0xff000000) == 0)
return;
for (int i = 0; i < (nb_points / 2); ++i)
{
int x1 = points[2 * i];
int y1 = points[(2 * i) + 1];
int x2, y2;
if (i < ((nb_points / 2) - 1))
{
x2 = points[2 * (i + 1)];
y2 = points[(2 * (i + 1)) + 1];
}
else
{
x2 = points[0];
y2 = points[1];
}
pntr_strike_line(p, x1, y1, x2, y2);
}
}
void pntr_fill_poly(painter_t *p, const int *points, int nb_points)
{
if (!p->target->data)
return;
if ((nb_points % 2) != 0)
return;
uint32_t color = p->foreground;
if ((color & 0xff000000) == 0)
return;
// find the top-most and bottom-most points
int ymin = p->target->height + 1;
int ymax = -1;
for (int i = 0; i < (nb_points / 2); ++i)
{
ymin = MIN(ymin, points[(2 * i) + 1]);
ymax = MAX(ymax, points[(2 * i) + 1]);
}
// Note: the following algorithm is correct for convex polygons only.
// However, the polygon fill function in Love2D is also incorrect for non-convex polygons.
for (int yy = ymin; yy <= ymax; ++yy)
{
int xmin = p->target->width + 1;
int xmax = -1;
for (int i = 0; i < (nb_points / 2); ++i)
{
int x1 = points[2 * i];
int y1 = points[(2 * i) + 1];
int x2, y2;
if (i < ((nb_points / 2) - 1))
{
x2 = points[2 * (i + 1)];
y2 = points[(2 * (i + 1)) + 1];
}
else
{
x2 = points[0];
y2 = points[1];
}
if ((y1 > yy) != (y2 > yy))
{
int testx = x1 + ((x2 - x1) * (yy - y1)) / (y2 - y1);
xmin = MIN(xmin, testx);
xmax = MAX(xmax, testx);
}
}
for (int xx = xmin; xx <= xmax; ++xx)
{
if (yy >= 0 && yy < p->target->height)
if (xx >= 0 && xx < p->target->width)
p->target->data[yy * (p->target->pitch >> 2) + xx] = color;
}
}
}
void pntr_strike_ellipse(painter_t *p, int x, int y, int radius_x, int radius_y, int nb_segments)
{
for (int i = 0; i < nb_segments; ++i)
{
int x1 = x + (radius_x * cos(2 * i * M_PI / nb_segments));
int y1 = y + (radius_y * sin(2 * i * M_PI / nb_segments));
int x2 = x + (radius_x * cos(2 * (i + 1) * M_PI / nb_segments));
int y2 = y + (radius_y * sin(2 * (i + 1) * M_PI / nb_segments));
pntr_strike_line(p, x1, y1, x2, y2);
}
}
void pntr_fill_ellipse(painter_t *p, int x, int y, int radius_x, int radius_y, int nb_segments)
{
if (!p->target->data)
return;
uint32_t color = p->foreground;
if ((color & 0xff000000) == 0)
return;
for (int yy = y - radius_y; yy <= y + radius_y; ++yy)
{
int xmin = p->target->width + 1;
int xmax = -1;
for (int i = 0; i < nb_segments; ++i)
{
int x1 = x + (radius_x * cos(2 * i * M_PI / nb_segments));
int y1 = y + (radius_y * sin(2 * i * M_PI / nb_segments));
int x2 = x + (radius_x * cos(2 * (i + 1) * M_PI / nb_segments));
int y2 = y + (radius_y * sin(2 * (i + 1) * M_PI / nb_segments));
if ((y1 > yy) != (y2 > yy))
{
int testx = x1 + ((x2 - x1) * (yy - y1)) / (y2 - y1);
xmin = MIN(xmin, testx);
xmax = MAX(xmax, testx);
}
}
for (int xx = xmin; xx <= xmax; ++xx)
{
if (yy >= 0 && yy < p->target->height)
if (xx >= 0 && xx < p->target->width)
p->target->data[yy * (p->target->pitch >> 2) + xx] = color;
}
}
}
void pntr_draw(painter_t *p, const bitmap_t *bmp, const rect_t *src_rect, const rect_t *dst_rect)
{
if (!p->target->data)
return;
rect_t srect = *src_rect, drect = *dst_rect;
drect.x += p->trans->tx;
drect.y += p->trans->ty;
/* scaling not supported */
drect.width = srect.width;
drect.height = srect.height;
if (drect.x < 0)
{
srect.x += -drect.x;
srect.width += drect.x;
}
if (drect.y < 0)
{
srect.y += -drect.y;
srect.height += drect.y;
}
drect = rect_intersect(&drect, &p->clip);
drect.width = MIN(drect.width, srect.width);
drect.height = MIN(drect.height, srect.height);
if (rect_is_null(&drect) || rect_is_null(&srect))
return;
size_t dst_skip = p->target->pitch >> 2;
size_t src_skip = bmp->pitch >> 2;
uint32_t *dst = p->target->data + dst_skip * drect.y + drect.x;
uint32_t *src = bmp->data + src_skip * srect.y + srect.x;
int rows_left = drect.height;
int cols = drect.width;
int x = 0;
#ifdef HAVE_COMPOSITION
uint32_t sa, sr, sg, sb, da, dr, dg, db, s, d;
while (rows_left--)
{
for (x = 0; x < cols; ++x)
{
s = src[x];
d = dst[x];
sa = s >> 24;
da = d >> 24;
DISASSEMBLE_RGB(s, sr, sg, sb);
DISASSEMBLE_RGB(d, dr, dg, db);
dst[x] = ((sa + da * (255 - sa)) << 24) | (COMPOSE_FAST(sr, dr, sa) << 16) | (COMPOSE_FAST(sg, dg, sa) << 8) | (COMPOSE_FAST(sb, db, sa));
}
dst += dst_skip;
src += src_skip;
}
#else
uint32_t c;
while (rows_left--)
{
for (x = 0; x < cols; ++x)
{
c = src[x];
if (c & 0xff000000)
dst[x] = c;
}
dst += dst_skip;
src += src_skip;
}
#endif
}
void pntr_print(painter_t *p, int x, int y, const char *text, int limit)
{
assert(p->font != NULL);
if (p->font->flags & FONT_FREETYPE)
{
}
else
{
bitmap_t *atlas = &p->font->atlas;
font_t *font = p->font;
rect_t drect = {
x, y, p->target->width, atlas->height
};
rect_t srect = {
0, 0, 0, atlas->height
};
size_t char_nb = utf8len(text);
uint32_t *utf32 = NULL;
// Avoid to call malloc for small string rendering
uint32_t buf[256];
if (char_nb < 256) {
utf32 = buf;
} else {
utf32 = lutro_malloc(char_nb * 4);
}
utf8_conv_utf32(utf32, char_nb, text, strlen(text));
for(int i = 0; i < char_nb; i++)
{
uint32_t c = utf32[i];
int pos = strpos(font->characters, c);
if (pos < 0)
continue;
srect.x = font->separators[pos] + 1;
drect.width = srect.width = font->separators[pos+1] - srect.x;
pntr_draw(p, atlas, &srect, &drect);
drect.x += srect.width + font->extraspacing;
if (limit > 0 && drect.x - x > limit)
{
drect.x = x;
drect.y += atlas->height;
}
if (c == '\n')
{
drect.x = x;
drect.y += atlas->height;
}
}
if (utf32 != buf)
lutro_free(utf32);
}
}
int pntr_text_width(painter_t *p, const char *text)
{
int width = 0;
assert(p->font != NULL);
if (p->font->flags & FONT_FREETYPE)
{
}
else
{
font_t *font = p->font;
int glyph_x, glyph_width;
size_t char_nb = utf8len(text);
uint32_t *utf32 = NULL;
// Avoid to call malloc for small string rendering
uint32_t buf[256];
if (char_nb < 256) {
utf32 = buf;
} else {
utf32 = lutro_malloc(char_nb * 4);
}
utf8_conv_utf32(utf32, char_nb, text, strlen(text));
for(int i = 0; i < char_nb; i++)
{
uint32_t c = utf32[i];
int pos = strpos(font->characters, c);
if (pos < 0)
continue;
glyph_x = font->separators[pos] + 1;
glyph_width = font->separators[pos+1] - glyph_x;
width += glyph_width + font->extraspacing;
}
if (utf32 != buf)
lutro_free(utf32);
}
return width;
}
void pntr_printf(painter_t *p, int x, int y, const char *format, ...)
{
char *buf;
va_list v;
va_start(v, format);
vasprintf(&buf, format, v);
va_end(v);
pntr_print(p, x, y, buf, 0);
lutro_free(buf);
}
bool pntr_push(painter_t *p)
{
if (p->stack_pos == ARRAY_SIZE(p->stack))
return false;
memcpy(&p->stack[p->stack_pos + 1], &p->stack[p->stack_pos], sizeof(p->stack[0]));
p->trans = &p->stack[++p->stack_pos];
return true;
}
bool pntr_pop(painter_t *p)
{
if (p->stack_pos == 0)
return false;
p->trans = &p->stack[--p->stack_pos];
return true;
}
void pntr_origin(painter_t *p, bool reset_stack)
{
if (reset_stack)
{
p->stack_pos = 0;
p->trans = &p->stack[p->stack_pos];
}
pntr_scale(p, 1, 1);
pntr_rotate(p, 0);
pntr_translate(p, 0, 0);
}
void pntr_scale(painter_t *p, float x, float y)
{
p->trans->sx = x;
p->trans->sy = y;
}
void pntr_rotate(painter_t *p, float rad)
{
p->trans->r = rad;
}
void pntr_translate(painter_t *p, int x, int y)
{
p->trans->tx += x;
p->trans->ty += y;
}
font_t *font_load_filename(const char *filename, const char *characters, unsigned flags)
{
// FIXME: note it would be better to refactor/rewrite this function as
// However special care must be taken for allocation/free outside of those functions
// 1/ create a bitmap from filename
// 2/ return font_load_bitmap(bitmap, characters, flags)
font_t *font = lutro_calloc(1, sizeof(font_t));
font->owner = lutro_calloc(1, sizeof(uint32_t));
flags &= ~FONT_FREETYPE;
font->pxsize = 0;
font->flags = flags;
bitmap_t *atlas = &font->atlas;
lutro_stb_image_load(filename, &atlas->data, &atlas->width, &atlas->height);
atlas->pitch = atlas->width << 2;
uint32_t separator = atlas->data[0];
int max_separators = MAX_FONT_CHAR;
int i, char_counter = 0;
for (i = 0; i < atlas->width && char_counter < max_separators; i++)
{
if (separator == atlas->data[i])
font->separators[char_counter++] = i;
}
// Note: later 'len' could be used to dynamically alloc the font->characters and
// font->separators buffers
size_t len = utf8len(characters);
if (len > MAX_FONT_CHAR) {
fprintf(stderr, "Font atlas is too big. It will be truncated !\n");
}
utf8_conv_utf32(font->characters, MAX_FONT_CHAR, characters, strlen(characters));
return font;
}
font_t *font_load_bitmap(const bitmap_t *atlas, const char *characters, unsigned flags)
{
font_t *font = lutro_calloc(1, sizeof(font_t));
font->owner = lutro_calloc(1, sizeof(uint32_t));
// Deep copy data from atlas to give ownership. It also matches the behavior
// of font_load_filename that allocate a buffer for the atlas
font->atlas = *atlas;
font->atlas.data = lutro_malloc(atlas->pitch * atlas->height);
memcpy(font->atlas.data, atlas->data, atlas->pitch * atlas->height);
flags &= ~FONT_FREETYPE;
font->pxsize = 0;
font->flags = flags;
uint32_t separator = font->atlas.data[0];
int max_separators = MAX_FONT_CHAR;
int i, char_counter = 0;
for (i = 0; i < font->atlas.width && char_counter < max_separators; i++)
{
if (separator == font->atlas.data[i])
font->separators[char_counter++] = i;
}
// Note: later 'len' could be used to dynamically alloc the font->characters and
// font->separators buffers
size_t len = utf8len(characters);
if (len > MAX_FONT_CHAR) {
fprintf(stderr, "Font atlas is too big. It will be truncated !\n");
}
utf8_conv_utf32(font->characters, MAX_FONT_CHAR, characters, strlen(characters));
return font;
}