From 555cfbe1ceeb879ecefcf8666225d3c28a19e6df Mon Sep 17 00:00:00 2001 From: Fabrice Date: Wed, 11 Feb 2026 21:55:20 +0100 Subject: uploading to atlas --- src/rendering.h | 5 ----- src/text.cc | 26 ++++++++++++++++++++++++-- src/text.h | 9 +++++++-- src/wayclock.cc | 4 +++- 4 files changed, 34 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/rendering.h b/src/rendering.h index 8927eff..a1ad270 100644 --- a/src/rendering.h +++ b/src/rendering.h @@ -6,11 +6,6 @@ #include "glad.h" #include "utils.h" -struct uv_s { - ivec2 uv0; - ivec2 uv1; -}; - enum image_type_e { IMAGE_TYPE_2D = GL_TEXTURE_2D, }; diff --git a/src/text.cc b/src/text.cc index 21cee19..fc67626 100644 --- a/src/text.cc +++ b/src/text.cc @@ -5,8 +5,10 @@ #include #include +#include "cglm/ivec2.h" #include "freetype/freetype.h" #include "hash.h" +#include "rendering.h" #include "utils.h" static bool wayc_font_cache_lookup(struct font_s* font, codepoint_t codepoint, @@ -39,7 +41,8 @@ void wayc_font_context_deinit(struct font_context_s* ctx) { } enum font_error_e wayc_font_init(struct font_s* font, - struct font_context_s* ctx, const char* path) { + struct font_context_s* ctx, u32 fsize, + const char* path) { wayc_notnull(font); wayc_notnull(ctx); wayc_notnull(path); @@ -62,9 +65,14 @@ enum font_error_e wayc_font_init(struct font_s* font, FT_Error err = FT_New_Memory_Face(ctx->library, data, size, 0, &font->face); if (err) return FONT_ERROR_FONT_LOAD; + wayc_defer_cond(FT_Done_Face(font->face);, success, true); + + err = FT_Set_Pixel_Sizes(font->face, 0, fsize); + if (err) return FONT_ERROR_FONT_LOAD; font->data = data; wayc_hashmap_init(&font->cache); + glm_ivec2_zero(font->cursor); success = true; return FONT_ERROR_NONE; @@ -87,7 +95,21 @@ enum font_error_e wayc_font_lookup(struct font_s* font, struct atlas_s* atlas, FT_GlyphSlot slot = face->glyph; FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL); - wayc_panic("not yet implemented"); + FT_Bitmap bitmap = slot->bitmap; + ivec2 size = {(i32)bitmap.width, (i32)bitmap.rows}; + + wayc_atlas_set(atlas, IMAGE_DATA_TYPE_UNSIGNED_BYTE, font->cursor, size[0], + size[1], bitmap.buffer); + + struct glyph_s glyph = {}; + glm_ivec2(font->cursor, glyph.uv0); + glm_ivec2_add(glyph.uv0, size, glyph.uv1); + + wayc_font_cache_insert(font, codepoint, glyph); + glm_ivec2_add(font->cursor, size, font->cursor); + + *out = glyph; + return FONT_ERROR_NONE; } void wayc_font_deinit(struct font_s* font) { diff --git a/src/text.h b/src/text.h index a3ed9d1..09f813d 100644 --- a/src/text.h +++ b/src/text.h @@ -1,5 +1,6 @@ #pragma once +#include "cglm/types.h" #include "freetype/freetype.h" #include "hash.h" #include "rendering.h" @@ -19,7 +20,9 @@ struct font_context_s { enum font_context_error_e wayc_font_context_init(struct font_context_s* ctx); void wayc_font_context_deinit(struct font_context_s* ctx); -struct glyph_s {}; +struct glyph_s { + ivec2 uv0, uv1; +}; enum font_error_e { FONT_ERROR_NONE, @@ -32,10 +35,12 @@ struct font_s { u8* data; FT_Face face; struct hashmap_s cache; + ivec2 cursor; }; enum font_error_e wayc_font_init(struct font_s* font, - struct font_context_s* ctx, const char* path); + struct font_context_s* ctx, u32 fsize, + const char* path); enum font_error_e wayc_font_lookup(struct font_s* font, struct atlas_s* atlas, codepoint_t codepoint, struct glyph_s* out); void wayc_font_deinit(struct font_s* font); diff --git a/src/wayclock.cc b/src/wayclock.cc index a34a0b8..d5f1daa 100644 --- a/src/wayclock.cc +++ b/src/wayclock.cc @@ -14,6 +14,7 @@ #define WAYC_FONT "AdwaitaMono-Regular.ttf" #define WAYC_ATLAS_WIDTH 512 #define WAYC_ATLAS_HEIGHT 512 +#define WAYC_FONT_SIZE 48 struct app_s { struct renderer_s* renderer; @@ -68,7 +69,8 @@ int main() { wayc_defer(wayc_font_context_deinit(&fctx)); struct font_s font; - if (wayc_font_init(&font, &fctx, WAYC_FONT) != FONT_ERROR_NONE) + if (wayc_font_init(&font, &fctx, WAYC_FONT_SIZE, WAYC_FONT) != + FONT_ERROR_NONE) wayc_panic("Failed to load font"); wayc_defer(wayc_font_deinit(&font)); -- cgit v1.2.3