From a12e06dc3591bc1e95372e08a08cf85fb53960e1 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Wed, 21 Sep 2011 15:14:47 +0000 Subject: Blenfont: add BLF_unload function to unload/reload fonts. --- source/blender/blenfont/intern/blf.c | 68 ++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 23 deletions(-) (limited to 'source/blender/blenfont/intern') diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c index cccecd00bf7..ec0605236dd 100644 --- a/source/blender/blenfont/intern/blf.c +++ b/source/blender/blenfont/intern/blf.c @@ -61,10 +61,7 @@ #define BLF_MAX_FONT 16 /* Font array. */ -static FontBLF *global_font[BLF_MAX_FONT]; - -/* Number of font. */ -static int global_font_num= 0; +static FontBLF *global_font[BLF_MAX_FONT] = {0}; /* Default size and dpi, for BLF_draw_default. */ static int global_font_default= -1; @@ -99,10 +96,12 @@ void BLF_exit(void) FontBLF *font; int i; - for (i= 0; i < global_font_num; i++) { + for (i= 0; i < BLF_MAX_FONT; i++) { font= global_font[i]; - if (font) + if (font) { blf_font_free(font); + global_font[i]= NULL; + } } blf_font_exit(); @@ -113,7 +112,7 @@ void BLF_cache_clear(void) FontBLF *font; int i; - for (i= 0; i < global_font_num; i++) { + for (i= 0; i < BLF_MAX_FONT; i++) { font= global_font[i]; if (font) blf_glyph_cache_clear(font); @@ -130,6 +129,18 @@ static int blf_search(const char *name) if (font && (!strcmp(font->name, name))) return i; } + + return -1; +} + +static int blf_search_available(void) +{ + int i; + + for (i= 0; i < BLF_MAX_FONT; i++) + if(!global_font[i]) + return i; + return -1; } @@ -149,7 +160,8 @@ int BLF_load(const char *name) return i; } - if (global_font_num+1 >= BLF_MAX_FONT) { + i = blf_search_available(); + if (i == -1) { printf("Too many fonts!!!\n"); return -1; } @@ -168,9 +180,7 @@ int BLF_load(const char *name) return -1; } - global_font[global_font_num]= font; - i= global_font_num; - global_font_num++; + global_font[i]= font; return i; } @@ -186,7 +196,8 @@ int BLF_load_unique(const char *name) /* Don't search in the cache!! make a new * object font, this is for keep fonts threads safe. */ - if (global_font_num+1 >= BLF_MAX_FONT) { + i = blf_search_available(); + if (i == -1) { printf("Too many fonts!!!\n"); return -1; } @@ -205,9 +216,7 @@ int BLF_load_unique(const char *name) return -1; } - global_font[global_font_num]= font; - i= global_font_num; - global_font_num++; + global_font[i]= font; return i; } @@ -234,7 +243,8 @@ int BLF_load_mem(const char *name, unsigned char *mem, int mem_size) return i; } - if (global_font_num+1 >= BLF_MAX_FONT) { + i = blf_search_available(); + if (i == -1) { printf("Too many fonts!!!\n"); return -1; } @@ -250,9 +260,7 @@ int BLF_load_mem(const char *name, unsigned char *mem, int mem_size) return -1; } - global_font[global_font_num]= font; - i= global_font_num; - global_font_num++; + global_font[i]= font; return i; } @@ -268,7 +276,8 @@ int BLF_load_mem_unique(const char *name, unsigned char *mem, int mem_size) * Don't search in the cache, make a new object font! * this is to keep the font thread safe. */ - if (global_font_num+1 >= BLF_MAX_FONT) { + i = blf_search_available(); + if (i == -1) { printf("Too many fonts!!!\n"); return -1; } @@ -284,12 +293,25 @@ int BLF_load_mem_unique(const char *name, unsigned char *mem, int mem_size) return -1; } - global_font[global_font_num]= font; - i= global_font_num; - global_font_num++; + global_font[i]= font; return i; } +void BLF_unload(const char *name) +{ + FontBLF *font; + int i; + + for (i= 0; i < BLF_MAX_FONT; i++) { + font= global_font[i]; + + if (font && (!strcmp(font->name, name))) { + blf_font_free(font); + global_font[i]= NULL; + } + } +} + void BLF_enable(int fontid, int option) { FontBLF *font= BLF_get(fontid); -- cgit v1.2.3