diff options
| author | Gabriel Schneider <[email protected]> | 2026-08-09 20:31:09 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-08-10 09:17:07 -0300 |
| commit | 4b5e793f48f6e721c39ab6ddcf755604df73b643 (patch) | |
| tree | f3cf8b009bbfbd5667635381f0f624cd6b466084 /vendor | |
| parent | 9085cb5bfdd0b78ff3a62c0c71fc231dd7b5052a (diff) | |
| download | pardes-4b5e793f48f6e721c39ab6ddcf755604df73b643.tar.gz pardes-4b5e793f48f6e721c39ab6ddcf755604df73b643.zip | |
Better text rendering
Diffstat (limited to 'vendor')
| -rw-r--r-- | vendor/stb/font.c | 81 | ||||
| -rw-r--r-- | vendor/stb/font.h | 41 |
2 files changed, 0 insertions, 122 deletions
diff --git a/vendor/stb/font.c b/vendor/stb/font.c deleted file mode 100644 index 6c5106f6..00000000 --- a/vendor/stb/font.c +++ /dev/null @@ -1,81 +0,0 @@ -// stb_truetype wrapper for ui_sdl.zig. See font.h. Compiled into the SDL UI -// backend module alongside stb_truetype_impl.c (which defines the impl). -#include "font.h" -#include "stb_truetype.h" - -#include <stdlib.h> -#include <string.h> - -struct UIFont { - stbtt_fontinfo info; - const uint8_t *data; -}; - -UIFont *ui_font_new(const uint8_t *data, int32_t len) { - if (!data || len <= 0) return NULL; - UIFont *f = (UIFont *)calloc(1, sizeof(UIFont)); - if (!f) return NULL; - f->data = data; - int offset = stbtt_GetFontOffsetForIndex(data, 0); - if (offset < 0 || !stbtt_InitFont(&f->info, (const unsigned char *)data, offset)) { - free(f); - return NULL; - } - return f; -} - -void ui_font_free(UIFont *f) { - free(f); -} - -float ui_font_scale_for_height(UIFont *f, float px) { - return stbtt_ScaleForPixelHeight(&f->info, px); -} - -void ui_font_cell_metrics(UIFont *f, float scale, int32_t *cell_w, int32_t *cell_h, int32_t *ascent) { - int asc = 0, desc = 0, gap = 0; - stbtt_GetFontVMetrics(&f->info, &asc, &desc, &gap); - int a = (int)(asc * scale + 0.5f); - int d = (int)(-desc * scale + 0.5f); - int g = (int)(gap * scale + 0.5f); - *ascent = a; - *cell_h = a + d + g; - if (*cell_h < 1) *cell_h = 1; - // monospace advance of 'M' - int adv = 0, lsb = 0; - stbtt_GetCodepointHMetrics(&f->info, 'M', &adv, &lsb); - int w = (int)(adv * scale + 0.5f); - if (w < 1) w = 1; - *cell_w = w; -} - -int ui_font_raster(UIFont *f, float scale, int32_t cp, - uint8_t *out, int32_t stride, int32_t cell_w, int32_t cell_h, - int32_t ascent) { - if (!out || cell_w <= 0 || cell_h <= 0) return 0; - // zero the cell - for (int y = 0; y < cell_h; y++) - memset(out + (size_t)y * stride, 0, (size_t)cell_w); - - int w = 0, h = 0, xoff = 0, yoff = 0; - unsigned char *bm = stbtt_GetCodepointBitmap(&f->info, scale, scale, cp, &w, &h, &xoff, &yoff); - if (!bm || w <= 0 || h <= 0) { - if (bm) stbtt_FreeBitmap(bm, NULL); - return 0; - } - // origin = (0, baseline=ascent); glyph top-left = (xoff, ascent + yoff). - int gx = xoff; - int gy = ascent + yoff; - for (int y = 0; y < h; y++) { - int dy = gy + y; - if (dy < 0 || dy >= cell_h) continue; - for (int x = 0; x < w; x++) { - int dx = gx + x; - if (dx < 0 || dx >= cell_w) continue; - uint8_t v = bm[y * w + x]; - if (v) out[(size_t)dy * stride + dx] = v; - } - } - stbtt_FreeBitmap(bm, NULL); - return 1; -} diff --git a/vendor/stb/font.h b/vendor/stb/font.h deleted file mode 100644 index 93bbac8d..00000000 --- a/vendor/stb/font.h +++ /dev/null @@ -1,41 +0,0 @@ -// Tiny C shim over stb_truetype, so ui_sdl.zig can @cInclude a small header -// instead of feeding the whole single-header lib to translate-c. The -// implementation (font.c) pulls in stb_truetype.h (declarations only — -// stb_truetype_impl.c defines STB_TRUETYPE_IMPLEMENTATION once elsewhere). -// -// A "font" is an opaque handle owning a stbtt_fontinfo; all rasterization goes -// through these functions so the Zig side never touches stb internals. -#ifndef UI_FONT_H -#define UI_FONT_H -#include <stdint.h> - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct UIFont UIFont; - -// Parse the embedded TTF. `data` must stay alive for the lifetime of the font. -// Returns NULL on failure. -UIFont *ui_font_new(const uint8_t *data, int32_t len); - -// Font metrics at a given pixel height. cell_w is the monospace advance (rounded -// up); ascent is the baseline offset from the top of a cell. -float ui_font_scale_for_height(UIFont *f, float px); -void ui_font_cell_metrics(UIFont *f, float scale, int32_t *cell_w, int32_t *cell_h, int32_t *ascent); - -// Rasterize `cp` into `out` (a cell_w*cell_h single-channel mask, row-major, -// `stride` bytes per row). The glyph is positioned for a monospace cell: origin -// at the baseline `ascent` pixels from the top. Pixels outside the glyph are 0. -// Returns 1 if the glyph produced any coverage (0 for an empty/missing glyph, -// but `out` is still zeroed). -int ui_font_raster(UIFont *f, float scale, int32_t cp, - uint8_t *out, int32_t stride, int32_t cell_w, int32_t cell_h, - int32_t ascent); - -void ui_font_free(UIFont *f); - -#ifdef __cplusplus -} -#endif -#endif |
