From d770fd5ac48604f17e2d8a1b96e8c88147d95a84 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 3 May 2023 15:57:37 +1000 Subject: [PATCH] Cleanup: remove unused BLI_dir_* functions Loading a font in BLF would search a list of directory presets however these presets were never used. Remove them as all callers pass in full paths. Also rename FontBLF::name to mem_name as it's only needed as an ID for in-memory fonts (to prevent them being loaded multiple times). Non-unique font loading now compares against the filepath or mem_name when loading from files or memory (before both used `name` which was often the filepath, sometimes the full ID-name). --- source/blender/blenfont/BLF_api.h | 65 +++++------ source/blender/blenfont/intern/blf.c | 62 ++++++---- source/blender/blenfont/intern/blf_dir.c | 106 ------------------ source/blender/blenfont/intern/blf_font.c | 29 +++-- source/blender/blenfont/intern/blf_internal.h | 16 +-- .../blenfont/intern/blf_internal_types.h | 13 +-- 6 files changed, 93 insertions(+), 198 deletions(-) diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h index c1baa071e5c..dcd1ce70496 100644 --- a/source/blender/blenfont/BLF_api.h +++ b/source/blender/blenfont/BLF_api.h @@ -43,28 +43,36 @@ void BLF_cache_flush_set_fn(void (*cache_flush_fn)(void)); /** * Loads a font, or returns an already loaded font and increments its reference count. */ -int BLF_load(const char *name) ATTR_NONNULL(); -int BLF_load_mem(const char *name, const unsigned char *mem, int mem_size) ATTR_NONNULL(); -bool BLF_is_loaded(const char *name) ATTR_NONNULL(); +int BLF_load(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); +int BLF_load_mem(const char *name, const unsigned char *mem, int mem_size) ATTR_WARN_UNUSED_RESULT + ATTR_NONNULL(1, 2); -int BLF_load_unique(const char *name) ATTR_NONNULL(); -int BLF_load_mem_unique(const char *name, const unsigned char *mem, int mem_size) ATTR_NONNULL(); +bool BLF_is_loaded(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); +bool BLF_is_loaded_mem(const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); + +int BLF_load_unique(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); +int BLF_load_mem_unique(const char *name, const unsigned char *mem, int mem_size) + ATTR_NONNULL(1, 2); + +void BLF_unload(const char *filepath) ATTR_NONNULL(1); +#if 0 /* Not needed at the moment. */ +void BLF_unload_mem(const char *name) ATTR_NONNULL(1); +#endif -void BLF_unload(const char *name) ATTR_NONNULL(); void BLF_unload_id(int fontid); void BLF_unload_all(void); -char *BLF_display_name_from_file(const char *filepath); +char *BLF_display_name_from_file(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1); /** * Check if font supports a particular glyph. */ -bool BLF_has_glyph(int fontid, unsigned int unicode); +bool BLF_has_glyph(int fontid, unsigned int unicode) ATTR_WARN_UNUSED_RESULT; /** * Attach a file with metrics information from memory. */ -void BLF_metrics_attach(int fontid, unsigned char *mem, int mem_size); +void BLF_metrics_attach(int fontid, unsigned char *mem, int mem_size) ATTR_NONNULL(2); void BLF_aspect(int fontid, float x, float y, float z); void BLF_position(int fontid, float x, float y, float z); @@ -140,7 +148,7 @@ void BLF_boundbox_foreach_glyph(int fontid, size_t BLF_str_offset_from_cursor_position(int fontid, const char *str, size_t str_len, - int location_x); + int location_x) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2); /** * Return bounds of the glyph rect at the string offset. @@ -148,18 +156,25 @@ size_t BLF_str_offset_from_cursor_position(int fontid, bool BLF_str_offset_to_glyph_bounds(int fontid, const char *str, size_t str_offset, - struct rcti *glyph_bounds); + struct rcti *glyph_bounds) ATTR_WARN_UNUSED_RESULT + ATTR_NONNULL(2, 4); /** * Get the string byte offset that fits within a given width. */ -size_t BLF_width_to_strlen( - int fontid, const char *str, size_t str_len, float width, float *r_width) ATTR_NONNULL(2); +size_t BLF_width_to_strlen(int fontid, + const char *str, + size_t str_len, + float width, + float *r_width) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2); /** * Same as BLF_width_to_strlen but search from the string end. */ -size_t BLF_width_to_rstrlen( - int fontid, const char *str, size_t str_len, float width, float *r_width) ATTR_NONNULL(2); +size_t BLF_width_to_rstrlen(int fontid, + const char *str, + size_t str_len, + float width, + float *r_width) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2); /** * This function return the bounding box of the string @@ -269,26 +284,6 @@ void BLF_draw_buffer_ex(int fontid, const char *str, size_t str_len, struct Resu ATTR_NONNULL(2); void BLF_draw_buffer(int fontid, const char *str, size_t str_len) ATTR_NONNULL(2); -/** - * Add a path to the font dir paths. - */ -void BLF_dir_add(const char *path) ATTR_NONNULL(); - -/** - * Remove a path from the font dir paths. - */ -void BLF_dir_rem(const char *path) ATTR_NONNULL(); - -/** - * Return an array with all the font dir (this can be used for file-selector). - */ -char **BLF_dir_get(int *ndir) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); - -/** - * Free the data return by #BLF_dir_get. - */ -void BLF_dir_free(char **dirs, int count) ATTR_NONNULL(); - /* blf_thumbs.c */ /** diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c index 457007a7f12..4ed4f688410 100644 --- a/source/blender/blenfont/intern/blf.c +++ b/source/blender/blenfont/intern/blf.c @@ -21,6 +21,7 @@ #include "MEM_guardedalloc.h" +#include "BLI_fileops.h" #include "BLI_math.h" #include "BLI_string.h" #include "BLI_threads.h" @@ -93,11 +94,26 @@ bool blf_font_id_is_valid(int fontid) return blf_get(fontid) != NULL; } -static int blf_search(const char *name) +static int blf_search_by_mem_name(const char *mem_name) { for (int i = 0; i < BLF_MAX_FONT; i++) { const FontBLF *font = global_font[i]; - if (font && STREQ(font->name, name)) { + if (font == NULL || font->mem_name == NULL) { + continue; + } + if (font && STREQ(font->mem_name, mem_name)) { + return i; + } + } + + return -1; +} + +static int blf_search_by_filepath(const char *filepath) +{ + for (int i = 0; i < BLF_MAX_FONT; i++) { + const FontBLF *font = global_font[i]; + if (font && STREQ(font->filepath, filepath)) { return i; } } @@ -125,25 +141,30 @@ bool BLF_has_glyph(int fontid, uint unicode) return false; } -bool BLF_is_loaded(const char *name) +bool BLF_is_loaded(const char *filepath) { - return blf_search(name) >= 0; + return blf_search_by_filepath(filepath) >= 0; } -int BLF_load(const char *name) +bool BLF_is_loaded_mem(const char *name) +{ + return blf_search_by_mem_name(name) >= 0; +} + +int BLF_load(const char *filepath) { /* check if we already load this font. */ - int i = blf_search(name); + int i = blf_search_by_filepath(filepath); if (i >= 0) { FontBLF *font = global_font[i]; font->reference_count++; return i; } - return BLF_load_unique(name); + return BLF_load_unique(filepath); } -int BLF_load_unique(const char *name) +int BLF_load_unique(const char *filepath) { /* Don't search in the cache!! make a new * object font, this is for keep fonts threads safe. @@ -154,18 +175,17 @@ int BLF_load_unique(const char *name) return -1; } - char *filepath = blf_dir_search(name); - if (!filepath) { - printf("Can't find font: %s\n", name); + /* This isn't essential, it will just cause confusing behavior to load a font + * that appears to succeed, then doesn't show up. */ + if (!BLI_exists(filepath)) { + printf("Can't find font: %s\n", filepath); return -1; } - FontBLF *font = blf_font_new(name, filepath); - - MEM_freeN(filepath); + FontBLF *font = blf_font_new_from_filepath(filepath); if (!font) { - printf("Can't load font: %s\n", name); + printf("Can't load font: %s\n", filepath); return -1; } @@ -185,7 +205,7 @@ void BLF_metrics_attach(int fontid, uchar *mem, int mem_size) int BLF_load_mem(const char *name, const uchar *mem, int mem_size) { - int i = blf_search(name); + int i = blf_search_by_mem_name(name); if (i >= 0) { // font = global_font[i]; /* UNUSED */ return i; @@ -221,12 +241,15 @@ int BLF_load_mem_unique(const char *name, const uchar *mem, int mem_size) return i; } -void BLF_unload(const char *name) +void BLF_unload(const char *filepath) { for (int i = 0; i < BLF_MAX_FONT; i++) { FontBLF *font = global_font[i]; + if (font == NULL || font->filepath == NULL) { + continue; + } - if (font && STREQ(font->name, name)) { + if (STREQ(font->filepath, filepath)) { BLI_assert(font->reference_count > 0); font->reference_count--; @@ -918,7 +941,8 @@ void BLF_state_print(int fontid) FontBLF *font = blf_get(fontid); if (font) { printf("fontid %d %p\n", fontid, (void *)font); - printf(" name: '%s'\n", font->name); + printf(" mem_name: '%s'\n", font->mem_name ? font->mem_name : ""); + printf(" filepath: '%s'\n", font->filepath ? font->filepath : ""); printf(" size: %f\n", font->size); printf(" pos: %d %d %d\n", UNPACK3(font->pos)); printf(" aspect: (%d) %.6f %.6f %.6f\n", diff --git a/source/blender/blenfont/intern/blf_dir.c b/source/blender/blenfont/intern/blf_dir.c index 3efe34bc3bc..f32241477da 100644 --- a/source/blender/blenfont/intern/blf_dir.c +++ b/source/blender/blenfont/intern/blf_dir.c @@ -21,116 +21,10 @@ #include "DNA_vec_types.h" #include "BLI_fileops.h" -#include "BLI_listbase.h" -#include "BLI_path_util.h" #include "BLI_string.h" -#include "BLI_threads.h" -#include "BLI_utildefines.h" #include "BLF_api.h" #include "blf_internal.h" -#include "blf_internal_types.h" - -static ListBase global_font_dir = {NULL, NULL}; - -static DirBLF *blf_dir_find(const char *path) -{ - DirBLF *p; - - p = global_font_dir.first; - while (p) { - if (BLI_path_cmp(p->path, path) == 0) { - return p; - } - p = p->next; - } - return NULL; -} - -void BLF_dir_add(const char *path) -{ - DirBLF *dir; - - dir = blf_dir_find(path); - if (dir) { /* already in the list ? just return. */ - return; - } - - dir = (DirBLF *)MEM_callocN(sizeof(DirBLF), "BLF_dir_add"); - dir->path = BLI_strdup(path); - BLI_addhead(&global_font_dir, dir); -} - -void BLF_dir_rem(const char *path) -{ - DirBLF *dir; - - dir = blf_dir_find(path); - if (dir) { - BLI_remlink(&global_font_dir, dir); - MEM_freeN(dir->path); - MEM_freeN(dir); - } -} - -char **BLF_dir_get(int *ndir) -{ - DirBLF *p; - char **dirs; - char *path; - int i, count; - - count = BLI_listbase_count(&global_font_dir); - if (!count) { - return NULL; - } - - dirs = (char **)MEM_callocN(sizeof(char *) * count, "BLF_dir_get"); - p = global_font_dir.first; - i = 0; - while (p) { - path = BLI_strdup(p->path); - dirs[i] = path; - p = p->next; - } - *ndir = i; - return dirs; -} - -void BLF_dir_free(char **dirs, int count) -{ - for (int i = 0; i < count; i++) { - char *path = dirs[i]; - MEM_freeN(path); - } - MEM_freeN(dirs); -} - -char *blf_dir_search(const char *file) -{ - BLI_assert_msg(!BLI_path_is_rel(file), "Relative paths must always be expanded!"); - - DirBLF *dir; - char full_path[FILE_MAX]; - char *s = NULL; - - for (dir = global_font_dir.first; dir; dir = dir->next) { - BLI_path_join(full_path, sizeof(full_path), dir->path, file); - if (BLI_exists(full_path)) { - s = BLI_strdup(full_path); - break; - } - } - - if (!s) { - /* This may be an absolute path which exists. */ - if (BLI_exists(file)) { - s = BLI_strdup(file); - } - } - - return s; -} char *blf_dir_metrics_search(const char *filepath) { diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c index 1c88d027b8c..2c2588e1bbd 100644 --- a/source/blender/blenfont/intern/blf_font.c +++ b/source/blender/blenfont/intern/blf_font.c @@ -1508,15 +1508,20 @@ static const struct FaceDetails static_face_details[] = { {"NotoSansThai-VariableFont_wdth,wght.woff2", TT_UCR_THAI, 0, 0, 0}, }; -FontBLF *blf_font_new_ex(const char *name, - const char *filepath, - const uchar *mem, - const size_t mem_size, - void *ft_library) +/** + * Create a new font from filename OR memory pointer. + * For normal operation pass NULL as FT_Library object. Pass a custom FT_Library if you + * want to use the font without its lifetime being managed by the FreeType cache subsystem. + */ +static FontBLF *blf_font_new_impl(const char *filepath, + const char *mem_name, + const uchar *mem, + const size_t mem_size, + void *ft_library) { FontBLF *font = (FontBLF *)MEM_callocN(sizeof(FontBLF), "blf_font_new"); - font->name = BLI_strdup(name); + font->mem_name = mem_name ? BLI_strdup(mem_name) : NULL; font->filepath = filepath ? BLI_strdup(filepath) : NULL; if (mem) { font->mem = (void *)mem; @@ -1573,14 +1578,14 @@ FontBLF *blf_font_new_ex(const char *name, return font; } -FontBLF *blf_font_new(const char *name, const char *filepath) +FontBLF *blf_font_new_from_filepath(const char *filepath) { - return blf_font_new_ex(name, filepath, NULL, 0, NULL); + return blf_font_new_impl(filepath, NULL, NULL, 0, NULL); } -FontBLF *blf_font_new_from_mem(const char *name, const uchar *mem, const size_t mem_size) +FontBLF *blf_font_new_from_mem(const char *mem_name, const uchar *mem, const size_t mem_size) { - return blf_font_new_ex(name, NULL, mem, mem_size, NULL); + return blf_font_new_impl(NULL, mem_name, mem, mem_size, NULL); } void blf_font_attach_from_mem(FontBLF *font, const uchar *mem, const size_t mem_size) @@ -1621,8 +1626,8 @@ void blf_font_free(FontBLF *font) if (font->filepath) { MEM_freeN(font->filepath); } - if (font->name) { - MEM_freeN(font->name); + if (font->mem_name) { + MEM_freeN(font->mem_name); } BLI_mutex_end(&font->glyph_cache_mutex); diff --git a/source/blender/blenfont/intern/blf_internal.h b/source/blender/blenfont/intern/blf_internal.h index fc399e47271..55515c9f793 100644 --- a/source/blender/blenfont/intern/blf_internal.h +++ b/source/blender/blenfont/intern/blf_internal.h @@ -42,14 +42,11 @@ void blf_batch_draw(void); unsigned int blf_next_p2(unsigned int x); unsigned int blf_hash(unsigned int val); - -char *blf_dir_search(const char *filepath); /** * Some font have additional file with metrics information, * in general, the extension of the file is: `.afm` or `.pfm` */ char *blf_dir_metrics_search(const char *filepath); -// int blf_dir_split(const char *str, char *file, int *size); /*UNUSED*/ int blf_font_init(void); void blf_font_exit(void); @@ -70,18 +67,7 @@ void blf_ensure_size(struct FontBLF *font); void blf_draw_buffer__start(struct FontBLF *font); void blf_draw_buffer__end(void); -/** - * Create a new font from filename OR memory pointer. - * For normal operation pass NULL as FT_Library object. Pass a custom FT_Library if you - * want to use the font without its lifetime being managed by the FreeType cache subsystem. - */ -struct FontBLF *blf_font_new_ex(const char *name, - const char *filepath, - const unsigned char *mem, - size_t mem_size, - void *ft_library); - -struct FontBLF *blf_font_new(const char *name, const char *filepath); +struct FontBLF *blf_font_new_from_filepath(const char *filepath); struct FontBLF *blf_font_new_from_mem(const char *name, const unsigned char *mem, size_t mem_size); void blf_font_attach_from_mem(struct FontBLF *font, const unsigned char *mem, size_t mem_size); diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h index fe184066efa..5b0810ca4ef 100644 --- a/source/blender/blenfont/intern/blf_internal_types.h +++ b/source/blender/blenfont/intern/blf_internal_types.h @@ -238,15 +238,14 @@ typedef struct FontBufInfoBLF { } FontBufInfoBLF; typedef struct FontBLF { - /** Font name. */ - char *name; - /** Full path to font file or NULL if from memory. */ char *filepath; /** Pointer to in-memory font, or NULL if from file. */ void *mem; size_t mem_size; + /** Handle for in-memory fonts to avoid loading them multiple times. */ + char *mem_name; /** * Copied from the SFNT OS/2 table. Bit flags for unicode blocks and ranges @@ -342,11 +341,3 @@ typedef struct FontBLF { /** Mutex lock for glyph cache. */ ThreadMutex glyph_cache_mutex; } FontBLF; - -typedef struct DirBLF { - struct DirBLF *next; - struct DirBLF *prev; - - /** Full path where search fonts. */ - char *path; -} DirBLF;