From 6013bac1556659f1685170b5e68752189f0b93ba Mon Sep 17 00:00:00 2001 From: Fabrice Date: Wed, 11 Feb 2026 23:53:46 +0100 Subject: assembling vertices --- src/text.cc | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 6 deletions(-) (limited to 'src/text.cc') diff --git a/src/text.cc b/src/text.cc index 0635440..4306f91 100644 --- a/src/text.cc +++ b/src/text.cc @@ -6,6 +6,7 @@ #include #include "cglm/types.h" +#include "cglm/vec2.h" #include "freetype/freetype.h" #include "hash.h" #include "rendering.h" @@ -28,12 +29,6 @@ static void wayc_font_cache_insert(struct font_s* font, codepoint_t codepoint, enum font_context_error_e wayc_font_context_init(struct font_context_s* ctx) { wayc_notnull(ctx); - u32 vao; - glCreateVertexArrays(1, &vao); - if (vao == 0) return FONT_CONTEXT_ERROR_VAO_CREATION; - - - FT_Error err = FT_Init_FreeType(&ctx->library); if (err) return FONT_CONTEXT_ERROR_LIBRARY_LOADING; return FONT_CONTEXT_ERROR_NONE; @@ -132,3 +127,55 @@ void wayc_font_deinit(struct font_s* font) { FT_Done_Face(font->face); mi_free(font->data); } + +enum text_error_e wayc_text_vertices(struct text_s* text, + struct text_vertex_s** out) { + wayc_notnull(text); + wayc_notnull(out); + + bool success = false; + + usize nverts = WAYC_LETTER_NVERTICES * wayc_string_length(&text->string); + usize verts_bytes = nverts * sizeof(struct text_vertex_s); + + struct text_vertex_s* verts = (struct text_vertex_s*)mi_malloc(verts_bytes); + wayc_defer_cond(mi_free(verts), success, true); + + vec2 pen = GLM_VEC2_ZERO_INIT; + usize idx = 0; + for (usize i = 0; i < wayc_string_length(&text->string); i++) { + codepoint_t codep = + wayc_string_data(&text->string)[i]; // TODO: do proper UTF-8 decoding + + struct glyph_s glyph; + enum font_error_e err = + wayc_font_lookup(text->font, text->atlas, text->packer, codep, &glyph); + if (err != FONT_ERROR_NONE) return TEXT_ERROR_ATLAS; + + f32 x0 = WAYC_X(pen) + glyph.bearing_x; + f32 y0 = WAYC_Y(pen) - glyph.bearing_y; + f32 x1 = x0 + WAYC_X(glyph.uv1) - WAYC_X(glyph.uv0); + f32 y1 = y0 + WAYC_Y(glyph.uv1) - WAYC_Y(glyph.uv0); + + f32 u0 = (f32)WAYC_X(glyph.uv0) / text->atlas->width; + f32 v0 = (f32)WAYC_Y(glyph.uv0) / text->atlas->height; + f32 u1 = (f32)WAYC_X(glyph.uv1) / text->atlas->width; + f32 v1 = (f32)WAYC_Y(glyph.uv1) / text->atlas->height; + + struct text_vertex_s ps0 = {{x0, y0}, {u0, v0}}; + struct text_vertex_s ps1 = {{x0, y1}, {u0, v1}}; + struct text_vertex_s ps2 = {{x1, y1}, {u1, v1}}; + struct text_vertex_s ps3 = {{x1, y0}, {u1, v0}}; + + verts[idx++] = ps0; + verts[idx++] = ps1; + verts[idx++] = ps2; + verts[idx++] = ps0; + verts[idx++] = ps2; + verts[idx++] = ps3; + } + + success = true; + *out = verts; + return TEXT_ERROR_NONE; +} \ No newline at end of file -- cgit v1.2.3