diff options
| author | Fabrice <fabrice@schaub-dev.xyz> | 2026-02-13 08:25:22 +0100 |
|---|---|---|
| committer | Fabrice <fabrice@schaub-dev.xyz> | 2026-02-13 08:25:22 +0100 |
| commit | 70925ed8178ceec3fa14e7f2abbc7f0054f1d190 (patch) | |
| tree | 7e38353167a2afd7fa380c28819c1c59142ba49a | |
| parent | 952712bdcc2cc3c055f0c8b306e9479d7a299a5a (diff) | |
rendering geometry
| -rw-r--r-- | src/text.cc | 45 | ||||
| -rw-r--r-- | src/text.h | 1 |
2 files changed, 46 insertions, 0 deletions
diff --git a/src/text.cc b/src/text.cc index 3bd3405..a56033a 100644 --- a/src/text.cc +++ b/src/text.cc @@ -8,6 +8,7 @@ #include "cglm/vec2.h" #include "freetype/freetype.h" #include "freetype/fttypes.h" +#include "geometry.h" #include "hash.h" #include "shaders.h" #include "utils.h" @@ -173,10 +174,12 @@ enum font_error_e wayc_font_raster(struct font_s* font, codepoint_t codepoint, if (aterr) return FONT_ERROR_ATLAS_FAILED; vec2 bearing = {(f32)slot->bitmap_left, (f32)slot->bitmap_top}; + vec2 size = {(f32)bitmap.width, (f32)bitmap.rows}; f32 advance = (f32)slot->advance.x / WAYC_SCALE; glm_vec2_copy(uv0, got.uv0); glm_vec2_copy(uv1, got.uv1); + glm_vec2_copy(size, got.size); glm_vec2_copy(bearing, got.bearing); got.advance = advance; @@ -237,6 +240,33 @@ void wayc_font_deinit(struct font_s* font) { FT_Done_Face(font->face); } +static void wayc_text_quad(struct text_vertex_s* verts, u16* indices, + u16 base, const struct glyph_s* glyph, + vec2 cursor) { + f32 x0 = WAYC_X(cursor) + WAYC_X(glyph->bearing); + f32 y0 = WAYC_Y(cursor) - WAYC_Y(glyph->bearing); + f32 x1 = x0 + WAYC_X(glyph->size); + f32 y1 = y0 + WAYC_Y(glyph->size); + + struct quad_s quad; + wayc_quad_init(&quad, x0, y0, x1, y1); + + vec2 uvs[WAYC_QUAD_NVERTS] = { + {WAYC_X(glyph->uv0), WAYC_Y(glyph->uv0)}, + {WAYC_X(glyph->uv1), WAYC_Y(glyph->uv0)}, + {WAYC_X(glyph->uv1), WAYC_Y(glyph->uv1)}, + {WAYC_X(glyph->uv0), WAYC_Y(glyph->uv1)}, + }; + + for (usize j = 0; j < WAYC_QUAD_NVERTS; j++) { + glm_vec2_copy(quad.vertices[j], verts[base + j].pos); + glm_vec2_copy(uvs[j], verts[base + j].uv); + } + + for (usize j = 0; j < WAYC_QUAD_NINDICES; j++) + indices[j] = (u16)(base + quad.indices[j]); +} + bool wayc_text_mesh_init(struct text_mesh_s* mesh, const char* text, usize text_len, struct font_s* font) { wayc_notnull(mesh); @@ -266,6 +296,21 @@ 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; + 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], + cursor); + WAYC_X(cursor) += glyphs[i].advance; + } + + mesh->vertices = verts; + mesh->vertex_count = nverts; + mesh->indices = indices; + mesh->index_count = nindices; + + success = true; return true; } @@ -34,6 +34,7 @@ void wayc_font_context_deinit(struct font_context_s* context); struct glyph_s { vec2 uv0, uv1; + vec2 size; vec2 bearing; f32 advance; }; |
