This commit is contained in:
2025-12-23 14:40:27 -06:00
parent 54f8c6539d
commit e5232a3009
3 changed files with 1433 additions and 8 deletions

1395
msdf.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1247,7 +1247,7 @@ function _render_text(cmd_buffer, pass, drawable, camera, target) {
// staef font has 'texture' property which is pixel blob + dims. We need to upload it to GPU if not already.
var font_tex = _get_font_texture(font, is_sdf)
pass.bind_fragment_samplers(0, [{texture: font_tex, sampler: is_sdf ? _sampler_linear : _sampler_nearest}])
pass.bind_fragment_samplers(0, [{texture: font_tex, sampler: _sampler_nearest}])
cmd_buffer.push_vertex_uniform_data(0, proj)
pass.draw_indexed(num_indices, 1, 0, 0, 0)
}

44
staef.c
View File

@@ -11,6 +11,9 @@
#include <stdlib.h>
#include <string.h>
#define MSDF_IMPLEMENTATION
#include "msdf.h"
#define STB_TRUETYPE_IMPLEMENTATION
#define STB_TRUETYPE_NO_STDIO
#include "stb_truetype.h"
@@ -113,7 +116,7 @@ struct sFont *MakeFont(void *ttf_buffer, size_t len, int height, int is_sdf) {
int row_height = 0;
int pad = 5; // padding for SDF
float onedge_value = 127.5f; // 128ish
float pixel_dist_scale = 32.0f; // Distance field range
float pixel_dist_scale = 150.f; // Distance field range
for (unsigned char c = 32; c < 127; c++) {
int glyph_index = c - 29; // Simple ASCII mapping? verify if font has proper map
@@ -124,7 +127,14 @@ struct sFont *MakeFont(void *ttf_buffer, size_t len, int height, int is_sdf) {
int width, height, xoff, yoff;
unsigned char *sdf = stbtt_GetGlyphSDF(&fontinfo, scale, g, pad, (unsigned char)onedge_value, pixel_dist_scale, &width, &height, &xoff, &yoff);
if (!sdf) continue;
if (!sdf) {
// Handle invisible characters (space)
int advance, lsb;
stbtt_GetGlyphHMetrics(&fontinfo, g, &advance, &lsb);
newfont->Characters[c].advance = advance * scale;
// Keep quad/uv as 0
continue;
}
if (x + width + 1 > packsize) {
x = 0;
@@ -135,7 +145,6 @@ struct sFont *MakeFont(void *ttf_buffer, size_t len, int height, int is_sdf) {
if (y + height + 1 > packsize) {
// Out of space
free(sdf);
// Continue or break? Missing chars better than crash
continue;
}
@@ -151,15 +160,36 @@ struct sFont *MakeFont(void *ttf_buffer, size_t len, int height, int is_sdf) {
// Store character info
rect uv;
uv.x = (float)x / packsize;
uv.y = (float)y / packsize;
uv.y = (float)(y + height) / packsize; // Bottom (Top of glyph in SDF?)
// To match bitmap path which produces UPSIDE DOWN results if not flipped:
// Bitmap path: uv.h = Negative.
// So UVs are (y1, y0). (Top Texture is at Bottom vertex).
// Here we want to match that.
// Top Vertex (v0) should get Bottom Texture.
// Bottom Vertex (v1) should get Top Texture.
// Let's make uv.h negative.
// Top Texture is at 'y'. Bottom Texture is at 'y+height'.
// uv.y = (y+height)/size.
// uv.h = -height/size.
// v0 (Top Vert) = uv.y = y+height (Bottom Tex).
// v1 (Bottom Vert) = uv.y + uv.h = y (Top Tex).
// This flips the texture on Y.
uv.w = (float)width / packsize;
uv.h = (float)height / packsize;
uv.h = -(float)height / packsize;
newfont->Characters[c].uv = uv;
rect quad;
quad.x = (float)xoff;
quad.y = (float)yoff;
quad.w = (float)width; // Includes padding
// Bitmap path: quad.y = -yoff2. (Bottom in Y-Up).
// SDF path: yoff is Top in Y-Down (-Top in Y-Up).
// height is height.
// We want Bottom in Y-Up.
// Bottom = - (yoff + height).
quad.y = (float)(-yoff - height);
quad.w = (float)width;
quad.h = (float)height;
newfont->Characters[c].quad = quad;