diff options
| author | Fabrice <fabrice@schaub-dev.xyz> | 2026-02-11 16:24:21 +0100 |
|---|---|---|
| committer | Fabrice <fabrice@schaub-dev.xyz> | 2026-02-11 16:24:21 +0100 |
| commit | 05078f4561c45a60cd208a9d3037ad1925a7e80a (patch) | |
| tree | bceeff05d3221b56f112c8ff6c3b064cb2bdacc9 /src/text.cc | |
| parent | cf3e6ac4c968b09b4e431b111162aea6041f47a6 (diff) | |
use err defers
Diffstat (limited to 'src/text.cc')
| -rw-r--r-- | src/text.cc | 20 |
1 files changed, 14 insertions, 6 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; } |
