From a749a95840af1d2d17d1f48440231da64c92ce90 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Wed, 15 Feb 2023 17:38:58 -0500 Subject: font.c cleanup find_font_index can use font_filename_matches_loaded_id() font_path_to_glyph_path doesn't need strcat Change-Id: I8d1d36a68abbc700078d651eed930035641b6240 --- firmware/export/font.h | 2 +- firmware/font.c | 51 ++++++++++++++++++++++---------------------------- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/firmware/export/font.h b/firmware/export/font.h index a0a7c37ba9..2334a8bd1a 100644 --- a/firmware/export/font.h +++ b/firmware/export/font.h @@ -117,7 +117,7 @@ struct font { /* font routines*/ void font_init(void) INIT_ATTR; -bool font_filename_matches_loaded_id(int font_id, char *filename); +bool font_filename_matches_loaded_id(int font_id, const char *filename); int font_load(const char *path); int font_load_ex(const char *path, size_t buffer_size, int glyphs); void font_unload(int font_id); diff --git a/firmware/font.c b/firmware/font.c index 97fee16dc2..01f187df45 100644 --- a/firmware/font.c +++ b/firmware/font.c @@ -326,44 +326,36 @@ static struct font* font_load_cached(struct font* pf, return pf; } - -static int find_font_index(const char* path) +bool font_filename_matches_loaded_id(int font_id, const char *filename) { - int index = 0, handle; - - while (index < MAXFONTS) + if ( font_id >= 0 && font_id < MAXFONTS ) { - handle = buflib_allocations[index]; + int handle = buflib_allocations[font_id]; if (handle > 0) { struct buflib_alloc_data *data = core_get_data(handle); - if(!strcmp(data->path, path)) - { - logf("%s Found id: [%d], %s", __func__, index, path); - return index; - } + logf("%s id: [%d], %s", __func__, font_id, data->path); + return strcmp(data->path, filename) == 0; } - - index++; } - logf("%s %s Not found using id: [%d], FONT_SYSFIXED", - __func__, path, FONT_SYSFIXED); - return FONT_SYSFIXED; + return false; } -bool font_filename_matches_loaded_id(int font_id, char *filename) +static int find_font_index(const char* path) { - if ( font_id >= 0 && font_id < MAXFONTS ) + for(int index = 0; index < MAXFONTS; index++) { - int handle = buflib_allocations[font_id]; - if (handle > 0) + if(font_filename_matches_loaded_id(index, path)) { - struct buflib_alloc_data *data = core_get_data(handle); - logf("%s id: [%d], %s", __func__, font_id, data->path); - return strcmp(data->path, filename) == 0; + logf("%s Found id: [%d], %s", __func__, index, path); + return index; } + + index++; } - return false; + logf("%s %s Not found using id: [%d], FONT_SYSFIXED", + __func__, path, FONT_SYSFIXED); + return FONT_SYSFIXED; } static size_t font_glyphs_to_bufsize(struct font *pf, int glyphs) @@ -541,8 +533,8 @@ int font_load_ex( const char *path, size_t buf_size, int glyphs ) return -1; } struct buflib_alloc_data *pdata; - core_pin(handle); - pdata = core_get_data(handle); + + pdata = core_get_data_pinned(handle); pdata->refcount = 1; pdata->path = pdata->buffer + bufsize; /* save load path so we can recognize this font later */ @@ -599,7 +591,7 @@ int font_load_ex( const char *path, size_t buf_size, int glyphs ) } buflib_allocations[font_id] = handle; //printf("%s -> [%d] -> %d\n", path, font_id, *handle); - lock_font_handle( handle, false ); + core_put_data_pinned(pdata); logf("%s id: [%d], %s", __func__, font_id, path); return font_id; /* success!*/ } @@ -882,8 +874,9 @@ static void font_path_to_glyph_path( const char *font_path, char *glyph_path) { /* take full file name, cut extension, and add .glyphcache */ strmemccpy(glyph_path, font_path, MAX_PATH); - glyph_path[strlen(glyph_path)- sizeof(FONT_EXT)] = '\0'; - strcat(glyph_path, "." GLYPH_CACHE_EXT); + int dotidx = strlen(glyph_path) - sizeof(FONT_EXT); + strmemccpy(glyph_path + dotidx, "." GLYPH_CACHE_EXT, MAX_PATH - dotidx); + logf("%s %s", __func__, glyph_path); } /* call with NULL to flush */ -- cgit v1.2.3