1#include "draw.h"
2
3static void draw_bitmap(uint32_t* dst, int w, int h, uint8_t* bmp,
4 int bw, int bh, int x, int y, uint32_t fg);
5static void blend(uint32_t* dst, int w, int h, int x, int y, uint8_t alpha,
6 uint32_t fg);
7
8/* draw bitmap of properties (look: vvvv) to screen */
9static void draw_bitmap(uint32_t* dst, int w, int h, uint8_t* bmp,
10 int bw, int bh, int x, int y, uint32_t fg)
11{
12 for (int by = 0; by < bh; by++) {
13 for (int bx = 0; bx < bw; bx++) {
14 uint8_t a = bmp[by * bw + bx];
15 blend(dst, w, h, x + bx, y + by, a, fg);
16 }
17 }
18}
19
20/* blend a pixel using alpha compositong */
21static void blend(uint32_t* dst, int w, int h, int x, int y, uint8_t alpha,
22 uint32_t fg)
23{
24 uint8_t* px;
25 uint8_t fg_r;
26 uint8_t fg_g;
27 uint8_t fg_b;
28
29 if (x < 0 || x >= w || y < 0 || y >= h || alpha == 0)
30 return; /* transparent/oob */
31
32 /* ptr to px at (x, y) */
33 px = (uint8_t*)&dst[y * w + x];
34
35 /* unpack fg colour*/
36 fg_r = (uint8_t)(fg & 0xff);
37 fg_g = (uint8_t)((fg >> 8) & 0xff);
38 fg_b = (uint8_t)((fg >> 16) & 0xff);
39
40 /* alpha blend each channel */
41 px[0] = (uint8_t)((fg_r * alpha + px[0] * (255 - alpha)) / 255);
42 px[1] = (uint8_t)((fg_g * alpha + px[1] * (255 - alpha)) / 255);
43 px[2] = (uint8_t)((fg_b * alpha + px[2] * (255 - alpha)) / 255);
44
45 px[3] = 255;
46}
47
48void draw_rune(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
49 uint32_t bg)
50{
51 int x0 = cl; /* x origin of cell */
52 int y0 = rw; /* y origin of cell */
53 int x;
54 int y;
55
56 /* iterate through columns */
57 for (y = y0; y < y0+ch && y < h; y++) {
58 if (y < 0)
59 continue;
60
61
62 /* through rows */
63 for (x = x0; x < x0+cw && x < w; x++) {
64 if (x < 0)
65 continue;
66 dst[y * w + x] = bg;
67 }
68 }
69}
70
71void draw_codepoint(Fontface* f, uint32_t* dst, int w, int h, int x,
72 int baseline, uint32_t cp, uint32_t fg)
73{
74 if (!f || !dst)
75 return;
76
77 if (!f->bdf || cp >= BDF_GLYPHS_MAX)
78 return;
79
80 BdfGlyph* g = &f->bdf[cp];
81 if (!g->valid)
82 return;
83
84 draw_bitmap(
85 dst, w, h, g->bmp, g->w, g->h, x + g->xoff,
86 baseline - g->yoff - g->h, fg
87 );
88}
89