[Bf-blender-cvs] [15bc30f] master: Text3d: store number of characters and utf8 length separately

Campbell Barton noreply at git.blender.org
Fri Jan 3 07:09:28 CET 2014


Commit: 15bc30f4ee49c69f3ed6f92701a0f5cd52d306c5
Author: Campbell Barton
Date:   Fri Jan 3 17:04:42 2014 +1100
https://developer.blender.org/rB15bc30f4ee49c69f3ed6f92701a0f5cd52d306c5

Text3d: store number of characters and utf8 length separately

EditFont's use of Curve.len was very confusing, in editmode it
represented the number of characters, in object mode the number of
bytes. add Curve.len_wchar and keep track of both.

Also don't convert the editmode text into utf8 on every keystroke.
Now this is done on exiting editmode or save - to match most other
object types.

This also fixes curves 'body_format' being reported with an invalid size.

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

M	source/blender/blenkernel/BKE_blender.h
M	source/blender/blenkernel/BKE_font.h
M	source/blender/blenkernel/intern/curve.c
M	source/blender/blenkernel/intern/font.c
M	source/blender/blenloader/intern/versioning_260.c
M	source/blender/blenloader/intern/writefile.c
M	source/blender/editors/curve/editfont.c
M	source/blender/makesdna/DNA_curve_types.h
M	source/blender/makesrna/intern/rna_curve.c

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

diff --git a/source/blender/blenkernel/BKE_blender.h b/source/blender/blenkernel/BKE_blender.h
index f397e6b..e2f86c2 100644
--- a/source/blender/blenkernel/BKE_blender.h
+++ b/source/blender/blenkernel/BKE_blender.h
@@ -42,7 +42,7 @@ extern "C" {
  * and keep comment above the defines.
  * Use STRINGIFY() rather than defining with quotes */
 #define BLENDER_VERSION         269
-#define BLENDER_SUBVERSION      7
+#define BLENDER_SUBVERSION      8
 /* 262 was the last editmesh release but it has compatibility code for bmesh data */
 #define BLENDER_MINVERSION      262
 #define BLENDER_MINSUBVERSION   0
diff --git a/source/blender/blenkernel/BKE_font.h b/source/blender/blenkernel/BKE_font.h
index 76fc735..16cbcde 100644
--- a/source/blender/blenkernel/BKE_font.h
+++ b/source/blender/blenkernel/BKE_font.h
@@ -65,6 +65,11 @@ typedef struct EditFont {
 	struct CharInfo *textbufinfo;
 	
 	float textcurs[4][2];
+
+	/* positional vars relative to the textbuf, textbufinfo (not utf8 bytes)
+	 * a copy of these is kept in Curve, but use these in editmode */
+	int len, pos;
+	int selstart, selend;
 	
 } EditFont;
 
@@ -80,7 +85,7 @@ struct VFont *BKE_vfont_load(struct Main *bmain, const char *name);
 bool BKE_vfont_to_curve(struct Main *bmain, struct Scene *scene, struct Object *ob, int mode,
                         struct CharTrans **r_chartransdata);
 
-int BKE_vfont_select_get(struct Object *ob, int *start, int *end);
+int BKE_vfont_select_get(struct Object *ob, int *r_start, int *r_end);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/blenkernel/intern/curve.c b/source/blender/blenkernel/intern/curve.c
index 1c4560c..d1dde91 100644
--- a/source/blender/blenkernel/intern/curve.c
+++ b/source/blender/blenkernel/intern/curve.c
@@ -190,7 +190,7 @@ Curve *BKE_curve_add(Main *bmain, const char *name, int type)
 		cu->vfont->id.us += 4;
 		cu->str = MEM_mallocN(12, "str");
 		BLI_strncpy(cu->str, "Text", 12);
-		cu->len = cu->pos = 4;
+		cu->len = cu->len_wchar = cu->pos = 4;
 		cu->strinfo = MEM_callocN(12 * sizeof(CharInfo), "strinfo new");
 		cu->totbox = cu->actbox = 1;
 		cu->tb = MEM_callocN(MAXTEXTBOX * sizeof(TextBox), "textbox");
@@ -4041,7 +4041,7 @@ void BKE_curve_material_index_remove(Curve *cu, int index)
 	if (curvetype == OB_FONT) {
 		struct CharInfo *info = cu->strinfo;
 		int i;
-		for (i = cu->len - 1; i >= 0; i--, info++) {
+		for (i = cu->len_wchar - 1; i >= 0; i--, info++) {
 			if (info->mat_nr && info->mat_nr >= index) {
 				info->mat_nr--;
 			}
@@ -4068,7 +4068,7 @@ void BKE_curve_material_index_clear(Curve *cu)
 	if (curvetype == OB_FONT) {
 		struct CharInfo *info = cu->strinfo;
 		int i;
-		for (i = cu->len - 1; i >= 0; i--, info++) {
+		for (i = cu->len_wchar - 1; i >= 0; i--, info++) {
 			info->mat_nr = 0;
 		}
 	}
diff --git a/source/blender/blenkernel/intern/font.c b/source/blender/blenkernel/intern/font.c
index 90362c7..222251f 100644
--- a/source/blender/blenkernel/intern/font.c
+++ b/source/blender/blenkernel/intern/font.c
@@ -457,25 +457,26 @@ static void buildchar(Main *bmain, Curve *cu, unsigned int character, CharInfo *
 	}
 }
 
-int BKE_vfont_select_get(Object *ob, int *start, int *end)
+int BKE_vfont_select_get(Object *ob, int *r_start, int *r_end)
 {
 	Curve *cu = ob->data;
+	EditFont *ef = cu->editfont;
 	
-	if (cu->editfont == NULL || ob->type != OB_FONT) return 0;
+	if ((ob->type != OB_FONT) || (ef == NULL)) return 0;
 
-	BLI_assert(cu->selstart >= 0 && cu->selstart <= cu->len + 1);
-	BLI_assert(cu->selend   >= 0 && cu->selend   <= cu->len + 1);
-	BLI_assert(cu->pos      >= 0 && cu->pos      <= cu->len);
+	BLI_assert(ef->selstart >= 0 && ef->selstart <= ef->len + 1);
+	BLI_assert(ef->selend   >= 0 && ef->selend   <= ef->len + 1);
+	BLI_assert(ef->pos      >= 0 && ef->pos      <= ef->len);
 
-	if (cu->selstart == 0) return 0;
-	if (cu->selstart <= cu->selend) {
-		*start = cu->selstart - 1;
-		*end = cu->selend - 1;
+	if (ef->selstart == 0) return 0;
+	if (ef->selstart <= ef->selend) {
+		*r_start = ef->selstart - 1;
+		*r_end = ef->selend - 1;
 		return 1;
 	}
 	else {
-		*start = cu->selend;
-		*end = cu->selstart - 2;
+		*r_start = ef->selend;
+		*r_end = ef->selstart - 2;
 		return -1;
 	}
 }
@@ -497,9 +498,10 @@ static float char_width(Curve *cu, VChar *che, CharInfo *info)
 bool BKE_vfont_to_curve(Main *bmain, Scene *scene, Object *ob, int mode,
                         struct CharTrans **r_chartransdata)
 {
+	Curve *cu = ob->data;
+	EditFont *ef = cu->editfont;
 	VFont *vfont, *oldvfont;
 	VFontData *vfd = NULL;
-	Curve *cu;
 	CharInfo *info = NULL, *custrinfo;
 	TextBox *tb;
 	VChar *che;
@@ -519,7 +521,6 @@ bool BKE_vfont_to_curve(Main *bmain, Scene *scene, Object *ob, int mode,
 	BLI_assert(ob->type == OB_FONT);
 
 	/* Set font data */
-	cu = (Curve *) ob->data;
 	vfont = cu->vfont;
 
 	if (cu->str == NULL) return ok;
@@ -533,20 +534,18 @@ bool BKE_vfont_to_curve(Main *bmain, Scene *scene, Object *ob, int mode,
 	if (cu->ulheight == 0.0f)
 		cu->ulheight = 0.05f;
 	
-	if (cu->editfont) {
-		slen = cu->len;
-		mem = cu->editfont->textbuf;
-		custrinfo = cu->editfont->textbufinfo;
+	if (ef) {
+		slen = ef->len;
+		mem = ef->textbuf;
+		custrinfo = ef->textbufinfo;
 	}
 	else {
-		size_t utf8len;
-
-		utf8len = BLI_strlen_utf8(cu->str);
+		slen = cu->len_wchar;
 
 		/* Create unicode string */
-		mem = MEM_mallocN(((utf8len + 1) * sizeof(wchar_t)), "convertedmem");
+		mem = MEM_mallocN(((slen + 1) * sizeof(wchar_t)), "convertedmem");
 
-		slen = BLI_strncpy_wchar_from_utf8(mem, cu->str, utf8len + 1);
+		BLI_strncpy_wchar_from_utf8(mem, cu->str, slen + 1);
 
 		if (cu->strinfo == NULL) {  /* old file */
 			cu->strinfo = MEM_callocN((slen + 4) * sizeof(CharInfo), "strinfo compat");
@@ -945,10 +944,8 @@ makebreak:
 	}
 
 	if (mode == FO_CURSUP || mode == FO_CURSDOWN || mode == FO_PAGEUP || mode == FO_PAGEDOWN) {
-		/* 2: curs up
-		 * 3: curs down */
-		ct = chartransdata + cu->pos;
-		
+		ct = &chartransdata[ef->pos];
+
 		if ((mode == FO_CURSUP || mode == FO_PAGEUP) && ct->linenr == 0) {
 			/* pass */
 		}
@@ -964,7 +961,7 @@ makebreak:
 			}
 			cnr = ct->charnr;
 			/* seek for char with lnr en cnr */
-			cu->pos = 0;
+			ef->pos = 0;
 			ct = chartransdata;
 			for (i = 0; i < slen; i++) {
 				if (ct->linenr == lnr) {
@@ -975,21 +972,21 @@ makebreak:
 				else if (ct->linenr > lnr) {
 					break;
 				}
-				cu->pos++;
+				ef->pos++;
 				ct++;
 			}
 		}
 	}
 	
 	/* cursor first */
-	if (cu->editfont) {
+	if (ef) {
 		float si, co;
 		
-		ct = chartransdata + cu->pos;
+		ct = &chartransdata[ef->pos];
 		si = sinf(ct->rot);
 		co = cosf(ct->rot);
 
-		f = cu->editfont->textcurs[0];
+		f = ef->textcurs[0];
 		
 		f[0] = cu->fsize * (-0.1f * co + ct->xof);
 		f[1] = cu->fsize * ( 0.1f * si + ct->yof);
@@ -1071,8 +1068,7 @@ makebreak:
 					mem[0] = ascii;
 					mem[1] = 0;
 					custrinfo[0] = *info;
-					cu->pos = 1;
-					cu->len = 1;
+					cu->len = cu->len_wchar = cu->pos = 1;
 					mul_v3_m4v3(ob->loc, ob->obmat, vecyo);
 					outta = 1;
 					cu->sepchar = 0;
@@ -1086,14 +1082,16 @@ makebreak:
 
 finally:
 
-	if (cu->editfont == NULL)
+	if (ef == NULL)
 		MEM_freeN(mem);
 
-	if (r_chartransdata) {
-		*r_chartransdata = chartransdata;
-	}
-	else {
-		MEM_freeN(chartransdata);
+	if (chartransdata) {
+		if (ok && r_chartransdata) {
+			*r_chartransdata = chartransdata;
+		}
+		else {
+			MEM_freeN(chartransdata);
+		}
 	}
 
 	return ok;
diff --git a/source/blender/blenloader/intern/versioning_260.c b/source/blender/blenloader/intern/versioning_260.c
index d63b72b..d8e3e02 100644
--- a/source/blender/blenloader/intern/versioning_260.c
+++ b/source/blender/blenloader/intern/versioning_260.c
@@ -2645,4 +2645,14 @@ void blo_do_versions_260(FileData *fd, Library *UNUSED(lib), Main *main)
 			}
 		}
 	}
+
+	if (!MAIN_VERSION_ATLEAST(main, 269, 8)) {
+		Curve *cu;
+
+		for (cu = main->curve.first; cu; cu = cu->id.next) {
+			if (cu->str) {
+				cu->len_wchar = BLI_strlen_utf8(cu->str);
+			}
+		}
+	}
 }
diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c
index 9ffe4da..beb8d58 100644
--- a/source/blender/blenloader/intern/writefile.c
+++ b/source/blender/blenloader/intern/writefile.c
@@ -1664,12 +1664,8 @@ static void write_curves(WriteData *wd, ListBase *idbase)
 			if (cu->adt) write_animdata(wd, cu->adt);
 			
 			if (cu->vfont) {
-				/* TODO, sort out 'cu->len', in editmode its character, object mode its bytes */
-				size_t len_bytes;
-				size_t len_chars = BLI_strlen_utf8_ex(cu->str, &len_bytes);
-
-				writedata(wd, DATA, len_bytes + 1, cu->str);
-				writestruct(wd, DATA, "CharInfo", len_chars + 1, cu->strinfo);
+				writedata(wd, DATA, cu->len + 1, cu->str);
+				writestruct(wd, DATA, "CharInfo", cu->len_wchar + 1, cu->strinfo);
 				writestruct(wd, DATA, "TextBox", cu->totbox, cu->tb);
 			}
 			else {
diff --git a/source/blender/editors/curve/editfont.c b/source/blender/editors/curve/editfont.c
index 6bb6b28..3118dd6 100644
--- a/source/blender/editors/curve/editfont.c
+++ b/source/blender/editors/curve/editfont.c
@@ -220,47 +220,24 @@ static char findaccent(char char1, unsigned int code)
 	else return char1;
 }
 
-
-static void update_string(Curve *cu)
-{
-	EditFont *ef = cu->editfont;
-	int len;
-
-	/* Free the old curve string */
-	MEM_freeN(cu->str);
-
-	/* Calculate the actual string length in UTF-8 variable characters */
-	len = BLI_wstrlen_utf8(ef->textbuf);
-
-	/* Alloc memory for UTF-8 variable char length string */
-	cu->str = MEM_callocN(len + sizeof(wchar_t), "str");
-
-	/* Copy the wchar to UTF-8 */
-	BLI_strncpy_wchar_as_utf8(cu->str, ef->textbuf, len + 1);
-
-	BLI_assert(wcslen(ef->textbuf) == cu->len);
-}
-
 static int insert_into_textbuf(Object *obedit, uintptr_t c)
 {
 	Curve *cu = obedit->data;
+	EditFont *ef = cu->editfont;
 	
-	if (cu->len < MAXTEXT - 1) {
-		EditFont *ef = cu->editfont;
+	if (ef->len < MAXTEXT - 1) {
 		int x;
 
-		for (x = cu->len; x > cu->pos; x--) ef->textbuf[x] = ef->textbuf[x - 1];
-		for (x = cu->len; x > cu->pos; x--) ef->textbufinfo[x] = ef->textbufinfo[x - 1];
-		ef->textbuf[cu->pos] = c;
-		ef->textbufinfo[cu->pos] = cu->curinfo;
-		ef->textbufinfo[cu->pos].kern = 0;
-		ef->textbufinfo[

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list