[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [31630] trunk/blender/source/blender: python/utf8 compatibility fixes.

Campbell Barton ideasman42 at gmail.com
Sat Aug 28 14:34:22 CEST 2010


Revision: 31630
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=31630
Author:   campbellbarton
Date:     2010-08-28 14:34:22 +0200 (Sat, 28 Aug 2010)

Log Message:
-----------
python/utf8 compatibility fixes. (as discussed on the mailing list)

- user input gets non utf8 chars stripped all text input other then file paths.

- python has the same limitations, it will raise an error on non utf8 strings except for paths use unicode escape literals so its possible to deal with saving to these file paths from python.

- new string functions
  BLI_utf8_invalid_byte(str, len) returns the first invalid utf8 byte or -1 on on success.
  BLI_utf8_invalid_strip(str, len) strips non utf-8 chars. 

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_string.h
    trunk/blender/source/blender/blenlib/intern/string.c
    trunk/blender/source/blender/editors/include/UI_interface.h
    trunk/blender/source/blender/editors/interface/interface_handlers.c
    trunk/blender/source/blender/editors/space_file/file_draw.c
    trunk/blender/source/blender/makesrna/intern/rna_render.c
    trunk/blender/source/blender/makesrna/intern/rna_sequencer.c
    trunk/blender/source/blender/makesrna/intern/rna_space.c
    trunk/blender/source/blender/python/intern/bpy_rna.c

Modified: trunk/blender/source/blender/blenlib/BLI_string.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_string.h	2010-08-28 12:15:14 UTC (rev 31629)
+++ trunk/blender/source/blender/blenlib/BLI_string.h	2010-08-28 12:34:22 UTC (rev 31630)
@@ -132,6 +132,9 @@
 
 void BLI_timestr(double _time, char *str); /* time var is global */
 
+int BLI_utf8_invalid_byte(const char *str, int length);
+int BLI_utf8_invalid_strip(char *str, int length);
+
 #ifdef __cplusplus
 }
 #endif

Modified: trunk/blender/source/blender/blenlib/intern/string.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/string.c	2010-08-28 12:15:14 UTC (rev 31629)
+++ trunk/blender/source/blender/blenlib/intern/string.c	2010-08-28 12:34:22 UTC (rev 31630)
@@ -348,3 +348,114 @@
 	const char *end = memchr(str, '\0', maxlen);
 	return end ? (size_t) (end - str) : maxlen;
 }
+
+/* from libswish3, originally called u8_isvalid(),
+ * modified to return the index of the bad character (byte index not utf).
+ * http://svn.swish-e.org/libswish3/trunk/src/libswish3/utf8.c r3044 - campbell */
+
+/* based on the valid_utf8 routine from the PCRE library by Philip Hazel
+
+   length is in bytes, since without knowing whether the string is valid
+   it's hard to know how many characters there are! */
+
+static const char trailingBytesForUTF8[256] = {
+	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+	1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+	2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
+};
+
+int BLI_utf8_invalid_byte(const char *str, int length)
+{
+    const unsigned char *p, *pend = (unsigned char*)str + length;
+    unsigned char c;
+    int ab;
+
+    for (p = (unsigned char*)str; p < pend; p++) {
+        c = *p;
+        if (c < 128)
+            continue;
+        if ((c & 0xc0) != 0xc0)
+            goto utf8_error;
+        ab = trailingBytesForUTF8[c];
+        if (length < ab)
+            goto utf8_error;
+        length -= ab;
+
+        p++;
+        /* Check top bits in the second byte */
+        if ((*p & 0xc0) != 0x80)
+            goto utf8_error;
+
+        /* Check for overlong sequences for each different length */
+        switch (ab) {
+            /* Check for xx00 000x */
+        case 1:
+            if ((c & 0x3e) == 0) goto utf8_error;
+            continue;   /* We know there aren't any more bytes to check */
+
+            /* Check for 1110 0000, xx0x xxxx */
+        case 2:
+            if (c == 0xe0 && (*p & 0x20) == 0) goto utf8_error;
+            break;
+
+            /* Check for 1111 0000, xx00 xxxx */
+        case 3:
+            if (c == 0xf0 && (*p & 0x30) == 0) goto utf8_error;
+            break;
+
+            /* Check for 1111 1000, xx00 0xxx */
+        case 4:
+            if (c == 0xf8 && (*p & 0x38) == 0) goto utf8_error;
+            break;
+
+            /* Check for leading 0xfe or 0xff,
+               and then for 1111 1100, xx00 00xx */
+        case 5:
+            if (c == 0xfe || c == 0xff ||
+                (c == 0xfc && (*p & 0x3c) == 0)) goto utf8_error;
+            break;
+        }
+
+        /* Check for valid bytes after the 2nd, if any; all must start 10 */
+        while (--ab > 0) {
+            if ((*(p+1) & 0xc0) != 0x80) goto utf8_error;
+			p++; /* do this after so we get usable offset - campbell */
+        }
+    }
+
+    return -1;
+
+utf8_error:
+
+	return (int)((char *)p - (char *)str) - 1;
+}
+
+int BLI_utf8_invalid_strip(char *str, int length)
+{
+	int bad_char, tot= 0;
+
+	while((bad_char= BLI_utf8_invalid_byte(str, length)) != -1) {
+		str += bad_char;
+		length -= bad_char;
+
+		if(length == 0) {
+			/* last character bad, strip it */
+			*str= '\0';
+			tot++;
+			break;
+		}
+		else {
+			/* strip, keep looking */
+			memmove(str, str + 1, length);
+			tot++;
+		}
+	}
+
+	return tot;
+}
+

Modified: trunk/blender/source/blender/editors/include/UI_interface.h
===================================================================
--- trunk/blender/source/blender/editors/include/UI_interface.h	2010-08-28 12:15:14 UTC (rev 31629)
+++ trunk/blender/source/blender/editors/include/UI_interface.h	2010-08-28 12:34:22 UTC (rev 31630)
@@ -135,7 +135,7 @@
 #define UI_MAKE_RIGHT	8192
 
 	/* button align flag, for drawing groups together */
-#define UI_BUT_ALIGN		(15<<14)
+#define UI_BUT_ALIGN		(UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT|UI_BUT_ALIGN_RIGHT|UI_BUT_ALIGN_DOWN)
 #define UI_BUT_ALIGN_TOP	(1<<14)
 #define UI_BUT_ALIGN_LEFT	(1<<15)
 #define UI_BUT_ALIGN_RIGHT	(1<<16)
@@ -151,9 +151,10 @@
 #define UI_BUT_UNDO			(1<<25)
 #define UI_BUT_IMMEDIATE	(1<<26)
 #define UI_BUT_NO_TOOLTIP	(1<<27)
+#define UI_BUT_NO_UTF8		(1<<28)
 
-#define UI_BUT_VEC_SIZE_LOCK (1<<28) /* used to flag if color hsv-circle should keep luminance */
-#define UI_BUT_COLOR_CUBIC	(1<<29) /* cubic saturation for the color wheel */
+#define UI_BUT_VEC_SIZE_LOCK (1<<29) /* used to flag if color hsv-circle should keep luminance */
+#define UI_BUT_COLOR_CUBIC	(1<<30) /* cubic saturation for the color wheel */
 
 #define UI_PANEL_WIDTH			340
 #define UI_COMPACT_PANEL_WIDTH	160

Modified: trunk/blender/source/blender/editors/interface/interface_handlers.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface_handlers.c	2010-08-28 12:15:14 UTC (rev 31629)
+++ trunk/blender/source/blender/editors/interface/interface_handlers.c	2010-08-28 12:34:22 UTC (rev 31630)
@@ -242,6 +242,20 @@
 	return FALSE;
 }
 
+/* file selectors are exempt from utf-8 checks */
+static int ui_is_utf8_but(uiBut *but)
+{
+	if (but->rnaprop) {
+		int subtype= RNA_property_subtype(but->rnaprop);
+		
+		if(ELEM3(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME)) {
+			return TRUE;
+		}
+	}
+
+	return !(but->flag & UI_BUT_NO_UTF8);
+}
+
 /* ********************** button apply/revert ************************/
 
 static ListBase UIAfterFuncs = {NULL, NULL};
@@ -1572,6 +1586,15 @@
 static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
 {
 	if(but) {
+		if(ui_is_utf8_but(but)) {
+			int strip= BLI_utf8_invalid_strip(but->editstr, strlen(but->editstr));
+			/* not a file?, strip non utf-8 chars */
+			if(strip) {
+				/* wont happen often so isnt that annoying to keep it here for a while */
+				printf("invalid utf8 - stripped chars %d\n", strip);
+			}
+		}
+		
 		if(data->searchbox) {
 			if(data->cancel==0)
 				ui_searchbox_apply(but, data->searchbox);

Modified: trunk/blender/source/blender/editors/space_file/file_draw.c
===================================================================
--- trunk/blender/source/blender/editors/space_file/file_draw.c	2010-08-28 12:15:14 UTC (rev 31629)
+++ trunk/blender/source/blender/editors/space_file/file_draw.c	2010-08-28 12:34:22 UTC (rev 31630)
@@ -177,11 +177,14 @@
 				 params->dir, 0.0, (float)FILE_MAX-1, 0, 0, 
 				 "File path.");
 		uiButSetCompleteFunc(but, autocomplete_directory, NULL);
+		uiButSetFlag(but, UI_BUT_NO_UTF8);
+
 		but = uiDefBut(block, TEX, B_FS_FILENAME, "",
 				 min_x, line2_y, line2_w-chan_offs, btn_h,
 				 params->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, 
 				 "File name.");
 		uiButSetCompleteFunc(but, autocomplete_file, NULL);
+		uiButSetFlag(but, UI_BUT_NO_UTF8);
 	}
 	
 	/* Filename number increment / decrement buttons. */

Modified: trunk/blender/source/blender/makesrna/intern/rna_render.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_render.c	2010-08-28 12:15:14 UTC (rev 31629)
+++ trunk/blender/source/blender/makesrna/intern/rna_render.c	2010-08-28 12:34:22 UTC (rev 31630)
@@ -291,8 +291,8 @@
 static void rna_def_render_result(BlenderRNA *brna)
 {
 	StructRNA *srna;
-	PropertyRNA *prop;
 	FunctionRNA *func;
+	PropertyRNA *parm;
 	
 	srna= RNA_def_struct(brna, "RenderResult", NULL);
 	RNA_def_struct_ui_text(srna, "Render Result", "Result of rendering, including all layers and passes");
@@ -300,22 +300,22 @@
 	func= RNA_def_function(srna, "load_from_file", "RE_result_load_from_file");
 	RNA_def_function_ui_description(func, "Copies the pixels of this render result from an image file.");
 	RNA_def_function_flag(func, FUNC_USE_REPORTS);
-	prop= RNA_def_string(func, "filename", "", 0, "Filename", "Filename to load into this render tile, must be no smaller then the render result");
-	RNA_def_property_flag(prop, PROP_REQUIRED);
+	parm= RNA_def_string_file_name(func, "filename", "", FILE_MAX, "File Name", "Filename to load into this render tile, must be no smaller then the render result");
+	RNA_def_property_flag(parm, PROP_REQUIRED);
 
 	RNA_define_verify_sdna(0);
 
-	prop= RNA_def_property(srna, "resolution_x", PROP_INT, PROP_NONE);
-	RNA_def_property_int_sdna(prop, NULL, "rectx");
-	RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+	parm= RNA_def_property(srna, "resolution_x", PROP_INT, PROP_NONE);
+	RNA_def_property_int_sdna(parm, NULL, "rectx");
+	RNA_def_property_clear_flag(parm, PROP_EDITABLE);
 
-	prop= RNA_def_property(srna, "resolution_y", PROP_INT, PROP_NONE);
-	RNA_def_property_int_sdna(prop, NULL, "recty");
-	RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+	parm= RNA_def_property(srna, "resolution_y", PROP_INT, PROP_NONE);
+	RNA_def_property_int_sdna(parm, NULL, "recty");
+	RNA_def_property_clear_flag(parm, PROP_EDITABLE);
 
-	prop= RNA_def_property(srna, "layers", PROP_COLLECTION, PROP_NONE);
-	RNA_def_property_struct_type(prop, "RenderLayer");
-	RNA_def_property_collection_funcs(prop, "rna_RenderResult_layers_begin", "rna_iterator_listbase_next", "rna_iterator_listbase_end", "rna_iterator_listbase_get", 0, 0, 0);
+	parm= RNA_def_property(srna, "layers", PROP_COLLECTION, PROP_NONE);
+	RNA_def_property_struct_type(parm, "RenderLayer");
+	RNA_def_property_collection_funcs(parm, "rna_RenderResult_layers_begin", "rna_iterator_listbase_next", "rna_iterator_listbase_end", "rna_iterator_listbase_get", 0, 0, 0);
 
 	RNA_define_verify_sdna(1);
 }


@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list