#include "text.h" #include #include #include #include #include "freetype/fttypes.h" #include "hash.h" #include "utils.h" static void wayc_font_cache_init(struct font_cache_s* cache) { wayc_notnull(cache); wayc_hashmap_init(&cache->glyphs); } static void wayc_font_cache_deinit(struct font_cache_s* cache) { wayc_notnull(cache); wayc_hashmap_deinit(&cache->glyphs); } 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; 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, 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; font->data = data; wayc_font_cache_init(&font->cache); success = true; return FONT_ERROR_NONE; } void wayc_font_deinit(struct font_s* font) { wayc_notnull(font); wayc_font_cache_deinit(&font->cache); FT_Done_Face(font->face); mi_free(font->data); }