Skip to content

LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.

5.8 KB
Raw Blame
/* draw.c — lordos-testbench's pixel drawing: boxes, circles, outlines and a 5×7 bitmap font (no font
 * files, so what it shows doesn't depend on anything the compositor renders). */
#include "draw.h"

#include <ctype.h>
#include <string.h>

/* 5×7 glyphs, bit 4 = left column; ASCII 32..95 (lowercase is drawn as uppercase). */
static const uint8_t FONT[64][7] = {
    [' ' - 32] = { 0, 0, 0, 0, 0, 0, 0 },
    ['!' - 32] = { 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x04 }, ['"' - 32] = { 0x0A, 0x0A, 0x0A, 0, 0, 0, 0 },
    ['#' - 32] = { 0x0A, 0x0A, 0x1F, 0x0A, 0x1F, 0x0A, 0x0A }, ['$' - 32] = { 0x04, 0x0F, 0x14, 0x0E, 0x05, 0x1E, 0x04 },
    ['%' - 32] = { 0x18, 0x19, 0x02, 0x04, 0x08, 0x13, 0x03 }, ['&' - 32] = { 0x0C, 0x12, 0x14, 0x08, 0x15, 0x12, 0x0D },
    ['\'' - 32] = { 0x0C, 0x04, 0x08, 0, 0, 0, 0 }, ['(' - 32] = { 0x02, 0x04, 0x08, 0x08, 0x08, 0x04, 0x02 },
    [')' - 32] = { 0x08, 0x04, 0x02, 0x02, 0x02, 0x04, 0x08 }, ['*' - 32] = { 0x00, 0x04, 0x15, 0x0E, 0x15, 0x04, 0x00 },
    ['+' - 32] = { 0x00, 0x04, 0x04, 0x1F, 0x04, 0x04, 0x00 }, [',' - 32] = { 0, 0, 0, 0, 0x0C, 0x04, 0x08 },
    ['-' - 32] = { 0, 0, 0, 0x1F, 0, 0, 0 }, ['.' - 32] = { 0, 0, 0, 0, 0, 0x0C, 0x0C },
    ['/' - 32] = { 0x01, 0x01, 0x02, 0x04, 0x08, 0x10, 0x10 },
    ['0' - 32] = { 0x0E, 0x11, 0x13, 0x15, 0x19, 0x11, 0x0E }, ['1' - 32] = { 0x04, 0x0C, 0x04, 0x04, 0x04, 0x04, 0x0E },
    ['2' - 32] = { 0x0E, 0x11, 0x01, 0x02, 0x04, 0x08, 0x1F }, ['3' - 32] = { 0x1F, 0x02, 0x04, 0x02, 0x01, 0x11, 0x0E },
    ['4' - 32] = { 0x02, 0x06, 0x0A, 0x12, 0x1F, 0x02, 0x02 }, ['5' - 32] = { 0x1F, 0x10, 0x1E, 0x01, 0x01, 0x11, 0x0E },
    ['6' - 32] = { 0x06, 0x08, 0x10, 0x1E, 0x11, 0x11, 0x0E }, ['7' - 32] = { 0x1F, 0x01, 0x02, 0x04, 0x08, 0x08, 0x08 },
    ['8' - 32] = { 0x0E, 0x11, 0x11, 0x0E, 0x11, 0x11, 0x0E }, ['9' - 32] = { 0x0E, 0x11, 0x11, 0x0F, 0x01, 0x02, 0x0C },
    [':' - 32] = { 0x00, 0x0C, 0x0C, 0x00, 0x0C, 0x0C, 0x00 }, [';' - 32] = { 0x00, 0x0C, 0x0C, 0x00, 0x0C, 0x04, 0x08 },
    ['<' - 32] = { 0x02, 0x04, 0x08, 0x10, 0x08, 0x04, 0x02 }, ['=' - 32] = { 0x00, 0x00, 0x1F, 0x00, 0x1F, 0x00, 0x00 },
    ['>' - 32] = { 0x08, 0x04, 0x02, 0x01, 0x02, 0x04, 0x08 }, ['?' - 32] = { 0x0E, 0x11, 0x01, 0x02, 0x04, 0x00, 0x04 },
    ['@' - 32] = { 0x0E, 0x11, 0x17, 0x15, 0x17, 0x10, 0x0E },
    ['A' - 32] = { 0x0E, 0x11, 0x11, 0x1F, 0x11, 0x11, 0x11 }, ['B' - 32] = { 0x1E, 0x11, 0x11, 0x1E, 0x11, 0x11, 0x1E },
    ['C' - 32] = { 0x0E, 0x11, 0x10, 0x10, 0x10, 0x11, 0x0E }, ['D' - 32] = { 0x1C, 0x12, 0x11, 0x11, 0x11, 0x12, 0x1C },
    ['E' - 32] = { 0x1F, 0x10, 0x10, 0x1E, 0x10, 0x10, 0x1F }, ['F' - 32] = { 0x1F, 0x10, 0x10, 0x1E, 0x10, 0x10, 0x10 },
    ['G' - 32] = { 0x0E, 0x11, 0x10, 0x17, 0x11, 0x11, 0x0F }, ['H' - 32] = { 0x11, 0x11, 0x11, 0x1F, 0x11, 0x11, 0x11 },
    ['I' - 32] = { 0x0E, 0x04, 0x04, 0x04, 0x04, 0x04, 0x0E }, ['J' - 32] = { 0x07, 0x02, 0x02, 0x02, 0x02, 0x12, 0x0C },
    ['K' - 32] = { 0x11, 0x12, 0x14, 0x18, 0x14, 0x12, 0x11 }, ['L' - 32] = { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x1F },
    ['M' - 32] = { 0x11, 0x1B, 0x15, 0x15, 0x11, 0x11, 0x11 }, ['N' - 32] = { 0x11, 0x11, 0x19, 0x15, 0x13, 0x11, 0x11 },
    ['O' - 32] = { 0x0E, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0E }, ['P' - 32] = { 0x1E, 0x11, 0x11, 0x1E, 0x10, 0x10, 0x10 },
    ['Q' - 32] = { 0x0E, 0x11, 0x11, 0x11, 0x15, 0x12, 0x0D }, ['R' - 32] = { 0x1E, 0x11, 0x11, 0x1E, 0x14, 0x12, 0x11 },
    ['S' - 32] = { 0x0F, 0x10, 0x10, 0x0E, 0x01, 0x01, 0x1E }, ['T' - 32] = { 0x1F, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04 },
    ['U' - 32] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0E }, ['V' - 32] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x0A, 0x04 },
    ['W' - 32] = { 0x11, 0x11, 0x11, 0x15, 0x15, 0x15, 0x0A }, ['X' - 32] = { 0x11, 0x11, 0x0A, 0x04, 0x0A, 0x11, 0x11 },
    ['Y' - 32] = { 0x11, 0x11, 0x0A, 0x04, 0x04, 0x04, 0x04 }, ['Z' - 32] = { 0x1F, 0x01, 0x02, 0x04, 0x08, 0x10, 0x1F },
    ['[' - 32] = { 0x0E, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0E }, ['\\' - 32] = { 0x10, 0x10, 0x08, 0x04, 0x02, 0x01, 0x01 },
    [']' - 32] = { 0x0E, 0x02, 0x02, 0x02, 0x02, 0x02, 0x0E }, ['^' - 32] = { 0x04, 0x0A, 0x11, 0, 0, 0, 0 },
    ['_' - 32] = { 0, 0, 0, 0, 0, 0, 0x1F },
};

void d_fill(Surf *s, int x, int y, int w, int h, uint32_t c) {
    int x0 = x < 0 ? 0 : x, y0 = y < 0 ? 0 : y, x1 = x + w > s->w ? s->w : x + w, y1 = y + h > s->h ? s->h : y + h;
    for (int j = y0; j < y1; j++) {
        uint32_t *row = s->px + (size_t)j * s->stride;
        for (int i = x0; i < x1; i++) row[i] = c;
    }
}

void d_frame(Surf *s, int x, int y, int w, int h, int t, uint32_t c) {
    d_fill(s, x, y, w, t, c);
    d_fill(s, x, y + h - t, w, t, c);
    d_fill(s, x, y, t, h, c);
    d_fill(s, x + w - t, y, t, h, c);
}

void d_circle(Surf *s, int cx, int cy, int r, uint32_t c) {
    for (int y = -r; y <= r; y++)
        for (int x = -r; x <= r; x++)
            if (x * x + y * y <= r * r) {
                int px = cx + x, py = cy + y;
                if (px >= 0 && px < s->w && py >= 0 && py < s->h) s->px[(size_t)py * s->stride + px] = c;
            }
}

int d_text(Surf *s, int x, int y, int scale, uint32_t c, const char *t) {
    int x0 = x;
    for (; *t; t++) {
        int ch = toupper((unsigned char)*t);
        if (ch == '`') ch = '\'';              /* the grave accent: drawn as the apostrophe */
        if (ch < 32 || ch > 95) ch = '?';
        const uint8_t *g = FONT[ch - 32];
        for (int row = 0; row < 7; row++)
            for (int col = 0; col < 5; col++)
                if (g[row] & (1 << (4 - col))) d_fill(s, x + col * scale, y + row * scale, scale, scale, c);
        x += 6 * scale;
    }
    return x - x0;
}

int d_text_w(const char *t, int scale) { int n = (int)strlen(t); return n ? n * 6 * scale - scale : 0; }

void d_text_c(Surf *s, int cx, int cy, int scale, uint32_t c, const char *t) {
    d_text(s, cx - d_text_w(t, scale) / 2, cy - 7 * scale / 2, scale, c, t);
}