twn_text: embed default font

This commit is contained in:
veclavtalica
2025-03-07 03:35:35 +03:00
parent b97a155de4
commit 13bc71a28d
4 changed files with 478 additions and 8 deletions

View File

@ -43,6 +43,14 @@ typedef struct FontFileCacheItem {
static FontFileCacheItem *font_file_cache_hash;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreserved-identifier"
extern uint8_t _binary_share_assets_Dernyns256_ttf_start[];
extern uint8_t _binary_share_assets_Dernyns256_ttf_end[];
#pragma GCC diagnostic pop
static void string_arena_init(StringArena *arena) {
arena->head = cmalloc(sizeof *arena->head);
@ -121,7 +129,13 @@ static FontData *text_load_font_data(const char *path, int height_px) {
buf = font_file_ptr->value.buffer;
buf_len = font_file_ptr->value.len;
} else {
buf_len = file_to_bytes(path, &buf);
/* TODO: use and reuse this on fonts that are not found as well */
if (SDL_strncmp(path, "!", 1) == 0) {
buf_len = _binary_share_assets_Dernyns256_ttf_end - _binary_share_assets_Dernyns256_ttf_start;
buf = _binary_share_assets_Dernyns256_ttf_start;
} else {
buf_len = file_to_bytes(path, &buf);
}
if (buf_len == -1) {
/* TODO: have a fallback default font */
log_warn("Font %s not found", path);
@ -284,7 +298,8 @@ void text_cache_deinit(TextCache *cache) {
}
for (size_t i = 0; i < shlenu(font_file_cache_hash); ++i) {
SDL_free(font_file_cache_hash[i].value.buffer);
if (font_file_cache_hash[i].value.buffer != _binary_share_assets_Dernyns256_ttf_start)
SDL_free(font_file_cache_hash[i].value.buffer);
}
shfree(font_file_cache_hash);
@ -300,11 +315,7 @@ void text_cache_reset_arena(TextCache *cache) {
void draw_text(const char *string, Vec2 position, float height, Color color, const char *font) {
if (!font) {
log_warn("Default font isn't yet implemented");
return;
}
if (!font) font = "!";
if (!ensure_font_cache(font, (int)height))
return;
@ -334,6 +345,7 @@ void draw_text(const char *string, Vec2 position, float height, Color color, con
float draw_text_width(const char *string, float height, const char *font) {
if (!font) font = "!";
if (!ensure_font_cache(font, (int)height))
return 0;