summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFabrice <fabrice@schaub-dev.xyz>2026-02-12 15:25:37 +0100
committerFabrice <fabrice@schaub-dev.xyz>2026-02-12 15:25:37 +0100
commit65ce5859ec10849fa71a42273038db2a820d3fb7 (patch)
tree394a23b37518b9b1e4fcfd1f9436c0e18fa4fc41 /src
parent716cccb220a9e2337320ac591d575a2f68b4ed2c (diff)
reworking text rendering
Diffstat (limited to 'src')
-rw-r--r--src/geometry.h1
-rw-r--r--src/text.cc231
-rw-r--r--src/text.h78
-rw-r--r--src/wayclock.cc19
4 files changed, 1 insertions, 328 deletions
diff --git a/src/geometry.h b/src/geometry.h
index d15b8ae..19698c6 100644
--- a/src/geometry.h
+++ b/src/geometry.h
@@ -3,7 +3,6 @@
#include <cglm/cglm.h>
#include "cglm/types.h"
-#include "glad.h"
#include "utils.h"
struct quad_s {
diff --git a/src/text.cc b/src/text.cc
deleted file mode 100644
index b52e9fe..0000000
--- a/src/text.cc
+++ /dev/null
@@ -1,231 +0,0 @@
-#include "text.h"
-
-#include <fcntl.h>
-#include <mimalloc.h>
-#include <unistd.h>
-#include <utils.h>
-
-#include "cglm/types.h"
-#include "freetype/freetype.h"
-#include "geometry.h"
-#include "hash.h"
-#include "utils.h"
-
-static bool wayc_font_cache_lookup(struct font_s* font, codepoint_t codepoint,
- struct glyph_s* out) {
- struct glyph_s glyph;
- bool ok = wayc_hashmap_get(&font->cache, &codepoint, &glyph);
- if (ok) *out = glyph;
- return ok;
-}
-/*
-static void wayc_font_cache_insert(struct font_s* font, codepoint_t codepoint,
- struct glyph_s glyph) {
- wayc_notnull(font);
- wayc_hashmap_insert(&font->cache, &codepoint, &glyph);
-} */
-
-enum font_context_error_e wayc_font_context_init(struct font_context_s* ctx) {
- wayc_notnull(ctx);
-
- FT_Error err = FT_Init_FreeType(&ctx->library);
- if (err) return FONT_CONTEXT_ERROR_LIBRARY_LOADING;
- return FONT_CONTEXT_ERROR_NONE;
-}
-
-void wayc_font_context_deinit(struct font_context_s* ctx) {
- wayc_notnull(ctx);
-
- FT_Done_FreeType(ctx->library);
-}
-
-enum font_error_e wayc_font_init(struct font_s* font,
- struct font_context_s* ctx, u32 fsize,
- const char* path) {
- wayc_notnull(font);
- wayc_notnull(ctx);
- wayc_notnull(path);
-
- bool success = false;
-
- i32 fd = open(path, O_RDONLY);
- if (fd < 0) return FONT_ERROR_FILE_LOAD;
- wayc_defer(close(fd));
-
- isize size = lseek(fd, 0, SEEK_END);
- if (size < 0) return FONT_ERROR_FILE_LOAD;
- lseek(fd, 0, SEEK_SET);
-
- u8* data = (u8*)mi_malloc(size);
- wayc_defer_cond({ mi_free(data); }, success, true);
-
- isize _read = read(fd, data, size);
- if (_read < 0 || _read != size) return FONT_ERROR_FILE_LOAD;
-
- 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);
-
- success = true;
- return FONT_ERROR_NONE;
-}
-
-enum font_error_e wayc_font_lookup(struct font_s* font, codepoint_t codepoint,
- struct glyph_s* out) {
- wayc_notnull(font);
- wayc_notnull(out);
- FT_Face face = font->face;
-
- bool found = wayc_font_cache_lookup(font, codepoint, out);
- if (found) return FONT_ERROR_NONE;
-
- FT_UInt glyph_index = FT_Get_Char_Index(face, codepoint);
- if (glyph_index == 0) return FONT_ERROR_NOT_MATCH;
-
- FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
- FT_GlyphSlot slot = face->glyph;
- FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
-
- FT_Bitmap bitmap = slot->bitmap;
- if (bitmap.pitch != (i32)bitmap.width) wayc_panic("unsupported pitch");
-
- /*
- ivec2 size = {(i32)bitmap.width, (i32)bitmap.rows};
- ivec2 uv0;
-
- bool ok = wayc_atlas_packer_allocate(packer, WAYC_X(size), WAYC_Y(size),
- uv0); if (!ok) return FONT_ERROR_ATLAS_FULL;
-
- ok = wayc_atlas_set(atlas, IMAGE_DATA_TYPE_UNSIGNED_BYTE, uv0, WAYC_X(size),
- WAYC_Y(size), IMAGE_FORMAT_RED_ALIGNMENT,
- bitmap.buffer); if (!ok) return FONT_ERROR_ATLAS_FULL;
-
- struct glyph_s glyph = {};
- glyph.bearing_x = slot->bitmap_left;
- glyph.bearing_y = slot->bitmap_top;
- glyph.advance = (f32)slot->advance.x / WAYC_SCALE;
-
- glyph.uv0[0] = uv0[0];
- glyph.uv0[1] = uv0[1];
- glyph.uv1[0] = uv0[0] + size[0];
- glyph.uv1[1] = uv0[1] + size[1];
-
- wayc_font_cache_insert(font, codepoint, glyph);
- *out = glyph;
-
- */
- wayc_panic("Atlas packing not implemented yet");
- return FONT_ERROR_NONE;
-}
-
-void wayc_font_deinit(struct font_s* font) {
- wayc_notnull(font);
-
- wayc_hashmap_deinit(&font->cache);
- FT_Done_Face(font->face);
- mi_free(font->data);
-}
-/*
-static void wayc_text_assemble_one(const struct glyph_s* glyph, vec2* pen,
- f32 atlas_width, f32 atlas_height,
- struct text_vertex_s* out_verts,
- text_index_t* out_indices,
- text_index_t base) {
- wayc_notnull(glyph);
- wayc_notnull(pen);
- wayc_notnull(out_verts);
- wayc_notnull(out_indices);
-
- const f32 left = WAYC_X(*pen) + glyph->bearing_x;
- const f32 top = WAYC_Y(*pen) - glyph->bearing_y;
- const f32 right = left + (WAYC_X(glyph->uv1) - WAYC_X(glyph->uv0));
- const f32 bottom = top + (WAYC_Y(glyph->uv1) - WAYC_Y(glyph->uv0));
-
- const f32 uv_left = (f32)WAYC_X(glyph->uv0) / atlas_width;
- const f32 uv_top = (f32)WAYC_Y(glyph->uv0) / atlas_height;
- const f32 uv_right = (f32)WAYC_X(glyph->uv1) / atlas_width;
- const f32 uv_bottom = (f32)WAYC_Y(glyph->uv1) / atlas_height;
-
- struct quad_s quad;
- wayc_quad_init(&quad, left, top, right, bottom);
-
- out_verts[0] = {{WAYC_X(quad.vertices[0]), WAYC_Y(quad.vertices[0])},
- {uv_left, uv_top}};
- out_verts[1] = {{WAYC_X(quad.vertices[1]), WAYC_Y(quad.vertices[1])},
- {uv_right, uv_top}};
- out_verts[2] = {{WAYC_X(quad.vertices[2]), WAYC_Y(quad.vertices[2])},
- {uv_right, uv_bottom}};
- out_verts[3] = {{WAYC_X(quad.vertices[3]), WAYC_Y(quad.vertices[3])},
- {uv_left, uv_bottom}};
-
-#pragma GCC unroll 6
- for (usize i = 0; i < WAYC_QUAD_NINDICES; ++i)
- out_indices[i] = base + (text_index_t)quad.indices[i];
-} */
-
-enum text_error_e wayc_text_assemble(struct text_s* text, usize* out_size,
- text_index_t** out_indices,
- struct text_vertex_s** out_verts) {
- wayc_notnull(text);
- wayc_notnull(out_size);
- wayc_notnull(out_indices);
- wayc_notnull(out_verts);
-
- /*
- bool success = false;
- usize glyph_count = wayc_string_length(&text->string);
-
- usize vertex_count = glyph_count * WAYC_QUAD_NVERTS;
- usize index_count = glyph_count * WAYC_QUAD_NINDICES;
-
- usize vertex_bytes = vertex_count * sizeof(struct text_vertex_s);
- usize index_bytes = index_count * sizeof(text_index_t);
-
- struct text_vertex_s* vertices =
- (struct text_vertex_s*)mi_malloc(vertex_bytes);
- wayc_defer_cond(mi_free(vertices), success, true);
-
- text_index_t* indices = (text_index_t*)mi_malloc(index_bytes);
- wayc_defer_cond(mi_free(indices), success, true);
-
- struct atlas_s* atlas = text->atlas;
- f32 atlas_width = (f32)atlas->width;
- f32 atlas_height = (f32)atlas->height;
-
- vec2 pen = {0, 0};
- for (usize i = 0; i < glyph_count; i++) {
- codepoint_t codepoint = text->string.data[i];
- struct glyph_s glyph;
- enum font_error_e err = wayc_font_lookup(text->font, text->atlas,
- text->packer, codepoint, &glyph);
- if (err != FONT_ERROR_NONE) return TEXT_ERROR_ATLAS;
-
- usize vertex_offset = i * WAYC_QUAD_NVERTS;
- usize index_offset = i * WAYC_QUAD_NINDICES;
-
- struct text_vertex_s* vertices_writer = &vertices[vertex_offset];
- text_index_t* indices_writer = &indices[index_offset];
-
- text_index_t base = (text_index_t)vertex_offset;
-
- wayc_text_assemble_one(&glyph, &pen, atlas_width, atlas_height,
- vertices_writer, indices_writer, base);
-
- pen[0] += glyph.advance;
- }
-
- *out_size = index_count;
- *out_indices = indices;
- *out_verts = vertices;
-
- success = true;
- */
- wayc_panic("Text assembly not implemented yet");
- return TEXT_ERROR_NONE;
-} \ No newline at end of file
diff --git a/src/text.h b/src/text.h
deleted file mode 100644
index 9ea7d0d..0000000
--- a/src/text.h
+++ /dev/null
@@ -1,78 +0,0 @@
-#pragma once
-
-#include "cglm/types.h"
-#include "freetype/freetype.h"
-#include "hash.h"
-#include "utils.h"
-
-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;
-};
-
-typedef u32 text_index_t;
-
-enum font_context_error_e {
- FONT_CONTEXT_ERROR_NONE,
- FONT_CONTEXT_ERROR_LIBRARY_LOADING,
- FONT_CONTEXT_ERROR_VAO_CREATION,
-};
-
-struct font_context_s {
- FT_Library library;
-};
-
-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 {
- ivec2 uv0, uv1;
-
- i32 bearing_x, bearing_y;
- f32 advance;
-};
-
-enum font_error_e {
- FONT_ERROR_NONE,
- FONT_ERROR_FILE_LOAD,
- FONT_ERROR_FONT_LOAD,
- FONT_ERROR_NOT_MATCH,
- FONT_ERROR_ATLAS_FULL,
-};
-
-struct font_s {
- u8* data;
- FT_Face face;
- struct hashmap_s<codepoint_t, struct glyph_s> cache;
-};
-
-enum font_error_e wayc_font_init(struct font_s* font,
- struct font_context_s* ctx, u32 fsize,
- const char* path);
-enum font_error_e wayc_font_lookup(struct font_s* font, 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_assemble(struct text_s* text, usize* out_size,
- text_index_t** out_indices,
- struct text_vertex_s** out_verts);
diff --git a/src/wayclock.cc b/src/wayclock.cc
index 37b4fa9..ef983ed 100644
--- a/src/wayclock.cc
+++ b/src/wayclock.cc
@@ -2,7 +2,6 @@
#include "events.h"
#include "graphics.h"
-#include "text.h"
#include "utils.h"
#include "window.h"
@@ -17,9 +16,6 @@
struct app_s {
struct renderer_s* renderer;
- struct font_context_s* fctx;
- struct font_s* font;
- struct atlas_s* atlas;
};
void wayc_frame(struct app_s* app, struct window_s* window,
@@ -61,19 +57,6 @@ void wayc_handle(u8* user, struct eventloop_s* loop, struct event_s* event) {
}
int main() {
- struct font_context_s fctx;
- if (wayc_font_context_init(&fctx) != FONT_CONTEXT_ERROR_NONE)
- wayc_panic("Failed to initialize font context");
-
- wayc_defer(wayc_font_context_deinit(&fctx));
-
- struct font_s font;
- 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));
-
struct eventloop_s loop;
if (wayc_eventloop_init(&loop, wayc_handle) != EVENTLOOP_ERROR_NONE)
wayc_panic("Failed to initialize event loop");
@@ -110,7 +93,7 @@ int main() {
wayc_window_redraw(&window, &loop);
- struct app_s app = {&renderer, &fctx, &font, nullptr};
+ struct app_s app = {&renderer};
while (wayc_eventloop_running(&loop)) {
wayc_eventloop_update(&loop, (u8*)&app);