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
This commit is contained in:
William Wilgus 2023-02-15 17:38:58 -05:00 committed by William Wilgus
parent e7a01ca4b2
commit a749a95840
2 changed files with 25 additions and 32 deletions

View file

@ -117,7 +117,7 @@ struct font {
/* font routines*/ /* font routines*/
void font_init(void) INIT_ATTR; 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(const char *path);
int font_load_ex(const char *path, size_t buffer_size, int glyphs); int font_load_ex(const char *path, size_t buffer_size, int glyphs);
void font_unload(int font_id); void font_unload(int font_id);

View file

@ -326,32 +326,7 @@ static struct font* font_load_cached(struct font* pf,
return pf; return pf;
} }
bool font_filename_matches_loaded_id(int font_id, const char *filename)
static int find_font_index(const char* path)
{
int index = 0, handle;
while (index < MAXFONTS)
{
handle = buflib_allocations[index];
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;
}
}
index++;
}
logf("%s %s Not found using id: [%d], FONT_SYSFIXED",
__func__, path, FONT_SYSFIXED);
return FONT_SYSFIXED;
}
bool font_filename_matches_loaded_id(int font_id, char *filename)
{ {
if ( font_id >= 0 && font_id < MAXFONTS ) if ( font_id >= 0 && font_id < MAXFONTS )
{ {
@ -366,6 +341,23 @@ bool font_filename_matches_loaded_id(int font_id, char *filename)
return false; return false;
} }
static int find_font_index(const char* path)
{
for(int index = 0; index < MAXFONTS; index++)
{
if(font_filename_matches_loaded_id(index, path))
{
logf("%s Found id: [%d], %s", __func__, index, path);
return index;
}
index++;
}
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) static size_t font_glyphs_to_bufsize(struct font *pf, int glyphs)
{ {
size_t bufsize; size_t bufsize;
@ -541,8 +533,8 @@ int font_load_ex( const char *path, size_t buf_size, int glyphs )
return -1; return -1;
} }
struct buflib_alloc_data *pdata; struct buflib_alloc_data *pdata;
core_pin(handle);
pdata = core_get_data(handle); pdata = core_get_data_pinned(handle);
pdata->refcount = 1; pdata->refcount = 1;
pdata->path = pdata->buffer + bufsize; pdata->path = pdata->buffer + bufsize;
/* save load path so we can recognize this font later */ /* 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; buflib_allocations[font_id] = handle;
//printf("%s -> [%d] -> %d\n", path, 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); logf("%s id: [%d], %s", __func__, font_id, path);
return font_id; /* success!*/ 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 */ /* take full file name, cut extension, and add .glyphcache */
strmemccpy(glyph_path, font_path, MAX_PATH); strmemccpy(glyph_path, font_path, MAX_PATH);
glyph_path[strlen(glyph_path)- sizeof(FONT_EXT)] = '\0'; int dotidx = strlen(glyph_path) - sizeof(FONT_EXT);
strcat(glyph_path, "." GLYPH_CACHE_EXT); strmemccpy(glyph_path + dotidx, "." GLYPH_CACHE_EXT, MAX_PATH - dotidx);
logf("%s %s", __func__, glyph_path);
} }
/* call with NULL to flush */ /* call with NULL to flush */