diff options
| author | Fabrice <fabrice@schaub-dev.xyz> | 2026-02-11 23:53:46 +0100 |
|---|---|---|
| committer | Fabrice <fabrice@schaub-dev.xyz> | 2026-02-11 23:53:46 +0100 |
| commit | 6013bac1556659f1685170b5e68752189f0b93ba (patch) | |
| tree | 166edafb021b49144211410c109a404fe44a2101 /src | |
| parent | 50a84a54e7cc2308ad87bd04e065151a61ffe513 (diff) | |
assembling vertices
Diffstat (limited to 'src')
| -rw-r--r-- | src/text.cc | 59 | ||||
| -rw-r--r-- | src/text.h | 22 | ||||
| -rw-r--r-- | src/utils.h | 20 |
3 files changed, 94 insertions, 7 deletions
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 <utils.h> #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 @@ -8,6 +8,9 @@ typedef u32 codepoint_t; +#define WAYC_LETTER_NTRIANGLES 2 +#define WAYC_LETTER_NVERTICES (WAYC_LETTER_NTRIANGLES * WAYC_TRIANGLE_NVERTS) + struct text_vertex_s { vec2 pos; vec2 uv; @@ -21,7 +24,6 @@ enum font_context_error_e { struct font_context_s { FT_Library library; - u32 vao; }; enum font_context_error_e wayc_font_context_init(struct font_context_s* ctx); @@ -55,3 +57,21 @@ enum font_error_e wayc_font_lookup(struct font_s* font, struct atlas_s* atlas, struct atlas_packer_s* packer, codepoint_t codepoint, struct glyph_s* out); void wayc_font_deinit(struct font_s* font); + +enum text_error_e { + TEXT_ERROR_NONE, + TEXT_ERROR_ATLAS, +}; + +struct text_s { + struct font_s* font; + struct atlas_s* atlas; + struct atlas_packer_s* packer; + struct string_s string; +}; + +#define WAYC_TEXT_INIT(font, atlas, packer, string) \ + text_s { font, atlas, packer, string } + +enum text_error_e wayc_text_vertices(struct text_s* text, + struct text_vertex_s** out); diff --git a/src/utils.h b/src/utils.h index 00be767..5a47c44 100644 --- a/src/utils.h +++ b/src/utils.h @@ -77,6 +77,8 @@ static inline u32 wayc_min(u32 a, u32 b) { return a > b ? a : b; } #define WAYC_X(v) ((v)[0]) #define WAYC_Y(v) ((v)[1]) +#define WAYC_TRIANGLE_NVERTS 3 + static inline usize wayc_max(usize a, usize b) { return a > b ? a : b; } template <typename A, typename B> @@ -117,3 +119,21 @@ struct defer_cond_s { #define wayc_defer_cond(func, cond, expected) \ auto WAYC_UNIQUE(_defer_) = defer_cond_s([&]() { func; }, &cond, expected) + +struct string_s { + char* data; + usize length; +}; + +#define WAYC_STRING_INIT(str, len) \ + string_s { str, len } + +static inline usize wayc_string_length(const struct string_s* str) { + wayc_notnull(str); + return str->length; +} + +static inline char* wayc_string_data(const struct string_s* str) { + wayc_notnull(str); + return str->data; +}
\ No newline at end of file |
