[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [41223] trunk/blender/source/blender: - fix for error with utf8 textinput for buttons

Campbell Barton ideasman42 at gmail.com
Sun Oct 23 15:52:51 CEST 2011


Revision: 41223
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=41223
Author:   campbellbarton
Date:     2011-10-23 13:52:51 +0000 (Sun, 23 Oct 2011)
Log Message:
-----------
- fix for error with utf8 textinput for buttons
- ensure input is valid utf8 from ghost and NULL then complain if its not.
- added function to get utf8 size BLI_str_utf8_size()

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_string_utf8.h
    trunk/blender/source/blender/blenlib/intern/string_utf8.c
    trunk/blender/source/blender/editors/interface/interface_handlers.c
    trunk/blender/source/blender/makesrna/intern/rna_wm.c
    trunk/blender/source/blender/windowmanager/WM_types.h
    trunk/blender/source/blender/windowmanager/intern/wm_event_system.c

Modified: trunk/blender/source/blender/blenlib/BLI_string_utf8.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_string_utf8.h	2011-10-23 13:00:41 UTC (rev 41222)
+++ trunk/blender/source/blender/blenlib/BLI_string_utf8.h	2011-10-23 13:52:51 UTC (rev 41223)
@@ -37,6 +37,7 @@
 int          BLI_utf8_invalid_byte(const char *str, int length);
 int          BLI_utf8_invalid_strip(char *str, int length);
 
+int          BLI_str_utf8_size(const char *p); /* warning, can return -1 on bad chars */
     /* copied from glib */
 unsigned int BLI_str_utf8_as_unicode(const char *p);
 unsigned int BLI_str_utf8_as_unicode_and_size(const char *p, size_t *index);

Modified: trunk/blender/source/blender/blenlib/intern/string_utf8.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/string_utf8.c	2011-10-23 13:00:41 UTC (rev 41222)
+++ trunk/blender/source/blender/blenlib/intern/string_utf8.c	2011-10-23 13:52:51 UTC (rev 41223)
@@ -312,6 +312,18 @@
 	}
 
 
+/* uses glib functions but not from glib */
+/* gets the size of a single utf8 char */
+int BLI_str_utf8_size(const char *p)
+{
+	int mask = 0, len;
+    unsigned char c = (unsigned char) *p;
+
+    UTF8_COMPUTE (c, mask, len);
+
+	return len;
+}
+
 /* was g_utf8_get_char */
 /**
  * BLI_str_utf8_as_unicode:

Modified: trunk/blender/source/blender/editors/interface/interface_handlers.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface_handlers.c	2011-10-23 13:00:41 UTC (rev 41222)
+++ trunk/blender/source/blender/editors/interface/interface_handlers.c	2011-10-23 13:52:51 UTC (rev 41223)
@@ -1447,7 +1447,7 @@
 	len= strlen(str);
 
 	if(len-(but->selend - but->selsta)+1 <= data->maxlen) {
-		int step= BLI_strnlen(utf8_buf, sizeof(utf8_buf));
+		int step= BLI_str_utf8_size(utf8_buf);
 
 		/* type over the current selection */
 		if ((but->selend - but->selsta) > 0) {

Modified: trunk/blender/source/blender/makesrna/intern/rna_wm.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_wm.c	2011-10-23 13:00:41 UTC (rev 41222)
+++ trunk/blender/source/blender/makesrna/intern/rna_wm.c	2011-10-23 13:52:51 UTC (rev 41223)
@@ -492,9 +492,7 @@
 
 	wmEvent *event= (wmEvent*)ptr->data;
 	if (event->utf8_buf[0]) {
-		size_t len= 0;
-		BLI_str_utf8_as_unicode_and_size(event->utf8_buf, &len);
-		return (int)len;
+		return BLI_str_utf8_size(event->utf8_buf); /* invalid value is checked on assignment so we dont need to account for this */
 	}
 	else {
 		return 0;

Modified: trunk/blender/source/blender/windowmanager/WM_types.h
===================================================================
--- trunk/blender/source/blender/windowmanager/WM_types.h	2011-10-23 13:00:41 UTC (rev 41222)
+++ trunk/blender/source/blender/windowmanager/WM_types.h	2011-10-23 13:52:51 UTC (rev 41223)
@@ -346,7 +346,9 @@
 	short val;			/* press, release, scrollvalue */
 	int x, y;			/* mouse pointer position, screen coord */
 	int mval[2];		/* region mouse position, name convention pre 2.5 :) */
-	char utf8_buf[6];	/* from, ghost if utf8 is enabled for the platform */
+	char utf8_buf[6];	/* from, ghost if utf8 is enabled for the platform,
+						 * BLI_str_utf8_size() must _always_ be valid, check
+						 * when assigning s we dont need to check on every access after */
 	char ascii;			/* from ghost, fallback if utf8 isnt set */
 	char pad;
 

Modified: trunk/blender/source/blender/windowmanager/intern/wm_event_system.c
===================================================================
--- trunk/blender/source/blender/windowmanager/intern/wm_event_system.c	2011-10-23 13:00:41 UTC (rev 41222)
+++ trunk/blender/source/blender/windowmanager/intern/wm_event_system.c	2011-10-23 13:52:51 UTC (rev 41223)
@@ -454,11 +454,12 @@
 
 		printf("wmEvent - type:%d/%s, val:%d/%s, "
 			   "shift:%d, ctrl:%d, alt:%d, oskey:%d, keymodifier:%d, "
-			   "mouse:(%d,%d), ascii:'%c', utf8:'%.6s', "
+			   "mouse:(%d,%d), ascii:'%c', utf8:'%.*s', "
 			   "keymap_idname:%s, pointer:%p\n",
 			   event->type, type_id, event->val, val_id,
 			   event->shift, event->ctrl, event->alt, event->oskey, event->keymodifier,
-			   event->x, event->y, event->ascii, event->utf8_buf,
+			   event->x, event->y, event->ascii,
+		       BLI_str_utf8_size(event->utf8_buf), event->utf8_buf,
 			   event->keymap_idname, (void *)event);
 	}
 	else {
@@ -2629,6 +2630,13 @@
 				/* TODO. should this also zero utf8?, dont for now, campbell */
 			}
 
+			if (event.utf8_buf[0]) {
+				if (BLI_str_utf8_size(event.utf8_buf) == -1) {
+					printf("%s: ghost detected an invalid unicode character '%d'!\n", __func__, (int)(unsigned char)event.utf8_buf[0]);
+					event.utf8_buf[0]= '\0';
+				}
+			}
+
 			/* modifiers */
 			/* assigning both first and second is strange - campbell */
 			switch(event.type) {




More information about the Bf-blender-cvs mailing list