diff options
| author | Fabrice <fabrice@schaub-dev.xyz> | 2026-02-20 06:24:57 +0100 |
|---|---|---|
| committer | Fabrice <fabrice@schaub-dev.xyz> | 2026-02-20 06:24:57 +0100 |
| commit | 027efb4c89e54f88f8a519a773ca803ec258c369 (patch) | |
| tree | 9fa121d3d7ed06f3881716e5cbeb80cc11594dfd | |
| parent | 6223e4356950268a2ca88a45a1de22bcae3e50fa (diff) | |
fixing text code
| -rw-r--r-- | src/text.cc | 31 |
1 files changed, 7 insertions, 24 deletions
diff --git a/src/text.cc b/src/text.cc index f438aed..36d842e 100644 --- a/src/text.cc +++ b/src/text.cc @@ -13,6 +13,7 @@ #include "shaders.h" #include "utils.h" +// Linear filtering for smooth glyph rendering, clamped UVs to prevent atlas bleeding static void wayc_font_context_sampler_desc(struct sg_sampler_desc* desc) { wayc_notnull(desc); @@ -36,6 +37,7 @@ static void wayc_font_context_layout(struct sg_vertex_layout_state* layout) { layout->attrs[ATTR_text_in_uv].offset = offsetof(text_vertex_s, uv); } +// Standard alpha blending for transparent text rendering static void wayc_font_context_color(struct sg_color_target_state* color) { wayc_notnull(color); @@ -179,6 +181,7 @@ enum font_error_e wayc_font_raster(struct font_s* font, codepoint_t codepoint, wayc_notnull(font); wayc_notnull(glyph); + // Return cached glyph if already rasterized to avoid redundant FreeType calls struct glyph_s got; if (wayc_hashmap_get(&font->cached, &codepoint, &got)) { *glyph = got; @@ -194,8 +197,8 @@ enum font_error_e wayc_font_raster(struct font_s* font, codepoint_t codepoint, FT_GlyphSlot slot = font->face->glyph; FT_Bitmap bitmap = slot->bitmap; + // Pack glyph bitmap into atlas texture and get UV coordinates vec2 uv0, uv1; - enum atlas_error_e aterr = wayc_atlas_insert( &font->atlas, bitmap.width, bitmap.rows, bitmap.buffer, &uv0, &uv1); if (aterr) return FONT_ERROR_ATLAS_FAILED; @@ -216,27 +219,6 @@ enum font_error_e wayc_font_raster(struct font_s* font, codepoint_t codepoint, return FONT_ERROR_NONE; } -enum font_error_e wayc_font_raster(struct font_s* font, const char* text, - usize text_len, struct glyph_s* glyphs, - usize max_glyphs) { - wayc_notnull(font); - wayc_notnull(text); - wayc_notnull(glyphs); - memset(glyphs, 0, sizeof(*glyphs) * max_glyphs); - - usize count = 0; - for (usize i = 0; i < text_len && count < max_glyphs; i++) { - codepoint_t codepoint = (codepoint_t)text[i]; // TODO: handle UTF-8 - struct glyph_s glyph; - enum font_error_e err = wayc_font_raster(font, codepoint, &glyph); - if (err) return err; - - glyphs[count++] = glyph; - } - - return FONT_ERROR_NONE; -} - enum font_error_e wayc_font_raster_text(struct font_s* font, const char* text, usize text_len, struct glyph_s* glyphs, usize max_glyphs) { @@ -247,7 +229,7 @@ enum font_error_e wayc_font_raster_text(struct font_s* font, const char* text, usize count = 0; for (usize i = 0; i < text_len && count < max_glyphs; i++) { - codepoint_t codepoint = (codepoint_t)text[i]; // TODO: handle utf-8 + codepoint_t codepoint = (codepoint_t)text[i]; // TODO: handle UTF-8 struct glyph_s glyph; enum font_error_e err = wayc_font_raster(font, codepoint, &glyph); @@ -289,6 +271,7 @@ bool wayc_text_mesh_init(struct text_mesh_s* mesh, const char* text, u16* indices = (u16*)mi_malloc(bytes_indices); wayc_defer_cond(mi_free(indices), success, true); + // Temporary storage for glyph metrics, freed after mesh generation struct glyph_s* glyphs = (struct glyph_s*)mi_malloc(bytes_glyphs); wayc_defer(mi_free(glyphs)); @@ -296,8 +279,8 @@ bool wayc_text_mesh_init(struct text_mesh_s* mesh, const char* text, wayc_font_raster_text(font, text, text_len, glyphs, text_len); if (err != FONT_ERROR_NONE) return false; + // Build quads for each character, advancing cursor by glyph width vec2 cursor = {0.0f, 0.0f}; - for (usize i = 0; i < text_len; i++) { u16 base = (u16)(i * WAYC_QUAD_NVERTS); wayc_text_quad(verts, &indices[i * WAYC_QUAD_NINDICES], base, &glyphs[i], |
