#pragma once #include "atlas.h" #include "cglm/types.h" #include "freetype/freetype.h" #include "hash.h" #include "sokol_gfx.h" #include "utils.h" struct text_vertex_s { vec2 pos; vec2 uv; }; typedef u32 codepoint_t; enum font_context_error_e : u8 { FONT_CONTEXT_ERROR_NONE = 0, FONT_CONTEXT_ERROR_LOAD_LIBRARY, FONT_CONTEXT_ERROR_CREATE_SAMPLER, FONT_CONTEXT_ERROR_CREATE_SHADER, FONT_CONTEXT_ERROR_CREATE_PIPELINE, }; struct font_context_s { FT_Library ft; struct sg_pipeline pipeline; struct sg_sampler sampler; }; enum font_context_error_e wayc_font_context_init( struct font_context_s* context); void wayc_font_context_deinit(struct font_context_s* context); struct glyph_s { vec2 uv0, uv1; vec2 bearing; f32 advance; }; enum font_error_e : u8 { FONT_ERROR_NONE = 0, FONT_ERROR_LOAD_FACE, FONT_ERROR_ATLAS_FAILED, FONT_ERROR_LOAD_GLYPH, }; struct font_s { struct font_context_s* context; FT_Face face; struct atlas_s atlas; struct hashmap_s cached; }; enum font_error_e wayc_font_init(struct font_s* font, struct font_context_s* context, const char* path, u32 font_size, u32 atlas_width, u32 atlas_height); enum font_error_e wayc_font_raster(struct font_s* font, codepoint_t codepoint, struct glyph_s* glyph); static inline enum font_error_e wayc_font_flush(struct font_s* font) { enum atlas_error_e atlas_err = wayc_atlas_flush(&font->atlas); return (enum font_error_e)wayc_max(atlas_err, FONT_ERROR_NONE); } enum font_error_e wayc_font_raster_text(struct font_s* font, const char* text, usize text_len, struct glyph_s* glyphs, usize max_glyphs); void wayc_font_deinit(struct font_s* font); struct text_mesh_s { struct text_vertex_s* vertices; usize vertex_count; u16* indices; usize index_count; }; bool wayc_text_mesh_init(struct text_mesh_s* mesh, const char* text, usize text_len, struct font_s* font); void wayc_text_mesh_deinit(struct text_mesh_s* mesh);