diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/text.cc | 20 | ||||
| -rw-r--r-- | src/text.h | 8 | ||||
| -rw-r--r-- | src/utils.h | 20 |
3 files changed, 41 insertions, 7 deletions
diff --git a/src/text.cc b/src/text.cc index 43ee74e..a5e3331 100644 --- a/src/text.cc +++ b/src/text.cc @@ -5,6 +5,7 @@ #include <unistd.h> #include <utils.h> +#include "freetype/fttypes.h" #include "utils.h" enum font_context_error_e wayc_font_context_init(struct font_context_s* ctx) { @@ -27,21 +28,28 @@ enum font_error_e wayc_font_init(struct font_s* 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)); - usize size = lseek(fd, 0, SEEK_END); + isize size = lseek(fd, 0, SEEK_END); if (size < 0) return FONT_ERROR_FILE_LOAD; lseek(fd, 0, SEEK_SET); - font->data = (u8*)mi_malloc(size); - usize _read = read(fd, font->data, size); - if (_read != size) return FONT_ERROR_FILE_LOAD; + 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, (const FT_Byte*)font->data, - size, 0, &font->face); + FT_Error err = FT_New_Memory_Face(ctx->library, data, size, 0, &font->face); if (err) return FONT_ERROR_FONT_LOAD; + + font->data = data; + + success = true; return FONT_ERROR_NONE; } @@ -1,6 +1,7 @@ #pragma once #include "freetype/freetype.h" +#include "hash.h" #include "utils.h" enum font_context_error_e { @@ -15,6 +16,12 @@ 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 font_cache_s { + struct hashmap_s<u32, struct glyph_s> glyphs; +}; + enum font_error_e { FONT_ERROR_NONE, FONT_ERROR_FILE_LOAD, @@ -24,6 +31,7 @@ enum font_error_e { struct font_s { u8* data; FT_Face face; + struct font_cache_s cache; }; enum font_error_e wayc_font_init(struct font_s* font, diff --git a/src/utils.h b/src/utils.h index deba95f..ce19428 100644 --- a/src/utils.h +++ b/src/utils.h @@ -88,4 +88,22 @@ struct defer_s { ~defer_s() { func(); } }; -#define wayc_defer(func) auto WAYC_UNIQUE(_defer_) = defer_s([&]() { func; })
\ No newline at end of file +#define wayc_defer(func) auto WAYC_UNIQUE(_defer_) = defer_s([&]() { func; }) + +template <typename Func, typename S> +struct defer_cond_s { + Func func; + S* signal; + S expected; + + defer_cond_s(Func func, S* cond, S expected) + : func(func), signal(cond), expected(expected) {} + + ~defer_cond_s() { + if (*signal == expected) return; + func(); + } +}; + +#define wayc_defer_cond(func, cond, expected) \ + auto WAYC_UNIQUE(_defer_) = defer_cond_s([&]() { func; }, &cond, expected)
\ No newline at end of file |
