headless.cpp
ui/src/backend/headless.cpp · 223 line(s) · c
/* =============================================================================
* SleelaUI headless backend.
*
* A backend with no window system at all: it renders the Canvas into memory and
* synthesises no input. It exists so the toolkit's portable core (layout, paint,
* theme, rasterizer) can be built and smoke-tested on a CI runner with no X
* server / no display, on every OS. It uses a built-in 5x7 bitmap font so glyph
* metrics and coverage are deterministic and dependency-free.
*
* Selected by compiling with SLUI_BACKEND_HEADLESS_BUILD and linking this file
* instead of a real backend. Max Rupplin -- MEARVK LLC -- 2026.
* ===========================================================================*/
#include "slui_backend.hpp"
#include "slui_window.hpp"
#include <algorithm>
#include <cstdint>
#include <cstring>
namespace slui {
namespace {
/* A minimal 5x7 glyph set: enough printable ASCII to measure and paint text in
* tests. Unknown scalars fall back to a filled box so coverage is non-zero. */
const int kGlyphW = 5, kGlyphH = 7, kAdvance = 6;
/* Each glyph is 7 rows of 5 bits (MSB-first). A complete printable-ASCII 5x7
* font (0x20..0x7E) so headless/offscreen rendering produces READABLE text, not
* placeholder boxes. Unknown scalars still fall back to a hollow box so their
* coverage is non-zero. */
const uint8_t kBox[kGlyphH] = {0x1F, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1F};
const uint8_t kSpace[kGlyphH] = {0, 0, 0, 0, 0, 0, 0};
/* ASCII 0x20..0x7E, 95 glyphs x 7 rows (5-bit, MSB-first). */
const uint8_t kFont5x7[95][kGlyphH] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 0x20 space */
{0x04,0x04,0x04,0x04,0x04,0x00,0x04}, /* ! */
{0x0A,0x0A,0x0A,0x00,0x00,0x00,0x00}, /* " */
{0x0A,0x0A,0x1F,0x0A,0x1F,0x0A,0x0A}, /* # */
{0x04,0x0F,0x14,0x0E,0x05,0x1E,0x04}, /* $ */
{0x18,0x19,0x02,0x04,0x08,0x13,0x03}, /* % */
{0x08,0x14,0x14,0x08,0x15,0x12,0x0D}, /* & */
{0x04,0x04,0x08,0x00,0x00,0x00,0x00}, /* ' */
{0x02,0x04,0x08,0x08,0x08,0x04,0x02}, /* ( */
{0x08,0x04,0x02,0x02,0x02,0x04,0x08}, /* ) */
{0x00,0x04,0x15,0x0E,0x15,0x04,0x00}, /* * */
{0x00,0x04,0x04,0x1F,0x04,0x04,0x00}, /* + */
{0x00,0x00,0x00,0x00,0x04,0x04,0x08}, /* , */
{0x00,0x00,0x00,0x1F,0x00,0x00,0x00}, /* - */
{0x00,0x00,0x00,0x00,0x00,0x0C,0x0C}, /* . */
{0x01,0x02,0x02,0x04,0x08,0x08,0x10}, /* / */
{0x0E,0x11,0x13,0x15,0x19,0x11,0x0E}, /* 0 */
{0x04,0x0C,0x04,0x04,0x04,0x04,0x0E}, /* 1 */
{0x0E,0x11,0x01,0x02,0x04,0x08,0x1F}, /* 2 */
{0x1F,0x02,0x04,0x02,0x01,0x11,0x0E}, /* 3 */
{0x02,0x06,0x0A,0x12,0x1F,0x02,0x02}, /* 4 */
{0x1F,0x10,0x1E,0x01,0x01,0x11,0x0E}, /* 5 */
{0x06,0x08,0x10,0x1E,0x11,0x11,0x0E}, /* 6 */
{0x1F,0x01,0x02,0x04,0x08,0x08,0x08}, /* 7 */
{0x0E,0x11,0x11,0x0E,0x11,0x11,0x0E}, /* 8 */
{0x0E,0x11,0x11,0x0F,0x01,0x02,0x0C}, /* 9 */
{0x00,0x0C,0x0C,0x00,0x0C,0x0C,0x00}, /* : */
{0x00,0x0C,0x0C,0x00,0x0C,0x04,0x08}, /* ; */
{0x02,0x04,0x08,0x10,0x08,0x04,0x02}, /* < */
{0x00,0x00,0x1F,0x00,0x1F,0x00,0x00}, /* = */
{0x08,0x04,0x02,0x01,0x02,0x04,0x08}, /* > */
{0x0E,0x11,0x01,0x02,0x04,0x00,0x04}, /* ? */
{0x0E,0x11,0x17,0x15,0x17,0x10,0x0E}, /* @ */
{0x0E,0x11,0x11,0x1F,0x11,0x11,0x11}, /* A */
{0x1E,0x11,0x11,0x1E,0x11,0x11,0x1E}, /* B */
{0x0E,0x11,0x10,0x10,0x10,0x11,0x0E}, /* C */
{0x1C,0x12,0x11,0x11,0x11,0x12,0x1C}, /* D */
{0x1F,0x10,0x10,0x1E,0x10,0x10,0x1F}, /* E */
{0x1F,0x10,0x10,0x1E,0x10,0x10,0x10}, /* F */
{0x0E,0x11,0x10,0x17,0x11,0x11,0x0F}, /* G */
{0x11,0x11,0x11,0x1F,0x11,0x11,0x11}, /* H */
{0x0E,0x04,0x04,0x04,0x04,0x04,0x0E}, /* I */
{0x07,0x02,0x02,0x02,0x02,0x12,0x0C}, /* J */
{0x11,0x12,0x14,0x18,0x14,0x12,0x11}, /* K */
{0x10,0x10,0x10,0x10,0x10,0x10,0x1F}, /* L */
{0x11,0x1B,0x15,0x15,0x11,0x11,0x11}, /* M */
{0x11,0x19,0x15,0x13,0x11,0x11,0x11}, /* N */
{0x0E,0x11,0x11,0x11,0x11,0x11,0x0E}, /* O */
{0x1E,0x11,0x11,0x1E,0x10,0x10,0x10}, /* P */
{0x0E,0x11,0x11,0x11,0x15,0x12,0x0D}, /* Q */
{0x1E,0x11,0x11,0x1E,0x14,0x12,0x11}, /* R */
{0x0F,0x10,0x10,0x0E,0x01,0x01,0x1E}, /* S */
{0x1F,0x04,0x04,0x04,0x04,0x04,0x04}, /* T */
{0x11,0x11,0x11,0x11,0x11,0x11,0x0E}, /* U */
{0x11,0x11,0x11,0x11,0x11,0x0A,0x04}, /* V */
{0x11,0x11,0x11,0x15,0x15,0x1B,0x11}, /* W */
{0x11,0x11,0x0A,0x04,0x0A,0x11,0x11}, /* X */
{0x11,0x11,0x0A,0x04,0x04,0x04,0x04}, /* Y */
{0x1F,0x01,0x02,0x04,0x08,0x10,0x1F}, /* Z */
{0x0E,0x08,0x08,0x08,0x08,0x08,0x0E}, /* [ */
{0x10,0x08,0x08,0x04,0x02,0x02,0x01}, /* \ */
{0x0E,0x02,0x02,0x02,0x02,0x02,0x0E}, /* ] */
{0x04,0x0A,0x11,0x00,0x00,0x00,0x00}, /* ^ */
{0x00,0x00,0x00,0x00,0x00,0x00,0x1F}, /* _ */
{0x08,0x04,0x02,0x00,0x00,0x00,0x00}, /* ` */
{0x00,0x00,0x0E,0x01,0x0F,0x11,0x0F}, /* a */
{0x10,0x10,0x16,0x19,0x11,0x11,0x1E}, /* b */
{0x00,0x00,0x0E,0x10,0x10,0x11,0x0E}, /* c */
{0x01,0x01,0x0D,0x13,0x11,0x11,0x0F}, /* d */
{0x00,0x00,0x0E,0x11,0x1F,0x10,0x0E}, /* e */
{0x06,0x09,0x08,0x1C,0x08,0x08,0x08}, /* f */
{0x00,0x0F,0x11,0x11,0x0F,0x01,0x0E}, /* g */
{0x10,0x10,0x16,0x19,0x11,0x11,0x11}, /* h */
{0x04,0x00,0x0C,0x04,0x04,0x04,0x0E}, /* i */
{0x02,0x00,0x06,0x02,0x02,0x12,0x0C}, /* j */
{0x10,0x10,0x12,0x14,0x18,0x14,0x12}, /* k */
{0x0C,0x04,0x04,0x04,0x04,0x04,0x0E}, /* l */
{0x00,0x00,0x1A,0x15,0x15,0x11,0x11}, /* m */
{0x00,0x00,0x16,0x19,0x11,0x11,0x11}, /* n */
{0x00,0x00,0x0E,0x11,0x11,0x11,0x0E}, /* o */
{0x00,0x00,0x1E,0x11,0x1E,0x10,0x10}, /* p */
{0x00,0x00,0x0D,0x13,0x0F,0x01,0x01}, /* q */
{0x00,0x00,0x16,0x19,0x10,0x10,0x10}, /* r */
{0x00,0x00,0x0F,0x10,0x0E,0x01,0x1E}, /* s */
{0x08,0x08,0x1C,0x08,0x08,0x09,0x06}, /* t */
{0x00,0x00,0x11,0x11,0x11,0x13,0x0D}, /* u */
{0x00,0x00,0x11,0x11,0x11,0x0A,0x04}, /* v */
{0x00,0x00,0x11,0x11,0x15,0x15,0x0A}, /* w */
{0x00,0x00,0x11,0x0A,0x04,0x0A,0x11}, /* x */
{0x00,0x00,0x11,0x11,0x0F,0x01,0x0E}, /* y */
{0x00,0x00,0x1F,0x02,0x04,0x08,0x1F}, /* z */
{0x02,0x04,0x04,0x08,0x04,0x04,0x02}, /* { */
{0x04,0x04,0x04,0x04,0x04,0x04,0x04}, /* | */
{0x08,0x04,0x04,0x02,0x04,0x04,0x08}, /* } */
{0x00,0x00,0x08,0x15,0x02,0x00,0x00}, /* ~ */
};
class HeadlessWindow : public NativeWindow {
public:
HeadlessWindow(int w, int h) : width_(w), height_(h) {}
void set_title(const std::string& t) override { title_ = t; }
void show() override { visible_ = true; }
void hide() override { visible_ = false; }
void set_size_limits(int minw, int minh, int maxw, int maxh) override {
min_width_ = minw; min_height_ = minh; max_width_ = maxw; max_height_ = maxh;
resize_content(width_, height_);
}
void resize_content(int w, int h) override {
width_ = clamp(w, min_width_, max_width_); height_ = clamp(h, min_height_, max_height_); dirty_ = true;
}
int width() const override { return width_; }
int height() const override { return height_; }
void request_redraw() override { dirty_ = true; }
void present(const Canvas& canvas) override {
last_ = canvas.width();
(void)canvas;
dirty_ = false;
}
bool visible_ = false;
bool dirty_ = true;
int last_ = 0;
private:
static int clamp(int v, int lo, int hi) { v = std::max(v, lo); return hi > 0 ? std::min(v, hi) : v; }
int width_, height_;
int min_width_ = 0, min_height_ = 0, max_width_ = 0, max_height_ = 0;
std::string title_;
};
class HeadlessBackend : public Backend {
public:
SLUIBackend id() const override { return SLUI_BACKEND_UNKNOWN; }
std::unique_ptr<NativeWindow> create_window(Window*, const std::string&,
int w, int h, bool) override {
return std::make_unique<HeadlessWindow>(w, h);
}
int pump(bool) override { return 0; }
void quit(int code) override {
quit_ = true;
code_ = code;
}
bool should_quit(int* code) const override {
if (quit_ && code) *code = code_;
return quit_;
}
bool set_font(const std::string&, int size) override {
size_ = size > 0 ? size : 11;
return true;
}
double font_ascent() const override { return kGlyphH; }
double font_descent() const override { return 2.0; }
double font_line_height() const override { return kGlyphH + 4; }
bool rasterize_glyph(uint32_t cp, GlyphBitmap* out) override {
const uint8_t* rows;
if (cp == ' ') rows = kSpace;
else if (cp >= 0x21 && cp <= 0x7E) rows = kFont5x7[cp - 0x20];
else rows = kBox; /* non-ASCII fallback */
out->left = 0;
out->top = kGlyphH;
out->width = kGlyphW;
out->height = kGlyphH;
out->advance = kAdvance;
out->coverage.assign(static_cast<size_t>(kGlyphW) * kGlyphH, 0);
for (int y = 0; y < kGlyphH; ++y)
for (int x = 0; x < kGlyphW; ++x)
if (rows[y] & (1 << (kGlyphW - 1 - x)))
out->coverage[static_cast<size_t>(y) * kGlyphW + x] = 255;
return true;
}
double glyph_advance(uint32_t) override { return kAdvance; }
private:
bool quit_ = false;
int code_ = 0;
int size_ = 11;
};
} // namespace
std::unique_ptr<Backend> create_backend(const std::string&) {
return std::make_unique<HeadlessBackend>();
}
} // namespace slui