[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [58106] trunk/blender/source/blender: fix [#36066] crash when Tab out text object

Campbell Barton ideasman42 at gmail.com
Tue Jul 9 08:21:46 CEST 2013


Revision: 58106
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58106
Author:   campbellbarton
Date:     2013-07-09 06:21:45 +0000 (Tue, 09 Jul 2013)
Log Message:
-----------
fix [#36066] crash when Tab out text object
the way Curve.len is used at the moment is really stupid, calculate string size on save for now, but should really store the length in bytes and total number of characters.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/text.c
    trunk/blender/source/blender/blenlib/BLI_string_utf8.h
    trunk/blender/source/blender/blenlib/intern/string_utf8.c
    trunk/blender/source/blender/blenloader/intern/writefile.c
    trunk/blender/source/blender/makesdna/DNA_curve_types.h

Modified: trunk/blender/source/blender/blenkernel/intern/text.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/text.c	2013-07-09 04:00:56 UTC (rev 58105)
+++ trunk/blender/source/blender/blenkernel/intern/text.c	2013-07-09 06:21:45 UTC (rev 58106)
@@ -847,19 +847,6 @@
 	return offset;
 }
 
-/* returns the real number of characters in string */
-/* not the same as BLI_strlen_utf8, which returns length for wide characters */
-static int txt_utf8_len(const char *src)
-{
-	int len;
-
-	for (len = 0; *src; len++) {
-		src += BLI_str_utf8_size(src);
-	}
-
-	return len;
-}
-
 void txt_move_up(Text *text, short sel)
 {
 	TextLine **linep;
@@ -2059,7 +2046,7 @@
 				text->undo_pos--;
 			}
 			buf[i] = 0;
-			linep = txt_utf8_len(buf);
+			linep = BLI_strlen_utf8(buf);
 			MEM_freeN(buf);
 			
 			/* skip over the length that was stored again */

Modified: trunk/blender/source/blender/blenlib/BLI_string_utf8.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_string_utf8.h	2013-07-09 04:00:56 UTC (rev 58105)
+++ trunk/blender/source/blender/blenlib/BLI_string_utf8.h	2013-07-09 06:21:45 UTC (rev 58106)
@@ -51,8 +51,10 @@
 
 /* wchar_t functions, copied from blenders own font.c originally */
 size_t       BLI_wstrlen_utf8(const wchar_t *src);
+size_t       BLI_strlen_utf8_ex(const char *strc, int *r_len_bytes);
 size_t       BLI_strlen_utf8(const char *strc);
-size_t       BLI_strnlen_utf8(const char *start, const size_t maxlen);
+size_t       BLI_strnlen_utf8_ex(const char *strc, const size_t maxlen, int *r_len_bytes);
+size_t       BLI_strnlen_utf8(const char *strc, const size_t maxlen);
 size_t       BLI_strncpy_wchar_as_utf8(char *__restrict dst, const wchar_t *__restrict src, const size_t maxcpy);
 size_t       BLI_strncpy_wchar_from_utf8(wchar_t *__restrict dst, const char *__restrict src, const size_t maxcpy);
 

Modified: trunk/blender/source/blender/blenlib/intern/string_utf8.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/string_utf8.c	2013-07-09 04:00:56 UTC (rev 58105)
+++ trunk/blender/source/blender/blenlib/intern/string_utf8.c	2013-07-09 06:21:45 UTC (rev 58106)
@@ -245,24 +245,16 @@
 	return len;
 }
 
-/* this is very close to 'BLI_str_utf8_size' functionality, perhaps we should de-duplicate */
-/* size of UTF-8 character in bytes */
-static size_t strlen_utf8_char(const char *strc)
+size_t BLI_strlen_utf8_ex(const char *strc, int *r_len_bytes)
 {
-	if ((*strc & 0xe0) == 0xc0) {
-		if ((strc[1] & 0x80) && (strc[1] & 0x40) == 0x00)
-			return 2;
-	}
-	else if ((*strc & 0xf0) == 0xe0) {
-		if ((strc[1] & strc[2] & 0x80) && ((strc[1] | strc[2]) & 0x40) == 0x00)
-			return 3;
-	}
-	else if ((*strc & 0xf8) == 0xf0) {
-		if ((strc[1] & strc[2] & strc[3] & 0x80) && ((strc[1] | strc[2] | strc[3]) & 0x40) == 0x00)
-			return 4;
-	}
+	size_t len;
+	const char *strc_orig = strc;
 
-	return 1;
+	for (len = 0; *strc; len++)
+		strc += BLI_str_utf8_size_safe(strc);
+
+	*r_len_bytes = (strc - strc_orig);
+	return len;
 }
 
 size_t BLI_strlen_utf8(const char *strc)
@@ -270,25 +262,37 @@
 	size_t len;
 
 	for (len = 0; *strc; len++)
-		strc += strlen_utf8_char(strc);
+		strc += BLI_str_utf8_size_safe(strc);
 
 	return len;
 }
 
+size_t BLI_strnlen_utf8_ex(const char *strc, const size_t maxlen, int *r_len_bytes)
+{
+	size_t len;
+	const char *strc_orig = strc;
+	const char *strc_end = strc + maxlen;
+
+	for (len = 0; *strc && strc < strc_end; len++) {
+		strc += BLI_str_utf8_size_safe(strc);
+	}
+
+	*r_len_bytes = (strc - strc_orig);
+	return len;
+}
+
 /**
  * \param start the string to measure the length.
  * \param maxlen the string length (in bytes)
  * \return the unicode length (not in bytes!)
  */
-size_t BLI_strnlen_utf8(const char *start, const size_t maxlen)
+size_t BLI_strnlen_utf8(const char *strc, const size_t maxlen)
 {
-	const char *strc = start;
-	const char *strc_end = start + maxlen;
-
 	size_t len;
+	const char *strc_end = strc + maxlen;
 
 	for (len = 0; *strc && strc < strc_end; len++) {
-		strc += strlen_utf8_char(strc);
+		strc += BLI_str_utf8_size_safe(strc);
 	}
 
 	return len;

Modified: trunk/blender/source/blender/blenloader/intern/writefile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/writefile.c	2013-07-09 04:00:56 UTC (rev 58105)
+++ trunk/blender/source/blender/blenloader/intern/writefile.c	2013-07-09 06:21:45 UTC (rev 58106)
@@ -1642,13 +1642,6 @@
 	}
 }
 
-static int amount_of_chars(char *str)
-{
-	// Since the data is saved as UTF-8 to the cu->str
-	// The cu->len is not same as the strlen(cu->str)
-	return strlen(str);
-}
-
 static void write_curves(WriteData *wd, ListBase *idbase)
 {
 	Curve *cu;
@@ -1666,8 +1659,12 @@
 			if (cu->adt) write_animdata(wd, cu->adt);
 			
 			if (cu->vfont) {
-				writedata(wd, DATA, amount_of_chars(cu->str)+1, cu->str);
-				writestruct(wd, DATA, "CharInfo", cu->len+1, cu->strinfo);
+				/* TODO, sort out 'cu->len', in editmode its character, object mode its bytes */
+				int len_bytes;
+				int 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);
 				writestruct(wd, DATA, "TextBox", cu->totbox, cu->tb);
 			}
 			else {

Modified: trunk/blender/source/blender/makesdna/DNA_curve_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_curve_types.h	2013-07-09 04:00:56 UTC (rev 58105)
+++ trunk/blender/source/blender/makesdna/DNA_curve_types.h	2013-07-09 06:21:45 UTC (rev 58106)
@@ -215,6 +215,10 @@
 	void *lastsel;
 	
 	/* font part */
+	/* WARNING: cu->len is...
+	 * - strlen(cu->str) object-mode (bytes).
+	 * - BLI_strlen_utf8(cu->str) in edit-mode.
+	 * This should be cleaned up and some point, see 'write_curves' - campbell */
 	short len, lines, pos, spacemode;
 	float spacing, linedist, shear, fsize, wordspace, ulpos, ulheight;
 	float xof, yof;




More information about the Bf-blender-cvs mailing list