summaryrefslogtreecommitdiff
path: root/src/text.cc
diff options
context:
space:
mode:
authorFabrice <fabrice@schaub-dev.xyz>2026-02-13 07:09:38 +0100
committerFabrice <fabrice@schaub-dev.xyz>2026-02-13 07:09:38 +0100
commite40037fd7a922132015340a758468cb5dec71096 (patch)
tree5f57004ffc0bed7c0d1b2e3ae5a45df104d9eabd /src/text.cc
parent2600ac4737d0098d650b792a30ccc557f6d76d16 (diff)
rasterizing and caching
Diffstat (limited to 'src/text.cc')
-rw-r--r--src/text.cc36
1 files changed, 31 insertions, 5 deletions
diff --git a/src/text.cc b/src/text.cc
index b703ad1..6de3506 100644
--- a/src/text.cc
+++ b/src/text.cc
@@ -4,7 +4,10 @@
#include <cstring>
#include "atlas.h"
+#include "cglm/types.h"
+#include "cglm/vec2.h"
#include "freetype/freetype.h"
+#include "freetype/fttypes.h"
#include "hash.h"
#include "shaders.h"
#include "utils.h"
@@ -124,9 +127,10 @@ enum font_error_e wayc_font_init(struct font_s* font,
FT_Face face;
FT_Error fterr = FT_New_Face(context->ft, path, 0, &face);
if (fterr) return FONT_ERROR_LOAD_FACE;
-
wayc_defer_cond(FT_Done_Face(face), success, true);
+ FT_Set_Pixel_Sizes(face, 0, font_size);
+
struct atlas_s atlas;
enum atlas_error_e aterr =
wayc_atlas_init(&atlas, atlas_width, atlas_height, SG_PIXELFORMAT_R8);
@@ -146,14 +150,36 @@ enum font_error_e wayc_font_render(struct font_s* font, codepoint_t codepoint,
wayc_notnull(font);
wayc_notnull(glyph);
- struct glyph_s cached;
- if (wayc_hashmap_get(&font->cached, &codepoint, &cached)) {
- *glyph = cached;
+ struct glyph_s got;
+ if (wayc_hashmap_get(&font->cached, &codepoint, &got)) {
+ *glyph = got;
return FONT_ERROR_NONE;
}
-
+ u32 index = FT_Get_Char_Index(font->face, codepoint);
+ if (index == 0) return FONT_ERROR_LOAD_GLYPH;
+
+ FT_Error fterr = FT_Load_Glyph(font->face, index, FT_LOAD_RENDER);
+ if (fterr) return FONT_ERROR_LOAD_GLYPH;
+
+ FT_GlyphSlot slot = font->face->glyph;
+ FT_Bitmap bitmap = slot->bitmap;
+
+ 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;
+
+ vec2 bearing = {(f32)slot->bitmap_left, (f32)slot->bitmap_top};
+ f32 advance = (f32)slot->advance.x / WAYC_SCALE;
+
+ glm_vec2_copy(uv0, got.uv0);
+ glm_vec2_copy(uv1, got.uv1);
+ glm_vec2_copy(bearing, got.bearing);
+ got.advance = advance;
+ wayc_hashmap_insert(&font->cached, &codepoint, &got);
return FONT_ERROR_NONE;
}