[Bf-blender-cvs] [961d040] master: 3D Text: use ghash for character lookups

Campbell Barton noreply at git.blender.org
Sat Dec 28 07:41:16 CET 2013


Commit: 961d0409c89dff01fd4c2c5f9d6e0406068e7552
Author: Campbell Barton
Date:   Sat Dec 28 17:33:19 2013 +1100
https://developer.blender.org/rB961d0409c89dff01fd4c2c5f9d6e0406068e7552

3D Text: use ghash for character lookups

===================================================================

M	source/blender/blenkernel/intern/font.c
M	source/blender/blenlib/BLI_vfontdata.h
M	source/blender/blenlib/intern/freetypefont.c

===================================================================

diff --git a/source/blender/blenkernel/intern/font.c b/source/blender/blenkernel/intern/font.c
index f489adc..0839ed8 100644
--- a/source/blender/blenkernel/intern/font.c
+++ b/source/blender/blenkernel/intern/font.c
@@ -39,11 +39,15 @@
 
 #include "MEM_guardedalloc.h"
 
+#include "BLI_utildefines.h"
+#include "BLI_path_util.h"
+#include "BLI_listbase.h"
+#include "BLI_ghash.h"
+#include "BLI_string.h"
+#include "BLI_string_utf8.h"
 #include "BLI_math.h"
-#include "BLI_blenlib.h"
 #include "BLI_threads.h"
 #include "BLI_vfontdata.h"
-#include "BLI_utildefines.h"
 
 #include "DNA_packedFile_types.h"
 #include "DNA_curve_types.h"
@@ -66,16 +70,21 @@ static ThreadMutex vfont_mutex = BLI_MUTEX_INITIALIZER;
 void BKE_vfont_free_data(struct VFont *vfont)
 {
 	if (vfont->data) {
-		while (vfont->data->characters.first) {
-			VChar *che = vfont->data->characters.first;
+		if (vfont->data->characters) {
+			GHashIterator gh_iter;
+			GHASH_ITER (gh_iter, vfont->data->characters) {
+				VChar *che = BLI_ghashIterator_getValue(&gh_iter);
+
+				while (che->nurbsbase.first) {
+					Nurb *nu = che->nurbsbase.first;
+					if (nu->bezt) MEM_freeN(nu->bezt);
+					BLI_freelinkN(&che->nurbsbase, nu);
+				}
 
-			while (che->nurbsbase.first) {
-				Nurb *nu = che->nurbsbase.first;
-				if (nu->bezt) MEM_freeN(nu->bezt);
-				BLI_freelinkN(&che->nurbsbase, nu);
+				MEM_freeN(che);
 			}
 
-			BLI_freelinkN(&vfont->data->characters, che);
+			BLI_ghash_free(vfont->data->characters, NULL, NULL);
 		}
 
 		MEM_freeN(vfont->data);
@@ -279,16 +288,9 @@ VFont *BKE_vfont_builtin_get(void)
 	return BKE_vfont_load(G.main, FO_BUILTIN_NAME);
 }
 
-static VChar *find_vfont_char(VFontData *vfd, intptr_t character)
+static VChar *find_vfont_char(VFontData *vfd, unsigned int character)
 {
-	VChar *che = NULL;
-
-	/* TODO: use ghash */
-	for (che = vfd->characters.first; che; che = che->next) {
-		if (che->index == character)
-			break;
-	}
-	return che; /* NULL if not found */
+	return BLI_ghash_lookup(vfd->characters, SET_UINT_IN_POINTER(character));
 }
 		
 static void build_underline(Curve *cu, float x1, float y1, float x2, float y2, int charidx, short mat_nr)
@@ -326,7 +328,7 @@ static void build_underline(Curve *cu, float x1, float y1, float x2, float y2, i
 
 }
 
-static void buildchar(Main *bmain, Curve *cu, unsigned long character, CharInfo *info,
+static void buildchar(Main *bmain, Curve *cu, unsigned int character, CharInfo *info,
                       float ofsx, float ofsy, float rot, int charidx)
 {
 	BezTriple *bezt1, *bezt2;
@@ -1004,7 +1006,7 @@ makebreak:
 		ct = chartransdata;
 		if (cu->sepchar == 0) {
 			for (i = 0; i < slen; i++) {
-				unsigned long cha = (uintptr_t) mem[i];
+				unsigned int cha = (unsigned int) mem[i];
 				info = &(custrinfo[i]);
 				if (info->mat_nr > (ob->totcol)) {
 					/* printf("Error: Illegal material index (%d) in text object, setting to 0\n", info->mat_nr); */
diff --git a/source/blender/blenlib/BLI_vfontdata.h b/source/blender/blenlib/BLI_vfontdata.h
index b0a57ee..9130061 100644
--- a/source/blender/blenlib/BLI_vfontdata.h
+++ b/source/blender/blenlib/BLI_vfontdata.h
@@ -40,17 +40,14 @@ struct PackedFile;
 struct VFont;
 
 typedef struct VFontData {
-	ListBase characters;
+	struct GHash *characters;
 	char name[128];
 } VFontData;
 
 typedef struct VChar {
-	struct VChar    *next, *prev;
 	ListBase nurbsbase;
-	intptr_t index;
-	float resol;
+	unsigned int index;
 	float width;
-	float           *points;
 } VChar;
 
 VFontData *BLI_vfontdata_from_freetypefont(struct PackedFile *pf);
diff --git a/source/blender/blenlib/intern/freetypefont.c b/source/blender/blenlib/intern/freetypefont.c
index 4f703d2..9cd0bf0 100644
--- a/source/blender/blenlib/intern/freetypefont.c
+++ b/source/blender/blenlib/intern/freetypefont.c
@@ -50,6 +50,7 @@
 #include "BLI_utildefines.h"
 #include "BLI_vfontdata.h"
 #include "BLI_listbase.h"
+#include "BLI_ghash.h"
 #include "BLI_string.h"
 #include "BLI_math.h"
 
@@ -100,7 +101,6 @@ static void freetypechar_to_vchar(FT_Face face, FT_ULong charcode, VFontData *vf
 
 		/* First we create entry for the new character to the character list */
 		che = (VChar *) MEM_callocN(sizeof(struct VChar), "objfnt_char");
-		BLI_addtail(&vfd->characters, che);
 
 		/* Take some data for modifying purposes */
 		glyph = face->glyph;
@@ -110,8 +110,10 @@ static void freetypechar_to_vchar(FT_Face face, FT_ULong charcode, VFontData *vf
 		che->index = charcode;
 		che->width = glyph->advance.x * scale;
 
+		BLI_ghash_insert(vfd->characters, SET_UINT_IN_POINTER(che->index), che);
+
 		/* Start converting the FT data */
-		npoints = (int *)MEM_callocN((ftoutline.n_contours) * sizeof(int), "endpoints");
+		npoints = (int *)MEM_mallocN((ftoutline.n_contours) * sizeof(int), "endpoints");
 		onpoints = (int *)MEM_callocN((ftoutline.n_contours) * sizeof(int), "onpoints");
 
 		/* calculate total points of each contour */
@@ -319,6 +321,7 @@ static VFontData *objfnt_to_ftvfontdata(PackedFile *pf)
 {
 	/* Variables */
 	FT_Face face;
+	const FT_ULong charcode_reserve = 256;
 	FT_ULong charcode = 0, lcode;
 	FT_UInt glyph_index;
 	const char *fontname;
@@ -393,7 +396,9 @@ static VFontData *objfnt_to_ftvfontdata(PackedFile *pf)
 	}
 
 	/* Load characters */
-	while (charcode < 256) {
+	vfd->characters = BLI_ghash_int_new_ex(__func__, charcode_reserve);
+
+	while (charcode < charcode_reserve) {
 		/* Generate the font data */
 		freetypechar_to_vchar(face, charcode, vfd);




More information about the Bf-blender-cvs mailing list