[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [40430] trunk/blender/source/blender/ blenfont: Blenfont: add BLF_unload function to unload/reload fonts.

Brecht Van Lommel brechtvanlommel at pandora.be
Wed Sep 21 17:14:47 CEST 2011


Revision: 40430
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=40430
Author:   blendix
Date:     2011-09-21 15:14:47 +0000 (Wed, 21 Sep 2011)
Log Message:
-----------
Blenfont: add BLF_unload function to unload/reload fonts.

Modified Paths:
--------------
    trunk/blender/source/blender/blenfont/BLF_api.h
    trunk/blender/source/blender/blenfont/intern/blf.c

Modified: trunk/blender/source/blender/blenfont/BLF_api.h
===================================================================
--- trunk/blender/source/blender/blenfont/BLF_api.h	2011-09-21 15:07:19 UTC (rev 40429)
+++ trunk/blender/source/blender/blenfont/BLF_api.h	2011-09-21 15:14:47 UTC (rev 40430)
@@ -47,6 +47,8 @@
 int BLF_load_unique(const char *name);
 int BLF_load_mem_unique(const char *name, unsigned char *mem, int mem_size);
 
+void BLF_unload(const char *name);
+
 /* Attach a file with metrics information from memory. */
 void BLF_metrics_attach(int fontid, unsigned char *mem, int mem_size);
 

Modified: trunk/blender/source/blender/blenfont/intern/blf.c
===================================================================
--- trunk/blender/source/blender/blenfont/intern/blf.c	2011-09-21 15:07:19 UTC (rev 40429)
+++ trunk/blender/source/blender/blenfont/intern/blf.c	2011-09-21 15:14:47 UTC (rev 40430)
@@ -61,11 +61,8 @@
 #define BLF_MAX_FONT 16
 
 /* Font array. */
-static FontBLF *global_font[BLF_MAX_FONT];
+static FontBLF *global_font[BLF_MAX_FONT] = {0};
 
-/* Number of font. */
-static int global_font_num= 0;
-
 /* Default size and dpi, for BLF_draw_default. */
 static int global_font_default= -1;
 static int global_font_points= 11;
@@ -99,10 +96,12 @@
 	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 @@
 	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,9 +129,21 @@
 		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;
+}
+
 int BLF_load(const char *name)
 {
 	FontBLF *font;
@@ -149,7 +160,8 @@
 		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 @@
 		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 @@
 	/* 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 @@
 		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 @@
 		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 @@
 		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 @@
 	 * 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 @@
 		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);




More information about the Bf-blender-cvs mailing list