[Bf-blender-cvs] [0e07cae] tmp_text_copy_paste: Fixups to handle utf8 strings properly

Dalai Felinto noreply at git.blender.org
Thu Feb 11 19:48:08 CET 2016


Commit: 0e07cae8f3147470cdf775cb280216d6c3106125
Author: Dalai Felinto
Date:   Thu Feb 11 15:27:03 2016 -0200
Branches: tmp_text_copy_paste
https://developer.blender.org/rB0e07cae8f3147470cdf775cb280216d6c3106125

Fixups to handle utf8 strings properly

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

M	source/blender/blenkernel/BKE_font.h
M	source/blender/editors/curve/editfont.c

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

diff --git a/source/blender/blenkernel/BKE_font.h b/source/blender/blenkernel/BKE_font.h
index 5dcc6f8..996c176 100644
--- a/source/blender/blenkernel/BKE_font.h
+++ b/source/blender/blenkernel/BKE_font.h
@@ -56,9 +56,6 @@ typedef struct EditFontSelBox {
 } EditFontSelBox;
 
 typedef struct EditFont {
-	wchar_t *copybuf;
-	struct CharInfo *copybufinfo;
-	
 	wchar_t *textbuf;
 	struct CharInfo *textbufinfo;
 	
diff --git a/source/blender/editors/curve/editfont.c b/source/blender/editors/curve/editfont.c
index 84e214f..9b01cf1 100644
--- a/source/blender/editors/curve/editfont.c
+++ b/source/blender/editors/curve/editfont.c
@@ -710,12 +710,14 @@ static void copy_selection(Object *obedit)
 
 		/* internal clipboard (for style) */
 		memcpy(&G.copybuf[0], ef->textbuf + selstart, len * sizeof(wchar_t));
-		G.copybuf[(selend - selstart) + 1] = 0;
-		memcpy(&G.copybufinfo[0], ef->textbufinfo + selstart, ((selend - selstart) + 1) * sizeof(CharInfo));
+		G.copybuf[len] = 0;
+		memcpy(&G.copybufinfo[0], ef->textbufinfo + selstart, len * sizeof(CharInfo));
+
+		len = BLI_wstrlen_utf8(&G.copybuf[0]);
 
 		/* system clipboard */
-		buf = MEM_mallocN(len * sizeof(char), __func__);
-		BLI_strncpy_wchar_as_utf8(buf, ef->textbuf + selstart, len - 1);
+		buf = MEM_mallocN(len + sizeof(wchar_t), __func__);
+		BLI_strncpy_wchar_as_utf8(buf, &G.copybuf[0], len);
 		WM_clipboard_text_set(buf, false);
 	}
 
@@ -782,9 +784,7 @@ void FONT_OT_text_cut(wmOperatorType *ot)
 
 static bool paste_selection(Object *obedit, ReportList *reports)
 {
-	Curve *cu = obedit->data;
-	EditFont *ef = cu->editfont;
-	int len = wcslen(ef->copybuf);
+	int len = wcslen(&G.copybuf[0]);
 
 	if (font_paste_wchar(obedit, &G.copybuf[0], len, &G.copybufinfo[0])) {
 		return true;
@@ -800,6 +800,7 @@ static int paste_text_exec(bContext *C, wmOperator *op)
 	Object *obedit = CTX_data_edit_object(C);
 	char *strp, *buf = NULL;
 	int len, retval;
+	size_t len_utf8;
 
 	/* get text from system */
 	strp = WM_clipboard_text_get(false, &len);
@@ -810,14 +811,16 @@ static int paste_text_exec(bContext *C, wmOperator *op)
 	}
 
 	/* get text from internal buffer */
-	buf = MEM_mallocN(len * sizeof(char), __func__);
-	BLI_strncpy_wchar_as_utf8(buf, &G.copybuf[0], len - 1);
+	len_utf8 = BLI_wstrlen_utf8(&G.copybuf[0]);
+	buf = MEM_mallocN(len_utf8 + sizeof(wchar_t), __func__);
+	BLI_strncpy_wchar_as_utf8(buf, &G.copybuf[0], len_utf8 + 1);
 
-	/* if they match, get textinfo from internall buffer */
-	if (STREQLEN(strp, buf, len)) {
+	if (STREQ(strp, buf)) {
+		/* get textinfo from internal buffer */
 		retval = paste_selection(obedit, op->reports) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
 	}
 	else {
+		/* get text from clipboard */
 		if ((len <= MAXTEXT) && font_paste_utf8(C, strp, len)) {
 			text_update_edited(C, obedit, FO_EDIT);
 			retval = OPERATOR_FINISHED;
@@ -826,6 +829,9 @@ static int paste_text_exec(bContext *C, wmOperator *op)
 			BKE_report(op->reports, RPT_ERROR, "Clipboard too long");
 			retval = OPERATOR_CANCELLED;
 		}
+
+		/* free the buffer */
+		G.copybuf[0] = 0;
 	}
 
 	if (retval != OPERATOR_CANCELLED) {




More information about the Bf-blender-cvs mailing list