aboutsummaryrefslogtreecommitdiff
path: root/draw.c
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-10-20 11:44:30 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-10-20 11:44:30 +0200
commit9b9005101e6170a226acb265428aac429be6edbf (patch)
treef89c76f4a5c2a00a72abe9b92468ac9f15083b94 /draw.c
downloadfacedet-9b9005101e6170a226acb265428aac429be6edbf.tar.gz
facedet-9b9005101e6170a226acb265428aac429be6edbf.zip
Init
Diffstat (limited to 'draw.c')
-rw-r--r--draw.c184
1 files changed, 184 insertions, 0 deletions
diff --git a/draw.c b/draw.c
new file mode 100644
index 0000000..e7b487e
--- /dev/null
+++ b/draw.c
@@ -0,0 +1,184 @@
+/* Printable ASCII uses 5x7 glyphs stored row-major, with the leftmost
+ * column in bit 4. Unsupported characters use a hollow box. */
+
+#include <string.h>
+
+#include "draw.h"
+
+#define G(a, b, c, d, e, f, g) {a, b, c, d, e, f, g}
+
+static const unsigned char FONT[95][7] = {
+ /* 32 space */ G(0, 0, 0, 0, 0, 0, 0),
+ /* ! */ G(0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x04),
+ /* " */ G(0x0a, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00),
+ /* # */ G(0x0a, 0x0a, 0x1f, 0x0a, 0x1f, 0x0a, 0x0a),
+ /* $ */ G(0x04, 0x0f, 0x14, 0x0e, 0x05, 0x1e, 0x04),
+ /* % */ G(0x19, 0x19, 0x02, 0x04, 0x08, 0x13, 0x13),
+ /* & */ G(0x0c, 0x12, 0x14, 0x08, 0x15, 0x12, 0x0d),
+ /* ' */ G(0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00),
+ /* ( */ G(0x02, 0x04, 0x08, 0x08, 0x08, 0x04, 0x02),
+ /* ) */ G(0x08, 0x04, 0x02, 0x02, 0x02, 0x04, 0x08),
+ /* * */ G(0x00, 0x15, 0x0e, 0x1f, 0x0e, 0x15, 0x00),
+ /* + */ G(0x00, 0x04, 0x04, 0x1f, 0x04, 0x04, 0x00),
+ /* , */ G(0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x08),
+ /* - */ G(0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00),
+ /* . */ G(0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c),
+ /* / */ G(0x01, 0x02, 0x02, 0x04, 0x08, 0x08, 0x10),
+ /* 0 */ G(0x0e, 0x11, 0x13, 0x15, 0x19, 0x11, 0x0e),
+ /* 1 */ G(0x04, 0x0c, 0x04, 0x04, 0x04, 0x04, 0x0e),
+ /* 2 */ G(0x0e, 0x11, 0x01, 0x02, 0x04, 0x08, 0x1f),
+ /* 3 */ G(0x1f, 0x02, 0x04, 0x02, 0x01, 0x11, 0x0e),
+ /* 4 */ G(0x02, 0x06, 0x0a, 0x12, 0x1f, 0x02, 0x02),
+ /* 5 */ G(0x1f, 0x10, 0x1e, 0x01, 0x01, 0x11, 0x0e),
+ /* 6 */ G(0x06, 0x08, 0x10, 0x1e, 0x11, 0x11, 0x0e),
+ /* 7 */ G(0x1f, 0x01, 0x02, 0x04, 0x08, 0x08, 0x08),
+ /* 8 */ G(0x0e, 0x11, 0x11, 0x0e, 0x11, 0x11, 0x0e),
+ /* 9 */ G(0x0e, 0x11, 0x11, 0x0f, 0x01, 0x02, 0x0c),
+ /* : */ G(0x00, 0x0c, 0x0c, 0x00, 0x0c, 0x0c, 0x00),
+ /* ; */ G(0x00, 0x0c, 0x0c, 0x00, 0x0c, 0x04, 0x08),
+ /* < */ G(0x02, 0x04, 0x08, 0x10, 0x08, 0x04, 0x02),
+ /* = */ G(0x00, 0x00, 0x1f, 0x00, 0x1f, 0x00, 0x00),
+ /* > */ G(0x08, 0x04, 0x02, 0x01, 0x02, 0x04, 0x08),
+ /* ? */ G(0x0e, 0x11, 0x01, 0x02, 0x04, 0x00, 0x04),
+ /* @ */ G(0x0e, 0x11, 0x17, 0x15, 0x17, 0x10, 0x0e),
+ /* A */ G(0x0e, 0x11, 0x11, 0x1f, 0x11, 0x11, 0x11),
+ /* B */ G(0x1e, 0x11, 0x11, 0x1e, 0x11, 0x11, 0x1e),
+ /* C */ G(0x0e, 0x11, 0x10, 0x10, 0x10, 0x11, 0x0e),
+ /* D */ G(0x1c, 0x12, 0x11, 0x11, 0x11, 0x12, 0x1c),
+ /* E */ G(0x1f, 0x10, 0x10, 0x1e, 0x10, 0x10, 0x1f),
+ /* F */ G(0x1f, 0x10, 0x10, 0x1e, 0x10, 0x10, 0x10),
+ /* G */ G(0x0e, 0x11, 0x10, 0x17, 0x11, 0x11, 0x0f),
+ /* H */ G(0x11, 0x11, 0x11, 0x1f, 0x11, 0x11, 0x11),
+ /* I */ G(0x0e, 0x04, 0x04, 0x04, 0x04, 0x04, 0x0e),
+ /* J */ G(0x07, 0x02, 0x02, 0x02, 0x02, 0x12, 0x0c),
+ /* K */ G(0x11, 0x12, 0x14, 0x18, 0x14, 0x12, 0x11),
+ /* L */ G(0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x1f),
+ /* M */ G(0x11, 0x1b, 0x15, 0x15, 0x11, 0x11, 0x11),
+ /* N */ G(0x11, 0x11, 0x19, 0x15, 0x13, 0x11, 0x11),
+ /* O */ G(0x0e, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0e),
+ /* P */ G(0x1e, 0x11, 0x11, 0x1e, 0x10, 0x10, 0x10),
+ /* Q */ G(0x0e, 0x11, 0x11, 0x11, 0x15, 0x12, 0x0d),
+ /* R */ G(0x1e, 0x11, 0x11, 0x1e, 0x14, 0x12, 0x11),
+ /* S */ G(0x0f, 0x10, 0x10, 0x0e, 0x01, 0x01, 0x1e),
+ /* T */ G(0x1f, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04),
+ /* U */ G(0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0e),
+ /* V */ G(0x11, 0x11, 0x11, 0x11, 0x11, 0x0a, 0x04),
+ /* W */ G(0x11, 0x11, 0x11, 0x15, 0x15, 0x1b, 0x11),
+ /* X */ G(0x11, 0x11, 0x0a, 0x04, 0x0a, 0x11, 0x11),
+ /* Y */ G(0x11, 0x11, 0x0a, 0x04, 0x04, 0x04, 0x04),
+ /* Z */ G(0x1f, 0x01, 0x02, 0x04, 0x08, 0x10, 0x1f),
+ /* [ */ G(0x0e, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0e),
+ /* \ */ G(0x10, 0x08, 0x08, 0x04, 0x02, 0x02, 0x01),
+ /* ] */ G(0x0e, 0x02, 0x02, 0x02, 0x02, 0x02, 0x0e),
+ /* ^ */ G(0x04, 0x0a, 0x11, 0x00, 0x00, 0x00, 0x00),
+ /* _ */ G(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f),
+ /* ` */ G(0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00),
+ /* a */ G(0x00, 0x00, 0x0e, 0x01, 0x0f, 0x11, 0x0f),
+ /* b */ G(0x10, 0x10, 0x1e, 0x11, 0x11, 0x11, 0x1e),
+ /* c */ G(0x00, 0x00, 0x0e, 0x10, 0x10, 0x11, 0x0e),
+ /* d */ G(0x01, 0x01, 0x0f, 0x11, 0x11, 0x11, 0x0f),
+ /* e */ G(0x00, 0x00, 0x0e, 0x11, 0x1f, 0x10, 0x0e),
+ /* f */ G(0x06, 0x09, 0x08, 0x1e, 0x08, 0x08, 0x08),
+ /* g */ G(0x00, 0x00, 0x0e, 0x11, 0x0f, 0x01, 0x0e),
+ /* h */ G(0x10, 0x10, 0x1e, 0x11, 0x11, 0x11, 0x11),
+ /* i */ G(0x04, 0x00, 0x0c, 0x04, 0x04, 0x04, 0x0e),
+ /* j */ G(0x02, 0x00, 0x06, 0x02, 0x02, 0x12, 0x0c),
+ /* k */ G(0x10, 0x10, 0x12, 0x14, 0x18, 0x14, 0x12),
+ /* l */ G(0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x0e),
+ /* m */ G(0x00, 0x00, 0x1a, 0x15, 0x15, 0x11, 0x11),
+ /* n */ G(0x00, 0x00, 0x1e, 0x11, 0x11, 0x11, 0x11),
+ /* o */ G(0x00, 0x00, 0x0e, 0x11, 0x11, 0x11, 0x0e),
+ /* p */ G(0x00, 0x00, 0x1e, 0x11, 0x1e, 0x10, 0x10),
+ /* q */ G(0x00, 0x00, 0x0f, 0x11, 0x0f, 0x01, 0x01),
+ /* r */ G(0x00, 0x00, 0x16, 0x19, 0x10, 0x10, 0x10),
+ /* s */ G(0x00, 0x00, 0x0f, 0x10, 0x0e, 0x01, 0x1e),
+ /* t */ G(0x08, 0x08, 0x1e, 0x08, 0x08, 0x09, 0x06),
+ /* u */ G(0x00, 0x00, 0x11, 0x11, 0x11, 0x13, 0x0d),
+ /* v */ G(0x00, 0x00, 0x11, 0x11, 0x11, 0x0a, 0x04),
+ /* w */ G(0x00, 0x00, 0x11, 0x11, 0x15, 0x15, 0x0a),
+ /* x */ G(0x00, 0x00, 0x11, 0x0a, 0x04, 0x0a, 0x11),
+ /* y */ G(0x00, 0x00, 0x11, 0x11, 0x0f, 0x01, 0x0e),
+ /* z */ G(0x00, 0x00, 0x1f, 0x02, 0x04, 0x08, 0x1f),
+ /* { */ G(0x03, 0x04, 0x04, 0x08, 0x04, 0x04, 0x03),
+ /* | */ G(0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04),
+ /* } */ G(0x18, 0x04, 0x04, 0x02, 0x04, 0x04, 0x18),
+ /* ~ */ G(0x00, 0x09, 0x15, 0x12, 0x00, 0x00, 0x00),
+};
+
+static const unsigned char MISSING[7] = {0x1f, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x1f};
+
+static void put(const vix_canvas *c, int x, int y, vix_rgb col) {
+ if (x < 0 || y < 0 || x >= c->w || y >= c->h)
+ return;
+ unsigned char *p = c->px + ((size_t)y * c->w + x) * 3;
+ p[0] = col.r;
+ p[1] = col.g;
+ p[2] = col.b;
+}
+
+void vix_draw_fill(const vix_canvas *c, int x, int y, int w, int h, vix_rgb col) {
+ if (w <= 0 || h <= 0)
+ return;
+ int x0 = x < 0 ? 0 : x, y0 = y < 0 ? 0 : y;
+ int x1 = x + w > c->w ? c->w : x + w;
+ int y1 = y + h > c->h ? c->h : y + h;
+ if (x0 >= x1 || y0 >= y1)
+ return;
+ for (int yy = y0; yy < y1; yy++) {
+ unsigned char *p = c->px + ((size_t)yy * c->w + x0) * 3;
+ for (int xx = x0; xx < x1; xx++) {
+ p[0] = col.r;
+ p[1] = col.g;
+ p[2] = col.b;
+ p += 3;
+ }
+ }
+}
+
+void vix_draw_rect(const vix_canvas *c, int x, int y, int w, int h, vix_rgb col, int thickness) {
+ if (thickness < 1)
+ thickness = 1;
+ if (w <= 0 || h <= 0)
+ return;
+ int t = thickness;
+ if (t > h)
+ t = h;
+ vix_draw_fill(c, x, y, w, t, col);
+ vix_draw_fill(c, x, y + h - t, w, t, col);
+ int t2 = thickness > w ? w : thickness;
+ vix_draw_fill(c, x, y, t2, h, col);
+ vix_draw_fill(c, x + w - t2, y, t2, h, col);
+}
+
+void vix_draw_dot(const vix_canvas *c, int cx, int cy, int r, vix_rgb col) {
+ for (int dy = -r; dy <= r; dy++)
+ for (int dx = -r; dx <= r; dx++)
+ if (dx * dx + dy * dy <= r * r + r)
+ put(c, cx + dx, cy + dy, col);
+}
+
+int vix_text_width(const char *s, int scale) {
+ if (scale < 1)
+ scale = 1;
+ return (int)strlen(s) * 6 * scale;
+}
+
+int vix_draw_text(const vix_canvas *c, int x, int y, const char *s, vix_rgb col, int scale) {
+ if (scale < 1)
+ scale = 1;
+ int pen = x;
+ for (const unsigned char *p = (const unsigned char *)s; *p; p++) {
+ const unsigned char *glyph = (*p >= 32 && *p <= 126) ? FONT[*p - 32] : MISSING;
+ for (int row = 0; row < 7; row++) {
+ unsigned bits = glyph[row];
+ for (int col_i = 0; col_i < 5; col_i++) {
+ if (!(bits & (1u << (4 - col_i))))
+ continue;
+ vix_draw_fill(c, pen + col_i * scale, y + row * scale, scale, scale, col);
+ }
+ }
+ pen += 6 * scale;
+ }
+ return pen - x;
+}