hud.c

C source for A clock on a round ESP32 display, 1213 lines. Raw file

#include "hud.h"

#include <math.h>
#include <stdlib.h>
#include <string.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#define R2 ((HUD_R) * (HUD_R))

static uint32_t *s_tile_rows;
static int s_tile_on;
static uint32_t s_erase_rng = 0xA5A5C3C3u;

/* sqrt(i/255)*255, for coverage alpha. 0 stays 0, 255 stays 255. */
static const uint8_t k_aa_lut[256] = {
      0,  16,  23,  28,  32,  36,  39,  42,  45,  48,  50,  53,  55,  58,  60,  62,
     64,  66,  68,  70,  71,  73,  75,  77,  78,  80,  81,  83,  84,  86,  87,  89,
     90,  92,  93,  94,  96,  97,  98, 100, 101, 102, 103, 105, 106, 107, 108, 109,
    111, 112, 113, 114, 115, 116, 117, 118, 119, 121, 122, 123, 124, 125, 126, 127,
    128, 129, 130, 131, 132, 133, 134, 135, 135, 136, 137, 138, 139, 140, 141, 142,
    143, 144, 145, 145, 146, 147, 148, 149, 150, 151, 151, 152, 153, 154, 155, 156,
    156, 157, 158, 159, 160, 160, 161, 162, 163, 164, 164, 165, 166, 167, 167, 168,
    169, 170, 170, 171, 172, 173, 173, 174, 175, 176, 176, 177, 178, 179, 179, 180,
    181, 181, 182, 183, 183, 184, 185, 186, 186, 187, 188, 188, 189, 190, 190, 191,
    192, 192, 193, 194, 194, 195, 196, 196, 197, 198, 198, 199, 199, 200, 201, 201,
    202, 203, 203, 204, 204, 205, 206, 206, 207, 208, 208, 209, 209, 210, 211, 211,
    212, 212, 213, 214, 214, 215, 215, 216, 217, 217, 218, 218, 219, 220, 220, 221,
    221, 222, 222, 223, 224, 224, 225, 225, 226, 226, 227, 228, 228, 229, 229, 230,
    230, 231, 231, 232, 233, 233, 234, 234, 235, 235, 236, 236, 237, 237, 238, 238,
    239, 240, 240, 241, 241, 242, 242, 243, 243, 244, 244, 245, 245, 246, 246, 247,
    247, 248, 248, 249, 249, 250, 250, 251, 251, 252, 252, 253, 253, 254, 254, 255,
};

static void hud_touch(int x, int y)
{
    if (s_tile_on) {
        s_tile_rows[y >> 4] |= 1u << (x >> 4);
    }
}

static uint16_t pack565(uint8_t r, uint8_t g, uint8_t b)
{
    uint8_t r5 = r >> 3;
    uint8_t g6 = g >> 2;
    uint8_t b5 = b >> 3;
    uint16_t c = (uint16_t)((r5 << 11) | (g6 << 5) | b5);
    return (uint16_t)((c << 8) | (c >> 8));
}

/* 5x7 glyphs, 5 columns, bit 0 = top. ASCII 32..90. */
static const uint8_t k_font[][5] = {
    {0x00, 0x00, 0x00, 0x00, 0x00}, /* 32 space */
    {0x00, 0x00, 0x5F, 0x00, 0x00}, /* 33 ! */
    {0x00, 0x07, 0x00, 0x07, 0x00}, /* 34 " */
    {0x14, 0x7F, 0x14, 0x7F, 0x14}, /* 35 # */
    {0x24, 0x2A, 0x7F, 0x2A, 0x12}, /* 36 $ */
    {0x23, 0x13, 0x08, 0x64, 0x62}, /* 37 % */
    {0x36, 0x49, 0x55, 0x22, 0x50}, /* 38 & */
    {0x00, 0x05, 0x03, 0x00, 0x00}, /* 39 ' */
    {0x00, 0x1C, 0x22, 0x41, 0x00}, /* 40 ( */
    {0x00, 0x41, 0x22, 0x1C, 0x00}, /* 41 ) */
    {0x14, 0x08, 0x3E, 0x08, 0x14}, /* 42 * */
    {0x08, 0x08, 0x3E, 0x08, 0x08}, /* 43 + */
    {0x00, 0x50, 0x30, 0x00, 0x00}, /* 44 , */
    {0x08, 0x08, 0x08, 0x08, 0x08}, /* 45 - */
    {0x00, 0x60, 0x60, 0x00, 0x00}, /* 46 . */
    {0x20, 0x10, 0x08, 0x04, 0x02}, /* 47 / */
    {0x3E, 0x51, 0x49, 0x45, 0x3E}, /* 48 0 */
    {0x00, 0x42, 0x7F, 0x40, 0x00}, /* 49 1 */
    {0x42, 0x61, 0x59, 0x49, 0x46}, /* 50 2 */
    {0x21, 0x41, 0x45, 0x4B, 0x31}, /* 51 3 */
    {0x18, 0x14, 0x12, 0x7F, 0x10}, /* 52 4 */
    {0x27, 0x45, 0x45, 0x45, 0x39}, /* 53 5 */
    {0x3C, 0x4A, 0x49, 0x49, 0x30}, /* 54 6 */
    {0x01, 0x71, 0x09, 0x05, 0x03}, /* 55 7 */
    {0x36, 0x49, 0x49, 0x49, 0x36}, /* 56 8 */
    {0x06, 0x49, 0x49, 0x29, 0x1E}, /* 57 9 */
    {0x00, 0x36, 0x36, 0x00, 0x00}, /* 58 : */
    {0x00, 0x56, 0x36, 0x00, 0x00}, /* 59 ; */
    {0x08, 0x14, 0x22, 0x41, 0x00}, /* 60 < */
    {0x14, 0x14, 0x14, 0x14, 0x14}, /* 61 = */
    {0x00, 0x41, 0x22, 0x14, 0x08}, /* 62 > */
    {0x02, 0x01, 0x51, 0x09, 0x06}, /* 63 ? */
    {0x32, 0x49, 0x79, 0x41, 0x3E}, /* 64 @ */
    {0x7E, 0x11, 0x11, 0x11, 0x7E}, /* 65 A */
    {0x7F, 0x49, 0x49, 0x49, 0x36}, /* 66 B */
    {0x3E, 0x41, 0x41, 0x41, 0x22}, /* 67 C */
    {0x7F, 0x41, 0x41, 0x22, 0x1C}, /* 68 D */
    {0x7F, 0x49, 0x49, 0x49, 0x41}, /* 69 E */
    {0x7F, 0x09, 0x09, 0x09, 0x01}, /* 70 F */
    {0x3E, 0x41, 0x49, 0x49, 0x7A}, /* 71 G */
    {0x7F, 0x08, 0x08, 0x08, 0x7F}, /* 72 H */
    {0x00, 0x41, 0x7F, 0x41, 0x00}, /* 73 I */
    {0x20, 0x40, 0x41, 0x3F, 0x01}, /* 74 J */
    {0x7F, 0x08, 0x14, 0x22, 0x41}, /* 75 K */
    {0x7F, 0x40, 0x40, 0x40, 0x40}, /* 76 L */
    {0x7F, 0x02, 0x0C, 0x02, 0x7F}, /* 77 M */
    {0x7F, 0x04, 0x08, 0x10, 0x7F}, /* 78 N */
    {0x3E, 0x41, 0x41, 0x41, 0x3E}, /* 79 O */
    {0x7F, 0x09, 0x09, 0x09, 0x06}, /* 80 P */
    {0x3E, 0x41, 0x51, 0x21, 0x5E}, /* 81 Q */
    {0x7F, 0x09, 0x19, 0x29, 0x46}, /* 82 R */
    {0x46, 0x49, 0x49, 0x49, 0x31}, /* 83 S */
    {0x01, 0x01, 0x7F, 0x01, 0x01}, /* 84 T */
    {0x3F, 0x40, 0x40, 0x40, 0x3F}, /* 85 U */
    {0x1F, 0x20, 0x40, 0x20, 0x1F}, /* 86 V */
    {0x3F, 0x40, 0x38, 0x40, 0x3F}, /* 87 W */
    {0x63, 0x14, 0x08, 0x14, 0x63}, /* 88 X */
    {0x07, 0x08, 0x70, 0x08, 0x07}, /* 89 Y */
    {0x61, 0x51, 0x49, 0x45, 0x43}, /* 90 Z */
};

static int in_disk(int x, int y)
{
    int dx = x - HUD_CX;
    int dy = y - HUD_CY;
    return dx * dx + dy * dy <= R2;
}

static float cover01(float v)
{
    if (v <= 0.0f) {
        return 0.0f;
    }
    if (v >= 1.0f) {
        return 1.0f;
    }
    return v;
}

static void hud_pixel_cov(uint8_t *rgb, int x, int y, uint8_t r, uint8_t g, uint8_t b, uint8_t a, float cov)
{
    if (cov <= 0.0f || a == 0) {
        return;
    }
    if (cov >= 1.0f) {
        hud_pixel(rgb, x, y, r, g, b, a);
        return;
    }
    hud_pixel(rgb, x, y, r, g, b, (uint8_t)((float)a * cov + 0.5f));
}

void hud_pixel(uint8_t *rgb, int x, int y, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    if ((unsigned)x >= HUD_W || (unsigned)y >= HUD_H || a == 0) {
        return;
    }
    if (!in_disk(x, y)) {
        return;
    }
    hud_touch(x, y);
    uint8_t *p = rgb + (y * HUD_W + x) * 3;
    if (a >= 255) {
        p[0] = r;
        p[1] = g;
        p[2] = b;
        return;
    }
    a = k_aa_lut[a];
    unsigned ia = 255u - a;
    p[0] = (uint8_t)((p[0] * ia + r * a) / 255u);
    p[1] = (uint8_t)((p[1] * ia + g * a) / 255u);
    p[2] = (uint8_t)((p[2] * ia + b * a) / 255u);
}

void hud_clear(uint8_t *rgb, uint8_t r, uint8_t g, uint8_t b)
{
    for (int y = 0; y < HUD_H; y++) {
        for (int x = 0; x < HUD_W; x++) {
            if (in_disk(x, y)) {
                uint8_t *p = rgb + (y * HUD_W + x) * 3;
                p[0] = r;
                p[1] = g;
                p[2] = b;
            } else {
                uint8_t *p = rgb + (y * HUD_W + x) * 3;
                p[0] = 0;
                p[1] = 0;
                p[2] = 0;
            }
        }
    }
}

static void span_add_x(float x, float *xl, float *xr, int *hit)
{
    if (!*hit) {
        *xl = x;
        *xr = x;
        *hit = 1;
        return;
    }
    if (x < *xl) {
        *xl = x;
    }
    if (x > *xr) {
        *xr = x;
    }
}

static void span_add_circle(float cx, float cy, float rad, float y,
                            float *xl, float *xr, int *hit)
{
    float d = y - cy;
    float rr = rad + 1.0f;
    float s = rr * rr - d * d;
    if (s < 0.0f) {
        return;
    }
    float e = sqrtf(s);
    span_add_x(cx - e, xl, xr, hit);
    span_add_x(cx + e, xl, xr, hit);
}

static void span_add_seg(float x0, float y0, float x1, float y1, float y,
                         float *xl, float *xr, int *hit)
{
    float dy = y1 - y0;
    if (fabsf(dy) < 1e-5f) {
        if (fabsf(y - y0) <= 1.25f) {
            span_add_x(fminf(x0, x1), xl, xr, hit);
            span_add_x(fmaxf(x0, x1), xl, xr, hit);
        }
        return;
    }
    float ymin = fminf(y0, y1) - 1.25f;
    float ymax = fmaxf(y0, y1) + 1.25f;
    if (y < ymin || y > ymax) {
        return;
    }
    float t = (y - y0) / dy;
    if (t < -0.08f || t > 1.08f) {
        return;
    }
    span_add_x(x0 + t * (x1 - x0), xl, xr, hit);
}

void hud_line_r2(uint8_t *rgb, float x0, float y0, float r0,
                 float x1, float y1, float r1,
                 uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    if (a == 0) {
        return;
    }
    if (r0 < 0.0f) {
        r0 = 0.0f;
    }
    if (r1 < 0.0f) {
        r1 = 0.0f;
    }
    float dx = x1 - x0;
    float dy = y1 - y0;
    float len2 = dx * dx + dy * dy;
    float pad = fmaxf(r0, r1) + 1.5f;
    int miny = (int)floorf(fminf(y0, y1) - pad);
    int maxy = (int)ceilf(fmaxf(y0, y1) + pad);
    if (miny < 0) {
        miny = 0;
    }
    if (maxy > HUD_H - 1) {
        maxy = HUD_H - 1;
    }

    if (len2 < 1e-8f) {
        float rad = fmaxf(r0, r1);
        int minx = (int)floorf(x0 - pad);
        int maxx = (int)ceilf(x0 + pad);
        if (minx < 0) {
            minx = 0;
        }
        if (maxx > HUD_W - 1) {
            maxx = HUD_W - 1;
        }
        for (int y = miny; y <= maxy; y++) {
            for (int x = minx; x <= maxx; x++) {
                float d = hypotf((float)x - x0, (float)y - y0);
                hud_pixel_cov(rgb, x, y, r, g, b, a, cover01((rad + 0.5f) - d));
            }
        }
        return;
    }

    float len = sqrtf(len2);
    float inv = 1.0f / len2;
    float nx = -dy / len;
    float ny = dx / len;
    float ax0 = x0 + nx * r0, ay0 = y0 + ny * r0;
    float ax1 = x1 + nx * r1, ay1 = y1 + ny * r1;
    float bx0 = x0 - nx * r0, by0 = y0 - ny * r0;
    float bx1 = x1 - nx * r1, by1 = y1 - ny * r1;

    for (int y = miny; y <= maxy; y++) {
        float py = (float)y;
        float xl = 0.0f, xr = 0.0f;
        int hit = 0;
        span_add_circle(x0, y0, r0, py, &xl, &xr, &hit);
        span_add_circle(x1, y1, r1, py, &xl, &xr, &hit);
        span_add_seg(ax0, ay0, ax1, ay1, py, &xl, &xr, &hit);
        span_add_seg(bx0, by0, bx1, by1, py, &xl, &xr, &hit);
        span_add_seg(ax0, ay0, bx0, by0, py, &xl, &xr, &hit);
        span_add_seg(ax1, ay1, bx1, by1, py, &xl, &xr, &hit);
        if (!hit) {
            continue;
        }
        int x_lo = (int)floorf(xl - 1.0f);
        int x_hi = (int)ceilf(xr + 1.0f);
        if (x_lo < 0) {
            x_lo = 0;
        }
        if (x_hi > HUD_W - 1) {
            x_hi = HUD_W - 1;
        }
        for (int x = x_lo; x <= x_hi; x++) {
            float px = (float)x;
            float t = ((px - x0) * dx + (py - y0) * dy) * inv;
            if (t < 0.0f) {
                t = 0.0f;
            } else if (t > 1.0f) {
                t = 1.0f;
            }
            float qx = x0 + t * dx;
            float qy = y0 + t * dy;
            float ddx = px - qx;
            float ddy = py - qy;
            float d2 = ddx * ddx + ddy * ddy;
            float rad = r0 + t * (r1 - r0);
            float outer = rad + 0.5f;
            if (d2 >= outer * outer) {
                continue;
            }
            float inner = rad - 0.5f;
            if (inner > 0.0f && d2 <= inner * inner) {
                hud_pixel(rgb, x, y, r, g, b, a);
            } else {
                hud_pixel_cov(rgb, x, y, r, g, b, a, cover01(outer - sqrtf(d2)));
            }
        }
    }
}

void hud_line_r(uint8_t *rgb, float x0, float y0, float x1, float y1, float rad,
                uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    hud_line_r2(rgb, x0, y0, rad, x1, y1, rad, r, g, b, a);
}

void hud_line(uint8_t *rgb, int x0, int y0, int x1, int y1, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    hud_line_r(rgb, (float)x0, (float)y0, (float)x1, (float)y1, 0.5f, r, g, b, a);
}

void hud_circle(uint8_t *rgb, int cx, int cy, int rad, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    hud_ring(rgb, cx, cy, (float)rad, 1.15f, r, g, b, a);
}

void hud_fill_circle(uint8_t *rgb, int cx, int cy, int rad, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    float radf = (float)rad;
    int rmax = rad + 1;
    for (int y = -rmax; y <= rmax; y++) {
        for (int x = -rmax; x <= rmax; x++) {
            float d = sqrtf((float)(x * x + y * y));
            hud_pixel_cov(rgb, cx + x, cy + y, r, g, b, a, cover01(radf + 0.5f - d));
        }
    }
}

void hud_fill_rect(uint8_t *rgb, int x, int y, int w, int h,
                   uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    if (w <= 0 || h <= 0) {
        return;
    }
    float l = (float)x;
    float rr = (float)(x + w);
    float t = (float)y;
    float btm = (float)(y + h);
    int minx = x - 1;
    int maxx = x + w;
    int miny = y - 1;
    int maxy = y + h;
    for (int py = miny; py <= maxy; py++) {
        for (int px = minx; px <= maxx; px++) {
            float x0 = fmaxf(l, (float)px - 0.5f);
            float x1 = fminf(rr, (float)px + 0.5f);
            float y0 = fmaxf(t, (float)py - 0.5f);
            float y1 = fminf(btm, (float)py + 0.5f);
            float cov = (x1 - x0) * (y1 - y0);
            hud_pixel_cov(rgb, px, py, r, g, b, a, cover01(cov));
        }
    }
}

void hud_rect(uint8_t *rgb, int x, int y, int w, int h,
              uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    if (w <= 0 || h <= 0) {
        return;
    }
    hud_line(rgb, x, y, x + w - 1, y, r, g, b, a);
    hud_line(rgb, x, y + h - 1, x + w - 1, y + h - 1, r, g, b, a);
    hud_line(rgb, x, y, x, y + h - 1, r, g, b, a);
    hud_line(rgb, x + w - 1, y, x + w - 1, y + h - 1, r, g, b, a);
}

void hud_ring(uint8_t *rgb, int cx, int cy, float rad, float thick, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    float inner = rad - thick * 0.5f;
    float outer = rad + thick * 0.5f;
    int rmax = (int)(outer + 2.0f);
    for (int y = -rmax; y <= rmax; y++) {
        for (int x = -rmax; x <= rmax; x++) {
            float d = sqrtf((float)(x * x + y * y));
            if (d < inner - 1.0f || d > outer + 1.0f) {
                continue;
            }
            float cov = 1.0f;
            if (d < inner) {
                cov = 1.0f - (inner - d);
            } else if (d > outer) {
                cov = 1.0f - (d - outer);
            }
            if (cov <= 0.0f) {
                continue;
            }
            if (cov > 1.0f) {
                cov = 1.0f;
            }
            hud_pixel(rgb, cx + x, cy + y, r, g, b, (uint8_t)(a * cov));
        }
    }
}

static void arc_add(float ang, float rad, float *mn_x, float *mx_x, float *mn_y, float *mx_y)
{
    float x = cosf(ang) * rad;
    float y = sinf(ang) * rad;
    if (x < *mn_x) {
        *mn_x = x;
    }
    if (x > *mx_x) {
        *mx_x = x;
    }
    if (y < *mn_y) {
        *mn_y = y;
    }
    if (y > *mx_y) {
        *mx_y = y;
    }
}

void hud_arc(uint8_t *rgb, int cx, int cy, float rad, float thick, float a0, float a1,
             uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    while (a1 < a0) {
        a1 += 2.0f * (float)M_PI;
    }
    float inner = rad - thick * 0.5f;
    float outer = rad + thick * 0.5f;
    float span = a1 - a0;
    const float twopi = 2.0f * (float)M_PI;
    float mn_x, mx_x, mn_y, mx_y;
    if (span >= twopi - 0.01f) {
        float rmax = outer + 2.0f;
        mn_x = mn_y = -rmax;
        mx_x = mx_y = rmax;
    } else {
        mn_x = mn_y = 1e9f;
        mx_x = mx_y = -1e9f;
        arc_add(a0, inner, &mn_x, &mx_x, &mn_y, &mx_y);
        arc_add(a0, outer, &mn_x, &mx_x, &mn_y, &mx_y);
        arc_add(a1, inner, &mn_x, &mx_x, &mn_y, &mx_y);
        arc_add(a1, outer, &mn_x, &mx_x, &mn_y, &mx_y);
        static const float k_card[4] = {
            0.0f, (float)M_PI * 0.5f, (float)M_PI, (float)M_PI * 1.5f
        };
        for (int i = 0; i < 4; i++) {
            float c = k_card[i];
            while (c <= a0) {
                c += twopi;
            }
            if (c < a1) {
                arc_add(c, inner, &mn_x, &mx_x, &mn_y, &mx_y);
                arc_add(c, outer, &mn_x, &mx_x, &mn_y, &mx_y);
            }
        }
        mn_x -= 2.0f;
        mx_x += 2.0f;
        mn_y -= 2.0f;
        mx_y += 2.0f;
    }
    int xmin = (int)floorf(mn_x);
    int xmax = (int)ceilf(mx_x);
    int ymin = (int)floorf(mn_y);
    int ymax = (int)ceilf(mx_y);
    for (int y = ymin; y <= ymax; y++) {
        for (int x = xmin; x <= xmax; x++) {
            float d = sqrtf((float)(x * x + y * y));
            if (d < inner - 1.0f || d > outer + 1.0f) {
                continue;
            }
            float ang = atan2f((float)y, (float)x);
            if (ang < 0.0f) {
                ang += 2.0f * (float)M_PI;
            }
            float a0n = a0;
            while (a0n < 0.0f) {
                a0n += 2.0f * (float)M_PI;
            }
            while (a0n >= 2.0f * (float)M_PI) {
                a0n -= 2.0f * (float)M_PI;
            }
            float span = a1 - a0;
            float rel = ang - a0n;
            if (rel < 0.0f) {
                rel += 2.0f * (float)M_PI;
            }
            if (rel > span && span < 2.0f * (float)M_PI - 0.001f) {
                continue;
            }
            float cov = 1.0f;
            if (d < inner) {
                cov = 1.0f - (inner - d);
            } else if (d > outer) {
                cov = 1.0f - (d - outer);
            }
            if (cov <= 0.0f) {
                continue;
            }
            if (cov > 1.0f) {
                cov = 1.0f;
            }
            hud_pixel(rgb, cx + x, cy + y, r, g, b, (uint8_t)(a * cov));
        }
    }
}

int hud_text_width(int scale, const char *s)
{
    int n = (int)strlen(s);
    return n * 6 * scale;
}

void hud_text(uint8_t *rgb, int x, int y, int scale, const char *s,
              uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    if (scale < 1) {
        scale = 1;
    }
    int cx = x;
    for (const char *p = s; *p; p++) {
        unsigned ch = (unsigned char)*p;
        if (ch >= 'a' && ch <= 'z') {
            ch = (unsigned)(ch - 32);
        }
        if (ch < 32 || ch > 90) {
            ch = '?';
        }
        const uint8_t *gcol = k_font[ch - 32];
        for (int col = 0; col < 5; col++) {
            uint8_t bits = gcol[col];
            for (int row = 0; row < 7; row++) {
                if (bits & (1u << row)) {
                    for (int sy = 0; sy < scale; sy++) {
                        for (int sx = 0; sx < scale; sx++) {
                            hud_pixel(rgb, cx + col * scale + sx, y + row * scale + sy, r, g, b, a);
                        }
                    }
                }
            }
        }
        cx += 6 * scale;
    }
}

void hud_text_cx(uint8_t *rgb, int y, int scale, const char *s,
                uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    int w = hud_text_width(scale, s);
    hud_text(rgb, HUD_CX - w / 2, y, scale, s, r, g, b, a);
}

void hud_text_outline(uint8_t *rgb, int x, int y, int scale, const char *s,
                      uint8_t r, uint8_t g, uint8_t b)
{
    static const int k_ox[8] = { -1, 0, 1, -1, 1, -1, 0, 1 };
    static const int k_oy[8] = { -1, -1, -1, 0, 0, 1, 1, 1 };
    for (int i = 0; i < 8; i++) {
        hud_text(rgb, x + k_ox[i], y + k_oy[i], scale, s, 0, 0, 0, 255);
    }
    hud_text(rgb, x, y, scale, s, r, g, b, 255);
}

void hud_text_cx_outline(uint8_t *rgb, int y, int scale, const char *s,
                        uint8_t r, uint8_t g, uint8_t b)
{
    int w = hud_text_width(scale, s);
    hud_text_outline(rgb, HUD_CX - w / 2, y, scale, s, r, g, b);
}

static unsigned hud_ch(unsigned ch)
{
    if (ch >= 'a' && ch <= 'z') {
        ch -= 32;
    }
    if (ch < 32 || ch > 90) {
        ch = '?';
    }
    return ch;
}

static void hud_glyph_polar(uint8_t *rgb, float px, float py, float ca, float sa,
                             unsigned ch, int scale, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    if (scale < 1) {
        scale = 1;
    }
    const uint8_t *gcol = k_font[ch - 32];
    float tx = -sa;
    float ty = ca;
    for (int col = 0; col < 5; col++) {
        uint8_t bits = gcol[col];
        for (int row = 0; row < 7; row++) {
            if ((bits & (1u << row)) == 0) {
                continue;
            }
            for (int sy = 0; sy < scale; sy++) {
                for (int sx = 0; sx < scale; sx++) {
                    float lx = ((float)col - 2.0f) * (float)scale + (float)sx;
                    float ly = (3.0f - (float)row) * (float)scale - (float)sy;
                    int x = (int)lroundf(px + tx * lx + ca * ly);
                    int y = (int)lroundf(py + ty * lx + sa * ly);
                    hud_pixel(rgb, x, y, r, g, b, a);
                }
            }
        }
    }
}

void hud_text_arc(uint8_t *rgb, int cx, int cy, float rad, float a_mid, int scale,
                   const char *s, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    if (scale < 1) {
        scale = 1;
    }
    if (rad < 8.0f) {
        rad = 8.0f;
    }
    int n = (int)strlen(s);
    if (n <= 0) {
        return;
    }
    float step = (6.0f * (float)scale) / rad;
    float a0 = a_mid - (float)n * step * 0.5f + step * 0.5f;
    for (int i = 0; i < n; i++) {
        unsigned ch = hud_ch((unsigned char)s[i]);
        float ang = a0 + (float)i * step;
        float ca = cosf(ang);
        float sa = sinf(ang);
        float px = (float)cx + ca * rad;
        float py = (float)cy + sa * rad;
        hud_glyph_polar(rgb, px, py, ca, sa, ch, scale, r, g, b, a);
    }
}

void hud_text_arc_outline(uint8_t *rgb, int cx, int cy, float rad, float a_mid, int scale,
                           const char *s, uint8_t r, uint8_t g, uint8_t b)
{
    if (scale < 1) {
        scale = 1;
    }
    if (rad < 8.0f) {
        rad = 8.0f;
    }
    int n = (int)strlen(s);
    if (n <= 0) {
        return;
    }
    float step = (6.0f * (float)scale) / rad;
    float a0 = a_mid - (float)n * step * 0.5f + step * 0.5f;
    static const int k_ox[8] = { -1, 0, 1, -1, 1, -1, 0, 1 };
    static const int k_oy[8] = { -1, -1, -1, 0, 0, 1, 1, 1 };
    for (int i = 0; i < n; i++) {
        unsigned ch = hud_ch((unsigned char)s[i]);
        float ang = a0 + (float)i * step;
        float ca = cosf(ang);
        float sa = sinf(ang);
        float px = (float)cx + ca * rad;
        float py = (float)cy + sa * rad;
        for (int k = 0; k < 8; k++) {
            hud_glyph_polar(rgb, px + (float)k_ox[k], py + (float)k_oy[k],
                            ca, sa, ch, scale, 0, 0, 0, 255);
        }
        hud_glyph_polar(rgb, px, py, ca, sa, ch, scale, r, g, b, 255);
    }
}

#define FONT15_ADV 9.0f

static void hud_glyph15_xy(uint8_t *rgb, int x, int y, unsigned ch,
                           uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    const uint8_t *gcol = k_font[ch - 32];
    for (int col = 0; col < 5; col++) {
        uint8_t bits = gcol[col];
        int dx = (col * 3) / 2;
        for (int row = 0; row < 7; row++) {
            if ((bits & (1u << row)) == 0) {
                continue;
            }
            int dy = (row * 3) / 2;
            for (int sy = 0; sy < 2; sy++) {
                for (int sx = 0; sx < 2; sx++) {
                    hud_pixel(rgb, x + dx + sx, y + dy + sy, r, g, b, a);
                }
            }
        }
    }
}

static void hud_glyph15_dir(uint8_t *rgb, float px, float py, float ca, float sa,
                             unsigned ch, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    const uint8_t *gcol = k_font[ch - 32];
    float tx = -sa;
    float ty = ca;
    for (int col = 0; col < 5; col++) {
        uint8_t bits = gcol[col];
        for (int row = 0; row < 7; row++) {
            if ((bits & (1u << row)) == 0) {
                continue;
            }
            float gx = ((float)col - 2.0f) * 1.5f;
            float gy = (3.0f - (float)row) * 1.5f;
            for (int sy = 0; sy < 2; sy++) {
                for (int sx = 0; sx < 2; sx++) {
                    float lx = gx + (float)sx;
                    float ly = gy - (float)sy;
                    int x = (int)lroundf(px + tx * lx + ca * ly);
                    int y = (int)lroundf(py + ty * lx + sa * ly);
                    hud_pixel(rgb, x, y, r, g, b, a);
                }
            }
        }
    }
}

int hud_text15_width(const char *s)
{
    return (int)strlen(s) * 9;
}

void hud_text15(uint8_t *rgb, int x, int y, const char *s,
                 uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    int cx = x;
    for (const char *p = s; *p; p++) {
        hud_glyph15_xy(rgb, cx, y, hud_ch((unsigned char)*p), r, g, b, a);
        cx += 9;
    }
}

void hud_text15_outline(uint8_t *rgb, int x, int y, const char *s,
                          uint8_t r, uint8_t g, uint8_t b)
{
    static const int k_ox[8] = { -1, 0, 1, -1, 1, -1, 0, 1 };
    static const int k_oy[8] = { -1, -1, -1, 0, 0, 1, 1, 1 };
    for (int i = 0; i < 8; i++) {
        hud_text15(rgb, x + k_ox[i], y + k_oy[i], s, 0, 0, 0, 255);
    }
    hud_text15(rgb, x, y, s, r, g, b, 255);
}

void hud_text15_cx_outline(uint8_t *rgb, int y, const char *s,
                            uint8_t r, uint8_t g, uint8_t b)
{
    hud_text15_outline(rgb, HUD_CX - hud_text15_width(s) / 2, y, s, r, g, b);
}

static void hud_text15_arc_at(uint8_t *rgb, int cx, int cy, float rad, float a_mid,
                               const char *s, uint8_t r, uint8_t g, uint8_t b, uint8_t a, int outline)
{
    if (rad < 10.0f) {
        rad = 10.0f;
    }
    int n = (int)strlen(s);
    if (n <= 0) {
        return;
    }
    float step = FONT15_ADV / rad;
    float a0 = a_mid - (float)n * step * 0.5f + step * 0.5f;
    static const int k_ox[8] = { -1, 0, 1, -1, 1, -1, 0, 1 };
    static const int k_oy[8] = { -1, -1, -1, 0, 0, 1, 1, 1 };
    for (int i = 0; i < n; i++) {
        unsigned ch = hud_ch((unsigned char)s[i]);
        float ang = a0 + (float)i * step;
        float ca = cosf(ang);
        float sa = sinf(ang);
        float px = (float)cx + ca * rad;
        float py = (float)cy + sa * rad;
        if (outline) {
            for (int k = 0; k < 8; k++) {
                hud_glyph15_dir(rgb, px + (float)k_ox[k], py + (float)k_oy[k],
                               ca, sa, ch, 0, 0, 0, 255);
            }
        }
        hud_glyph15_dir(rgb, px, py, ca, sa, ch, r, g, b, a);
    }
}

void hud_text15_arc(uint8_t *rgb, int cx, int cy, float rad, float a_mid,
                      const char *s, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    hud_text15_arc_at(rgb, cx, cy, rad, a_mid, s, r, g, b, a, 0);
}

void hud_text15_arc_outline(uint8_t *rgb, int cx, int cy, float rad, float a_mid,
                             const char *s, uint8_t r, uint8_t g, uint8_t b)
{
    hud_text15_arc_at(rgb, cx, cy, rad, a_mid, s, r, g, b, 255, 1);
}

static void hud_text15_radial_at(uint8_t *rgb, int cx, int cy, float ang, float rad_mid,
                                   const char *s, uint8_t r, uint8_t g, uint8_t b, int outline)
{
    int n = (int)strlen(s);
    if (n <= 0) {
        return;
    }
    float ca = cosf(ang);
    float sa = sinf(ang);
    float r0 = rad_mid - ((float)n - 1.0f) * FONT15_ADV * 0.5f;
    float uca = -sa;
    float usa = ca;
    static const int k_ox[8] = { -1, 0, 1, -1, 1, -1, 0, 1 };
    static const int k_oy[8] = { -1, -1, -1, 0, 0, 1, 1, 1 };
    for (int i = 0; i < n; i++) {
        unsigned ch = hud_ch((unsigned char)s[i]);
        float rad = r0 + (float)i * FONT15_ADV;
        float px = (float)cx + ca * rad;
        float py = (float)cy + sa * rad;
        if (outline) {
            for (int k = 0; k < 8; k++) {
                hud_glyph15_dir(rgb, px + (float)k_ox[k], py + (float)k_oy[k],
                               uca, usa, ch, 0, 0, 0, 255);
            }
        }
        hud_glyph15_dir(rgb, px, py, uca, usa, ch, r, g, b, 255);
    }
}

void hud_text15_radial(uint8_t *rgb, int cx, int cy, float ang, float rad_mid,
                        const char *s, uint8_t r, uint8_t g, uint8_t b)
{
    hud_text15_radial_at(rgb, cx, cy, ang, rad_mid, s, r, g, b, 0);
}

void hud_text15_radial_outline(uint8_t *rgb, int cx, int cy, float ang, float rad_mid,
                                const char *s, uint8_t r, uint8_t g, uint8_t b)
{
    hud_text15_radial_at(rgb, cx, cy, ang, rad_mid, s, r, g, b, 1);
}

int hud_in_disk(int x, int y)
{
    return in_disk(x, y);
}

void hud_fill_image(uint8_t *rgb, const uint8_t *src)
{
    for (int y = 0; y < HUD_H; y++) {
        for (int x = 0; x < HUD_W; x++) {
            uint8_t *p = rgb + (y * HUD_W + x) * 3;
            if (in_disk(x, y)) {
                const uint8_t *s = src + (y * HUD_W + x) * 3;
                p[0] = s[0];
                p[1] = s[1];
                p[2] = s[2];
            } else {
                p[0] = 0;
                p[1] = 0;
                p[2] = 0;
            }
        }
    }
}

static float edge_dist(float ax, float ay, float bx, float by, float px, float py)
{
    float ex = bx - ax;
    float ey = by - ay;
    float len = sqrtf(ex * ex + ey * ey);
    if (len < 1e-6f) {
        return 0.0f;
    }
    return ((px - ax) * ey - (py - ay) * ex) / len;
}

void hud_fill_tri(uint8_t *rgb, int x0, int y0, int x1, int y1, int x2, int y2,
                   uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
    int minx = x0, maxx = x0, miny = y0, maxy = y0;
    if (x1 < minx) minx = x1;
    if (x1 > maxx) maxx = x1;
    if (y1 < miny) miny = y1;
    if (y1 > maxy) maxy = y1;
    if (x2 < minx) minx = x2;
    if (x2 > maxx) maxx = x2;
    if (y2 < miny) miny = y2;
    if (y2 > maxy) maxy = y2;
    minx -= 1;
    maxx += 1;
    miny -= 1;
    maxy += 1;
    float area = edge_dist((float)x0, (float)y0, (float)x1, (float)y1, (float)x2, (float)y2);
    if (fabsf(area) < 1e-4f) {
        hud_line(rgb, x0, y0, x1, y1, r, g, b, a);
        hud_line(rgb, x1, y1, x2, y2, r, g, b, a);
        hud_line(rgb, x2, y2, x0, y0, r, g, b, a);
        return;
    }
    float s = area > 0.0f ? 1.0f : -1.0f;
    float ax0 = (float)x0, ay0 = (float)y0;
    float ax1 = (float)x1, ay1 = (float)y1;
    float ax2 = (float)x2, ay2 = (float)y2;
    for (int y = miny; y <= maxy; y++) {
        for (int x = minx; x <= maxx; x++) {
            float px = (float)x;
            float py = (float)y;
            float d0 = s * edge_dist(ax1, ay1, ax2, ay2, px, py);
            float d1 = s * edge_dist(ax2, ay2, ax0, ay0, px, py);
            float d2 = s * edge_dist(ax0, ay0, ax1, ay1, px, py);
            float mind = d0;
            if (d1 < mind) {
                mind = d1;
            }
            if (d2 < mind) {
                mind = d2;
            }
            hud_pixel_cov(rgb, x, y, r, g, b, a, cover01(mind + 0.5f));
        }
    }
}

void hud_icon_claude(uint8_t *rgb, int cx, int cy, int size, uint8_t a)
{
    /* 6-point asterisk-star in Claude orange #D97757, slightly off-grid. */
    const uint8_t cr = 0xD9, cg = 0x77, cb = 0x57;
    if (size < 5) {
        size = 5;
    }
    const float lens[6] = {1.00f, 0.78f, 0.92f, 0.80f, 0.96f, 0.74f};
    const float rot = - (float)M_PI / 2.0f + 0.11f;
    float w = (float)size * 0.22f;
    for (int i = 0; i < 6; i++) {
        float ang = rot + (float)i * (float)M_PI / 3.0f;
        float outer = (float)size * lens[i];
        int tx = cx + (int)lroundf(cosf(ang) * outer);
        int ty = cy + (int)lroundf(sinf(ang) * outer);
        int s1x = cx + (int)lroundf(cosf(ang + 1.20f) * w);
        int s1y = cy + (int)lroundf(sinf(ang + 1.20f) * w);
        int s2x = cx + (int)lroundf(cosf(ang - 1.20f) * w);
        int s2y = cy + (int)lroundf(sinf(ang - 1.20f) * w);
        hud_fill_tri(rgb, tx, ty, s1x, s1y, s2x, s2y, cr, cg, cb, a);
    }
    hud_fill_circle(rgb, cx, cy, size / 7 + 1, cr, cg, cb, a);
}

void hud_icon_cursor(uint8_t *rgb, int cx, int cy, int size, uint8_t a)
{
    /* Classic mouse-pointer mark: tip at top-left of the glyph box. */
    if (size < 6) {
        size = 6;
    }
    float s = (float)size / 8.0f;
    int ox = cx - (int)(3.5f * s);
    int oy = cy - (int)(4.0f * s);
    int x0 = ox;
    int y0 = oy;
    int x1 = ox;
    int y1 = oy + (int)lroundf(12.0f * s);
    int x2 = ox + (int)lroundf(3.2f * s);
    int y2 = oy + (int)lroundf(9.0f * s);
    int x3 = ox + (int)lroundf(5.6f * s);
    int y3 = oy + (int)lroundf(15.2f * s);
    int x4 = ox + (int)lroundf(7.6f * s);
    int y4 = oy + (int)lroundf(14.2f * s);
    int x5 = ox + (int)lroundf(4.6f * s);
    int y5 = oy + (int)lroundf(8.0f * s);
    int x6 = ox + (int)lroundf(10.0f * s);
    int y6 = oy + (int)lroundf(8.0f * s);
    const uint8_t wr = 235, wg = 240, wb = 250;
    hud_fill_tri(rgb, x0, y0, x1, y1, x2, y2, wr, wg, wb, a);
    hud_fill_tri(rgb, x0, y0, x2, y2, x5, y5, wr, wg, wb, a);
    hud_fill_tri(rgb, x0, y0, x5, y5, x6, y6, wr, wg, wb, a);
    hud_fill_tri(rgb, x2, y2, x3, y3, x4, y4, wr, wg, wb, a);
    hud_fill_tri(rgb, x2, y2, x4, y4, x5, y5, wr, wg, wb, a);
    hud_line(rgb, x0, y0, x1, y1, 20, 24, 32, a);
    hud_line(rgb, x1, y1, x2, y2, 20, 24, 32, a);
    hud_line(rgb, x2, y2, x3, y3, 20, 24, 32, a);
    hud_line(rgb, x3, y3, x4, y4, 20, 24, 32, a);
    hud_line(rgb, x4, y4, x5, y5, 20, 24, 32, a);
    hud_line(rgb, x5, y5, x6, y6, 20, 24, 32, a);
    hud_line(rgb, x6, y6, x0, y0, 20, 24, 32, a);
}

void hud_pack_rgb565(const uint8_t *rgb, uint16_t *dst)
{
    /* CO5300 QSPI wants byte-swapped RGB565. */
    for (int y = 0; y < HUD_H; y++) {
        for (int x = 0; x < HUD_W; x++) {
            const uint8_t *p = rgb + (y * HUD_W + x) * 3;
            dst[y * HUD_W + x] = pack565(p[0], p[1], p[2]);
        }
    }
}

void hud_pack_rect(const uint8_t *rgb, uint16_t *dst, int x0, int y0, int x1, int y1)
{
    int w = x1 - x0;
    int i = 0;
    for (int y = y0; y < y1; y++) {
        const uint8_t *row = rgb + (y * HUD_W + x0) * 3;
        for (int x = 0; x < w; x++) {
            const uint8_t *p = row + x * 3;
            dst[i++] = pack565(p[0], p[1], p[2]);
        }
    }
}

void hud_pack_rect_inv(const uint8_t *rgb, uint16_t *dst, int x0, int y0, int x1, int y1)
{
    int w = x1 - x0;
    int i = 0;
    for (int y = y0; y < y1; y++) {
        const uint8_t *row = rgb + (y * HUD_W + x0) * 3;
        for (int x = 0; x < w; x++) {
            const uint8_t *p = row + x * 3;
            dst[i++] = pack565((uint8_t)(p[0] ^ 0xFF), (uint8_t)(p[1] ^ 0xFF),
                               (uint8_t)(p[2] ^ 0xFF));
        }
    }
}

void hud_tiles_begin(hud_tiles_t rows)
{
    memset(rows, 0, sizeof(hud_tiles_t));
    s_tile_rows = rows;
    s_tile_on = 1;
}

void hud_tiles_end(void)
{
    s_tile_on = 0;
    s_tile_rows = NULL;
}

void hud_tiles_all(hud_tiles_t rows)
{
    uint32_t row_bits = (HUD_TX >= 32) ? 0xffffffffu : ((1u << HUD_TX) - 1u);
    for (int ty = 0; ty < HUD_TY; ty++) {
        rows[ty] = row_bits;
    }
}

void hud_tiles_add_rect(hud_tiles_t rows, int x0, int y0, int x1, int y1)
{
    if (x0 < 0) {
        x0 = 0;
    }
    if (y0 < 0) {
        y0 = 0;
    }
    if (x1 > HUD_W) {
        x1 = HUD_W;
    }
    if (y1 > HUD_H) {
        y1 = HUD_H;
    }
    if (x1 <= x0 || y1 <= y0) {
        return;
    }
    int tx0 = x0 >> 4;
    int ty0 = y0 >> 4;
    int tx1 = (x1 - 1) >> 4;
    int ty1 = (y1 - 1) >> 4;
    for (int ty = ty0; ty <= ty1; ty++) {
        for (int tx = tx0; tx <= tx1; tx++) {
            rows[ty] |= 1u << tx;
        }
    }
}

void hud_tiles_or(hud_tiles_t dst, const hud_tiles_t a, const hud_tiles_t b)
{
    for (int ty = 0; ty < HUD_TY; ty++) {
        dst[ty] = a[ty] | b[ty];
    }
}

void hud_tiles_diff(hud_tiles_t rows, const uint8_t *a, const uint8_t *b)
{
    memset(rows, 0, sizeof(hud_tiles_t));
    for (int ty = 0; ty < HUD_TY; ty++) {
        int y0 = ty * HUD_TILE;
        int y1 = y0 + HUD_TILE;
        if (y1 > HUD_H) {
            y1 = HUD_H;
        }
        for (int tx = 0; tx < HUD_TX; tx++) {
            int x0 = tx * HUD_TILE;
            int x1 = x0 + HUD_TILE;
            if (x1 > HUD_W) {
                x1 = HUD_W;
            }
            int differ = 0;
            for (int y = y0; y < y1; y++) {
                const uint8_t *pa = a + (y * HUD_W + x0) * 3;
                const uint8_t *pb = b + (y * HUD_W + x0) * 3;
                if (memcmp(pa, pb, (size_t)(x1 - x0) * 3) != 0) {
                    differ = 1;
                    break;
                }
            }
            if (differ) {
                rows[ty] |= 1u << tx;
            }
        }
    }
}

int hud_tiles_count(const hud_tiles_t rows)
{
    int n = 0;
    for (int ty = 0; ty < HUD_TY; ty++) {
        n += __builtin_popcount(rows[ty]);
    }
    return n;
}

void hud_tile_rect(int tx, int ty, int *x0, int *y0, int *x1, int *y1)
{
    *x0 = tx * HUD_TILE;
    *y0 = ty * HUD_TILE;
    *x1 = *x0 + HUD_TILE;
    *y1 = *y0 + HUD_TILE;
    if (*x1 > HUD_W) {
        *x1 = HUD_W;
    }
    if (*y1 > HUD_H) {
        *y1 = HUD_H;
    }
}

void hud_restore_tile(uint8_t *dst, const uint8_t *src, int tx, int ty)
{
    int x0, y0, x1, y1;
    hud_tile_rect(tx, ty, &x0, &y0, &x1, &y1);
    size_t row_bytes = (size_t)(x1 - x0) * 3;
    for (int y = y0; y < y1; y++) {
        int off = (y * HUD_W + x0) * 3;
        memcpy(dst + off, src + off, row_bytes);
    }
}

void hud_erase_tile(uint8_t *dst, int tx, int ty)
{
    int x0, y0, x1, y1;
    hud_tile_rect(tx, ty, &x0, &y0, &x1, &y1);
    s_erase_rng = s_erase_rng * 1664525u + 1013904223u;
    uint8_t r = (uint8_t)(s_erase_rng >> 16);
    uint8_t g = (uint8_t)(s_erase_rng >> 8);
    uint8_t b = (uint8_t)s_erase_rng;
    for (int y = y0; y < y1; y++) {
        uint8_t *p = dst + (y * HUD_W + x0) * 3;
        for (int x = x0; x < x1; x++) {
            p[0] = r;
            p[1] = g;
            p[2] = b;
            p += 3;
        }
    }
}

void hud_restore_tiles(uint8_t *dst, const uint8_t *src, const hud_tiles_t rows)
{
    for (int ty = 0; ty < HUD_TY; ty++) {
        uint32_t bits = rows[ty];
        int y0 = ty * HUD_TILE;
        int h = HUD_H - y0;
        if (h > HUD_TILE) {
            h = HUD_TILE;
        }
        while (bits) {
            int tx = __builtin_ctz(bits);
            bits &= bits - 1u;
            int x0 = tx * HUD_TILE;
            int w = HUD_W - x0;
            if (w > HUD_TILE) {
                w = HUD_TILE;
            }
            size_t row_bytes = (size_t)w * 3;
            for (int y = 0; y < h; y++) {
                int off = ((y0 + y) * HUD_W + x0) * 3;
                memcpy(dst + off, src + off, row_bytes);
            }
        }
    }
}